blob: 2404b59b3097e09f1db4c15a1a8cf5240dbe4c51 [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 Rostedt6912896e2008-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;
267#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400268};
269
270struct ftrace_profile_page {
271 struct ftrace_profile_page *next;
272 unsigned long index;
273 struct ftrace_profile records[];
274};
275
Steven Rostedtcafb1682009-03-24 20:50:39 -0400276struct ftrace_profile_stat {
277 atomic_t disabled;
278 struct hlist_head *hash;
279 struct ftrace_profile_page *pages;
280 struct ftrace_profile_page *start;
281 struct tracer_stat stat;
282};
283
Steven Rostedt493762f2009-03-23 17:12:36 -0400284#define PROFILE_RECORDS_SIZE \
285 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
286
287#define PROFILES_PER_PAGE \
288 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
289
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400290static int ftrace_profile_bits __read_mostly;
291static int ftrace_profile_enabled __read_mostly;
292
293/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400294static DEFINE_MUTEX(ftrace_profile_lock);
295
Steven Rostedtcafb1682009-03-24 20:50:39 -0400296static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400297
298#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
299
Steven Rostedt493762f2009-03-23 17:12:36 -0400300static void *
301function_stat_next(void *v, int idx)
302{
303 struct ftrace_profile *rec = v;
304 struct ftrace_profile_page *pg;
305
306 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
307
308 again:
Li Zefan0296e422009-06-26 11:15:37 +0800309 if (idx != 0)
310 rec++;
311
Steven Rostedt493762f2009-03-23 17:12:36 -0400312 if ((void *)rec >= (void *)&pg->records[pg->index]) {
313 pg = pg->next;
314 if (!pg)
315 return NULL;
316 rec = &pg->records[0];
317 if (!rec->counter)
318 goto again;
319 }
320
321 return rec;
322}
323
324static void *function_stat_start(struct tracer_stat *trace)
325{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400326 struct ftrace_profile_stat *stat =
327 container_of(trace, struct ftrace_profile_stat, stat);
328
329 if (!stat || !stat->start)
330 return NULL;
331
332 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400333}
334
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400335#ifdef CONFIG_FUNCTION_GRAPH_TRACER
336/* function graph compares on total time */
337static int function_stat_cmp(void *p1, void *p2)
338{
339 struct ftrace_profile *a = p1;
340 struct ftrace_profile *b = p2;
341
342 if (a->time < b->time)
343 return -1;
344 if (a->time > b->time)
345 return 1;
346 else
347 return 0;
348}
349#else
350/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400351static int function_stat_cmp(void *p1, void *p2)
352{
353 struct ftrace_profile *a = p1;
354 struct ftrace_profile *b = p2;
355
356 if (a->counter < b->counter)
357 return -1;
358 if (a->counter > b->counter)
359 return 1;
360 else
361 return 0;
362}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400363#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400364
365static int function_stat_headers(struct seq_file *m)
366{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400367#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400368 seq_printf(m, " Function "
369 "Hit Time Avg\n"
370 " -------- "
371 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400372#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400373 seq_printf(m, " Function Hit\n"
374 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400375#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400376 return 0;
377}
378
379static int function_stat_show(struct seq_file *m, void *v)
380{
381 struct ftrace_profile *rec = v;
382 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400383#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400384 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400385 static struct trace_seq s;
386 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400387#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400388
389 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400390 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400391
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400392#ifdef CONFIG_FUNCTION_GRAPH_TRACER
393 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400394 avg = rec->time;
395 do_div(avg, rec->counter);
396
397 mutex_lock(&mutex);
398 trace_seq_init(&s);
399 trace_print_graph_duration(rec->time, &s);
400 trace_seq_puts(&s, " ");
401 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400402 trace_print_seq(m, &s);
403 mutex_unlock(&mutex);
404#endif
405 seq_putc(m, '\n');
406
Steven Rostedt493762f2009-03-23 17:12:36 -0400407 return 0;
408}
409
Steven Rostedtcafb1682009-03-24 20:50:39 -0400410static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400411{
412 struct ftrace_profile_page *pg;
413
Steven Rostedtcafb1682009-03-24 20:50:39 -0400414 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400415
416 while (pg) {
417 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
418 pg->index = 0;
419 pg = pg->next;
420 }
421
Steven Rostedtcafb1682009-03-24 20:50:39 -0400422 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400423 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
424}
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400427{
428 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400429 int functions;
430 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400431 int i;
432
433 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400434 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400435 return 0;
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
438 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400439 return -ENOMEM;
440
Steven Rostedt318e0a72009-03-25 20:06:34 -0400441#ifdef CONFIG_DYNAMIC_FTRACE
442 functions = ftrace_update_tot_cnt;
443#else
444 /*
445 * We do not know the number of functions that exist because
446 * dynamic tracing is what counts them. With past experience
447 * we have around 20K functions. That should be more than enough.
448 * It is highly unlikely we will execute every function in
449 * the kernel.
450 */
451 functions = 20000;
452#endif
453
Steven Rostedtcafb1682009-03-24 20:50:39 -0400454 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400455
Steven Rostedt318e0a72009-03-25 20:06:34 -0400456 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
457
458 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400459 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400460 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400461 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 pg = pg->next;
463 }
464
465 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400466
467 out_free:
468 pg = stat->start;
469 while (pg) {
470 unsigned long tmp = (unsigned long)pg;
471
472 pg = pg->next;
473 free_page(tmp);
474 }
475
476 free_page((unsigned long)stat->pages);
477 stat->pages = NULL;
478 stat->start = NULL;
479
480 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400481}
482
Steven Rostedtcafb1682009-03-24 20:50:39 -0400483static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400484{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400485 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400486 int size;
487
Steven Rostedtcafb1682009-03-24 20:50:39 -0400488 stat = &per_cpu(ftrace_profile_stats, cpu);
489
490 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400491 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400493 return 0;
494 }
495
496 /*
497 * We are profiling all functions, but usually only a few thousand
498 * functions are hit. We'll make a hash of 1024 items.
499 */
500 size = FTRACE_PROFILE_HASH_SIZE;
501
Steven Rostedtcafb1682009-03-24 20:50:39 -0400502 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400503
Steven Rostedtcafb1682009-03-24 20:50:39 -0400504 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400505 return -ENOMEM;
506
Steven Rostedtcafb1682009-03-24 20:50:39 -0400507 if (!ftrace_profile_bits) {
508 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400509
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510 for (; size; size >>= 1)
511 ftrace_profile_bits++;
512 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400513
Steven Rostedt318e0a72009-03-25 20:06:34 -0400514 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 if (ftrace_profile_pages_init(stat) < 0) {
516 kfree(stat->hash);
517 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400518 return -ENOMEM;
519 }
520
521 return 0;
522}
523
Steven Rostedtcafb1682009-03-24 20:50:39 -0400524static int ftrace_profile_init(void)
525{
526 int cpu;
527 int ret = 0;
528
529 for_each_online_cpu(cpu) {
530 ret = ftrace_profile_init_cpu(cpu);
531 if (ret)
532 break;
533 }
534
535 return ret;
536}
537
Steven Rostedt493762f2009-03-23 17:12:36 -0400538/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400539static struct ftrace_profile *
540ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400541{
542 struct ftrace_profile *rec;
543 struct hlist_head *hhd;
544 struct hlist_node *n;
545 unsigned long key;
546
547 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400548 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400549
550 if (hlist_empty(hhd))
551 return NULL;
552
553 hlist_for_each_entry_rcu(rec, n, hhd, node) {
554 if (rec->ip == ip)
555 return rec;
556 }
557
558 return NULL;
559}
560
Steven Rostedtcafb1682009-03-24 20:50:39 -0400561static void ftrace_add_profile(struct ftrace_profile_stat *stat,
562 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400563{
564 unsigned long key;
565
566 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400567 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400568}
569
Steven Rostedt318e0a72009-03-25 20:06:34 -0400570/*
571 * The memory is already allocated, this simply finds a new record to use.
572 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400573static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400574ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400575{
576 struct ftrace_profile *rec = NULL;
577
Steven Rostedt318e0a72009-03-25 20:06:34 -0400578 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400579 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400580 goto out;
581
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400583 * Try to find the function again since an NMI
584 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 if (stat->pages->index == PROFILES_PER_PAGE) {
591 if (!stat->pages->next)
592 goto out;
593 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 }
595
Steven Rostedtcafb1682009-03-24 20:50:39 -0400596 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400599
Steven Rostedt493762f2009-03-23 17:12:36 -0400600 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400602
603 return rec;
604}
605
Steven Rostedt493762f2009-03-23 17:12:36 -0400606static void
607function_profile_call(unsigned long ip, unsigned long parent_ip)
608{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 struct ftrace_profile *rec;
611 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400612
613 if (!ftrace_profile_enabled)
614 return;
615
Steven Rostedt493762f2009-03-23 17:12:36 -0400616 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617
618 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400619 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400620 goto out;
621
622 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400625 if (!rec)
626 goto out;
627 }
628
629 rec->counter++;
630 out:
631 local_irq_restore(flags);
632}
633
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634#ifdef CONFIG_FUNCTION_GRAPH_TRACER
635static int profile_graph_entry(struct ftrace_graph_ent *trace)
636{
637 function_profile_call(trace->func, 0);
638 return 1;
639}
640
641static void profile_graph_return(struct ftrace_graph_ret *trace)
642{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400644 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400645 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647
648 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400650 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400651 goto out;
652
Steven Rostedta2a16d62009-03-24 23:17:58 -0400653 calltime = trace->rettime - trace->calltime;
654
655 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
656 int index;
657
658 index = trace->depth;
659
660 /* Append this call time to the parent time to subtract */
661 if (index)
662 current->ret_stack[index - 1].subtime += calltime;
663
664 if (current->ret_stack[index].subtime < calltime)
665 calltime -= current->ret_stack[index].subtime;
666 else
667 calltime = 0;
668 }
669
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400671 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400672 rec->time += calltime;
673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400675 local_irq_restore(flags);
676}
677
678static int register_ftrace_profiler(void)
679{
680 return register_ftrace_graph(&profile_graph_return,
681 &profile_graph_entry);
682}
683
684static void unregister_ftrace_profiler(void)
685{
686 unregister_ftrace_graph();
687}
688#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400689static struct ftrace_ops ftrace_profile_ops __read_mostly =
690{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400691 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400692};
693
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400694static int register_ftrace_profiler(void)
695{
696 return register_ftrace_function(&ftrace_profile_ops);
697}
698
699static void unregister_ftrace_profiler(void)
700{
701 unregister_ftrace_function(&ftrace_profile_ops);
702}
703#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
704
Steven Rostedt493762f2009-03-23 17:12:36 -0400705static ssize_t
706ftrace_profile_write(struct file *filp, const char __user *ubuf,
707 size_t cnt, loff_t *ppos)
708{
709 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400710 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 int ret;
712
713 if (cnt >= sizeof(buf))
714 return -EINVAL;
715
716 if (copy_from_user(&buf, ubuf, cnt))
717 return -EFAULT;
718
719 buf[cnt] = 0;
720
721 ret = strict_strtoul(buf, 10, &val);
722 if (ret < 0)
723 return ret;
724
725 val = !!val;
726
727 mutex_lock(&ftrace_profile_lock);
728 if (ftrace_profile_enabled ^ val) {
729 if (val) {
730 ret = ftrace_profile_init();
731 if (ret < 0) {
732 cnt = ret;
733 goto out;
734 }
735
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400736 ret = register_ftrace_profiler();
737 if (ret < 0) {
738 cnt = ret;
739 goto out;
740 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400741 ftrace_profile_enabled = 1;
742 } else {
743 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400744 /*
745 * unregister_ftrace_profiler calls stop_machine
746 * so this acts like an synchronize_sched.
747 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400748 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 }
750 }
751 out:
752 mutex_unlock(&ftrace_profile_lock);
753
Jiri Olsacf8517c2009-10-23 19:36:16 -0400754 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400755
756 return cnt;
757}
758
759static ssize_t
760ftrace_profile_read(struct file *filp, char __user *ubuf,
761 size_t cnt, loff_t *ppos)
762{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400763 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 int r;
765
766 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
767 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
768}
769
770static const struct file_operations ftrace_profile_fops = {
771 .open = tracing_open_generic,
772 .read = ftrace_profile_read,
773 .write = ftrace_profile_write,
774};
775
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776/* used to initialize the real stat files */
777static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400778 .name = "functions",
779 .stat_start = function_stat_start,
780 .stat_next = function_stat_next,
781 .stat_cmp = function_stat_cmp,
782 .stat_headers = function_stat_headers,
783 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784};
785
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400786static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400787{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400793
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794 for_each_possible_cpu(cpu) {
795 stat = &per_cpu(ftrace_profile_stats, cpu);
796
797 /* allocate enough for function name + cpu number */
798 name = kmalloc(32, GFP_KERNEL);
799 if (!name) {
800 /*
801 * The files created are permanent, if something happens
802 * we still do not free memory.
803 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 WARN(1,
805 "Could not allocate stat file for cpu %d\n",
806 cpu);
807 return;
808 }
809 stat->stat = function_stats;
810 snprintf(name, 32, "function%d", cpu);
811 stat->stat.name = name;
812 ret = register_stat_tracer(&stat->stat);
813 if (ret) {
814 WARN(1,
815 "Could not register function stat for cpu %d\n",
816 cpu);
817 kfree(name);
818 return;
819 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 }
821
822 entry = debugfs_create_file("function_profile_enabled", 0644,
823 d_tracer, NULL, &ftrace_profile_fops);
824 if (!entry)
825 pr_warning("Could not create debugfs "
826 "'function_profile_enabled' entry\n");
827}
828
829#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400830static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400831{
832}
833#endif /* CONFIG_FUNCTION_PROFILER */
834
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100835static struct pid * const ftrace_swapper_pid = &init_struct_pid;
836
Steven Rostedt3d083392008-05-12 21:20:42 +0200837#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100838
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400839#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400840# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400841#endif
842
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500843static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
844
Steven Rostedtb6887d72009-02-17 12:32:04 -0500845struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500846 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500847 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500848 unsigned long flags;
849 unsigned long ip;
850 void *data;
851 struct rcu_head rcu;
852};
853
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200854enum {
855 FTRACE_ENABLE_CALLS = (1 << 0),
856 FTRACE_DISABLE_CALLS = (1 << 1),
857 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
858 FTRACE_ENABLE_MCOUNT = (1 << 3),
859 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500860 FTRACE_START_FUNC_RET = (1 << 5),
861 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200862};
863
Steven Rostedt5072c592008-05-12 21:20:43 +0200864static int ftrace_filtered;
865
Lai Jiangshane94142a2009-03-13 17:51:27 +0800866static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200867
Steven Rostedt41c52c02008-05-22 11:46:33 -0400868static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200869
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200870struct ftrace_page {
871 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500872 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200873 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700874};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200875
876#define ENTRIES_PER_PAGE \
877 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
878
879/* estimate from running different kernels */
880#define NR_TO_INIT 10000
881
882static struct ftrace_page *ftrace_pages_start;
883static struct ftrace_page *ftrace_pages;
884
Steven Rostedt37ad5082008-05-12 21:20:48 +0200885static struct dyn_ftrace *ftrace_free_records;
886
Steven Rostedt265c8312009-02-13 12:43:56 -0500887/*
888 * This is a double for. Do not use 'break' to break out of the loop,
889 * you must use a goto.
890 */
891#define do_for_each_ftrace_rec(pg, rec) \
892 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
893 int _____i; \
894 for (_____i = 0; _____i < pg->index; _____i++) { \
895 rec = &pg->records[_____i];
896
897#define while_for_each_ftrace_rec() \
898 } \
899 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530900
Ingo Molnare309b412008-05-12 21:20:51 +0200901static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200902{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800903 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200904 ftrace_free_records = rec;
905 rec->flags |= FTRACE_FL_FREE;
906}
907
Ingo Molnare309b412008-05-12 21:20:51 +0200908static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200909{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200910 struct dyn_ftrace *rec;
911
912 /* First check for freed records */
913 if (ftrace_free_records) {
914 rec = ftrace_free_records;
915
Steven Rostedt37ad5082008-05-12 21:20:48 +0200916 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400917 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200918 ftrace_free_records = NULL;
919 return NULL;
920 }
921
Lai Jiangshanee000b72009-03-24 13:38:06 +0800922 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200923 memset(rec, 0, sizeof(*rec));
924 return rec;
925 }
926
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200927 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400928 if (!ftrace_pages->next) {
929 /* allocate another page */
930 ftrace_pages->next =
931 (void *)get_zeroed_page(GFP_KERNEL);
932 if (!ftrace_pages->next)
933 return NULL;
934 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200935 ftrace_pages = ftrace_pages->next;
936 }
937
938 return &ftrace_pages->records[ftrace_pages->index++];
939}
940
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400941static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200942ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200943{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400944 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200945
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800946 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400947 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200948
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400949 rec = ftrace_alloc_dyn_node(ip);
950 if (!rec)
951 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200952
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400953 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800954 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800955 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200956
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400957 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200958}
959
Steven Rostedt05736a42008-09-22 14:55:47 -0700960static void print_ip_ins(const char *fmt, unsigned char *p)
961{
962 int i;
963
964 printk(KERN_CONT "%s", fmt);
965
966 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
967 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
968}
969
Steven Rostedt31e88902008-11-14 16:21:19 -0800970static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800971{
972 switch (failed) {
973 case -EFAULT:
974 FTRACE_WARN_ON_ONCE(1);
975 pr_info("ftrace faulted on modifying ");
976 print_ip_sym(ip);
977 break;
978 case -EINVAL:
979 FTRACE_WARN_ON_ONCE(1);
980 pr_info("ftrace failed to modify ");
981 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800982 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800983 printk(KERN_CONT "\n");
984 break;
985 case -EPERM:
986 FTRACE_WARN_ON_ONCE(1);
987 pr_info("ftrace faulted on writing ");
988 print_ip_sym(ip);
989 break;
990 default:
991 FTRACE_WARN_ON_ONCE(1);
992 pr_info("ftrace faulted on unknown error ");
993 print_ip_sym(ip);
994 }
995}
996
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200997
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500998/* Return 1 if the address range is reserved for ftrace */
999int ftrace_text_reserved(void *start, void *end)
1000{
1001 struct dyn_ftrace *rec;
1002 struct ftrace_page *pg;
1003
1004 do_for_each_ftrace_rec(pg, rec) {
1005 if (rec->ip <= (unsigned long)end &&
1006 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1007 return 1;
1008 } while_for_each_ftrace_rec();
1009 return 0;
1010}
1011
1012
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301013static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001014__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001015{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001016 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001017 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001018
Shaohua Lif0001202009-01-09 11:29:42 +08001019 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001020
Steven Rostedt982c3502008-11-15 16:31:41 -05001021 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001022 * If this record is not to be traced or we want to disable it,
1023 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001024 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001025 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001026 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001027 * If we want to enable it and filtering is on, enable it only if
1028 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001029 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001030 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1031 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1032 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001033 }
1034
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001035 /* If the state of this record hasn't changed, then do nothing */
1036 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1037 return 0;
1038
1039 if (flag) {
1040 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001041 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001042 }
1043
1044 rec->flags &= ~FTRACE_FL_ENABLED;
1045 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001046}
1047
1048static void ftrace_replace_code(int enable)
1049{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001050 struct dyn_ftrace *rec;
1051 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001052 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001053
Steven Rostedt265c8312009-02-13 12:43:56 -05001054 do_for_each_ftrace_rec(pg, rec) {
1055 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001056 * Skip over free records, records that have
1057 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001058 */
1059 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001060 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001061 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001062 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001063
Steven Rostedt265c8312009-02-13 12:43:56 -05001064 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001065 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001066 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001067 ftrace_bug(failed, rec->ip);
1068 /* Stop processing */
1069 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001070 }
1071 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001072}
1073
Ingo Molnare309b412008-05-12 21:20:51 +02001074static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001075ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001076{
1077 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001078 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001079
1080 ip = rec->ip;
1081
Shaohua Li25aac9d2009-01-09 11:29:40 +08001082 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001083 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001084 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001085 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301086 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001087 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301088 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001089}
1090
Steven Rostedt000ab692009-02-17 13:35:06 -05001091/*
1092 * archs can override this function if they must do something
1093 * before the modifying code is performed.
1094 */
1095int __weak ftrace_arch_code_modify_prepare(void)
1096{
1097 return 0;
1098}
1099
1100/*
1101 * archs can override this function if they must do something
1102 * after the modifying code is performed.
1103 */
1104int __weak ftrace_arch_code_modify_post_process(void)
1105{
1106 return 0;
1107}
1108
Ingo Molnare309b412008-05-12 21:20:51 +02001109static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001110{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001111 int *command = data;
1112
Steven Rostedta3583242008-11-11 15:01:42 -05001113 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001114 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001115 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001116 ftrace_replace_code(0);
1117
1118 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1119 ftrace_update_ftrace_func(ftrace_trace_function);
1120
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001121 if (*command & FTRACE_START_FUNC_RET)
1122 ftrace_enable_ftrace_graph_caller();
1123 else if (*command & FTRACE_STOP_FUNC_RET)
1124 ftrace_disable_ftrace_graph_caller();
1125
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001126 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001127}
1128
Ingo Molnare309b412008-05-12 21:20:51 +02001129static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001130{
Steven Rostedt000ab692009-02-17 13:35:06 -05001131 int ret;
1132
1133 ret = ftrace_arch_code_modify_prepare();
1134 FTRACE_WARN_ON(ret);
1135 if (ret)
1136 return;
1137
Rusty Russell784e2d72008-07-28 12:16:31 -05001138 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001139
1140 ret = ftrace_arch_code_modify_post_process();
1141 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001142}
1143
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001144static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001145static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001146
1147static void ftrace_startup_enable(int command)
1148{
1149 if (saved_ftrace_func != ftrace_trace_function) {
1150 saved_ftrace_func = ftrace_trace_function;
1151 command |= FTRACE_UPDATE_TRACE_FUNC;
1152 }
1153
1154 if (!command || !ftrace_enabled)
1155 return;
1156
1157 ftrace_run_update_code(command);
1158}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001159
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001160static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001161{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001162 if (unlikely(ftrace_disabled))
1163 return;
1164
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001165 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001166 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001167
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001168 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001169}
1170
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001171static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001172{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001173 if (unlikely(ftrace_disabled))
1174 return;
1175
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001176 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001177 /*
1178 * Just warn in case of unbalance, no need to kill ftrace, it's not
1179 * critical but the ftrace_call callers may be never nopped again after
1180 * further ftrace uses.
1181 */
1182 WARN_ON_ONCE(ftrace_start_up < 0);
1183
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001184 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001185 command |= FTRACE_DISABLE_CALLS;
1186
1187 if (saved_ftrace_func != ftrace_trace_function) {
1188 saved_ftrace_func = ftrace_trace_function;
1189 command |= FTRACE_UPDATE_TRACE_FUNC;
1190 }
1191
1192 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001193 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001194
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001195 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001196}
1197
Ingo Molnare309b412008-05-12 21:20:51 +02001198static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001199{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001200 int command = FTRACE_ENABLE_MCOUNT;
1201
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001202 if (unlikely(ftrace_disabled))
1203 return;
1204
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001205 /* Force update next time */
1206 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001207 /* ftrace_start_up is true if we want ftrace running */
1208 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001209 command |= FTRACE_ENABLE_CALLS;
1210
1211 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001212}
1213
Ingo Molnare309b412008-05-12 21:20:51 +02001214static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001215{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001216 int command = FTRACE_DISABLE_MCOUNT;
1217
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001218 if (unlikely(ftrace_disabled))
1219 return;
1220
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001221 /* ftrace_start_up is true if ftrace is running */
1222 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001223 command |= FTRACE_DISABLE_CALLS;
1224
1225 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001226}
1227
Steven Rostedt3d083392008-05-12 21:20:42 +02001228static cycle_t ftrace_update_time;
1229static unsigned long ftrace_update_cnt;
1230unsigned long ftrace_update_tot_cnt;
1231
Steven Rostedt31e88902008-11-14 16:21:19 -08001232static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001233{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001234 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301235 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001236
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001237 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001238 ftrace_update_cnt = 0;
1239
Lai Jiangshane94142a2009-03-13 17:51:27 +08001240 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301241
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001242 /* If something went wrong, bail without enabling anything */
1243 if (unlikely(ftrace_disabled))
1244 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001245
Lai Jiangshane94142a2009-03-13 17:51:27 +08001246 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001247 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001248 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301249
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001250 /*
1251 * Do the initial record convertion from mcount jump
1252 * to the NOP instructions.
1253 */
1254 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001255 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001256 continue;
1257 }
1258
1259 p->flags |= FTRACE_FL_CONVERTED;
1260 ftrace_update_cnt++;
1261
1262 /*
1263 * If the tracing is enabled, go ahead and enable the record.
1264 *
1265 * The reason not to enable the record immediatelly is the
1266 * inherent check of ftrace_make_nop/ftrace_make_call for
1267 * correct previous instructions. Making first the NOP
1268 * conversion puts the module to the correct state, thus
1269 * passing the ftrace_make_call check.
1270 */
1271 if (ftrace_start_up) {
1272 int failed = __ftrace_replace_code(p, 1);
1273 if (failed) {
1274 ftrace_bug(failed, p->ip);
1275 ftrace_free_rec(p);
1276 }
1277 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001278 }
1279
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001280 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001281 ftrace_update_time = stop - start;
1282 ftrace_update_tot_cnt += ftrace_update_cnt;
1283
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001284 return 0;
1285}
1286
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001287static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001288{
1289 struct ftrace_page *pg;
1290 int cnt;
1291 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001292
1293 /* allocate a few pages */
1294 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1295 if (!ftrace_pages_start)
1296 return -1;
1297
1298 /*
1299 * Allocate a few more pages.
1300 *
1301 * TODO: have some parser search vmlinux before
1302 * final linking to find all calls to ftrace.
1303 * Then we can:
1304 * a) know how many pages to allocate.
1305 * and/or
1306 * b) set up the table then.
1307 *
1308 * The dynamic code is still necessary for
1309 * modules.
1310 */
1311
1312 pg = ftrace_pages = ftrace_pages_start;
1313
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001314 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001315 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001316 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001317
1318 for (i = 0; i < cnt; i++) {
1319 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1320
1321 /* If we fail, we'll try later anyway */
1322 if (!pg->next)
1323 break;
1324
1325 pg = pg->next;
1326 }
1327
1328 return 0;
1329}
1330
Steven Rostedt5072c592008-05-12 21:20:43 +02001331enum {
1332 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001333 FTRACE_ITER_NOTRACE = (1 << 1),
1334 FTRACE_ITER_FAILURES = (1 << 2),
1335 FTRACE_ITER_PRINTALL = (1 << 3),
1336 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001337};
1338
1339#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1340
1341struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001342 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001343 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001344 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001345 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001346 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001347};
1348
Ingo Molnare309b412008-05-12 21:20:51 +02001349static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001350t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1351{
1352 struct ftrace_iterator *iter = m->private;
1353 struct hlist_node *hnd = v;
1354 struct hlist_head *hhd;
1355
1356 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1357
1358 (*pos)++;
1359
1360 retry:
1361 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1362 return NULL;
1363
1364 hhd = &ftrace_func_hash[iter->hidx];
1365
1366 if (hlist_empty(hhd)) {
1367 iter->hidx++;
1368 hnd = NULL;
1369 goto retry;
1370 }
1371
1372 if (!hnd)
1373 hnd = hhd->first;
1374 else {
1375 hnd = hnd->next;
1376 if (!hnd) {
1377 iter->hidx++;
1378 goto retry;
1379 }
1380 }
1381
1382 return hnd;
1383}
1384
1385static void *t_hash_start(struct seq_file *m, loff_t *pos)
1386{
1387 struct ftrace_iterator *iter = m->private;
1388 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001389 loff_t l;
1390
1391 if (!(iter->flags & FTRACE_ITER_HASH))
1392 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001393
1394 iter->flags |= FTRACE_ITER_HASH;
1395
Li Zefand82d6242009-06-24 09:54:54 +08001396 iter->hidx = 0;
1397 for (l = 0; l <= *pos; ) {
1398 p = t_hash_next(m, p, &l);
1399 if (!p)
1400 break;
1401 }
1402 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001403}
1404
1405static int t_hash_show(struct seq_file *m, void *v)
1406{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001407 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001408 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001409
Steven Rostedtb6887d72009-02-17 12:32:04 -05001410 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001411
Steven Rostedt809dcf22009-02-16 23:06:01 -05001412 if (rec->ops->print)
1413 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1414
Steven Rostedtb375a112009-09-17 00:05:58 -04001415 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001416
1417 if (rec->data)
1418 seq_printf(m, ":%p", rec->data);
1419 seq_putc(m, '\n');
1420
1421 return 0;
1422}
1423
1424static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001425t_next(struct seq_file *m, void *v, loff_t *pos)
1426{
1427 struct ftrace_iterator *iter = m->private;
1428 struct dyn_ftrace *rec = NULL;
1429
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001430 if (iter->flags & FTRACE_ITER_HASH)
1431 return t_hash_next(m, v, pos);
1432
Steven Rostedt5072c592008-05-12 21:20:43 +02001433 (*pos)++;
1434
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001435 if (iter->flags & FTRACE_ITER_PRINTALL)
1436 return NULL;
1437
Steven Rostedt5072c592008-05-12 21:20:43 +02001438 retry:
1439 if (iter->idx >= iter->pg->index) {
1440 if (iter->pg->next) {
1441 iter->pg = iter->pg->next;
1442 iter->idx = 0;
1443 goto retry;
1444 }
1445 } else {
1446 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001447 if ((rec->flags & FTRACE_FL_FREE) ||
1448
1449 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301450 (rec->flags & FTRACE_FL_FAILED)) ||
1451
1452 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001453 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301454
Steven Rostedt0183fb12008-11-07 22:36:02 -05001455 ((iter->flags & FTRACE_ITER_FILTER) &&
1456 !(rec->flags & FTRACE_FL_FILTER)) ||
1457
Steven Rostedt41c52c02008-05-22 11:46:33 -04001458 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1459 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001460 rec = NULL;
1461 goto retry;
1462 }
1463 }
1464
Steven Rostedt5072c592008-05-12 21:20:43 +02001465 return rec;
1466}
1467
1468static void *t_start(struct seq_file *m, loff_t *pos)
1469{
1470 struct ftrace_iterator *iter = m->private;
1471 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001472 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001473
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001474 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001475 /*
1476 * For set_ftrace_filter reading, if we have the filter
1477 * off, we can short cut and just print out that all
1478 * functions are enabled.
1479 */
1480 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1481 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001482 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001483 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001484 return iter;
1485 }
1486
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001487 if (iter->flags & FTRACE_ITER_HASH)
1488 return t_hash_start(m, pos);
1489
Li Zefan694ce0a2009-06-24 09:54:19 +08001490 iter->pg = ftrace_pages_start;
1491 iter->idx = 0;
1492 for (l = 0; l <= *pos; ) {
1493 p = t_next(m, p, &l);
1494 if (!p)
1495 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001496 }
walimis5821e1b2008-11-15 15:19:06 +08001497
Li Zefan694ce0a2009-06-24 09:54:19 +08001498 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001499 return t_hash_start(m, pos);
1500
Steven Rostedt5072c592008-05-12 21:20:43 +02001501 return p;
1502}
1503
1504static void t_stop(struct seq_file *m, void *p)
1505{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001506 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001507}
1508
1509static int t_show(struct seq_file *m, void *v)
1510{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001511 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001512 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001513
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001514 if (iter->flags & FTRACE_ITER_HASH)
1515 return t_hash_show(m, v);
1516
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001517 if (iter->flags & FTRACE_ITER_PRINTALL) {
1518 seq_printf(m, "#### all functions enabled ####\n");
1519 return 0;
1520 }
1521
Steven Rostedt5072c592008-05-12 21:20:43 +02001522 if (!rec)
1523 return 0;
1524
Steven Rostedtb375a112009-09-17 00:05:58 -04001525 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001526
1527 return 0;
1528}
1529
James Morris88e9d342009-09-22 16:43:43 -07001530static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001531 .start = t_start,
1532 .next = t_next,
1533 .stop = t_stop,
1534 .show = t_show,
1535};
1536
Ingo Molnare309b412008-05-12 21:20:51 +02001537static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001538ftrace_avail_open(struct inode *inode, struct file *file)
1539{
1540 struct ftrace_iterator *iter;
1541 int ret;
1542
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001543 if (unlikely(ftrace_disabled))
1544 return -ENODEV;
1545
Steven Rostedt5072c592008-05-12 21:20:43 +02001546 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1547 if (!iter)
1548 return -ENOMEM;
1549
1550 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001551
1552 ret = seq_open(file, &show_ftrace_seq_ops);
1553 if (!ret) {
1554 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001555
Steven Rostedt5072c592008-05-12 21:20:43 +02001556 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001557 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001558 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001559 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001560
1561 return ret;
1562}
1563
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301564static int
1565ftrace_failures_open(struct inode *inode, struct file *file)
1566{
1567 int ret;
1568 struct seq_file *m;
1569 struct ftrace_iterator *iter;
1570
1571 ret = ftrace_avail_open(inode, file);
1572 if (!ret) {
1573 m = (struct seq_file *)file->private_data;
1574 iter = (struct ftrace_iterator *)m->private;
1575 iter->flags = FTRACE_ITER_FAILURES;
1576 }
1577
1578 return ret;
1579}
1580
1581
Steven Rostedt41c52c02008-05-22 11:46:33 -04001582static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001583{
1584 struct ftrace_page *pg;
1585 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001586 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001587
Steven Rostedt52baf112009-02-14 01:15:39 -05001588 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001589 if (enable)
1590 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001591 do_for_each_ftrace_rec(pg, rec) {
1592 if (rec->flags & FTRACE_FL_FAILED)
1593 continue;
1594 rec->flags &= ~type;
1595 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001596 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001597}
1598
Ingo Molnare309b412008-05-12 21:20:51 +02001599static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001600ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001601{
1602 struct ftrace_iterator *iter;
1603 int ret = 0;
1604
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001605 if (unlikely(ftrace_disabled))
1606 return -ENODEV;
1607
Steven Rostedt5072c592008-05-12 21:20:43 +02001608 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1609 if (!iter)
1610 return -ENOMEM;
1611
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001612 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1613 kfree(iter);
1614 return -ENOMEM;
1615 }
1616
Steven Rostedt41c52c02008-05-22 11:46:33 -04001617 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001618 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001619 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001620 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001621
1622 if (file->f_mode & FMODE_READ) {
1623 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001624 iter->flags = enable ? FTRACE_ITER_FILTER :
1625 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001626
1627 ret = seq_open(file, &show_ftrace_seq_ops);
1628 if (!ret) {
1629 struct seq_file *m = file->private_data;
1630 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001631 } else {
1632 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001633 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001634 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001635 } else
1636 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001637 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001638
1639 return ret;
1640}
1641
Steven Rostedt41c52c02008-05-22 11:46:33 -04001642static int
1643ftrace_filter_open(struct inode *inode, struct file *file)
1644{
1645 return ftrace_regex_open(inode, file, 1);
1646}
1647
1648static int
1649ftrace_notrace_open(struct inode *inode, struct file *file)
1650{
1651 return ftrace_regex_open(inode, file, 0);
1652}
1653
Ingo Molnare309b412008-05-12 21:20:51 +02001654static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001655ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001656{
1657 loff_t ret;
1658
1659 if (file->f_mode & FMODE_READ)
1660 ret = seq_lseek(file, offset, origin);
1661 else
1662 file->f_pos = ret = 1;
1663
1664 return ret;
1665}
1666
Steven Rostedt64e7c442009-02-13 17:08:48 -05001667static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001668{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001669 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001670 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001671
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001672 switch (type) {
1673 case MATCH_FULL:
1674 if (strcmp(str, regex) == 0)
1675 matched = 1;
1676 break;
1677 case MATCH_FRONT_ONLY:
1678 if (strncmp(str, regex, len) == 0)
1679 matched = 1;
1680 break;
1681 case MATCH_MIDDLE_ONLY:
1682 if (strstr(str, regex))
1683 matched = 1;
1684 break;
1685 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001686 slen = strlen(str);
1687 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001688 matched = 1;
1689 break;
1690 }
1691
1692 return matched;
1693}
1694
Steven Rostedt64e7c442009-02-13 17:08:48 -05001695static int
1696ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1697{
1698 char str[KSYM_SYMBOL_LEN];
1699
1700 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1701 return ftrace_match(str, regex, len, type);
1702}
1703
Li Zefan311d16d2009-12-08 11:15:11 +08001704static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001705{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001706 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001707 struct ftrace_page *pg;
1708 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001709 unsigned long flag;
1710 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001711 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001712 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001713 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001714
Steven Rostedt6a24a242009-02-17 11:20:26 -05001715 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001716 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001717
1718 search_len = strlen(search);
1719
Steven Rostedt52baf112009-02-14 01:15:39 -05001720 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001721 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001722
Steven Rostedt265c8312009-02-13 12:43:56 -05001723 if (rec->flags & FTRACE_FL_FAILED)
1724 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001725
1726 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001727 if (not)
1728 rec->flags &= ~flag;
1729 else
1730 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001731 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001732 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001733 /*
1734 * Only enable filtering if we have a function that
1735 * is filtered on.
1736 */
1737 if (enable && (rec->flags & FTRACE_FL_FILTER))
1738 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001739 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001740 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001741
1742 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001743}
1744
Steven Rostedt64e7c442009-02-13 17:08:48 -05001745static int
1746ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1747 char *regex, int len, int type)
1748{
1749 char str[KSYM_SYMBOL_LEN];
1750 char *modname;
1751
1752 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1753
1754 if (!modname || strcmp(modname, mod))
1755 return 0;
1756
1757 /* blank search means to match all funcs in the mod */
1758 if (len)
1759 return ftrace_match(str, regex, len, type);
1760 else
1761 return 1;
1762}
1763
Li Zefan311d16d2009-12-08 11:15:11 +08001764static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001765{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001766 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001767 struct ftrace_page *pg;
1768 struct dyn_ftrace *rec;
1769 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001770 char *search = buff;
1771 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001772 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001773 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001774
Steven Rostedt6a24a242009-02-17 11:20:26 -05001775 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1776
Steven Rostedt64e7c442009-02-13 17:08:48 -05001777 /* blank or '*' mean the same */
1778 if (strcmp(buff, "*") == 0)
1779 buff[0] = 0;
1780
1781 /* handle the case of 'dont filter this module' */
1782 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1783 buff[0] = 0;
1784 not = 1;
1785 }
1786
1787 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001788 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001789 search_len = strlen(search);
1790 }
1791
Steven Rostedt52baf112009-02-14 01:15:39 -05001792 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001793 do_for_each_ftrace_rec(pg, rec) {
1794
1795 if (rec->flags & FTRACE_FL_FAILED)
1796 continue;
1797
1798 if (ftrace_match_module_record(rec, mod,
1799 search, search_len, type)) {
1800 if (not)
1801 rec->flags &= ~flag;
1802 else
1803 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001804 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001805 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001806 if (enable && (rec->flags & FTRACE_FL_FILTER))
1807 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001808
1809 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001810 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001811
1812 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001813}
1814
Steven Rostedtf6180772009-02-14 00:40:25 -05001815/*
1816 * We register the module command as a template to show others how
1817 * to register the a command as well.
1818 */
1819
1820static int
1821ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1822{
1823 char *mod;
1824
1825 /*
1826 * cmd == 'mod' because we only registered this func
1827 * for the 'mod' ftrace_func_command.
1828 * But if you register one func with multiple commands,
1829 * you can tell which command was used by the cmd
1830 * parameter.
1831 */
1832
1833 /* we must have a module name */
1834 if (!param)
1835 return -EINVAL;
1836
1837 mod = strsep(&param, ":");
1838 if (!strlen(mod))
1839 return -EINVAL;
1840
Li Zefan311d16d2009-12-08 11:15:11 +08001841 if (ftrace_match_module_records(func, mod, enable))
1842 return 0;
1843 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001844}
1845
1846static struct ftrace_func_command ftrace_mod_cmd = {
1847 .name = "mod",
1848 .func = ftrace_mod_callback,
1849};
1850
1851static int __init ftrace_mod_cmd_init(void)
1852{
1853 return register_ftrace_command(&ftrace_mod_cmd);
1854}
1855device_initcall(ftrace_mod_cmd_init);
1856
Steven Rostedt59df055f2009-02-14 15:29:06 -05001857static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001858function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001859{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001860 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001861 struct hlist_head *hhd;
1862 struct hlist_node *n;
1863 unsigned long key;
1864 int resched;
1865
1866 key = hash_long(ip, FTRACE_HASH_BITS);
1867
1868 hhd = &ftrace_func_hash[key];
1869
1870 if (hlist_empty(hhd))
1871 return;
1872
1873 /*
1874 * Disable preemption for these calls to prevent a RCU grace
1875 * period. This syncs the hash iteration and freeing of items
1876 * on the hash. rcu_read_lock is too dangerous here.
1877 */
1878 resched = ftrace_preempt_disable();
1879 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1880 if (entry->ip == ip)
1881 entry->ops->func(ip, parent_ip, &entry->data);
1882 }
1883 ftrace_preempt_enable(resched);
1884}
1885
Steven Rostedtb6887d72009-02-17 12:32:04 -05001886static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001887{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001888 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001889};
1890
Steven Rostedtb6887d72009-02-17 12:32:04 -05001891static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001892
Steven Rostedtb6887d72009-02-17 12:32:04 -05001893static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001894{
1895 int i;
1896
Steven Rostedtb6887d72009-02-17 12:32:04 -05001897 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001898 return;
1899
1900 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1901 struct hlist_head *hhd = &ftrace_func_hash[i];
1902 if (hhd->first)
1903 break;
1904 }
1905 /* Nothing registered? */
1906 if (i == FTRACE_FUNC_HASHSIZE)
1907 return;
1908
Steven Rostedtb6887d72009-02-17 12:32:04 -05001909 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001910 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001911 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001912}
1913
Steven Rostedtb6887d72009-02-17 12:32:04 -05001914static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915{
1916 int i;
1917
Steven Rostedtb6887d72009-02-17 12:32:04 -05001918 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001919 return;
1920
1921 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1922 struct hlist_head *hhd = &ftrace_func_hash[i];
1923 if (hhd->first)
1924 return;
1925 }
1926
1927 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001928 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001929 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001930 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001931}
1932
1933
1934static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1935{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001936 struct ftrace_func_probe *entry =
1937 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001938
1939 if (entry->ops->free)
1940 entry->ops->free(&entry->data);
1941 kfree(entry);
1942}
1943
1944
1945int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001946register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001947 void *data)
1948{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001949 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950 struct ftrace_page *pg;
1951 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001952 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001953 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001954 int count = 0;
1955 char *search;
1956
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001957 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001958 len = strlen(search);
1959
Steven Rostedtb6887d72009-02-17 12:32:04 -05001960 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001961 if (WARN_ON(not))
1962 return -EINVAL;
1963
1964 mutex_lock(&ftrace_lock);
1965 do_for_each_ftrace_rec(pg, rec) {
1966
1967 if (rec->flags & FTRACE_FL_FAILED)
1968 continue;
1969
1970 if (!ftrace_match_record(rec, search, len, type))
1971 continue;
1972
1973 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1974 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001975 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001976 if (!count)
1977 count = -ENOMEM;
1978 goto out_unlock;
1979 }
1980
1981 count++;
1982
1983 entry->data = data;
1984
1985 /*
1986 * The caller might want to do something special
1987 * for each function we find. We call the callback
1988 * to give the caller an opportunity to do so.
1989 */
1990 if (ops->callback) {
1991 if (ops->callback(rec->ip, &entry->data) < 0) {
1992 /* caller does not like this func */
1993 kfree(entry);
1994 continue;
1995 }
1996 }
1997
1998 entry->ops = ops;
1999 entry->ip = rec->ip;
2000
2001 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2002 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2003
2004 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002005 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002006
2007 out_unlock:
2008 mutex_unlock(&ftrace_lock);
2009
2010 return count;
2011}
2012
2013enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002014 PROBE_TEST_FUNC = 1,
2015 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002016};
2017
2018static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002019__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002020 void *data, int flags)
2021{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002022 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002023 struct hlist_node *n, *tmp;
2024 char str[KSYM_SYMBOL_LEN];
2025 int type = MATCH_FULL;
2026 int i, len = 0;
2027 char *search;
2028
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002029 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002030 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002031 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002032 int not;
2033
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002034 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002035 len = strlen(search);
2036
Steven Rostedtb6887d72009-02-17 12:32:04 -05002037 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002038 if (WARN_ON(not))
2039 return;
2040 }
2041
2042 mutex_lock(&ftrace_lock);
2043 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2044 struct hlist_head *hhd = &ftrace_func_hash[i];
2045
2046 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2047
2048 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002049 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002050 continue;
2051
Steven Rostedtb6887d72009-02-17 12:32:04 -05002052 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002053 continue;
2054
2055 /* do this last, since it is the most expensive */
2056 if (glob) {
2057 kallsyms_lookup(entry->ip, NULL, NULL,
2058 NULL, str);
2059 if (!ftrace_match(str, glob, len, type))
2060 continue;
2061 }
2062
2063 hlist_del(&entry->node);
2064 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2065 }
2066 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002067 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002068 mutex_unlock(&ftrace_lock);
2069}
2070
2071void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002072unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002073 void *data)
2074{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002075 __unregister_ftrace_function_probe(glob, ops, data,
2076 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002077}
2078
2079void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002080unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002081{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002082 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002083}
2084
Steven Rostedtb6887d72009-02-17 12:32:04 -05002085void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002086{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002087 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002088}
2089
Steven Rostedtf6180772009-02-14 00:40:25 -05002090static LIST_HEAD(ftrace_commands);
2091static DEFINE_MUTEX(ftrace_cmd_mutex);
2092
2093int register_ftrace_command(struct ftrace_func_command *cmd)
2094{
2095 struct ftrace_func_command *p;
2096 int ret = 0;
2097
2098 mutex_lock(&ftrace_cmd_mutex);
2099 list_for_each_entry(p, &ftrace_commands, list) {
2100 if (strcmp(cmd->name, p->name) == 0) {
2101 ret = -EBUSY;
2102 goto out_unlock;
2103 }
2104 }
2105 list_add(&cmd->list, &ftrace_commands);
2106 out_unlock:
2107 mutex_unlock(&ftrace_cmd_mutex);
2108
2109 return ret;
2110}
2111
2112int unregister_ftrace_command(struct ftrace_func_command *cmd)
2113{
2114 struct ftrace_func_command *p, *n;
2115 int ret = -ENODEV;
2116
2117 mutex_lock(&ftrace_cmd_mutex);
2118 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2119 if (strcmp(cmd->name, p->name) == 0) {
2120 ret = 0;
2121 list_del_init(&p->list);
2122 goto out_unlock;
2123 }
2124 }
2125 out_unlock:
2126 mutex_unlock(&ftrace_cmd_mutex);
2127
2128 return ret;
2129}
2130
Steven Rostedt64e7c442009-02-13 17:08:48 -05002131static int ftrace_process_regex(char *buff, int len, int enable)
2132{
Steven Rostedtf6180772009-02-14 00:40:25 -05002133 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002134 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002135 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002136
2137 func = strsep(&next, ":");
2138
2139 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002140 if (ftrace_match_records(func, len, enable))
2141 return 0;
2142 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002143 }
2144
Steven Rostedtf6180772009-02-14 00:40:25 -05002145 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002146
2147 command = strsep(&next, ":");
2148
Steven Rostedtf6180772009-02-14 00:40:25 -05002149 mutex_lock(&ftrace_cmd_mutex);
2150 list_for_each_entry(p, &ftrace_commands, list) {
2151 if (strcmp(p->name, command) == 0) {
2152 ret = p->func(func, command, next, enable);
2153 goto out_unlock;
2154 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002155 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002156 out_unlock:
2157 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002158
Steven Rostedtf6180772009-02-14 00:40:25 -05002159 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002160}
2161
Ingo Molnare309b412008-05-12 21:20:51 +02002162static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002163ftrace_regex_write(struct file *file, const char __user *ubuf,
2164 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002165{
2166 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002167 struct trace_parser *parser;
2168 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002169
Li Zefan4ba79782009-09-22 13:52:20 +08002170 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002171 return 0;
2172
Steven Rostedt41c52c02008-05-22 11:46:33 -04002173 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002174
2175 if (file->f_mode & FMODE_READ) {
2176 struct seq_file *m = file->private_data;
2177 iter = m->private;
2178 } else
2179 iter = file->private_data;
2180
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002181 parser = &iter->parser;
2182 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002183
Li Zefan4ba79782009-09-22 13:52:20 +08002184 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002185 !trace_parser_cont(parser)) {
2186 ret = ftrace_process_regex(parser->buffer,
2187 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002188 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002189 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002190 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002191 }
2192
Steven Rostedt5072c592008-05-12 21:20:43 +02002193 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002194out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002195 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002196
Steven Rostedt5072c592008-05-12 21:20:43 +02002197 return ret;
2198}
2199
Steven Rostedt41c52c02008-05-22 11:46:33 -04002200static ssize_t
2201ftrace_filter_write(struct file *file, const char __user *ubuf,
2202 size_t cnt, loff_t *ppos)
2203{
2204 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2205}
2206
2207static ssize_t
2208ftrace_notrace_write(struct file *file, const char __user *ubuf,
2209 size_t cnt, loff_t *ppos)
2210{
2211 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2212}
2213
2214static void
2215ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2216{
2217 if (unlikely(ftrace_disabled))
2218 return;
2219
2220 mutex_lock(&ftrace_regex_lock);
2221 if (reset)
2222 ftrace_filter_reset(enable);
2223 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002224 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002225 mutex_unlock(&ftrace_regex_lock);
2226}
2227
Steven Rostedt77a2b372008-05-12 21:20:45 +02002228/**
2229 * ftrace_set_filter - set a function to filter on in ftrace
2230 * @buf - the string that holds the function filter text.
2231 * @len - the length of the string.
2232 * @reset - non zero to reset all filters before applying this filter.
2233 *
2234 * Filters denote which functions should be enabled when tracing is enabled.
2235 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2236 */
Ingo Molnare309b412008-05-12 21:20:51 +02002237void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002238{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002239 ftrace_set_regex(buf, len, reset, 1);
2240}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002241
Steven Rostedt41c52c02008-05-22 11:46:33 -04002242/**
2243 * ftrace_set_notrace - set a function to not trace in ftrace
2244 * @buf - the string that holds the function notrace text.
2245 * @len - the length of the string.
2246 * @reset - non zero to reset all filters before applying this filter.
2247 *
2248 * Notrace Filters denote which functions should not be enabled when tracing
2249 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2250 * for tracing.
2251 */
2252void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2253{
2254 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002255}
2256
Steven Rostedt2af15d62009-05-28 13:37:24 -04002257/*
2258 * command line interface to allow users to set filters on boot up.
2259 */
2260#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2261static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2262static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2263
2264static int __init set_ftrace_notrace(char *str)
2265{
2266 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2267 return 1;
2268}
2269__setup("ftrace_notrace=", set_ftrace_notrace);
2270
2271static int __init set_ftrace_filter(char *str)
2272{
2273 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2274 return 1;
2275}
2276__setup("ftrace_filter=", set_ftrace_filter);
2277
Stefan Assmann369bc182009-10-12 22:17:21 +02002278#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002279static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002280static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2281
Stefan Assmann369bc182009-10-12 22:17:21 +02002282static int __init set_graph_function(char *str)
2283{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002284 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002285 return 1;
2286}
2287__setup("ftrace_graph_filter=", set_graph_function);
2288
2289static void __init set_ftrace_early_graph(char *buf)
2290{
2291 int ret;
2292 char *func;
2293
2294 while (buf) {
2295 func = strsep(&buf, ",");
2296 /* we allow only one expression at a time */
2297 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2298 func);
2299 if (ret)
2300 printk(KERN_DEBUG "ftrace: function %s not "
2301 "traceable\n", func);
2302 }
2303}
2304#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2305
Steven Rostedt2af15d62009-05-28 13:37:24 -04002306static void __init set_ftrace_early_filter(char *buf, int enable)
2307{
2308 char *func;
2309
2310 while (buf) {
2311 func = strsep(&buf, ",");
2312 ftrace_set_regex(func, strlen(func), 0, enable);
2313 }
2314}
2315
2316static void __init set_ftrace_early_filters(void)
2317{
2318 if (ftrace_filter_buf[0])
2319 set_ftrace_early_filter(ftrace_filter_buf, 1);
2320 if (ftrace_notrace_buf[0])
2321 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002322#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2323 if (ftrace_graph_buf[0])
2324 set_ftrace_early_graph(ftrace_graph_buf);
2325#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002326}
2327
Ingo Molnare309b412008-05-12 21:20:51 +02002328static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002329ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002330{
2331 struct seq_file *m = (struct seq_file *)file->private_data;
2332 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002333 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002334
Steven Rostedt41c52c02008-05-22 11:46:33 -04002335 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002336 if (file->f_mode & FMODE_READ) {
2337 iter = m->private;
2338
2339 seq_release(inode, file);
2340 } else
2341 iter = file->private_data;
2342
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002343 parser = &iter->parser;
2344 if (trace_parser_loaded(parser)) {
2345 parser->buffer[parser->idx] = 0;
2346 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002347 }
2348
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002349 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002350 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002351 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002352 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002353
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002354 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002355 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002356
Steven Rostedt41c52c02008-05-22 11:46:33 -04002357 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002358 return 0;
2359}
2360
Steven Rostedt41c52c02008-05-22 11:46:33 -04002361static int
2362ftrace_filter_release(struct inode *inode, struct file *file)
2363{
2364 return ftrace_regex_release(inode, file, 1);
2365}
2366
2367static int
2368ftrace_notrace_release(struct inode *inode, struct file *file)
2369{
2370 return ftrace_regex_release(inode, file, 0);
2371}
2372
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002373static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002374 .open = ftrace_avail_open,
2375 .read = seq_read,
2376 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002377 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002378};
2379
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002380static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302381 .open = ftrace_failures_open,
2382 .read = seq_read,
2383 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002384 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302385};
2386
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002387static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002388 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002389 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002390 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002391 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002392 .release = ftrace_filter_release,
2393};
2394
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002395static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002396 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002397 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002398 .write = ftrace_notrace_write,
2399 .llseek = ftrace_regex_lseek,
2400 .release = ftrace_notrace_release,
2401};
2402
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002403#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2404
2405static DEFINE_MUTEX(graph_lock);
2406
2407int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002408int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002409unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2410
2411static void *
Li Zefan85951842009-06-24 09:54:00 +08002412__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002413{
Li Zefan85951842009-06-24 09:54:00 +08002414 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002415 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002416 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002417}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002418
Li Zefan85951842009-06-24 09:54:00 +08002419static void *
2420g_next(struct seq_file *m, void *v, loff_t *pos)
2421{
2422 (*pos)++;
2423 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002424}
2425
2426static void *g_start(struct seq_file *m, loff_t *pos)
2427{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002428 mutex_lock(&graph_lock);
2429
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002430 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002431 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002432 return (void *)1;
2433
Li Zefan85951842009-06-24 09:54:00 +08002434 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002435}
2436
2437static void g_stop(struct seq_file *m, void *p)
2438{
2439 mutex_unlock(&graph_lock);
2440}
2441
2442static int g_show(struct seq_file *m, void *v)
2443{
2444 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002445
2446 if (!ptr)
2447 return 0;
2448
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002449 if (ptr == (unsigned long *)1) {
2450 seq_printf(m, "#### all functions enabled ####\n");
2451 return 0;
2452 }
2453
Steven Rostedtb375a112009-09-17 00:05:58 -04002454 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002455
2456 return 0;
2457}
2458
James Morris88e9d342009-09-22 16:43:43 -07002459static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002460 .start = g_start,
2461 .next = g_next,
2462 .stop = g_stop,
2463 .show = g_show,
2464};
2465
2466static int
2467ftrace_graph_open(struct inode *inode, struct file *file)
2468{
2469 int ret = 0;
2470
2471 if (unlikely(ftrace_disabled))
2472 return -ENODEV;
2473
2474 mutex_lock(&graph_lock);
2475 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002476 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002477 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002478 ftrace_graph_count = 0;
2479 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2480 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002481 mutex_unlock(&graph_lock);
2482
Li Zefana4ec5e02009-09-18 14:06:28 +08002483 if (file->f_mode & FMODE_READ)
2484 ret = seq_open(file, &ftrace_graph_seq_ops);
2485
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002486 return ret;
2487}
2488
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002489static int
Li Zefan87827112009-07-23 11:29:11 +08002490ftrace_graph_release(struct inode *inode, struct file *file)
2491{
2492 if (file->f_mode & FMODE_READ)
2493 seq_release(inode, file);
2494 return 0;
2495}
2496
2497static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002498ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002499{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002500 struct dyn_ftrace *rec;
2501 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002502 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002503 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002504 int type, not;
2505 char *search;
2506 bool exists;
2507 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002508
2509 if (ftrace_disabled)
2510 return -ENODEV;
2511
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002512 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002513 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002514 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2515 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002516
2517 search_len = strlen(search);
2518
Steven Rostedt52baf112009-02-14 01:15:39 -05002519 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002520 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002521
Steven Rostedt265c8312009-02-13 12:43:56 -05002522 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2523 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002524
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002525 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002526 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002527 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002528 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002529 if (array[i] == rec->ip) {
2530 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002531 break;
2532 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002533 }
2534
2535 if (!not) {
2536 fail = 0;
2537 if (!exists) {
2538 array[(*idx)++] = rec->ip;
2539 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2540 goto out;
2541 }
2542 } else {
2543 if (exists) {
2544 array[i] = array[--(*idx)];
2545 array[*idx] = 0;
2546 fail = 0;
2547 }
2548 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002549 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002550 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002551out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002552 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002553
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002554 if (fail)
2555 return -EINVAL;
2556
2557 ftrace_graph_filter_enabled = 1;
2558 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002559}
2560
2561static ssize_t
2562ftrace_graph_write(struct file *file, const char __user *ubuf,
2563 size_t cnt, loff_t *ppos)
2564{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002565 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002566 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002567
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002568 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002569 return 0;
2570
2571 mutex_lock(&graph_lock);
2572
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002573 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2574 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002575 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002576 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002577
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002578 read = trace_get_user(&parser, ubuf, cnt, ppos);
2579
Li Zefan4ba79782009-09-22 13:52:20 +08002580 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002581 parser.buffer[parser.idx] = 0;
2582
2583 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002584 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002585 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002586 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002587 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002588 }
2589
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002590 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002591
2592out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002593 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002594out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002595 mutex_unlock(&graph_lock);
2596
2597 return ret;
2598}
2599
2600static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002601 .open = ftrace_graph_open,
2602 .read = seq_read,
2603 .write = ftrace_graph_write,
2604 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002605};
2606#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2607
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002608static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002609{
Steven Rostedt5072c592008-05-12 21:20:43 +02002610
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002611 trace_create_file("available_filter_functions", 0444,
2612 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002613
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002614 trace_create_file("failures", 0444,
2615 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302616
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002617 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2618 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002619
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002620 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002621 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002622
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002624 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002625 NULL,
2626 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002627#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2628
Steven Rostedt5072c592008-05-12 21:20:43 +02002629 return 0;
2630}
2631
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002632static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002633 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002634 unsigned long *end)
2635{
2636 unsigned long *p;
2637 unsigned long addr;
2638 unsigned long flags;
2639
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002640 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002641 p = start;
2642 while (p < end) {
2643 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002644 /*
2645 * Some architecture linkers will pad between
2646 * the different mcount_loc sections of different
2647 * object files to satisfy alignments.
2648 * Skip any NULL pointers.
2649 */
2650 if (!addr)
2651 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002652 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002653 }
2654
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002655 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002656 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002657 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002658 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002659 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002660
2661 return 0;
2662}
2663
Steven Rostedt93eb6772009-04-15 13:24:06 -04002664#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002665void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002666{
2667 struct dyn_ftrace *rec;
2668 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002669
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002670 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002671 return;
2672
2673 mutex_lock(&ftrace_lock);
2674 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002675 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002676 /*
2677 * rec->ip is changed in ftrace_free_rec()
2678 * It should not between s and e if record was freed.
2679 */
2680 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2681 ftrace_free_rec(rec);
2682 }
2683 } while_for_each_ftrace_rec();
2684 mutex_unlock(&ftrace_lock);
2685}
2686
2687static void ftrace_init_module(struct module *mod,
2688 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002689{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002690 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002691 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002692 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002693}
2694
Steven Rostedt93eb6772009-04-15 13:24:06 -04002695static int ftrace_module_notify(struct notifier_block *self,
2696 unsigned long val, void *data)
2697{
2698 struct module *mod = data;
2699
2700 switch (val) {
2701 case MODULE_STATE_COMING:
2702 ftrace_init_module(mod, mod->ftrace_callsites,
2703 mod->ftrace_callsites +
2704 mod->num_ftrace_callsites);
2705 break;
2706 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002707 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002708 break;
2709 }
2710
2711 return 0;
2712}
2713#else
2714static int ftrace_module_notify(struct notifier_block *self,
2715 unsigned long val, void *data)
2716{
2717 return 0;
2718}
2719#endif /* CONFIG_MODULES */
2720
2721struct notifier_block ftrace_module_nb = {
2722 .notifier_call = ftrace_module_notify,
2723 .priority = 0,
2724};
2725
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002726extern unsigned long __start_mcount_loc[];
2727extern unsigned long __stop_mcount_loc[];
2728
2729void __init ftrace_init(void)
2730{
2731 unsigned long count, addr, flags;
2732 int ret;
2733
2734 /* Keep the ftrace pointer to the stub */
2735 addr = (unsigned long)ftrace_stub;
2736
2737 local_irq_save(flags);
2738 ftrace_dyn_arch_init(&addr);
2739 local_irq_restore(flags);
2740
2741 /* ftrace_dyn_arch_init places the return code in addr */
2742 if (addr)
2743 goto failed;
2744
2745 count = __stop_mcount_loc - __start_mcount_loc;
2746
2747 ret = ftrace_dyn_table_alloc(count);
2748 if (ret)
2749 goto failed;
2750
2751 last_ftrace_enabled = ftrace_enabled = 1;
2752
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002753 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002754 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002755 __stop_mcount_loc);
2756
Steven Rostedt93eb6772009-04-15 13:24:06 -04002757 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002758 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002759 pr_warning("Failed to register trace ftrace module notifier\n");
2760
Steven Rostedt2af15d62009-05-28 13:37:24 -04002761 set_ftrace_early_filters();
2762
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002763 return;
2764 failed:
2765 ftrace_disabled = 1;
2766}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002767
Steven Rostedt3d083392008-05-12 21:20:42 +02002768#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002769
2770static int __init ftrace_nodyn_init(void)
2771{
2772 ftrace_enabled = 1;
2773 return 0;
2774}
2775device_initcall(ftrace_nodyn_init);
2776
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002777static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2778static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002779/* Keep as macros so we do not need to define the commands */
2780# define ftrace_startup(command) do { } while (0)
2781# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002782# define ftrace_startup_sysctl() do { } while (0)
2783# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002784#endif /* CONFIG_DYNAMIC_FTRACE */
2785
Steven Rostedte32d8952008-12-04 00:26:41 -05002786static void clear_ftrace_swapper(void)
2787{
2788 struct task_struct *p;
2789 int cpu;
2790
2791 get_online_cpus();
2792 for_each_online_cpu(cpu) {
2793 p = idle_task(cpu);
2794 clear_tsk_trace_trace(p);
2795 }
2796 put_online_cpus();
2797}
2798
2799static void set_ftrace_swapper(void)
2800{
2801 struct task_struct *p;
2802 int cpu;
2803
2804 get_online_cpus();
2805 for_each_online_cpu(cpu) {
2806 p = idle_task(cpu);
2807 set_tsk_trace_trace(p);
2808 }
2809 put_online_cpus();
2810}
2811
2812static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002813{
2814 struct task_struct *p;
2815
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002816 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002817 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002818 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002819 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002820 rcu_read_unlock();
2821
Steven Rostedte32d8952008-12-04 00:26:41 -05002822 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002823}
2824
Steven Rostedte32d8952008-12-04 00:26:41 -05002825static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002826{
2827 struct task_struct *p;
2828
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002829 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002830 do_each_pid_task(pid, PIDTYPE_PID, p) {
2831 set_tsk_trace_trace(p);
2832 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002833 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002834}
2835
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002836static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002837{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002838 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002839 clear_ftrace_swapper();
2840 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002841 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002842}
2843
2844static void set_ftrace_pid_task(struct pid *pid)
2845{
2846 if (pid == ftrace_swapper_pid)
2847 set_ftrace_swapper();
2848 else
2849 set_ftrace_pid(pid);
2850}
2851
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002852static int ftrace_pid_add(int p)
2853{
2854 struct pid *pid;
2855 struct ftrace_pid *fpid;
2856 int ret = -EINVAL;
2857
2858 mutex_lock(&ftrace_lock);
2859
2860 if (!p)
2861 pid = ftrace_swapper_pid;
2862 else
2863 pid = find_get_pid(p);
2864
2865 if (!pid)
2866 goto out;
2867
2868 ret = 0;
2869
2870 list_for_each_entry(fpid, &ftrace_pids, list)
2871 if (fpid->pid == pid)
2872 goto out_put;
2873
2874 ret = -ENOMEM;
2875
2876 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2877 if (!fpid)
2878 goto out_put;
2879
2880 list_add(&fpid->list, &ftrace_pids);
2881 fpid->pid = pid;
2882
2883 set_ftrace_pid_task(pid);
2884
2885 ftrace_update_pid_func();
2886 ftrace_startup_enable(0);
2887
2888 mutex_unlock(&ftrace_lock);
2889 return 0;
2890
2891out_put:
2892 if (pid != ftrace_swapper_pid)
2893 put_pid(pid);
2894
2895out:
2896 mutex_unlock(&ftrace_lock);
2897 return ret;
2898}
2899
2900static void ftrace_pid_reset(void)
2901{
2902 struct ftrace_pid *fpid, *safe;
2903
2904 mutex_lock(&ftrace_lock);
2905 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2906 struct pid *pid = fpid->pid;
2907
2908 clear_ftrace_pid_task(pid);
2909
2910 list_del(&fpid->list);
2911 kfree(fpid);
2912 }
2913
2914 ftrace_update_pid_func();
2915 ftrace_startup_enable(0);
2916
2917 mutex_unlock(&ftrace_lock);
2918}
2919
2920static void *fpid_start(struct seq_file *m, loff_t *pos)
2921{
2922 mutex_lock(&ftrace_lock);
2923
2924 if (list_empty(&ftrace_pids) && (!*pos))
2925 return (void *) 1;
2926
2927 return seq_list_start(&ftrace_pids, *pos);
2928}
2929
2930static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2931{
2932 if (v == (void *)1)
2933 return NULL;
2934
2935 return seq_list_next(v, &ftrace_pids, pos);
2936}
2937
2938static void fpid_stop(struct seq_file *m, void *p)
2939{
2940 mutex_unlock(&ftrace_lock);
2941}
2942
2943static int fpid_show(struct seq_file *m, void *v)
2944{
2945 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2946
2947 if (v == (void *)1) {
2948 seq_printf(m, "no pid\n");
2949 return 0;
2950 }
2951
2952 if (fpid->pid == ftrace_swapper_pid)
2953 seq_printf(m, "swapper tasks\n");
2954 else
2955 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2956
2957 return 0;
2958}
2959
2960static const struct seq_operations ftrace_pid_sops = {
2961 .start = fpid_start,
2962 .next = fpid_next,
2963 .stop = fpid_stop,
2964 .show = fpid_show,
2965};
2966
2967static int
2968ftrace_pid_open(struct inode *inode, struct file *file)
2969{
2970 int ret = 0;
2971
2972 if ((file->f_mode & FMODE_WRITE) &&
2973 (file->f_flags & O_TRUNC))
2974 ftrace_pid_reset();
2975
2976 if (file->f_mode & FMODE_READ)
2977 ret = seq_open(file, &ftrace_pid_sops);
2978
2979 return ret;
2980}
2981
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002982static ssize_t
2983ftrace_pid_write(struct file *filp, const char __user *ubuf,
2984 size_t cnt, loff_t *ppos)
2985{
Ingo Molnar457dc922009-11-23 11:03:28 +01002986 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002987 long val;
2988 int ret;
2989
2990 if (cnt >= sizeof(buf))
2991 return -EINVAL;
2992
2993 if (copy_from_user(&buf, ubuf, cnt))
2994 return -EFAULT;
2995
2996 buf[cnt] = 0;
2997
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002998 /*
2999 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3000 * to clean the filter quietly.
3001 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003002 tmp = strstrip(buf);
3003 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003004 return 1;
3005
Ingo Molnar457dc922009-11-23 11:03:28 +01003006 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003007 if (ret < 0)
3008 return ret;
3009
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003010 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003011
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003012 return ret ? ret : cnt;
3013}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003014
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003015static int
3016ftrace_pid_release(struct inode *inode, struct file *file)
3017{
3018 if (file->f_mode & FMODE_READ)
3019 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003020
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003021 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003022}
3023
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003024static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003025 .open = ftrace_pid_open,
3026 .write = ftrace_pid_write,
3027 .read = seq_read,
3028 .llseek = seq_lseek,
3029 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003030};
3031
3032static __init int ftrace_init_debugfs(void)
3033{
3034 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003035
3036 d_tracer = tracing_init_dentry();
3037 if (!d_tracer)
3038 return 0;
3039
3040 ftrace_init_dyn_debugfs(d_tracer);
3041
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003042 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3043 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003044
3045 ftrace_profile_debugfs(d_tracer);
3046
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003047 return 0;
3048}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003049fs_initcall(ftrace_init_debugfs);
3050
Steven Rostedt3d083392008-05-12 21:20:42 +02003051/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003052 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003053 *
3054 * This function should be used by panic code. It stops ftrace
3055 * but in a not so nice way. If you need to simply kill ftrace
3056 * from a non-atomic section, use ftrace_kill.
3057 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003058void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003059{
3060 ftrace_disabled = 1;
3061 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003062 clear_ftrace_function();
3063}
3064
3065/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003066 * register_ftrace_function - register a function for profiling
3067 * @ops - ops structure that holds the function for profiling.
3068 *
3069 * Register a function to be called by all functions in the
3070 * kernel.
3071 *
3072 * Note: @ops->func and all the functions it calls must be labeled
3073 * with "notrace", otherwise it will go into a
3074 * recursive loop.
3075 */
3076int register_ftrace_function(struct ftrace_ops *ops)
3077{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003078 int ret;
3079
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003080 if (unlikely(ftrace_disabled))
3081 return -1;
3082
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003083 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003084
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003085 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003086 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003087
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003088 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003089 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003090}
3091
3092/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003093 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003094 * @ops - ops structure that holds the function to unregister
3095 *
3096 * Unregister a function that was added to be called by ftrace profiling.
3097 */
3098int unregister_ftrace_function(struct ftrace_ops *ops)
3099{
3100 int ret;
3101
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003102 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003103 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003104 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003105 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003106
3107 return ret;
3108}
3109
Ingo Molnare309b412008-05-12 21:20:51 +02003110int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003111ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003112 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003113 loff_t *ppos)
3114{
3115 int ret;
3116
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003117 if (unlikely(ftrace_disabled))
3118 return -ENODEV;
3119
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003120 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003121
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003122 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003123
Li Zefana32c7762009-06-26 16:55:51 +08003124 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003125 goto out;
3126
Li Zefana32c7762009-06-26 16:55:51 +08003127 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003128
3129 if (ftrace_enabled) {
3130
3131 ftrace_startup_sysctl();
3132
3133 /* we are starting ftrace again */
3134 if (ftrace_list != &ftrace_list_end) {
3135 if (ftrace_list->next == &ftrace_list_end)
3136 ftrace_trace_function = ftrace_list->func;
3137 else
3138 ftrace_trace_function = ftrace_list_func;
3139 }
3140
3141 } else {
3142 /* stopping ftrace calls (just send to ftrace_stub) */
3143 ftrace_trace_function = ftrace_stub;
3144
3145 ftrace_shutdown_sysctl();
3146 }
3147
3148 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003149 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003150 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003151}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003152
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003153#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003154
Steven Rostedt597af812009-04-03 15:24:12 -04003155static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003156static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003157
Steven Rostedte49dc192008-12-02 23:50:05 -05003158int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3159{
3160 return 0;
3161}
3162
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003163/* The callbacks that hook a function */
3164trace_func_graph_ret_t ftrace_graph_return =
3165 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003166trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003167
3168/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3169static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3170{
3171 int i;
3172 int ret = 0;
3173 unsigned long flags;
3174 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3175 struct task_struct *g, *t;
3176
3177 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3178 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3179 * sizeof(struct ftrace_ret_stack),
3180 GFP_KERNEL);
3181 if (!ret_stack_list[i]) {
3182 start = 0;
3183 end = i;
3184 ret = -ENOMEM;
3185 goto free;
3186 }
3187 }
3188
3189 read_lock_irqsave(&tasklist_lock, flags);
3190 do_each_thread(g, t) {
3191 if (start == end) {
3192 ret = -EAGAIN;
3193 goto unlock;
3194 }
3195
3196 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003197 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003198 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003199 t->curr_ret_stack = -1;
3200 /* Make sure the tasks see the -1 first: */
3201 smp_wmb();
3202 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003203 }
3204 } while_each_thread(g, t);
3205
3206unlock:
3207 read_unlock_irqrestore(&tasklist_lock, flags);
3208free:
3209 for (i = start; i < end; i++)
3210 kfree(ret_stack_list[i]);
3211 return ret;
3212}
3213
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003214static void
3215ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3216 struct task_struct *next)
3217{
3218 unsigned long long timestamp;
3219 int index;
3220
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003221 /*
3222 * Does the user want to count the time a function was asleep.
3223 * If so, do not update the time stamps.
3224 */
3225 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3226 return;
3227
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003228 timestamp = trace_clock_local();
3229
3230 prev->ftrace_timestamp = timestamp;
3231
3232 /* only process tasks that we timestamped */
3233 if (!next->ftrace_timestamp)
3234 return;
3235
3236 /*
3237 * Update all the counters in next to make up for the
3238 * time next was sleeping.
3239 */
3240 timestamp -= next->ftrace_timestamp;
3241
3242 for (index = next->curr_ret_stack; index >= 0; index--)
3243 next->ret_stack[index].calltime += timestamp;
3244}
3245
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003246/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003247static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003248{
3249 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003250 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003251
3252 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3253 sizeof(struct ftrace_ret_stack *),
3254 GFP_KERNEL);
3255
3256 if (!ret_stack_list)
3257 return -ENOMEM;
3258
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003259 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003260 for_each_online_cpu(cpu) {
3261 if (!idle_task(cpu)->ret_stack)
3262 ftrace_graph_init_task(idle_task(cpu));
3263 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003264
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003265 do {
3266 ret = alloc_retstack_tasklist(ret_stack_list);
3267 } while (ret == -EAGAIN);
3268
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003269 if (!ret) {
3270 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3271 if (ret)
3272 pr_info("ftrace_graph: Couldn't activate tracepoint"
3273 " probe to kernel_sched_switch\n");
3274 }
3275
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003276 kfree(ret_stack_list);
3277 return ret;
3278}
3279
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003280/*
3281 * Hibernation protection.
3282 * The state of the current task is too much unstable during
3283 * suspend/restore to disk. We want to protect against that.
3284 */
3285static int
3286ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3287 void *unused)
3288{
3289 switch (state) {
3290 case PM_HIBERNATION_PREPARE:
3291 pause_graph_tracing();
3292 break;
3293
3294 case PM_POST_HIBERNATION:
3295 unpause_graph_tracing();
3296 break;
3297 }
3298 return NOTIFY_DONE;
3299}
3300
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003301int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3302 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003303{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003304 int ret = 0;
3305
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003306 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003307
Steven Rostedt05ce5812009-03-24 00:18:31 -04003308 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003309 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003310 ret = -EBUSY;
3311 goto out;
3312 }
3313
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003314 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3315 register_pm_notifier(&ftrace_suspend_notifier);
3316
Steven Rostedt597af812009-04-03 15:24:12 -04003317 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003318 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003319 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003320 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003321 goto out;
3322 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003323
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003324 ftrace_graph_return = retfunc;
3325 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003326
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003327 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003328
3329out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003330 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003331 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003332}
3333
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003334void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003335{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003336 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003337
Steven Rostedt597af812009-04-03 15:24:12 -04003338 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003339 goto out;
3340
Steven Rostedt597af812009-04-03 15:24:12 -04003341 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003342 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003343 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003344 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003345 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003346 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003347
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003348 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003349 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003350}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003351
3352/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003353void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003354{
Steven Rostedt84047e32009-06-02 16:51:55 -04003355 /* Make sure we do not use the parent ret_stack */
3356 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003357 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003358
Steven Rostedt597af812009-04-03 15:24:12 -04003359 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003360 struct ftrace_ret_stack *ret_stack;
3361
3362 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003363 * sizeof(struct ftrace_ret_stack),
3364 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003365 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003366 return;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003367 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003368 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003369 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003370 /* make curr_ret_stack visable before we add the ret_stack */
3371 smp_wmb();
3372 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003373 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003374}
3375
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003376void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003377{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003378 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3379
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003380 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003381 /* NULL must become visible to IRQs before we free it: */
3382 barrier();
3383
3384 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003385}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003386
3387void ftrace_graph_stop(void)
3388{
3389 ftrace_stop();
3390}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003391#endif