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/kthread.h> |
| 22 | #include <linux/hardirq.h> |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 23 | #include <linux/ftrace.h> |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 25 | #include <linux/sysctl.h> |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 26 | #include <linux/hash.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 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 30 | #include "trace.h" |
| 31 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 32 | int ftrace_enabled; |
| 33 | static int last_ftrace_enabled; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 34 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | static DEFINE_SPINLOCK(ftrace_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 36 | static DEFINE_MUTEX(ftrace_sysctl_lock); |
| 37 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 38 | static struct ftrace_ops ftrace_list_end __read_mostly = |
| 39 | { |
| 40 | .func = ftrace_stub, |
| 41 | }; |
| 42 | |
| 43 | static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; |
| 44 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
| 45 | |
| 46 | /* mcount is defined per arch in assembly */ |
| 47 | EXPORT_SYMBOL(mcount); |
| 48 | |
| 49 | notrace void ftrace_list_func(unsigned long ip, unsigned long parent_ip) |
| 50 | { |
| 51 | struct ftrace_ops *op = ftrace_list; |
| 52 | |
| 53 | /* in case someone actually ports this to alpha! */ |
| 54 | read_barrier_depends(); |
| 55 | |
| 56 | while (op != &ftrace_list_end) { |
| 57 | /* silly alpha */ |
| 58 | read_barrier_depends(); |
| 59 | op->func(ip, parent_ip); |
| 60 | op = op->next; |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | /** |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 65 | * clear_ftrace_function - reset the ftrace function |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 66 | * |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 67 | * This NULLs the ftrace function and in essence stops |
| 68 | * tracing. There may be lag |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 69 | */ |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 70 | void clear_ftrace_function(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 71 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 72 | ftrace_trace_function = ftrace_stub; |
| 73 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 74 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 75 | static int notrace __register_ftrace_function(struct ftrace_ops *ops) |
| 76 | { |
| 77 | /* Should never be called by interrupts */ |
| 78 | spin_lock(&ftrace_lock); |
| 79 | |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 80 | ops->next = ftrace_list; |
| 81 | /* |
| 82 | * We are entering ops into the ftrace_list but another |
| 83 | * CPU might be walking that list. We need to make sure |
| 84 | * the ops->next pointer is valid before another CPU sees |
| 85 | * the ops pointer included into the ftrace_list. |
| 86 | */ |
| 87 | smp_wmb(); |
| 88 | ftrace_list = ops; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 89 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 90 | if (ftrace_enabled) { |
| 91 | /* |
| 92 | * For one func, simply call it directly. |
| 93 | * For more than one func, call the chain. |
| 94 | */ |
| 95 | if (ops->next == &ftrace_list_end) |
| 96 | ftrace_trace_function = ops->func; |
| 97 | else |
| 98 | ftrace_trace_function = ftrace_list_func; |
| 99 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 100 | |
| 101 | spin_unlock(&ftrace_lock); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 106 | static int notrace __unregister_ftrace_function(struct ftrace_ops *ops) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 107 | { |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 108 | struct ftrace_ops **p; |
| 109 | int ret = 0; |
| 110 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 111 | spin_lock(&ftrace_lock); |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 112 | |
| 113 | /* |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 114 | * If we are removing the last function, then simply point |
| 115 | * to the ftrace_stub. |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 116 | */ |
| 117 | if (ftrace_list == ops && ops->next == &ftrace_list_end) { |
| 118 | ftrace_trace_function = ftrace_stub; |
| 119 | ftrace_list = &ftrace_list_end; |
| 120 | goto out; |
| 121 | } |
| 122 | |
| 123 | for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) |
| 124 | if (*p == ops) |
| 125 | break; |
| 126 | |
| 127 | if (*p != ops) { |
| 128 | ret = -1; |
| 129 | goto out; |
| 130 | } |
| 131 | |
| 132 | *p = (*p)->next; |
| 133 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 134 | if (ftrace_enabled) { |
| 135 | /* If we only have one func left, then call that directly */ |
| 136 | if (ftrace_list == &ftrace_list_end || |
| 137 | ftrace_list->next == &ftrace_list_end) |
| 138 | ftrace_trace_function = ftrace_list->func; |
| 139 | } |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 140 | |
| 141 | out: |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 142 | spin_unlock(&ftrace_lock); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 148 | |
Steven Rostedt | e1c08bd | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 149 | static struct task_struct *ftraced_task; |
| 150 | static DECLARE_WAIT_QUEUE_HEAD(ftraced_waiters); |
| 151 | static unsigned long ftraced_iteration_counter; |
| 152 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 153 | enum { |
| 154 | FTRACE_ENABLE_CALLS = (1 << 0), |
| 155 | FTRACE_DISABLE_CALLS = (1 << 1), |
| 156 | FTRACE_UPDATE_TRACE_FUNC = (1 << 2), |
| 157 | FTRACE_ENABLE_MCOUNT = (1 << 3), |
| 158 | FTRACE_DISABLE_MCOUNT = (1 << 4), |
| 159 | }; |
| 160 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 161 | static int ftrace_filtered; |
| 162 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 163 | static struct hlist_head ftrace_hash[FTRACE_HASHSIZE]; |
| 164 | |
| 165 | static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu); |
| 166 | |
| 167 | static DEFINE_SPINLOCK(ftrace_shutdown_lock); |
| 168 | static DEFINE_MUTEX(ftraced_lock); |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 169 | static DEFINE_MUTEX(ftrace_filter_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 170 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 171 | struct ftrace_page { |
| 172 | struct ftrace_page *next; |
| 173 | int index; |
| 174 | struct dyn_ftrace records[]; |
| 175 | } __attribute__((packed)); |
| 176 | |
| 177 | #define ENTRIES_PER_PAGE \ |
| 178 | ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace)) |
| 179 | |
| 180 | /* estimate from running different kernels */ |
| 181 | #define NR_TO_INIT 10000 |
| 182 | |
| 183 | static struct ftrace_page *ftrace_pages_start; |
| 184 | static struct ftrace_page *ftrace_pages; |
| 185 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 186 | static int ftraced_trigger; |
| 187 | static int ftraced_suspend; |
| 188 | |
| 189 | static int ftrace_record_suspend; |
| 190 | |
| 191 | static inline int |
| 192 | notrace ftrace_ip_in_hash(unsigned long ip, unsigned long key) |
| 193 | { |
| 194 | struct dyn_ftrace *p; |
| 195 | struct hlist_node *t; |
| 196 | int found = 0; |
| 197 | |
| 198 | hlist_for_each_entry(p, t, &ftrace_hash[key], node) { |
| 199 | if (p->ip == ip) { |
| 200 | found = 1; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return found; |
| 206 | } |
| 207 | |
| 208 | static inline void notrace |
| 209 | ftrace_add_hash(struct dyn_ftrace *node, unsigned long key) |
| 210 | { |
| 211 | hlist_add_head(&node->node, &ftrace_hash[key]); |
| 212 | } |
| 213 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 214 | static notrace struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 215 | { |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 216 | if (ftrace_pages->index == ENTRIES_PER_PAGE) { |
| 217 | if (!ftrace_pages->next) |
| 218 | return NULL; |
| 219 | ftrace_pages = ftrace_pages->next; |
| 220 | } |
| 221 | |
| 222 | return &ftrace_pages->records[ftrace_pages->index++]; |
| 223 | } |
| 224 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 225 | static void notrace |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 226 | ftrace_record_ip(unsigned long ip) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 227 | { |
| 228 | struct dyn_ftrace *node; |
| 229 | unsigned long flags; |
| 230 | unsigned long key; |
| 231 | int resched; |
| 232 | int atomic; |
| 233 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 234 | if (!ftrace_enabled) |
| 235 | return; |
| 236 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 237 | resched = need_resched(); |
| 238 | preempt_disable_notrace(); |
| 239 | |
| 240 | /* We simply need to protect against recursion */ |
| 241 | __get_cpu_var(ftrace_shutdown_disable_cpu)++; |
| 242 | if (__get_cpu_var(ftrace_shutdown_disable_cpu) != 1) |
| 243 | goto out; |
| 244 | |
| 245 | if (unlikely(ftrace_record_suspend)) |
| 246 | goto out; |
| 247 | |
| 248 | key = hash_long(ip, FTRACE_HASHBITS); |
| 249 | |
| 250 | WARN_ON_ONCE(key >= FTRACE_HASHSIZE); |
| 251 | |
| 252 | if (ftrace_ip_in_hash(ip, key)) |
| 253 | goto out; |
| 254 | |
| 255 | atomic = irqs_disabled(); |
| 256 | |
| 257 | spin_lock_irqsave(&ftrace_shutdown_lock, flags); |
| 258 | |
| 259 | /* This ip may have hit the hash before the lock */ |
| 260 | if (ftrace_ip_in_hash(ip, key)) |
| 261 | goto out_unlock; |
| 262 | |
| 263 | /* |
| 264 | * There's a slight race that the ftraced will update the |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 265 | * hash and reset here. If it is already converted, skip it. |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 266 | */ |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 267 | if (ftrace_ip_converted(ip)) |
| 268 | goto out_unlock; |
| 269 | |
| 270 | node = ftrace_alloc_dyn_node(ip); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 271 | if (!node) |
| 272 | goto out_unlock; |
| 273 | |
| 274 | node->ip = ip; |
| 275 | |
| 276 | ftrace_add_hash(node, key); |
| 277 | |
| 278 | ftraced_trigger = 1; |
| 279 | |
| 280 | out_unlock: |
| 281 | spin_unlock_irqrestore(&ftrace_shutdown_lock, flags); |
| 282 | out: |
| 283 | __get_cpu_var(ftrace_shutdown_disable_cpu)--; |
| 284 | |
| 285 | /* prevent recursion with scheduler */ |
| 286 | if (resched) |
| 287 | preempt_enable_no_resched_notrace(); |
| 288 | else |
| 289 | preempt_enable_notrace(); |
| 290 | } |
| 291 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 292 | #define FTRACE_ADDR ((long)(&ftrace_caller)) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 293 | #define MCOUNT_ADDR ((long)(&mcount)) |
| 294 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 295 | static void notrace |
| 296 | __ftrace_replace_code(struct dyn_ftrace *rec, |
| 297 | unsigned char *old, unsigned char *new, int enable) |
| 298 | { |
| 299 | unsigned long ip; |
| 300 | int failed; |
| 301 | |
| 302 | ip = rec->ip; |
| 303 | |
| 304 | if (ftrace_filtered && enable) { |
| 305 | unsigned long fl; |
| 306 | /* |
| 307 | * If filtering is on: |
| 308 | * |
| 309 | * If this record is set to be filtered and |
| 310 | * is enabled then do nothing. |
| 311 | * |
| 312 | * If this record is set to be filtered and |
| 313 | * it is not enabled, enable it. |
| 314 | * |
| 315 | * If this record is not set to be filtered |
| 316 | * and it is not enabled do nothing. |
| 317 | * |
| 318 | * If this record is not set to be filtered and |
| 319 | * it is enabled, disable it. |
| 320 | */ |
| 321 | fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED); |
| 322 | |
| 323 | if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) || |
| 324 | (fl == 0)) |
| 325 | return; |
| 326 | |
| 327 | /* |
| 328 | * If it is enabled disable it, |
| 329 | * otherwise enable it! |
| 330 | */ |
| 331 | if (fl == FTRACE_FL_ENABLED) { |
| 332 | /* swap new and old */ |
| 333 | new = old; |
| 334 | old = ftrace_call_replace(ip, FTRACE_ADDR); |
| 335 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 336 | } else { |
| 337 | new = ftrace_call_replace(ip, FTRACE_ADDR); |
| 338 | rec->flags |= FTRACE_FL_ENABLED; |
| 339 | } |
| 340 | } else { |
| 341 | |
| 342 | if (enable) |
| 343 | new = ftrace_call_replace(ip, FTRACE_ADDR); |
| 344 | else |
| 345 | old = ftrace_call_replace(ip, FTRACE_ADDR); |
| 346 | |
| 347 | if (enable) { |
| 348 | if (rec->flags & FTRACE_FL_ENABLED) |
| 349 | return; |
| 350 | rec->flags |= FTRACE_FL_ENABLED; |
| 351 | } else { |
| 352 | if (!(rec->flags & FTRACE_FL_ENABLED)) |
| 353 | return; |
| 354 | rec->flags &= ~FTRACE_FL_ENABLED; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | failed = ftrace_modify_code(ip, old, new); |
| 359 | if (failed) |
| 360 | rec->flags |= FTRACE_FL_FAILED; |
| 361 | } |
| 362 | |
| 363 | static void notrace ftrace_replace_code(int enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 364 | { |
| 365 | unsigned char *new = NULL, *old = NULL; |
| 366 | struct dyn_ftrace *rec; |
| 367 | struct ftrace_page *pg; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 368 | int i; |
| 369 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 370 | if (enable) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 371 | old = ftrace_nop_replace(); |
| 372 | else |
| 373 | new = ftrace_nop_replace(); |
| 374 | |
| 375 | for (pg = ftrace_pages_start; pg; pg = pg->next) { |
| 376 | for (i = 0; i < pg->index; i++) { |
| 377 | rec = &pg->records[i]; |
| 378 | |
| 379 | /* don't modify code that has already faulted */ |
| 380 | if (rec->flags & FTRACE_FL_FAILED) |
| 381 | continue; |
| 382 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 383 | __ftrace_replace_code(rec, old, new, enable); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 388 | static notrace void ftrace_shutdown_replenish(void) |
| 389 | { |
| 390 | if (ftrace_pages->next) |
| 391 | return; |
| 392 | |
| 393 | /* allocate another page */ |
| 394 | ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL); |
| 395 | } |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 396 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 397 | static notrace void |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 398 | ftrace_code_disable(struct dyn_ftrace *rec) |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 399 | { |
| 400 | unsigned long ip; |
| 401 | unsigned char *nop, *call; |
| 402 | int failed; |
| 403 | |
| 404 | ip = rec->ip; |
| 405 | |
| 406 | nop = ftrace_nop_replace(); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 407 | call = ftrace_call_replace(ip, MCOUNT_ADDR); |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 408 | |
| 409 | failed = ftrace_modify_code(ip, call, nop); |
| 410 | if (failed) |
| 411 | rec->flags |= FTRACE_FL_FAILED; |
| 412 | } |
| 413 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 414 | static int notrace __ftrace_modify_code(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 415 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 416 | unsigned long addr; |
| 417 | int *command = data; |
| 418 | |
| 419 | if (*command & FTRACE_ENABLE_CALLS) |
| 420 | ftrace_replace_code(1); |
| 421 | else if (*command & FTRACE_DISABLE_CALLS) |
| 422 | ftrace_replace_code(0); |
| 423 | |
| 424 | if (*command & FTRACE_UPDATE_TRACE_FUNC) |
| 425 | ftrace_update_ftrace_func(ftrace_trace_function); |
| 426 | |
| 427 | if (*command & FTRACE_ENABLE_MCOUNT) { |
| 428 | addr = (unsigned long)ftrace_record_ip; |
| 429 | ftrace_mcount_set(&addr); |
| 430 | } else if (*command & FTRACE_DISABLE_MCOUNT) { |
| 431 | addr = (unsigned long)ftrace_stub; |
| 432 | ftrace_mcount_set(&addr); |
| 433 | } |
| 434 | |
| 435 | return 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 438 | static void notrace ftrace_run_update_code(int command) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 439 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 440 | stop_machine_run(__ftrace_modify_code, &command, NR_CPUS); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 443 | static ftrace_func_t saved_ftrace_func; |
| 444 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 445 | static void notrace ftrace_startup(void) |
| 446 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 447 | int command = 0; |
| 448 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 449 | mutex_lock(&ftraced_lock); |
| 450 | ftraced_suspend++; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 451 | if (ftraced_suspend == 1) |
| 452 | command |= FTRACE_ENABLE_CALLS; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 453 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 454 | if (saved_ftrace_func != ftrace_trace_function) { |
| 455 | saved_ftrace_func = ftrace_trace_function; |
| 456 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 457 | } |
| 458 | |
| 459 | if (!command || !ftrace_enabled) |
| 460 | goto out; |
| 461 | |
| 462 | ftrace_run_update_code(command); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 463 | out: |
| 464 | mutex_unlock(&ftraced_lock); |
| 465 | } |
| 466 | |
| 467 | static void notrace ftrace_shutdown(void) |
| 468 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 469 | int command = 0; |
| 470 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 471 | mutex_lock(&ftraced_lock); |
| 472 | ftraced_suspend--; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 473 | if (!ftraced_suspend) |
| 474 | command |= FTRACE_DISABLE_CALLS; |
| 475 | |
| 476 | if (saved_ftrace_func != ftrace_trace_function) { |
| 477 | saved_ftrace_func = ftrace_trace_function; |
| 478 | command |= FTRACE_UPDATE_TRACE_FUNC; |
| 479 | } |
| 480 | |
| 481 | if (!command || !ftrace_enabled) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 482 | goto out; |
| 483 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 484 | ftrace_run_update_code(command); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 485 | out: |
| 486 | mutex_unlock(&ftraced_lock); |
| 487 | } |
| 488 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 489 | static void notrace ftrace_startup_sysctl(void) |
| 490 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 491 | int command = FTRACE_ENABLE_MCOUNT; |
| 492 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 493 | mutex_lock(&ftraced_lock); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 494 | /* Force update next time */ |
| 495 | saved_ftrace_func = NULL; |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 496 | /* ftraced_suspend is true if we want ftrace running */ |
| 497 | if (ftraced_suspend) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 498 | command |= FTRACE_ENABLE_CALLS; |
| 499 | |
| 500 | ftrace_run_update_code(command); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 501 | mutex_unlock(&ftraced_lock); |
| 502 | } |
| 503 | |
| 504 | static void notrace ftrace_shutdown_sysctl(void) |
| 505 | { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 506 | int command = FTRACE_DISABLE_MCOUNT; |
| 507 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 508 | mutex_lock(&ftraced_lock); |
| 509 | /* ftraced_suspend is true if ftrace is running */ |
| 510 | if (ftraced_suspend) |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 511 | command |= FTRACE_DISABLE_CALLS; |
| 512 | |
| 513 | ftrace_run_update_code(command); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 514 | mutex_unlock(&ftraced_lock); |
| 515 | } |
| 516 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 517 | static cycle_t ftrace_update_time; |
| 518 | static unsigned long ftrace_update_cnt; |
| 519 | unsigned long ftrace_update_tot_cnt; |
| 520 | |
| 521 | static int notrace __ftrace_update_code(void *ignore) |
| 522 | { |
| 523 | struct dyn_ftrace *p; |
| 524 | struct hlist_head head; |
| 525 | struct hlist_node *t; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 526 | int save_ftrace_enabled; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 527 | cycle_t start, stop; |
| 528 | int i; |
| 529 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 530 | /* Don't be recording funcs now */ |
| 531 | save_ftrace_enabled = ftrace_enabled; |
| 532 | ftrace_enabled = 0; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 533 | |
| 534 | start = now(raw_smp_processor_id()); |
| 535 | ftrace_update_cnt = 0; |
| 536 | |
| 537 | /* No locks needed, the machine is stopped! */ |
| 538 | for (i = 0; i < FTRACE_HASHSIZE; i++) { |
| 539 | if (hlist_empty(&ftrace_hash[i])) |
| 540 | continue; |
| 541 | |
| 542 | head = ftrace_hash[i]; |
| 543 | INIT_HLIST_HEAD(&ftrace_hash[i]); |
| 544 | |
| 545 | /* all CPUS are stopped, we are safe to modify code */ |
| 546 | hlist_for_each_entry(p, t, &head, node) { |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 547 | ftrace_code_disable(p); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 548 | ftrace_update_cnt++; |
| 549 | } |
| 550 | |
| 551 | } |
| 552 | |
| 553 | stop = now(raw_smp_processor_id()); |
| 554 | ftrace_update_time = stop - start; |
| 555 | ftrace_update_tot_cnt += ftrace_update_cnt; |
| 556 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 557 | ftrace_enabled = save_ftrace_enabled; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 562 | static void notrace ftrace_update_code(void) |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 563 | { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 564 | stop_machine_run(__ftrace_update_code, NULL, NR_CPUS); |
| 565 | } |
| 566 | |
| 567 | static int notrace ftraced(void *ignore) |
| 568 | { |
| 569 | unsigned long usecs; |
| 570 | |
| 571 | set_current_state(TASK_INTERRUPTIBLE); |
| 572 | |
| 573 | while (!kthread_should_stop()) { |
| 574 | |
| 575 | /* check once a second */ |
| 576 | schedule_timeout(HZ); |
| 577 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 578 | mutex_lock(&ftrace_sysctl_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 579 | mutex_lock(&ftraced_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 580 | if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) { |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 581 | ftrace_record_suspend++; |
| 582 | ftrace_update_code(); |
| 583 | usecs = nsecs_to_usecs(ftrace_update_time); |
| 584 | if (ftrace_update_tot_cnt > 100000) { |
| 585 | ftrace_update_tot_cnt = 0; |
| 586 | pr_info("hm, dftrace overflow: %lu change%s" |
| 587 | " (%lu total) in %lu usec%s\n", |
| 588 | ftrace_update_cnt, |
| 589 | ftrace_update_cnt != 1 ? "s" : "", |
| 590 | ftrace_update_tot_cnt, |
| 591 | usecs, usecs != 1 ? "s" : ""); |
| 592 | WARN_ON_ONCE(1); |
| 593 | } |
| 594 | ftraced_trigger = 0; |
| 595 | ftrace_record_suspend--; |
| 596 | } |
Steven Rostedt | e1c08bd | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 597 | ftraced_iteration_counter++; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 598 | mutex_unlock(&ftraced_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 599 | mutex_unlock(&ftrace_sysctl_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 600 | |
Steven Rostedt | e1c08bd | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 601 | wake_up_interruptible(&ftraced_waiters); |
| 602 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 603 | ftrace_shutdown_replenish(); |
| 604 | |
| 605 | set_current_state(TASK_INTERRUPTIBLE); |
| 606 | } |
| 607 | __set_current_state(TASK_RUNNING); |
| 608 | return 0; |
| 609 | } |
| 610 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 611 | static int __init ftrace_dyn_table_alloc(void) |
| 612 | { |
| 613 | struct ftrace_page *pg; |
| 614 | int cnt; |
| 615 | int i; |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 616 | |
| 617 | /* allocate a few pages */ |
| 618 | ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL); |
| 619 | if (!ftrace_pages_start) |
| 620 | return -1; |
| 621 | |
| 622 | /* |
| 623 | * Allocate a few more pages. |
| 624 | * |
| 625 | * TODO: have some parser search vmlinux before |
| 626 | * final linking to find all calls to ftrace. |
| 627 | * Then we can: |
| 628 | * a) know how many pages to allocate. |
| 629 | * and/or |
| 630 | * b) set up the table then. |
| 631 | * |
| 632 | * The dynamic code is still necessary for |
| 633 | * modules. |
| 634 | */ |
| 635 | |
| 636 | pg = ftrace_pages = ftrace_pages_start; |
| 637 | |
| 638 | cnt = NR_TO_INIT / ENTRIES_PER_PAGE; |
| 639 | |
| 640 | for (i = 0; i < cnt; i++) { |
| 641 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
| 642 | |
| 643 | /* If we fail, we'll try later anyway */ |
| 644 | if (!pg->next) |
| 645 | break; |
| 646 | |
| 647 | pg = pg->next; |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 653 | enum { |
| 654 | FTRACE_ITER_FILTER = (1 << 0), |
| 655 | FTRACE_ITER_CONT = (1 << 1), |
| 656 | }; |
| 657 | |
| 658 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
| 659 | |
| 660 | struct ftrace_iterator { |
| 661 | loff_t pos; |
| 662 | struct ftrace_page *pg; |
| 663 | unsigned idx; |
| 664 | unsigned flags; |
| 665 | unsigned char buffer[FTRACE_BUFF_MAX+1]; |
| 666 | unsigned buffer_idx; |
| 667 | unsigned filtered; |
| 668 | }; |
| 669 | |
| 670 | static void notrace * |
| 671 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 672 | { |
| 673 | struct ftrace_iterator *iter = m->private; |
| 674 | struct dyn_ftrace *rec = NULL; |
| 675 | |
| 676 | (*pos)++; |
| 677 | |
| 678 | retry: |
| 679 | if (iter->idx >= iter->pg->index) { |
| 680 | if (iter->pg->next) { |
| 681 | iter->pg = iter->pg->next; |
| 682 | iter->idx = 0; |
| 683 | goto retry; |
| 684 | } |
| 685 | } else { |
| 686 | rec = &iter->pg->records[iter->idx++]; |
| 687 | if ((rec->flags & FTRACE_FL_FAILED) || |
| 688 | ((iter->flags & FTRACE_ITER_FILTER) && |
| 689 | !(rec->flags & FTRACE_FL_FILTER))) { |
| 690 | rec = NULL; |
| 691 | goto retry; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | iter->pos = *pos; |
| 696 | |
| 697 | return rec; |
| 698 | } |
| 699 | |
| 700 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 701 | { |
| 702 | struct ftrace_iterator *iter = m->private; |
| 703 | void *p = NULL; |
| 704 | loff_t l = -1; |
| 705 | |
| 706 | if (*pos != iter->pos) { |
| 707 | for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l)) |
| 708 | ; |
| 709 | } else { |
| 710 | l = *pos; |
| 711 | p = t_next(m, p, &l); |
| 712 | } |
| 713 | |
| 714 | return p; |
| 715 | } |
| 716 | |
| 717 | static void t_stop(struct seq_file *m, void *p) |
| 718 | { |
| 719 | } |
| 720 | |
| 721 | static int t_show(struct seq_file *m, void *v) |
| 722 | { |
| 723 | struct dyn_ftrace *rec = v; |
| 724 | char str[KSYM_SYMBOL_LEN]; |
| 725 | |
| 726 | if (!rec) |
| 727 | return 0; |
| 728 | |
| 729 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
| 730 | |
| 731 | seq_printf(m, "%s\n", str); |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static struct seq_operations show_ftrace_seq_ops = { |
| 737 | .start = t_start, |
| 738 | .next = t_next, |
| 739 | .stop = t_stop, |
| 740 | .show = t_show, |
| 741 | }; |
| 742 | |
| 743 | static int notrace |
| 744 | ftrace_avail_open(struct inode *inode, struct file *file) |
| 745 | { |
| 746 | struct ftrace_iterator *iter; |
| 747 | int ret; |
| 748 | |
| 749 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 750 | if (!iter) |
| 751 | return -ENOMEM; |
| 752 | |
| 753 | iter->pg = ftrace_pages_start; |
| 754 | iter->pos = -1; |
| 755 | |
| 756 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 757 | if (!ret) { |
| 758 | struct seq_file *m = file->private_data; |
| 759 | m->private = iter; |
| 760 | } else |
| 761 | kfree(iter); |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | int ftrace_avail_release(struct inode *inode, struct file *file) |
| 767 | { |
| 768 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 769 | struct ftrace_iterator *iter = m->private; |
| 770 | |
| 771 | seq_release(inode, file); |
| 772 | kfree(iter); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static void notrace ftrace_filter_reset(void) |
| 777 | { |
| 778 | struct ftrace_page *pg; |
| 779 | struct dyn_ftrace *rec; |
| 780 | unsigned i; |
| 781 | |
| 782 | /* keep kstop machine from running */ |
| 783 | preempt_disable(); |
| 784 | ftrace_filtered = 0; |
| 785 | pg = ftrace_pages_start; |
| 786 | while (pg) { |
| 787 | for (i = 0; i < pg->index; i++) { |
| 788 | rec = &pg->records[i]; |
| 789 | if (rec->flags & FTRACE_FL_FAILED) |
| 790 | continue; |
| 791 | rec->flags &= ~FTRACE_FL_FILTER; |
| 792 | } |
| 793 | pg = pg->next; |
| 794 | } |
| 795 | preempt_enable(); |
| 796 | } |
| 797 | |
| 798 | static int notrace |
| 799 | ftrace_filter_open(struct inode *inode, struct file *file) |
| 800 | { |
| 801 | struct ftrace_iterator *iter; |
| 802 | int ret = 0; |
| 803 | |
| 804 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 805 | if (!iter) |
| 806 | return -ENOMEM; |
| 807 | |
| 808 | mutex_lock(&ftrace_filter_lock); |
| 809 | if ((file->f_mode & FMODE_WRITE) && |
| 810 | !(file->f_flags & O_APPEND)) |
| 811 | ftrace_filter_reset(); |
| 812 | |
| 813 | if (file->f_mode & FMODE_READ) { |
| 814 | iter->pg = ftrace_pages_start; |
| 815 | iter->pos = -1; |
| 816 | iter->flags = FTRACE_ITER_FILTER; |
| 817 | |
| 818 | ret = seq_open(file, &show_ftrace_seq_ops); |
| 819 | if (!ret) { |
| 820 | struct seq_file *m = file->private_data; |
| 821 | m->private = iter; |
| 822 | } else |
| 823 | kfree(iter); |
| 824 | } else |
| 825 | file->private_data = iter; |
| 826 | mutex_unlock(&ftrace_filter_lock); |
| 827 | |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | static ssize_t notrace |
| 832 | ftrace_filter_read(struct file *file, char __user *ubuf, |
| 833 | size_t cnt, loff_t *ppos) |
| 834 | { |
| 835 | if (file->f_mode & FMODE_READ) |
| 836 | return seq_read(file, ubuf, cnt, ppos); |
| 837 | else |
| 838 | return -EPERM; |
| 839 | } |
| 840 | |
| 841 | static loff_t notrace |
| 842 | ftrace_filter_lseek(struct file *file, loff_t offset, int origin) |
| 843 | { |
| 844 | loff_t ret; |
| 845 | |
| 846 | if (file->f_mode & FMODE_READ) |
| 847 | ret = seq_lseek(file, offset, origin); |
| 848 | else |
| 849 | file->f_pos = ret = 1; |
| 850 | |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | enum { |
| 855 | MATCH_FULL, |
| 856 | MATCH_FRONT_ONLY, |
| 857 | MATCH_MIDDLE_ONLY, |
| 858 | MATCH_END_ONLY, |
| 859 | }; |
| 860 | |
| 861 | static void notrace |
| 862 | ftrace_match(unsigned char *buff, int len) |
| 863 | { |
| 864 | char str[KSYM_SYMBOL_LEN]; |
| 865 | char *search = NULL; |
| 866 | struct ftrace_page *pg; |
| 867 | struct dyn_ftrace *rec; |
| 868 | int type = MATCH_FULL; |
| 869 | unsigned i, match = 0, search_len = 0; |
| 870 | |
| 871 | for (i = 0; i < len; i++) { |
| 872 | if (buff[i] == '*') { |
| 873 | if (!i) { |
| 874 | search = buff + i + 1; |
| 875 | type = MATCH_END_ONLY; |
| 876 | search_len = len - (i + 1); |
| 877 | } else { |
| 878 | if (type == MATCH_END_ONLY) { |
| 879 | type = MATCH_MIDDLE_ONLY; |
| 880 | } else { |
| 881 | match = i; |
| 882 | type = MATCH_FRONT_ONLY; |
| 883 | } |
| 884 | buff[i] = 0; |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* keep kstop machine from running */ |
| 891 | preempt_disable(); |
| 892 | ftrace_filtered = 1; |
| 893 | pg = ftrace_pages_start; |
| 894 | while (pg) { |
| 895 | for (i = 0; i < pg->index; i++) { |
| 896 | int matched = 0; |
| 897 | char *ptr; |
| 898 | |
| 899 | rec = &pg->records[i]; |
| 900 | if (rec->flags & FTRACE_FL_FAILED) |
| 901 | continue; |
| 902 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); |
| 903 | switch (type) { |
| 904 | case MATCH_FULL: |
| 905 | if (strcmp(str, buff) == 0) |
| 906 | matched = 1; |
| 907 | break; |
| 908 | case MATCH_FRONT_ONLY: |
| 909 | if (memcmp(str, buff, match) == 0) |
| 910 | matched = 1; |
| 911 | break; |
| 912 | case MATCH_MIDDLE_ONLY: |
| 913 | if (strstr(str, search)) |
| 914 | matched = 1; |
| 915 | break; |
| 916 | case MATCH_END_ONLY: |
| 917 | ptr = strstr(str, search); |
| 918 | if (ptr && (ptr[search_len] == 0)) |
| 919 | matched = 1; |
| 920 | break; |
| 921 | } |
| 922 | if (matched) |
| 923 | rec->flags |= FTRACE_FL_FILTER; |
| 924 | } |
| 925 | pg = pg->next; |
| 926 | } |
| 927 | preempt_enable(); |
| 928 | } |
| 929 | |
| 930 | static ssize_t notrace |
| 931 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
| 932 | size_t cnt, loff_t *ppos) |
| 933 | { |
| 934 | struct ftrace_iterator *iter; |
| 935 | char ch; |
| 936 | size_t read = 0; |
| 937 | ssize_t ret; |
| 938 | |
| 939 | if (!cnt || cnt < 0) |
| 940 | return 0; |
| 941 | |
| 942 | mutex_lock(&ftrace_filter_lock); |
| 943 | |
| 944 | if (file->f_mode & FMODE_READ) { |
| 945 | struct seq_file *m = file->private_data; |
| 946 | iter = m->private; |
| 947 | } else |
| 948 | iter = file->private_data; |
| 949 | |
| 950 | if (!*ppos) { |
| 951 | iter->flags &= ~FTRACE_ITER_CONT; |
| 952 | iter->buffer_idx = 0; |
| 953 | } |
| 954 | |
| 955 | ret = get_user(ch, ubuf++); |
| 956 | if (ret) |
| 957 | goto out; |
| 958 | read++; |
| 959 | cnt--; |
| 960 | |
| 961 | if (!(iter->flags & ~FTRACE_ITER_CONT)) { |
| 962 | /* skip white space */ |
| 963 | while (cnt && isspace(ch)) { |
| 964 | ret = get_user(ch, ubuf++); |
| 965 | if (ret) |
| 966 | goto out; |
| 967 | read++; |
| 968 | cnt--; |
| 969 | } |
| 970 | |
| 971 | |
| 972 | if (isspace(ch)) { |
| 973 | file->f_pos += read; |
| 974 | ret = read; |
| 975 | goto out; |
| 976 | } |
| 977 | |
| 978 | iter->buffer_idx = 0; |
| 979 | } |
| 980 | |
| 981 | while (cnt && !isspace(ch)) { |
| 982 | if (iter->buffer_idx < FTRACE_BUFF_MAX) |
| 983 | iter->buffer[iter->buffer_idx++] = ch; |
| 984 | else { |
| 985 | ret = -EINVAL; |
| 986 | goto out; |
| 987 | } |
| 988 | ret = get_user(ch, ubuf++); |
| 989 | if (ret) |
| 990 | goto out; |
| 991 | read++; |
| 992 | cnt--; |
| 993 | } |
| 994 | |
| 995 | if (isspace(ch)) { |
| 996 | iter->filtered++; |
| 997 | iter->buffer[iter->buffer_idx] = 0; |
| 998 | ftrace_match(iter->buffer, iter->buffer_idx); |
| 999 | iter->buffer_idx = 0; |
| 1000 | } else |
| 1001 | iter->flags |= FTRACE_ITER_CONT; |
| 1002 | |
| 1003 | |
| 1004 | file->f_pos += read; |
| 1005 | |
| 1006 | ret = read; |
| 1007 | out: |
| 1008 | mutex_unlock(&ftrace_filter_lock); |
| 1009 | |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Steven Rostedt | 77a2b37 | 2008-05-12 21:20:45 +0200 | [diff] [blame^] | 1013 | /** |
| 1014 | * ftrace_set_filter - set a function to filter on in ftrace |
| 1015 | * @buf - the string that holds the function filter text. |
| 1016 | * @len - the length of the string. |
| 1017 | * @reset - non zero to reset all filters before applying this filter. |
| 1018 | * |
| 1019 | * Filters denote which functions should be enabled when tracing is enabled. |
| 1020 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. |
| 1021 | */ |
| 1022 | notrace void ftrace_set_filter(unsigned char *buf, int len, int reset) |
| 1023 | { |
| 1024 | mutex_lock(&ftrace_filter_lock); |
| 1025 | if (reset) |
| 1026 | ftrace_filter_reset(); |
| 1027 | if (buf) |
| 1028 | ftrace_match(buf, len); |
| 1029 | mutex_unlock(&ftrace_filter_lock); |
| 1030 | } |
| 1031 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1032 | static int notrace |
| 1033 | ftrace_filter_release(struct inode *inode, struct file *file) |
| 1034 | { |
| 1035 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 1036 | struct ftrace_iterator *iter; |
| 1037 | |
| 1038 | mutex_lock(&ftrace_filter_lock); |
| 1039 | if (file->f_mode & FMODE_READ) { |
| 1040 | iter = m->private; |
| 1041 | |
| 1042 | seq_release(inode, file); |
| 1043 | } else |
| 1044 | iter = file->private_data; |
| 1045 | |
| 1046 | if (iter->buffer_idx) { |
| 1047 | iter->filtered++; |
| 1048 | iter->buffer[iter->buffer_idx] = 0; |
| 1049 | ftrace_match(iter->buffer, iter->buffer_idx); |
| 1050 | } |
| 1051 | |
| 1052 | mutex_lock(&ftrace_sysctl_lock); |
| 1053 | mutex_lock(&ftraced_lock); |
| 1054 | if (iter->filtered && ftraced_suspend && ftrace_enabled) |
| 1055 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); |
| 1056 | mutex_unlock(&ftraced_lock); |
| 1057 | mutex_unlock(&ftrace_sysctl_lock); |
| 1058 | |
| 1059 | kfree(iter); |
| 1060 | mutex_unlock(&ftrace_filter_lock); |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static struct file_operations ftrace_avail_fops = { |
| 1065 | .open = ftrace_avail_open, |
| 1066 | .read = seq_read, |
| 1067 | .llseek = seq_lseek, |
| 1068 | .release = ftrace_avail_release, |
| 1069 | }; |
| 1070 | |
| 1071 | static struct file_operations ftrace_filter_fops = { |
| 1072 | .open = ftrace_filter_open, |
| 1073 | .read = ftrace_filter_read, |
| 1074 | .write = ftrace_filter_write, |
| 1075 | .llseek = ftrace_filter_lseek, |
| 1076 | .release = ftrace_filter_release, |
| 1077 | }; |
| 1078 | |
Steven Rostedt | e1c08bd | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1079 | /** |
| 1080 | * ftrace_force_update - force an update to all recording ftrace functions |
| 1081 | * |
| 1082 | * The ftrace dynamic update daemon only wakes up once a second. |
| 1083 | * There may be cases where an update needs to be done immediately |
| 1084 | * for tests or internal kernel tracing to begin. This function |
| 1085 | * wakes the daemon to do an update and will not return until the |
| 1086 | * update is complete. |
| 1087 | */ |
| 1088 | int ftrace_force_update(void) |
| 1089 | { |
| 1090 | unsigned long last_counter; |
| 1091 | DECLARE_WAITQUEUE(wait, current); |
| 1092 | int ret = 0; |
| 1093 | |
| 1094 | if (!ftraced_task) |
| 1095 | return -ENODEV; |
| 1096 | |
| 1097 | mutex_lock(&ftraced_lock); |
| 1098 | last_counter = ftraced_iteration_counter; |
| 1099 | |
| 1100 | set_current_state(TASK_INTERRUPTIBLE); |
| 1101 | add_wait_queue(&ftraced_waiters, &wait); |
| 1102 | |
| 1103 | do { |
| 1104 | mutex_unlock(&ftraced_lock); |
| 1105 | wake_up_process(ftraced_task); |
| 1106 | schedule(); |
| 1107 | mutex_lock(&ftraced_lock); |
| 1108 | if (signal_pending(current)) { |
| 1109 | ret = -EINTR; |
| 1110 | break; |
| 1111 | } |
| 1112 | set_current_state(TASK_INTERRUPTIBLE); |
| 1113 | } while (last_counter == ftraced_iteration_counter); |
| 1114 | |
| 1115 | mutex_unlock(&ftraced_lock); |
| 1116 | remove_wait_queue(&ftraced_waiters, &wait); |
| 1117 | set_current_state(TASK_RUNNING); |
| 1118 | |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1122 | static __init int ftrace_init_debugfs(void) |
| 1123 | { |
| 1124 | struct dentry *d_tracer; |
| 1125 | struct dentry *entry; |
| 1126 | |
| 1127 | d_tracer = tracing_init_dentry(); |
| 1128 | |
| 1129 | entry = debugfs_create_file("available_filter_functions", 0444, |
| 1130 | d_tracer, NULL, &ftrace_avail_fops); |
| 1131 | if (!entry) |
| 1132 | pr_warning("Could not create debugfs " |
| 1133 | "'available_filter_functions' entry\n"); |
| 1134 | |
| 1135 | entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer, |
| 1136 | NULL, &ftrace_filter_fops); |
| 1137 | if (!entry) |
| 1138 | pr_warning("Could not create debugfs " |
| 1139 | "'set_ftrace_filter' entry\n"); |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | fs_initcall(ftrace_init_debugfs); |
| 1144 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1145 | static int __init notrace ftrace_dynamic_init(void) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1146 | { |
| 1147 | struct task_struct *p; |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1148 | unsigned long addr; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1149 | int ret; |
| 1150 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1151 | addr = (unsigned long)ftrace_record_ip; |
| 1152 | stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS); |
| 1153 | |
| 1154 | /* ftrace_dyn_arch_init places the return code in addr */ |
| 1155 | if (addr) |
| 1156 | return addr; |
| 1157 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1158 | ret = ftrace_dyn_table_alloc(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1159 | if (ret) |
| 1160 | return ret; |
| 1161 | |
| 1162 | p = kthread_run(ftraced, NULL, "ftraced"); |
| 1163 | if (IS_ERR(p)) |
| 1164 | return -1; |
| 1165 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1166 | last_ftrace_enabled = ftrace_enabled = 1; |
Steven Rostedt | e1c08bd | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 1167 | ftraced_task = p; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1172 | core_initcall(ftrace_dynamic_init); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1173 | #else |
Ingo Molnar | c7aafc5 | 2008-05-12 21:20:45 +0200 | [diff] [blame] | 1174 | # define ftrace_startup() do { } while (0) |
| 1175 | # define ftrace_shutdown() do { } while (0) |
| 1176 | # define ftrace_startup_sysctl() do { } while (0) |
| 1177 | # define ftrace_shutdown_sysctl() do { } while (0) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1178 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 1179 | |
| 1180 | /** |
| 1181 | * register_ftrace_function - register a function for profiling |
| 1182 | * @ops - ops structure that holds the function for profiling. |
| 1183 | * |
| 1184 | * Register a function to be called by all functions in the |
| 1185 | * kernel. |
| 1186 | * |
| 1187 | * Note: @ops->func and all the functions it calls must be labeled |
| 1188 | * with "notrace", otherwise it will go into a |
| 1189 | * recursive loop. |
| 1190 | */ |
| 1191 | int register_ftrace_function(struct ftrace_ops *ops) |
| 1192 | { |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1193 | int ret; |
| 1194 | |
| 1195 | mutex_lock(&ftrace_sysctl_lock); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1196 | ret = __register_ftrace_function(ops); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1197 | ftrace_startup(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1198 | mutex_unlock(&ftrace_sysctl_lock); |
| 1199 | |
| 1200 | return ret; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * unregister_ftrace_function - unresgister a function for profiling. |
| 1205 | * @ops - ops structure that holds the function to unregister |
| 1206 | * |
| 1207 | * Unregister a function that was added to be called by ftrace profiling. |
| 1208 | */ |
| 1209 | int unregister_ftrace_function(struct ftrace_ops *ops) |
| 1210 | { |
| 1211 | int ret; |
| 1212 | |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1213 | mutex_lock(&ftrace_sysctl_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1214 | ret = __unregister_ftrace_function(ops); |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1215 | ftrace_shutdown(); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1216 | mutex_unlock(&ftrace_sysctl_lock); |
| 1217 | |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
| 1221 | notrace int |
| 1222 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1223 | struct file *file, void __user *buffer, size_t *lenp, |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1224 | loff_t *ppos) |
| 1225 | { |
| 1226 | int ret; |
| 1227 | |
| 1228 | mutex_lock(&ftrace_sysctl_lock); |
| 1229 | |
Steven Rostedt | 5072c59 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1230 | ret = proc_dointvec(table, write, file, buffer, lenp, ppos); |
Steven Rostedt | b0fc494 | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 1231 | |
| 1232 | if (ret || !write || (last_ftrace_enabled == ftrace_enabled)) |
| 1233 | goto out; |
| 1234 | |
| 1235 | last_ftrace_enabled = ftrace_enabled; |
| 1236 | |
| 1237 | if (ftrace_enabled) { |
| 1238 | |
| 1239 | ftrace_startup_sysctl(); |
| 1240 | |
| 1241 | /* we are starting ftrace again */ |
| 1242 | if (ftrace_list != &ftrace_list_end) { |
| 1243 | if (ftrace_list->next == &ftrace_list_end) |
| 1244 | ftrace_trace_function = ftrace_list->func; |
| 1245 | else |
| 1246 | ftrace_trace_function = ftrace_list_func; |
| 1247 | } |
| 1248 | |
| 1249 | } else { |
| 1250 | /* stopping ftrace calls (just send to ftrace_stub) */ |
| 1251 | ftrace_trace_function = ftrace_stub; |
| 1252 | |
| 1253 | ftrace_shutdown_sysctl(); |
| 1254 | } |
| 1255 | |
| 1256 | out: |
| 1257 | mutex_unlock(&ftrace_sysctl_lock); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1258 | return ret; |
Arnaldo Carvalho de Melo | 16444a8 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1259 | } |