blob: 7694f3e597976ed77d0428666e1f01ffe82bbcaa [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 Rostedt99ecdc42008-08-15 21:40:05 -0400167#else
168/* This is protected via the ftrace_lock with MCOUNT_RECORD. */
Ingo Molnarac8825e2008-08-25 08:12:04 +0200169#define ftrace_hash_lock(flags) do { (void)(flags); } while (0)
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400170#define ftrace_hash_unlock(flags) do { } while(0)
171#endif
172
Steven Noonan71c67d52008-09-20 01:00:37 -0700173/*
174 * Since MCOUNT_ADDR may point to mcount itself, we do not want
175 * to get it confused by reading a reference in the code as we
176 * are parsing on objcopy output of text. Use a variable for
177 * it instead.
178 */
179static unsigned long mcount_addr = MCOUNT_ADDR;
180
Steven Rostedte1c08bd2008-05-12 21:20:44 +0200181static struct task_struct *ftraced_task;
Steven Rostedte1c08bd2008-05-12 21:20:44 +0200182
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200183enum {
184 FTRACE_ENABLE_CALLS = (1 << 0),
185 FTRACE_DISABLE_CALLS = (1 << 1),
186 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
187 FTRACE_ENABLE_MCOUNT = (1 << 3),
188 FTRACE_DISABLE_MCOUNT = (1 << 4),
189};
190
Steven Rostedt5072c592008-05-12 21:20:43 +0200191static int ftrace_filtered;
Abhishek Sagarecea6562008-06-21 23:47:53 +0530192static int tracing_on;
193static int frozen_record_count;
Steven Rostedt5072c592008-05-12 21:20:43 +0200194
Steven Rostedt3d083392008-05-12 21:20:42 +0200195static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
196
197static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
198
Steven Rostedt3d083392008-05-12 21:20:42 +0200199static DEFINE_MUTEX(ftraced_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400200static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200201
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200202struct ftrace_page {
203 struct ftrace_page *next;
David Milleraa5e5ce2008-05-13 22:06:56 -0700204 unsigned long index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200205 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700206};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200207
208#define ENTRIES_PER_PAGE \
209 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
210
211/* estimate from running different kernels */
212#define NR_TO_INIT 10000
213
214static struct ftrace_page *ftrace_pages_start;
215static struct ftrace_page *ftrace_pages;
216
Steven Rostedt3d083392008-05-12 21:20:42 +0200217static int ftraced_trigger;
218static int ftraced_suspend;
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400219static int ftraced_stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200220
221static int ftrace_record_suspend;
222
Steven Rostedt37ad5082008-05-12 21:20:48 +0200223static struct dyn_ftrace *ftrace_free_records;
224
Abhishek Sagarecea6562008-06-21 23:47:53 +0530225
226#ifdef CONFIG_KPROBES
227static inline void freeze_record(struct dyn_ftrace *rec)
228{
229 if (!(rec->flags & FTRACE_FL_FROZEN)) {
230 rec->flags |= FTRACE_FL_FROZEN;
231 frozen_record_count++;
232 }
233}
234
235static inline void unfreeze_record(struct dyn_ftrace *rec)
236{
237 if (rec->flags & FTRACE_FL_FROZEN) {
238 rec->flags &= ~FTRACE_FL_FROZEN;
239 frozen_record_count--;
240 }
241}
242
243static inline int record_frozen(struct dyn_ftrace *rec)
244{
245 return rec->flags & FTRACE_FL_FROZEN;
246}
247#else
248# define freeze_record(rec) ({ 0; })
249# define unfreeze_record(rec) ({ 0; })
250# define record_frozen(rec) ({ 0; })
251#endif /* CONFIG_KPROBES */
252
253int skip_trace(unsigned long ip)
254{
255 unsigned long fl;
256 struct dyn_ftrace *rec;
257 struct hlist_node *t;
258 struct hlist_head *head;
259
260 if (frozen_record_count == 0)
261 return 0;
262
263 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
264 hlist_for_each_entry_rcu(rec, t, head, node) {
265 if (rec->ip == ip) {
266 if (record_frozen(rec)) {
267 if (rec->flags & FTRACE_FL_FAILED)
268 return 1;
269
270 if (!(rec->flags & FTRACE_FL_CONVERTED))
271 return 1;
272
273 if (!tracing_on || !ftrace_enabled)
274 return 1;
275
276 if (ftrace_filtered) {
277 fl = rec->flags & (FTRACE_FL_FILTER |
278 FTRACE_FL_NOTRACE);
279 if (!fl || (fl & FTRACE_FL_NOTRACE))
280 return 1;
281 }
282 }
283 break;
284 }
285 }
286
287 return 0;
288}
289
Ingo Molnare309b412008-05-12 21:20:51 +0200290static inline int
Ingo Molnar9ff9cdb2008-05-12 21:20:50 +0200291ftrace_ip_in_hash(unsigned long ip, unsigned long key)
Steven Rostedt3d083392008-05-12 21:20:42 +0200292{
293 struct dyn_ftrace *p;
294 struct hlist_node *t;
295 int found = 0;
296
Abhishek Sagarffdaa352008-05-24 23:45:02 +0530297 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
Steven Rostedt3d083392008-05-12 21:20:42 +0200298 if (p->ip == ip) {
299 found = 1;
300 break;
301 }
302 }
303
304 return found;
305}
306
Ingo Molnare309b412008-05-12 21:20:51 +0200307static inline void
Steven Rostedt3d083392008-05-12 21:20:42 +0200308ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
309{
Abhishek Sagarffdaa352008-05-24 23:45:02 +0530310 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
Steven Rostedt3d083392008-05-12 21:20:42 +0200311}
312
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530313/* called from kstop_machine */
314static inline void ftrace_del_hash(struct dyn_ftrace *node)
315{
316 hlist_del(&node->node);
317}
318
Ingo Molnare309b412008-05-12 21:20:51 +0200319static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200320{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200321 rec->ip = (unsigned long)ftrace_free_records;
322 ftrace_free_records = rec;
323 rec->flags |= FTRACE_FL_FREE;
324}
325
Steven Rostedtfed19392008-08-14 22:47:19 -0400326void ftrace_release(void *start, unsigned long size)
327{
328 struct dyn_ftrace *rec;
329 struct ftrace_page *pg;
330 unsigned long s = (unsigned long)start;
331 unsigned long e = s + size;
332 int i;
333
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400334 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400335 return;
336
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400337 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -0400338 spin_lock(&ftrace_lock);
339
340 for (pg = ftrace_pages_start; pg; pg = pg->next) {
341 for (i = 0; i < pg->index; i++) {
342 rec = &pg->records[i];
343
344 if ((rec->ip >= s) && (rec->ip < e))
345 ftrace_free_rec(rec);
346 }
347 }
348 spin_unlock(&ftrace_lock);
349
350}
351
Ingo Molnare309b412008-05-12 21:20:51 +0200352static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200353{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200354 struct dyn_ftrace *rec;
355
356 /* First check for freed records */
357 if (ftrace_free_records) {
358 rec = ftrace_free_records;
359
Steven Rostedt37ad5082008-05-12 21:20:48 +0200360 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
361 WARN_ON_ONCE(1);
362 ftrace_free_records = NULL;
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200363 ftrace_disabled = 1;
364 ftrace_enabled = 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200365 return NULL;
366 }
367
368 ftrace_free_records = (void *)rec->ip;
369 memset(rec, 0, sizeof(*rec));
370 return rec;
371 }
372
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200373 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
374 if (!ftrace_pages->next)
375 return NULL;
376 ftrace_pages = ftrace_pages->next;
377 }
378
379 return &ftrace_pages->records[ftrace_pages->index++];
380}
381
Ingo Molnare309b412008-05-12 21:20:51 +0200382static void
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200383ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200384{
385 struct dyn_ftrace *node;
386 unsigned long flags;
387 unsigned long key;
388 int resched;
Steven Rostedt2bb6f8d2008-05-12 21:21:02 +0200389 int cpu;
Steven Rostedt3d083392008-05-12 21:20:42 +0200390
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200391 if (!ftrace_enabled || ftrace_disabled)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200392 return;
393
Steven Rostedt3d083392008-05-12 21:20:42 +0200394 resched = need_resched();
395 preempt_disable_notrace();
396
Steven Rostedt2bb6f8d2008-05-12 21:21:02 +0200397 /*
398 * We simply need to protect against recursion.
399 * Use the the raw version of smp_processor_id and not
400 * __get_cpu_var which can call debug hooks that can
401 * cause a recursive crash here.
402 */
403 cpu = raw_smp_processor_id();
404 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
405 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
Steven Rostedt3d083392008-05-12 21:20:42 +0200406 goto out;
407
408 if (unlikely(ftrace_record_suspend))
409 goto out;
410
411 key = hash_long(ip, FTRACE_HASHBITS);
412
413 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
414
415 if (ftrace_ip_in_hash(ip, key))
416 goto out;
417
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400418 ftrace_hash_lock(flags);
Steven Rostedt3d083392008-05-12 21:20:42 +0200419
420 /* This ip may have hit the hash before the lock */
421 if (ftrace_ip_in_hash(ip, key))
422 goto out_unlock;
423
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200424 node = ftrace_alloc_dyn_node(ip);
Steven Rostedt3d083392008-05-12 21:20:42 +0200425 if (!node)
426 goto out_unlock;
427
428 node->ip = ip;
429
430 ftrace_add_hash(node, key);
431
432 ftraced_trigger = 1;
433
434 out_unlock:
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400435 ftrace_hash_unlock(flags);
Steven Rostedt3d083392008-05-12 21:20:42 +0200436 out:
Steven Rostedt2bb6f8d2008-05-12 21:21:02 +0200437 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
Steven Rostedt3d083392008-05-12 21:20:42 +0200438
439 /* prevent recursion with scheduler */
440 if (resched)
441 preempt_enable_no_resched_notrace();
442 else
443 preempt_enable_notrace();
444}
445
Steven Rostedtcaf8cde2008-05-12 21:20:50 +0200446#define FTRACE_ADDR ((long)(ftrace_caller))
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200447
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530448static int
Steven Rostedt5072c592008-05-12 21:20:43 +0200449__ftrace_replace_code(struct dyn_ftrace *rec,
450 unsigned char *old, unsigned char *new, int enable)
451{
Steven Rostedt41c52c02008-05-22 11:46:33 -0400452 unsigned long ip, fl;
Steven Rostedt5072c592008-05-12 21:20:43 +0200453
454 ip = rec->ip;
455
456 if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200457 /*
458 * If filtering is on:
459 *
460 * If this record is set to be filtered and
461 * is enabled then do nothing.
462 *
463 * If this record is set to be filtered and
464 * it is not enabled, enable it.
465 *
466 * If this record is not set to be filtered
467 * and it is not enabled do nothing.
468 *
Steven Rostedt41c52c02008-05-22 11:46:33 -0400469 * If this record is set not to trace then
470 * do nothing.
471 *
Abhishek Sagara4500b82008-06-14 11:59:39 +0530472 * If this record is set not to trace and
473 * it is enabled then disable it.
474 *
Steven Rostedt5072c592008-05-12 21:20:43 +0200475 * If this record is not set to be filtered and
476 * it is enabled, disable it.
477 */
Abhishek Sagara4500b82008-06-14 11:59:39 +0530478
479 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
480 FTRACE_FL_ENABLED);
Steven Rostedt5072c592008-05-12 21:20:43 +0200481
482 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
Abhishek Sagara4500b82008-06-14 11:59:39 +0530483 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
484 !fl || (fl == FTRACE_FL_NOTRACE))
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530485 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200486
487 /*
488 * If it is enabled disable it,
489 * otherwise enable it!
490 */
Abhishek Sagara4500b82008-06-14 11:59:39 +0530491 if (fl & FTRACE_FL_ENABLED) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200492 /* swap new and old */
493 new = old;
494 old = ftrace_call_replace(ip, FTRACE_ADDR);
495 rec->flags &= ~FTRACE_FL_ENABLED;
496 } else {
497 new = ftrace_call_replace(ip, FTRACE_ADDR);
498 rec->flags |= FTRACE_FL_ENABLED;
499 }
500 } else {
501
Steven Rostedt41c52c02008-05-22 11:46:33 -0400502 if (enable) {
503 /*
504 * If this record is set not to trace and is
505 * not enabled, do nothing.
506 */
507 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
508 if (fl == FTRACE_FL_NOTRACE)
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530509 return 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400510
Steven Rostedt5072c592008-05-12 21:20:43 +0200511 new = ftrace_call_replace(ip, FTRACE_ADDR);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400512 } else
Steven Rostedt5072c592008-05-12 21:20:43 +0200513 old = ftrace_call_replace(ip, FTRACE_ADDR);
514
515 if (enable) {
516 if (rec->flags & FTRACE_FL_ENABLED)
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530517 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200518 rec->flags |= FTRACE_FL_ENABLED;
519 } else {
520 if (!(rec->flags & FTRACE_FL_ENABLED))
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530521 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200522 rec->flags &= ~FTRACE_FL_ENABLED;
523 }
524 }
525
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530526 return ftrace_modify_code(ip, old, new);
Steven Rostedt5072c592008-05-12 21:20:43 +0200527}
528
Ingo Molnare309b412008-05-12 21:20:51 +0200529static void ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200530{
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530531 int i, failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200532 unsigned char *new = NULL, *old = NULL;
533 struct dyn_ftrace *rec;
534 struct ftrace_page *pg;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200535
Steven Rostedt5072c592008-05-12 21:20:43 +0200536 if (enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200537 old = ftrace_nop_replace();
538 else
539 new = ftrace_nop_replace();
540
541 for (pg = ftrace_pages_start; pg; pg = pg->next) {
542 for (i = 0; i < pg->index; i++) {
543 rec = &pg->records[i];
544
545 /* don't modify code that has already faulted */
546 if (rec->flags & FTRACE_FL_FAILED)
547 continue;
548
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530549 /* ignore updates to this record's mcount site */
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530550 if (get_kprobe((void *)rec->ip)) {
551 freeze_record(rec);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530552 continue;
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530553 } else {
554 unfreeze_record(rec);
555 }
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530556
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530557 failed = __ftrace_replace_code(rec, old, new, enable);
558 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
559 rec->flags |= FTRACE_FL_FAILED;
560 if ((system_state == SYSTEM_BOOTING) ||
Abhishek Sagar34078a52008-06-03 08:33:41 +0530561 !core_kernel_text(rec->ip)) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530562 ftrace_del_hash(rec);
563 ftrace_free_rec(rec);
564 }
565 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200566 }
567 }
568}
569
Ingo Molnare309b412008-05-12 21:20:51 +0200570static void ftrace_shutdown_replenish(void)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200571{
572 if (ftrace_pages->next)
573 return;
574
575 /* allocate another page */
576 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
577}
Steven Rostedt3d083392008-05-12 21:20:42 +0200578
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530579static int
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200580ftrace_code_disable(struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200581{
582 unsigned long ip;
583 unsigned char *nop, *call;
584 int failed;
585
586 ip = rec->ip;
587
588 nop = ftrace_nop_replace();
Steven Rostedt3b47bfc2008-08-27 23:24:15 -0400589 call = ftrace_call_replace(ip, mcount_addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200590
591 failed = ftrace_modify_code(ip, call, nop);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200592 if (failed) {
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200593 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530594 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200595 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530596 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200597}
598
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400599static int __ftrace_update_code(void *ignore);
600
Ingo Molnare309b412008-05-12 21:20:51 +0200601static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200602{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200603 unsigned long addr;
604 int *command = data;
605
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400606 if (*command & FTRACE_ENABLE_CALLS) {
607 /*
608 * Update any recorded ips now that we have the
609 * machine stopped
610 */
611 __ftrace_update_code(NULL);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200612 ftrace_replace_code(1);
Abhishek Sagarecea6562008-06-21 23:47:53 +0530613 tracing_on = 1;
614 } else if (*command & FTRACE_DISABLE_CALLS) {
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200615 ftrace_replace_code(0);
Abhishek Sagarecea6562008-06-21 23:47:53 +0530616 tracing_on = 0;
617 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200618
619 if (*command & FTRACE_UPDATE_TRACE_FUNC)
620 ftrace_update_ftrace_func(ftrace_trace_function);
621
622 if (*command & FTRACE_ENABLE_MCOUNT) {
623 addr = (unsigned long)ftrace_record_ip;
624 ftrace_mcount_set(&addr);
625 } else if (*command & FTRACE_DISABLE_MCOUNT) {
626 addr = (unsigned long)ftrace_stub;
627 ftrace_mcount_set(&addr);
628 }
629
630 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200631}
632
Ingo Molnare309b412008-05-12 21:20:51 +0200633static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200634{
Rusty Russell784e2d72008-07-28 12:16:31 -0500635 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt3d083392008-05-12 21:20:42 +0200636}
637
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400638void ftrace_disable_daemon(void)
639{
640 /* Stop the daemon from calling kstop_machine */
641 mutex_lock(&ftraced_lock);
642 ftraced_stop = 1;
643 mutex_unlock(&ftraced_lock);
644
645 ftrace_force_update();
646}
647
648void ftrace_enable_daemon(void)
649{
650 mutex_lock(&ftraced_lock);
651 ftraced_stop = 0;
652 mutex_unlock(&ftraced_lock);
653
654 ftrace_force_update();
655}
656
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200657static ftrace_func_t saved_ftrace_func;
658
Ingo Molnare309b412008-05-12 21:20:51 +0200659static void ftrace_startup(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200660{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200661 int command = 0;
662
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200663 if (unlikely(ftrace_disabled))
664 return;
665
Steven Rostedt3d083392008-05-12 21:20:42 +0200666 mutex_lock(&ftraced_lock);
667 ftraced_suspend++;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200668 if (ftraced_suspend == 1)
669 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +0200670
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200671 if (saved_ftrace_func != ftrace_trace_function) {
672 saved_ftrace_func = ftrace_trace_function;
673 command |= FTRACE_UPDATE_TRACE_FUNC;
674 }
675
676 if (!command || !ftrace_enabled)
677 goto out;
678
679 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200680 out:
681 mutex_unlock(&ftraced_lock);
682}
683
Ingo Molnare309b412008-05-12 21:20:51 +0200684static void ftrace_shutdown(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200685{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200686 int command = 0;
687
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200688 if (unlikely(ftrace_disabled))
689 return;
690
Steven Rostedt3d083392008-05-12 21:20:42 +0200691 mutex_lock(&ftraced_lock);
692 ftraced_suspend--;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200693 if (!ftraced_suspend)
694 command |= FTRACE_DISABLE_CALLS;
695
696 if (saved_ftrace_func != ftrace_trace_function) {
697 saved_ftrace_func = ftrace_trace_function;
698 command |= FTRACE_UPDATE_TRACE_FUNC;
699 }
700
701 if (!command || !ftrace_enabled)
Steven Rostedt3d083392008-05-12 21:20:42 +0200702 goto out;
703
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200704 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200705 out:
706 mutex_unlock(&ftraced_lock);
707}
708
Ingo Molnare309b412008-05-12 21:20:51 +0200709static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200710{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200711 int command = FTRACE_ENABLE_MCOUNT;
712
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200713 if (unlikely(ftrace_disabled))
714 return;
715
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200716 mutex_lock(&ftraced_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200717 /* Force update next time */
718 saved_ftrace_func = NULL;
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200719 /* ftraced_suspend is true if we want ftrace running */
720 if (ftraced_suspend)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200721 command |= FTRACE_ENABLE_CALLS;
722
723 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200724 mutex_unlock(&ftraced_lock);
725}
726
Ingo Molnare309b412008-05-12 21:20:51 +0200727static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200728{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200729 int command = FTRACE_DISABLE_MCOUNT;
730
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200731 if (unlikely(ftrace_disabled))
732 return;
733
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200734 mutex_lock(&ftraced_lock);
735 /* ftraced_suspend is true if ftrace is running */
736 if (ftraced_suspend)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200737 command |= FTRACE_DISABLE_CALLS;
738
739 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200740 mutex_unlock(&ftraced_lock);
741}
742
Steven Rostedt3d083392008-05-12 21:20:42 +0200743static cycle_t ftrace_update_time;
744static unsigned long ftrace_update_cnt;
745unsigned long ftrace_update_tot_cnt;
746
Ingo Molnare309b412008-05-12 21:20:51 +0200747static int __ftrace_update_code(void *ignore)
Steven Rostedt3d083392008-05-12 21:20:42 +0200748{
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530749 int i, save_ftrace_enabled;
750 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200751 struct dyn_ftrace *p;
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530752 struct hlist_node *t, *n;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530753 struct hlist_head *head, temp_list;
Steven Rostedt3d083392008-05-12 21:20:42 +0200754
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200755 /* Don't be recording funcs now */
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400756 ftrace_record_suspend++;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200757 save_ftrace_enabled = ftrace_enabled;
758 ftrace_enabled = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200759
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200760 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200761 ftrace_update_cnt = 0;
762
763 /* No locks needed, the machine is stopped! */
764 for (i = 0; i < FTRACE_HASHSIZE; i++) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530765 INIT_HLIST_HEAD(&temp_list);
766 head = &ftrace_hash[i];
767
Steven Rostedt3d083392008-05-12 21:20:42 +0200768 /* all CPUS are stopped, we are safe to modify code */
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530769 hlist_for_each_entry_safe(p, t, n, head, node) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530770 /* Skip over failed records which have not been
771 * freed. */
772 if (p->flags & FTRACE_FL_FAILED)
773 continue;
Steven Rostedt3d083392008-05-12 21:20:42 +0200774
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530775 /* Unconverted records are always at the head of the
776 * hash bucket. Once we encounter a converted record,
777 * simply skip over to the next bucket. Saves ftraced
778 * some processor cycles (ftrace does its bid for
779 * global warming :-p ). */
780 if (p->flags & (FTRACE_FL_CONVERTED))
781 break;
782
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530783 /* Ignore updates to this record's mcount site.
784 * Reintroduce this record at the head of this
785 * bucket to attempt to "convert" it again if
786 * the kprobe on it is unregistered before the
787 * next run. */
788 if (get_kprobe((void *)p->ip)) {
789 ftrace_del_hash(p);
790 INIT_HLIST_NODE(&p->node);
791 hlist_add_head(&p->node, &temp_list);
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530792 freeze_record(p);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530793 continue;
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530794 } else {
795 unfreeze_record(p);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530796 }
797
798 /* convert record (i.e, patch mcount-call with NOP) */
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530799 if (ftrace_code_disable(p)) {
800 p->flags |= FTRACE_FL_CONVERTED;
801 ftrace_update_cnt++;
802 } else {
803 if ((system_state == SYSTEM_BOOTING) ||
Abhishek Sagar34078a52008-06-03 08:33:41 +0530804 !core_kernel_text(p->ip)) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530805 ftrace_del_hash(p);
806 ftrace_free_rec(p);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530807 }
808 }
809 }
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530810
811 hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
812 hlist_del(&p->node);
813 INIT_HLIST_NODE(&p->node);
814 hlist_add_head(&p->node, head);
815 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200816 }
817
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200818 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200819 ftrace_update_time = stop - start;
820 ftrace_update_tot_cnt += ftrace_update_cnt;
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400821 ftraced_trigger = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200822
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200823 ftrace_enabled = save_ftrace_enabled;
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400824 ftrace_record_suspend--;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200825
826 return 0;
827}
828
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400829static int ftrace_update_code(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200830{
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400831 if (unlikely(ftrace_disabled) ||
832 !ftrace_enabled || !ftraced_trigger)
833 return 0;
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200834
Rusty Russell784e2d72008-07-28 12:16:31 -0500835 stop_machine(__ftrace_update_code, NULL, NULL);
Steven Rostedtad90c0e2008-05-27 20:48:37 -0400836
837 return 1;
Steven Rostedt3d083392008-05-12 21:20:42 +0200838}
839
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400840static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200841{
842 struct ftrace_page *pg;
843 int cnt;
844 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200845
846 /* allocate a few pages */
847 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
848 if (!ftrace_pages_start)
849 return -1;
850
851 /*
852 * Allocate a few more pages.
853 *
854 * TODO: have some parser search vmlinux before
855 * final linking to find all calls to ftrace.
856 * Then we can:
857 * a) know how many pages to allocate.
858 * and/or
859 * b) set up the table then.
860 *
861 * The dynamic code is still necessary for
862 * modules.
863 */
864
865 pg = ftrace_pages = ftrace_pages_start;
866
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400867 cnt = num_to_init / ENTRIES_PER_PAGE;
868 pr_info("ftrace: allocating %ld hash entries in %d pages\n",
869 num_to_init, cnt);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200870
871 for (i = 0; i < cnt; i++) {
872 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
873
874 /* If we fail, we'll try later anyway */
875 if (!pg->next)
876 break;
877
878 pg = pg->next;
879 }
880
881 return 0;
882}
883
Steven Rostedt5072c592008-05-12 21:20:43 +0200884enum {
885 FTRACE_ITER_FILTER = (1 << 0),
886 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -0400887 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530888 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +0200889};
890
891#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
892
893struct ftrace_iterator {
894 loff_t pos;
895 struct ftrace_page *pg;
896 unsigned idx;
897 unsigned flags;
898 unsigned char buffer[FTRACE_BUFF_MAX+1];
899 unsigned buffer_idx;
900 unsigned filtered;
901};
902
Ingo Molnare309b412008-05-12 21:20:51 +0200903static void *
Steven Rostedt5072c592008-05-12 21:20:43 +0200904t_next(struct seq_file *m, void *v, loff_t *pos)
905{
906 struct ftrace_iterator *iter = m->private;
907 struct dyn_ftrace *rec = NULL;
908
909 (*pos)++;
910
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400911 /* should not be called from interrupt context */
912 spin_lock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200913 retry:
914 if (iter->idx >= iter->pg->index) {
915 if (iter->pg->next) {
916 iter->pg = iter->pg->next;
917 iter->idx = 0;
918 goto retry;
919 }
920 } else {
921 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -0400922 if ((rec->flags & FTRACE_FL_FREE) ||
923
924 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530925 (rec->flags & FTRACE_FL_FAILED)) ||
926
927 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -0400928 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530929
Steven Rostedt41c52c02008-05-22 11:46:33 -0400930 ((iter->flags & FTRACE_ITER_NOTRACE) &&
931 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200932 rec = NULL;
933 goto retry;
934 }
935 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400936 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200937
938 iter->pos = *pos;
939
940 return rec;
941}
942
943static void *t_start(struct seq_file *m, loff_t *pos)
944{
945 struct ftrace_iterator *iter = m->private;
946 void *p = NULL;
947 loff_t l = -1;
948
949 if (*pos != iter->pos) {
950 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
951 ;
952 } else {
953 l = *pos;
954 p = t_next(m, p, &l);
955 }
956
957 return p;
958}
959
960static void t_stop(struct seq_file *m, void *p)
961{
962}
963
964static int t_show(struct seq_file *m, void *v)
965{
966 struct dyn_ftrace *rec = v;
967 char str[KSYM_SYMBOL_LEN];
968
969 if (!rec)
970 return 0;
971
972 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
973
974 seq_printf(m, "%s\n", str);
975
976 return 0;
977}
978
979static struct seq_operations show_ftrace_seq_ops = {
980 .start = t_start,
981 .next = t_next,
982 .stop = t_stop,
983 .show = t_show,
984};
985
Ingo Molnare309b412008-05-12 21:20:51 +0200986static int
Steven Rostedt5072c592008-05-12 21:20:43 +0200987ftrace_avail_open(struct inode *inode, struct file *file)
988{
989 struct ftrace_iterator *iter;
990 int ret;
991
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200992 if (unlikely(ftrace_disabled))
993 return -ENODEV;
994
Steven Rostedt5072c592008-05-12 21:20:43 +0200995 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
996 if (!iter)
997 return -ENOMEM;
998
999 iter->pg = ftrace_pages_start;
1000 iter->pos = -1;
1001
1002 ret = seq_open(file, &show_ftrace_seq_ops);
1003 if (!ret) {
1004 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001005
Steven Rostedt5072c592008-05-12 21:20:43 +02001006 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001007 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001008 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001009 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001010
1011 return ret;
1012}
1013
1014int ftrace_avail_release(struct inode *inode, struct file *file)
1015{
1016 struct seq_file *m = (struct seq_file *)file->private_data;
1017 struct ftrace_iterator *iter = m->private;
1018
1019 seq_release(inode, file);
1020 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001021
Steven Rostedt5072c592008-05-12 21:20:43 +02001022 return 0;
1023}
1024
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301025static int
1026ftrace_failures_open(struct inode *inode, struct file *file)
1027{
1028 int ret;
1029 struct seq_file *m;
1030 struct ftrace_iterator *iter;
1031
1032 ret = ftrace_avail_open(inode, file);
1033 if (!ret) {
1034 m = (struct seq_file *)file->private_data;
1035 iter = (struct ftrace_iterator *)m->private;
1036 iter->flags = FTRACE_ITER_FAILURES;
1037 }
1038
1039 return ret;
1040}
1041
1042
Steven Rostedt41c52c02008-05-22 11:46:33 -04001043static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001044{
1045 struct ftrace_page *pg;
1046 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001047 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001048 unsigned i;
1049
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001050 /* should not be called from interrupt context */
1051 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001052 if (enable)
1053 ftrace_filtered = 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02001054 pg = ftrace_pages_start;
1055 while (pg) {
1056 for (i = 0; i < pg->index; i++) {
1057 rec = &pg->records[i];
1058 if (rec->flags & FTRACE_FL_FAILED)
1059 continue;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001060 rec->flags &= ~type;
Steven Rostedt5072c592008-05-12 21:20:43 +02001061 }
1062 pg = pg->next;
1063 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001064 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001065}
1066
Ingo Molnare309b412008-05-12 21:20:51 +02001067static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001068ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001069{
1070 struct ftrace_iterator *iter;
1071 int ret = 0;
1072
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001073 if (unlikely(ftrace_disabled))
1074 return -ENODEV;
1075
Steven Rostedt5072c592008-05-12 21:20:43 +02001076 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1077 if (!iter)
1078 return -ENOMEM;
1079
Steven Rostedt41c52c02008-05-22 11:46:33 -04001080 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001081 if ((file->f_mode & FMODE_WRITE) &&
1082 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001083 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001084
1085 if (file->f_mode & FMODE_READ) {
1086 iter->pg = ftrace_pages_start;
1087 iter->pos = -1;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001088 iter->flags = enable ? FTRACE_ITER_FILTER :
1089 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001090
1091 ret = seq_open(file, &show_ftrace_seq_ops);
1092 if (!ret) {
1093 struct seq_file *m = file->private_data;
1094 m->private = iter;
1095 } else
1096 kfree(iter);
1097 } else
1098 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001099 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001100
1101 return ret;
1102}
1103
Steven Rostedt41c52c02008-05-22 11:46:33 -04001104static int
1105ftrace_filter_open(struct inode *inode, struct file *file)
1106{
1107 return ftrace_regex_open(inode, file, 1);
1108}
1109
1110static int
1111ftrace_notrace_open(struct inode *inode, struct file *file)
1112{
1113 return ftrace_regex_open(inode, file, 0);
1114}
1115
Ingo Molnare309b412008-05-12 21:20:51 +02001116static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001117ftrace_regex_read(struct file *file, char __user *ubuf,
Steven Rostedt5072c592008-05-12 21:20:43 +02001118 size_t cnt, loff_t *ppos)
1119{
1120 if (file->f_mode & FMODE_READ)
1121 return seq_read(file, ubuf, cnt, ppos);
1122 else
1123 return -EPERM;
1124}
1125
Ingo Molnare309b412008-05-12 21:20:51 +02001126static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001127ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001128{
1129 loff_t ret;
1130
1131 if (file->f_mode & FMODE_READ)
1132 ret = seq_lseek(file, offset, origin);
1133 else
1134 file->f_pos = ret = 1;
1135
1136 return ret;
1137}
1138
1139enum {
1140 MATCH_FULL,
1141 MATCH_FRONT_ONLY,
1142 MATCH_MIDDLE_ONLY,
1143 MATCH_END_ONLY,
1144};
1145
Ingo Molnare309b412008-05-12 21:20:51 +02001146static void
Steven Rostedt41c52c02008-05-22 11:46:33 -04001147ftrace_match(unsigned char *buff, int len, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001148{
1149 char str[KSYM_SYMBOL_LEN];
1150 char *search = NULL;
1151 struct ftrace_page *pg;
1152 struct dyn_ftrace *rec;
1153 int type = MATCH_FULL;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001154 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001155 unsigned i, match = 0, search_len = 0;
1156
1157 for (i = 0; i < len; i++) {
1158 if (buff[i] == '*') {
1159 if (!i) {
1160 search = buff + i + 1;
1161 type = MATCH_END_ONLY;
1162 search_len = len - (i + 1);
1163 } else {
1164 if (type == MATCH_END_ONLY) {
1165 type = MATCH_MIDDLE_ONLY;
1166 } else {
1167 match = i;
1168 type = MATCH_FRONT_ONLY;
1169 }
1170 buff[i] = 0;
1171 break;
1172 }
1173 }
1174 }
1175
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001176 /* should not be called from interrupt context */
1177 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001178 if (enable)
1179 ftrace_filtered = 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001180 pg = ftrace_pages_start;
1181 while (pg) {
1182 for (i = 0; i < pg->index; i++) {
1183 int matched = 0;
1184 char *ptr;
1185
1186 rec = &pg->records[i];
1187 if (rec->flags & FTRACE_FL_FAILED)
1188 continue;
1189 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1190 switch (type) {
1191 case MATCH_FULL:
1192 if (strcmp(str, buff) == 0)
1193 matched = 1;
1194 break;
1195 case MATCH_FRONT_ONLY:
1196 if (memcmp(str, buff, match) == 0)
1197 matched = 1;
1198 break;
1199 case MATCH_MIDDLE_ONLY:
1200 if (strstr(str, search))
1201 matched = 1;
1202 break;
1203 case MATCH_END_ONLY:
1204 ptr = strstr(str, search);
1205 if (ptr && (ptr[search_len] == 0))
1206 matched = 1;
1207 break;
1208 }
1209 if (matched)
Steven Rostedt41c52c02008-05-22 11:46:33 -04001210 rec->flags |= flag;
Steven Rostedt5072c592008-05-12 21:20:43 +02001211 }
1212 pg = pg->next;
1213 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001214 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001215}
1216
Ingo Molnare309b412008-05-12 21:20:51 +02001217static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001218ftrace_regex_write(struct file *file, const char __user *ubuf,
1219 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001220{
1221 struct ftrace_iterator *iter;
1222 char ch;
1223 size_t read = 0;
1224 ssize_t ret;
1225
1226 if (!cnt || cnt < 0)
1227 return 0;
1228
Steven Rostedt41c52c02008-05-22 11:46:33 -04001229 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001230
1231 if (file->f_mode & FMODE_READ) {
1232 struct seq_file *m = file->private_data;
1233 iter = m->private;
1234 } else
1235 iter = file->private_data;
1236
1237 if (!*ppos) {
1238 iter->flags &= ~FTRACE_ITER_CONT;
1239 iter->buffer_idx = 0;
1240 }
1241
1242 ret = get_user(ch, ubuf++);
1243 if (ret)
1244 goto out;
1245 read++;
1246 cnt--;
1247
1248 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1249 /* skip white space */
1250 while (cnt && isspace(ch)) {
1251 ret = get_user(ch, ubuf++);
1252 if (ret)
1253 goto out;
1254 read++;
1255 cnt--;
1256 }
1257
Steven Rostedt5072c592008-05-12 21:20:43 +02001258 if (isspace(ch)) {
1259 file->f_pos += read;
1260 ret = read;
1261 goto out;
1262 }
1263
1264 iter->buffer_idx = 0;
1265 }
1266
1267 while (cnt && !isspace(ch)) {
1268 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1269 iter->buffer[iter->buffer_idx++] = ch;
1270 else {
1271 ret = -EINVAL;
1272 goto out;
1273 }
1274 ret = get_user(ch, ubuf++);
1275 if (ret)
1276 goto out;
1277 read++;
1278 cnt--;
1279 }
1280
1281 if (isspace(ch)) {
1282 iter->filtered++;
1283 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001284 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001285 iter->buffer_idx = 0;
1286 } else
1287 iter->flags |= FTRACE_ITER_CONT;
1288
1289
1290 file->f_pos += read;
1291
1292 ret = read;
1293 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04001294 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001295
1296 return ret;
1297}
1298
Steven Rostedt41c52c02008-05-22 11:46:33 -04001299static ssize_t
1300ftrace_filter_write(struct file *file, const char __user *ubuf,
1301 size_t cnt, loff_t *ppos)
1302{
1303 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1304}
1305
1306static ssize_t
1307ftrace_notrace_write(struct file *file, const char __user *ubuf,
1308 size_t cnt, loff_t *ppos)
1309{
1310 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1311}
1312
1313static void
1314ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1315{
1316 if (unlikely(ftrace_disabled))
1317 return;
1318
1319 mutex_lock(&ftrace_regex_lock);
1320 if (reset)
1321 ftrace_filter_reset(enable);
1322 if (buf)
1323 ftrace_match(buf, len, enable);
1324 mutex_unlock(&ftrace_regex_lock);
1325}
1326
Steven Rostedt77a2b372008-05-12 21:20:45 +02001327/**
1328 * ftrace_set_filter - set a function to filter on in ftrace
1329 * @buf - the string that holds the function filter text.
1330 * @len - the length of the string.
1331 * @reset - non zero to reset all filters before applying this filter.
1332 *
1333 * Filters denote which functions should be enabled when tracing is enabled.
1334 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1335 */
Ingo Molnare309b412008-05-12 21:20:51 +02001336void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02001337{
Steven Rostedt41c52c02008-05-22 11:46:33 -04001338 ftrace_set_regex(buf, len, reset, 1);
1339}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001340
Steven Rostedt41c52c02008-05-22 11:46:33 -04001341/**
1342 * ftrace_set_notrace - set a function to not trace in ftrace
1343 * @buf - the string that holds the function notrace text.
1344 * @len - the length of the string.
1345 * @reset - non zero to reset all filters before applying this filter.
1346 *
1347 * Notrace Filters denote which functions should not be enabled when tracing
1348 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1349 * for tracing.
1350 */
1351void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1352{
1353 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02001354}
1355
Ingo Molnare309b412008-05-12 21:20:51 +02001356static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001357ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001358{
1359 struct seq_file *m = (struct seq_file *)file->private_data;
1360 struct ftrace_iterator *iter;
1361
Steven Rostedt41c52c02008-05-22 11:46:33 -04001362 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001363 if (file->f_mode & FMODE_READ) {
1364 iter = m->private;
1365
1366 seq_release(inode, file);
1367 } else
1368 iter = file->private_data;
1369
1370 if (iter->buffer_idx) {
1371 iter->filtered++;
1372 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001373 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001374 }
1375
1376 mutex_lock(&ftrace_sysctl_lock);
1377 mutex_lock(&ftraced_lock);
1378 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1379 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1380 mutex_unlock(&ftraced_lock);
1381 mutex_unlock(&ftrace_sysctl_lock);
1382
1383 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001384 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001385 return 0;
1386}
1387
Steven Rostedt41c52c02008-05-22 11:46:33 -04001388static int
1389ftrace_filter_release(struct inode *inode, struct file *file)
1390{
1391 return ftrace_regex_release(inode, file, 1);
1392}
1393
1394static int
1395ftrace_notrace_release(struct inode *inode, struct file *file)
1396{
1397 return ftrace_regex_release(inode, file, 0);
1398}
1399
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001400static ssize_t
1401ftraced_read(struct file *filp, char __user *ubuf,
1402 size_t cnt, loff_t *ppos)
1403{
1404 /* don't worry about races */
1405 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1406 int r = strlen(buf);
1407
1408 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1409}
1410
1411static ssize_t
1412ftraced_write(struct file *filp, const char __user *ubuf,
1413 size_t cnt, loff_t *ppos)
1414{
1415 char buf[64];
1416 long val;
1417 int ret;
1418
1419 if (cnt >= sizeof(buf))
1420 return -EINVAL;
1421
1422 if (copy_from_user(&buf, ubuf, cnt))
1423 return -EFAULT;
1424
1425 if (strncmp(buf, "enable", 6) == 0)
1426 val = 1;
1427 else if (strncmp(buf, "disable", 7) == 0)
1428 val = 0;
1429 else {
1430 buf[cnt] = 0;
1431
1432 ret = strict_strtoul(buf, 10, &val);
1433 if (ret < 0)
1434 return ret;
1435
1436 val = !!val;
1437 }
1438
1439 if (val)
1440 ftrace_enable_daemon();
1441 else
1442 ftrace_disable_daemon();
1443
1444 filp->f_pos += cnt;
1445
1446 return cnt;
1447}
1448
Steven Rostedt5072c592008-05-12 21:20:43 +02001449static struct file_operations ftrace_avail_fops = {
1450 .open = ftrace_avail_open,
1451 .read = seq_read,
1452 .llseek = seq_lseek,
1453 .release = ftrace_avail_release,
1454};
1455
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301456static struct file_operations ftrace_failures_fops = {
1457 .open = ftrace_failures_open,
1458 .read = seq_read,
1459 .llseek = seq_lseek,
1460 .release = ftrace_avail_release,
1461};
1462
Steven Rostedt5072c592008-05-12 21:20:43 +02001463static struct file_operations ftrace_filter_fops = {
1464 .open = ftrace_filter_open,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001465 .read = ftrace_regex_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02001466 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001467 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02001468 .release = ftrace_filter_release,
1469};
1470
Steven Rostedt41c52c02008-05-22 11:46:33 -04001471static struct file_operations ftrace_notrace_fops = {
1472 .open = ftrace_notrace_open,
1473 .read = ftrace_regex_read,
1474 .write = ftrace_notrace_write,
1475 .llseek = ftrace_regex_lseek,
1476 .release = ftrace_notrace_release,
1477};
1478
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001479static struct file_operations ftraced_fops = {
1480 .open = tracing_open_generic,
1481 .read = ftraced_read,
1482 .write = ftraced_write,
1483};
1484
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001485/**
1486 * ftrace_force_update - force an update to all recording ftrace functions
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001487 */
1488int ftrace_force_update(void)
1489{
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001490 int ret = 0;
1491
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001492 if (unlikely(ftrace_disabled))
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001493 return -ENODEV;
1494
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001495 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001496 mutex_lock(&ftraced_lock);
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001497
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001498 /*
1499 * If ftraced_trigger is not set, then there is nothing
1500 * to update.
1501 */
1502 if (ftraced_trigger && !ftrace_update_code())
1503 ret = -EBUSY;
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001504
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001505 mutex_unlock(&ftraced_lock);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001506 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001507
1508 return ret;
1509}
1510
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001511static void ftrace_force_shutdown(void)
1512{
1513 struct task_struct *task;
1514 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1515
1516 mutex_lock(&ftraced_lock);
1517 task = ftraced_task;
1518 ftraced_task = NULL;
1519 ftraced_suspend = -1;
1520 ftrace_run_update_code(command);
1521 mutex_unlock(&ftraced_lock);
1522
1523 if (task)
1524 kthread_stop(task);
1525}
1526
Steven Rostedt5072c592008-05-12 21:20:43 +02001527static __init int ftrace_init_debugfs(void)
1528{
1529 struct dentry *d_tracer;
1530 struct dentry *entry;
1531
1532 d_tracer = tracing_init_dentry();
1533
1534 entry = debugfs_create_file("available_filter_functions", 0444,
1535 d_tracer, NULL, &ftrace_avail_fops);
1536 if (!entry)
1537 pr_warning("Could not create debugfs "
1538 "'available_filter_functions' entry\n");
1539
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301540 entry = debugfs_create_file("failures", 0444,
1541 d_tracer, NULL, &ftrace_failures_fops);
1542 if (!entry)
1543 pr_warning("Could not create debugfs 'failures' entry\n");
1544
Steven Rostedt5072c592008-05-12 21:20:43 +02001545 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1546 NULL, &ftrace_filter_fops);
1547 if (!entry)
1548 pr_warning("Could not create debugfs "
1549 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04001550
1551 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1552 NULL, &ftrace_notrace_fops);
1553 if (!entry)
1554 pr_warning("Could not create debugfs "
1555 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001556
1557 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1558 NULL, &ftraced_fops);
1559 if (!entry)
1560 pr_warning("Could not create debugfs "
1561 "'ftraced_enabled' entry\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02001562 return 0;
1563}
1564
1565fs_initcall(ftrace_init_debugfs);
1566
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001567#ifdef CONFIG_FTRACE_MCOUNT_RECORD
1568static int ftrace_convert_nops(unsigned long *start,
1569 unsigned long *end)
1570{
1571 unsigned long *p;
1572 unsigned long addr;
1573 unsigned long flags;
1574
1575 p = start;
1576 while (p < end) {
1577 addr = ftrace_call_adjust(*p++);
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001578 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -04001579 spin_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001580 ftrace_record_ip(addr);
Steven Rostedtfed19392008-08-14 22:47:19 -04001581 spin_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001582 ftrace_shutdown_replenish();
1583 }
1584
1585 /* p is ignored */
1586 local_irq_save(flags);
1587 __ftrace_update_code(p);
1588 local_irq_restore(flags);
1589
1590 return 0;
1591}
1592
Steven Rostedt90d595f2008-08-14 15:45:09 -04001593void ftrace_init_module(unsigned long *start, unsigned long *end)
1594{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04001595 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04001596 return;
Steven Rostedt90d595f2008-08-14 15:45:09 -04001597 ftrace_convert_nops(start, end);
1598}
1599
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001600extern unsigned long __start_mcount_loc[];
1601extern unsigned long __stop_mcount_loc[];
1602
1603void __init ftrace_init(void)
1604{
1605 unsigned long count, addr, flags;
1606 int ret;
1607
1608 /* Keep the ftrace pointer to the stub */
1609 addr = (unsigned long)ftrace_stub;
1610
1611 local_irq_save(flags);
1612 ftrace_dyn_arch_init(&addr);
1613 local_irq_restore(flags);
1614
1615 /* ftrace_dyn_arch_init places the return code in addr */
1616 if (addr)
1617 goto failed;
1618
1619 count = __stop_mcount_loc - __start_mcount_loc;
1620
1621 ret = ftrace_dyn_table_alloc(count);
1622 if (ret)
1623 goto failed;
1624
1625 last_ftrace_enabled = ftrace_enabled = 1;
1626
1627 ret = ftrace_convert_nops(__start_mcount_loc,
1628 __stop_mcount_loc);
1629
1630 return;
1631 failed:
1632 ftrace_disabled = 1;
1633}
1634#else /* CONFIG_FTRACE_MCOUNT_RECORD */
1635static int ftraced(void *ignore)
1636{
1637 unsigned long usecs;
1638
1639 while (!kthread_should_stop()) {
1640
1641 set_current_state(TASK_INTERRUPTIBLE);
1642
1643 /* check once a second */
1644 schedule_timeout(HZ);
1645
1646 if (unlikely(ftrace_disabled))
1647 continue;
1648
1649 mutex_lock(&ftrace_sysctl_lock);
1650 mutex_lock(&ftraced_lock);
1651 if (!ftraced_suspend && !ftraced_stop &&
1652 ftrace_update_code()) {
1653 usecs = nsecs_to_usecs(ftrace_update_time);
1654 if (ftrace_update_tot_cnt > 100000) {
1655 ftrace_update_tot_cnt = 0;
1656 pr_info("hm, dftrace overflow: %lu change%s"
1657 " (%lu total) in %lu usec%s\n",
1658 ftrace_update_cnt,
1659 ftrace_update_cnt != 1 ? "s" : "",
1660 ftrace_update_tot_cnt,
1661 usecs, usecs != 1 ? "s" : "");
1662 ftrace_disabled = 1;
1663 WARN_ON_ONCE(1);
1664 }
1665 }
1666 mutex_unlock(&ftraced_lock);
1667 mutex_unlock(&ftrace_sysctl_lock);
1668
1669 ftrace_shutdown_replenish();
1670 }
1671 __set_current_state(TASK_RUNNING);
1672 return 0;
1673}
1674
Ingo Molnare309b412008-05-12 21:20:51 +02001675static int __init ftrace_dynamic_init(void)
Steven Rostedt3d083392008-05-12 21:20:42 +02001676{
1677 struct task_struct *p;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001678 unsigned long addr;
Steven Rostedt3d083392008-05-12 21:20:42 +02001679 int ret;
1680
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001681 addr = (unsigned long)ftrace_record_ip;
Ingo Molnar9ff9cdb2008-05-12 21:20:50 +02001682
Rusty Russell784e2d72008-07-28 12:16:31 -05001683 stop_machine(ftrace_dyn_arch_init, &addr, NULL);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001684
1685 /* ftrace_dyn_arch_init places the return code in addr */
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001686 if (addr) {
1687 ret = (int)addr;
1688 goto failed;
1689 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001690
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001691 ret = ftrace_dyn_table_alloc(NR_TO_INIT);
Steven Rostedt3d083392008-05-12 21:20:42 +02001692 if (ret)
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001693 goto failed;
Steven Rostedt3d083392008-05-12 21:20:42 +02001694
1695 p = kthread_run(ftraced, NULL, "ftraced");
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001696 if (IS_ERR(p)) {
1697 ret = -1;
1698 goto failed;
1699 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001700
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001701 last_ftrace_enabled = ftrace_enabled = 1;
Steven Rostedte1c08bd2008-05-12 21:20:44 +02001702 ftraced_task = p;
Steven Rostedt3d083392008-05-12 21:20:42 +02001703
1704 return 0;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001705
1706 failed:
1707 ftrace_disabled = 1;
1708 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02001709}
1710
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001711core_initcall(ftrace_dynamic_init);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001712#endif /* CONFIG_FTRACE_MCOUNT_RECORD */
1713
Steven Rostedt3d083392008-05-12 21:20:42 +02001714#else
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001715# define ftrace_startup() do { } while (0)
1716# define ftrace_shutdown() do { } while (0)
1717# define ftrace_startup_sysctl() do { } while (0)
1718# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001719# define ftrace_force_shutdown() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02001720#endif /* CONFIG_DYNAMIC_FTRACE */
1721
1722/**
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001723 * ftrace_kill_atomic - kill ftrace from critical sections
1724 *
1725 * This function should be used by panic code. It stops ftrace
1726 * but in a not so nice way. If you need to simply kill ftrace
1727 * from a non-atomic section, use ftrace_kill.
1728 */
1729void ftrace_kill_atomic(void)
1730{
1731 ftrace_disabled = 1;
1732 ftrace_enabled = 0;
Ingo Molnarb2613e32008-07-11 16:44:27 +02001733#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001734 ftraced_suspend = -1;
Ingo Molnarb2613e32008-07-11 16:44:27 +02001735#endif
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001736 clear_ftrace_function();
1737}
1738
1739/**
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001740 * ftrace_kill - totally shutdown ftrace
1741 *
1742 * This is a safety measure. If something was detected that seems
1743 * wrong, calling this function will keep ftrace from doing
1744 * any more modifications, and updates.
1745 * used when something went wrong.
1746 */
1747void ftrace_kill(void)
1748{
1749 mutex_lock(&ftrace_sysctl_lock);
1750 ftrace_disabled = 1;
1751 ftrace_enabled = 0;
1752
1753 clear_ftrace_function();
1754 mutex_unlock(&ftrace_sysctl_lock);
1755
1756 /* Try to totally disable ftrace */
1757 ftrace_force_shutdown();
1758}
1759
1760/**
Steven Rostedt3d083392008-05-12 21:20:42 +02001761 * register_ftrace_function - register a function for profiling
1762 * @ops - ops structure that holds the function for profiling.
1763 *
1764 * Register a function to be called by all functions in the
1765 * kernel.
1766 *
1767 * Note: @ops->func and all the functions it calls must be labeled
1768 * with "notrace", otherwise it will go into a
1769 * recursive loop.
1770 */
1771int register_ftrace_function(struct ftrace_ops *ops)
1772{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001773 int ret;
1774
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001775 if (unlikely(ftrace_disabled))
1776 return -1;
1777
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001778 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001779 ret = __register_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001780 ftrace_startup();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001781 mutex_unlock(&ftrace_sysctl_lock);
1782
1783 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02001784}
1785
1786/**
1787 * unregister_ftrace_function - unresgister a function for profiling.
1788 * @ops - ops structure that holds the function to unregister
1789 *
1790 * Unregister a function that was added to be called by ftrace profiling.
1791 */
1792int unregister_ftrace_function(struct ftrace_ops *ops)
1793{
1794 int ret;
1795
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001796 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001797 ret = __unregister_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001798 ftrace_shutdown();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001799 mutex_unlock(&ftrace_sysctl_lock);
1800
1801 return ret;
1802}
1803
Ingo Molnare309b412008-05-12 21:20:51 +02001804int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001805ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02001806 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001807 loff_t *ppos)
1808{
1809 int ret;
1810
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001811 if (unlikely(ftrace_disabled))
1812 return -ENODEV;
1813
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001814 mutex_lock(&ftrace_sysctl_lock);
1815
Steven Rostedt5072c592008-05-12 21:20:43 +02001816 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001817
1818 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1819 goto out;
1820
1821 last_ftrace_enabled = ftrace_enabled;
1822
1823 if (ftrace_enabled) {
1824
1825 ftrace_startup_sysctl();
1826
1827 /* we are starting ftrace again */
1828 if (ftrace_list != &ftrace_list_end) {
1829 if (ftrace_list->next == &ftrace_list_end)
1830 ftrace_trace_function = ftrace_list->func;
1831 else
1832 ftrace_trace_function = ftrace_list_func;
1833 }
1834
1835 } else {
1836 /* stopping ftrace calls (just send to ftrace_stub) */
1837 ftrace_trace_function = ftrace_stub;
1838
1839 ftrace_shutdown_sysctl();
1840 }
1841
1842 out:
1843 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001844 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001845}