blob: cc4219135dc97261eea7f45456809a4d3bd6045b [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>
Steven Rostedt3d083392008-05-12 21:20:42 +020028#include <linux/list.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020029
Abhishek Sagar395a59d2008-06-21 23:47:27 +053030#include <asm/ftrace.h>
31
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include "trace.h"
33
Steven Rostedt6912896e2008-10-23 09:33:03 -040034#define FTRACE_WARN_ON(cond) \
35 do { \
36 if (WARN_ON(cond)) \
37 ftrace_kill(); \
38 } while (0)
39
40#define FTRACE_WARN_ON_ONCE(cond) \
41 do { \
42 if (WARN_ON_ONCE(cond)) \
43 ftrace_kill(); \
44 } while (0)
45
Steven Rostedt4eebcc82008-05-12 21:20:48 +020046/* ftrace_enabled is a method to turn ftrace on or off */
47int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020048static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020049
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050050/* Quick disabling of function tracer. */
51int function_trace_stop;
52
Steven Rostedt4eebcc82008-05-12 21:20:48 +020053/*
54 * ftrace_disabled is set when an anomaly is discovered.
55 * ftrace_disabled is much stronger than ftrace_enabled.
56 */
57static int ftrace_disabled __read_mostly;
58
Steven Rostedt3d083392008-05-12 21:20:42 +020059static DEFINE_SPINLOCK(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020060static DEFINE_MUTEX(ftrace_sysctl_lock);
61
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020062static struct ftrace_ops ftrace_list_end __read_mostly =
63{
64 .func = ftrace_stub,
65};
66
67static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
68ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050069ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020070
Ingo Molnarf2252932008-05-22 10:37:48 +020071static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020072{
73 struct ftrace_ops *op = ftrace_list;
74
75 /* in case someone actually ports this to alpha! */
76 read_barrier_depends();
77
78 while (op != &ftrace_list_end) {
79 /* silly alpha */
80 read_barrier_depends();
81 op->func(ip, parent_ip);
82 op = op->next;
83 };
84}
85
86/**
Steven Rostedt3d083392008-05-12 21:20:42 +020087 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088 *
Steven Rostedt3d083392008-05-12 21:20:42 +020089 * This NULLs the ftrace function and in essence stops
90 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020091 */
Steven Rostedt3d083392008-05-12 21:20:42 +020092void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020093{
Steven Rostedt3d083392008-05-12 21:20:42 +020094 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050095 __ftrace_trace_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +020096}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020097
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050098#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
99/*
100 * For those archs that do not test ftrace_trace_stop in their
101 * mcount call site, we need to do it from C.
102 */
103static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
104{
105 if (function_trace_stop)
106 return;
107
108 __ftrace_trace_function(ip, parent_ip);
109}
110#endif
111
Ingo Molnare309b412008-05-12 21:20:51 +0200112static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200113{
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400114 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200115 spin_lock(&ftrace_lock);
116
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117 ops->next = ftrace_list;
118 /*
119 * We are entering ops into the ftrace_list but another
120 * CPU might be walking that list. We need to make sure
121 * the ops->next pointer is valid before another CPU sees
122 * the ops pointer included into the ftrace_list.
123 */
124 smp_wmb();
125 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200126
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200127 if (ftrace_enabled) {
128 /*
129 * For one func, simply call it directly.
130 * For more than one func, call the chain.
131 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500132#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200133 if (ops->next == &ftrace_list_end)
134 ftrace_trace_function = ops->func;
135 else
136 ftrace_trace_function = ftrace_list_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500137#else
138 if (ops->next == &ftrace_list_end)
139 __ftrace_trace_function = ops->func;
140 else
141 __ftrace_trace_function = ftrace_list_func;
142 ftrace_trace_function = ftrace_test_stop_func;
143#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200144 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200145
146 spin_unlock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147
148 return 0;
149}
150
Ingo Molnare309b412008-05-12 21:20:51 +0200151static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200153 struct ftrace_ops **p;
154 int ret = 0;
155
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400156 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200157 spin_lock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158
159 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200160 * If we are removing the last function, then simply point
161 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200162 */
163 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
164 ftrace_trace_function = ftrace_stub;
165 ftrace_list = &ftrace_list_end;
166 goto out;
167 }
168
169 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
170 if (*p == ops)
171 break;
172
173 if (*p != ops) {
174 ret = -1;
175 goto out;
176 }
177
178 *p = (*p)->next;
179
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200180 if (ftrace_enabled) {
181 /* If we only have one func left, then call that directly */
walimisb3535c62008-11-14 00:21:02 +0800182 if (ftrace_list->next == &ftrace_list_end)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200183 ftrace_trace_function = ftrace_list->func;
184 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200185
186 out:
Steven Rostedt3d083392008-05-12 21:20:42 +0200187 spin_unlock(&ftrace_lock);
188
189 return ret;
190}
191
192#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400193#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400194# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400195#endif
196
Steven Noonan71c67d52008-09-20 01:00:37 -0700197/*
198 * Since MCOUNT_ADDR may point to mcount itself, we do not want
199 * to get it confused by reading a reference in the code as we
200 * are parsing on objcopy output of text. Use a variable for
201 * it instead.
202 */
203static unsigned long mcount_addr = MCOUNT_ADDR;
204
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200205enum {
206 FTRACE_ENABLE_CALLS = (1 << 0),
207 FTRACE_DISABLE_CALLS = (1 << 1),
208 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
209 FTRACE_ENABLE_MCOUNT = (1 << 3),
210 FTRACE_DISABLE_MCOUNT = (1 << 4),
211};
212
Steven Rostedt5072c592008-05-12 21:20:43 +0200213static int ftrace_filtered;
214
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400215static LIST_HEAD(ftrace_new_addrs);
Steven Rostedt3d083392008-05-12 21:20:42 +0200216
Steven Rostedt41c52c02008-05-22 11:46:33 -0400217static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200218
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200219struct ftrace_page {
220 struct ftrace_page *next;
David Milleraa5e5ce2008-05-13 22:06:56 -0700221 unsigned long index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200222 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700223};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200224
225#define ENTRIES_PER_PAGE \
226 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
227
228/* estimate from running different kernels */
229#define NR_TO_INIT 10000
230
231static struct ftrace_page *ftrace_pages_start;
232static struct ftrace_page *ftrace_pages;
233
Steven Rostedt37ad50842008-05-12 21:20:48 +0200234static struct dyn_ftrace *ftrace_free_records;
235
Abhishek Sagarecea6562008-06-21 23:47:53 +0530236
237#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200238
239static int frozen_record_count;
240
Abhishek Sagarecea6562008-06-21 23:47:53 +0530241static inline void freeze_record(struct dyn_ftrace *rec)
242{
243 if (!(rec->flags & FTRACE_FL_FROZEN)) {
244 rec->flags |= FTRACE_FL_FROZEN;
245 frozen_record_count++;
246 }
247}
248
249static inline void unfreeze_record(struct dyn_ftrace *rec)
250{
251 if (rec->flags & FTRACE_FL_FROZEN) {
252 rec->flags &= ~FTRACE_FL_FROZEN;
253 frozen_record_count--;
254 }
255}
256
257static inline int record_frozen(struct dyn_ftrace *rec)
258{
259 return rec->flags & FTRACE_FL_FROZEN;
260}
261#else
262# define freeze_record(rec) ({ 0; })
263# define unfreeze_record(rec) ({ 0; })
264# define record_frozen(rec) ({ 0; })
265#endif /* CONFIG_KPROBES */
266
Ingo Molnare309b412008-05-12 21:20:51 +0200267static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +0200268{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200269 rec->ip = (unsigned long)ftrace_free_records;
270 ftrace_free_records = rec;
271 rec->flags |= FTRACE_FL_FREE;
272}
273
Steven Rostedtfed19392008-08-14 22:47:19 -0400274void ftrace_release(void *start, unsigned long size)
275{
276 struct dyn_ftrace *rec;
277 struct ftrace_page *pg;
278 unsigned long s = (unsigned long)start;
279 unsigned long e = s + size;
280 int i;
281
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400282 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400283 return;
284
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400285 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -0400286 spin_lock(&ftrace_lock);
287
288 for (pg = ftrace_pages_start; pg; pg = pg->next) {
289 for (i = 0; i < pg->index; i++) {
290 rec = &pg->records[i];
291
292 if ((rec->ip >= s) && (rec->ip < e))
293 ftrace_free_rec(rec);
294 }
295 }
296 spin_unlock(&ftrace_lock);
Steven Rostedtfed19392008-08-14 22:47:19 -0400297}
298
Ingo Molnare309b412008-05-12 21:20:51 +0200299static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200300{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200301 struct dyn_ftrace *rec;
302
303 /* First check for freed records */
304 if (ftrace_free_records) {
305 rec = ftrace_free_records;
306
Steven Rostedt37ad50842008-05-12 21:20:48 +0200307 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400308 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +0200309 ftrace_free_records = NULL;
310 return NULL;
311 }
312
313 ftrace_free_records = (void *)rec->ip;
314 memset(rec, 0, sizeof(*rec));
315 return rec;
316 }
317
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200318 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400319 if (!ftrace_pages->next) {
320 /* allocate another page */
321 ftrace_pages->next =
322 (void *)get_zeroed_page(GFP_KERNEL);
323 if (!ftrace_pages->next)
324 return NULL;
325 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200326 ftrace_pages = ftrace_pages->next;
327 }
328
329 return &ftrace_pages->records[ftrace_pages->index++];
330}
331
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400332static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200333ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200334{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400335 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200336
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800337 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400338 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200339
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400340 rec = ftrace_alloc_dyn_node(ip);
341 if (!rec)
342 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200343
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400344 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +0200345
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400346 list_add(&rec->list, &ftrace_new_addrs);
Steven Rostedt3d083392008-05-12 21:20:42 +0200347
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400348 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200349}
350
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800351static void print_ip_ins(const char *fmt, unsigned char *p)
352{
353 int i;
354
355 printk(KERN_CONT "%s", fmt);
356
357 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
358 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
359}
360
Steven Rostedt31e88902008-11-14 16:21:19 -0800361static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800362{
363 switch (failed) {
364 case -EFAULT:
365 FTRACE_WARN_ON_ONCE(1);
366 pr_info("ftrace faulted on modifying ");
367 print_ip_sym(ip);
368 break;
369 case -EINVAL:
370 FTRACE_WARN_ON_ONCE(1);
371 pr_info("ftrace failed to modify ");
372 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800373 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800374 printk(KERN_CONT "\n");
375 break;
376 case -EPERM:
377 FTRACE_WARN_ON_ONCE(1);
378 pr_info("ftrace faulted on writing ");
379 print_ip_sym(ip);
380 break;
381 default:
382 FTRACE_WARN_ON_ONCE(1);
383 pr_info("ftrace faulted on unknown error ");
384 print_ip_sym(ip);
385 }
386}
387
Steven Rostedtcaf8cde2008-05-12 21:20:50 +0200388#define FTRACE_ADDR ((long)(ftrace_caller))
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200389
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530390static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800391__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200392{
Steven Rostedt41c52c02008-05-22 11:46:33 -0400393 unsigned long ip, fl;
Steven Rostedt5072c592008-05-12 21:20:43 +0200394
395 ip = rec->ip;
396
397 if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200398 /*
399 * If filtering is on:
400 *
401 * If this record is set to be filtered and
402 * is enabled then do nothing.
403 *
404 * If this record is set to be filtered and
405 * it is not enabled, enable it.
406 *
407 * If this record is not set to be filtered
408 * and it is not enabled do nothing.
409 *
Steven Rostedt41c52c02008-05-22 11:46:33 -0400410 * If this record is set not to trace then
411 * do nothing.
412 *
Abhishek Sagara4500b82008-06-14 11:59:39 +0530413 * If this record is set not to trace and
414 * it is enabled then disable it.
415 *
Steven Rostedt5072c592008-05-12 21:20:43 +0200416 * If this record is not set to be filtered and
417 * it is enabled, disable it.
418 */
Abhishek Sagara4500b82008-06-14 11:59:39 +0530419
420 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
421 FTRACE_FL_ENABLED);
Steven Rostedt5072c592008-05-12 21:20:43 +0200422
423 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
Abhishek Sagara4500b82008-06-14 11:59:39 +0530424 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
425 !fl || (fl == FTRACE_FL_NOTRACE))
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530426 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200427
428 /*
429 * If it is enabled disable it,
430 * otherwise enable it!
431 */
Abhishek Sagara4500b82008-06-14 11:59:39 +0530432 if (fl & FTRACE_FL_ENABLED) {
Steven Rostedt31e88902008-11-14 16:21:19 -0800433 enable = 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200434 rec->flags &= ~FTRACE_FL_ENABLED;
435 } else {
Steven Rostedt31e88902008-11-14 16:21:19 -0800436 enable = 1;
Steven Rostedt5072c592008-05-12 21:20:43 +0200437 rec->flags |= FTRACE_FL_ENABLED;
438 }
439 } else {
440
Steven Rostedt41c52c02008-05-22 11:46:33 -0400441 if (enable) {
442 /*
443 * If this record is set not to trace and is
444 * not enabled, do nothing.
445 */
446 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
447 if (fl == FTRACE_FL_NOTRACE)
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530448 return 0;
Steven Rostedt31e88902008-11-14 16:21:19 -0800449 }
Steven Rostedt5072c592008-05-12 21:20:43 +0200450
451 if (enable) {
452 if (rec->flags & FTRACE_FL_ENABLED)
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530453 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200454 rec->flags |= FTRACE_FL_ENABLED;
455 } else {
456 if (!(rec->flags & FTRACE_FL_ENABLED))
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530457 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200458 rec->flags &= ~FTRACE_FL_ENABLED;
459 }
460 }
461
Steven Rostedt31e88902008-11-14 16:21:19 -0800462 if (enable)
463 return ftrace_make_call(rec, FTRACE_ADDR);
464 else
465 return ftrace_make_nop(NULL, rec, FTRACE_ADDR);
Steven Rostedt5072c592008-05-12 21:20:43 +0200466}
467
Ingo Molnare309b412008-05-12 21:20:51 +0200468static void ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200469{
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530470 int i, failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200471 struct dyn_ftrace *rec;
472 struct ftrace_page *pg;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200473
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200474 for (pg = ftrace_pages_start; pg; pg = pg->next) {
475 for (i = 0; i < pg->index; i++) {
476 rec = &pg->records[i];
477
Steven Rostedt918c1152008-11-14 16:21:19 -0800478 /*
479 * Skip over free records and records that have
480 * failed.
481 */
482 if (rec->flags & FTRACE_FL_FREE ||
483 rec->flags & FTRACE_FL_FAILED)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200484 continue;
485
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530486 /* ignore updates to this record's mcount site */
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530487 if (get_kprobe((void *)rec->ip)) {
488 freeze_record(rec);
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530489 continue;
Abhishek Sagar98a05ed2008-06-26 22:51:51 +0530490 } else {
491 unfreeze_record(rec);
492 }
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530493
Steven Rostedt31e88902008-11-14 16:21:19 -0800494 failed = __ftrace_replace_code(rec, enable);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530495 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
496 rec->flags |= FTRACE_FL_FAILED;
497 if ((system_state == SYSTEM_BOOTING) ||
Abhishek Sagar34078a52008-06-03 08:33:41 +0530498 !core_kernel_text(rec->ip)) {
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530499 ftrace_free_rec(rec);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800500 } else
Steven Rostedt31e88902008-11-14 16:21:19 -0800501 ftrace_bug(failed, rec->ip);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530502 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200503 }
504 }
505}
506
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530507static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800508ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200509{
510 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400511 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200512
513 ip = rec->ip;
514
Steven Rostedt31e88902008-11-14 16:21:19 -0800515 ret = ftrace_make_nop(mod, rec, mcount_addr);
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400516 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -0800517 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200518 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530519 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200520 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530521 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200522}
523
Ingo Molnare309b412008-05-12 21:20:51 +0200524static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200525{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200526 int *command = data;
527
Steven Rostedta3583242008-11-11 15:01:42 -0500528 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200529 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -0500530 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200531 ftrace_replace_code(0);
532
533 if (*command & FTRACE_UPDATE_TRACE_FUNC)
534 ftrace_update_ftrace_func(ftrace_trace_function);
535
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200536 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200537}
538
Ingo Molnare309b412008-05-12 21:20:51 +0200539static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200540{
Rusty Russell784e2d72008-07-28 12:16:31 -0500541 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt3d083392008-05-12 21:20:42 +0200542}
543
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200544static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500545static int ftrace_start_up;
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400546static DEFINE_MUTEX(ftrace_start_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200547
Ingo Molnare309b412008-05-12 21:20:51 +0200548static void ftrace_startup(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200549{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200550 int command = 0;
551
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200552 if (unlikely(ftrace_disabled))
553 return;
554
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400555 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500556 ftrace_start_up++;
557 if (ftrace_start_up == 1)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200558 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +0200559
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200560 if (saved_ftrace_func != ftrace_trace_function) {
561 saved_ftrace_func = ftrace_trace_function;
562 command |= FTRACE_UPDATE_TRACE_FUNC;
563 }
564
565 if (!command || !ftrace_enabled)
566 goto out;
567
568 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200569 out:
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400570 mutex_unlock(&ftrace_start_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200571}
572
Ingo Molnare309b412008-05-12 21:20:51 +0200573static void ftrace_shutdown(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200574{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200575 int command = 0;
576
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200577 if (unlikely(ftrace_disabled))
578 return;
579
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400580 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500581 ftrace_start_up--;
582 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200583 command |= FTRACE_DISABLE_CALLS;
584
585 if (saved_ftrace_func != ftrace_trace_function) {
586 saved_ftrace_func = ftrace_trace_function;
587 command |= FTRACE_UPDATE_TRACE_FUNC;
588 }
589
590 if (!command || !ftrace_enabled)
Steven Rostedt3d083392008-05-12 21:20:42 +0200591 goto out;
592
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200593 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200594 out:
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400595 mutex_unlock(&ftrace_start_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200596}
597
Ingo Molnare309b412008-05-12 21:20:51 +0200598static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200599{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200600 int command = FTRACE_ENABLE_MCOUNT;
601
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200602 if (unlikely(ftrace_disabled))
603 return;
604
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400605 mutex_lock(&ftrace_start_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200606 /* Force update next time */
607 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500608 /* ftrace_start_up is true if we want ftrace running */
609 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200610 command |= FTRACE_ENABLE_CALLS;
611
612 ftrace_run_update_code(command);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400613 mutex_unlock(&ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200614}
615
Ingo Molnare309b412008-05-12 21:20:51 +0200616static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200617{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200618 int command = FTRACE_DISABLE_MCOUNT;
619
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200620 if (unlikely(ftrace_disabled))
621 return;
622
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400623 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500624 /* ftrace_start_up is true if ftrace is running */
625 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200626 command |= FTRACE_DISABLE_CALLS;
627
628 ftrace_run_update_code(command);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400629 mutex_unlock(&ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200630}
631
Steven Rostedt3d083392008-05-12 21:20:42 +0200632static cycle_t ftrace_update_time;
633static unsigned long ftrace_update_cnt;
634unsigned long ftrace_update_tot_cnt;
635
Steven Rostedt31e88902008-11-14 16:21:19 -0800636static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +0200637{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400638 struct dyn_ftrace *p, *t;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530639 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200640
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200641 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200642 ftrace_update_cnt = 0;
643
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400644 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530645
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400646 /* If something went wrong, bail without enabling anything */
647 if (unlikely(ftrace_disabled))
648 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +0200649
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400650 list_del_init(&p->list);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530651
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400652 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -0800653 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400654 p->flags |= FTRACE_FL_CONVERTED;
655 ftrace_update_cnt++;
656 } else
657 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +0200658 }
659
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200660 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200661 ftrace_update_time = stop - start;
662 ftrace_update_tot_cnt += ftrace_update_cnt;
663
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200664 return 0;
665}
666
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400667static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200668{
669 struct ftrace_page *pg;
670 int cnt;
671 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200672
673 /* allocate a few pages */
674 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
675 if (!ftrace_pages_start)
676 return -1;
677
678 /*
679 * Allocate a few more pages.
680 *
681 * TODO: have some parser search vmlinux before
682 * final linking to find all calls to ftrace.
683 * Then we can:
684 * a) know how many pages to allocate.
685 * and/or
686 * b) set up the table then.
687 *
688 * The dynamic code is still necessary for
689 * modules.
690 */
691
692 pg = ftrace_pages = ftrace_pages_start;
693
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400694 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400695 pr_info("ftrace: allocating %ld entries in %d pages\n",
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400696 num_to_init, cnt);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200697
698 for (i = 0; i < cnt; i++) {
699 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
700
701 /* If we fail, we'll try later anyway */
702 if (!pg->next)
703 break;
704
705 pg = pg->next;
706 }
707
708 return 0;
709}
710
Steven Rostedt5072c592008-05-12 21:20:43 +0200711enum {
712 FTRACE_ITER_FILTER = (1 << 0),
713 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -0400714 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530715 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +0200716};
717
718#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
719
720struct ftrace_iterator {
721 loff_t pos;
722 struct ftrace_page *pg;
723 unsigned idx;
724 unsigned flags;
725 unsigned char buffer[FTRACE_BUFF_MAX+1];
726 unsigned buffer_idx;
727 unsigned filtered;
728};
729
Ingo Molnare309b412008-05-12 21:20:51 +0200730static void *
Steven Rostedt5072c592008-05-12 21:20:43 +0200731t_next(struct seq_file *m, void *v, loff_t *pos)
732{
733 struct ftrace_iterator *iter = m->private;
734 struct dyn_ftrace *rec = NULL;
735
736 (*pos)++;
737
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400738 /* should not be called from interrupt context */
739 spin_lock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200740 retry:
741 if (iter->idx >= iter->pg->index) {
742 if (iter->pg->next) {
743 iter->pg = iter->pg->next;
744 iter->idx = 0;
745 goto retry;
746 }
747 } else {
748 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -0400749 if ((rec->flags & FTRACE_FL_FREE) ||
750
751 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530752 (rec->flags & FTRACE_FL_FAILED)) ||
753
754 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -0400755 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530756
Steven Rostedt0183fb1c2008-11-07 22:36:02 -0500757 ((iter->flags & FTRACE_ITER_FILTER) &&
758 !(rec->flags & FTRACE_FL_FILTER)) ||
759
Steven Rostedt41c52c02008-05-22 11:46:33 -0400760 ((iter->flags & FTRACE_ITER_NOTRACE) &&
761 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200762 rec = NULL;
763 goto retry;
764 }
765 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400766 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200767
768 iter->pos = *pos;
769
770 return rec;
771}
772
773static void *t_start(struct seq_file *m, loff_t *pos)
774{
775 struct ftrace_iterator *iter = m->private;
776 void *p = NULL;
777 loff_t l = -1;
778
779 if (*pos != iter->pos) {
780 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
781 ;
782 } else {
783 l = *pos;
784 p = t_next(m, p, &l);
785 }
786
787 return p;
788}
789
790static void t_stop(struct seq_file *m, void *p)
791{
792}
793
794static int t_show(struct seq_file *m, void *v)
795{
796 struct dyn_ftrace *rec = v;
797 char str[KSYM_SYMBOL_LEN];
798
799 if (!rec)
800 return 0;
801
802 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
803
804 seq_printf(m, "%s\n", str);
805
806 return 0;
807}
808
809static struct seq_operations show_ftrace_seq_ops = {
810 .start = t_start,
811 .next = t_next,
812 .stop = t_stop,
813 .show = t_show,
814};
815
Ingo Molnare309b412008-05-12 21:20:51 +0200816static int
Steven Rostedt5072c592008-05-12 21:20:43 +0200817ftrace_avail_open(struct inode *inode, struct file *file)
818{
819 struct ftrace_iterator *iter;
820 int ret;
821
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200822 if (unlikely(ftrace_disabled))
823 return -ENODEV;
824
Steven Rostedt5072c592008-05-12 21:20:43 +0200825 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
826 if (!iter)
827 return -ENOMEM;
828
829 iter->pg = ftrace_pages_start;
830 iter->pos = -1;
831
832 ret = seq_open(file, &show_ftrace_seq_ops);
833 if (!ret) {
834 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200835
Steven Rostedt5072c592008-05-12 21:20:43 +0200836 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200837 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +0200838 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200839 }
Steven Rostedt5072c592008-05-12 21:20:43 +0200840
841 return ret;
842}
843
844int ftrace_avail_release(struct inode *inode, struct file *file)
845{
846 struct seq_file *m = (struct seq_file *)file->private_data;
847 struct ftrace_iterator *iter = m->private;
848
849 seq_release(inode, file);
850 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200851
Steven Rostedt5072c592008-05-12 21:20:43 +0200852 return 0;
853}
854
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530855static int
856ftrace_failures_open(struct inode *inode, struct file *file)
857{
858 int ret;
859 struct seq_file *m;
860 struct ftrace_iterator *iter;
861
862 ret = ftrace_avail_open(inode, file);
863 if (!ret) {
864 m = (struct seq_file *)file->private_data;
865 iter = (struct ftrace_iterator *)m->private;
866 iter->flags = FTRACE_ITER_FAILURES;
867 }
868
869 return ret;
870}
871
872
Steven Rostedt41c52c02008-05-22 11:46:33 -0400873static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200874{
875 struct ftrace_page *pg;
876 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400877 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +0200878 unsigned i;
879
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400880 /* should not be called from interrupt context */
881 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400882 if (enable)
883 ftrace_filtered = 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200884 pg = ftrace_pages_start;
885 while (pg) {
886 for (i = 0; i < pg->index; i++) {
887 rec = &pg->records[i];
888 if (rec->flags & FTRACE_FL_FAILED)
889 continue;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400890 rec->flags &= ~type;
Steven Rostedt5072c592008-05-12 21:20:43 +0200891 }
892 pg = pg->next;
893 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400894 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200895}
896
Ingo Molnare309b412008-05-12 21:20:51 +0200897static int
Steven Rostedt41c52c02008-05-22 11:46:33 -0400898ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200899{
900 struct ftrace_iterator *iter;
901 int ret = 0;
902
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200903 if (unlikely(ftrace_disabled))
904 return -ENODEV;
905
Steven Rostedt5072c592008-05-12 21:20:43 +0200906 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
907 if (!iter)
908 return -ENOMEM;
909
Steven Rostedt41c52c02008-05-22 11:46:33 -0400910 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200911 if ((file->f_mode & FMODE_WRITE) &&
912 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -0400913 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +0200914
915 if (file->f_mode & FMODE_READ) {
916 iter->pg = ftrace_pages_start;
917 iter->pos = -1;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400918 iter->flags = enable ? FTRACE_ITER_FILTER :
919 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +0200920
921 ret = seq_open(file, &show_ftrace_seq_ops);
922 if (!ret) {
923 struct seq_file *m = file->private_data;
924 m->private = iter;
925 } else
926 kfree(iter);
927 } else
928 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400929 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200930
931 return ret;
932}
933
Steven Rostedt41c52c02008-05-22 11:46:33 -0400934static int
935ftrace_filter_open(struct inode *inode, struct file *file)
936{
937 return ftrace_regex_open(inode, file, 1);
938}
939
940static int
941ftrace_notrace_open(struct inode *inode, struct file *file)
942{
943 return ftrace_regex_open(inode, file, 0);
944}
945
Ingo Molnare309b412008-05-12 21:20:51 +0200946static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -0400947ftrace_regex_read(struct file *file, char __user *ubuf,
Steven Rostedt5072c592008-05-12 21:20:43 +0200948 size_t cnt, loff_t *ppos)
949{
950 if (file->f_mode & FMODE_READ)
951 return seq_read(file, ubuf, cnt, ppos);
952 else
953 return -EPERM;
954}
955
Ingo Molnare309b412008-05-12 21:20:51 +0200956static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -0400957ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +0200958{
959 loff_t ret;
960
961 if (file->f_mode & FMODE_READ)
962 ret = seq_lseek(file, offset, origin);
963 else
964 file->f_pos = ret = 1;
965
966 return ret;
967}
968
969enum {
970 MATCH_FULL,
971 MATCH_FRONT_ONLY,
972 MATCH_MIDDLE_ONLY,
973 MATCH_END_ONLY,
974};
975
Ingo Molnare309b412008-05-12 21:20:51 +0200976static void
Steven Rostedt41c52c02008-05-22 11:46:33 -0400977ftrace_match(unsigned char *buff, int len, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200978{
979 char str[KSYM_SYMBOL_LEN];
980 char *search = NULL;
981 struct ftrace_page *pg;
982 struct dyn_ftrace *rec;
983 int type = MATCH_FULL;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400984 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +0200985 unsigned i, match = 0, search_len = 0;
986
987 for (i = 0; i < len; i++) {
988 if (buff[i] == '*') {
989 if (!i) {
990 search = buff + i + 1;
991 type = MATCH_END_ONLY;
992 search_len = len - (i + 1);
993 } else {
994 if (type == MATCH_END_ONLY) {
995 type = MATCH_MIDDLE_ONLY;
996 } else {
997 match = i;
998 type = MATCH_FRONT_ONLY;
999 }
1000 buff[i] = 0;
1001 break;
1002 }
1003 }
1004 }
1005
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001006 /* should not be called from interrupt context */
1007 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001008 if (enable)
1009 ftrace_filtered = 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001010 pg = ftrace_pages_start;
1011 while (pg) {
1012 for (i = 0; i < pg->index; i++) {
1013 int matched = 0;
1014 char *ptr;
1015
1016 rec = &pg->records[i];
1017 if (rec->flags & FTRACE_FL_FAILED)
1018 continue;
1019 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1020 switch (type) {
1021 case MATCH_FULL:
1022 if (strcmp(str, buff) == 0)
1023 matched = 1;
1024 break;
1025 case MATCH_FRONT_ONLY:
1026 if (memcmp(str, buff, match) == 0)
1027 matched = 1;
1028 break;
1029 case MATCH_MIDDLE_ONLY:
1030 if (strstr(str, search))
1031 matched = 1;
1032 break;
1033 case MATCH_END_ONLY:
1034 ptr = strstr(str, search);
1035 if (ptr && (ptr[search_len] == 0))
1036 matched = 1;
1037 break;
1038 }
1039 if (matched)
Steven Rostedt41c52c02008-05-22 11:46:33 -04001040 rec->flags |= flag;
Steven Rostedt5072c592008-05-12 21:20:43 +02001041 }
1042 pg = pg->next;
1043 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001044 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001045}
1046
Ingo Molnare309b412008-05-12 21:20:51 +02001047static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001048ftrace_regex_write(struct file *file, const char __user *ubuf,
1049 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001050{
1051 struct ftrace_iterator *iter;
1052 char ch;
1053 size_t read = 0;
1054 ssize_t ret;
1055
1056 if (!cnt || cnt < 0)
1057 return 0;
1058
Steven Rostedt41c52c02008-05-22 11:46:33 -04001059 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001060
1061 if (file->f_mode & FMODE_READ) {
1062 struct seq_file *m = file->private_data;
1063 iter = m->private;
1064 } else
1065 iter = file->private_data;
1066
1067 if (!*ppos) {
1068 iter->flags &= ~FTRACE_ITER_CONT;
1069 iter->buffer_idx = 0;
1070 }
1071
1072 ret = get_user(ch, ubuf++);
1073 if (ret)
1074 goto out;
1075 read++;
1076 cnt--;
1077
1078 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1079 /* skip white space */
1080 while (cnt && isspace(ch)) {
1081 ret = get_user(ch, ubuf++);
1082 if (ret)
1083 goto out;
1084 read++;
1085 cnt--;
1086 }
1087
Steven Rostedt5072c592008-05-12 21:20:43 +02001088 if (isspace(ch)) {
1089 file->f_pos += read;
1090 ret = read;
1091 goto out;
1092 }
1093
1094 iter->buffer_idx = 0;
1095 }
1096
1097 while (cnt && !isspace(ch)) {
1098 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1099 iter->buffer[iter->buffer_idx++] = ch;
1100 else {
1101 ret = -EINVAL;
1102 goto out;
1103 }
1104 ret = get_user(ch, ubuf++);
1105 if (ret)
1106 goto out;
1107 read++;
1108 cnt--;
1109 }
1110
1111 if (isspace(ch)) {
1112 iter->filtered++;
1113 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001114 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001115 iter->buffer_idx = 0;
1116 } else
1117 iter->flags |= FTRACE_ITER_CONT;
1118
1119
1120 file->f_pos += read;
1121
1122 ret = read;
1123 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04001124 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001125
1126 return ret;
1127}
1128
Steven Rostedt41c52c02008-05-22 11:46:33 -04001129static ssize_t
1130ftrace_filter_write(struct file *file, const char __user *ubuf,
1131 size_t cnt, loff_t *ppos)
1132{
1133 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1134}
1135
1136static ssize_t
1137ftrace_notrace_write(struct file *file, const char __user *ubuf,
1138 size_t cnt, loff_t *ppos)
1139{
1140 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1141}
1142
1143static void
1144ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1145{
1146 if (unlikely(ftrace_disabled))
1147 return;
1148
1149 mutex_lock(&ftrace_regex_lock);
1150 if (reset)
1151 ftrace_filter_reset(enable);
1152 if (buf)
1153 ftrace_match(buf, len, enable);
1154 mutex_unlock(&ftrace_regex_lock);
1155}
1156
Steven Rostedt77a2b372008-05-12 21:20:45 +02001157/**
1158 * ftrace_set_filter - set a function to filter on in ftrace
1159 * @buf - the string that holds the function filter text.
1160 * @len - the length of the string.
1161 * @reset - non zero to reset all filters before applying this filter.
1162 *
1163 * Filters denote which functions should be enabled when tracing is enabled.
1164 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1165 */
Ingo Molnare309b412008-05-12 21:20:51 +02001166void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02001167{
Steven Rostedt41c52c02008-05-22 11:46:33 -04001168 ftrace_set_regex(buf, len, reset, 1);
1169}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001170
Steven Rostedt41c52c02008-05-22 11:46:33 -04001171/**
1172 * ftrace_set_notrace - set a function to not trace in ftrace
1173 * @buf - the string that holds the function notrace text.
1174 * @len - the length of the string.
1175 * @reset - non zero to reset all filters before applying this filter.
1176 *
1177 * Notrace Filters denote which functions should not be enabled when tracing
1178 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1179 * for tracing.
1180 */
1181void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1182{
1183 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02001184}
1185
Ingo Molnare309b412008-05-12 21:20:51 +02001186static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001187ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001188{
1189 struct seq_file *m = (struct seq_file *)file->private_data;
1190 struct ftrace_iterator *iter;
1191
Steven Rostedt41c52c02008-05-22 11:46:33 -04001192 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001193 if (file->f_mode & FMODE_READ) {
1194 iter = m->private;
1195
1196 seq_release(inode, file);
1197 } else
1198 iter = file->private_data;
1199
1200 if (iter->buffer_idx) {
1201 iter->filtered++;
1202 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001203 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001204 }
1205
1206 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001207 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001208 if (iter->filtered && ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02001209 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001210 mutex_unlock(&ftrace_start_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001211 mutex_unlock(&ftrace_sysctl_lock);
1212
1213 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001214 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001215 return 0;
1216}
1217
Steven Rostedt41c52c02008-05-22 11:46:33 -04001218static int
1219ftrace_filter_release(struct inode *inode, struct file *file)
1220{
1221 return ftrace_regex_release(inode, file, 1);
1222}
1223
1224static int
1225ftrace_notrace_release(struct inode *inode, struct file *file)
1226{
1227 return ftrace_regex_release(inode, file, 0);
1228}
1229
Steven Rostedt5072c592008-05-12 21:20:43 +02001230static struct file_operations ftrace_avail_fops = {
1231 .open = ftrace_avail_open,
1232 .read = seq_read,
1233 .llseek = seq_lseek,
1234 .release = ftrace_avail_release,
1235};
1236
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301237static struct file_operations ftrace_failures_fops = {
1238 .open = ftrace_failures_open,
1239 .read = seq_read,
1240 .llseek = seq_lseek,
1241 .release = ftrace_avail_release,
1242};
1243
Steven Rostedt5072c592008-05-12 21:20:43 +02001244static struct file_operations ftrace_filter_fops = {
1245 .open = ftrace_filter_open,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001246 .read = ftrace_regex_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02001247 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001248 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02001249 .release = ftrace_filter_release,
1250};
1251
Steven Rostedt41c52c02008-05-22 11:46:33 -04001252static struct file_operations ftrace_notrace_fops = {
1253 .open = ftrace_notrace_open,
1254 .read = ftrace_regex_read,
1255 .write = ftrace_notrace_write,
1256 .llseek = ftrace_regex_lseek,
1257 .release = ftrace_notrace_release,
1258};
1259
Steven Rostedt5072c592008-05-12 21:20:43 +02001260static __init int ftrace_init_debugfs(void)
1261{
1262 struct dentry *d_tracer;
1263 struct dentry *entry;
1264
1265 d_tracer = tracing_init_dentry();
1266
1267 entry = debugfs_create_file("available_filter_functions", 0444,
1268 d_tracer, NULL, &ftrace_avail_fops);
1269 if (!entry)
1270 pr_warning("Could not create debugfs "
1271 "'available_filter_functions' entry\n");
1272
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301273 entry = debugfs_create_file("failures", 0444,
1274 d_tracer, NULL, &ftrace_failures_fops);
1275 if (!entry)
1276 pr_warning("Could not create debugfs 'failures' entry\n");
1277
Steven Rostedt5072c592008-05-12 21:20:43 +02001278 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1279 NULL, &ftrace_filter_fops);
1280 if (!entry)
1281 pr_warning("Could not create debugfs "
1282 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04001283
1284 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1285 NULL, &ftrace_notrace_fops);
1286 if (!entry)
1287 pr_warning("Could not create debugfs "
1288 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001289
Steven Rostedt5072c592008-05-12 21:20:43 +02001290 return 0;
1291}
1292
1293fs_initcall(ftrace_init_debugfs);
1294
Steven Rostedt31e88902008-11-14 16:21:19 -08001295static int ftrace_convert_nops(struct module *mod,
1296 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001297 unsigned long *end)
1298{
1299 unsigned long *p;
1300 unsigned long addr;
1301 unsigned long flags;
1302
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001303 mutex_lock(&ftrace_start_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001304 p = start;
1305 while (p < end) {
1306 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08001307 /*
1308 * Some architecture linkers will pad between
1309 * the different mcount_loc sections of different
1310 * object files to satisfy alignments.
1311 * Skip any NULL pointers.
1312 */
1313 if (!addr)
1314 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001315 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001316 }
1317
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001318 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001319 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08001320 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001321 local_irq_restore(flags);
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001322 mutex_unlock(&ftrace_start_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001323
1324 return 0;
1325}
1326
Steven Rostedt31e88902008-11-14 16:21:19 -08001327void ftrace_init_module(struct module *mod,
1328 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04001329{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04001330 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04001331 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08001332 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04001333}
1334
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001335extern unsigned long __start_mcount_loc[];
1336extern unsigned long __stop_mcount_loc[];
1337
1338void __init ftrace_init(void)
1339{
1340 unsigned long count, addr, flags;
1341 int ret;
1342
1343 /* Keep the ftrace pointer to the stub */
1344 addr = (unsigned long)ftrace_stub;
1345
1346 local_irq_save(flags);
1347 ftrace_dyn_arch_init(&addr);
1348 local_irq_restore(flags);
1349
1350 /* ftrace_dyn_arch_init places the return code in addr */
1351 if (addr)
1352 goto failed;
1353
1354 count = __stop_mcount_loc - __start_mcount_loc;
1355
1356 ret = ftrace_dyn_table_alloc(count);
1357 if (ret)
1358 goto failed;
1359
1360 last_ftrace_enabled = ftrace_enabled = 1;
1361
Steven Rostedt31e88902008-11-14 16:21:19 -08001362 ret = ftrace_convert_nops(NULL,
1363 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001364 __stop_mcount_loc);
1365
1366 return;
1367 failed:
1368 ftrace_disabled = 1;
1369}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001370
Steven Rostedt3d083392008-05-12 21:20:42 +02001371#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01001372
1373static int __init ftrace_nodyn_init(void)
1374{
1375 ftrace_enabled = 1;
1376 return 0;
1377}
1378device_initcall(ftrace_nodyn_init);
1379
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001380# define ftrace_startup() do { } while (0)
1381# define ftrace_shutdown() do { } while (0)
1382# define ftrace_startup_sysctl() do { } while (0)
1383# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02001384#endif /* CONFIG_DYNAMIC_FTRACE */
1385
1386/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04001387 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001388 *
1389 * This function should be used by panic code. It stops ftrace
1390 * but in a not so nice way. If you need to simply kill ftrace
1391 * from a non-atomic section, use ftrace_kill.
1392 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04001393void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001394{
1395 ftrace_disabled = 1;
1396 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001397 clear_ftrace_function();
1398}
1399
1400/**
Steven Rostedt3d083392008-05-12 21:20:42 +02001401 * register_ftrace_function - register a function for profiling
1402 * @ops - ops structure that holds the function for profiling.
1403 *
1404 * Register a function to be called by all functions in the
1405 * kernel.
1406 *
1407 * Note: @ops->func and all the functions it calls must be labeled
1408 * with "notrace", otherwise it will go into a
1409 * recursive loop.
1410 */
1411int register_ftrace_function(struct ftrace_ops *ops)
1412{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001413 int ret;
1414
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001415 if (unlikely(ftrace_disabled))
1416 return -1;
1417
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001418 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001419 ret = __register_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001420 ftrace_startup();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001421 mutex_unlock(&ftrace_sysctl_lock);
1422
1423 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02001424}
1425
1426/**
1427 * unregister_ftrace_function - unresgister a function for profiling.
1428 * @ops - ops structure that holds the function to unregister
1429 *
1430 * Unregister a function that was added to be called by ftrace profiling.
1431 */
1432int unregister_ftrace_function(struct ftrace_ops *ops)
1433{
1434 int ret;
1435
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001436 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001437 ret = __unregister_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001438 ftrace_shutdown();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001439 mutex_unlock(&ftrace_sysctl_lock);
1440
1441 return ret;
1442}
1443
Ingo Molnare309b412008-05-12 21:20:51 +02001444int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001445ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02001446 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001447 loff_t *ppos)
1448{
1449 int ret;
1450
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001451 if (unlikely(ftrace_disabled))
1452 return -ENODEV;
1453
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001454 mutex_lock(&ftrace_sysctl_lock);
1455
Steven Rostedt5072c592008-05-12 21:20:43 +02001456 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001457
1458 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1459 goto out;
1460
1461 last_ftrace_enabled = ftrace_enabled;
1462
1463 if (ftrace_enabled) {
1464
1465 ftrace_startup_sysctl();
1466
1467 /* we are starting ftrace again */
1468 if (ftrace_list != &ftrace_list_end) {
1469 if (ftrace_list->next == &ftrace_list_end)
1470 ftrace_trace_function = ftrace_list->func;
1471 else
1472 ftrace_trace_function = ftrace_list_func;
1473 }
1474
1475 } else {
1476 /* stopping ftrace calls (just send to ftrace_stub) */
1477 ftrace_trace_function = ftrace_stub;
1478
1479 ftrace_shutdown_sysctl();
1480 }
1481
1482 out:
1483 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001484 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001485}
Ingo Molnarf17845e2008-10-24 12:47:10 +02001486
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001487#ifdef CONFIG_FUNCTION_RET_TRACER
1488trace_function_return_t ftrace_function_return =
1489 (trace_function_return_t)ftrace_stub;
1490void register_ftrace_return(trace_function_return_t func)
1491{
1492 ftrace_function_return = func;
1493}
1494
1495void unregister_ftrace_return(void)
1496{
1497 ftrace_function_return = (trace_function_return_t)ftrace_stub;
1498}
1499#endif
1500
1501
1502