blob: 3bcb340d6f02701c57ce77f4d1ac2fffdbb001a4 [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>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010025#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020026#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.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>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080031#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020032
Steven Rostedtad8d75f2009-04-14 19:39:12 -040033#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040034
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035#include <asm/ftrace.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040036#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053037
Steven Rostedt0706f1c2009-03-23 23:12:58 -040038#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040039#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020040
Steven Rostedt69128962008-10-23 09:33:03 -040041#define FTRACE_WARN_ON(cond) \
42 do { \
43 if (WARN_ON(cond)) \
44 ftrace_kill(); \
45 } while (0)
46
47#define FTRACE_WARN_ON_ONCE(cond) \
48 do { \
49 if (WARN_ON_ONCE(cond)) \
50 ftrace_kill(); \
51 } while (0)
52
Steven Rostedt8fc0c702009-02-16 15:28:00 -050053/* hash bits for specific function selection */
54#define FTRACE_HASH_BITS 7
55#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56
Steven Rostedt4eebcc82008-05-12 21:20:48 +020057/* ftrace_enabled is a method to turn ftrace on or off */
58int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020059static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020060
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050061/* Quick disabling of function tracer. */
62int function_trace_stop;
63
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040064/* List for set_ftrace_pid's pids. */
65LIST_HEAD(ftrace_pids);
66struct ftrace_pid {
67 struct list_head list;
68 struct pid *pid;
69};
70
Steven Rostedt4eebcc82008-05-12 21:20:48 +020071/*
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
74 */
75static int ftrace_disabled __read_mostly;
76
Steven Rostedt52baf112009-02-14 01:15:39 -050077static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020078
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020079static struct ftrace_ops ftrace_list_end __read_mostly =
80{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040081 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020082};
83
84static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
85ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050086ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050087ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020088
Paul E. McKenney3f379b02010-03-05 15:03:25 -080089/*
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
95 *
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
97 */
Ingo Molnarf2252932008-05-22 10:37:48 +020098static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020099{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800100 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200101
102 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200103 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800104 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105 };
106}
107
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500108static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
109{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500110 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500111 return;
112
113 ftrace_pid_function(ip, parent_ip);
114}
115
116static void set_ftrace_pid_function(ftrace_func_t func)
117{
118 /* do not set ftrace_pid_function to itself! */
119 if (func != ftrace_pid_func)
120 ftrace_pid_function = func;
121}
122
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200124 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200129void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130{
Steven Rostedt3d083392008-05-12 21:20:42 +0200131 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500132 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500133 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200134}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500136#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
137/*
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
140 */
141static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
142{
143 if (function_trace_stop)
144 return;
145
146 __ftrace_trace_function(ip, parent_ip);
147}
148#endif
149
Ingo Molnare309b412008-05-12 21:20:51 +0200150static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200151{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152 ops->next = ftrace_list;
153 /*
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
158 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800159 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200160
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200161 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500162 ftrace_func_t func;
163
164 if (ops->next == &ftrace_list_end)
165 func = ops->func;
166 else
167 func = ftrace_list_func;
168
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400169 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500170 set_ftrace_pid_function(func);
171 func = ftrace_pid_func;
172 }
173
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200174 /*
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
177 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500178#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500180#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500181 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500182 ftrace_trace_function = ftrace_test_stop_func;
183#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200184 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200185
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186 return 0;
187}
188
Ingo Molnare309b412008-05-12 21:20:51 +0200189static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192
193 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196 */
197 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
198 ftrace_trace_function = ftrace_stub;
199 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500200 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 }
202
203 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
204 if (*p == ops)
205 break;
206
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500207 if (*p != ops)
208 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209
210 *p = (*p)->next;
211
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200212 if (ftrace_enabled) {
213 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 if (ftrace_list->next == &ftrace_list_end) {
215 ftrace_func_t func = ftrace_list->func;
216
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400217 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
220 }
221#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function = func;
223#else
224 __ftrace_trace_function = func;
225#endif
226 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200227 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200228
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500229 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200230}
231
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232static void ftrace_update_pid_func(void)
233{
234 ftrace_func_t func;
235
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900237 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500238
Matt Fleming33974092009-09-28 16:43:01 +0100239#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500240 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100241#else
242 func = __ftrace_trace_function;
243#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500244
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400245 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500246 set_ftrace_pid_function(func);
247 func = ftrace_pid_func;
248 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800249 if (func == ftrace_pid_func)
250 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500251 }
252
253#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function = func;
255#else
256 __ftrace_trace_function = func;
257#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500258}
259
Steven Rostedt493762f2009-03-23 17:12:36 -0400260#ifdef CONFIG_FUNCTION_PROFILER
261struct ftrace_profile {
262 struct hlist_node node;
263 unsigned long ip;
264 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400265#ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400267 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400268#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400269};
270
271struct ftrace_profile_page {
272 struct ftrace_profile_page *next;
273 unsigned long index;
274 struct ftrace_profile records[];
275};
276
Steven Rostedtcafb1682009-03-24 20:50:39 -0400277struct ftrace_profile_stat {
278 atomic_t disabled;
279 struct hlist_head *hash;
280 struct ftrace_profile_page *pages;
281 struct ftrace_profile_page *start;
282 struct tracer_stat stat;
283};
284
Steven Rostedt493762f2009-03-23 17:12:36 -0400285#define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
287
288#define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
290
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400291static int ftrace_profile_bits __read_mostly;
292static int ftrace_profile_enabled __read_mostly;
293
294/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400295static DEFINE_MUTEX(ftrace_profile_lock);
296
Steven Rostedtcafb1682009-03-24 20:50:39 -0400297static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400298
299#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
300
Steven Rostedt493762f2009-03-23 17:12:36 -0400301static void *
302function_stat_next(void *v, int idx)
303{
304 struct ftrace_profile *rec = v;
305 struct ftrace_profile_page *pg;
306
307 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
308
309 again:
Li Zefan0296e422009-06-26 11:15:37 +0800310 if (idx != 0)
311 rec++;
312
Steven Rostedt493762f2009-03-23 17:12:36 -0400313 if ((void *)rec >= (void *)&pg->records[pg->index]) {
314 pg = pg->next;
315 if (!pg)
316 return NULL;
317 rec = &pg->records[0];
318 if (!rec->counter)
319 goto again;
320 }
321
322 return rec;
323}
324
325static void *function_stat_start(struct tracer_stat *trace)
326{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400327 struct ftrace_profile_stat *stat =
328 container_of(trace, struct ftrace_profile_stat, stat);
329
330 if (!stat || !stat->start)
331 return NULL;
332
333 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400334}
335
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400336#ifdef CONFIG_FUNCTION_GRAPH_TRACER
337/* function graph compares on total time */
338static int function_stat_cmp(void *p1, void *p2)
339{
340 struct ftrace_profile *a = p1;
341 struct ftrace_profile *b = p2;
342
343 if (a->time < b->time)
344 return -1;
345 if (a->time > b->time)
346 return 1;
347 else
348 return 0;
349}
350#else
351/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400352static int function_stat_cmp(void *p1, void *p2)
353{
354 struct ftrace_profile *a = p1;
355 struct ftrace_profile *b = p2;
356
357 if (a->counter < b->counter)
358 return -1;
359 if (a->counter > b->counter)
360 return 1;
361 else
362 return 0;
363}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400364#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400365
366static int function_stat_headers(struct seq_file *m)
367{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400369 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400370 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400371 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400372 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400373#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400374 seq_printf(m, " Function Hit\n"
375 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400376#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400377 return 0;
378}
379
380static int function_stat_show(struct seq_file *m, void *v)
381{
382 struct ftrace_profile *rec = v;
383 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400384#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400385 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400386 static struct trace_seq s;
387 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400388 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400389#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400390
391 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400392 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400393
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400394#ifdef CONFIG_FUNCTION_GRAPH_TRACER
395 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400396 avg = rec->time;
397 do_div(avg, rec->counter);
398
Chase Douglase330b3b2010-04-26 14:02:05 -0400399 /* Sample standard deviation (s^2) */
400 if (rec->counter <= 1)
401 stddev = 0;
402 else {
403 stddev = rec->time_squared - rec->counter * avg * avg;
404 /*
405 * Divide only 1000 for ns^2 -> us^2 conversion.
406 * trace_print_graph_duration will divide 1000 again.
407 */
408 do_div(stddev, (rec->counter - 1) * 1000);
409 }
410
Steven Rostedt34886c82009-03-25 21:00:47 -0400411 mutex_lock(&mutex);
412 trace_seq_init(&s);
413 trace_print_graph_duration(rec->time, &s);
414 trace_seq_puts(&s, " ");
415 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400416 trace_seq_puts(&s, " ");
417 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400418 trace_print_seq(m, &s);
419 mutex_unlock(&mutex);
420#endif
421 seq_putc(m, '\n');
422
Steven Rostedt493762f2009-03-23 17:12:36 -0400423 return 0;
424}
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400427{
428 struct ftrace_profile_page *pg;
429
Steven Rostedtcafb1682009-03-24 20:50:39 -0400430 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400431
432 while (pg) {
433 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
434 pg->index = 0;
435 pg = pg->next;
436 }
437
Steven Rostedtcafb1682009-03-24 20:50:39 -0400438 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400439 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
440}
441
Steven Rostedtcafb1682009-03-24 20:50:39 -0400442int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400443{
444 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400445 int functions;
446 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400447 int i;
448
449 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400450 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400451 return 0;
452
Steven Rostedtcafb1682009-03-24 20:50:39 -0400453 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
454 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400455 return -ENOMEM;
456
Steven Rostedt318e0a72009-03-25 20:06:34 -0400457#ifdef CONFIG_DYNAMIC_FTRACE
458 functions = ftrace_update_tot_cnt;
459#else
460 /*
461 * We do not know the number of functions that exist because
462 * dynamic tracing is what counts them. With past experience
463 * we have around 20K functions. That should be more than enough.
464 * It is highly unlikely we will execute every function in
465 * the kernel.
466 */
467 functions = 20000;
468#endif
469
Steven Rostedtcafb1682009-03-24 20:50:39 -0400470 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400471
Steven Rostedt318e0a72009-03-25 20:06:34 -0400472 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
473
474 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400475 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400476 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400477 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400478 pg = pg->next;
479 }
480
481 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400482
483 out_free:
484 pg = stat->start;
485 while (pg) {
486 unsigned long tmp = (unsigned long)pg;
487
488 pg = pg->next;
489 free_page(tmp);
490 }
491
492 free_page((unsigned long)stat->pages);
493 stat->pages = NULL;
494 stat->start = NULL;
495
496 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400497}
498
Steven Rostedtcafb1682009-03-24 20:50:39 -0400499static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400500{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400501 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400502 int size;
503
Steven Rostedtcafb1682009-03-24 20:50:39 -0400504 stat = &per_cpu(ftrace_profile_stats, cpu);
505
506 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400507 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400509 return 0;
510 }
511
512 /*
513 * We are profiling all functions, but usually only a few thousand
514 * functions are hit. We'll make a hash of 1024 items.
515 */
516 size = FTRACE_PROFILE_HASH_SIZE;
517
Steven Rostedtcafb1682009-03-24 20:50:39 -0400518 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400519
Steven Rostedtcafb1682009-03-24 20:50:39 -0400520 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400521 return -ENOMEM;
522
Steven Rostedtcafb1682009-03-24 20:50:39 -0400523 if (!ftrace_profile_bits) {
524 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Steven Rostedtcafb1682009-03-24 20:50:39 -0400526 for (; size; size >>= 1)
527 ftrace_profile_bits++;
528 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400529
Steven Rostedt318e0a72009-03-25 20:06:34 -0400530 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400531 if (ftrace_profile_pages_init(stat) < 0) {
532 kfree(stat->hash);
533 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400534 return -ENOMEM;
535 }
536
537 return 0;
538}
539
Steven Rostedtcafb1682009-03-24 20:50:39 -0400540static int ftrace_profile_init(void)
541{
542 int cpu;
543 int ret = 0;
544
545 for_each_online_cpu(cpu) {
546 ret = ftrace_profile_init_cpu(cpu);
547 if (ret)
548 break;
549 }
550
551 return ret;
552}
553
Steven Rostedt493762f2009-03-23 17:12:36 -0400554/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400555static struct ftrace_profile *
556ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400557{
558 struct ftrace_profile *rec;
559 struct hlist_head *hhd;
560 struct hlist_node *n;
561 unsigned long key;
562
563 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400564 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400565
566 if (hlist_empty(hhd))
567 return NULL;
568
569 hlist_for_each_entry_rcu(rec, n, hhd, node) {
570 if (rec->ip == ip)
571 return rec;
572 }
573
574 return NULL;
575}
576
Steven Rostedtcafb1682009-03-24 20:50:39 -0400577static void ftrace_add_profile(struct ftrace_profile_stat *stat,
578 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400579{
580 unsigned long key;
581
582 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400584}
585
Steven Rostedt318e0a72009-03-25 20:06:34 -0400586/*
587 * The memory is already allocated, this simply finds a new record to use.
588 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400589static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400590ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400591{
592 struct ftrace_profile *rec = NULL;
593
Steven Rostedt318e0a72009-03-25 20:06:34 -0400594 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 goto out;
597
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400599 * Try to find the function again since an NMI
600 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400604 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400605
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 if (stat->pages->index == PROFILES_PER_PAGE) {
607 if (!stat->pages->next)
608 goto out;
609 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 }
611
Steven Rostedtcafb1682009-03-24 20:50:39 -0400612 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400613 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400614 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400615
Steven Rostedt493762f2009-03-23 17:12:36 -0400616 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400618
619 return rec;
620}
621
Steven Rostedt493762f2009-03-23 17:12:36 -0400622static void
623function_profile_call(unsigned long ip, unsigned long parent_ip)
624{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 struct ftrace_profile *rec;
627 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400628
629 if (!ftrace_profile_enabled)
630 return;
631
Steven Rostedt493762f2009-03-23 17:12:36 -0400632 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400633
634 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400635 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400636 goto out;
637
638 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400639 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400640 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400641 if (!rec)
642 goto out;
643 }
644
645 rec->counter++;
646 out:
647 local_irq_restore(flags);
648}
649
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400650#ifdef CONFIG_FUNCTION_GRAPH_TRACER
651static int profile_graph_entry(struct ftrace_graph_ent *trace)
652{
653 function_profile_call(trace->func, 0);
654 return 1;
655}
656
657static void profile_graph_return(struct ftrace_graph_ret *trace)
658{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400659 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400660 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400661 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400663
664 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400665 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400666 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667 goto out;
668
Steven Rostedta2a16d62009-03-24 23:17:58 -0400669 calltime = trace->rettime - trace->calltime;
670
671 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
672 int index;
673
674 index = trace->depth;
675
676 /* Append this call time to the parent time to subtract */
677 if (index)
678 current->ret_stack[index - 1].subtime += calltime;
679
680 if (current->ret_stack[index].subtime < calltime)
681 calltime -= current->ret_stack[index].subtime;
682 else
683 calltime = 0;
684 }
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400687 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400688 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400689 rec->time_squared += calltime * calltime;
690 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400691
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400693 local_irq_restore(flags);
694}
695
696static int register_ftrace_profiler(void)
697{
698 return register_ftrace_graph(&profile_graph_return,
699 &profile_graph_entry);
700}
701
702static void unregister_ftrace_profiler(void)
703{
704 unregister_ftrace_graph();
705}
706#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400707static struct ftrace_ops ftrace_profile_ops __read_mostly =
708{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400709 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400710};
711
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400712static int register_ftrace_profiler(void)
713{
714 return register_ftrace_function(&ftrace_profile_ops);
715}
716
717static void unregister_ftrace_profiler(void)
718{
719 unregister_ftrace_function(&ftrace_profile_ops);
720}
721#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
722
Steven Rostedt493762f2009-03-23 17:12:36 -0400723static ssize_t
724ftrace_profile_write(struct file *filp, const char __user *ubuf,
725 size_t cnt, loff_t *ppos)
726{
727 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400728 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 int ret;
730
731 if (cnt >= sizeof(buf))
732 return -EINVAL;
733
734 if (copy_from_user(&buf, ubuf, cnt))
735 return -EFAULT;
736
737 buf[cnt] = 0;
738
739 ret = strict_strtoul(buf, 10, &val);
740 if (ret < 0)
741 return ret;
742
743 val = !!val;
744
745 mutex_lock(&ftrace_profile_lock);
746 if (ftrace_profile_enabled ^ val) {
747 if (val) {
748 ret = ftrace_profile_init();
749 if (ret < 0) {
750 cnt = ret;
751 goto out;
752 }
753
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400754 ret = register_ftrace_profiler();
755 if (ret < 0) {
756 cnt = ret;
757 goto out;
758 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400759 ftrace_profile_enabled = 1;
760 } else {
761 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400762 /*
763 * unregister_ftrace_profiler calls stop_machine
764 * so this acts like an synchronize_sched.
765 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400766 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 }
768 }
769 out:
770 mutex_unlock(&ftrace_profile_lock);
771
Jiri Olsacf8517c2009-10-23 19:36:16 -0400772 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400773
774 return cnt;
775}
776
777static ssize_t
778ftrace_profile_read(struct file *filp, char __user *ubuf,
779 size_t cnt, loff_t *ppos)
780{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400781 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400782 int r;
783
784 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
785 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
786}
787
788static const struct file_operations ftrace_profile_fops = {
789 .open = tracing_open_generic,
790 .read = ftrace_profile_read,
791 .write = ftrace_profile_write,
792};
793
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794/* used to initialize the real stat files */
795static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400796 .name = "functions",
797 .stat_start = function_stat_start,
798 .stat_next = function_stat_next,
799 .stat_cmp = function_stat_cmp,
800 .stat_headers = function_stat_headers,
801 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802};
803
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400804static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400805{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400807 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400809 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400811
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 for_each_possible_cpu(cpu) {
813 stat = &per_cpu(ftrace_profile_stats, cpu);
814
815 /* allocate enough for function name + cpu number */
816 name = kmalloc(32, GFP_KERNEL);
817 if (!name) {
818 /*
819 * The files created are permanent, if something happens
820 * we still do not free memory.
821 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 WARN(1,
823 "Could not allocate stat file for cpu %d\n",
824 cpu);
825 return;
826 }
827 stat->stat = function_stats;
828 snprintf(name, 32, "function%d", cpu);
829 stat->stat.name = name;
830 ret = register_stat_tracer(&stat->stat);
831 if (ret) {
832 WARN(1,
833 "Could not register function stat for cpu %d\n",
834 cpu);
835 kfree(name);
836 return;
837 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 }
839
840 entry = debugfs_create_file("function_profile_enabled", 0644,
841 d_tracer, NULL, &ftrace_profile_fops);
842 if (!entry)
843 pr_warning("Could not create debugfs "
844 "'function_profile_enabled' entry\n");
845}
846
847#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400848static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400849{
850}
851#endif /* CONFIG_FUNCTION_PROFILER */
852
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100853static struct pid * const ftrace_swapper_pid = &init_struct_pid;
854
Steven Rostedt3d083392008-05-12 21:20:42 +0200855#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100856
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400857#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400858# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400859#endif
860
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500861static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
862
Steven Rostedtb6887d72009-02-17 12:32:04 -0500863struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500864 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500865 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500866 unsigned long flags;
867 unsigned long ip;
868 void *data;
869 struct rcu_head rcu;
870};
871
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200872enum {
873 FTRACE_ENABLE_CALLS = (1 << 0),
874 FTRACE_DISABLE_CALLS = (1 << 1),
875 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
876 FTRACE_ENABLE_MCOUNT = (1 << 3),
877 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500878 FTRACE_START_FUNC_RET = (1 << 5),
879 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200880};
881
Steven Rostedt5072c592008-05-12 21:20:43 +0200882static int ftrace_filtered;
883
Lai Jiangshane94142a2009-03-13 17:51:27 +0800884static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200885
Steven Rostedt41c52c02008-05-22 11:46:33 -0400886static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200887
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200888struct ftrace_page {
889 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500890 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200891 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700892};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200893
894#define ENTRIES_PER_PAGE \
895 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
896
897/* estimate from running different kernels */
898#define NR_TO_INIT 10000
899
900static struct ftrace_page *ftrace_pages_start;
901static struct ftrace_page *ftrace_pages;
902
Steven Rostedt37ad5082008-05-12 21:20:48 +0200903static struct dyn_ftrace *ftrace_free_records;
904
Steven Rostedt265c8312009-02-13 12:43:56 -0500905/*
906 * This is a double for. Do not use 'break' to break out of the loop,
907 * you must use a goto.
908 */
909#define do_for_each_ftrace_rec(pg, rec) \
910 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
911 int _____i; \
912 for (_____i = 0; _____i < pg->index; _____i++) { \
913 rec = &pg->records[_____i];
914
915#define while_for_each_ftrace_rec() \
916 } \
917 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530918
Ingo Molnare309b412008-05-12 21:20:51 +0200919static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200920{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800921 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200922 ftrace_free_records = rec;
923 rec->flags |= FTRACE_FL_FREE;
924}
925
Ingo Molnare309b412008-05-12 21:20:51 +0200926static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200927{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200928 struct dyn_ftrace *rec;
929
930 /* First check for freed records */
931 if (ftrace_free_records) {
932 rec = ftrace_free_records;
933
Steven Rostedt37ad5082008-05-12 21:20:48 +0200934 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400935 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200936 ftrace_free_records = NULL;
937 return NULL;
938 }
939
Lai Jiangshanee000b72009-03-24 13:38:06 +0800940 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200941 memset(rec, 0, sizeof(*rec));
942 return rec;
943 }
944
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200945 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400946 if (!ftrace_pages->next) {
947 /* allocate another page */
948 ftrace_pages->next =
949 (void *)get_zeroed_page(GFP_KERNEL);
950 if (!ftrace_pages->next)
951 return NULL;
952 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200953 ftrace_pages = ftrace_pages->next;
954 }
955
956 return &ftrace_pages->records[ftrace_pages->index++];
957}
958
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400959static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200960ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200961{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400962 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200963
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800964 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400965 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200966
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400967 rec = ftrace_alloc_dyn_node(ip);
968 if (!rec)
969 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200970
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400971 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800972 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800973 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200974
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400975 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200976}
977
Steven Rostedt05736a42008-09-22 14:55:47 -0700978static void print_ip_ins(const char *fmt, unsigned char *p)
979{
980 int i;
981
982 printk(KERN_CONT "%s", fmt);
983
984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
986}
987
Steven Rostedt31e88902008-11-14 16:21:19 -0800988static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800989{
990 switch (failed) {
991 case -EFAULT:
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace faulted on modifying ");
994 print_ip_sym(ip);
995 break;
996 case -EINVAL:
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace failed to modify ");
999 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001000 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001001 printk(KERN_CONT "\n");
1002 break;
1003 case -EPERM:
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on writing ");
1006 print_ip_sym(ip);
1007 break;
1008 default:
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on unknown error ");
1011 print_ip_sym(ip);
1012 }
1013}
1014
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001015
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001016/* Return 1 if the address range is reserved for ftrace */
1017int ftrace_text_reserved(void *start, void *end)
1018{
1019 struct dyn_ftrace *rec;
1020 struct ftrace_page *pg;
1021
1022 do_for_each_ftrace_rec(pg, rec) {
1023 if (rec->ip <= (unsigned long)end &&
1024 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1025 return 1;
1026 } while_for_each_ftrace_rec();
1027 return 0;
1028}
1029
1030
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301031static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001032__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001033{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001034 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001035 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001036
Shaohua Lif0001202009-01-09 11:29:42 +08001037 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001038
Steven Rostedt982c3502008-11-15 16:31:41 -05001039 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001040 * If this record is not to be traced or we want to disable it,
1041 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001042 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001043 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001044 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001045 * If we want to enable it and filtering is on, enable it only if
1046 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001047 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001048 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1049 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1050 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001051 }
1052
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001053 /* If the state of this record hasn't changed, then do nothing */
1054 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1055 return 0;
1056
1057 if (flag) {
1058 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001059 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001060 }
1061
1062 rec->flags &= ~FTRACE_FL_ENABLED;
1063 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001064}
1065
1066static void ftrace_replace_code(int enable)
1067{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001068 struct dyn_ftrace *rec;
1069 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001070 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001071
Steven Rostedt265c8312009-02-13 12:43:56 -05001072 do_for_each_ftrace_rec(pg, rec) {
1073 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001074 * Skip over free records, records that have
1075 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001076 */
1077 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001078 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001079 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001080 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001081
Steven Rostedt265c8312009-02-13 12:43:56 -05001082 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001083 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001084 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001085 ftrace_bug(failed, rec->ip);
1086 /* Stop processing */
1087 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001088 }
1089 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001090}
1091
Ingo Molnare309b412008-05-12 21:20:51 +02001092static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001093ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001094{
1095 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001096 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001097
1098 ip = rec->ip;
1099
Shaohua Li25aac9d2009-01-09 11:29:40 +08001100 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001101 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001102 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001103 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301104 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001105 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301106 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107}
1108
Steven Rostedt000ab692009-02-17 13:35:06 -05001109/*
1110 * archs can override this function if they must do something
1111 * before the modifying code is performed.
1112 */
1113int __weak ftrace_arch_code_modify_prepare(void)
1114{
1115 return 0;
1116}
1117
1118/*
1119 * archs can override this function if they must do something
1120 * after the modifying code is performed.
1121 */
1122int __weak ftrace_arch_code_modify_post_process(void)
1123{
1124 return 0;
1125}
1126
Ingo Molnare309b412008-05-12 21:20:51 +02001127static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001128{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001129 int *command = data;
1130
Steven Rostedta3583242008-11-11 15:01:42 -05001131 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001132 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001133 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001134 ftrace_replace_code(0);
1135
1136 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1137 ftrace_update_ftrace_func(ftrace_trace_function);
1138
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001139 if (*command & FTRACE_START_FUNC_RET)
1140 ftrace_enable_ftrace_graph_caller();
1141 else if (*command & FTRACE_STOP_FUNC_RET)
1142 ftrace_disable_ftrace_graph_caller();
1143
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001144 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001145}
1146
Ingo Molnare309b412008-05-12 21:20:51 +02001147static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001148{
Steven Rostedt000ab692009-02-17 13:35:06 -05001149 int ret;
1150
1151 ret = ftrace_arch_code_modify_prepare();
1152 FTRACE_WARN_ON(ret);
1153 if (ret)
1154 return;
1155
Rusty Russell784e2d72008-07-28 12:16:31 -05001156 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001157
1158 ret = ftrace_arch_code_modify_post_process();
1159 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001160}
1161
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001162static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001163static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001164
1165static void ftrace_startup_enable(int command)
1166{
1167 if (saved_ftrace_func != ftrace_trace_function) {
1168 saved_ftrace_func = ftrace_trace_function;
1169 command |= FTRACE_UPDATE_TRACE_FUNC;
1170 }
1171
1172 if (!command || !ftrace_enabled)
1173 return;
1174
1175 ftrace_run_update_code(command);
1176}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001177
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001178static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001179{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001180 if (unlikely(ftrace_disabled))
1181 return;
1182
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001183 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001184 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001185
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001186 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001187}
1188
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001189static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001190{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001191 if (unlikely(ftrace_disabled))
1192 return;
1193
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001194 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001195 /*
1196 * Just warn in case of unbalance, no need to kill ftrace, it's not
1197 * critical but the ftrace_call callers may be never nopped again after
1198 * further ftrace uses.
1199 */
1200 WARN_ON_ONCE(ftrace_start_up < 0);
1201
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001202 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001203 command |= FTRACE_DISABLE_CALLS;
1204
1205 if (saved_ftrace_func != ftrace_trace_function) {
1206 saved_ftrace_func = ftrace_trace_function;
1207 command |= FTRACE_UPDATE_TRACE_FUNC;
1208 }
1209
1210 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001211 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001212
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001213 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001214}
1215
Ingo Molnare309b412008-05-12 21:20:51 +02001216static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001217{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001218 int command = FTRACE_ENABLE_MCOUNT;
1219
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001220 if (unlikely(ftrace_disabled))
1221 return;
1222
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001223 /* Force update next time */
1224 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001225 /* ftrace_start_up is true if we want ftrace running */
1226 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001227 command |= FTRACE_ENABLE_CALLS;
1228
1229 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001230}
1231
Ingo Molnare309b412008-05-12 21:20:51 +02001232static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001233{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001234 int command = FTRACE_DISABLE_MCOUNT;
1235
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001236 if (unlikely(ftrace_disabled))
1237 return;
1238
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001239 /* ftrace_start_up is true if ftrace is running */
1240 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001241 command |= FTRACE_DISABLE_CALLS;
1242
1243 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001244}
1245
Steven Rostedt3d083392008-05-12 21:20:42 +02001246static cycle_t ftrace_update_time;
1247static unsigned long ftrace_update_cnt;
1248unsigned long ftrace_update_tot_cnt;
1249
Steven Rostedt31e88902008-11-14 16:21:19 -08001250static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001251{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001252 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301253 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001254
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001255 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001256 ftrace_update_cnt = 0;
1257
Lai Jiangshane94142a2009-03-13 17:51:27 +08001258 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301259
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001260 /* If something went wrong, bail without enabling anything */
1261 if (unlikely(ftrace_disabled))
1262 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001263
Lai Jiangshane94142a2009-03-13 17:51:27 +08001264 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001265 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001266 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301267
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001268 /*
1269 * Do the initial record convertion from mcount jump
1270 * to the NOP instructions.
1271 */
1272 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001273 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001274 continue;
1275 }
1276
1277 p->flags |= FTRACE_FL_CONVERTED;
1278 ftrace_update_cnt++;
1279
1280 /*
1281 * If the tracing is enabled, go ahead and enable the record.
1282 *
1283 * The reason not to enable the record immediatelly is the
1284 * inherent check of ftrace_make_nop/ftrace_make_call for
1285 * correct previous instructions. Making first the NOP
1286 * conversion puts the module to the correct state, thus
1287 * passing the ftrace_make_call check.
1288 */
1289 if (ftrace_start_up) {
1290 int failed = __ftrace_replace_code(p, 1);
1291 if (failed) {
1292 ftrace_bug(failed, p->ip);
1293 ftrace_free_rec(p);
1294 }
1295 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001296 }
1297
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001298 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001299 ftrace_update_time = stop - start;
1300 ftrace_update_tot_cnt += ftrace_update_cnt;
1301
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001302 return 0;
1303}
1304
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001305static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001306{
1307 struct ftrace_page *pg;
1308 int cnt;
1309 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001310
1311 /* allocate a few pages */
1312 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1313 if (!ftrace_pages_start)
1314 return -1;
1315
1316 /*
1317 * Allocate a few more pages.
1318 *
1319 * TODO: have some parser search vmlinux before
1320 * final linking to find all calls to ftrace.
1321 * Then we can:
1322 * a) know how many pages to allocate.
1323 * and/or
1324 * b) set up the table then.
1325 *
1326 * The dynamic code is still necessary for
1327 * modules.
1328 */
1329
1330 pg = ftrace_pages = ftrace_pages_start;
1331
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001332 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001333 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001334 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001335
1336 for (i = 0; i < cnt; i++) {
1337 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1338
1339 /* If we fail, we'll try later anyway */
1340 if (!pg->next)
1341 break;
1342
1343 pg = pg->next;
1344 }
1345
1346 return 0;
1347}
1348
Steven Rostedt5072c592008-05-12 21:20:43 +02001349enum {
1350 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001351 FTRACE_ITER_NOTRACE = (1 << 1),
1352 FTRACE_ITER_FAILURES = (1 << 2),
1353 FTRACE_ITER_PRINTALL = (1 << 3),
1354 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001355};
1356
1357#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1358
1359struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001360 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001361 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001362 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001363 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001364 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001365};
1366
Ingo Molnare309b412008-05-12 21:20:51 +02001367static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001368t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1369{
1370 struct ftrace_iterator *iter = m->private;
1371 struct hlist_node *hnd = v;
1372 struct hlist_head *hhd;
1373
1374 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1375
1376 (*pos)++;
1377
1378 retry:
1379 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1380 return NULL;
1381
1382 hhd = &ftrace_func_hash[iter->hidx];
1383
1384 if (hlist_empty(hhd)) {
1385 iter->hidx++;
1386 hnd = NULL;
1387 goto retry;
1388 }
1389
1390 if (!hnd)
1391 hnd = hhd->first;
1392 else {
1393 hnd = hnd->next;
1394 if (!hnd) {
1395 iter->hidx++;
1396 goto retry;
1397 }
1398 }
1399
1400 return hnd;
1401}
1402
1403static void *t_hash_start(struct seq_file *m, loff_t *pos)
1404{
1405 struct ftrace_iterator *iter = m->private;
1406 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001407 loff_t l;
1408
1409 if (!(iter->flags & FTRACE_ITER_HASH))
1410 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001411
1412 iter->flags |= FTRACE_ITER_HASH;
1413
Li Zefand82d6242009-06-24 09:54:54 +08001414 iter->hidx = 0;
1415 for (l = 0; l <= *pos; ) {
1416 p = t_hash_next(m, p, &l);
1417 if (!p)
1418 break;
1419 }
1420 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001421}
1422
1423static int t_hash_show(struct seq_file *m, void *v)
1424{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001425 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001426 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001427
Steven Rostedtb6887d72009-02-17 12:32:04 -05001428 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001429
Steven Rostedt809dcf22009-02-16 23:06:01 -05001430 if (rec->ops->print)
1431 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1432
Steven Rostedtb375a112009-09-17 00:05:58 -04001433 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001434
1435 if (rec->data)
1436 seq_printf(m, ":%p", rec->data);
1437 seq_putc(m, '\n');
1438
1439 return 0;
1440}
1441
1442static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001443t_next(struct seq_file *m, void *v, loff_t *pos)
1444{
1445 struct ftrace_iterator *iter = m->private;
1446 struct dyn_ftrace *rec = NULL;
1447
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001448 if (iter->flags & FTRACE_ITER_HASH)
1449 return t_hash_next(m, v, pos);
1450
Steven Rostedt5072c592008-05-12 21:20:43 +02001451 (*pos)++;
1452
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001453 if (iter->flags & FTRACE_ITER_PRINTALL)
1454 return NULL;
1455
Steven Rostedt5072c592008-05-12 21:20:43 +02001456 retry:
1457 if (iter->idx >= iter->pg->index) {
1458 if (iter->pg->next) {
1459 iter->pg = iter->pg->next;
1460 iter->idx = 0;
1461 goto retry;
1462 }
1463 } else {
1464 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001465 if ((rec->flags & FTRACE_FL_FREE) ||
1466
1467 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301468 (rec->flags & FTRACE_FL_FAILED)) ||
1469
1470 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001471 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301472
Steven Rostedt0183fb12008-11-07 22:36:02 -05001473 ((iter->flags & FTRACE_ITER_FILTER) &&
1474 !(rec->flags & FTRACE_FL_FILTER)) ||
1475
Steven Rostedt41c52c02008-05-22 11:46:33 -04001476 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1477 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001478 rec = NULL;
1479 goto retry;
1480 }
1481 }
1482
Steven Rostedt5072c592008-05-12 21:20:43 +02001483 return rec;
1484}
1485
1486static void *t_start(struct seq_file *m, loff_t *pos)
1487{
1488 struct ftrace_iterator *iter = m->private;
1489 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001490 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001491
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001492 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001493 /*
1494 * For set_ftrace_filter reading, if we have the filter
1495 * off, we can short cut and just print out that all
1496 * functions are enabled.
1497 */
1498 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1499 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001500 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001501 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001502 return iter;
1503 }
1504
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001505 if (iter->flags & FTRACE_ITER_HASH)
1506 return t_hash_start(m, pos);
1507
Li Zefan694ce0a2009-06-24 09:54:19 +08001508 iter->pg = ftrace_pages_start;
1509 iter->idx = 0;
1510 for (l = 0; l <= *pos; ) {
1511 p = t_next(m, p, &l);
1512 if (!p)
1513 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001514 }
walimis5821e1b2008-11-15 15:19:06 +08001515
Li Zefan694ce0a2009-06-24 09:54:19 +08001516 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001517 return t_hash_start(m, pos);
1518
Steven Rostedt5072c592008-05-12 21:20:43 +02001519 return p;
1520}
1521
1522static void t_stop(struct seq_file *m, void *p)
1523{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001524 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001525}
1526
1527static int t_show(struct seq_file *m, void *v)
1528{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001529 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001530 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001531
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001532 if (iter->flags & FTRACE_ITER_HASH)
1533 return t_hash_show(m, v);
1534
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001535 if (iter->flags & FTRACE_ITER_PRINTALL) {
1536 seq_printf(m, "#### all functions enabled ####\n");
1537 return 0;
1538 }
1539
Steven Rostedt5072c592008-05-12 21:20:43 +02001540 if (!rec)
1541 return 0;
1542
Steven Rostedtb375a112009-09-17 00:05:58 -04001543 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001544
1545 return 0;
1546}
1547
James Morris88e9d342009-09-22 16:43:43 -07001548static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001549 .start = t_start,
1550 .next = t_next,
1551 .stop = t_stop,
1552 .show = t_show,
1553};
1554
Ingo Molnare309b412008-05-12 21:20:51 +02001555static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001556ftrace_avail_open(struct inode *inode, struct file *file)
1557{
1558 struct ftrace_iterator *iter;
1559 int ret;
1560
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001561 if (unlikely(ftrace_disabled))
1562 return -ENODEV;
1563
Steven Rostedt5072c592008-05-12 21:20:43 +02001564 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1565 if (!iter)
1566 return -ENOMEM;
1567
1568 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001569
1570 ret = seq_open(file, &show_ftrace_seq_ops);
1571 if (!ret) {
1572 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001573
Steven Rostedt5072c592008-05-12 21:20:43 +02001574 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001575 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001576 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001577 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001578
1579 return ret;
1580}
1581
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301582static int
1583ftrace_failures_open(struct inode *inode, struct file *file)
1584{
1585 int ret;
1586 struct seq_file *m;
1587 struct ftrace_iterator *iter;
1588
1589 ret = ftrace_avail_open(inode, file);
1590 if (!ret) {
1591 m = (struct seq_file *)file->private_data;
1592 iter = (struct ftrace_iterator *)m->private;
1593 iter->flags = FTRACE_ITER_FAILURES;
1594 }
1595
1596 return ret;
1597}
1598
1599
Steven Rostedt41c52c02008-05-22 11:46:33 -04001600static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001601{
1602 struct ftrace_page *pg;
1603 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001604 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001605
Steven Rostedt52baf112009-02-14 01:15:39 -05001606 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001607 if (enable)
1608 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001609 do_for_each_ftrace_rec(pg, rec) {
1610 if (rec->flags & FTRACE_FL_FAILED)
1611 continue;
1612 rec->flags &= ~type;
1613 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001614 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001615}
1616
Ingo Molnare309b412008-05-12 21:20:51 +02001617static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001618ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001619{
1620 struct ftrace_iterator *iter;
1621 int ret = 0;
1622
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001623 if (unlikely(ftrace_disabled))
1624 return -ENODEV;
1625
Steven Rostedt5072c592008-05-12 21:20:43 +02001626 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1627 if (!iter)
1628 return -ENOMEM;
1629
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001630 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1631 kfree(iter);
1632 return -ENOMEM;
1633 }
1634
Steven Rostedt41c52c02008-05-22 11:46:33 -04001635 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001636 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001637 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001638 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001639
1640 if (file->f_mode & FMODE_READ) {
1641 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001642 iter->flags = enable ? FTRACE_ITER_FILTER :
1643 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001644
1645 ret = seq_open(file, &show_ftrace_seq_ops);
1646 if (!ret) {
1647 struct seq_file *m = file->private_data;
1648 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001649 } else {
1650 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001651 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001652 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001653 } else
1654 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001655 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001656
1657 return ret;
1658}
1659
Steven Rostedt41c52c02008-05-22 11:46:33 -04001660static int
1661ftrace_filter_open(struct inode *inode, struct file *file)
1662{
1663 return ftrace_regex_open(inode, file, 1);
1664}
1665
1666static int
1667ftrace_notrace_open(struct inode *inode, struct file *file)
1668{
1669 return ftrace_regex_open(inode, file, 0);
1670}
1671
Ingo Molnare309b412008-05-12 21:20:51 +02001672static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001673ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001674{
1675 loff_t ret;
1676
1677 if (file->f_mode & FMODE_READ)
1678 ret = seq_lseek(file, offset, origin);
1679 else
1680 file->f_pos = ret = 1;
1681
1682 return ret;
1683}
1684
Steven Rostedt64e7c442009-02-13 17:08:48 -05001685static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001686{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001687 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001688 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001689
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001690 switch (type) {
1691 case MATCH_FULL:
1692 if (strcmp(str, regex) == 0)
1693 matched = 1;
1694 break;
1695 case MATCH_FRONT_ONLY:
1696 if (strncmp(str, regex, len) == 0)
1697 matched = 1;
1698 break;
1699 case MATCH_MIDDLE_ONLY:
1700 if (strstr(str, regex))
1701 matched = 1;
1702 break;
1703 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001704 slen = strlen(str);
1705 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001706 matched = 1;
1707 break;
1708 }
1709
1710 return matched;
1711}
1712
Steven Rostedt64e7c442009-02-13 17:08:48 -05001713static int
1714ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1715{
1716 char str[KSYM_SYMBOL_LEN];
1717
1718 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1719 return ftrace_match(str, regex, len, type);
1720}
1721
Li Zefan311d16d2009-12-08 11:15:11 +08001722static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001723{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001724 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001725 struct ftrace_page *pg;
1726 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001727 unsigned long flag;
1728 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001729 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001730 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001731 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001732
Steven Rostedt6a24a242009-02-17 11:20:26 -05001733 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001734 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001735
1736 search_len = strlen(search);
1737
Steven Rostedt52baf112009-02-14 01:15:39 -05001738 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001739 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001740
Steven Rostedt265c8312009-02-13 12:43:56 -05001741 if (rec->flags & FTRACE_FL_FAILED)
1742 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001743
1744 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001745 if (not)
1746 rec->flags &= ~flag;
1747 else
1748 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001749 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001750 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001751 /*
1752 * Only enable filtering if we have a function that
1753 * is filtered on.
1754 */
1755 if (enable && (rec->flags & FTRACE_FL_FILTER))
1756 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001757 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001758 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001759
1760 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001761}
1762
Steven Rostedt64e7c442009-02-13 17:08:48 -05001763static int
1764ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1765 char *regex, int len, int type)
1766{
1767 char str[KSYM_SYMBOL_LEN];
1768 char *modname;
1769
1770 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1771
1772 if (!modname || strcmp(modname, mod))
1773 return 0;
1774
1775 /* blank search means to match all funcs in the mod */
1776 if (len)
1777 return ftrace_match(str, regex, len, type);
1778 else
1779 return 1;
1780}
1781
Li Zefan311d16d2009-12-08 11:15:11 +08001782static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001783{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001784 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001785 struct ftrace_page *pg;
1786 struct dyn_ftrace *rec;
1787 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001788 char *search = buff;
1789 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001790 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001791 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001792
Steven Rostedt6a24a242009-02-17 11:20:26 -05001793 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1794
Steven Rostedt64e7c442009-02-13 17:08:48 -05001795 /* blank or '*' mean the same */
1796 if (strcmp(buff, "*") == 0)
1797 buff[0] = 0;
1798
1799 /* handle the case of 'dont filter this module' */
1800 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1801 buff[0] = 0;
1802 not = 1;
1803 }
1804
1805 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001806 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001807 search_len = strlen(search);
1808 }
1809
Steven Rostedt52baf112009-02-14 01:15:39 -05001810 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001811 do_for_each_ftrace_rec(pg, rec) {
1812
1813 if (rec->flags & FTRACE_FL_FAILED)
1814 continue;
1815
1816 if (ftrace_match_module_record(rec, mod,
1817 search, search_len, type)) {
1818 if (not)
1819 rec->flags &= ~flag;
1820 else
1821 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001822 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001823 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001824 if (enable && (rec->flags & FTRACE_FL_FILTER))
1825 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001826
1827 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001828 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001829
1830 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001831}
1832
Steven Rostedtf6180772009-02-14 00:40:25 -05001833/*
1834 * We register the module command as a template to show others how
1835 * to register the a command as well.
1836 */
1837
1838static int
1839ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1840{
1841 char *mod;
1842
1843 /*
1844 * cmd == 'mod' because we only registered this func
1845 * for the 'mod' ftrace_func_command.
1846 * But if you register one func with multiple commands,
1847 * you can tell which command was used by the cmd
1848 * parameter.
1849 */
1850
1851 /* we must have a module name */
1852 if (!param)
1853 return -EINVAL;
1854
1855 mod = strsep(&param, ":");
1856 if (!strlen(mod))
1857 return -EINVAL;
1858
Li Zefan311d16d2009-12-08 11:15:11 +08001859 if (ftrace_match_module_records(func, mod, enable))
1860 return 0;
1861 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001862}
1863
1864static struct ftrace_func_command ftrace_mod_cmd = {
1865 .name = "mod",
1866 .func = ftrace_mod_callback,
1867};
1868
1869static int __init ftrace_mod_cmd_init(void)
1870{
1871 return register_ftrace_command(&ftrace_mod_cmd);
1872}
1873device_initcall(ftrace_mod_cmd_init);
1874
Steven Rostedt59df055f2009-02-14 15:29:06 -05001875static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001876function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001877{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001878 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001879 struct hlist_head *hhd;
1880 struct hlist_node *n;
1881 unsigned long key;
1882 int resched;
1883
1884 key = hash_long(ip, FTRACE_HASH_BITS);
1885
1886 hhd = &ftrace_func_hash[key];
1887
1888 if (hlist_empty(hhd))
1889 return;
1890
1891 /*
1892 * Disable preemption for these calls to prevent a RCU grace
1893 * period. This syncs the hash iteration and freeing of items
1894 * on the hash. rcu_read_lock is too dangerous here.
1895 */
1896 resched = ftrace_preempt_disable();
1897 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1898 if (entry->ip == ip)
1899 entry->ops->func(ip, parent_ip, &entry->data);
1900 }
1901 ftrace_preempt_enable(resched);
1902}
1903
Steven Rostedtb6887d72009-02-17 12:32:04 -05001904static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001905{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001906 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001907};
1908
Steven Rostedtb6887d72009-02-17 12:32:04 -05001909static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001910
Steven Rostedtb6887d72009-02-17 12:32:04 -05001911static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001912{
1913 int i;
1914
Steven Rostedtb6887d72009-02-17 12:32:04 -05001915 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001916 return;
1917
1918 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1919 struct hlist_head *hhd = &ftrace_func_hash[i];
1920 if (hhd->first)
1921 break;
1922 }
1923 /* Nothing registered? */
1924 if (i == FTRACE_FUNC_HASHSIZE)
1925 return;
1926
Steven Rostedtb6887d72009-02-17 12:32:04 -05001927 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001928 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001929 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001930}
1931
Steven Rostedtb6887d72009-02-17 12:32:04 -05001932static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001933{
1934 int i;
1935
Steven Rostedtb6887d72009-02-17 12:32:04 -05001936 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001937 return;
1938
1939 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1940 struct hlist_head *hhd = &ftrace_func_hash[i];
1941 if (hhd->first)
1942 return;
1943 }
1944
1945 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001946 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001947 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001948 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001949}
1950
1951
1952static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1953{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001954 struct ftrace_func_probe *entry =
1955 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001956
1957 if (entry->ops->free)
1958 entry->ops->free(&entry->data);
1959 kfree(entry);
1960}
1961
1962
1963int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001964register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001965 void *data)
1966{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001967 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001968 struct ftrace_page *pg;
1969 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001970 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001971 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001972 int count = 0;
1973 char *search;
1974
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001975 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001976 len = strlen(search);
1977
Steven Rostedtb6887d72009-02-17 12:32:04 -05001978 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001979 if (WARN_ON(not))
1980 return -EINVAL;
1981
1982 mutex_lock(&ftrace_lock);
1983 do_for_each_ftrace_rec(pg, rec) {
1984
1985 if (rec->flags & FTRACE_FL_FAILED)
1986 continue;
1987
1988 if (!ftrace_match_record(rec, search, len, type))
1989 continue;
1990
1991 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1992 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001993 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001994 if (!count)
1995 count = -ENOMEM;
1996 goto out_unlock;
1997 }
1998
1999 count++;
2000
2001 entry->data = data;
2002
2003 /*
2004 * The caller might want to do something special
2005 * for each function we find. We call the callback
2006 * to give the caller an opportunity to do so.
2007 */
2008 if (ops->callback) {
2009 if (ops->callback(rec->ip, &entry->data) < 0) {
2010 /* caller does not like this func */
2011 kfree(entry);
2012 continue;
2013 }
2014 }
2015
2016 entry->ops = ops;
2017 entry->ip = rec->ip;
2018
2019 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2020 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2021
2022 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002023 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002024
2025 out_unlock:
2026 mutex_unlock(&ftrace_lock);
2027
2028 return count;
2029}
2030
2031enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002032 PROBE_TEST_FUNC = 1,
2033 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002034};
2035
2036static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002037__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002038 void *data, int flags)
2039{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002040 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002041 struct hlist_node *n, *tmp;
2042 char str[KSYM_SYMBOL_LEN];
2043 int type = MATCH_FULL;
2044 int i, len = 0;
2045 char *search;
2046
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002047 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002048 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002049 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002050 int not;
2051
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002052 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002053 len = strlen(search);
2054
Steven Rostedtb6887d72009-02-17 12:32:04 -05002055 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002056 if (WARN_ON(not))
2057 return;
2058 }
2059
2060 mutex_lock(&ftrace_lock);
2061 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2062 struct hlist_head *hhd = &ftrace_func_hash[i];
2063
2064 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2065
2066 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002067 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002068 continue;
2069
Steven Rostedtb6887d72009-02-17 12:32:04 -05002070 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002071 continue;
2072
2073 /* do this last, since it is the most expensive */
2074 if (glob) {
2075 kallsyms_lookup(entry->ip, NULL, NULL,
2076 NULL, str);
2077 if (!ftrace_match(str, glob, len, type))
2078 continue;
2079 }
2080
2081 hlist_del(&entry->node);
2082 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2083 }
2084 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002085 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002086 mutex_unlock(&ftrace_lock);
2087}
2088
2089void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002090unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002091 void *data)
2092{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002093 __unregister_ftrace_function_probe(glob, ops, data,
2094 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002095}
2096
2097void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002098unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002099{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002100 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002101}
2102
Steven Rostedtb6887d72009-02-17 12:32:04 -05002103void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002104{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002105 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002106}
2107
Steven Rostedtf6180772009-02-14 00:40:25 -05002108static LIST_HEAD(ftrace_commands);
2109static DEFINE_MUTEX(ftrace_cmd_mutex);
2110
2111int register_ftrace_command(struct ftrace_func_command *cmd)
2112{
2113 struct ftrace_func_command *p;
2114 int ret = 0;
2115
2116 mutex_lock(&ftrace_cmd_mutex);
2117 list_for_each_entry(p, &ftrace_commands, list) {
2118 if (strcmp(cmd->name, p->name) == 0) {
2119 ret = -EBUSY;
2120 goto out_unlock;
2121 }
2122 }
2123 list_add(&cmd->list, &ftrace_commands);
2124 out_unlock:
2125 mutex_unlock(&ftrace_cmd_mutex);
2126
2127 return ret;
2128}
2129
2130int unregister_ftrace_command(struct ftrace_func_command *cmd)
2131{
2132 struct ftrace_func_command *p, *n;
2133 int ret = -ENODEV;
2134
2135 mutex_lock(&ftrace_cmd_mutex);
2136 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2137 if (strcmp(cmd->name, p->name) == 0) {
2138 ret = 0;
2139 list_del_init(&p->list);
2140 goto out_unlock;
2141 }
2142 }
2143 out_unlock:
2144 mutex_unlock(&ftrace_cmd_mutex);
2145
2146 return ret;
2147}
2148
Steven Rostedt64e7c442009-02-13 17:08:48 -05002149static int ftrace_process_regex(char *buff, int len, int enable)
2150{
Steven Rostedtf6180772009-02-14 00:40:25 -05002151 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002152 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002153 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002154
2155 func = strsep(&next, ":");
2156
2157 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002158 if (ftrace_match_records(func, len, enable))
2159 return 0;
2160 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002161 }
2162
Steven Rostedtf6180772009-02-14 00:40:25 -05002163 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002164
2165 command = strsep(&next, ":");
2166
Steven Rostedtf6180772009-02-14 00:40:25 -05002167 mutex_lock(&ftrace_cmd_mutex);
2168 list_for_each_entry(p, &ftrace_commands, list) {
2169 if (strcmp(p->name, command) == 0) {
2170 ret = p->func(func, command, next, enable);
2171 goto out_unlock;
2172 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002173 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002174 out_unlock:
2175 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002176
Steven Rostedtf6180772009-02-14 00:40:25 -05002177 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002178}
2179
Ingo Molnare309b412008-05-12 21:20:51 +02002180static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002181ftrace_regex_write(struct file *file, const char __user *ubuf,
2182 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002183{
2184 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002185 struct trace_parser *parser;
2186 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002187
Li Zefan4ba79782009-09-22 13:52:20 +08002188 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002189 return 0;
2190
Steven Rostedt41c52c02008-05-22 11:46:33 -04002191 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002192
2193 if (file->f_mode & FMODE_READ) {
2194 struct seq_file *m = file->private_data;
2195 iter = m->private;
2196 } else
2197 iter = file->private_data;
2198
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002199 parser = &iter->parser;
2200 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002201
Li Zefan4ba79782009-09-22 13:52:20 +08002202 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002203 !trace_parser_cont(parser)) {
2204 ret = ftrace_process_regex(parser->buffer,
2205 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002206 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002207 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002208 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002209 }
2210
Steven Rostedt5072c592008-05-12 21:20:43 +02002211 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002212out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002213 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002214
Steven Rostedt5072c592008-05-12 21:20:43 +02002215 return ret;
2216}
2217
Steven Rostedt41c52c02008-05-22 11:46:33 -04002218static ssize_t
2219ftrace_filter_write(struct file *file, const char __user *ubuf,
2220 size_t cnt, loff_t *ppos)
2221{
2222 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2223}
2224
2225static ssize_t
2226ftrace_notrace_write(struct file *file, const char __user *ubuf,
2227 size_t cnt, loff_t *ppos)
2228{
2229 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2230}
2231
2232static void
2233ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2234{
2235 if (unlikely(ftrace_disabled))
2236 return;
2237
2238 mutex_lock(&ftrace_regex_lock);
2239 if (reset)
2240 ftrace_filter_reset(enable);
2241 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002242 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002243 mutex_unlock(&ftrace_regex_lock);
2244}
2245
Steven Rostedt77a2b372008-05-12 21:20:45 +02002246/**
2247 * ftrace_set_filter - set a function to filter on in ftrace
2248 * @buf - the string that holds the function filter text.
2249 * @len - the length of the string.
2250 * @reset - non zero to reset all filters before applying this filter.
2251 *
2252 * Filters denote which functions should be enabled when tracing is enabled.
2253 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2254 */
Ingo Molnare309b412008-05-12 21:20:51 +02002255void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002256{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002257 ftrace_set_regex(buf, len, reset, 1);
2258}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002259
Steven Rostedt41c52c02008-05-22 11:46:33 -04002260/**
2261 * ftrace_set_notrace - set a function to not trace in ftrace
2262 * @buf - the string that holds the function notrace text.
2263 * @len - the length of the string.
2264 * @reset - non zero to reset all filters before applying this filter.
2265 *
2266 * Notrace Filters denote which functions should not be enabled when tracing
2267 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2268 * for tracing.
2269 */
2270void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2271{
2272 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002273}
2274
Steven Rostedt2af15d62009-05-28 13:37:24 -04002275/*
2276 * command line interface to allow users to set filters on boot up.
2277 */
2278#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2279static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2280static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2281
2282static int __init set_ftrace_notrace(char *str)
2283{
2284 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2285 return 1;
2286}
2287__setup("ftrace_notrace=", set_ftrace_notrace);
2288
2289static int __init set_ftrace_filter(char *str)
2290{
2291 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2292 return 1;
2293}
2294__setup("ftrace_filter=", set_ftrace_filter);
2295
Stefan Assmann369bc182009-10-12 22:17:21 +02002296#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002297static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002298static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2299
Stefan Assmann369bc182009-10-12 22:17:21 +02002300static int __init set_graph_function(char *str)
2301{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002302 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002303 return 1;
2304}
2305__setup("ftrace_graph_filter=", set_graph_function);
2306
2307static void __init set_ftrace_early_graph(char *buf)
2308{
2309 int ret;
2310 char *func;
2311
2312 while (buf) {
2313 func = strsep(&buf, ",");
2314 /* we allow only one expression at a time */
2315 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2316 func);
2317 if (ret)
2318 printk(KERN_DEBUG "ftrace: function %s not "
2319 "traceable\n", func);
2320 }
2321}
2322#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2323
Steven Rostedt2af15d62009-05-28 13:37:24 -04002324static void __init set_ftrace_early_filter(char *buf, int enable)
2325{
2326 char *func;
2327
2328 while (buf) {
2329 func = strsep(&buf, ",");
2330 ftrace_set_regex(func, strlen(func), 0, enable);
2331 }
2332}
2333
2334static void __init set_ftrace_early_filters(void)
2335{
2336 if (ftrace_filter_buf[0])
2337 set_ftrace_early_filter(ftrace_filter_buf, 1);
2338 if (ftrace_notrace_buf[0])
2339 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002340#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2341 if (ftrace_graph_buf[0])
2342 set_ftrace_early_graph(ftrace_graph_buf);
2343#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002344}
2345
Ingo Molnare309b412008-05-12 21:20:51 +02002346static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002347ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002348{
2349 struct seq_file *m = (struct seq_file *)file->private_data;
2350 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002351 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002352
Steven Rostedt41c52c02008-05-22 11:46:33 -04002353 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002354 if (file->f_mode & FMODE_READ) {
2355 iter = m->private;
2356
2357 seq_release(inode, file);
2358 } else
2359 iter = file->private_data;
2360
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002361 parser = &iter->parser;
2362 if (trace_parser_loaded(parser)) {
2363 parser->buffer[parser->idx] = 0;
2364 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002365 }
2366
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002367 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002368 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002369 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002370 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002371
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002372 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002373 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002374
Steven Rostedt41c52c02008-05-22 11:46:33 -04002375 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002376 return 0;
2377}
2378
Steven Rostedt41c52c02008-05-22 11:46:33 -04002379static int
2380ftrace_filter_release(struct inode *inode, struct file *file)
2381{
2382 return ftrace_regex_release(inode, file, 1);
2383}
2384
2385static int
2386ftrace_notrace_release(struct inode *inode, struct file *file)
2387{
2388 return ftrace_regex_release(inode, file, 0);
2389}
2390
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002391static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002392 .open = ftrace_avail_open,
2393 .read = seq_read,
2394 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002395 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002396};
2397
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002398static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302399 .open = ftrace_failures_open,
2400 .read = seq_read,
2401 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002402 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302403};
2404
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002405static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002406 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002407 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002408 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002409 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002410 .release = ftrace_filter_release,
2411};
2412
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002413static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002414 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002415 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002416 .write = ftrace_notrace_write,
2417 .llseek = ftrace_regex_lseek,
2418 .release = ftrace_notrace_release,
2419};
2420
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002421#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2422
2423static DEFINE_MUTEX(graph_lock);
2424
2425int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002426int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002427unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2428
2429static void *
Li Zefan85951842009-06-24 09:54:00 +08002430__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002431{
Li Zefan85951842009-06-24 09:54:00 +08002432 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002433 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002434 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002435}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002436
Li Zefan85951842009-06-24 09:54:00 +08002437static void *
2438g_next(struct seq_file *m, void *v, loff_t *pos)
2439{
2440 (*pos)++;
2441 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002442}
2443
2444static void *g_start(struct seq_file *m, loff_t *pos)
2445{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002446 mutex_lock(&graph_lock);
2447
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002448 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002449 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002450 return (void *)1;
2451
Li Zefan85951842009-06-24 09:54:00 +08002452 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002453}
2454
2455static void g_stop(struct seq_file *m, void *p)
2456{
2457 mutex_unlock(&graph_lock);
2458}
2459
2460static int g_show(struct seq_file *m, void *v)
2461{
2462 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002463
2464 if (!ptr)
2465 return 0;
2466
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002467 if (ptr == (unsigned long *)1) {
2468 seq_printf(m, "#### all functions enabled ####\n");
2469 return 0;
2470 }
2471
Steven Rostedtb375a112009-09-17 00:05:58 -04002472 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002473
2474 return 0;
2475}
2476
James Morris88e9d342009-09-22 16:43:43 -07002477static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002478 .start = g_start,
2479 .next = g_next,
2480 .stop = g_stop,
2481 .show = g_show,
2482};
2483
2484static int
2485ftrace_graph_open(struct inode *inode, struct file *file)
2486{
2487 int ret = 0;
2488
2489 if (unlikely(ftrace_disabled))
2490 return -ENODEV;
2491
2492 mutex_lock(&graph_lock);
2493 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002494 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002495 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002496 ftrace_graph_count = 0;
2497 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2498 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002499 mutex_unlock(&graph_lock);
2500
Li Zefana4ec5e02009-09-18 14:06:28 +08002501 if (file->f_mode & FMODE_READ)
2502 ret = seq_open(file, &ftrace_graph_seq_ops);
2503
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002504 return ret;
2505}
2506
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002507static int
Li Zefan87827112009-07-23 11:29:11 +08002508ftrace_graph_release(struct inode *inode, struct file *file)
2509{
2510 if (file->f_mode & FMODE_READ)
2511 seq_release(inode, file);
2512 return 0;
2513}
2514
2515static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002516ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002517{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002518 struct dyn_ftrace *rec;
2519 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002520 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002521 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002522 int type, not;
2523 char *search;
2524 bool exists;
2525 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002526
2527 if (ftrace_disabled)
2528 return -ENODEV;
2529
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002530 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002531 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002532 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2533 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002534
2535 search_len = strlen(search);
2536
Steven Rostedt52baf112009-02-14 01:15:39 -05002537 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002538 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002539
Steven Rostedt265c8312009-02-13 12:43:56 -05002540 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2541 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002542
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002543 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002544 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002545 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002546 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002547 if (array[i] == rec->ip) {
2548 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002549 break;
2550 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002551 }
2552
2553 if (!not) {
2554 fail = 0;
2555 if (!exists) {
2556 array[(*idx)++] = rec->ip;
2557 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2558 goto out;
2559 }
2560 } else {
2561 if (exists) {
2562 array[i] = array[--(*idx)];
2563 array[*idx] = 0;
2564 fail = 0;
2565 }
2566 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002567 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002568 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002569out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002570 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002571
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002572 if (fail)
2573 return -EINVAL;
2574
2575 ftrace_graph_filter_enabled = 1;
2576 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002577}
2578
2579static ssize_t
2580ftrace_graph_write(struct file *file, const char __user *ubuf,
2581 size_t cnt, loff_t *ppos)
2582{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002583 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002584 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002585
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002586 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002587 return 0;
2588
2589 mutex_lock(&graph_lock);
2590
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002591 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2592 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002593 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002594 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002595
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002596 read = trace_get_user(&parser, ubuf, cnt, ppos);
2597
Li Zefan4ba79782009-09-22 13:52:20 +08002598 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002599 parser.buffer[parser.idx] = 0;
2600
2601 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002602 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002603 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002604 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002605 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002606 }
2607
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002608 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002609
2610out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002611 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002612out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002613 mutex_unlock(&graph_lock);
2614
2615 return ret;
2616}
2617
2618static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002619 .open = ftrace_graph_open,
2620 .read = seq_read,
2621 .write = ftrace_graph_write,
2622 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623};
2624#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2625
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002626static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002627{
Steven Rostedt5072c592008-05-12 21:20:43 +02002628
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002629 trace_create_file("available_filter_functions", 0444,
2630 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002631
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002632 trace_create_file("failures", 0444,
2633 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302634
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002635 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2636 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002637
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002638 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002639 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002640
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002641#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002642 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002643 NULL,
2644 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002645#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2646
Steven Rostedt5072c592008-05-12 21:20:43 +02002647 return 0;
2648}
2649
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002650static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002651 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002652 unsigned long *end)
2653{
2654 unsigned long *p;
2655 unsigned long addr;
2656 unsigned long flags;
2657
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002658 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002659 p = start;
2660 while (p < end) {
2661 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002662 /*
2663 * Some architecture linkers will pad between
2664 * the different mcount_loc sections of different
2665 * object files to satisfy alignments.
2666 * Skip any NULL pointers.
2667 */
2668 if (!addr)
2669 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002670 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002671 }
2672
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002673 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002674 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002675 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002676 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002677 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002678
2679 return 0;
2680}
2681
Steven Rostedt93eb6772009-04-15 13:24:06 -04002682#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002683void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002684{
2685 struct dyn_ftrace *rec;
2686 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002687
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002688 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002689 return;
2690
2691 mutex_lock(&ftrace_lock);
2692 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002693 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002694 /*
2695 * rec->ip is changed in ftrace_free_rec()
2696 * It should not between s and e if record was freed.
2697 */
2698 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2699 ftrace_free_rec(rec);
2700 }
2701 } while_for_each_ftrace_rec();
2702 mutex_unlock(&ftrace_lock);
2703}
2704
2705static void ftrace_init_module(struct module *mod,
2706 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002707{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002708 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002709 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002710 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002711}
2712
Steven Rostedt93eb6772009-04-15 13:24:06 -04002713static int ftrace_module_notify(struct notifier_block *self,
2714 unsigned long val, void *data)
2715{
2716 struct module *mod = data;
2717
2718 switch (val) {
2719 case MODULE_STATE_COMING:
2720 ftrace_init_module(mod, mod->ftrace_callsites,
2721 mod->ftrace_callsites +
2722 mod->num_ftrace_callsites);
2723 break;
2724 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002725 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002726 break;
2727 }
2728
2729 return 0;
2730}
2731#else
2732static int ftrace_module_notify(struct notifier_block *self,
2733 unsigned long val, void *data)
2734{
2735 return 0;
2736}
2737#endif /* CONFIG_MODULES */
2738
2739struct notifier_block ftrace_module_nb = {
2740 .notifier_call = ftrace_module_notify,
2741 .priority = 0,
2742};
2743
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002744extern unsigned long __start_mcount_loc[];
2745extern unsigned long __stop_mcount_loc[];
2746
2747void __init ftrace_init(void)
2748{
2749 unsigned long count, addr, flags;
2750 int ret;
2751
2752 /* Keep the ftrace pointer to the stub */
2753 addr = (unsigned long)ftrace_stub;
2754
2755 local_irq_save(flags);
2756 ftrace_dyn_arch_init(&addr);
2757 local_irq_restore(flags);
2758
2759 /* ftrace_dyn_arch_init places the return code in addr */
2760 if (addr)
2761 goto failed;
2762
2763 count = __stop_mcount_loc - __start_mcount_loc;
2764
2765 ret = ftrace_dyn_table_alloc(count);
2766 if (ret)
2767 goto failed;
2768
2769 last_ftrace_enabled = ftrace_enabled = 1;
2770
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002771 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002772 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002773 __stop_mcount_loc);
2774
Steven Rostedt93eb6772009-04-15 13:24:06 -04002775 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002776 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002777 pr_warning("Failed to register trace ftrace module notifier\n");
2778
Steven Rostedt2af15d62009-05-28 13:37:24 -04002779 set_ftrace_early_filters();
2780
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002781 return;
2782 failed:
2783 ftrace_disabled = 1;
2784}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002785
Steven Rostedt3d083392008-05-12 21:20:42 +02002786#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002787
2788static int __init ftrace_nodyn_init(void)
2789{
2790 ftrace_enabled = 1;
2791 return 0;
2792}
2793device_initcall(ftrace_nodyn_init);
2794
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002795static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2796static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002797/* Keep as macros so we do not need to define the commands */
2798# define ftrace_startup(command) do { } while (0)
2799# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002800# define ftrace_startup_sysctl() do { } while (0)
2801# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002802#endif /* CONFIG_DYNAMIC_FTRACE */
2803
Steven Rostedte32d8952008-12-04 00:26:41 -05002804static void clear_ftrace_swapper(void)
2805{
2806 struct task_struct *p;
2807 int cpu;
2808
2809 get_online_cpus();
2810 for_each_online_cpu(cpu) {
2811 p = idle_task(cpu);
2812 clear_tsk_trace_trace(p);
2813 }
2814 put_online_cpus();
2815}
2816
2817static void set_ftrace_swapper(void)
2818{
2819 struct task_struct *p;
2820 int cpu;
2821
2822 get_online_cpus();
2823 for_each_online_cpu(cpu) {
2824 p = idle_task(cpu);
2825 set_tsk_trace_trace(p);
2826 }
2827 put_online_cpus();
2828}
2829
2830static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002831{
2832 struct task_struct *p;
2833
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002834 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002835 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002836 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002837 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002838 rcu_read_unlock();
2839
Steven Rostedte32d8952008-12-04 00:26:41 -05002840 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002841}
2842
Steven Rostedte32d8952008-12-04 00:26:41 -05002843static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002844{
2845 struct task_struct *p;
2846
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002847 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002848 do_each_pid_task(pid, PIDTYPE_PID, p) {
2849 set_tsk_trace_trace(p);
2850 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002851 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002852}
2853
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002854static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002855{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002856 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002857 clear_ftrace_swapper();
2858 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002859 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002860}
2861
2862static void set_ftrace_pid_task(struct pid *pid)
2863{
2864 if (pid == ftrace_swapper_pid)
2865 set_ftrace_swapper();
2866 else
2867 set_ftrace_pid(pid);
2868}
2869
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002870static int ftrace_pid_add(int p)
2871{
2872 struct pid *pid;
2873 struct ftrace_pid *fpid;
2874 int ret = -EINVAL;
2875
2876 mutex_lock(&ftrace_lock);
2877
2878 if (!p)
2879 pid = ftrace_swapper_pid;
2880 else
2881 pid = find_get_pid(p);
2882
2883 if (!pid)
2884 goto out;
2885
2886 ret = 0;
2887
2888 list_for_each_entry(fpid, &ftrace_pids, list)
2889 if (fpid->pid == pid)
2890 goto out_put;
2891
2892 ret = -ENOMEM;
2893
2894 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2895 if (!fpid)
2896 goto out_put;
2897
2898 list_add(&fpid->list, &ftrace_pids);
2899 fpid->pid = pid;
2900
2901 set_ftrace_pid_task(pid);
2902
2903 ftrace_update_pid_func();
2904 ftrace_startup_enable(0);
2905
2906 mutex_unlock(&ftrace_lock);
2907 return 0;
2908
2909out_put:
2910 if (pid != ftrace_swapper_pid)
2911 put_pid(pid);
2912
2913out:
2914 mutex_unlock(&ftrace_lock);
2915 return ret;
2916}
2917
2918static void ftrace_pid_reset(void)
2919{
2920 struct ftrace_pid *fpid, *safe;
2921
2922 mutex_lock(&ftrace_lock);
2923 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2924 struct pid *pid = fpid->pid;
2925
2926 clear_ftrace_pid_task(pid);
2927
2928 list_del(&fpid->list);
2929 kfree(fpid);
2930 }
2931
2932 ftrace_update_pid_func();
2933 ftrace_startup_enable(0);
2934
2935 mutex_unlock(&ftrace_lock);
2936}
2937
2938static void *fpid_start(struct seq_file *m, loff_t *pos)
2939{
2940 mutex_lock(&ftrace_lock);
2941
2942 if (list_empty(&ftrace_pids) && (!*pos))
2943 return (void *) 1;
2944
2945 return seq_list_start(&ftrace_pids, *pos);
2946}
2947
2948static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2949{
2950 if (v == (void *)1)
2951 return NULL;
2952
2953 return seq_list_next(v, &ftrace_pids, pos);
2954}
2955
2956static void fpid_stop(struct seq_file *m, void *p)
2957{
2958 mutex_unlock(&ftrace_lock);
2959}
2960
2961static int fpid_show(struct seq_file *m, void *v)
2962{
2963 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2964
2965 if (v == (void *)1) {
2966 seq_printf(m, "no pid\n");
2967 return 0;
2968 }
2969
2970 if (fpid->pid == ftrace_swapper_pid)
2971 seq_printf(m, "swapper tasks\n");
2972 else
2973 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2974
2975 return 0;
2976}
2977
2978static const struct seq_operations ftrace_pid_sops = {
2979 .start = fpid_start,
2980 .next = fpid_next,
2981 .stop = fpid_stop,
2982 .show = fpid_show,
2983};
2984
2985static int
2986ftrace_pid_open(struct inode *inode, struct file *file)
2987{
2988 int ret = 0;
2989
2990 if ((file->f_mode & FMODE_WRITE) &&
2991 (file->f_flags & O_TRUNC))
2992 ftrace_pid_reset();
2993
2994 if (file->f_mode & FMODE_READ)
2995 ret = seq_open(file, &ftrace_pid_sops);
2996
2997 return ret;
2998}
2999
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003000static ssize_t
3001ftrace_pid_write(struct file *filp, const char __user *ubuf,
3002 size_t cnt, loff_t *ppos)
3003{
Ingo Molnar457dc922009-11-23 11:03:28 +01003004 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003005 long val;
3006 int ret;
3007
3008 if (cnt >= sizeof(buf))
3009 return -EINVAL;
3010
3011 if (copy_from_user(&buf, ubuf, cnt))
3012 return -EFAULT;
3013
3014 buf[cnt] = 0;
3015
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003016 /*
3017 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3018 * to clean the filter quietly.
3019 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003020 tmp = strstrip(buf);
3021 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003022 return 1;
3023
Ingo Molnar457dc922009-11-23 11:03:28 +01003024 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003025 if (ret < 0)
3026 return ret;
3027
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003028 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003029
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003030 return ret ? ret : cnt;
3031}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003032
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003033static int
3034ftrace_pid_release(struct inode *inode, struct file *file)
3035{
3036 if (file->f_mode & FMODE_READ)
3037 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003038
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003039 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003040}
3041
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003042static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003043 .open = ftrace_pid_open,
3044 .write = ftrace_pid_write,
3045 .read = seq_read,
3046 .llseek = seq_lseek,
3047 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003048};
3049
3050static __init int ftrace_init_debugfs(void)
3051{
3052 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003053
3054 d_tracer = tracing_init_dentry();
3055 if (!d_tracer)
3056 return 0;
3057
3058 ftrace_init_dyn_debugfs(d_tracer);
3059
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003060 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3061 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003062
3063 ftrace_profile_debugfs(d_tracer);
3064
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003065 return 0;
3066}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003067fs_initcall(ftrace_init_debugfs);
3068
Steven Rostedt3d083392008-05-12 21:20:42 +02003069/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003070 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003071 *
3072 * This function should be used by panic code. It stops ftrace
3073 * but in a not so nice way. If you need to simply kill ftrace
3074 * from a non-atomic section, use ftrace_kill.
3075 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003076void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003077{
3078 ftrace_disabled = 1;
3079 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003080 clear_ftrace_function();
3081}
3082
3083/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003084 * register_ftrace_function - register a function for profiling
3085 * @ops - ops structure that holds the function for profiling.
3086 *
3087 * Register a function to be called by all functions in the
3088 * kernel.
3089 *
3090 * Note: @ops->func and all the functions it calls must be labeled
3091 * with "notrace", otherwise it will go into a
3092 * recursive loop.
3093 */
3094int register_ftrace_function(struct ftrace_ops *ops)
3095{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003096 int ret;
3097
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003098 if (unlikely(ftrace_disabled))
3099 return -1;
3100
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003101 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003102
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003103 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003104 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003105
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003106 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003107 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003108}
3109
3110/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003111 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003112 * @ops - ops structure that holds the function to unregister
3113 *
3114 * Unregister a function that was added to be called by ftrace profiling.
3115 */
3116int unregister_ftrace_function(struct ftrace_ops *ops)
3117{
3118 int ret;
3119
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003120 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003121 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003122 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003123 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003124
3125 return ret;
3126}
3127
Ingo Molnare309b412008-05-12 21:20:51 +02003128int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003129ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003130 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003131 loff_t *ppos)
3132{
3133 int ret;
3134
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003135 if (unlikely(ftrace_disabled))
3136 return -ENODEV;
3137
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003138 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003139
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003140 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003141
Li Zefana32c7762009-06-26 16:55:51 +08003142 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003143 goto out;
3144
Li Zefana32c7762009-06-26 16:55:51 +08003145 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003146
3147 if (ftrace_enabled) {
3148
3149 ftrace_startup_sysctl();
3150
3151 /* we are starting ftrace again */
3152 if (ftrace_list != &ftrace_list_end) {
3153 if (ftrace_list->next == &ftrace_list_end)
3154 ftrace_trace_function = ftrace_list->func;
3155 else
3156 ftrace_trace_function = ftrace_list_func;
3157 }
3158
3159 } else {
3160 /* stopping ftrace calls (just send to ftrace_stub) */
3161 ftrace_trace_function = ftrace_stub;
3162
3163 ftrace_shutdown_sysctl();
3164 }
3165
3166 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003167 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003168 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003169}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003170
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003171#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003172
Steven Rostedt597af812009-04-03 15:24:12 -04003173static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003174static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003175
Steven Rostedte49dc192008-12-02 23:50:05 -05003176int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3177{
3178 return 0;
3179}
3180
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003181/* The callbacks that hook a function */
3182trace_func_graph_ret_t ftrace_graph_return =
3183 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003184trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003185
3186/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3187static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3188{
3189 int i;
3190 int ret = 0;
3191 unsigned long flags;
3192 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3193 struct task_struct *g, *t;
3194
3195 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3196 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3197 * sizeof(struct ftrace_ret_stack),
3198 GFP_KERNEL);
3199 if (!ret_stack_list[i]) {
3200 start = 0;
3201 end = i;
3202 ret = -ENOMEM;
3203 goto free;
3204 }
3205 }
3206
3207 read_lock_irqsave(&tasklist_lock, flags);
3208 do_each_thread(g, t) {
3209 if (start == end) {
3210 ret = -EAGAIN;
3211 goto unlock;
3212 }
3213
3214 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003215 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003216 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003217 t->curr_ret_stack = -1;
3218 /* Make sure the tasks see the -1 first: */
3219 smp_wmb();
3220 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003221 }
3222 } while_each_thread(g, t);
3223
3224unlock:
3225 read_unlock_irqrestore(&tasklist_lock, flags);
3226free:
3227 for (i = start; i < end; i++)
3228 kfree(ret_stack_list[i]);
3229 return ret;
3230}
3231
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003232static void
3233ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3234 struct task_struct *next)
3235{
3236 unsigned long long timestamp;
3237 int index;
3238
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003239 /*
3240 * Does the user want to count the time a function was asleep.
3241 * If so, do not update the time stamps.
3242 */
3243 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3244 return;
3245
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003246 timestamp = trace_clock_local();
3247
3248 prev->ftrace_timestamp = timestamp;
3249
3250 /* only process tasks that we timestamped */
3251 if (!next->ftrace_timestamp)
3252 return;
3253
3254 /*
3255 * Update all the counters in next to make up for the
3256 * time next was sleeping.
3257 */
3258 timestamp -= next->ftrace_timestamp;
3259
3260 for (index = next->curr_ret_stack; index >= 0; index--)
3261 next->ret_stack[index].calltime += timestamp;
3262}
3263
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003264/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003265static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003266{
3267 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003268 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003269
3270 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3271 sizeof(struct ftrace_ret_stack *),
3272 GFP_KERNEL);
3273
3274 if (!ret_stack_list)
3275 return -ENOMEM;
3276
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003277 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003278 for_each_online_cpu(cpu) {
3279 if (!idle_task(cpu)->ret_stack)
3280 ftrace_graph_init_task(idle_task(cpu));
3281 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003282
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003283 do {
3284 ret = alloc_retstack_tasklist(ret_stack_list);
3285 } while (ret == -EAGAIN);
3286
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003287 if (!ret) {
3288 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3289 if (ret)
3290 pr_info("ftrace_graph: Couldn't activate tracepoint"
3291 " probe to kernel_sched_switch\n");
3292 }
3293
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003294 kfree(ret_stack_list);
3295 return ret;
3296}
3297
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003298/*
3299 * Hibernation protection.
3300 * The state of the current task is too much unstable during
3301 * suspend/restore to disk. We want to protect against that.
3302 */
3303static int
3304ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3305 void *unused)
3306{
3307 switch (state) {
3308 case PM_HIBERNATION_PREPARE:
3309 pause_graph_tracing();
3310 break;
3311
3312 case PM_POST_HIBERNATION:
3313 unpause_graph_tracing();
3314 break;
3315 }
3316 return NOTIFY_DONE;
3317}
3318
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003319int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3320 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003321{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003322 int ret = 0;
3323
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003324 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003325
Steven Rostedt05ce5812009-03-24 00:18:31 -04003326 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003327 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003328 ret = -EBUSY;
3329 goto out;
3330 }
3331
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003332 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3333 register_pm_notifier(&ftrace_suspend_notifier);
3334
Steven Rostedt597af812009-04-03 15:24:12 -04003335 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003336 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003337 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003338 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003339 goto out;
3340 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003341
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003342 ftrace_graph_return = retfunc;
3343 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003344
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003345 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003346
3347out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003348 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003349 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003350}
3351
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003352void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003353{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003354 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003355
Steven Rostedt597af812009-04-03 15:24:12 -04003356 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003357 goto out;
3358
Steven Rostedt597af812009-04-03 15:24:12 -04003359 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003360 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003361 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003362 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003363 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003364 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003365
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003366 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003367 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003368}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003369
3370/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003371void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003372{
Steven Rostedt84047e32009-06-02 16:51:55 -04003373 /* Make sure we do not use the parent ret_stack */
3374 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003375 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003376
Steven Rostedt597af812009-04-03 15:24:12 -04003377 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003378 struct ftrace_ret_stack *ret_stack;
3379
3380 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003381 * sizeof(struct ftrace_ret_stack),
3382 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003383 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003384 return;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003385 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003386 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003387 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003388 /* make curr_ret_stack visable before we add the ret_stack */
3389 smp_wmb();
3390 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003391 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003392}
3393
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003394void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003395{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003396 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3397
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003398 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003399 /* NULL must become visible to IRQs before we free it: */
3400 barrier();
3401
3402 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003403}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003404
3405void ftrace_graph_stop(void)
3406{
3407 ftrace_stop();
3408}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003409#endif