blob: 2971fe48f55e1d1315e1847500499b587a225042 [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 Rostedt0ef8cde2008-12-03 15:36:58 -050050/* set when tracing only a pid */
Steven Rostedt978f3a42008-12-04 00:26:40 -050051struct pid *ftrace_pid_trace;
Steven Rostedt21bbecd2008-12-04 23:30:56 -050052static struct pid * const ftrace_swapper_pid = &init_struct_pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050053
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050054/* Quick disabling of function tracer. */
55int function_trace_stop;
56
Steven Rostedt4eebcc82008-05-12 21:20:48 +020057/*
58 * ftrace_disabled is set when an anomaly is discovered.
59 * ftrace_disabled is much stronger than ftrace_enabled.
60 */
61static int ftrace_disabled __read_mostly;
62
Steven Rostedt3d083392008-05-12 21:20:42 +020063static DEFINE_SPINLOCK(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020064static DEFINE_MUTEX(ftrace_sysctl_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -050065static DEFINE_MUTEX(ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020066
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020067static struct ftrace_ops ftrace_list_end __read_mostly =
68{
69 .func = ftrace_stub,
70};
71
72static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
73ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050074ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050075ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020076
Ingo Molnarf2252932008-05-22 10:37:48 +020077static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020078{
79 struct ftrace_ops *op = ftrace_list;
80
81 /* in case someone actually ports this to alpha! */
82 read_barrier_depends();
83
84 while (op != &ftrace_list_end) {
85 /* silly alpha */
86 read_barrier_depends();
87 op->func(ip, parent_ip);
88 op = op->next;
89 };
90}
91
Steven Rostedtdf4fc312008-11-26 00:16:23 -050092static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
93{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -050094 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -050095 return;
96
97 ftrace_pid_function(ip, parent_ip);
98}
99
100static void set_ftrace_pid_function(ftrace_func_t func)
101{
102 /* do not set ftrace_pid_function to itself! */
103 if (func != ftrace_pid_func)
104 ftrace_pid_function = func;
105}
106
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200107/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200108 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200110 * This NULLs the ftrace function and in essence stops
111 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200112 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200113void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200114{
Steven Rostedt3d083392008-05-12 21:20:42 +0200115 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500116 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500117 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200118}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500120#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
121/*
122 * For those archs that do not test ftrace_trace_stop in their
123 * mcount call site, we need to do it from C.
124 */
125static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
126{
127 if (function_trace_stop)
128 return;
129
130 __ftrace_trace_function(ip, parent_ip);
131}
132#endif
133
Ingo Molnare309b412008-05-12 21:20:51 +0200134static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200135{
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400136 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200137 spin_lock(&ftrace_lock);
138
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200139 ops->next = ftrace_list;
140 /*
141 * We are entering ops into the ftrace_list but another
142 * CPU might be walking that list. We need to make sure
143 * the ops->next pointer is valid before another CPU sees
144 * the ops pointer included into the ftrace_list.
145 */
146 smp_wmb();
147 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200148
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200149 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500150 ftrace_func_t func;
151
152 if (ops->next == &ftrace_list_end)
153 func = ops->func;
154 else
155 func = ftrace_list_func;
156
Steven Rostedt978f3a42008-12-04 00:26:40 -0500157 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158 set_ftrace_pid_function(func);
159 func = ftrace_pid_func;
160 }
161
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200162 /*
163 * For one func, simply call it directly.
164 * For more than one func, call the chain.
165 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500166#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500167 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500168#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500169 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500170 ftrace_trace_function = ftrace_test_stop_func;
171#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200172 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200173
174 spin_unlock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200175
176 return 0;
177}
178
Ingo Molnare309b412008-05-12 21:20:51 +0200179static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200180{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200181 struct ftrace_ops **p;
182 int ret = 0;
183
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400184 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200185 spin_lock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186
187 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200188 * If we are removing the last function, then simply point
189 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 */
191 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
192 ftrace_trace_function = ftrace_stub;
193 ftrace_list = &ftrace_list_end;
194 goto out;
195 }
196
197 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
198 if (*p == ops)
199 break;
200
201 if (*p != ops) {
202 ret = -1;
203 goto out;
204 }
205
206 *p = (*p)->next;
207
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200208 if (ftrace_enabled) {
209 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500210 if (ftrace_list->next == &ftrace_list_end) {
211 ftrace_func_t func = ftrace_list->func;
212
Steven Rostedt978f3a42008-12-04 00:26:40 -0500213 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 set_ftrace_pid_function(func);
215 func = ftrace_pid_func;
216 }
217#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
218 ftrace_trace_function = func;
219#else
220 __ftrace_trace_function = func;
221#endif
222 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200223 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200224
225 out:
Steven Rostedt3d083392008-05-12 21:20:42 +0200226 spin_unlock(&ftrace_lock);
227
228 return ret;
229}
230
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500231static void ftrace_update_pid_func(void)
232{
233 ftrace_func_t func;
234
235 /* should not be called from interrupt context */
236 spin_lock(&ftrace_lock);
237
238 if (ftrace_trace_function == ftrace_stub)
239 goto out;
240
241 func = ftrace_trace_function;
242
Steven Rostedt978f3a42008-12-04 00:26:40 -0500243 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500244 set_ftrace_pid_function(func);
245 func = ftrace_pid_func;
246 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800247 if (func == ftrace_pid_func)
248 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500249 }
250
251#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
252 ftrace_trace_function = func;
253#else
254 __ftrace_trace_function = func;
255#endif
256
257 out:
258 spin_unlock(&ftrace_lock);
259}
260
Steven Rostedt3d083392008-05-12 21:20:42 +0200261#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400262#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400263# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400264#endif
265
Steven Noonan71c67d52008-09-20 01:00:37 -0700266/*
267 * Since MCOUNT_ADDR may point to mcount itself, we do not want
268 * to get it confused by reading a reference in the code as we
269 * are parsing on objcopy output of text. Use a variable for
270 * it instead.
271 */
272static unsigned long mcount_addr = MCOUNT_ADDR;
273
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200274enum {
275 FTRACE_ENABLE_CALLS = (1 << 0),
276 FTRACE_DISABLE_CALLS = (1 << 1),
277 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
278 FTRACE_ENABLE_MCOUNT = (1 << 3),
279 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500280 FTRACE_START_FUNC_RET = (1 << 5),
281 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200282};
283
Steven Rostedt5072c592008-05-12 21:20:43 +0200284static int ftrace_filtered;
285
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400286static LIST_HEAD(ftrace_new_addrs);
Steven Rostedt3d083392008-05-12 21:20:42 +0200287
Steven Rostedt41c52c02008-05-22 11:46:33 -0400288static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200289
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200290struct ftrace_page {
291 struct ftrace_page *next;
David Milleraa5e5ce2008-05-13 22:06:56 -0700292 unsigned long index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200293 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700294};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200295
296#define ENTRIES_PER_PAGE \
297 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
298
299/* estimate from running different kernels */
300#define NR_TO_INIT 10000
301
302static struct ftrace_page *ftrace_pages_start;
303static struct ftrace_page *ftrace_pages;
304
Steven Rostedt37ad50842008-05-12 21:20:48 +0200305static struct dyn_ftrace *ftrace_free_records;
306
Abhishek Sagarecea6562008-06-21 23:47:53 +0530307
308#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200309
310static int frozen_record_count;
311
Abhishek Sagarecea6562008-06-21 23:47:53 +0530312static inline void freeze_record(struct dyn_ftrace *rec)
313{
314 if (!(rec->flags & FTRACE_FL_FROZEN)) {
315 rec->flags |= FTRACE_FL_FROZEN;
316 frozen_record_count++;
317 }
318}
319
320static inline void unfreeze_record(struct dyn_ftrace *rec)
321{
322 if (rec->flags & FTRACE_FL_FROZEN) {
323 rec->flags &= ~FTRACE_FL_FROZEN;
324 frozen_record_count--;
325 }
326}
327
328static inline int record_frozen(struct dyn_ftrace *rec)
329{
330 return rec->flags & FTRACE_FL_FROZEN;
331}
332#else
333# define freeze_record(rec) ({ 0; })
334# define unfreeze_record(rec) ({ 0; })
335# define record_frozen(rec) ({ 0; })
336#endif /* CONFIG_KPROBES */
337
Ingo Molnare309b412008-05-12 21:20:51 +0200338static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +0200339{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200340 rec->ip = (unsigned long)ftrace_free_records;
341 ftrace_free_records = rec;
342 rec->flags |= FTRACE_FL_FREE;
343}
344
Steven Rostedtfed19392008-08-14 22:47:19 -0400345void ftrace_release(void *start, unsigned long size)
346{
347 struct dyn_ftrace *rec;
348 struct ftrace_page *pg;
349 unsigned long s = (unsigned long)start;
350 unsigned long e = s + size;
351 int i;
352
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400353 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400354 return;
355
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400356 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -0400357 spin_lock(&ftrace_lock);
358
359 for (pg = ftrace_pages_start; pg; pg = pg->next) {
360 for (i = 0; i < pg->index; i++) {
361 rec = &pg->records[i];
362
363 if ((rec->ip >= s) && (rec->ip < e))
364 ftrace_free_rec(rec);
365 }
366 }
367 spin_unlock(&ftrace_lock);
Steven Rostedtfed19392008-08-14 22:47:19 -0400368}
369
Ingo Molnare309b412008-05-12 21:20:51 +0200370static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200371{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200372 struct dyn_ftrace *rec;
373
374 /* First check for freed records */
375 if (ftrace_free_records) {
376 rec = ftrace_free_records;
377
Steven Rostedt37ad50842008-05-12 21:20:48 +0200378 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400379 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +0200380 ftrace_free_records = NULL;
381 return NULL;
382 }
383
384 ftrace_free_records = (void *)rec->ip;
385 memset(rec, 0, sizeof(*rec));
386 return rec;
387 }
388
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200389 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400390 if (!ftrace_pages->next) {
391 /* allocate another page */
392 ftrace_pages->next =
393 (void *)get_zeroed_page(GFP_KERNEL);
394 if (!ftrace_pages->next)
395 return NULL;
396 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200397 ftrace_pages = ftrace_pages->next;
398 }
399
400 return &ftrace_pages->records[ftrace_pages->index++];
401}
402
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400403static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200404ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200405{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400406 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200407
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800408 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400409 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200410
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400411 rec = ftrace_alloc_dyn_node(ip);
412 if (!rec)
413 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200414
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400415 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +0200416
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400417 list_add(&rec->list, &ftrace_new_addrs);
Steven Rostedt3d083392008-05-12 21:20:42 +0200418
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400419 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200420}
421
Steven Rostedt05736a42008-09-22 14:55:47 -0700422static void print_ip_ins(const char *fmt, unsigned char *p)
423{
424 int i;
425
426 printk(KERN_CONT "%s", fmt);
427
428 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
429 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
430}
431
Steven Rostedt31e88902008-11-14 16:21:19 -0800432static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800433{
434 switch (failed) {
435 case -EFAULT:
436 FTRACE_WARN_ON_ONCE(1);
437 pr_info("ftrace faulted on modifying ");
438 print_ip_sym(ip);
439 break;
440 case -EINVAL:
441 FTRACE_WARN_ON_ONCE(1);
442 pr_info("ftrace failed to modify ");
443 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800444 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800445 printk(KERN_CONT "\n");
446 break;
447 case -EPERM:
448 FTRACE_WARN_ON_ONCE(1);
449 pr_info("ftrace faulted on writing ");
450 print_ip_sym(ip);
451 break;
452 default:
453 FTRACE_WARN_ON_ONCE(1);
454 pr_info("ftrace faulted on unknown error ");
455 print_ip_sym(ip);
456 }
457}
458
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200459
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530460static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800461__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200462{
Ingo Molnare309b412008-05-12 21:20:51 +0200463 unsigned long ip, fl;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100464 unsigned long ftrace_addr;
465
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100466 ftrace_addr = (unsigned long)ftrace_caller;
Steven Rostedt5072c592008-05-12 21:20:43 +0200467
468 ip = rec->ip;
469
Steven Rostedt982c3502008-11-15 16:31:41 -0500470 /*
471 * If this record is not to be traced and
472 * it is not enabled then do nothing.
473 *
474 * If this record is not to be traced and
475 * it is enabled then disabled it.
476 *
477 */
478 if (rec->flags & FTRACE_FL_NOTRACE) {
479 if (rec->flags & FTRACE_FL_ENABLED)
480 rec->flags &= ~FTRACE_FL_ENABLED;
481 else
Steven Rostedt5072c592008-05-12 21:20:43 +0200482 return 0;
483
Steven Rostedt982c3502008-11-15 16:31:41 -0500484 } else if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200485 /*
Steven Rostedt982c3502008-11-15 16:31:41 -0500486 * Filtering is on:
Steven Rostedt5072c592008-05-12 21:20:43 +0200487 */
Steven Rostedt982c3502008-11-15 16:31:41 -0500488
489 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
490
491 /* Record is filtered and enabled, do nothing */
492 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
493 return 0;
494
495 /* Record is not filtered and is not enabled do nothing */
496 if (!fl)
497 return 0;
498
499 /* Record is not filtered but enabled, disable it */
500 if (fl == FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +0200501 rec->flags &= ~FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -0500502 else
503 /* Otherwise record is filtered but not enabled, enable it */
Steven Rostedt5072c592008-05-12 21:20:43 +0200504 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +0200505 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -0500506 /* Disable or not filtered */
Steven Rostedt5072c592008-05-12 21:20:43 +0200507
508 if (enable) {
Steven Rostedt982c3502008-11-15 16:31:41 -0500509 /* if record is enabled, do nothing */
Steven Rostedt41c52c02008-05-22 11:46:33 -0400510 if (rec->flags & FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +0200511 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -0500512
Steven Rostedt41c52c02008-05-22 11:46:33 -0400513 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -0500514
Steven Rostedt5072c592008-05-12 21:20:43 +0200515 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -0500516
517 /* if record is not enabled do nothing */
Steven Rostedt5072c592008-05-12 21:20:43 +0200518 if (!(rec->flags & FTRACE_FL_ENABLED))
519 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -0500520
Steven Rostedt5072c592008-05-12 21:20:43 +0200521 rec->flags &= ~FTRACE_FL_ENABLED;
522 }
523 }
524
Steven Rostedt982c3502008-11-15 16:31:41 -0500525 if (rec->flags & FTRACE_FL_ENABLED)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100526 return ftrace_make_call(rec, ftrace_addr);
Steven Rostedt31e88902008-11-14 16:21:19 -0800527 else
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100528 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +0200529}
530
531static void ftrace_replace_code(int enable)
532{
533 int i, failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200534 struct dyn_ftrace *rec;
535 struct ftrace_page *pg;
536
Steven Rostedt37ad50842008-05-12 21:20:48 +0200537 for (pg = ftrace_pages_start; pg; pg = pg->next) {
538 for (i = 0; i < pg->index; i++) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200539 rec = &pg->records[i];
540
Steven Rostedt918c1152008-11-14 16:21:19 -0800541 /*
542 * Skip over free records and records that have
543 * failed.
544 */
545 if (rec->flags & FTRACE_FL_FREE ||
546 rec->flags & FTRACE_FL_FAILED)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200547 continue;
548
549 /* ignore updates to this record's mcount site */
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200550 if (get_kprobe((void *)rec->ip)) {
551 freeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +0200552 continue;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200553 } else {
554 unfreeze_record(rec);
555 }
556
Steven Rostedt31e88902008-11-14 16:21:19 -0800557 failed = __ftrace_replace_code(rec, enable);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200558 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
559 rec->flags |= FTRACE_FL_FAILED;
560 if ((system_state == SYSTEM_BOOTING) ||
561 !core_kernel_text(rec->ip)) {
562 ftrace_free_rec(rec);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800563 } else
Steven Rostedt31e88902008-11-14 16:21:19 -0800564 ftrace_bug(failed, rec->ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200565 }
Steven Rostedt5072c592008-05-12 21:20:43 +0200566 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200567 }
568}
569
Ingo Molnare309b412008-05-12 21:20:51 +0200570static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800571ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200572{
573 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400574 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200575
576 ip = rec->ip;
577
Steven Rostedt31e88902008-11-14 16:21:19 -0800578 ret = ftrace_make_nop(mod, rec, mcount_addr);
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400579 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -0800580 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200581 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530582 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200583 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530584 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200585}
586
Ingo Molnare309b412008-05-12 21:20:51 +0200587static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200588{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200589 int *command = data;
590
Steven Rostedta3583242008-11-11 15:01:42 -0500591 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200592 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -0500593 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200594 ftrace_replace_code(0);
595
596 if (*command & FTRACE_UPDATE_TRACE_FUNC)
597 ftrace_update_ftrace_func(ftrace_trace_function);
598
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500599 if (*command & FTRACE_START_FUNC_RET)
600 ftrace_enable_ftrace_graph_caller();
601 else if (*command & FTRACE_STOP_FUNC_RET)
602 ftrace_disable_ftrace_graph_caller();
603
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200604 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200605}
606
Ingo Molnare309b412008-05-12 21:20:51 +0200607static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200608{
Rusty Russell784e2d72008-07-28 12:16:31 -0500609 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt3d083392008-05-12 21:20:42 +0200610}
611
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200612static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500613static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500614
615static void ftrace_startup_enable(int command)
616{
617 if (saved_ftrace_func != ftrace_trace_function) {
618 saved_ftrace_func = ftrace_trace_function;
619 command |= FTRACE_UPDATE_TRACE_FUNC;
620 }
621
622 if (!command || !ftrace_enabled)
623 return;
624
625 ftrace_run_update_code(command);
626}
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200627
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500628static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200629{
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200630 if (unlikely(ftrace_disabled))
631 return;
632
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400633 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500634 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -0500635 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +0200636
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500637 ftrace_startup_enable(command);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200638
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400639 mutex_unlock(&ftrace_start_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200640}
641
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500642static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200643{
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200644 if (unlikely(ftrace_disabled))
645 return;
646
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400647 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500648 ftrace_start_up--;
649 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200650 command |= FTRACE_DISABLE_CALLS;
651
652 if (saved_ftrace_func != ftrace_trace_function) {
653 saved_ftrace_func = ftrace_trace_function;
654 command |= FTRACE_UPDATE_TRACE_FUNC;
655 }
656
657 if (!command || !ftrace_enabled)
Steven Rostedt3d083392008-05-12 21:20:42 +0200658 goto out;
659
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200660 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200661 out:
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400662 mutex_unlock(&ftrace_start_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200663}
664
Ingo Molnare309b412008-05-12 21:20:51 +0200665static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200666{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200667 int command = FTRACE_ENABLE_MCOUNT;
668
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200669 if (unlikely(ftrace_disabled))
670 return;
671
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400672 mutex_lock(&ftrace_start_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200673 /* Force update next time */
674 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500675 /* ftrace_start_up is true if we want ftrace running */
676 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200677 command |= FTRACE_ENABLE_CALLS;
678
679 ftrace_run_update_code(command);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400680 mutex_unlock(&ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200681}
682
Ingo Molnare309b412008-05-12 21:20:51 +0200683static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200684{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200685 int command = FTRACE_DISABLE_MCOUNT;
686
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200687 if (unlikely(ftrace_disabled))
688 return;
689
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400690 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500691 /* ftrace_start_up is true if ftrace is running */
692 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200693 command |= FTRACE_DISABLE_CALLS;
694
695 ftrace_run_update_code(command);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400696 mutex_unlock(&ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200697}
698
Steven Rostedt3d083392008-05-12 21:20:42 +0200699static cycle_t ftrace_update_time;
700static unsigned long ftrace_update_cnt;
701unsigned long ftrace_update_tot_cnt;
702
Steven Rostedt31e88902008-11-14 16:21:19 -0800703static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +0200704{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400705 struct dyn_ftrace *p, *t;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530706 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200707
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200708 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200709 ftrace_update_cnt = 0;
710
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400711 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530712
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400713 /* If something went wrong, bail without enabling anything */
714 if (unlikely(ftrace_disabled))
715 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +0200716
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400717 list_del_init(&p->list);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530718
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400719 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -0800720 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400721 p->flags |= FTRACE_FL_CONVERTED;
722 ftrace_update_cnt++;
723 } else
724 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +0200725 }
726
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200727 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200728 ftrace_update_time = stop - start;
729 ftrace_update_tot_cnt += ftrace_update_cnt;
730
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200731 return 0;
732}
733
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400734static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200735{
736 struct ftrace_page *pg;
737 int cnt;
738 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200739
740 /* allocate a few pages */
741 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
742 if (!ftrace_pages_start)
743 return -1;
744
745 /*
746 * Allocate a few more pages.
747 *
748 * TODO: have some parser search vmlinux before
749 * final linking to find all calls to ftrace.
750 * Then we can:
751 * a) know how many pages to allocate.
752 * and/or
753 * b) set up the table then.
754 *
755 * The dynamic code is still necessary for
756 * modules.
757 */
758
759 pg = ftrace_pages = ftrace_pages_start;
760
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400761 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400762 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +0800763 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200764
765 for (i = 0; i < cnt; i++) {
766 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
767
768 /* If we fail, we'll try later anyway */
769 if (!pg->next)
770 break;
771
772 pg = pg->next;
773 }
774
775 return 0;
776}
777
Steven Rostedt5072c592008-05-12 21:20:43 +0200778enum {
779 FTRACE_ITER_FILTER = (1 << 0),
780 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -0400781 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530782 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt5072c592008-05-12 21:20:43 +0200783};
784
785#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
786
787struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +0200788 struct ftrace_page *pg;
789 unsigned idx;
790 unsigned flags;
791 unsigned char buffer[FTRACE_BUFF_MAX+1];
792 unsigned buffer_idx;
793 unsigned filtered;
794};
795
Ingo Molnare309b412008-05-12 21:20:51 +0200796static void *
Steven Rostedt5072c592008-05-12 21:20:43 +0200797t_next(struct seq_file *m, void *v, loff_t *pos)
798{
799 struct ftrace_iterator *iter = m->private;
800 struct dyn_ftrace *rec = NULL;
801
802 (*pos)++;
803
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400804 /* should not be called from interrupt context */
805 spin_lock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200806 retry:
807 if (iter->idx >= iter->pg->index) {
808 if (iter->pg->next) {
809 iter->pg = iter->pg->next;
810 iter->idx = 0;
811 goto retry;
Liming Wang50cdaf02008-11-28 12:13:21 +0800812 } else {
813 iter->idx = -1;
Steven Rostedt5072c592008-05-12 21:20:43 +0200814 }
815 } else {
816 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -0400817 if ((rec->flags & FTRACE_FL_FREE) ||
818
819 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530820 (rec->flags & FTRACE_FL_FAILED)) ||
821
822 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -0400823 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530824
Steven Rostedt0183fb1c2008-11-07 22:36:02 -0500825 ((iter->flags & FTRACE_ITER_FILTER) &&
826 !(rec->flags & FTRACE_FL_FILTER)) ||
827
Steven Rostedt41c52c02008-05-22 11:46:33 -0400828 ((iter->flags & FTRACE_ITER_NOTRACE) &&
829 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200830 rec = NULL;
831 goto retry;
832 }
833 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400834 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200835
Steven Rostedt5072c592008-05-12 21:20:43 +0200836 return rec;
837}
838
839static void *t_start(struct seq_file *m, loff_t *pos)
840{
841 struct ftrace_iterator *iter = m->private;
842 void *p = NULL;
Steven Rostedt5072c592008-05-12 21:20:43 +0200843
Liming Wang50cdaf02008-11-28 12:13:21 +0800844 if (*pos > 0) {
845 if (iter->idx < 0)
846 return p;
847 (*pos)--;
848 iter->idx--;
849 }
walimis5821e1b2008-11-15 15:19:06 +0800850
Liming Wang50cdaf02008-11-28 12:13:21 +0800851 p = t_next(m, p, pos);
Steven Rostedt5072c592008-05-12 21:20:43 +0200852
853 return p;
854}
855
856static void t_stop(struct seq_file *m, void *p)
857{
858}
859
860static int t_show(struct seq_file *m, void *v)
861{
862 struct dyn_ftrace *rec = v;
863 char str[KSYM_SYMBOL_LEN];
864
865 if (!rec)
866 return 0;
867
868 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
869
Liming Wang50cdaf02008-11-28 12:13:21 +0800870 seq_printf(m, "%s\n", str);
Steven Rostedt5072c592008-05-12 21:20:43 +0200871
872 return 0;
873}
874
875static struct seq_operations show_ftrace_seq_ops = {
876 .start = t_start,
877 .next = t_next,
878 .stop = t_stop,
879 .show = t_show,
880};
881
Ingo Molnare309b412008-05-12 21:20:51 +0200882static int
Steven Rostedt5072c592008-05-12 21:20:43 +0200883ftrace_avail_open(struct inode *inode, struct file *file)
884{
885 struct ftrace_iterator *iter;
886 int ret;
887
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200888 if (unlikely(ftrace_disabled))
889 return -ENODEV;
890
Steven Rostedt5072c592008-05-12 21:20:43 +0200891 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
892 if (!iter)
893 return -ENOMEM;
894
895 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +0200896
897 ret = seq_open(file, &show_ftrace_seq_ops);
898 if (!ret) {
899 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200900
Steven Rostedt5072c592008-05-12 21:20:43 +0200901 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200902 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +0200903 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200904 }
Steven Rostedt5072c592008-05-12 21:20:43 +0200905
906 return ret;
907}
908
909int ftrace_avail_release(struct inode *inode, struct file *file)
910{
911 struct seq_file *m = (struct seq_file *)file->private_data;
912 struct ftrace_iterator *iter = m->private;
913
914 seq_release(inode, file);
915 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200916
Steven Rostedt5072c592008-05-12 21:20:43 +0200917 return 0;
918}
919
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530920static int
921ftrace_failures_open(struct inode *inode, struct file *file)
922{
923 int ret;
924 struct seq_file *m;
925 struct ftrace_iterator *iter;
926
927 ret = ftrace_avail_open(inode, file);
928 if (!ret) {
929 m = (struct seq_file *)file->private_data;
930 iter = (struct ftrace_iterator *)m->private;
931 iter->flags = FTRACE_ITER_FAILURES;
932 }
933
934 return ret;
935}
936
937
Steven Rostedt41c52c02008-05-22 11:46:33 -0400938static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200939{
940 struct ftrace_page *pg;
941 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400942 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +0200943 unsigned i;
944
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400945 /* should not be called from interrupt context */
946 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400947 if (enable)
948 ftrace_filtered = 0;
Steven Rostedt5072c592008-05-12 21:20:43 +0200949 pg = ftrace_pages_start;
950 while (pg) {
951 for (i = 0; i < pg->index; i++) {
952 rec = &pg->records[i];
953 if (rec->flags & FTRACE_FL_FAILED)
954 continue;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400955 rec->flags &= ~type;
Steven Rostedt5072c592008-05-12 21:20:43 +0200956 }
957 pg = pg->next;
958 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400959 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200960}
961
Ingo Molnare309b412008-05-12 21:20:51 +0200962static int
Steven Rostedt41c52c02008-05-22 11:46:33 -0400963ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200964{
965 struct ftrace_iterator *iter;
966 int ret = 0;
967
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200968 if (unlikely(ftrace_disabled))
969 return -ENODEV;
970
Steven Rostedt5072c592008-05-12 21:20:43 +0200971 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
972 if (!iter)
973 return -ENOMEM;
974
Steven Rostedt41c52c02008-05-22 11:46:33 -0400975 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200976 if ((file->f_mode & FMODE_WRITE) &&
977 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -0400978 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +0200979
980 if (file->f_mode & FMODE_READ) {
981 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400982 iter->flags = enable ? FTRACE_ITER_FILTER :
983 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +0200984
985 ret = seq_open(file, &show_ftrace_seq_ops);
986 if (!ret) {
987 struct seq_file *m = file->private_data;
988 m->private = iter;
989 } else
990 kfree(iter);
991 } else
992 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400993 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200994
995 return ret;
996}
997
Steven Rostedt41c52c02008-05-22 11:46:33 -0400998static int
999ftrace_filter_open(struct inode *inode, struct file *file)
1000{
1001 return ftrace_regex_open(inode, file, 1);
1002}
1003
1004static int
1005ftrace_notrace_open(struct inode *inode, struct file *file)
1006{
1007 return ftrace_regex_open(inode, file, 0);
1008}
1009
Ingo Molnare309b412008-05-12 21:20:51 +02001010static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001011ftrace_regex_read(struct file *file, char __user *ubuf,
Steven Rostedt5072c592008-05-12 21:20:43 +02001012 size_t cnt, loff_t *ppos)
1013{
1014 if (file->f_mode & FMODE_READ)
1015 return seq_read(file, ubuf, cnt, ppos);
1016 else
1017 return -EPERM;
1018}
1019
Ingo Molnare309b412008-05-12 21:20:51 +02001020static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001021ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001022{
1023 loff_t ret;
1024
1025 if (file->f_mode & FMODE_READ)
1026 ret = seq_lseek(file, offset, origin);
1027 else
1028 file->f_pos = ret = 1;
1029
1030 return ret;
1031}
1032
1033enum {
1034 MATCH_FULL,
1035 MATCH_FRONT_ONLY,
1036 MATCH_MIDDLE_ONLY,
1037 MATCH_END_ONLY,
1038};
1039
Ingo Molnare309b412008-05-12 21:20:51 +02001040static void
Steven Rostedt41c52c02008-05-22 11:46:33 -04001041ftrace_match(unsigned char *buff, int len, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001042{
1043 char str[KSYM_SYMBOL_LEN];
1044 char *search = NULL;
1045 struct ftrace_page *pg;
1046 struct dyn_ftrace *rec;
1047 int type = MATCH_FULL;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001048 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001049 unsigned i, match = 0, search_len = 0;
1050
1051 for (i = 0; i < len; i++) {
1052 if (buff[i] == '*') {
1053 if (!i) {
1054 search = buff + i + 1;
1055 type = MATCH_END_ONLY;
1056 search_len = len - (i + 1);
1057 } else {
1058 if (type == MATCH_END_ONLY) {
1059 type = MATCH_MIDDLE_ONLY;
1060 } else {
1061 match = i;
1062 type = MATCH_FRONT_ONLY;
1063 }
1064 buff[i] = 0;
1065 break;
1066 }
1067 }
1068 }
1069
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001070 /* should not be called from interrupt context */
1071 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001072 if (enable)
1073 ftrace_filtered = 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001074 pg = ftrace_pages_start;
1075 while (pg) {
1076 for (i = 0; i < pg->index; i++) {
1077 int matched = 0;
1078 char *ptr;
1079
1080 rec = &pg->records[i];
1081 if (rec->flags & FTRACE_FL_FAILED)
1082 continue;
1083 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1084 switch (type) {
1085 case MATCH_FULL:
1086 if (strcmp(str, buff) == 0)
1087 matched = 1;
1088 break;
1089 case MATCH_FRONT_ONLY:
1090 if (memcmp(str, buff, match) == 0)
1091 matched = 1;
1092 break;
1093 case MATCH_MIDDLE_ONLY:
1094 if (strstr(str, search))
1095 matched = 1;
1096 break;
1097 case MATCH_END_ONLY:
1098 ptr = strstr(str, search);
1099 if (ptr && (ptr[search_len] == 0))
1100 matched = 1;
1101 break;
1102 }
1103 if (matched)
Steven Rostedt41c52c02008-05-22 11:46:33 -04001104 rec->flags |= flag;
Steven Rostedt5072c592008-05-12 21:20:43 +02001105 }
1106 pg = pg->next;
1107 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001108 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001109}
1110
Ingo Molnare309b412008-05-12 21:20:51 +02001111static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001112ftrace_regex_write(struct file *file, const char __user *ubuf,
1113 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001114{
1115 struct ftrace_iterator *iter;
1116 char ch;
1117 size_t read = 0;
1118 ssize_t ret;
1119
1120 if (!cnt || cnt < 0)
1121 return 0;
1122
Steven Rostedt41c52c02008-05-22 11:46:33 -04001123 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001124
1125 if (file->f_mode & FMODE_READ) {
1126 struct seq_file *m = file->private_data;
1127 iter = m->private;
1128 } else
1129 iter = file->private_data;
1130
1131 if (!*ppos) {
1132 iter->flags &= ~FTRACE_ITER_CONT;
1133 iter->buffer_idx = 0;
1134 }
1135
1136 ret = get_user(ch, ubuf++);
1137 if (ret)
1138 goto out;
1139 read++;
1140 cnt--;
1141
1142 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1143 /* skip white space */
1144 while (cnt && isspace(ch)) {
1145 ret = get_user(ch, ubuf++);
1146 if (ret)
1147 goto out;
1148 read++;
1149 cnt--;
1150 }
1151
Steven Rostedt5072c592008-05-12 21:20:43 +02001152 if (isspace(ch)) {
1153 file->f_pos += read;
1154 ret = read;
1155 goto out;
1156 }
1157
1158 iter->buffer_idx = 0;
1159 }
1160
1161 while (cnt && !isspace(ch)) {
1162 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1163 iter->buffer[iter->buffer_idx++] = ch;
1164 else {
1165 ret = -EINVAL;
1166 goto out;
1167 }
1168 ret = get_user(ch, ubuf++);
1169 if (ret)
1170 goto out;
1171 read++;
1172 cnt--;
1173 }
1174
1175 if (isspace(ch)) {
1176 iter->filtered++;
1177 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001178 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001179 iter->buffer_idx = 0;
1180 } else
1181 iter->flags |= FTRACE_ITER_CONT;
1182
1183
1184 file->f_pos += read;
1185
1186 ret = read;
1187 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04001188 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001189
1190 return ret;
1191}
1192
Steven Rostedt41c52c02008-05-22 11:46:33 -04001193static ssize_t
1194ftrace_filter_write(struct file *file, const char __user *ubuf,
1195 size_t cnt, loff_t *ppos)
1196{
1197 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1198}
1199
1200static ssize_t
1201ftrace_notrace_write(struct file *file, const char __user *ubuf,
1202 size_t cnt, loff_t *ppos)
1203{
1204 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1205}
1206
1207static void
1208ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1209{
1210 if (unlikely(ftrace_disabled))
1211 return;
1212
1213 mutex_lock(&ftrace_regex_lock);
1214 if (reset)
1215 ftrace_filter_reset(enable);
1216 if (buf)
1217 ftrace_match(buf, len, enable);
1218 mutex_unlock(&ftrace_regex_lock);
1219}
1220
Steven Rostedt77a2b372008-05-12 21:20:45 +02001221/**
1222 * ftrace_set_filter - set a function to filter on in ftrace
1223 * @buf - the string that holds the function filter text.
1224 * @len - the length of the string.
1225 * @reset - non zero to reset all filters before applying this filter.
1226 *
1227 * Filters denote which functions should be enabled when tracing is enabled.
1228 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1229 */
Ingo Molnare309b412008-05-12 21:20:51 +02001230void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02001231{
Steven Rostedt41c52c02008-05-22 11:46:33 -04001232 ftrace_set_regex(buf, len, reset, 1);
1233}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001234
Steven Rostedt41c52c02008-05-22 11:46:33 -04001235/**
1236 * ftrace_set_notrace - set a function to not trace in ftrace
1237 * @buf - the string that holds the function notrace text.
1238 * @len - the length of the string.
1239 * @reset - non zero to reset all filters before applying this filter.
1240 *
1241 * Notrace Filters denote which functions should not be enabled when tracing
1242 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1243 * for tracing.
1244 */
1245void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1246{
1247 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02001248}
1249
Ingo Molnare309b412008-05-12 21:20:51 +02001250static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001251ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001252{
1253 struct seq_file *m = (struct seq_file *)file->private_data;
1254 struct ftrace_iterator *iter;
1255
Steven Rostedt41c52c02008-05-22 11:46:33 -04001256 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001257 if (file->f_mode & FMODE_READ) {
1258 iter = m->private;
1259
1260 seq_release(inode, file);
1261 } else
1262 iter = file->private_data;
1263
1264 if (iter->buffer_idx) {
1265 iter->filtered++;
1266 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001267 ftrace_match(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001268 }
1269
1270 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001271 mutex_lock(&ftrace_start_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05001272 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02001273 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001274 mutex_unlock(&ftrace_start_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001275 mutex_unlock(&ftrace_sysctl_lock);
1276
1277 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001278 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001279 return 0;
1280}
1281
Steven Rostedt41c52c02008-05-22 11:46:33 -04001282static int
1283ftrace_filter_release(struct inode *inode, struct file *file)
1284{
1285 return ftrace_regex_release(inode, file, 1);
1286}
1287
1288static int
1289ftrace_notrace_release(struct inode *inode, struct file *file)
1290{
1291 return ftrace_regex_release(inode, file, 0);
1292}
1293
Steven Rostedt5072c592008-05-12 21:20:43 +02001294static struct file_operations ftrace_avail_fops = {
1295 .open = ftrace_avail_open,
1296 .read = seq_read,
1297 .llseek = seq_lseek,
1298 .release = ftrace_avail_release,
1299};
1300
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301301static struct file_operations ftrace_failures_fops = {
1302 .open = ftrace_failures_open,
1303 .read = seq_read,
1304 .llseek = seq_lseek,
1305 .release = ftrace_avail_release,
1306};
1307
Steven Rostedt5072c592008-05-12 21:20:43 +02001308static struct file_operations ftrace_filter_fops = {
1309 .open = ftrace_filter_open,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001310 .read = ftrace_regex_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02001311 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001312 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02001313 .release = ftrace_filter_release,
1314};
1315
Steven Rostedt41c52c02008-05-22 11:46:33 -04001316static struct file_operations ftrace_notrace_fops = {
1317 .open = ftrace_notrace_open,
1318 .read = ftrace_regex_read,
1319 .write = ftrace_notrace_write,
1320 .llseek = ftrace_regex_lseek,
1321 .release = ftrace_notrace_release,
1322};
1323
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001324#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1325
1326static DEFINE_MUTEX(graph_lock);
1327
1328int ftrace_graph_count;
1329unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1330
1331static void *
1332g_next(struct seq_file *m, void *v, loff_t *pos)
1333{
1334 unsigned long *array = m->private;
1335 int index = *pos;
1336
1337 (*pos)++;
1338
1339 if (index >= ftrace_graph_count)
1340 return NULL;
1341
1342 return &array[index];
1343}
1344
1345static void *g_start(struct seq_file *m, loff_t *pos)
1346{
1347 void *p = NULL;
1348
1349 mutex_lock(&graph_lock);
1350
1351 p = g_next(m, p, pos);
1352
1353 return p;
1354}
1355
1356static void g_stop(struct seq_file *m, void *p)
1357{
1358 mutex_unlock(&graph_lock);
1359}
1360
1361static int g_show(struct seq_file *m, void *v)
1362{
1363 unsigned long *ptr = v;
1364 char str[KSYM_SYMBOL_LEN];
1365
1366 if (!ptr)
1367 return 0;
1368
1369 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1370
1371 seq_printf(m, "%s\n", str);
1372
1373 return 0;
1374}
1375
1376static struct seq_operations ftrace_graph_seq_ops = {
1377 .start = g_start,
1378 .next = g_next,
1379 .stop = g_stop,
1380 .show = g_show,
1381};
1382
1383static int
1384ftrace_graph_open(struct inode *inode, struct file *file)
1385{
1386 int ret = 0;
1387
1388 if (unlikely(ftrace_disabled))
1389 return -ENODEV;
1390
1391 mutex_lock(&graph_lock);
1392 if ((file->f_mode & FMODE_WRITE) &&
1393 !(file->f_flags & O_APPEND)) {
1394 ftrace_graph_count = 0;
1395 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1396 }
1397
1398 if (file->f_mode & FMODE_READ) {
1399 ret = seq_open(file, &ftrace_graph_seq_ops);
1400 if (!ret) {
1401 struct seq_file *m = file->private_data;
1402 m->private = ftrace_graph_funcs;
1403 }
1404 } else
1405 file->private_data = ftrace_graph_funcs;
1406 mutex_unlock(&graph_lock);
1407
1408 return ret;
1409}
1410
1411static ssize_t
1412ftrace_graph_read(struct file *file, char __user *ubuf,
1413 size_t cnt, loff_t *ppos)
1414{
1415 if (file->f_mode & FMODE_READ)
1416 return seq_read(file, ubuf, cnt, ppos);
1417 else
1418 return -EPERM;
1419}
1420
1421static int
1422ftrace_set_func(unsigned long *array, int idx, char *buffer)
1423{
1424 char str[KSYM_SYMBOL_LEN];
1425 struct dyn_ftrace *rec;
1426 struct ftrace_page *pg;
1427 int found = 0;
Liming Wangfaec2ec2008-12-04 14:24:49 +08001428 int i, j;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001429
1430 if (ftrace_disabled)
1431 return -ENODEV;
1432
1433 /* should not be called from interrupt context */
1434 spin_lock(&ftrace_lock);
1435
1436 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1437 for (i = 0; i < pg->index; i++) {
1438 rec = &pg->records[i];
1439
1440 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
1441 continue;
1442
1443 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1444 if (strcmp(str, buffer) == 0) {
1445 found = 1;
Liming Wangfaec2ec2008-12-04 14:24:49 +08001446 for (j = 0; j < idx; j++)
1447 if (array[j] == rec->ip) {
1448 found = 0;
1449 break;
1450 }
1451 if (found)
1452 array[idx] = rec->ip;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001453 break;
1454 }
1455 }
1456 }
1457 spin_unlock(&ftrace_lock);
1458
1459 return found ? 0 : -EINVAL;
1460}
1461
1462static ssize_t
1463ftrace_graph_write(struct file *file, const char __user *ubuf,
1464 size_t cnt, loff_t *ppos)
1465{
1466 unsigned char buffer[FTRACE_BUFF_MAX+1];
1467 unsigned long *array;
1468 size_t read = 0;
1469 ssize_t ret;
1470 int index = 0;
1471 char ch;
1472
1473 if (!cnt || cnt < 0)
1474 return 0;
1475
1476 mutex_lock(&graph_lock);
1477
1478 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
1479 ret = -EBUSY;
1480 goto out;
1481 }
1482
1483 if (file->f_mode & FMODE_READ) {
1484 struct seq_file *m = file->private_data;
1485 array = m->private;
1486 } else
1487 array = file->private_data;
1488
1489 ret = get_user(ch, ubuf++);
1490 if (ret)
1491 goto out;
1492 read++;
1493 cnt--;
1494
1495 /* skip white space */
1496 while (cnt && isspace(ch)) {
1497 ret = get_user(ch, ubuf++);
1498 if (ret)
1499 goto out;
1500 read++;
1501 cnt--;
1502 }
1503
1504 if (isspace(ch)) {
1505 *ppos += read;
1506 ret = read;
1507 goto out;
1508 }
1509
1510 while (cnt && !isspace(ch)) {
1511 if (index < FTRACE_BUFF_MAX)
1512 buffer[index++] = ch;
1513 else {
1514 ret = -EINVAL;
1515 goto out;
1516 }
1517 ret = get_user(ch, ubuf++);
1518 if (ret)
1519 goto out;
1520 read++;
1521 cnt--;
1522 }
1523 buffer[index] = 0;
1524
1525 /* we allow only one at a time */
1526 ret = ftrace_set_func(array, ftrace_graph_count, buffer);
1527 if (ret)
1528 goto out;
1529
1530 ftrace_graph_count++;
1531
1532 file->f_pos += read;
1533
1534 ret = read;
1535 out:
1536 mutex_unlock(&graph_lock);
1537
1538 return ret;
1539}
1540
1541static const struct file_operations ftrace_graph_fops = {
1542 .open = ftrace_graph_open,
1543 .read = ftrace_graph_read,
1544 .write = ftrace_graph_write,
1545};
1546#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1547
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001548static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02001549{
Steven Rostedt5072c592008-05-12 21:20:43 +02001550 struct dentry *entry;
1551
Steven Rostedt5072c592008-05-12 21:20:43 +02001552 entry = debugfs_create_file("available_filter_functions", 0444,
1553 d_tracer, NULL, &ftrace_avail_fops);
1554 if (!entry)
1555 pr_warning("Could not create debugfs "
1556 "'available_filter_functions' entry\n");
1557
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301558 entry = debugfs_create_file("failures", 0444,
1559 d_tracer, NULL, &ftrace_failures_fops);
1560 if (!entry)
1561 pr_warning("Could not create debugfs 'failures' entry\n");
1562
Steven Rostedt5072c592008-05-12 21:20:43 +02001563 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1564 NULL, &ftrace_filter_fops);
1565 if (!entry)
1566 pr_warning("Could not create debugfs "
1567 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04001568
1569 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1570 NULL, &ftrace_notrace_fops);
1571 if (!entry)
1572 pr_warning("Could not create debugfs "
1573 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001574
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001575#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1576 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
1577 NULL,
1578 &ftrace_graph_fops);
1579 if (!entry)
1580 pr_warning("Could not create debugfs "
1581 "'set_graph_function' entry\n");
1582#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1583
Steven Rostedt5072c592008-05-12 21:20:43 +02001584 return 0;
1585}
1586
Steven Rostedt31e88902008-11-14 16:21:19 -08001587static int ftrace_convert_nops(struct module *mod,
1588 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001589 unsigned long *end)
1590{
1591 unsigned long *p;
1592 unsigned long addr;
1593 unsigned long flags;
1594
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001595 mutex_lock(&ftrace_start_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001596 p = start;
1597 while (p < end) {
1598 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08001599 /*
1600 * Some architecture linkers will pad between
1601 * the different mcount_loc sections of different
1602 * object files to satisfy alignments.
1603 * Skip any NULL pointers.
1604 */
1605 if (!addr)
1606 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001607 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001608 }
1609
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001610 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001611 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08001612 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001613 local_irq_restore(flags);
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001614 mutex_unlock(&ftrace_start_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001615
1616 return 0;
1617}
1618
Steven Rostedt31e88902008-11-14 16:21:19 -08001619void ftrace_init_module(struct module *mod,
1620 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04001621{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04001622 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04001623 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08001624 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04001625}
1626
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001627extern unsigned long __start_mcount_loc[];
1628extern unsigned long __stop_mcount_loc[];
1629
1630void __init ftrace_init(void)
1631{
1632 unsigned long count, addr, flags;
1633 int ret;
1634
1635 /* Keep the ftrace pointer to the stub */
1636 addr = (unsigned long)ftrace_stub;
1637
1638 local_irq_save(flags);
1639 ftrace_dyn_arch_init(&addr);
1640 local_irq_restore(flags);
1641
1642 /* ftrace_dyn_arch_init places the return code in addr */
1643 if (addr)
1644 goto failed;
1645
1646 count = __stop_mcount_loc - __start_mcount_loc;
1647
1648 ret = ftrace_dyn_table_alloc(count);
1649 if (ret)
1650 goto failed;
1651
1652 last_ftrace_enabled = ftrace_enabled = 1;
1653
Steven Rostedt31e88902008-11-14 16:21:19 -08001654 ret = ftrace_convert_nops(NULL,
1655 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001656 __stop_mcount_loc);
1657
1658 return;
1659 failed:
1660 ftrace_disabled = 1;
1661}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001662
Steven Rostedt3d083392008-05-12 21:20:42 +02001663#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01001664
1665static int __init ftrace_nodyn_init(void)
1666{
1667 ftrace_enabled = 1;
1668 return 0;
1669}
1670device_initcall(ftrace_nodyn_init);
1671
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001672static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
1673static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001674/* Keep as macros so we do not need to define the commands */
1675# define ftrace_startup(command) do { } while (0)
1676# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001677# define ftrace_startup_sysctl() do { } while (0)
1678# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02001679#endif /* CONFIG_DYNAMIC_FTRACE */
1680
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001681static ssize_t
1682ftrace_pid_read(struct file *file, char __user *ubuf,
1683 size_t cnt, loff_t *ppos)
1684{
1685 char buf[64];
1686 int r;
1687
Steven Rostedte32d8952008-12-04 00:26:41 -05001688 if (ftrace_pid_trace == ftrace_swapper_pid)
1689 r = sprintf(buf, "swapper tasks\n");
1690 else if (ftrace_pid_trace)
Steven Rostedt978f3a42008-12-04 00:26:40 -05001691 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001692 else
1693 r = sprintf(buf, "no pid\n");
1694
1695 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1696}
1697
Steven Rostedte32d8952008-12-04 00:26:41 -05001698static void clear_ftrace_swapper(void)
1699{
1700 struct task_struct *p;
1701 int cpu;
1702
1703 get_online_cpus();
1704 for_each_online_cpu(cpu) {
1705 p = idle_task(cpu);
1706 clear_tsk_trace_trace(p);
1707 }
1708 put_online_cpus();
1709}
1710
1711static void set_ftrace_swapper(void)
1712{
1713 struct task_struct *p;
1714 int cpu;
1715
1716 get_online_cpus();
1717 for_each_online_cpu(cpu) {
1718 p = idle_task(cpu);
1719 set_tsk_trace_trace(p);
1720 }
1721 put_online_cpus();
1722}
1723
1724static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05001725{
1726 struct task_struct *p;
1727
Steven Rostedte32d8952008-12-04 00:26:41 -05001728 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05001729 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05001730 } while_each_pid_task(pid, PIDTYPE_PID, p);
1731 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05001732}
1733
Steven Rostedte32d8952008-12-04 00:26:41 -05001734static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05001735{
1736 struct task_struct *p;
1737
1738 do_each_pid_task(pid, PIDTYPE_PID, p) {
1739 set_tsk_trace_trace(p);
1740 } while_each_pid_task(pid, PIDTYPE_PID, p);
1741}
1742
Steven Rostedte32d8952008-12-04 00:26:41 -05001743static void clear_ftrace_pid_task(struct pid **pid)
1744{
1745 if (*pid == ftrace_swapper_pid)
1746 clear_ftrace_swapper();
1747 else
1748 clear_ftrace_pid(*pid);
1749
1750 *pid = NULL;
1751}
1752
1753static void set_ftrace_pid_task(struct pid *pid)
1754{
1755 if (pid == ftrace_swapper_pid)
1756 set_ftrace_swapper();
1757 else
1758 set_ftrace_pid(pid);
1759}
1760
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001761static ssize_t
1762ftrace_pid_write(struct file *filp, const char __user *ubuf,
1763 size_t cnt, loff_t *ppos)
1764{
Steven Rostedt978f3a42008-12-04 00:26:40 -05001765 struct pid *pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001766 char buf[64];
1767 long val;
1768 int ret;
1769
1770 if (cnt >= sizeof(buf))
1771 return -EINVAL;
1772
1773 if (copy_from_user(&buf, ubuf, cnt))
1774 return -EFAULT;
1775
1776 buf[cnt] = 0;
1777
1778 ret = strict_strtol(buf, 10, &val);
1779 if (ret < 0)
1780 return ret;
1781
1782 mutex_lock(&ftrace_start_lock);
Steven Rostedt978f3a42008-12-04 00:26:40 -05001783 if (val < 0) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001784 /* disable pid tracing */
Steven Rostedt978f3a42008-12-04 00:26:40 -05001785 if (!ftrace_pid_trace)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001786 goto out;
Steven Rostedt978f3a42008-12-04 00:26:40 -05001787
1788 clear_ftrace_pid_task(&ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001789
1790 } else {
Steven Rostedte32d8952008-12-04 00:26:41 -05001791 /* swapper task is special */
1792 if (!val) {
1793 pid = ftrace_swapper_pid;
1794 if (pid == ftrace_pid_trace)
1795 goto out;
1796 } else {
1797 pid = find_get_pid(val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001798
Steven Rostedte32d8952008-12-04 00:26:41 -05001799 if (pid == ftrace_pid_trace) {
1800 put_pid(pid);
1801 goto out;
1802 }
Steven Rostedt978f3a42008-12-04 00:26:40 -05001803 }
1804
1805 if (ftrace_pid_trace)
1806 clear_ftrace_pid_task(&ftrace_pid_trace);
1807
1808 if (!pid)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001809 goto out;
1810
Steven Rostedt978f3a42008-12-04 00:26:40 -05001811 ftrace_pid_trace = pid;
Steven Rostedt0ef8cde2008-12-03 15:36:58 -05001812
Steven Rostedt978f3a42008-12-04 00:26:40 -05001813 set_ftrace_pid_task(ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001814 }
1815
1816 /* update the function call */
1817 ftrace_update_pid_func();
1818 ftrace_startup_enable(0);
1819
1820 out:
1821 mutex_unlock(&ftrace_start_lock);
1822
1823 return cnt;
1824}
1825
1826static struct file_operations ftrace_pid_fops = {
1827 .read = ftrace_pid_read,
1828 .write = ftrace_pid_write,
1829};
1830
1831static __init int ftrace_init_debugfs(void)
1832{
1833 struct dentry *d_tracer;
1834 struct dentry *entry;
1835
1836 d_tracer = tracing_init_dentry();
1837 if (!d_tracer)
1838 return 0;
1839
1840 ftrace_init_dyn_debugfs(d_tracer);
1841
1842 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
1843 NULL, &ftrace_pid_fops);
1844 if (!entry)
1845 pr_warning("Could not create debugfs "
1846 "'set_ftrace_pid' entry\n");
1847 return 0;
1848}
1849
1850fs_initcall(ftrace_init_debugfs);
1851
Steven Rostedt3d083392008-05-12 21:20:42 +02001852/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04001853 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001854 *
1855 * This function should be used by panic code. It stops ftrace
1856 * but in a not so nice way. If you need to simply kill ftrace
1857 * from a non-atomic section, use ftrace_kill.
1858 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04001859void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001860{
1861 ftrace_disabled = 1;
1862 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04001863 clear_ftrace_function();
1864}
1865
1866/**
Steven Rostedt3d083392008-05-12 21:20:42 +02001867 * register_ftrace_function - register a function for profiling
1868 * @ops - ops structure that holds the function for profiling.
1869 *
1870 * Register a function to be called by all functions in the
1871 * kernel.
1872 *
1873 * Note: @ops->func and all the functions it calls must be labeled
1874 * with "notrace", otherwise it will go into a
1875 * recursive loop.
1876 */
1877int register_ftrace_function(struct ftrace_ops *ops)
1878{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001879 int ret;
1880
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001881 if (unlikely(ftrace_disabled))
1882 return -1;
1883
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001884 mutex_lock(&ftrace_sysctl_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001885
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001886 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001887 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001888
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001889 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001890 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02001891}
1892
1893/**
1894 * unregister_ftrace_function - unresgister a function for profiling.
1895 * @ops - ops structure that holds the function to unregister
1896 *
1897 * Unregister a function that was added to be called by ftrace profiling.
1898 */
1899int unregister_ftrace_function(struct ftrace_ops *ops)
1900{
1901 int ret;
1902
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001903 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001904 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001905 ftrace_shutdown(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001906 mutex_unlock(&ftrace_sysctl_lock);
1907
1908 return ret;
1909}
1910
Ingo Molnare309b412008-05-12 21:20:51 +02001911int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001912ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02001913 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001914 loff_t *ppos)
1915{
1916 int ret;
1917
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001918 if (unlikely(ftrace_disabled))
1919 return -ENODEV;
1920
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001921 mutex_lock(&ftrace_sysctl_lock);
1922
Steven Rostedt5072c592008-05-12 21:20:43 +02001923 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001924
1925 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1926 goto out;
1927
1928 last_ftrace_enabled = ftrace_enabled;
1929
1930 if (ftrace_enabled) {
1931
1932 ftrace_startup_sysctl();
1933
1934 /* we are starting ftrace again */
1935 if (ftrace_list != &ftrace_list_end) {
1936 if (ftrace_list->next == &ftrace_list_end)
1937 ftrace_trace_function = ftrace_list->func;
1938 else
1939 ftrace_trace_function = ftrace_list_func;
1940 }
1941
1942 } else {
1943 /* stopping ftrace calls (just send to ftrace_stub) */
1944 ftrace_trace_function = ftrace_stub;
1945
1946 ftrace_shutdown_sysctl();
1947 }
1948
1949 out:
1950 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001951 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001952}
Ingo Molnarf17845e2008-10-24 12:47:10 +02001953
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001954#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001955
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001956static atomic_t ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01001957
Steven Rostedte49dc192008-12-02 23:50:05 -05001958int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
1959{
1960 return 0;
1961}
1962
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001963/* The callbacks that hook a function */
1964trace_func_graph_ret_t ftrace_graph_return =
1965 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05001966trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01001967
1968/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
1969static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
1970{
1971 int i;
1972 int ret = 0;
1973 unsigned long flags;
1974 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
1975 struct task_struct *g, *t;
1976
1977 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
1978 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
1979 * sizeof(struct ftrace_ret_stack),
1980 GFP_KERNEL);
1981 if (!ret_stack_list[i]) {
1982 start = 0;
1983 end = i;
1984 ret = -ENOMEM;
1985 goto free;
1986 }
1987 }
1988
1989 read_lock_irqsave(&tasklist_lock, flags);
1990 do_each_thread(g, t) {
1991 if (start == end) {
1992 ret = -EAGAIN;
1993 goto unlock;
1994 }
1995
1996 if (t->ret_stack == NULL) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01001997 t->curr_ret_stack = -1;
Frederic Weisbecker48d68b22008-12-02 00:20:39 +01001998 /* Make sure IRQs see the -1 first: */
1999 barrier();
2000 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002001 atomic_set(&t->trace_overrun, 0);
2002 }
2003 } while_each_thread(g, t);
2004
2005unlock:
2006 read_unlock_irqrestore(&tasklist_lock, flags);
2007free:
2008 for (i = start; i < end; i++)
2009 kfree(ret_stack_list[i]);
2010 return ret;
2011}
2012
2013/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002014static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002015{
2016 struct ftrace_ret_stack **ret_stack_list;
2017 int ret;
2018
2019 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2020 sizeof(struct ftrace_ret_stack *),
2021 GFP_KERNEL);
2022
2023 if (!ret_stack_list)
2024 return -ENOMEM;
2025
2026 do {
2027 ret = alloc_retstack_tasklist(ret_stack_list);
2028 } while (ret == -EAGAIN);
2029
2030 kfree(ret_stack_list);
2031 return ret;
2032}
2033
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002034int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2035 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002036{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002037 int ret = 0;
2038
2039 mutex_lock(&ftrace_sysctl_lock);
2040
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002041 atomic_inc(&ftrace_graph_active);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002042 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002043 if (ret) {
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002044 atomic_dec(&ftrace_graph_active);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002045 goto out;
2046 }
Steven Rostedte53a6312008-11-26 00:16:25 -05002047
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002048 ftrace_graph_return = retfunc;
2049 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05002050
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002051 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002052
2053out:
2054 mutex_unlock(&ftrace_sysctl_lock);
2055 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002056}
2057
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002058void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002059{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002060 mutex_lock(&ftrace_sysctl_lock);
2061
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002062 atomic_dec(&ftrace_graph_active);
2063 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05002064 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002065 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002066
2067 mutex_unlock(&ftrace_sysctl_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002068}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002069
2070/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002071void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002072{
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002073 if (atomic_read(&ftrace_graph_active)) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002074 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2075 * sizeof(struct ftrace_ret_stack),
2076 GFP_KERNEL);
2077 if (!t->ret_stack)
2078 return;
2079 t->curr_ret_stack = -1;
2080 atomic_set(&t->trace_overrun, 0);
2081 } else
2082 t->ret_stack = NULL;
2083}
2084
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002085void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002086{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01002087 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2088
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002089 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01002090 /* NULL must become visible to IRQs before we free it: */
2091 barrier();
2092
2093 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002094}
Steven Rostedt14a866c2008-12-02 23:50:02 -05002095
2096void ftrace_graph_stop(void)
2097{
2098 ftrace_stop();
2099}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002100#endif
2101