blob: 1f54a94189fe8f53a5382f7a931e41bd39e57520 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
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 Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
20#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020021#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010022#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020023#include <linux/uaccess.h>
Abhishek Sagarf22f9a82008-06-21 23:50:29 +053024#include <linux/kprobes.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010025#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020026#include <linux/sysctl.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020027#include <linux/ctype.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010028#include <linux/hash.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020030
Abhishek Sagar395a59d2008-06-21 23:47:27 +053031#include <asm/ftrace.h>
32
Steven Rostedt3d083392008-05-12 21:20:42 +020033#include "trace.h"
34
Steven Rostedt4eebcc82008-05-12 21:20:48 +020035/* ftrace_enabled is a method to turn ftrace on or off */
36int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020037static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020038
Steven Rostedt4eebcc82008-05-12 21:20:48 +020039/*
40 * ftrace_disabled is set when an anomaly is discovered.
41 * ftrace_disabled is much stronger than ftrace_enabled.
42 */
43static int ftrace_disabled __read_mostly;
44
Steven Rostedt3d083392008-05-12 21:20:42 +020045static DEFINE_SPINLOCK(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020046static DEFINE_MUTEX(ftrace_sysctl_lock);
47
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020048static struct ftrace_ops ftrace_list_end __read_mostly =
49{
50 .func = ftrace_stub,
51};
52
53static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
54ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
55
Ingo Molnarf2252932008-05-22 10:37:48 +020056static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020057{
58 struct ftrace_ops *op = ftrace_list;
59
60 /* in case someone actually ports this to alpha! */
61 read_barrier_depends();
62
63 while (op != &ftrace_list_end) {
64 /* silly alpha */
65 read_barrier_depends();
66 op->func(ip, parent_ip);
67 op = op->next;
68 };
69}
70
71/**
Steven Rostedt3d083392008-05-12 21:20:42 +020072 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020073 *
Steven Rostedt3d083392008-05-12 21:20:42 +020074 * This NULLs the ftrace function and in essence stops
75 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020076 */
Steven Rostedt3d083392008-05-12 21:20:42 +020077void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020078{
Steven Rostedt3d083392008-05-12 21:20:42 +020079 ftrace_trace_function = ftrace_stub;
80}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020081
Ingo Molnare309b412008-05-12 21:20:51 +020082static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +020083{
Steven Rostedt99ecdc42008-08-15 21:40:05 -040084 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +020085 spin_lock(&ftrace_lock);
86
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020087 ops->next = ftrace_list;
88 /*
89 * We are entering ops into the ftrace_list but another
90 * CPU might be walking that list. We need to make sure
91 * the ops->next pointer is valid before another CPU sees
92 * the ops pointer included into the ftrace_list.
93 */
94 smp_wmb();
95 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +020096
Steven Rostedtb0fc4942008-05-12 21:20:43 +020097 if (ftrace_enabled) {
98 /*
99 * For one func, simply call it directly.
100 * For more than one func, call the chain.
101 */
102 if (ops->next == &ftrace_list_end)
103 ftrace_trace_function = ops->func;
104 else
105 ftrace_trace_function = ftrace_list_func;
106 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200107
108 spin_unlock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109
110 return 0;
111}
112
Ingo Molnare309b412008-05-12 21:20:51 +0200113static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200114{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115 struct ftrace_ops **p;
116 int ret = 0;
117
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400118 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200119 spin_lock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120
121 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200122 * If we are removing the last function, then simply point
123 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200124 */
125 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
126 ftrace_trace_function = ftrace_stub;
127 ftrace_list = &ftrace_list_end;
128 goto out;
129 }
130
131 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
132 if (*p == ops)
133 break;
134
135 if (*p != ops) {
136 ret = -1;
137 goto out;
138 }
139
140 *p = (*p)->next;
141
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200142 if (ftrace_enabled) {
143 /* If we only have one func left, then call that directly */
144 if (ftrace_list == &ftrace_list_end ||
145 ftrace_list->next == &ftrace_list_end)
146 ftrace_trace_function = ftrace_list->func;
147 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200148
149 out:
Steven Rostedt3d083392008-05-12 21:20:42 +0200150 spin_unlock(&ftrace_lock);
151
152 return ret;
153}
154
155#ifdef CONFIG_DYNAMIC_FTRACE
156
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400157#ifndef CONFIG_FTRACE_MCOUNT_RECORD
158/*
159 * The hash lock is only needed when the recording of the mcount
160 * callers are dynamic. That is, by the caller themselves and
161 * not recorded via the compilation.
162 */
163static DEFINE_SPINLOCK(ftrace_hash_lock);
Stephen Rothwell2d7da802008-08-25 13:08:44 +1000164#define ftrace_hash_lock(flags) spin_lock_irqsave(&ftrace_hash_lock, flags)
Steven Rostedt644f9912008-09-06 01:06:04 -0400165#define ftrace_hash_unlock(flags) \
166 spin_unlock_irqrestore(&ftrace_hash_lock, flags)
Steven Rostedtbd95b882008-10-16 09:31:27 -0400167static void ftrace_release_hash(unsigned long start, unsigned long end);
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400168#else
169/* This is protected via the ftrace_lock with MCOUNT_RECORD. */
Ingo Molnarac8825e2008-08-25 08:12:04 +0200170#define ftrace_hash_lock(flags) do { (void)(flags); } while (0)
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400171#define ftrace_hash_unlock(flags) do { } while(0)
Steven Rostedtbd95b882008-10-16 09:31:27 -0400172static inline void ftrace_release_hash(unsigned long start, unsigned long end)
173{
174}
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400175#endif
176
Steven Noonan71c67d52008-09-20 01:00:37 -0700177/*
178 * Since MCOUNT_ADDR may point to mcount itself, we do not want
179 * to get it confused by reading a reference in the code as we
180 * are parsing on objcopy output of text. Use a variable for
181 * it instead.
182 */
183static unsigned long mcount_addr = MCOUNT_ADDR;
184
Steven Rostedte1c08bd2008-05-12 21:20:44 +0200185static struct task_struct *ftraced_task;
Steven Rostedte1c08bd2008-05-12 21:20:44 +0200186
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200187enum {
188 FTRACE_ENABLE_CALLS = (1 << 0),
189 FTRACE_DISABLE_CALLS = (1 << 1),
190 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
191 FTRACE_ENABLE_MCOUNT = (1 << 3),
192 FTRACE_DISABLE_MCOUNT = (1 << 4),
193};
194
Steven Rostedt5072c592008-05-12 21:20:43 +0200195static int ftrace_filtered;
Abhishek Sagarecea6562008-06-21 23:47:53 +0530196static int tracing_on;
197static int frozen_record_count;
Steven Rostedt5072c592008-05-12 21:20:43 +0200198
Steven Rostedt3d083392008-05-12 21:20:42 +0200199static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
200
201static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
202
Steven Rostedt3d083392008-05-12 21:20:42 +0200203static DEFINE_MUTEX(ftraced_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400204static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200205
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200206struct ftrace_page {
207 struct ftrace_page *next;
David Milleraa5e5ce2008-05-13 22:06:56 -0700208 unsigned long index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200209 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700210};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200211
212#define ENTRIES_PER_PAGE \
213 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
214
215/* estimate from running different kernels */
216#define NR_TO_INIT 10000
217
218static struct ftrace_page *ftrace_pages_start;
219static struct ftrace_page *ftrace_pages;
220
Steven Rostedt3d083392008-05-12 21:20:42 +0200221static int ftraced_trigger;
222static int ftraced_suspend;
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400223static int ftraced_stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200224
225static int ftrace_record_suspend;
226
Steven Rostedt37ad5082008-05-12 21:20:48 +0200227static struct dyn_ftrace *ftrace_free_records;
228
Abhishek Sagarecea6562008-06-21 23:47:53 +0530229
230#ifdef CONFIG_KPROBES
231static inline void freeze_record(struct dyn_ftrace *rec)
232{
233 if (!(rec->flags & FTRACE_FL_FROZEN)) {
234 rec->flags |= FTRACE_FL_FROZEN;
235 frozen_record_count++;
236 }
237}
238
239static inline void unfreeze_record(struct dyn_ftrace *rec)
240{
241 if (rec->flags & FTRACE_FL_FROZEN) {
242 rec->flags &= ~FTRACE_FL_FROZEN;
243 frozen_record_count--;
244 }
245}
246
247static inline int record_frozen(struct dyn_ftrace *rec)
248{
249 return rec->flags & FTRACE_FL_FROZEN;
250}
251#else
252# define freeze_record(rec) ({ 0; })
253# define unfreeze_record(rec) ({ 0; })
254# define record_frozen(rec) ({ 0; })
255#endif /* CONFIG_KPROBES */
256
257int skip_trace(unsigned long ip)
258{
259 unsigned long fl;
260 struct dyn_ftrace *rec;
261 struct hlist_node *t;
262 struct hlist_head *head;
263
264 if (frozen_record_count == 0)
265 return 0;
266
267 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
268 hlist_for_each_entry_rcu(rec, t, head, node) {
269 if (rec->ip == ip) {
270 if (record_frozen(rec)) {
271 if (rec->flags & FTRACE_FL_FAILED)
272 return 1;
273
274 if (!(rec->flags & FTRACE_FL_CONVERTED))
275 return 1;
276
277 if (!tracing_on || !ftrace_enabled)
278 return 1;
279
280 if (ftrace_filtered) {
281 fl = rec->flags & (FTRACE_FL_FILTER |
282 FTRACE_FL_NOTRACE);
283 if (!fl || (fl & FTRACE_FL_NOTRACE))
284 return 1;
285 }
286 }
287 break;
288 }
289 }
290
291 return 0;
292}
293
Ingo Molnare309b412008-05-12 21:20:51 +0200294static inline int
Ingo Molnar9ff9cdb2008-05-12 21:20:50 +0200295ftrace_ip_in_hash(unsigned long ip, unsigned long key)
Steven Rostedt3d083392008-05-12 21:20:42 +0200296{
297 struct dyn_ftrace *p;
298 struct hlist_node *t;
299 int found = 0;
300
Abhishek Sagarffdaa352008-05-24 23:45:02 +0530301 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
Steven Rostedt3d083392008-05-12 21:20:42 +0200302 if (p->ip == ip) {
303 found = 1;
304 break;
305 }
306 }
307
308 return found;
309}
310
Ingo Molnare309b412008-05-12 21:20:51 +0200311static inline void
Steven Rostedt3d083392008-05-12 21:20:42 +0200312ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
313{
Abhishek Sagarffdaa352008-05-24 23:45:02 +0530314 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
Steven Rostedt3d083392008-05-12 21:20:42 +0200315}
316
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530317/* called from kstop_machine */
318static inline void ftrace_del_hash(struct dyn_ftrace *node)
319{
320 hlist_del(&node->node);
321}
322
Ingo Molnare309b412008-05-12 21:20:51 +0200323static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200324{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200325 rec->ip = (unsigned long)ftrace_free_records;
326 ftrace_free_records = rec;
327 rec->flags |= FTRACE_FL_FREE;
328}
329
Steven Rostedtfed19392008-08-14 22:47:19 -0400330void ftrace_release(void *start, unsigned long size)
331{
332 struct dyn_ftrace *rec;
333 struct ftrace_page *pg;
334 unsigned long s = (unsigned long)start;
335 unsigned long e = s + size;
336 int i;
337
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400338 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400339 return;
340
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400341 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -0400342 spin_lock(&ftrace_lock);
343
344 for (pg = ftrace_pages_start; pg; pg = pg->next) {
345 for (i = 0; i < pg->index; i++) {
346 rec = &pg->records[i];
347
348 if ((rec->ip >= s) && (rec->ip < e))
349 ftrace_free_rec(rec);
350 }
351 }
352 spin_unlock(&ftrace_lock);
353
Steven Rostedtbd95b882008-10-16 09:31:27 -0400354 ftrace_release_hash(s, e);
Steven Rostedtfed19392008-08-14 22:47:19 -0400355}
356
Ingo Molnare309b412008-05-12 21:20:51 +0200357static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200358{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200359 struct dyn_ftrace *rec;
360
361 /* First check for freed records */
362 if (ftrace_free_records) {
363 rec = ftrace_free_records;
364
Steven Rostedt37ad5082008-05-12 21:20:48 +0200365 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
366 WARN_ON_ONCE(1);
367 ftrace_free_records = NULL;
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200368 ftrace_disabled = 1;
369 ftrace_enabled = 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200370 return NULL;
371 }
372
373 ftrace_free_records = (void *)rec->ip;
374 memset(rec, 0, sizeof(*rec));
375 return rec;
376 }
377
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200378 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
379 if (!ftrace_pages->next)
380 return NULL;
381 ftrace_pages = ftrace_pages->next;
382 }
383
384 return &ftrace_pages->records[ftrace_pages->index++];
385}
386
Ingo Molnare309b412008-05-12 21:20:51 +0200387static void
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200388ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200389{
390 struct dyn_ftrace *node;
391 unsigned long flags;
392 unsigned long key;
393 int resched;
Steven Rostedt2bb6f8d2008-05-12 21:21:02 +0200394 int cpu;
Steven Rostedt3d083392008-05-12 21:20:42 +0200395
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200396 if (!ftrace_enabled || ftrace_disabled)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200397 return;
398
Steven Rostedt3d083392008-05-12 21:20:42 +0200399 resched = need_resched();
400 preempt_disable_notrace();
401
Steven Rostedt2bb6f8d2008-05-12 21:21:02 +0200402 /*
403 * We simply need to protect against recursion.
404 * Use the the raw version of smp_processor_id and not
405 * __get_cpu_var which can call debug hooks that can
406 * cause a recursive crash here.
407 */
408 cpu = raw_smp_processor_id();
409 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
410 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
Steven Rostedt3d083392008-05-12 21:20:42 +0200411 goto out;
412
413 if (unlikely(ftrace_record_suspend))
414 goto out;
415
416 key = hash_long(ip, FTRACE_HASHBITS);
417
418 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
419
420 if (ftrace_ip_in_hash(ip, key))
421 goto out;
422
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400423 ftrace_hash_lock(flags);
Steven Rostedt3d083392008-05-12 21:20:42 +0200424
425 /* This ip may have hit the hash before the lock */
426 if (ftrace_ip_in_hash(ip, key))
427 goto out_unlock;
428
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200429 node = ftrace_alloc_dyn_node(ip);
Steven Rostedt3d083392008-05-12 21:20:42 +0200430 if (!node)
431 goto out_unlock;
432
433 node->ip = ip;
434
435 ftrace_add_hash(node, key);
436
437 ftraced_trigger = 1;
438
439 out_unlock:
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400440 ftrace_hash_unlock(flags);
Steven Rostedt3d083392008-05-12 21:20:42 +0200441 out:
Steven Rostedt2bb6f8d2008-05-12 21:21:02 +0200442 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
Steven Rostedt3d083392008-05-12 21:20:42 +0200443
444 /* prevent recursion with scheduler */
445 if (resched)
446 preempt_enable_no_resched_notrace();
447 else
448 preempt_enable_notrace();
449}
450
Steven Rostedtcaf8cde2008-05-12 21:20:50 +0200451#define FTRACE_ADDR ((long)(ftrace_caller))
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200452
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530453static int
Steven Rostedt5072c592008-05-12 21:20:43 +0200454__ftrace_replace_code(struct dyn_ftrace *rec,
455 unsigned char *old, unsigned char *new, int enable)
456{
Steven Rostedt41c52c02008-05-22 11:46:33 -0400457 unsigned long ip, fl;
Steven Rostedt5072c592008-05-12 21:20:43 +0200458
459 ip = rec->ip;
460
461 if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200462 /*
463 * If filtering is on:
464 *
465 * If this record is set to be filtered and
466 * is enabled then do nothing.
467 *
468 * If this record is set to be filtered and
469 * it is not enabled, enable it.
470 *
471 * If this record is not set to be filtered
472 * and it is not enabled do nothing.
473 *
Steven Rostedt41c52c02008-05-22 11:46:33 -0400474 * If this record is set not to trace then
475 * do nothing.
476 *
Abhishek Sagara4500b82008-06-14 11:59:39 +0530477 * If this record is set not to trace and
478 * it is enabled then disable it.
479 *
Steven Rostedt5072c592008-05-12 21:20:43 +0200480 * If this record is not set to be filtered and
481 * it is enabled, disable it.
482 */
Abhishek Sagara4500b82008-06-14 11:59:39 +0530483
484 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
485 FTRACE_FL_ENABLED);
Steven Rostedt5072c592008-05-12 21:20:43 +0200486
487 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
Abhishek Sagara4500b82008-06-14 11:59:39 +0530488 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
489 !fl || (fl == FTRACE_FL_NOTRACE))
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530490 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200491
492 /*
493 * If it is enabled disable it,
494 * otherwise enable it!
495 */
Abhishek Sagara4500b82008-06-14 11:59:39 +0530496 if (fl & FTRACE_FL_ENABLED) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200497 /* swap new and old */
498 new = old;
499 old = ftrace_call_replace(ip, FTRACE_ADDR);
500 rec->flags &= ~FTRACE_FL_ENABLED;
501 } else {
502 new = ftrace_call_replace(ip, FTRACE_ADDR);
503 rec->flags |= FTRACE_FL_ENABLED;
504 }
505 } else {
506
Steven Rostedt41c52c02008-05-22 11:46:33 -0400507 if (enable) {
508 /*
509 * If this record is set not to trace and is
510 * not enabled, do nothing.
511 */
512 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
513 if (fl == FTRACE_FL_NOTRACE)
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530514 return 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400515
Steven Rostedt5072c592008-05-12 21:20:43 +0200516 new = ftrace_call_replace(ip, FTRACE_ADDR);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400517 } else
Steven Rostedt5072c592008-05-12 21:20:43 +0200518 old = ftrace_call_replace(ip, FTRACE_ADDR);
519
520 if (enable) {
521 if (rec->flags & FTRACE_FL_ENABLED)
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530522 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200523 rec->flags |= FTRACE_FL_ENABLED;
524 } else {
525 if (!(rec->flags & FTRACE_FL_ENABLED))
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530526 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200527 rec->flags &= ~FTRACE_FL_ENABLED;
528 }
529 }
530
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530531 return ftrace_modify_code(ip, old, new);
Steven Rostedt5072c592008-05-12 21:20:43 +0200532}
533
Ingo Molnare309b412008-05-12 21:20:51 +0200534static void ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200535{
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530536 int i, failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200537 unsigned char *new = NULL, *old = NULL;
538 struct dyn_ftrace *rec;
539 struct ftrace_page *pg;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200540
Steven Rostedt5072c592008-05-12 21:20:43 +0200541 if (enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200542 old = ftrace_nop_replace();
543 else
544 new = ftrace_nop_replace();
545
546 for (pg = ftrace_pages_start; pg; pg = pg->next) {
547 for (i = 0; i < pg->index; i++) {
548 rec = &pg->records[i];
549
550 /* don't modify code that has already faulted */
551 if (rec->flags & FTRACE_FL_FAILED)
552 continue;
553
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530554 /* ignore updates to this record's mcount site */
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530555 if (get_kprobe((void *)rec->ip)) {
556 freeze_record(rec);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530557 continue;
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530558 } else {
559 unfreeze_record(rec);
560 }
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530561
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530562 failed = __ftrace_replace_code(rec, old, new, enable);
563 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
564 rec->flags |= FTRACE_FL_FAILED;
565 if ((system_state == SYSTEM_BOOTING) ||
Abhishek Sagar34078a52008-06-03 08:33:41 +0530566 !core_kernel_text(rec->ip)) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530567 ftrace_del_hash(rec);
568 ftrace_free_rec(rec);
569 }
570 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200571 }
572 }
573}
574
Ingo Molnare309b412008-05-12 21:20:51 +0200575static void ftrace_shutdown_replenish(void)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200576{
577 if (ftrace_pages->next)
578 return;
579
580 /* allocate another page */
581 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
582}
Steven Rostedt3d083392008-05-12 21:20:42 +0200583
Steven Rostedt05736a42008-09-22 14:55:47 -0700584static void print_ip_ins(const char *fmt, unsigned char *p)
585{
586 int i;
587
588 printk(KERN_CONT "%s", fmt);
589
590 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
591 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
592}
593
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530594static int
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200595ftrace_code_disable(struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200596{
597 unsigned long ip;
598 unsigned char *nop, *call;
599 int failed;
600
601 ip = rec->ip;
602
603 nop = ftrace_nop_replace();
Steven Rostedt3b47bfc2008-08-27 23:24:15 -0400604 call = ftrace_call_replace(ip, mcount_addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200605
606 failed = ftrace_modify_code(ip, call, nop);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200607 if (failed) {
Steven Rostedt05736a42008-09-22 14:55:47 -0700608 switch (failed) {
609 case 1:
610 WARN_ON_ONCE(1);
611 pr_info("ftrace faulted on modifying ");
612 print_ip_sym(ip);
613 break;
614 case 2:
615 WARN_ON_ONCE(1);
616 pr_info("ftrace failed to modify ");
617 print_ip_sym(ip);
618 print_ip_ins(" expected: ", call);
619 print_ip_ins(" actual: ", (unsigned char *)ip);
620 print_ip_ins(" replace: ", nop);
621 printk(KERN_CONT "\n");
622 break;
623 }
624
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200625 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530626 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200627 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530628 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200629}
630
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400631static int __ftrace_update_code(void *ignore);
632
Ingo Molnare309b412008-05-12 21:20:51 +0200633static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200634{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200635 unsigned long addr;
636 int *command = data;
637
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400638 if (*command & FTRACE_ENABLE_CALLS) {
639 /*
640 * Update any recorded ips now that we have the
641 * machine stopped
642 */
643 __ftrace_update_code(NULL);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200644 ftrace_replace_code(1);
Abhishek Sagarecea6562008-06-21 23:47:53 +0530645 tracing_on = 1;
646 } else if (*command & FTRACE_DISABLE_CALLS) {
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200647 ftrace_replace_code(0);
Abhishek Sagarecea6562008-06-21 23:47:53 +0530648 tracing_on = 0;
649 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200650
651 if (*command & FTRACE_UPDATE_TRACE_FUNC)
652 ftrace_update_ftrace_func(ftrace_trace_function);
653
654 if (*command & FTRACE_ENABLE_MCOUNT) {
655 addr = (unsigned long)ftrace_record_ip;
656 ftrace_mcount_set(&addr);
657 } else if (*command & FTRACE_DISABLE_MCOUNT) {
658 addr = (unsigned long)ftrace_stub;
659 ftrace_mcount_set(&addr);
660 }
661
662 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200663}
664
Ingo Molnare309b412008-05-12 21:20:51 +0200665static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200666{
Rusty Russell784e2d72008-07-28 12:16:31 -0500667 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt3d083392008-05-12 21:20:42 +0200668}
669
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400670void ftrace_disable_daemon(void)
671{
672 /* Stop the daemon from calling kstop_machine */
673 mutex_lock(&ftraced_lock);
674 ftraced_stop = 1;
675 mutex_unlock(&ftraced_lock);
676
677 ftrace_force_update();
678}
679
680void ftrace_enable_daemon(void)
681{
682 mutex_lock(&ftraced_lock);
683 ftraced_stop = 0;
684 mutex_unlock(&ftraced_lock);
685
686 ftrace_force_update();
687}
688
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200689static ftrace_func_t saved_ftrace_func;
690
Ingo Molnare309b412008-05-12 21:20:51 +0200691static void ftrace_startup(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200692{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200693 int command = 0;
694
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200695 if (unlikely(ftrace_disabled))
696 return;
697
Steven Rostedt3d083392008-05-12 21:20:42 +0200698 mutex_lock(&ftraced_lock);
699 ftraced_suspend++;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200700 if (ftraced_suspend == 1)
701 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +0200702
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200703 if (saved_ftrace_func != ftrace_trace_function) {
704 saved_ftrace_func = ftrace_trace_function;
705 command |= FTRACE_UPDATE_TRACE_FUNC;
706 }
707
708 if (!command || !ftrace_enabled)
709 goto out;
710
711 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200712 out:
713 mutex_unlock(&ftraced_lock);
714}
715
Ingo Molnare309b412008-05-12 21:20:51 +0200716static void ftrace_shutdown(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200717{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200718 int command = 0;
719
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200720 if (unlikely(ftrace_disabled))
721 return;
722
Steven Rostedt3d083392008-05-12 21:20:42 +0200723 mutex_lock(&ftraced_lock);
724 ftraced_suspend--;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200725 if (!ftraced_suspend)
726 command |= FTRACE_DISABLE_CALLS;
727
728 if (saved_ftrace_func != ftrace_trace_function) {
729 saved_ftrace_func = ftrace_trace_function;
730 command |= FTRACE_UPDATE_TRACE_FUNC;
731 }
732
733 if (!command || !ftrace_enabled)
Steven Rostedt3d083392008-05-12 21:20:42 +0200734 goto out;
735
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200736 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200737 out:
738 mutex_unlock(&ftraced_lock);
739}
740
Ingo Molnare309b412008-05-12 21:20:51 +0200741static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200742{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200743 int command = FTRACE_ENABLE_MCOUNT;
744
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200745 if (unlikely(ftrace_disabled))
746 return;
747
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200748 mutex_lock(&ftraced_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200749 /* Force update next time */
750 saved_ftrace_func = NULL;
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200751 /* ftraced_suspend is true if we want ftrace running */
752 if (ftraced_suspend)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200753 command |= FTRACE_ENABLE_CALLS;
754
755 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200756 mutex_unlock(&ftraced_lock);
757}
758
Ingo Molnare309b412008-05-12 21:20:51 +0200759static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200760{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200761 int command = FTRACE_DISABLE_MCOUNT;
762
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200763 if (unlikely(ftrace_disabled))
764 return;
765
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200766 mutex_lock(&ftraced_lock);
767 /* ftraced_suspend is true if ftrace is running */
768 if (ftraced_suspend)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200769 command |= FTRACE_DISABLE_CALLS;
770
771 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200772 mutex_unlock(&ftraced_lock);
773}
774
Steven Rostedt3d083392008-05-12 21:20:42 +0200775static cycle_t ftrace_update_time;
776static unsigned long ftrace_update_cnt;
777unsigned long ftrace_update_tot_cnt;
778
Ingo Molnare309b412008-05-12 21:20:51 +0200779static int __ftrace_update_code(void *ignore)
Steven Rostedt3d083392008-05-12 21:20:42 +0200780{
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530781 int i, save_ftrace_enabled;
782 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200783 struct dyn_ftrace *p;
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530784 struct hlist_node *t, *n;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530785 struct hlist_head *head, temp_list;
Steven Rostedt3d083392008-05-12 21:20:42 +0200786
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200787 /* Don't be recording funcs now */
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400788 ftrace_record_suspend++;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200789 save_ftrace_enabled = ftrace_enabled;
790 ftrace_enabled = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200791
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200792 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200793 ftrace_update_cnt = 0;
794
795 /* No locks needed, the machine is stopped! */
796 for (i = 0; i < FTRACE_HASHSIZE; i++) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530797 INIT_HLIST_HEAD(&temp_list);
798 head = &ftrace_hash[i];
799
Steven Rostedt3d083392008-05-12 21:20:42 +0200800 /* all CPUS are stopped, we are safe to modify code */
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530801 hlist_for_each_entry_safe(p, t, n, head, node) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530802 /* Skip over failed records which have not been
803 * freed. */
804 if (p->flags & FTRACE_FL_FAILED)
805 continue;
Steven Rostedt3d083392008-05-12 21:20:42 +0200806
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530807 /* Unconverted records are always at the head of the
808 * hash bucket. Once we encounter a converted record,
809 * simply skip over to the next bucket. Saves ftraced
810 * some processor cycles (ftrace does its bid for
811 * global warming :-p ). */
812 if (p->flags & (FTRACE_FL_CONVERTED))
813 break;
814
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530815 /* Ignore updates to this record's mcount site.
816 * Reintroduce this record at the head of this
817 * bucket to attempt to "convert" it again if
818 * the kprobe on it is unregistered before the
819 * next run. */
820 if (get_kprobe((void *)p->ip)) {
821 ftrace_del_hash(p);
822 INIT_HLIST_NODE(&p->node);
823 hlist_add_head(&p->node, &temp_list);
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530824 freeze_record(p);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530825 continue;
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530826 } else {
827 unfreeze_record(p);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530828 }
829
830 /* convert record (i.e, patch mcount-call with NOP) */
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530831 if (ftrace_code_disable(p)) {
832 p->flags |= FTRACE_FL_CONVERTED;
833 ftrace_update_cnt++;
834 } else {
835 if ((system_state == SYSTEM_BOOTING) ||
Abhishek Sagar34078a52008-06-03 08:33:41 +0530836 !core_kernel_text(p->ip)) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530837 ftrace_del_hash(p);
838 ftrace_free_rec(p);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530839 }
840 }
841 }
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530842
843 hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
844 hlist_del(&p->node);
845 INIT_HLIST_NODE(&p->node);
846 hlist_add_head(&p->node, head);
847 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200848 }
849
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200850 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200851 ftrace_update_time = stop - start;
852 ftrace_update_tot_cnt += ftrace_update_cnt;
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400853 ftraced_trigger = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200854
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200855 ftrace_enabled = save_ftrace_enabled;
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400856 ftrace_record_suspend--;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200857
858 return 0;
859}
860
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400861static int ftrace_update_code(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200862{
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400863 if (unlikely(ftrace_disabled) ||
864 !ftrace_enabled || !ftraced_trigger)
865 return 0;
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200866
Rusty Russell784e2d72008-07-28 12:16:31 -0500867 stop_machine(__ftrace_update_code, NULL, NULL);
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400868
869 return 1;
Steven Rostedt3d083392008-05-12 21:20:42 +0200870}
871
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400872static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200873{
874 struct ftrace_page *pg;
875 int cnt;
876 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200877
878 /* allocate a few pages */
879 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
880 if (!ftrace_pages_start)
881 return -1;
882
883 /*
884 * Allocate a few more pages.
885 *
886 * TODO: have some parser search vmlinux before
887 * final linking to find all calls to ftrace.
888 * Then we can:
889 * a) know how many pages to allocate.
890 * and/or
891 * b) set up the table then.
892 *
893 * The dynamic code is still necessary for
894 * modules.
895 */
896
897 pg = ftrace_pages = ftrace_pages_start;
898
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400899 cnt = num_to_init / ENTRIES_PER_PAGE;
900 pr_info("ftrace: allocating %ld hash entries in %d pages\n",
901 num_to_init, cnt);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200902
903 for (i = 0; i < cnt; i++) {
904 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
905
906 /* If we fail, we'll try later anyway */
907 if (!pg->next)
908 break;
909
910 pg = pg->next;
911 }
912
913 return 0;
914}
915
Steven Rostedt5072c592008-05-12 21:20:43 +0200916enum {
917 FTRACE_ITER_FILTER = (1 << 0),
918 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -0400919 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530920 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +0200921};
922
923#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
924
925struct ftrace_iterator {
926 loff_t pos;
927 struct ftrace_page *pg;
928 unsigned idx;
929 unsigned flags;
930 unsigned char buffer[FTRACE_BUFF_MAX+1];
931 unsigned buffer_idx;
932 unsigned filtered;
933};
934
Ingo Molnare309b412008-05-12 21:20:51 +0200935static void *
Steven Rostedt5072c592008-05-12 21:20:43 +0200936t_next(struct seq_file *m, void *v, loff_t *pos)
937{
938 struct ftrace_iterator *iter = m->private;
939 struct dyn_ftrace *rec = NULL;
940
941 (*pos)++;
942
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400943 /* should not be called from interrupt context */
944 spin_lock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200945 retry:
946 if (iter->idx >= iter->pg->index) {
947 if (iter->pg->next) {
948 iter->pg = iter->pg->next;
949 iter->idx = 0;
950 goto retry;
951 }
952 } else {
953 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -0400954 if ((rec->flags & FTRACE_FL_FREE) ||
955
956 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530957 (rec->flags & FTRACE_FL_FAILED)) ||
958
959 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -0400960 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530961
Steven Rostedt41c52c02008-05-22 11:46:33 -0400962 ((iter->flags & FTRACE_ITER_NOTRACE) &&
963 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200964 rec = NULL;
965 goto retry;
966 }
967 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400968 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200969
970 iter->pos = *pos;
971
972 return rec;
973}
974
975static void *t_start(struct seq_file *m, loff_t *pos)
976{
977 struct ftrace_iterator *iter = m->private;
978 void *p = NULL;
979 loff_t l = -1;
980
981 if (*pos != iter->pos) {
982 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
983 ;
984 } else {
985 l = *pos;
986 p = t_next(m, p, &l);
987 }
988
989 return p;
990}
991
992static void t_stop(struct seq_file *m, void *p)
993{
994}
995
996static int t_show(struct seq_file *m, void *v)
997{
998 struct dyn_ftrace *rec = v;
999 char str[KSYM_SYMBOL_LEN];
1000
1001 if (!rec)
1002 return 0;
1003
1004 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1005
1006 seq_printf(m, "%s\n", str);
1007
1008 return 0;
1009}
1010
1011static struct seq_operations show_ftrace_seq_ops = {
1012 .start = t_start,
1013 .next = t_next,
1014 .stop = t_stop,
1015 .show = t_show,
1016};
1017
Ingo Molnare309b412008-05-12 21:20:51 +02001018static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001019ftrace_avail_open(struct inode *inode, struct file *file)
1020{
1021 struct ftrace_iterator *iter;
1022 int ret;
1023
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001024 if (unlikely(ftrace_disabled))
1025 return -ENODEV;
1026
Steven Rostedt5072c592008-05-12 21:20:43 +02001027 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1028 if (!iter)
1029 return -ENOMEM;
1030
1031 iter->pg = ftrace_pages_start;
1032 iter->pos = -1;
1033
1034 ret = seq_open(file, &show_ftrace_seq_ops);
1035 if (!ret) {
1036 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001037
Steven Rostedt5072c592008-05-12 21:20:43 +02001038 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001039 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001040 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001041 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001042
1043 return ret;
1044}
1045
1046int ftrace_avail_release(struct inode *inode, struct file *file)
1047{
1048 struct seq_file *m = (struct seq_file *)file->private_data;
1049 struct ftrace_iterator *iter = m->private;
1050
1051 seq_release(inode, file);
1052 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001053
Steven Rostedt5072c592008-05-12 21:20:43 +02001054 return 0;
1055}
1056
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301057static int
1058ftrace_failures_open(struct inode *inode, struct file *file)
1059{
1060 int ret;
1061 struct seq_file *m;
1062 struct ftrace_iterator *iter;
1063
1064 ret = ftrace_avail_open(inode, file);
1065 if (!ret) {
1066 m = (struct seq_file *)file->private_data;
1067 iter = (struct ftrace_iterator *)m->private;
1068 iter->flags = FTRACE_ITER_FAILURES;
1069 }
1070
1071 return ret;
1072}
1073
1074
Steven Rostedt41c52c02008-05-22 11:46:33 -04001075static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001076{
1077 struct ftrace_page *pg;
1078 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001079 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001080 unsigned i;
1081
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001082 /* should not be called from interrupt context */
1083 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001084 if (enable)
1085 ftrace_filtered = 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02001086 pg = ftrace_pages_start;
1087 while (pg) {
1088 for (i = 0; i < pg->index; i++) {
1089 rec = &pg->records[i];
1090 if (rec->flags & FTRACE_FL_FAILED)
1091 continue;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001092 rec->flags &= ~type;
Steven Rostedt5072c592008-05-12 21:20:43 +02001093 }
1094 pg = pg->next;
1095 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001096 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001097}
1098
Ingo Molnare309b412008-05-12 21:20:51 +02001099static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001100ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001101{
1102 struct ftrace_iterator *iter;
1103 int ret = 0;
1104
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001105 if (unlikely(ftrace_disabled))
1106 return -ENODEV;
1107
Steven Rostedt5072c592008-05-12 21:20:43 +02001108 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1109 if (!iter)
1110 return -ENOMEM;
1111
Steven Rostedt41c52c02008-05-22 11:46:33 -04001112 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001113 if ((file->f_mode & FMODE_WRITE) &&
1114 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001115 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001116
1117 if (file->f_mode & FMODE_READ) {
1118 iter->pg = ftrace_pages_start;
1119 iter->pos = -1;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001120 iter->flags = enable ? FTRACE_ITER_FILTER :
1121 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001122
1123 ret = seq_open(file, &show_ftrace_seq_ops);
1124 if (!ret) {
1125 struct seq_file *m = file->private_data;
1126 m->private = iter;
1127 } else
1128 kfree(iter);
1129 } else
1130 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001131 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001132
1133 return ret;
1134}
1135
Steven Rostedt41c52c02008-05-22 11:46:33 -04001136static int
1137ftrace_filter_open(struct inode *inode, struct file *file)
1138{
1139 return ftrace_regex_open(inode, file, 1);
1140}
1141
1142static int
1143ftrace_notrace_open(struct inode *inode, struct file *file)
1144{
1145 return ftrace_regex_open(inode, file, 0);
1146}
1147
Ingo Molnare309b412008-05-12 21:20:51 +02001148static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001149ftrace_regex_read(struct file *file, char __user *ubuf,
Steven Rostedt5072c592008-05-12 21:20:43 +02001150 size_t cnt, loff_t *ppos)
1151{
1152 if (file->f_mode & FMODE_READ)
1153 return seq_read(file, ubuf, cnt, ppos);
1154 else
1155 return -EPERM;
1156}
1157
Ingo Molnare309b412008-05-12 21:20:51 +02001158static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001159ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001160{
1161 loff_t ret;
1162
1163 if (file->f_mode & FMODE_READ)
1164 ret = seq_lseek(file, offset, origin);
1165 else
1166 file->f_pos = ret = 1;
1167
1168 return ret;
1169}
1170
1171enum {
1172 MATCH_FULL,
1173 MATCH_FRONT_ONLY,
1174 MATCH_MIDDLE_ONLY,
1175 MATCH_END_ONLY,
1176};
1177
Ingo Molnare309b412008-05-12 21:20:51 +02001178static void
Steven Rostedt41c52c02008-05-22 11:46:33 -04001179ftrace_match(unsigned char *buff, int len, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001180{
1181 char str[KSYM_SYMBOL_LEN];
1182 char *search = NULL;
1183 struct ftrace_page *pg;
1184 struct dyn_ftrace *rec;
1185 int type = MATCH_FULL;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001186 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001187 unsigned i, match = 0, search_len = 0;
1188
1189 for (i = 0; i < len; i++) {
1190 if (buff[i] == '*') {
1191 if (!i) {
1192 search = buff + i + 1;
1193 type = MATCH_END_ONLY;
1194 search_len = len - (i + 1);
1195 } else {
1196 if (type == MATCH_END_ONLY) {
1197 type = MATCH_MIDDLE_ONLY;
1198 } else {
1199 match = i;
1200 type = MATCH_FRONT_ONLY;
1201 }
1202 buff[i] = 0;
1203 break;
1204 }
1205 }
1206 }
1207
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001208 /* should not be called from interrupt context */
1209 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001210 if (enable)
1211 ftrace_filtered = 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001212 pg = ftrace_pages_start;
1213 while (pg) {
1214 for (i = 0; i < pg->index; i++) {
1215 int matched = 0;
1216 char *ptr;
1217
1218 rec = &pg->records[i];
1219 if (rec->flags & FTRACE_FL_FAILED)
1220 continue;
1221 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1222 switch (type) {
1223 case MATCH_FULL:
1224 if (strcmp(str, buff) == 0)
1225 matched = 1;
1226 break;
1227 case MATCH_FRONT_ONLY:
1228 if (memcmp(str, buff, match) == 0)
1229 matched = 1;
1230 break;
1231 case MATCH_MIDDLE_ONLY:
1232 if (strstr(str, search))
1233 matched = 1;
1234 break;
1235 case MATCH_END_ONLY:
1236 ptr = strstr(str, search);
1237 if (ptr && (ptr[search_len] == 0))
1238 matched = 1;
1239 break;
1240 }
1241 if (matched)
Steven Rostedt41c52c02008-05-22 11:46:33 -04001242 rec->flags |= flag;
Steven Rostedt5072c592008-05-12 21:20:43 +02001243 }
1244 pg = pg->next;
1245 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001246 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001247}
1248
Ingo Molnare309b412008-05-12 21:20:51 +02001249static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001250ftrace_regex_write(struct file *file, const char __user *ubuf,
1251 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001252{
1253 struct ftrace_iterator *iter;
1254 char ch;
1255 size_t read = 0;
1256 ssize_t ret;
1257
1258 if (!cnt || cnt < 0)
1259 return 0;
1260
Steven Rostedt41c52c02008-05-22 11:46:33 -04001261 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001262
1263 if (file->f_mode & FMODE_READ) {
1264 struct seq_file *m = file->private_data;
1265 iter = m->private;
1266 } else
1267 iter = file->private_data;
1268
1269 if (!*ppos) {
1270 iter->flags &= ~FTRACE_ITER_CONT;
1271 iter->buffer_idx = 0;
1272 }
1273
1274 ret = get_user(ch, ubuf++);
1275 if (ret)
1276 goto out;
1277 read++;
1278 cnt--;
1279
1280 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1281 /* skip white space */
1282 while (cnt && isspace(ch)) {
1283 ret = get_user(ch, ubuf++);
1284 if (ret)
1285 goto out;
1286 read++;
1287 cnt--;
1288 }
1289
Steven Rostedt5072c592008-05-12 21:20:43 +02001290 if (isspace(ch)) {
1291 file->f_pos += read;
1292 ret = read;
1293 goto out;
1294 }
1295
1296 iter->buffer_idx = 0;
1297 }
1298
1299 while (cnt && !isspace(ch)) {
1300 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1301 iter->buffer[iter->buffer_idx++] = ch;
1302 else {
1303 ret = -EINVAL;
1304 goto out;
1305 }
1306 ret = get_user(ch, ubuf++);
1307 if (ret)
1308 goto out;
1309 read++;
1310 cnt--;
1311 }
1312
1313 if (isspace(ch)) {
1314 iter->filtered++;
1315 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001316 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001317 iter->buffer_idx = 0;
1318 } else
1319 iter->flags |= FTRACE_ITER_CONT;
1320
1321
1322 file->f_pos += read;
1323
1324 ret = read;
1325 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04001326 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001327
1328 return ret;
1329}
1330
Steven Rostedt41c52c02008-05-22 11:46:33 -04001331static ssize_t
1332ftrace_filter_write(struct file *file, const char __user *ubuf,
1333 size_t cnt, loff_t *ppos)
1334{
1335 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1336}
1337
1338static ssize_t
1339ftrace_notrace_write(struct file *file, const char __user *ubuf,
1340 size_t cnt, loff_t *ppos)
1341{
1342 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1343}
1344
1345static void
1346ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1347{
1348 if (unlikely(ftrace_disabled))
1349 return;
1350
1351 mutex_lock(&ftrace_regex_lock);
1352 if (reset)
1353 ftrace_filter_reset(enable);
1354 if (buf)
1355 ftrace_match(buf, len, enable);
1356 mutex_unlock(&ftrace_regex_lock);
1357}
1358
Steven Rostedt77a2b372008-05-12 21:20:45 +02001359/**
1360 * ftrace_set_filter - set a function to filter on in ftrace
1361 * @buf - the string that holds the function filter text.
1362 * @len - the length of the string.
1363 * @reset - non zero to reset all filters before applying this filter.
1364 *
1365 * Filters denote which functions should be enabled when tracing is enabled.
1366 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1367 */
Ingo Molnare309b412008-05-12 21:20:51 +02001368void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02001369{
Steven Rostedt41c52c02008-05-22 11:46:33 -04001370 ftrace_set_regex(buf, len, reset, 1);
1371}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001372
Steven Rostedt41c52c02008-05-22 11:46:33 -04001373/**
1374 * ftrace_set_notrace - set a function to not trace in ftrace
1375 * @buf - the string that holds the function notrace text.
1376 * @len - the length of the string.
1377 * @reset - non zero to reset all filters before applying this filter.
1378 *
1379 * Notrace Filters denote which functions should not be enabled when tracing
1380 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1381 * for tracing.
1382 */
1383void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1384{
1385 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02001386}
1387
Ingo Molnare309b412008-05-12 21:20:51 +02001388static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001389ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001390{
1391 struct seq_file *m = (struct seq_file *)file->private_data;
1392 struct ftrace_iterator *iter;
1393
Steven Rostedt41c52c02008-05-22 11:46:33 -04001394 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001395 if (file->f_mode & FMODE_READ) {
1396 iter = m->private;
1397
1398 seq_release(inode, file);
1399 } else
1400 iter = file->private_data;
1401
1402 if (iter->buffer_idx) {
1403 iter->filtered++;
1404 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001405 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001406 }
1407
1408 mutex_lock(&ftrace_sysctl_lock);
1409 mutex_lock(&ftraced_lock);
1410 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1411 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1412 mutex_unlock(&ftraced_lock);
1413 mutex_unlock(&ftrace_sysctl_lock);
1414
1415 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001416 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001417 return 0;
1418}
1419
Steven Rostedt41c52c02008-05-22 11:46:33 -04001420static int
1421ftrace_filter_release(struct inode *inode, struct file *file)
1422{
1423 return ftrace_regex_release(inode, file, 1);
1424}
1425
1426static int
1427ftrace_notrace_release(struct inode *inode, struct file *file)
1428{
1429 return ftrace_regex_release(inode, file, 0);
1430}
1431
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001432static ssize_t
1433ftraced_read(struct file *filp, char __user *ubuf,
1434 size_t cnt, loff_t *ppos)
1435{
1436 /* don't worry about races */
1437 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1438 int r = strlen(buf);
1439
1440 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1441}
1442
1443static ssize_t
1444ftraced_write(struct file *filp, const char __user *ubuf,
1445 size_t cnt, loff_t *ppos)
1446{
1447 char buf[64];
1448 long val;
1449 int ret;
1450
1451 if (cnt >= sizeof(buf))
1452 return -EINVAL;
1453
1454 if (copy_from_user(&buf, ubuf, cnt))
1455 return -EFAULT;
1456
1457 if (strncmp(buf, "enable", 6) == 0)
1458 val = 1;
1459 else if (strncmp(buf, "disable", 7) == 0)
1460 val = 0;
1461 else {
1462 buf[cnt] = 0;
1463
1464 ret = strict_strtoul(buf, 10, &val);
1465 if (ret < 0)
1466 return ret;
1467
1468 val = !!val;
1469 }
1470
1471 if (val)
1472 ftrace_enable_daemon();
1473 else
1474 ftrace_disable_daemon();
1475
1476 filp->f_pos += cnt;
1477
1478 return cnt;
1479}
1480
Steven Rostedt5072c592008-05-12 21:20:43 +02001481static struct file_operations ftrace_avail_fops = {
1482 .open = ftrace_avail_open,
1483 .read = seq_read,
1484 .llseek = seq_lseek,
1485 .release = ftrace_avail_release,
1486};
1487
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301488static struct file_operations ftrace_failures_fops = {
1489 .open = ftrace_failures_open,
1490 .read = seq_read,
1491 .llseek = seq_lseek,
1492 .release = ftrace_avail_release,
1493};
1494
Steven Rostedt5072c592008-05-12 21:20:43 +02001495static struct file_operations ftrace_filter_fops = {
1496 .open = ftrace_filter_open,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001497 .read = ftrace_regex_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02001498 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001499 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02001500 .release = ftrace_filter_release,
1501};
1502
Steven Rostedt41c52c02008-05-22 11:46:33 -04001503static struct file_operations ftrace_notrace_fops = {
1504 .open = ftrace_notrace_open,
1505 .read = ftrace_regex_read,
1506 .write = ftrace_notrace_write,
1507 .llseek = ftrace_regex_lseek,
1508 .release = ftrace_notrace_release,
1509};
1510
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001511static struct file_operations ftraced_fops = {
1512 .open = tracing_open_generic,
1513 .read = ftraced_read,
1514 .write = ftraced_write,
1515};
1516
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001517/**
1518 * ftrace_force_update - force an update to all recording ftrace functions
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001519 */
1520int ftrace_force_update(void)
1521{
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001522 int ret = 0;
1523
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001524 if (unlikely(ftrace_disabled))
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001525 return -ENODEV;
1526
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001527 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001528 mutex_lock(&ftraced_lock);
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001529
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001530 /*
1531 * If ftraced_trigger is not set, then there is nothing
1532 * to update.
1533 */
1534 if (ftraced_trigger && !ftrace_update_code())
1535 ret = -EBUSY;
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001536
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001537 mutex_unlock(&ftraced_lock);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001538 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001539
1540 return ret;
1541}
1542
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001543static void ftrace_force_shutdown(void)
1544{
1545 struct task_struct *task;
1546 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1547
1548 mutex_lock(&ftraced_lock);
1549 task = ftraced_task;
1550 ftraced_task = NULL;
1551 ftraced_suspend = -1;
1552 ftrace_run_update_code(command);
1553 mutex_unlock(&ftraced_lock);
1554
1555 if (task)
1556 kthread_stop(task);
1557}
1558
Steven Rostedt5072c592008-05-12 21:20:43 +02001559static __init int ftrace_init_debugfs(void)
1560{
1561 struct dentry *d_tracer;
1562 struct dentry *entry;
1563
1564 d_tracer = tracing_init_dentry();
1565
1566 entry = debugfs_create_file("available_filter_functions", 0444,
1567 d_tracer, NULL, &ftrace_avail_fops);
1568 if (!entry)
1569 pr_warning("Could not create debugfs "
1570 "'available_filter_functions' entry\n");
1571
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301572 entry = debugfs_create_file("failures", 0444,
1573 d_tracer, NULL, &ftrace_failures_fops);
1574 if (!entry)
1575 pr_warning("Could not create debugfs 'failures' entry\n");
1576
Steven Rostedt5072c592008-05-12 21:20:43 +02001577 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1578 NULL, &ftrace_filter_fops);
1579 if (!entry)
1580 pr_warning("Could not create debugfs "
1581 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04001582
1583 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1584 NULL, &ftrace_notrace_fops);
1585 if (!entry)
1586 pr_warning("Could not create debugfs "
1587 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001588
1589 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1590 NULL, &ftraced_fops);
1591 if (!entry)
1592 pr_warning("Could not create debugfs "
1593 "'ftraced_enabled' entry\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02001594 return 0;
1595}
1596
1597fs_initcall(ftrace_init_debugfs);
1598
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001599#ifdef CONFIG_FTRACE_MCOUNT_RECORD
1600static int ftrace_convert_nops(unsigned long *start,
1601 unsigned long *end)
1602{
1603 unsigned long *p;
1604 unsigned long addr;
1605 unsigned long flags;
1606
1607 p = start;
1608 while (p < end) {
1609 addr = ftrace_call_adjust(*p++);
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001610 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -04001611 spin_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001612 ftrace_record_ip(addr);
Steven Rostedtfed19392008-08-14 22:47:19 -04001613 spin_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001614 ftrace_shutdown_replenish();
1615 }
1616
1617 /* p is ignored */
1618 local_irq_save(flags);
1619 __ftrace_update_code(p);
1620 local_irq_restore(flags);
1621
1622 return 0;
1623}
1624
Steven Rostedt90d595f2008-08-14 15:45:09 -04001625void ftrace_init_module(unsigned long *start, unsigned long *end)
1626{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04001627 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04001628 return;
Steven Rostedt90d595f2008-08-14 15:45:09 -04001629 ftrace_convert_nops(start, end);
1630}
1631
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001632extern unsigned long __start_mcount_loc[];
1633extern unsigned long __stop_mcount_loc[];
1634
1635void __init ftrace_init(void)
1636{
1637 unsigned long count, addr, flags;
1638 int ret;
1639
1640 /* Keep the ftrace pointer to the stub */
1641 addr = (unsigned long)ftrace_stub;
1642
1643 local_irq_save(flags);
1644 ftrace_dyn_arch_init(&addr);
1645 local_irq_restore(flags);
1646
1647 /* ftrace_dyn_arch_init places the return code in addr */
1648 if (addr)
1649 goto failed;
1650
1651 count = __stop_mcount_loc - __start_mcount_loc;
1652
1653 ret = ftrace_dyn_table_alloc(count);
1654 if (ret)
1655 goto failed;
1656
1657 last_ftrace_enabled = ftrace_enabled = 1;
1658
1659 ret = ftrace_convert_nops(__start_mcount_loc,
1660 __stop_mcount_loc);
1661
1662 return;
1663 failed:
1664 ftrace_disabled = 1;
1665}
1666#else /* CONFIG_FTRACE_MCOUNT_RECORD */
Steven Rostedtbd95b882008-10-16 09:31:27 -04001667
1668static void ftrace_release_hash(unsigned long start, unsigned long end)
1669{
1670 struct dyn_ftrace *rec;
1671 struct hlist_node *t, *n;
1672 struct hlist_head *head, temp_list;
1673 unsigned long flags;
1674 int i, cpu;
1675
1676 preempt_disable_notrace();
1677
1678 /* disable incase we call something that calls mcount */
1679 cpu = raw_smp_processor_id();
1680 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
1681
1682 ftrace_hash_lock(flags);
1683
1684 for (i = 0; i < FTRACE_HASHSIZE; i++) {
1685 INIT_HLIST_HEAD(&temp_list);
1686 head = &ftrace_hash[i];
1687
1688 /* all CPUS are stopped, we are safe to modify code */
1689 hlist_for_each_entry_safe(rec, t, n, head, node) {
1690 if (rec->flags & FTRACE_FL_FREE)
1691 continue;
1692
1693 if ((rec->ip >= start) && (rec->ip < end))
1694 ftrace_free_rec(rec);
1695 }
1696 }
1697
1698 ftrace_hash_unlock(flags);
1699
1700 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
1701 preempt_enable_notrace();
1702
1703}
1704
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001705static int ftraced(void *ignore)
1706{
1707 unsigned long usecs;
1708
1709 while (!kthread_should_stop()) {
1710
1711 set_current_state(TASK_INTERRUPTIBLE);
1712
1713 /* check once a second */
1714 schedule_timeout(HZ);
1715
1716 if (unlikely(ftrace_disabled))
1717 continue;
1718
1719 mutex_lock(&ftrace_sysctl_lock);
1720 mutex_lock(&ftraced_lock);
1721 if (!ftraced_suspend && !ftraced_stop &&
1722 ftrace_update_code()) {
1723 usecs = nsecs_to_usecs(ftrace_update_time);
1724 if (ftrace_update_tot_cnt > 100000) {
1725 ftrace_update_tot_cnt = 0;
1726 pr_info("hm, dftrace overflow: %lu change%s"
1727 " (%lu total) in %lu usec%s\n",
1728 ftrace_update_cnt,
1729 ftrace_update_cnt != 1 ? "s" : "",
1730 ftrace_update_tot_cnt,
1731 usecs, usecs != 1 ? "s" : "");
1732 ftrace_disabled = 1;
1733 WARN_ON_ONCE(1);
1734 }
1735 }
1736 mutex_unlock(&ftraced_lock);
1737 mutex_unlock(&ftrace_sysctl_lock);
1738
1739 ftrace_shutdown_replenish();
1740 }
1741 __set_current_state(TASK_RUNNING);
1742 return 0;
1743}
1744
Ingo Molnare309b412008-05-12 21:20:51 +02001745static int __init ftrace_dynamic_init(void)
Steven Rostedt3d083392008-05-12 21:20:42 +02001746{
1747 struct task_struct *p;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001748 unsigned long addr;
Steven Rostedt3d083392008-05-12 21:20:42 +02001749 int ret;
1750
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001751 addr = (unsigned long)ftrace_record_ip;
Ingo Molnar9ff9cdb2008-05-12 21:20:50 +02001752
Rusty Russell784e2d72008-07-28 12:16:31 -05001753 stop_machine(ftrace_dyn_arch_init, &addr, NULL);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001754
1755 /* ftrace_dyn_arch_init places the return code in addr */
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001756 if (addr) {
1757 ret = (int)addr;
1758 goto failed;
1759 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001760
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001761 ret = ftrace_dyn_table_alloc(NR_TO_INIT);
Steven Rostedt3d083392008-05-12 21:20:42 +02001762 if (ret)
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001763 goto failed;
Steven Rostedt3d083392008-05-12 21:20:42 +02001764
1765 p = kthread_run(ftraced, NULL, "ftraced");
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001766 if (IS_ERR(p)) {
1767 ret = -1;
1768 goto failed;
1769 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001770
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001771 last_ftrace_enabled = ftrace_enabled = 1;
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001772 ftraced_task = p;
Steven Rostedt3d083392008-05-12 21:20:42 +02001773
1774 return 0;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001775
1776 failed:
1777 ftrace_disabled = 1;
1778 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02001779}
1780
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001781core_initcall(ftrace_dynamic_init);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001782#endif /* CONFIG_FTRACE_MCOUNT_RECORD */
1783
Steven Rostedt3d083392008-05-12 21:20:42 +02001784#else
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001785# define ftrace_startup() do { } while (0)
1786# define ftrace_shutdown() do { } while (0)
1787# define ftrace_startup_sysctl() do { } while (0)
1788# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001789# define ftrace_force_shutdown() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02001790#endif /* CONFIG_DYNAMIC_FTRACE */
1791
1792/**
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001793 * ftrace_kill_atomic - kill ftrace from critical sections
1794 *
1795 * This function should be used by panic code. It stops ftrace
1796 * but in a not so nice way. If you need to simply kill ftrace
1797 * from a non-atomic section, use ftrace_kill.
1798 */
1799void ftrace_kill_atomic(void)
1800{
1801 ftrace_disabled = 1;
1802 ftrace_enabled = 0;
Ingo Molnarb2613e32008-07-11 16:44:27 +02001803#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001804 ftraced_suspend = -1;
Ingo Molnarb2613e32008-07-11 16:44:27 +02001805#endif
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001806 clear_ftrace_function();
1807}
1808
1809/**
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001810 * ftrace_kill - totally shutdown ftrace
1811 *
1812 * This is a safety measure. If something was detected that seems
1813 * wrong, calling this function will keep ftrace from doing
1814 * any more modifications, and updates.
1815 * used when something went wrong.
1816 */
1817void ftrace_kill(void)
1818{
1819 mutex_lock(&ftrace_sysctl_lock);
1820 ftrace_disabled = 1;
1821 ftrace_enabled = 0;
1822
1823 clear_ftrace_function();
1824 mutex_unlock(&ftrace_sysctl_lock);
1825
1826 /* Try to totally disable ftrace */
1827 ftrace_force_shutdown();
1828}
1829
1830/**
Steven Rostedt3d083392008-05-12 21:20:42 +02001831 * register_ftrace_function - register a function for profiling
1832 * @ops - ops structure that holds the function for profiling.
1833 *
1834 * Register a function to be called by all functions in the
1835 * kernel.
1836 *
1837 * Note: @ops->func and all the functions it calls must be labeled
1838 * with "notrace", otherwise it will go into a
1839 * recursive loop.
1840 */
1841int register_ftrace_function(struct ftrace_ops *ops)
1842{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001843 int ret;
1844
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001845 if (unlikely(ftrace_disabled))
1846 return -1;
1847
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001848 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001849 ret = __register_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001850 ftrace_startup();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001851 mutex_unlock(&ftrace_sysctl_lock);
1852
1853 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02001854}
1855
1856/**
1857 * unregister_ftrace_function - unresgister a function for profiling.
1858 * @ops - ops structure that holds the function to unregister
1859 *
1860 * Unregister a function that was added to be called by ftrace profiling.
1861 */
1862int unregister_ftrace_function(struct ftrace_ops *ops)
1863{
1864 int ret;
1865
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001866 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001867 ret = __unregister_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001868 ftrace_shutdown();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001869 mutex_unlock(&ftrace_sysctl_lock);
1870
1871 return ret;
1872}
1873
Ingo Molnare309b412008-05-12 21:20:51 +02001874int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001875ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02001876 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001877 loff_t *ppos)
1878{
1879 int ret;
1880
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001881 if (unlikely(ftrace_disabled))
1882 return -ENODEV;
1883
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001884 mutex_lock(&ftrace_sysctl_lock);
1885
Steven Rostedt5072c592008-05-12 21:20:43 +02001886 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001887
1888 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1889 goto out;
1890
1891 last_ftrace_enabled = ftrace_enabled;
1892
1893 if (ftrace_enabled) {
1894
1895 ftrace_startup_sysctl();
1896
1897 /* we are starting ftrace again */
1898 if (ftrace_list != &ftrace_list_end) {
1899 if (ftrace_list->next == &ftrace_list_end)
1900 ftrace_trace_function = ftrace_list->func;
1901 else
1902 ftrace_trace_function = ftrace_list_func;
1903 }
1904
1905 } else {
1906 /* stopping ftrace calls (just send to ftrace_stub) */
1907 ftrace_trace_function = ftrace_stub;
1908
1909 ftrace_shutdown_sysctl();
1910 }
1911
1912 out:
1913 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001914 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001915}