blob: 45a44c4025665bed6b5646c28bcbb0df05121a41 [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>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Abhishek Sagarf22f9a82008-06-21 23:50:29 +053025#include <linux/kprobes.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010026#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020027#include <linux/sysctl.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020030
Abhishek Sagar395a59d2008-06-21 23:47:27 +053031#include <asm/ftrace.h>
32
Steven Rostedt3d083392008-05-12 21:20:42 +020033#include "trace.h"
34
Steven Rostedt6912896e2008-10-23 09:33:03 -040035#define FTRACE_WARN_ON(cond) \
36 do { \
37 if (WARN_ON(cond)) \
38 ftrace_kill(); \
39 } while (0)
40
41#define FTRACE_WARN_ON_ONCE(cond) \
42 do { \
43 if (WARN_ON_ONCE(cond)) \
44 ftrace_kill(); \
45 } while (0)
46
Steven Rostedt4eebcc82008-05-12 21:20:48 +020047/* ftrace_enabled is a method to turn ftrace on or off */
48int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020049static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020050
Steven Rostedt0ef8cde2008-12-03 15:36:58 -050051/* set when tracing only a pid */
Steven Rostedt978f3a42008-12-04 00:26:40 -050052struct pid *ftrace_pid_trace;
Steven Rostedt21bbecd2008-12-04 23:30:56 -050053static struct pid * const ftrace_swapper_pid = &init_struct_pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050054
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050055/* Quick disabling of function tracer. */
56int function_trace_stop;
57
Steven Rostedt4eebcc82008-05-12 21:20:48 +020058/*
59 * ftrace_disabled is set when an anomaly is discovered.
60 * ftrace_disabled is much stronger than ftrace_enabled.
61 */
62static int ftrace_disabled __read_mostly;
63
Steven Rostedt3d083392008-05-12 21:20:42 +020064static DEFINE_SPINLOCK(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020065static DEFINE_MUTEX(ftrace_sysctl_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -050066static DEFINE_MUTEX(ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020067
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020068static struct ftrace_ops ftrace_list_end __read_mostly =
69{
70 .func = ftrace_stub,
71};
72
73static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
74ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050075ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050076ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020077
Ingo Molnarf2252932008-05-22 10:37:48 +020078static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020079{
80 struct ftrace_ops *op = ftrace_list;
81
82 /* in case someone actually ports this to alpha! */
83 read_barrier_depends();
84
85 while (op != &ftrace_list_end) {
86 /* silly alpha */
87 read_barrier_depends();
88 op->func(ip, parent_ip);
89 op = op->next;
90 };
91}
92
Steven Rostedtdf4fc312008-11-26 00:16:23 -050093static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
94{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -050095 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -050096 return;
97
98 ftrace_pid_function(ip, parent_ip);
99}
100
101static void set_ftrace_pid_function(ftrace_func_t func)
102{
103 /* do not set ftrace_pid_function to itself! */
104 if (func != ftrace_pid_func)
105 ftrace_pid_function = func;
106}
107
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200108/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200109 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200110 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200111 * This NULLs the ftrace function and in essence stops
112 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200114void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115{
Steven Rostedt3d083392008-05-12 21:20:42 +0200116 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500117 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500118 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200119}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500121#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
122/*
123 * For those archs that do not test ftrace_trace_stop in their
124 * mcount call site, we need to do it from C.
125 */
126static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
127{
128 if (function_trace_stop)
129 return;
130
131 __ftrace_trace_function(ip, parent_ip);
132}
133#endif
134
Ingo Molnare309b412008-05-12 21:20:51 +0200135static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200136{
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400137 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200138 spin_lock(&ftrace_lock);
139
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200140 ops->next = ftrace_list;
141 /*
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
146 */
147 smp_wmb();
148 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200149
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200150 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500151 ftrace_func_t func;
152
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
157
Steven Rostedt978f3a42008-12-04 00:26:40 -0500158 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
161 }
162
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200163 /*
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
166 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500167#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500168 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500169#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500170 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500171 ftrace_trace_function = ftrace_test_stop_func;
172#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200173 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200174
175 spin_unlock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200176
177 return 0;
178}
179
Ingo Molnare309b412008-05-12 21:20:51 +0200180static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200181{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200182 struct ftrace_ops **p;
183 int ret = 0;
184
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400185 /* should not be called from interrupt context */
Steven Rostedt3d083392008-05-12 21:20:42 +0200186 spin_lock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187
188 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200189 * If we are removing the last function, then simply point
190 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 */
192 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
193 ftrace_trace_function = ftrace_stub;
194 ftrace_list = &ftrace_list_end;
195 goto out;
196 }
197
198 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
199 if (*p == ops)
200 break;
201
202 if (*p != ops) {
203 ret = -1;
204 goto out;
205 }
206
207 *p = (*p)->next;
208
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200209 if (ftrace_enabled) {
210 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500211 if (ftrace_list->next == &ftrace_list_end) {
212 ftrace_func_t func = ftrace_list->func;
213
Steven Rostedt978f3a42008-12-04 00:26:40 -0500214 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500215 set_ftrace_pid_function(func);
216 func = ftrace_pid_func;
217 }
218#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
219 ftrace_trace_function = func;
220#else
221 __ftrace_trace_function = func;
222#endif
223 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200224 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200225
226 out:
Steven Rostedt3d083392008-05-12 21:20:42 +0200227 spin_unlock(&ftrace_lock);
228
229 return ret;
230}
231
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232static void ftrace_update_pid_func(void)
233{
234 ftrace_func_t func;
235
236 /* should not be called from interrupt context */
237 spin_lock(&ftrace_lock);
238
239 if (ftrace_trace_function == ftrace_stub)
240 goto out;
241
242 func = ftrace_trace_function;
243
Steven Rostedt978f3a42008-12-04 00:26:40 -0500244 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500245 set_ftrace_pid_function(func);
246 func = ftrace_pid_func;
247 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800248 if (func == ftrace_pid_func)
249 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500250 }
251
252#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
253 ftrace_trace_function = func;
254#else
255 __ftrace_trace_function = func;
256#endif
257
258 out:
259 spin_unlock(&ftrace_lock);
260}
261
Steven Rostedt3d083392008-05-12 21:20:42 +0200262#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400263#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400264# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400265#endif
266
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200267enum {
268 FTRACE_ENABLE_CALLS = (1 << 0),
269 FTRACE_DISABLE_CALLS = (1 << 1),
270 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
271 FTRACE_ENABLE_MCOUNT = (1 << 3),
272 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500273 FTRACE_START_FUNC_RET = (1 << 5),
274 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200275};
276
Steven Rostedt5072c592008-05-12 21:20:43 +0200277static int ftrace_filtered;
278
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400279static LIST_HEAD(ftrace_new_addrs);
Steven Rostedt3d083392008-05-12 21:20:42 +0200280
Steven Rostedt41c52c02008-05-22 11:46:33 -0400281static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200282
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200283struct ftrace_page {
284 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500285 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200286 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700287};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200288
289#define ENTRIES_PER_PAGE \
290 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
291
292/* estimate from running different kernels */
293#define NR_TO_INIT 10000
294
295static struct ftrace_page *ftrace_pages_start;
296static struct ftrace_page *ftrace_pages;
297
Steven Rostedt37ad5082008-05-12 21:20:48 +0200298static struct dyn_ftrace *ftrace_free_records;
299
Steven Rostedt265c8312009-02-13 12:43:56 -0500300/*
301 * This is a double for. Do not use 'break' to break out of the loop,
302 * you must use a goto.
303 */
304#define do_for_each_ftrace_rec(pg, rec) \
305 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
306 int _____i; \
307 for (_____i = 0; _____i < pg->index; _____i++) { \
308 rec = &pg->records[_____i];
309
310#define while_for_each_ftrace_rec() \
311 } \
312 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530313
314#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200315
316static int frozen_record_count;
317
Abhishek Sagarecea6562008-06-21 23:47:53 +0530318static inline void freeze_record(struct dyn_ftrace *rec)
319{
320 if (!(rec->flags & FTRACE_FL_FROZEN)) {
321 rec->flags |= FTRACE_FL_FROZEN;
322 frozen_record_count++;
323 }
324}
325
326static inline void unfreeze_record(struct dyn_ftrace *rec)
327{
328 if (rec->flags & FTRACE_FL_FROZEN) {
329 rec->flags &= ~FTRACE_FL_FROZEN;
330 frozen_record_count--;
331 }
332}
333
334static inline int record_frozen(struct dyn_ftrace *rec)
335{
336 return rec->flags & FTRACE_FL_FROZEN;
337}
338#else
339# define freeze_record(rec) ({ 0; })
340# define unfreeze_record(rec) ({ 0; })
341# define record_frozen(rec) ({ 0; })
342#endif /* CONFIG_KPROBES */
343
Ingo Molnare309b412008-05-12 21:20:51 +0200344static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200345{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200346 rec->ip = (unsigned long)ftrace_free_records;
347 ftrace_free_records = rec;
348 rec->flags |= FTRACE_FL_FREE;
349}
350
Steven Rostedtfed19392008-08-14 22:47:19 -0400351void ftrace_release(void *start, unsigned long size)
352{
353 struct dyn_ftrace *rec;
354 struct ftrace_page *pg;
355 unsigned long s = (unsigned long)start;
356 unsigned long e = s + size;
Steven Rostedtfed19392008-08-14 22:47:19 -0400357
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400358 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400359 return;
360
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400361 /* should not be called from interrupt context */
Steven Rostedtfed19392008-08-14 22:47:19 -0400362 spin_lock(&ftrace_lock);
363
Steven Rostedt265c8312009-02-13 12:43:56 -0500364 do_for_each_ftrace_rec(pg, rec) {
365 if ((rec->ip >= s) && (rec->ip < e))
366 ftrace_free_rec(rec);
367 } while_for_each_ftrace_rec();
Steven Rostedtfed19392008-08-14 22:47:19 -0400368
Steven Rostedtfed19392008-08-14 22:47:19 -0400369 spin_unlock(&ftrace_lock);
Steven Rostedtfed19392008-08-14 22:47:19 -0400370}
371
Ingo Molnare309b412008-05-12 21:20:51 +0200372static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200373{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200374 struct dyn_ftrace *rec;
375
376 /* First check for freed records */
377 if (ftrace_free_records) {
378 rec = ftrace_free_records;
379
Steven Rostedt37ad5082008-05-12 21:20:48 +0200380 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400381 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200382 ftrace_free_records = NULL;
383 return NULL;
384 }
385
386 ftrace_free_records = (void *)rec->ip;
387 memset(rec, 0, sizeof(*rec));
388 return rec;
389 }
390
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200391 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400392 if (!ftrace_pages->next) {
393 /* allocate another page */
394 ftrace_pages->next =
395 (void *)get_zeroed_page(GFP_KERNEL);
396 if (!ftrace_pages->next)
397 return NULL;
398 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200399 ftrace_pages = ftrace_pages->next;
400 }
401
402 return &ftrace_pages->records[ftrace_pages->index++];
403}
404
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400405static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200406ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200407{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400408 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200409
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800410 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400411 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200412
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400413 rec = ftrace_alloc_dyn_node(ip);
414 if (!rec)
415 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200416
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400417 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +0200418
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400419 list_add(&rec->list, &ftrace_new_addrs);
Steven Rostedt3d083392008-05-12 21:20:42 +0200420
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400421 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200422}
423
Steven Rostedt05736a42008-09-22 14:55:47 -0700424static void print_ip_ins(const char *fmt, unsigned char *p)
425{
426 int i;
427
428 printk(KERN_CONT "%s", fmt);
429
430 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
431 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
432}
433
Steven Rostedt31e88902008-11-14 16:21:19 -0800434static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800435{
436 switch (failed) {
437 case -EFAULT:
438 FTRACE_WARN_ON_ONCE(1);
439 pr_info("ftrace faulted on modifying ");
440 print_ip_sym(ip);
441 break;
442 case -EINVAL:
443 FTRACE_WARN_ON_ONCE(1);
444 pr_info("ftrace failed to modify ");
445 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800446 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800447 printk(KERN_CONT "\n");
448 break;
449 case -EPERM:
450 FTRACE_WARN_ON_ONCE(1);
451 pr_info("ftrace faulted on writing ");
452 print_ip_sym(ip);
453 break;
454 default:
455 FTRACE_WARN_ON_ONCE(1);
456 pr_info("ftrace faulted on unknown error ");
457 print_ip_sym(ip);
458 }
459}
460
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200461
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530462static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800463__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200464{
Ingo Molnare309b412008-05-12 21:20:51 +0200465 unsigned long ip, fl;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100466 unsigned long ftrace_addr;
467
Shaohua Lif0001202009-01-09 11:29:42 +0800468 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +0200469
470 ip = rec->ip;
471
Steven Rostedt982c3502008-11-15 16:31:41 -0500472 /*
473 * If this record is not to be traced and
474 * it is not enabled then do nothing.
475 *
476 * If this record is not to be traced and
Wenji Huang57794a92009-02-06 17:33:27 +0800477 * it is enabled then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -0500478 *
479 */
480 if (rec->flags & FTRACE_FL_NOTRACE) {
481 if (rec->flags & FTRACE_FL_ENABLED)
482 rec->flags &= ~FTRACE_FL_ENABLED;
483 else
Steven Rostedt5072c592008-05-12 21:20:43 +0200484 return 0;
485
Steven Rostedt982c3502008-11-15 16:31:41 -0500486 } else if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200487 /*
Steven Rostedt982c3502008-11-15 16:31:41 -0500488 * Filtering is on:
Steven Rostedt5072c592008-05-12 21:20:43 +0200489 */
Steven Rostedt982c3502008-11-15 16:31:41 -0500490
491 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
492
493 /* Record is filtered and enabled, do nothing */
494 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
495 return 0;
496
Wenji Huang57794a92009-02-06 17:33:27 +0800497 /* Record is not filtered or enabled, do nothing */
Steven Rostedt982c3502008-11-15 16:31:41 -0500498 if (!fl)
499 return 0;
500
501 /* Record is not filtered but enabled, disable it */
502 if (fl == FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +0200503 rec->flags &= ~FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -0500504 else
505 /* Otherwise record is filtered but not enabled, enable it */
Steven Rostedt5072c592008-05-12 21:20:43 +0200506 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +0200507 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -0500508 /* Disable or not filtered */
Steven Rostedt5072c592008-05-12 21:20:43 +0200509
510 if (enable) {
Steven Rostedt982c3502008-11-15 16:31:41 -0500511 /* if record is enabled, do nothing */
Steven Rostedt41c52c02008-05-22 11:46:33 -0400512 if (rec->flags & FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +0200513 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -0500514
Steven Rostedt41c52c02008-05-22 11:46:33 -0400515 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -0500516
Steven Rostedt5072c592008-05-12 21:20:43 +0200517 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -0500518
Wenji Huang57794a92009-02-06 17:33:27 +0800519 /* if record is not enabled, do nothing */
Steven Rostedt5072c592008-05-12 21:20:43 +0200520 if (!(rec->flags & FTRACE_FL_ENABLED))
521 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -0500522
Steven Rostedt5072c592008-05-12 21:20:43 +0200523 rec->flags &= ~FTRACE_FL_ENABLED;
524 }
525 }
526
Steven Rostedt982c3502008-11-15 16:31:41 -0500527 if (rec->flags & FTRACE_FL_ENABLED)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100528 return ftrace_make_call(rec, ftrace_addr);
Steven Rostedt31e88902008-11-14 16:21:19 -0800529 else
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100530 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +0200531}
532
533static void ftrace_replace_code(int enable)
534{
Steven Rostedt265c8312009-02-13 12:43:56 -0500535 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200536 struct dyn_ftrace *rec;
537 struct ftrace_page *pg;
538
Steven Rostedt265c8312009-02-13 12:43:56 -0500539 do_for_each_ftrace_rec(pg, rec) {
540 /*
541 * Skip over free records and records that have
542 * failed.
543 */
544 if (rec->flags & FTRACE_FL_FREE ||
545 rec->flags & FTRACE_FL_FAILED)
546 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +0200547
Steven Rostedt265c8312009-02-13 12:43:56 -0500548 /* ignore updates to this record's mcount site */
549 if (get_kprobe((void *)rec->ip)) {
550 freeze_record(rec);
551 continue;
552 } else {
553 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +0200554 }
Steven Rostedt265c8312009-02-13 12:43:56 -0500555
556 failed = __ftrace_replace_code(rec, enable);
557 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
558 rec->flags |= FTRACE_FL_FAILED;
559 if ((system_state == SYSTEM_BOOTING) ||
560 !core_kernel_text(rec->ip)) {
561 ftrace_free_rec(rec);
562 } else
563 ftrace_bug(failed, rec->ip);
564 }
565 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200566}
567
Ingo Molnare309b412008-05-12 21:20:51 +0200568static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800569ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200570{
571 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400572 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200573
574 ip = rec->ip;
575
Shaohua Li25aac9d2009-01-09 11:29:40 +0800576 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400577 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -0800578 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200579 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530580 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200581 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530582 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200583}
584
Ingo Molnare309b412008-05-12 21:20:51 +0200585static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200586{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200587 int *command = data;
588
Steven Rostedta3583242008-11-11 15:01:42 -0500589 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200590 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -0500591 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200592 ftrace_replace_code(0);
593
594 if (*command & FTRACE_UPDATE_TRACE_FUNC)
595 ftrace_update_ftrace_func(ftrace_trace_function);
596
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500597 if (*command & FTRACE_START_FUNC_RET)
598 ftrace_enable_ftrace_graph_caller();
599 else if (*command & FTRACE_STOP_FUNC_RET)
600 ftrace_disable_ftrace_graph_caller();
601
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200602 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200603}
604
Ingo Molnare309b412008-05-12 21:20:51 +0200605static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200606{
Rusty Russell784e2d72008-07-28 12:16:31 -0500607 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt3d083392008-05-12 21:20:42 +0200608}
609
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200610static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500611static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500612
613static void ftrace_startup_enable(int command)
614{
615 if (saved_ftrace_func != ftrace_trace_function) {
616 saved_ftrace_func = ftrace_trace_function;
617 command |= FTRACE_UPDATE_TRACE_FUNC;
618 }
619
620 if (!command || !ftrace_enabled)
621 return;
622
623 ftrace_run_update_code(command);
624}
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200625
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500626static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200627{
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200628 if (unlikely(ftrace_disabled))
629 return;
630
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400631 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500632 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -0500633 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +0200634
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500635 ftrace_startup_enable(command);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200636
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400637 mutex_unlock(&ftrace_start_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200638}
639
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500640static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200641{
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200642 if (unlikely(ftrace_disabled))
643 return;
644
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400645 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500646 ftrace_start_up--;
647 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200648 command |= FTRACE_DISABLE_CALLS;
649
650 if (saved_ftrace_func != ftrace_trace_function) {
651 saved_ftrace_func = ftrace_trace_function;
652 command |= FTRACE_UPDATE_TRACE_FUNC;
653 }
654
655 if (!command || !ftrace_enabled)
Steven Rostedt3d083392008-05-12 21:20:42 +0200656 goto out;
657
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200658 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200659 out:
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400660 mutex_unlock(&ftrace_start_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200661}
662
Ingo Molnare309b412008-05-12 21:20:51 +0200663static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200664{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200665 int command = FTRACE_ENABLE_MCOUNT;
666
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200667 if (unlikely(ftrace_disabled))
668 return;
669
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400670 mutex_lock(&ftrace_start_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200671 /* Force update next time */
672 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500673 /* ftrace_start_up is true if we want ftrace running */
674 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200675 command |= FTRACE_ENABLE_CALLS;
676
677 ftrace_run_update_code(command);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400678 mutex_unlock(&ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200679}
680
Ingo Molnare309b412008-05-12 21:20:51 +0200681static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200682{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200683 int command = FTRACE_DISABLE_MCOUNT;
684
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200685 if (unlikely(ftrace_disabled))
686 return;
687
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400688 mutex_lock(&ftrace_start_lock);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500689 /* ftrace_start_up is true if ftrace is running */
690 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200691 command |= FTRACE_DISABLE_CALLS;
692
693 ftrace_run_update_code(command);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400694 mutex_unlock(&ftrace_start_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200695}
696
Steven Rostedt3d083392008-05-12 21:20:42 +0200697static cycle_t ftrace_update_time;
698static unsigned long ftrace_update_cnt;
699unsigned long ftrace_update_tot_cnt;
700
Steven Rostedt31e88902008-11-14 16:21:19 -0800701static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +0200702{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400703 struct dyn_ftrace *p, *t;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530704 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +0200705
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200706 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200707 ftrace_update_cnt = 0;
708
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400709 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +0530710
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400711 /* If something went wrong, bail without enabling anything */
712 if (unlikely(ftrace_disabled))
713 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +0200714
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400715 list_del_init(&p->list);
Abhishek Sagar0eb96702008-06-01 21:47:30 +0530716
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400717 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -0800718 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400719 p->flags |= FTRACE_FL_CONVERTED;
720 ftrace_update_cnt++;
721 } else
722 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +0200723 }
724
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200725 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +0200726 ftrace_update_time = stop - start;
727 ftrace_update_tot_cnt += ftrace_update_cnt;
728
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200729 return 0;
730}
731
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400732static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200733{
734 struct ftrace_page *pg;
735 int cnt;
736 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200737
738 /* allocate a few pages */
739 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
740 if (!ftrace_pages_start)
741 return -1;
742
743 /*
744 * Allocate a few more pages.
745 *
746 * TODO: have some parser search vmlinux before
747 * final linking to find all calls to ftrace.
748 * Then we can:
749 * a) know how many pages to allocate.
750 * and/or
751 * b) set up the table then.
752 *
753 * The dynamic code is still necessary for
754 * modules.
755 */
756
757 pg = ftrace_pages = ftrace_pages_start;
758
Steven Rostedt68bf21a2008-08-14 15:45:08 -0400759 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400760 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +0800761 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200762
763 for (i = 0; i < cnt; i++) {
764 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
765
766 /* If we fail, we'll try later anyway */
767 if (!pg->next)
768 break;
769
770 pg = pg->next;
771 }
772
773 return 0;
774}
775
Steven Rostedt5072c592008-05-12 21:20:43 +0200776enum {
777 FTRACE_ITER_FILTER = (1 << 0),
778 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -0400779 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530780 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt0c75a3e2009-02-16 11:21:52 -0500781 FTRACE_ITER_PRINTALL = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +0200782};
783
784#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
785
786struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +0200787 struct ftrace_page *pg;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500788 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +0200789 unsigned flags;
790 unsigned char buffer[FTRACE_BUFF_MAX+1];
791 unsigned buffer_idx;
792 unsigned filtered;
793};
794
Ingo Molnare309b412008-05-12 21:20:51 +0200795static void *
Steven Rostedt5072c592008-05-12 21:20:43 +0200796t_next(struct seq_file *m, void *v, loff_t *pos)
797{
798 struct ftrace_iterator *iter = m->private;
799 struct dyn_ftrace *rec = NULL;
800
801 (*pos)++;
802
Steven Rostedt0c75a3e2009-02-16 11:21:52 -0500803 if (iter->flags & FTRACE_ITER_PRINTALL)
804 return NULL;
805
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400806 /* should not be called from interrupt context */
807 spin_lock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200808 retry:
809 if (iter->idx >= iter->pg->index) {
810 if (iter->pg->next) {
811 iter->pg = iter->pg->next;
812 iter->idx = 0;
813 goto retry;
Liming Wang50cdaf02008-11-28 12:13:21 +0800814 } else {
815 iter->idx = -1;
Steven Rostedt5072c592008-05-12 21:20:43 +0200816 }
817 } else {
818 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -0400819 if ((rec->flags & FTRACE_FL_FREE) ||
820
821 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530822 (rec->flags & FTRACE_FL_FAILED)) ||
823
824 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -0400825 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530826
Steven Rostedt0183fb12008-11-07 22:36:02 -0500827 ((iter->flags & FTRACE_ITER_FILTER) &&
828 !(rec->flags & FTRACE_FL_FILTER)) ||
829
Steven Rostedt41c52c02008-05-22 11:46:33 -0400830 ((iter->flags & FTRACE_ITER_NOTRACE) &&
831 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200832 rec = NULL;
833 goto retry;
834 }
835 }
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400836 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200837
Steven Rostedt5072c592008-05-12 21:20:43 +0200838 return rec;
839}
840
841static void *t_start(struct seq_file *m, loff_t *pos)
842{
843 struct ftrace_iterator *iter = m->private;
844 void *p = NULL;
Steven Rostedt5072c592008-05-12 21:20:43 +0200845
Steven Rostedt0c75a3e2009-02-16 11:21:52 -0500846 /*
847 * For set_ftrace_filter reading, if we have the filter
848 * off, we can short cut and just print out that all
849 * functions are enabled.
850 */
851 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
852 if (*pos > 0)
853 return NULL;
854 iter->flags |= FTRACE_ITER_PRINTALL;
855 (*pos)++;
856 return iter;
857 }
858
Liming Wang50cdaf02008-11-28 12:13:21 +0800859 if (*pos > 0) {
860 if (iter->idx < 0)
861 return p;
862 (*pos)--;
863 iter->idx--;
864 }
walimis5821e1b2008-11-15 15:19:06 +0800865
Liming Wang50cdaf02008-11-28 12:13:21 +0800866 p = t_next(m, p, pos);
Steven Rostedt5072c592008-05-12 21:20:43 +0200867
868 return p;
869}
870
871static void t_stop(struct seq_file *m, void *p)
872{
873}
874
875static int t_show(struct seq_file *m, void *v)
876{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -0500877 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +0200878 struct dyn_ftrace *rec = v;
879 char str[KSYM_SYMBOL_LEN];
880
Steven Rostedt0c75a3e2009-02-16 11:21:52 -0500881 if (iter->flags & FTRACE_ITER_PRINTALL) {
882 seq_printf(m, "#### all functions enabled ####\n");
883 return 0;
884 }
885
Steven Rostedt5072c592008-05-12 21:20:43 +0200886 if (!rec)
887 return 0;
888
889 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
890
Liming Wang50cdaf02008-11-28 12:13:21 +0800891 seq_printf(m, "%s\n", str);
Steven Rostedt5072c592008-05-12 21:20:43 +0200892
893 return 0;
894}
895
896static struct seq_operations show_ftrace_seq_ops = {
897 .start = t_start,
898 .next = t_next,
899 .stop = t_stop,
900 .show = t_show,
901};
902
Ingo Molnare309b412008-05-12 21:20:51 +0200903static int
Steven Rostedt5072c592008-05-12 21:20:43 +0200904ftrace_avail_open(struct inode *inode, struct file *file)
905{
906 struct ftrace_iterator *iter;
907 int ret;
908
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200909 if (unlikely(ftrace_disabled))
910 return -ENODEV;
911
Steven Rostedt5072c592008-05-12 21:20:43 +0200912 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
913 if (!iter)
914 return -ENOMEM;
915
916 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +0200917
918 ret = seq_open(file, &show_ftrace_seq_ops);
919 if (!ret) {
920 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200921
Steven Rostedt5072c592008-05-12 21:20:43 +0200922 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200923 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +0200924 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200925 }
Steven Rostedt5072c592008-05-12 21:20:43 +0200926
927 return ret;
928}
929
930int ftrace_avail_release(struct inode *inode, struct file *file)
931{
932 struct seq_file *m = (struct seq_file *)file->private_data;
933 struct ftrace_iterator *iter = m->private;
934
935 seq_release(inode, file);
936 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +0200937
Steven Rostedt5072c592008-05-12 21:20:43 +0200938 return 0;
939}
940
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +0530941static int
942ftrace_failures_open(struct inode *inode, struct file *file)
943{
944 int ret;
945 struct seq_file *m;
946 struct ftrace_iterator *iter;
947
948 ret = ftrace_avail_open(inode, file);
949 if (!ret) {
950 m = (struct seq_file *)file->private_data;
951 iter = (struct ftrace_iterator *)m->private;
952 iter->flags = FTRACE_ITER_FAILURES;
953 }
954
955 return ret;
956}
957
958
Steven Rostedt41c52c02008-05-22 11:46:33 -0400959static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200960{
961 struct ftrace_page *pg;
962 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400963 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +0200964
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400965 /* should not be called from interrupt context */
966 spin_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -0400967 if (enable)
968 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -0500969 do_for_each_ftrace_rec(pg, rec) {
970 if (rec->flags & FTRACE_FL_FAILED)
971 continue;
972 rec->flags &= ~type;
973 } while_for_each_ftrace_rec();
974
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400975 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200976}
977
Ingo Molnare309b412008-05-12 21:20:51 +0200978static int
Steven Rostedt41c52c02008-05-22 11:46:33 -0400979ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +0200980{
981 struct ftrace_iterator *iter;
982 int ret = 0;
983
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200984 if (unlikely(ftrace_disabled))
985 return -ENODEV;
986
Steven Rostedt5072c592008-05-12 21:20:43 +0200987 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
988 if (!iter)
989 return -ENOMEM;
990
Steven Rostedt41c52c02008-05-22 11:46:33 -0400991 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +0200992 if ((file->f_mode & FMODE_WRITE) &&
993 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -0400994 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +0200995
996 if (file->f_mode & FMODE_READ) {
997 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -0400998 iter->flags = enable ? FTRACE_ITER_FILTER :
999 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001000
1001 ret = seq_open(file, &show_ftrace_seq_ops);
1002 if (!ret) {
1003 struct seq_file *m = file->private_data;
1004 m->private = iter;
1005 } else
1006 kfree(iter);
1007 } else
1008 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001009 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001010
1011 return ret;
1012}
1013
Steven Rostedt41c52c02008-05-22 11:46:33 -04001014static int
1015ftrace_filter_open(struct inode *inode, struct file *file)
1016{
1017 return ftrace_regex_open(inode, file, 1);
1018}
1019
1020static int
1021ftrace_notrace_open(struct inode *inode, struct file *file)
1022{
1023 return ftrace_regex_open(inode, file, 0);
1024}
1025
Ingo Molnare309b412008-05-12 21:20:51 +02001026static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001027ftrace_regex_read(struct file *file, char __user *ubuf,
Steven Rostedt5072c592008-05-12 21:20:43 +02001028 size_t cnt, loff_t *ppos)
1029{
1030 if (file->f_mode & FMODE_READ)
1031 return seq_read(file, ubuf, cnt, ppos);
1032 else
1033 return -EPERM;
1034}
1035
Ingo Molnare309b412008-05-12 21:20:51 +02001036static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001037ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001038{
1039 loff_t ret;
1040
1041 if (file->f_mode & FMODE_READ)
1042 ret = seq_lseek(file, offset, origin);
1043 else
1044 file->f_pos = ret = 1;
1045
1046 return ret;
1047}
1048
1049enum {
1050 MATCH_FULL,
1051 MATCH_FRONT_ONLY,
1052 MATCH_MIDDLE_ONLY,
1053 MATCH_END_ONLY,
1054};
1055
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001056/*
1057 * (static function - no need for kernel doc)
1058 *
1059 * Pass in a buffer containing a glob and this function will
1060 * set search to point to the search part of the buffer and
1061 * return the type of search it is (see enum above).
1062 * This does modify buff.
1063 *
1064 * Returns enum type.
1065 * search returns the pointer to use for comparison.
1066 * not returns 1 if buff started with a '!'
1067 * 0 otherwise.
1068 */
1069static int
Steven Rostedt64e7c442009-02-13 17:08:48 -05001070ftrace_setup_glob(char *buff, int len, char **search, int *not)
Steven Rostedt5072c592008-05-12 21:20:43 +02001071{
Steven Rostedt5072c592008-05-12 21:20:43 +02001072 int type = MATCH_FULL;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001073 int i;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001074
1075 if (buff[0] == '!') {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001076 *not = 1;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001077 buff++;
1078 len--;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001079 } else
1080 *not = 0;
1081
1082 *search = buff;
Steven Rostedt5072c592008-05-12 21:20:43 +02001083
1084 for (i = 0; i < len; i++) {
1085 if (buff[i] == '*') {
1086 if (!i) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001087 *search = buff + 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001088 type = MATCH_END_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001089 } else {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001090 if (type == MATCH_END_ONLY)
Steven Rostedt5072c592008-05-12 21:20:43 +02001091 type = MATCH_MIDDLE_ONLY;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001092 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001093 type = MATCH_FRONT_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001094 buff[i] = 0;
1095 break;
1096 }
1097 }
1098 }
1099
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001100 return type;
1101}
1102
Steven Rostedt64e7c442009-02-13 17:08:48 -05001103static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001104{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001105 int matched = 0;
1106 char *ptr;
1107
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001108 switch (type) {
1109 case MATCH_FULL:
1110 if (strcmp(str, regex) == 0)
1111 matched = 1;
1112 break;
1113 case MATCH_FRONT_ONLY:
1114 if (strncmp(str, regex, len) == 0)
1115 matched = 1;
1116 break;
1117 case MATCH_MIDDLE_ONLY:
1118 if (strstr(str, regex))
1119 matched = 1;
1120 break;
1121 case MATCH_END_ONLY:
1122 ptr = strstr(str, regex);
1123 if (ptr && (ptr[len] == 0))
1124 matched = 1;
1125 break;
1126 }
1127
1128 return matched;
1129}
1130
Steven Rostedt64e7c442009-02-13 17:08:48 -05001131static int
1132ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1133{
1134 char str[KSYM_SYMBOL_LEN];
1135
1136 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1137 return ftrace_match(str, regex, len, type);
1138}
1139
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001140static void ftrace_match_records(char *buff, int len, int enable)
1141{
1142 char *search;
1143 struct ftrace_page *pg;
1144 struct dyn_ftrace *rec;
1145 int type;
1146 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1147 unsigned search_len;
1148 int not;
1149
1150 type = ftrace_setup_glob(buff, len, &search, &not);
1151
1152 search_len = strlen(search);
1153
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001154 /* should not be called from interrupt context */
1155 spin_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001156 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001157
Steven Rostedt265c8312009-02-13 12:43:56 -05001158 if (rec->flags & FTRACE_FL_FAILED)
1159 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001160
1161 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001162 if (not)
1163 rec->flags &= ~flag;
1164 else
1165 rec->flags |= flag;
1166 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001167 /*
1168 * Only enable filtering if we have a function that
1169 * is filtered on.
1170 */
1171 if (enable && (rec->flags & FTRACE_FL_FILTER))
1172 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001173 } while_for_each_ftrace_rec();
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001174 spin_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001175}
1176
Steven Rostedt64e7c442009-02-13 17:08:48 -05001177static int
1178ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1179 char *regex, int len, int type)
1180{
1181 char str[KSYM_SYMBOL_LEN];
1182 char *modname;
1183
1184 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1185
1186 if (!modname || strcmp(modname, mod))
1187 return 0;
1188
1189 /* blank search means to match all funcs in the mod */
1190 if (len)
1191 return ftrace_match(str, regex, len, type);
1192 else
1193 return 1;
1194}
1195
1196static void ftrace_match_module_records(char *buff, char *mod, int enable)
1197{
1198 char *search = buff;
1199 struct ftrace_page *pg;
1200 struct dyn_ftrace *rec;
1201 int type = MATCH_FULL;
1202 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1203 unsigned search_len = 0;
1204 int not = 0;
1205
1206 /* blank or '*' mean the same */
1207 if (strcmp(buff, "*") == 0)
1208 buff[0] = 0;
1209
1210 /* handle the case of 'dont filter this module' */
1211 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1212 buff[0] = 0;
1213 not = 1;
1214 }
1215
1216 if (strlen(buff)) {
1217 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1218 search_len = strlen(search);
1219 }
1220
1221 /* should not be called from interrupt context */
1222 spin_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001223 do_for_each_ftrace_rec(pg, rec) {
1224
1225 if (rec->flags & FTRACE_FL_FAILED)
1226 continue;
1227
1228 if (ftrace_match_module_record(rec, mod,
1229 search, search_len, type)) {
1230 if (not)
1231 rec->flags &= ~flag;
1232 else
1233 rec->flags |= flag;
1234 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001235 if (enable && (rec->flags & FTRACE_FL_FILTER))
1236 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001237
1238 } while_for_each_ftrace_rec();
1239 spin_unlock(&ftrace_lock);
1240}
1241
Steven Rostedtf6180772009-02-14 00:40:25 -05001242/*
1243 * We register the module command as a template to show others how
1244 * to register the a command as well.
1245 */
1246
1247static int
1248ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1249{
1250 char *mod;
1251
1252 /*
1253 * cmd == 'mod' because we only registered this func
1254 * for the 'mod' ftrace_func_command.
1255 * But if you register one func with multiple commands,
1256 * you can tell which command was used by the cmd
1257 * parameter.
1258 */
1259
1260 /* we must have a module name */
1261 if (!param)
1262 return -EINVAL;
1263
1264 mod = strsep(&param, ":");
1265 if (!strlen(mod))
1266 return -EINVAL;
1267
1268 ftrace_match_module_records(func, mod, enable);
1269 return 0;
1270}
1271
1272static struct ftrace_func_command ftrace_mod_cmd = {
1273 .name = "mod",
1274 .func = ftrace_mod_callback,
1275};
1276
1277static int __init ftrace_mod_cmd_init(void)
1278{
1279 return register_ftrace_command(&ftrace_mod_cmd);
1280}
1281device_initcall(ftrace_mod_cmd_init);
1282
1283static LIST_HEAD(ftrace_commands);
1284static DEFINE_MUTEX(ftrace_cmd_mutex);
1285
1286int register_ftrace_command(struct ftrace_func_command *cmd)
1287{
1288 struct ftrace_func_command *p;
1289 int ret = 0;
1290
1291 mutex_lock(&ftrace_cmd_mutex);
1292 list_for_each_entry(p, &ftrace_commands, list) {
1293 if (strcmp(cmd->name, p->name) == 0) {
1294 ret = -EBUSY;
1295 goto out_unlock;
1296 }
1297 }
1298 list_add(&cmd->list, &ftrace_commands);
1299 out_unlock:
1300 mutex_unlock(&ftrace_cmd_mutex);
1301
1302 return ret;
1303}
1304
1305int unregister_ftrace_command(struct ftrace_func_command *cmd)
1306{
1307 struct ftrace_func_command *p, *n;
1308 int ret = -ENODEV;
1309
1310 mutex_lock(&ftrace_cmd_mutex);
1311 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1312 if (strcmp(cmd->name, p->name) == 0) {
1313 ret = 0;
1314 list_del_init(&p->list);
1315 goto out_unlock;
1316 }
1317 }
1318 out_unlock:
1319 mutex_unlock(&ftrace_cmd_mutex);
1320
1321 return ret;
1322}
1323
Steven Rostedt64e7c442009-02-13 17:08:48 -05001324static int ftrace_process_regex(char *buff, int len, int enable)
1325{
Steven Rostedtf6180772009-02-14 00:40:25 -05001326 struct ftrace_func_command *p;
1327 char *func, *command, *next = buff;
1328 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001329
1330 func = strsep(&next, ":");
1331
1332 if (!next) {
1333 ftrace_match_records(func, len, enable);
1334 return 0;
1335 }
1336
Steven Rostedtf6180772009-02-14 00:40:25 -05001337 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05001338
1339 command = strsep(&next, ":");
1340
Steven Rostedtf6180772009-02-14 00:40:25 -05001341 mutex_lock(&ftrace_cmd_mutex);
1342 list_for_each_entry(p, &ftrace_commands, list) {
1343 if (strcmp(p->name, command) == 0) {
1344 ret = p->func(func, command, next, enable);
1345 goto out_unlock;
1346 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05001347 }
Steven Rostedtf6180772009-02-14 00:40:25 -05001348 out_unlock:
1349 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001350
Steven Rostedtf6180772009-02-14 00:40:25 -05001351 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001352}
1353
Ingo Molnare309b412008-05-12 21:20:51 +02001354static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001355ftrace_regex_write(struct file *file, const char __user *ubuf,
1356 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001357{
1358 struct ftrace_iterator *iter;
1359 char ch;
1360 size_t read = 0;
1361 ssize_t ret;
1362
1363 if (!cnt || cnt < 0)
1364 return 0;
1365
Steven Rostedt41c52c02008-05-22 11:46:33 -04001366 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001367
1368 if (file->f_mode & FMODE_READ) {
1369 struct seq_file *m = file->private_data;
1370 iter = m->private;
1371 } else
1372 iter = file->private_data;
1373
1374 if (!*ppos) {
1375 iter->flags &= ~FTRACE_ITER_CONT;
1376 iter->buffer_idx = 0;
1377 }
1378
1379 ret = get_user(ch, ubuf++);
1380 if (ret)
1381 goto out;
1382 read++;
1383 cnt--;
1384
1385 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1386 /* skip white space */
1387 while (cnt && isspace(ch)) {
1388 ret = get_user(ch, ubuf++);
1389 if (ret)
1390 goto out;
1391 read++;
1392 cnt--;
1393 }
1394
Steven Rostedt5072c592008-05-12 21:20:43 +02001395 if (isspace(ch)) {
1396 file->f_pos += read;
1397 ret = read;
1398 goto out;
1399 }
1400
1401 iter->buffer_idx = 0;
1402 }
1403
1404 while (cnt && !isspace(ch)) {
1405 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1406 iter->buffer[iter->buffer_idx++] = ch;
1407 else {
1408 ret = -EINVAL;
1409 goto out;
1410 }
1411 ret = get_user(ch, ubuf++);
1412 if (ret)
1413 goto out;
1414 read++;
1415 cnt--;
1416 }
1417
1418 if (isspace(ch)) {
1419 iter->filtered++;
1420 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001421 ret = ftrace_process_regex(iter->buffer,
1422 iter->buffer_idx, enable);
1423 if (ret)
1424 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02001425 iter->buffer_idx = 0;
1426 } else
1427 iter->flags |= FTRACE_ITER_CONT;
1428
1429
1430 file->f_pos += read;
1431
1432 ret = read;
1433 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04001434 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001435
1436 return ret;
1437}
1438
Steven Rostedt41c52c02008-05-22 11:46:33 -04001439static ssize_t
1440ftrace_filter_write(struct file *file, const char __user *ubuf,
1441 size_t cnt, loff_t *ppos)
1442{
1443 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1444}
1445
1446static ssize_t
1447ftrace_notrace_write(struct file *file, const char __user *ubuf,
1448 size_t cnt, loff_t *ppos)
1449{
1450 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1451}
1452
1453static void
1454ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1455{
1456 if (unlikely(ftrace_disabled))
1457 return;
1458
1459 mutex_lock(&ftrace_regex_lock);
1460 if (reset)
1461 ftrace_filter_reset(enable);
1462 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05001463 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001464 mutex_unlock(&ftrace_regex_lock);
1465}
1466
Steven Rostedt77a2b372008-05-12 21:20:45 +02001467/**
1468 * ftrace_set_filter - set a function to filter on in ftrace
1469 * @buf - the string that holds the function filter text.
1470 * @len - the length of the string.
1471 * @reset - non zero to reset all filters before applying this filter.
1472 *
1473 * Filters denote which functions should be enabled when tracing is enabled.
1474 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1475 */
Ingo Molnare309b412008-05-12 21:20:51 +02001476void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02001477{
Steven Rostedt41c52c02008-05-22 11:46:33 -04001478 ftrace_set_regex(buf, len, reset, 1);
1479}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001480
Steven Rostedt41c52c02008-05-22 11:46:33 -04001481/**
1482 * ftrace_set_notrace - set a function to not trace in ftrace
1483 * @buf - the string that holds the function notrace text.
1484 * @len - the length of the string.
1485 * @reset - non zero to reset all filters before applying this filter.
1486 *
1487 * Notrace Filters denote which functions should not be enabled when tracing
1488 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1489 * for tracing.
1490 */
1491void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1492{
1493 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02001494}
1495
Ingo Molnare309b412008-05-12 21:20:51 +02001496static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001497ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001498{
1499 struct seq_file *m = (struct seq_file *)file->private_data;
1500 struct ftrace_iterator *iter;
1501
Steven Rostedt41c52c02008-05-22 11:46:33 -04001502 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001503 if (file->f_mode & FMODE_READ) {
1504 iter = m->private;
1505
1506 seq_release(inode, file);
1507 } else
1508 iter = file->private_data;
1509
1510 if (iter->buffer_idx) {
1511 iter->filtered++;
1512 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt7f24b312009-02-13 14:37:33 -05001513 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001514 }
1515
1516 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001517 mutex_lock(&ftrace_start_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05001518 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02001519 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001520 mutex_unlock(&ftrace_start_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001521 mutex_unlock(&ftrace_sysctl_lock);
1522
1523 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001524 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001525 return 0;
1526}
1527
Steven Rostedt41c52c02008-05-22 11:46:33 -04001528static int
1529ftrace_filter_release(struct inode *inode, struct file *file)
1530{
1531 return ftrace_regex_release(inode, file, 1);
1532}
1533
1534static int
1535ftrace_notrace_release(struct inode *inode, struct file *file)
1536{
1537 return ftrace_regex_release(inode, file, 0);
1538}
1539
Steven Rostedt5072c592008-05-12 21:20:43 +02001540static struct file_operations ftrace_avail_fops = {
1541 .open = ftrace_avail_open,
1542 .read = seq_read,
1543 .llseek = seq_lseek,
1544 .release = ftrace_avail_release,
1545};
1546
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301547static struct file_operations ftrace_failures_fops = {
1548 .open = ftrace_failures_open,
1549 .read = seq_read,
1550 .llseek = seq_lseek,
1551 .release = ftrace_avail_release,
1552};
1553
Steven Rostedt5072c592008-05-12 21:20:43 +02001554static struct file_operations ftrace_filter_fops = {
1555 .open = ftrace_filter_open,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001556 .read = ftrace_regex_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02001557 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04001558 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02001559 .release = ftrace_filter_release,
1560};
1561
Steven Rostedt41c52c02008-05-22 11:46:33 -04001562static struct file_operations ftrace_notrace_fops = {
1563 .open = ftrace_notrace_open,
1564 .read = ftrace_regex_read,
1565 .write = ftrace_notrace_write,
1566 .llseek = ftrace_regex_lseek,
1567 .release = ftrace_notrace_release,
1568};
1569
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001570#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1571
1572static DEFINE_MUTEX(graph_lock);
1573
1574int ftrace_graph_count;
1575unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1576
1577static void *
1578g_next(struct seq_file *m, void *v, loff_t *pos)
1579{
1580 unsigned long *array = m->private;
1581 int index = *pos;
1582
1583 (*pos)++;
1584
1585 if (index >= ftrace_graph_count)
1586 return NULL;
1587
1588 return &array[index];
1589}
1590
1591static void *g_start(struct seq_file *m, loff_t *pos)
1592{
1593 void *p = NULL;
1594
1595 mutex_lock(&graph_lock);
1596
1597 p = g_next(m, p, pos);
1598
1599 return p;
1600}
1601
1602static void g_stop(struct seq_file *m, void *p)
1603{
1604 mutex_unlock(&graph_lock);
1605}
1606
1607static int g_show(struct seq_file *m, void *v)
1608{
1609 unsigned long *ptr = v;
1610 char str[KSYM_SYMBOL_LEN];
1611
1612 if (!ptr)
1613 return 0;
1614
1615 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1616
1617 seq_printf(m, "%s\n", str);
1618
1619 return 0;
1620}
1621
1622static struct seq_operations ftrace_graph_seq_ops = {
1623 .start = g_start,
1624 .next = g_next,
1625 .stop = g_stop,
1626 .show = g_show,
1627};
1628
1629static int
1630ftrace_graph_open(struct inode *inode, struct file *file)
1631{
1632 int ret = 0;
1633
1634 if (unlikely(ftrace_disabled))
1635 return -ENODEV;
1636
1637 mutex_lock(&graph_lock);
1638 if ((file->f_mode & FMODE_WRITE) &&
1639 !(file->f_flags & O_APPEND)) {
1640 ftrace_graph_count = 0;
1641 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1642 }
1643
1644 if (file->f_mode & FMODE_READ) {
1645 ret = seq_open(file, &ftrace_graph_seq_ops);
1646 if (!ret) {
1647 struct seq_file *m = file->private_data;
1648 m->private = ftrace_graph_funcs;
1649 }
1650 } else
1651 file->private_data = ftrace_graph_funcs;
1652 mutex_unlock(&graph_lock);
1653
1654 return ret;
1655}
1656
1657static ssize_t
1658ftrace_graph_read(struct file *file, char __user *ubuf,
1659 size_t cnt, loff_t *ppos)
1660{
1661 if (file->f_mode & FMODE_READ)
1662 return seq_read(file, ubuf, cnt, ppos);
1663 else
1664 return -EPERM;
1665}
1666
1667static int
1668ftrace_set_func(unsigned long *array, int idx, char *buffer)
1669{
1670 char str[KSYM_SYMBOL_LEN];
1671 struct dyn_ftrace *rec;
1672 struct ftrace_page *pg;
1673 int found = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001674 int j;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001675
1676 if (ftrace_disabled)
1677 return -ENODEV;
1678
1679 /* should not be called from interrupt context */
1680 spin_lock(&ftrace_lock);
1681
Steven Rostedt265c8312009-02-13 12:43:56 -05001682 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001683
Steven Rostedt265c8312009-02-13 12:43:56 -05001684 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
1685 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001686
Steven Rostedt265c8312009-02-13 12:43:56 -05001687 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1688 if (strcmp(str, buffer) == 0) {
1689 /* Return 1 if we add it to the array */
1690 found = 1;
1691 for (j = 0; j < idx; j++)
1692 if (array[j] == rec->ip) {
1693 found = 0;
1694 break;
1695 }
1696 if (found)
1697 array[idx] = rec->ip;
1698 goto out;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001699 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001700 } while_for_each_ftrace_rec();
1701 out:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001702 spin_unlock(&ftrace_lock);
1703
1704 return found ? 0 : -EINVAL;
1705}
1706
1707static ssize_t
1708ftrace_graph_write(struct file *file, const char __user *ubuf,
1709 size_t cnt, loff_t *ppos)
1710{
1711 unsigned char buffer[FTRACE_BUFF_MAX+1];
1712 unsigned long *array;
1713 size_t read = 0;
1714 ssize_t ret;
1715 int index = 0;
1716 char ch;
1717
1718 if (!cnt || cnt < 0)
1719 return 0;
1720
1721 mutex_lock(&graph_lock);
1722
1723 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
1724 ret = -EBUSY;
1725 goto out;
1726 }
1727
1728 if (file->f_mode & FMODE_READ) {
1729 struct seq_file *m = file->private_data;
1730 array = m->private;
1731 } else
1732 array = file->private_data;
1733
1734 ret = get_user(ch, ubuf++);
1735 if (ret)
1736 goto out;
1737 read++;
1738 cnt--;
1739
1740 /* skip white space */
1741 while (cnt && isspace(ch)) {
1742 ret = get_user(ch, ubuf++);
1743 if (ret)
1744 goto out;
1745 read++;
1746 cnt--;
1747 }
1748
1749 if (isspace(ch)) {
1750 *ppos += read;
1751 ret = read;
1752 goto out;
1753 }
1754
1755 while (cnt && !isspace(ch)) {
1756 if (index < FTRACE_BUFF_MAX)
1757 buffer[index++] = ch;
1758 else {
1759 ret = -EINVAL;
1760 goto out;
1761 }
1762 ret = get_user(ch, ubuf++);
1763 if (ret)
1764 goto out;
1765 read++;
1766 cnt--;
1767 }
1768 buffer[index] = 0;
1769
1770 /* we allow only one at a time */
1771 ret = ftrace_set_func(array, ftrace_graph_count, buffer);
1772 if (ret)
1773 goto out;
1774
1775 ftrace_graph_count++;
1776
1777 file->f_pos += read;
1778
1779 ret = read;
1780 out:
1781 mutex_unlock(&graph_lock);
1782
1783 return ret;
1784}
1785
1786static const struct file_operations ftrace_graph_fops = {
1787 .open = ftrace_graph_open,
1788 .read = ftrace_graph_read,
1789 .write = ftrace_graph_write,
1790};
1791#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1792
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001793static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02001794{
Steven Rostedt5072c592008-05-12 21:20:43 +02001795 struct dentry *entry;
1796
Steven Rostedt5072c592008-05-12 21:20:43 +02001797 entry = debugfs_create_file("available_filter_functions", 0444,
1798 d_tracer, NULL, &ftrace_avail_fops);
1799 if (!entry)
1800 pr_warning("Could not create debugfs "
1801 "'available_filter_functions' entry\n");
1802
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301803 entry = debugfs_create_file("failures", 0444,
1804 d_tracer, NULL, &ftrace_failures_fops);
1805 if (!entry)
1806 pr_warning("Could not create debugfs 'failures' entry\n");
1807
Steven Rostedt5072c592008-05-12 21:20:43 +02001808 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1809 NULL, &ftrace_filter_fops);
1810 if (!entry)
1811 pr_warning("Could not create debugfs "
1812 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04001813
1814 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1815 NULL, &ftrace_notrace_fops);
1816 if (!entry)
1817 pr_warning("Could not create debugfs "
1818 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04001819
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001820#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1821 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
1822 NULL,
1823 &ftrace_graph_fops);
1824 if (!entry)
1825 pr_warning("Could not create debugfs "
1826 "'set_graph_function' entry\n");
1827#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1828
Steven Rostedt5072c592008-05-12 21:20:43 +02001829 return 0;
1830}
1831
Steven Rostedt31e88902008-11-14 16:21:19 -08001832static int ftrace_convert_nops(struct module *mod,
1833 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001834 unsigned long *end)
1835{
1836 unsigned long *p;
1837 unsigned long addr;
1838 unsigned long flags;
1839
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001840 mutex_lock(&ftrace_start_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001841 p = start;
1842 while (p < end) {
1843 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08001844 /*
1845 * Some architecture linkers will pad between
1846 * the different mcount_loc sections of different
1847 * object files to satisfy alignments.
1848 * Skip any NULL pointers.
1849 */
1850 if (!addr)
1851 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001852 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001853 }
1854
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001855 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001856 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08001857 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001858 local_irq_restore(flags);
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001859 mutex_unlock(&ftrace_start_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001860
1861 return 0;
1862}
1863
Steven Rostedt31e88902008-11-14 16:21:19 -08001864void ftrace_init_module(struct module *mod,
1865 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04001866{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04001867 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04001868 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08001869 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04001870}
1871
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001872extern unsigned long __start_mcount_loc[];
1873extern unsigned long __stop_mcount_loc[];
1874
1875void __init ftrace_init(void)
1876{
1877 unsigned long count, addr, flags;
1878 int ret;
1879
1880 /* Keep the ftrace pointer to the stub */
1881 addr = (unsigned long)ftrace_stub;
1882
1883 local_irq_save(flags);
1884 ftrace_dyn_arch_init(&addr);
1885 local_irq_restore(flags);
1886
1887 /* ftrace_dyn_arch_init places the return code in addr */
1888 if (addr)
1889 goto failed;
1890
1891 count = __stop_mcount_loc - __start_mcount_loc;
1892
1893 ret = ftrace_dyn_table_alloc(count);
1894 if (ret)
1895 goto failed;
1896
1897 last_ftrace_enabled = ftrace_enabled = 1;
1898
Steven Rostedt31e88902008-11-14 16:21:19 -08001899 ret = ftrace_convert_nops(NULL,
1900 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001901 __stop_mcount_loc);
1902
1903 return;
1904 failed:
1905 ftrace_disabled = 1;
1906}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001907
Steven Rostedt3d083392008-05-12 21:20:42 +02001908#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01001909
1910static int __init ftrace_nodyn_init(void)
1911{
1912 ftrace_enabled = 1;
1913 return 0;
1914}
1915device_initcall(ftrace_nodyn_init);
1916
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001917static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
1918static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001919/* Keep as macros so we do not need to define the commands */
1920# define ftrace_startup(command) do { } while (0)
1921# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001922# define ftrace_startup_sysctl() do { } while (0)
1923# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02001924#endif /* CONFIG_DYNAMIC_FTRACE */
1925
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001926static ssize_t
1927ftrace_pid_read(struct file *file, char __user *ubuf,
1928 size_t cnt, loff_t *ppos)
1929{
1930 char buf[64];
1931 int r;
1932
Steven Rostedte32d8952008-12-04 00:26:41 -05001933 if (ftrace_pid_trace == ftrace_swapper_pid)
1934 r = sprintf(buf, "swapper tasks\n");
1935 else if (ftrace_pid_trace)
Steven Rostedt978f3a42008-12-04 00:26:40 -05001936 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001937 else
1938 r = sprintf(buf, "no pid\n");
1939
1940 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1941}
1942
Steven Rostedte32d8952008-12-04 00:26:41 -05001943static void clear_ftrace_swapper(void)
1944{
1945 struct task_struct *p;
1946 int cpu;
1947
1948 get_online_cpus();
1949 for_each_online_cpu(cpu) {
1950 p = idle_task(cpu);
1951 clear_tsk_trace_trace(p);
1952 }
1953 put_online_cpus();
1954}
1955
1956static void set_ftrace_swapper(void)
1957{
1958 struct task_struct *p;
1959 int cpu;
1960
1961 get_online_cpus();
1962 for_each_online_cpu(cpu) {
1963 p = idle_task(cpu);
1964 set_tsk_trace_trace(p);
1965 }
1966 put_online_cpus();
1967}
1968
1969static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05001970{
1971 struct task_struct *p;
1972
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01001973 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05001974 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05001975 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05001976 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01001977 rcu_read_unlock();
1978
Steven Rostedte32d8952008-12-04 00:26:41 -05001979 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05001980}
1981
Steven Rostedte32d8952008-12-04 00:26:41 -05001982static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05001983{
1984 struct task_struct *p;
1985
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01001986 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05001987 do_each_pid_task(pid, PIDTYPE_PID, p) {
1988 set_tsk_trace_trace(p);
1989 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01001990 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05001991}
1992
Steven Rostedte32d8952008-12-04 00:26:41 -05001993static void clear_ftrace_pid_task(struct pid **pid)
1994{
1995 if (*pid == ftrace_swapper_pid)
1996 clear_ftrace_swapper();
1997 else
1998 clear_ftrace_pid(*pid);
1999
2000 *pid = NULL;
2001}
2002
2003static void set_ftrace_pid_task(struct pid *pid)
2004{
2005 if (pid == ftrace_swapper_pid)
2006 set_ftrace_swapper();
2007 else
2008 set_ftrace_pid(pid);
2009}
2010
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002011static ssize_t
2012ftrace_pid_write(struct file *filp, const char __user *ubuf,
2013 size_t cnt, loff_t *ppos)
2014{
Steven Rostedt978f3a42008-12-04 00:26:40 -05002015 struct pid *pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002016 char buf[64];
2017 long val;
2018 int ret;
2019
2020 if (cnt >= sizeof(buf))
2021 return -EINVAL;
2022
2023 if (copy_from_user(&buf, ubuf, cnt))
2024 return -EFAULT;
2025
2026 buf[cnt] = 0;
2027
2028 ret = strict_strtol(buf, 10, &val);
2029 if (ret < 0)
2030 return ret;
2031
2032 mutex_lock(&ftrace_start_lock);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002033 if (val < 0) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002034 /* disable pid tracing */
Steven Rostedt978f3a42008-12-04 00:26:40 -05002035 if (!ftrace_pid_trace)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002036 goto out;
Steven Rostedt978f3a42008-12-04 00:26:40 -05002037
2038 clear_ftrace_pid_task(&ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002039
2040 } else {
Steven Rostedte32d8952008-12-04 00:26:41 -05002041 /* swapper task is special */
2042 if (!val) {
2043 pid = ftrace_swapper_pid;
2044 if (pid == ftrace_pid_trace)
2045 goto out;
2046 } else {
2047 pid = find_get_pid(val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002048
Steven Rostedte32d8952008-12-04 00:26:41 -05002049 if (pid == ftrace_pid_trace) {
2050 put_pid(pid);
2051 goto out;
2052 }
Steven Rostedt978f3a42008-12-04 00:26:40 -05002053 }
2054
2055 if (ftrace_pid_trace)
2056 clear_ftrace_pid_task(&ftrace_pid_trace);
2057
2058 if (!pid)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002059 goto out;
2060
Steven Rostedt978f3a42008-12-04 00:26:40 -05002061 ftrace_pid_trace = pid;
Steven Rostedt0ef8cde2008-12-03 15:36:58 -05002062
Steven Rostedt978f3a42008-12-04 00:26:40 -05002063 set_ftrace_pid_task(ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002064 }
2065
2066 /* update the function call */
2067 ftrace_update_pid_func();
2068 ftrace_startup_enable(0);
2069
2070 out:
2071 mutex_unlock(&ftrace_start_lock);
2072
2073 return cnt;
2074}
2075
2076static struct file_operations ftrace_pid_fops = {
2077 .read = ftrace_pid_read,
2078 .write = ftrace_pid_write,
2079};
2080
2081static __init int ftrace_init_debugfs(void)
2082{
2083 struct dentry *d_tracer;
2084 struct dentry *entry;
2085
2086 d_tracer = tracing_init_dentry();
2087 if (!d_tracer)
2088 return 0;
2089
2090 ftrace_init_dyn_debugfs(d_tracer);
2091
2092 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2093 NULL, &ftrace_pid_fops);
2094 if (!entry)
2095 pr_warning("Could not create debugfs "
2096 "'set_ftrace_pid' entry\n");
2097 return 0;
2098}
2099
2100fs_initcall(ftrace_init_debugfs);
2101
Steven Rostedt3d083392008-05-12 21:20:42 +02002102/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002103 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002104 *
2105 * This function should be used by panic code. It stops ftrace
2106 * but in a not so nice way. If you need to simply kill ftrace
2107 * from a non-atomic section, use ftrace_kill.
2108 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002109void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002110{
2111 ftrace_disabled = 1;
2112 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002113 clear_ftrace_function();
2114}
2115
2116/**
Steven Rostedt3d083392008-05-12 21:20:42 +02002117 * register_ftrace_function - register a function for profiling
2118 * @ops - ops structure that holds the function for profiling.
2119 *
2120 * Register a function to be called by all functions in the
2121 * kernel.
2122 *
2123 * Note: @ops->func and all the functions it calls must be labeled
2124 * with "notrace", otherwise it will go into a
2125 * recursive loop.
2126 */
2127int register_ftrace_function(struct ftrace_ops *ops)
2128{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002129 int ret;
2130
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002131 if (unlikely(ftrace_disabled))
2132 return -1;
2133
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002134 mutex_lock(&ftrace_sysctl_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002135
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002136 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002137 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002138
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002139 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002140 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02002141}
2142
2143/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01002144 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02002145 * @ops - ops structure that holds the function to unregister
2146 *
2147 * Unregister a function that was added to be called by ftrace profiling.
2148 */
2149int unregister_ftrace_function(struct ftrace_ops *ops)
2150{
2151 int ret;
2152
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002153 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02002154 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002155 ftrace_shutdown(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002156 mutex_unlock(&ftrace_sysctl_lock);
2157
2158 return ret;
2159}
2160
Ingo Molnare309b412008-05-12 21:20:51 +02002161int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002162ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02002163 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002164 loff_t *ppos)
2165{
2166 int ret;
2167
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002168 if (unlikely(ftrace_disabled))
2169 return -ENODEV;
2170
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002171 mutex_lock(&ftrace_sysctl_lock);
2172
Steven Rostedt5072c592008-05-12 21:20:43 +02002173 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002174
2175 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2176 goto out;
2177
2178 last_ftrace_enabled = ftrace_enabled;
2179
2180 if (ftrace_enabled) {
2181
2182 ftrace_startup_sysctl();
2183
2184 /* we are starting ftrace again */
2185 if (ftrace_list != &ftrace_list_end) {
2186 if (ftrace_list->next == &ftrace_list_end)
2187 ftrace_trace_function = ftrace_list->func;
2188 else
2189 ftrace_trace_function = ftrace_list_func;
2190 }
2191
2192 } else {
2193 /* stopping ftrace calls (just send to ftrace_stub) */
2194 ftrace_trace_function = ftrace_stub;
2195
2196 ftrace_shutdown_sysctl();
2197 }
2198
2199 out:
2200 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02002201 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002202}
Ingo Molnarf17845e2008-10-24 12:47:10 +02002203
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002204#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002205
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002206static atomic_t ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08002207static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002208
Steven Rostedte49dc192008-12-02 23:50:05 -05002209int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2210{
2211 return 0;
2212}
2213
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002214/* The callbacks that hook a function */
2215trace_func_graph_ret_t ftrace_graph_return =
2216 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05002217trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002218
2219/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2220static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2221{
2222 int i;
2223 int ret = 0;
2224 unsigned long flags;
2225 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2226 struct task_struct *g, *t;
2227
2228 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2229 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2230 * sizeof(struct ftrace_ret_stack),
2231 GFP_KERNEL);
2232 if (!ret_stack_list[i]) {
2233 start = 0;
2234 end = i;
2235 ret = -ENOMEM;
2236 goto free;
2237 }
2238 }
2239
2240 read_lock_irqsave(&tasklist_lock, flags);
2241 do_each_thread(g, t) {
2242 if (start == end) {
2243 ret = -EAGAIN;
2244 goto unlock;
2245 }
2246
2247 if (t->ret_stack == NULL) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002248 t->curr_ret_stack = -1;
Frederic Weisbecker48d68b22008-12-02 00:20:39 +01002249 /* Make sure IRQs see the -1 first: */
2250 barrier();
2251 t->ret_stack = ret_stack_list[start++];
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01002252 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002253 atomic_set(&t->trace_overrun, 0);
2254 }
2255 } while_each_thread(g, t);
2256
2257unlock:
2258 read_unlock_irqrestore(&tasklist_lock, flags);
2259free:
2260 for (i = start; i < end; i++)
2261 kfree(ret_stack_list[i]);
2262 return ret;
2263}
2264
2265/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002266static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002267{
2268 struct ftrace_ret_stack **ret_stack_list;
2269 int ret;
2270
2271 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2272 sizeof(struct ftrace_ret_stack *),
2273 GFP_KERNEL);
2274
2275 if (!ret_stack_list)
2276 return -ENOMEM;
2277
2278 do {
2279 ret = alloc_retstack_tasklist(ret_stack_list);
2280 } while (ret == -EAGAIN);
2281
2282 kfree(ret_stack_list);
2283 return ret;
2284}
2285
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08002286/*
2287 * Hibernation protection.
2288 * The state of the current task is too much unstable during
2289 * suspend/restore to disk. We want to protect against that.
2290 */
2291static int
2292ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2293 void *unused)
2294{
2295 switch (state) {
2296 case PM_HIBERNATION_PREPARE:
2297 pause_graph_tracing();
2298 break;
2299
2300 case PM_POST_HIBERNATION:
2301 unpause_graph_tracing();
2302 break;
2303 }
2304 return NOTIFY_DONE;
2305}
2306
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002307int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2308 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002309{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002310 int ret = 0;
2311
2312 mutex_lock(&ftrace_sysctl_lock);
2313
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08002314 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2315 register_pm_notifier(&ftrace_suspend_notifier);
2316
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002317 atomic_inc(&ftrace_graph_active);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002318 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002319 if (ret) {
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002320 atomic_dec(&ftrace_graph_active);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002321 goto out;
2322 }
Steven Rostedte53a6312008-11-26 00:16:25 -05002323
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002324 ftrace_graph_return = retfunc;
2325 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05002326
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002327 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002328
2329out:
2330 mutex_unlock(&ftrace_sysctl_lock);
2331 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002332}
2333
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002334void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002335{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002336 mutex_lock(&ftrace_sysctl_lock);
2337
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002338 atomic_dec(&ftrace_graph_active);
2339 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05002340 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002341 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08002342 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002343
2344 mutex_unlock(&ftrace_sysctl_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002345}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002346
2347/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002348void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002349{
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01002350 if (atomic_read(&ftrace_graph_active)) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002351 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2352 * sizeof(struct ftrace_ret_stack),
2353 GFP_KERNEL);
2354 if (!t->ret_stack)
2355 return;
2356 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01002357 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002358 atomic_set(&t->trace_overrun, 0);
2359 } else
2360 t->ret_stack = NULL;
2361}
2362
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002363void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002364{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01002365 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2366
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002367 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01002368 /* NULL must become visible to IRQs before we free it: */
2369 barrier();
2370
2371 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01002372}
Steven Rostedt14a866c2008-12-02 23:50:02 -05002373
2374void ftrace_graph_stop(void)
2375{
2376 ftrace_stop();
2377}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01002378#endif
2379