blob: d9062f5cc0c01e598670d651fba80b87ccb68a57 [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>
Steven Rostedt5072c592008-05-12 21:20:43 +020027#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020028#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050029#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080030#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020031
Steven Rostedtad8d75f2009-04-14 19:39:12 -040032#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040033
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034#include <asm/ftrace.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040035#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053036
Steven Rostedt0706f1c2009-03-23 23:12:58 -040037#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040038#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020039
Steven Rostedt69128962008-10-23 09:33:03 -040040#define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
45
46#define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
51
Steven Rostedt8fc0c702009-02-16 15:28:00 -050052/* hash bits for specific function selection */
53#define FTRACE_HASH_BITS 7
54#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55
Steven Rostedt4eebcc82008-05-12 21:20:48 +020056/* ftrace_enabled is a method to turn ftrace on or off */
57int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020058static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020059
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050060/* Quick disabling of function tracer. */
61int function_trace_stop;
62
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040063/* List for set_ftrace_pid's pids. */
64LIST_HEAD(ftrace_pids);
65struct ftrace_pid {
66 struct list_head list;
67 struct pid *pid;
68};
69
Steven Rostedt4eebcc82008-05-12 21:20:48 +020070/*
71 * ftrace_disabled is set when an anomaly is discovered.
72 * ftrace_disabled is much stronger than ftrace_enabled.
73 */
74static int ftrace_disabled __read_mostly;
75
Steven Rostedt52baf112009-02-14 01:15:39 -050076static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020077
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020078static struct ftrace_ops ftrace_list_end __read_mostly =
79{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040080 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020081};
82
83static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
84ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050085ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050086ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020087
Paul E. McKenney3f379b02010-03-05 15:03:25 -080088/*
89 * Traverse the ftrace_list, invoking all entries. The reason that we
90 * can use rcu_dereference_raw() is that elements removed from this list
91 * are simply leaked, so there is no need to interact with a grace-period
92 * mechanism. The rcu_dereference_raw() calls are needed to handle
93 * concurrent insertions into the ftrace_list.
94 *
95 * Silly Alpha and silly pointer-speculation compiler optimizations!
96 */
Ingo Molnarf2252932008-05-22 10:37:48 +020097static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020098{
Paul E. McKenney3f379b02010-03-05 15:03:25 -080099 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200100
101 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200102 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800103 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104 };
105}
106
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500107static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
108{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500109 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500110 return;
111
112 ftrace_pid_function(ip, parent_ip);
113}
114
115static void set_ftrace_pid_function(ftrace_func_t func)
116{
117 /* do not set ftrace_pid_function to itself! */
118 if (func != ftrace_pid_func)
119 ftrace_pid_function = func;
120}
121
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200123 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200124 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200125 * This NULLs the ftrace function and in essence stops
126 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200127 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200128void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200129{
Steven Rostedt3d083392008-05-12 21:20:42 +0200130 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500131 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500132 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200133}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200134
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500135#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
136/*
137 * For those archs that do not test ftrace_trace_stop in their
138 * mcount call site, we need to do it from C.
139 */
140static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
141{
142 if (function_trace_stop)
143 return;
144
145 __ftrace_trace_function(ip, parent_ip);
146}
147#endif
148
Ingo Molnare309b412008-05-12 21:20:51 +0200149static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200150{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200151 ops->next = ftrace_list;
152 /*
153 * We are entering ops into the ftrace_list but another
154 * CPU might be walking that list. We need to make sure
155 * the ops->next pointer is valid before another CPU sees
156 * the ops pointer included into the ftrace_list.
157 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800158 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200159
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200160 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500161 ftrace_func_t func;
162
163 if (ops->next == &ftrace_list_end)
164 func = ops->func;
165 else
166 func = ftrace_list_func;
167
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400168 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500169 set_ftrace_pid_function(func);
170 func = ftrace_pid_func;
171 }
172
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200173 /*
174 * For one func, simply call it directly.
175 * For more than one func, call the chain.
176 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500177#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500178 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500179#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500180 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500181 ftrace_trace_function = ftrace_test_stop_func;
182#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200183 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200184
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200185 return 0;
186}
187
Ingo Molnare309b412008-05-12 21:20:51 +0200188static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191
192 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200193 * If we are removing the last function, then simply point
194 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195 */
196 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
197 ftrace_trace_function = ftrace_stub;
198 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500199 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200 }
201
202 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
203 if (*p == ops)
204 break;
205
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500206 if (*p != ops)
207 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200208
209 *p = (*p)->next;
210
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200211 if (ftrace_enabled) {
212 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500213 if (ftrace_list->next == &ftrace_list_end) {
214 ftrace_func_t func = ftrace_list->func;
215
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400216 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500217 set_ftrace_pid_function(func);
218 func = ftrace_pid_func;
219 }
220#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
221 ftrace_trace_function = func;
222#else
223 __ftrace_trace_function = func;
224#endif
225 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200226 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200227
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500228 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200229}
230
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500231static void ftrace_update_pid_func(void)
232{
233 ftrace_func_t func;
234
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500235 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900236 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500237
Matt Fleming33974092009-09-28 16:43:01 +0100238#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500239 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100240#else
241 func = __ftrace_trace_function;
242#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500243
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400244 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500245 set_ftrace_pid_function(func);
246 func = ftrace_pid_func;
247 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800248 if (func == ftrace_pid_func)
249 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500250 }
251
252#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
253 ftrace_trace_function = func;
254#else
255 __ftrace_trace_function = func;
256#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500257}
258
Steven Rostedt493762f2009-03-23 17:12:36 -0400259#ifdef CONFIG_FUNCTION_PROFILER
260struct ftrace_profile {
261 struct hlist_node node;
262 unsigned long ip;
263 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400264#ifdef CONFIG_FUNCTION_GRAPH_TRACER
265 unsigned long long time;
266#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400267};
268
269struct ftrace_profile_page {
270 struct ftrace_profile_page *next;
271 unsigned long index;
272 struct ftrace_profile records[];
273};
274
Steven Rostedtcafb1682009-03-24 20:50:39 -0400275struct ftrace_profile_stat {
276 atomic_t disabled;
277 struct hlist_head *hash;
278 struct ftrace_profile_page *pages;
279 struct ftrace_profile_page *start;
280 struct tracer_stat stat;
281};
282
Steven Rostedt493762f2009-03-23 17:12:36 -0400283#define PROFILE_RECORDS_SIZE \
284 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
285
286#define PROFILES_PER_PAGE \
287 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
288
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400289static int ftrace_profile_bits __read_mostly;
290static int ftrace_profile_enabled __read_mostly;
291
292/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400293static DEFINE_MUTEX(ftrace_profile_lock);
294
Steven Rostedtcafb1682009-03-24 20:50:39 -0400295static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400296
297#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
298
Steven Rostedt493762f2009-03-23 17:12:36 -0400299static void *
300function_stat_next(void *v, int idx)
301{
302 struct ftrace_profile *rec = v;
303 struct ftrace_profile_page *pg;
304
305 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
306
307 again:
Li Zefan0296e422009-06-26 11:15:37 +0800308 if (idx != 0)
309 rec++;
310
Steven Rostedt493762f2009-03-23 17:12:36 -0400311 if ((void *)rec >= (void *)&pg->records[pg->index]) {
312 pg = pg->next;
313 if (!pg)
314 return NULL;
315 rec = &pg->records[0];
316 if (!rec->counter)
317 goto again;
318 }
319
320 return rec;
321}
322
323static void *function_stat_start(struct tracer_stat *trace)
324{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400325 struct ftrace_profile_stat *stat =
326 container_of(trace, struct ftrace_profile_stat, stat);
327
328 if (!stat || !stat->start)
329 return NULL;
330
331 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400332}
333
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400334#ifdef CONFIG_FUNCTION_GRAPH_TRACER
335/* function graph compares on total time */
336static int function_stat_cmp(void *p1, void *p2)
337{
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
340
341 if (a->time < b->time)
342 return -1;
343 if (a->time > b->time)
344 return 1;
345 else
346 return 0;
347}
348#else
349/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400350static int function_stat_cmp(void *p1, void *p2)
351{
352 struct ftrace_profile *a = p1;
353 struct ftrace_profile *b = p2;
354
355 if (a->counter < b->counter)
356 return -1;
357 if (a->counter > b->counter)
358 return 1;
359 else
360 return 0;
361}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400362#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400363
364static int function_stat_headers(struct seq_file *m)
365{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400366#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400367 seq_printf(m, " Function "
368 "Hit Time Avg\n"
369 " -------- "
370 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400371#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400372 seq_printf(m, " Function Hit\n"
373 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400374#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400375 return 0;
376}
377
378static int function_stat_show(struct seq_file *m, void *v)
379{
380 struct ftrace_profile *rec = v;
381 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400382#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400383 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400384 static struct trace_seq s;
385 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400386#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400387
388 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400389 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400390
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400391#ifdef CONFIG_FUNCTION_GRAPH_TRACER
392 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400393 avg = rec->time;
394 do_div(avg, rec->counter);
395
396 mutex_lock(&mutex);
397 trace_seq_init(&s);
398 trace_print_graph_duration(rec->time, &s);
399 trace_seq_puts(&s, " ");
400 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400401 trace_print_seq(m, &s);
402 mutex_unlock(&mutex);
403#endif
404 seq_putc(m, '\n');
405
Steven Rostedt493762f2009-03-23 17:12:36 -0400406 return 0;
407}
408
Steven Rostedtcafb1682009-03-24 20:50:39 -0400409static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400410{
411 struct ftrace_profile_page *pg;
412
Steven Rostedtcafb1682009-03-24 20:50:39 -0400413 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400414
415 while (pg) {
416 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
417 pg->index = 0;
418 pg = pg->next;
419 }
420
Steven Rostedtcafb1682009-03-24 20:50:39 -0400421 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400422 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
423}
424
Steven Rostedtcafb1682009-03-24 20:50:39 -0400425int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400426{
427 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400428 int functions;
429 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400430 int i;
431
432 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400433 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400434 return 0;
435
Steven Rostedtcafb1682009-03-24 20:50:39 -0400436 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
437 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400438 return -ENOMEM;
439
Steven Rostedt318e0a72009-03-25 20:06:34 -0400440#ifdef CONFIG_DYNAMIC_FTRACE
441 functions = ftrace_update_tot_cnt;
442#else
443 /*
444 * We do not know the number of functions that exist because
445 * dynamic tracing is what counts them. With past experience
446 * we have around 20K functions. That should be more than enough.
447 * It is highly unlikely we will execute every function in
448 * the kernel.
449 */
450 functions = 20000;
451#endif
452
Steven Rostedtcafb1682009-03-24 20:50:39 -0400453 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400454
Steven Rostedt318e0a72009-03-25 20:06:34 -0400455 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
456
457 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400458 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400459 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400460 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400461 pg = pg->next;
462 }
463
464 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400465
466 out_free:
467 pg = stat->start;
468 while (pg) {
469 unsigned long tmp = (unsigned long)pg;
470
471 pg = pg->next;
472 free_page(tmp);
473 }
474
475 free_page((unsigned long)stat->pages);
476 stat->pages = NULL;
477 stat->start = NULL;
478
479 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400480}
481
Steven Rostedtcafb1682009-03-24 20:50:39 -0400482static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400483{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400484 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400485 int size;
486
Steven Rostedtcafb1682009-03-24 20:50:39 -0400487 stat = &per_cpu(ftrace_profile_stats, cpu);
488
489 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400490 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400491 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400492 return 0;
493 }
494
495 /*
496 * We are profiling all functions, but usually only a few thousand
497 * functions are hit. We'll make a hash of 1024 items.
498 */
499 size = FTRACE_PROFILE_HASH_SIZE;
500
Steven Rostedtcafb1682009-03-24 20:50:39 -0400501 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400502
Steven Rostedtcafb1682009-03-24 20:50:39 -0400503 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400504 return -ENOMEM;
505
Steven Rostedtcafb1682009-03-24 20:50:39 -0400506 if (!ftrace_profile_bits) {
507 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400508
Steven Rostedtcafb1682009-03-24 20:50:39 -0400509 for (; size; size >>= 1)
510 ftrace_profile_bits++;
511 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400512
Steven Rostedt318e0a72009-03-25 20:06:34 -0400513 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400514 if (ftrace_profile_pages_init(stat) < 0) {
515 kfree(stat->hash);
516 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400517 return -ENOMEM;
518 }
519
520 return 0;
521}
522
Steven Rostedtcafb1682009-03-24 20:50:39 -0400523static int ftrace_profile_init(void)
524{
525 int cpu;
526 int ret = 0;
527
528 for_each_online_cpu(cpu) {
529 ret = ftrace_profile_init_cpu(cpu);
530 if (ret)
531 break;
532 }
533
534 return ret;
535}
536
Steven Rostedt493762f2009-03-23 17:12:36 -0400537/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538static struct ftrace_profile *
539ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400540{
541 struct ftrace_profile *rec;
542 struct hlist_head *hhd;
543 struct hlist_node *n;
544 unsigned long key;
545
546 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400547 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400548
549 if (hlist_empty(hhd))
550 return NULL;
551
552 hlist_for_each_entry_rcu(rec, n, hhd, node) {
553 if (rec->ip == ip)
554 return rec;
555 }
556
557 return NULL;
558}
559
Steven Rostedtcafb1682009-03-24 20:50:39 -0400560static void ftrace_add_profile(struct ftrace_profile_stat *stat,
561 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400562{
563 unsigned long key;
564
565 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400566 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400567}
568
Steven Rostedt318e0a72009-03-25 20:06:34 -0400569/*
570 * The memory is already allocated, this simply finds a new record to use.
571 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400572static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400573ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400574{
575 struct ftrace_profile *rec = NULL;
576
Steven Rostedt318e0a72009-03-25 20:06:34 -0400577 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400578 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400579 goto out;
580
Steven Rostedt493762f2009-03-23 17:12:36 -0400581 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400582 * Try to find the function again since an NMI
583 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400584 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400585 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400588
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 if (stat->pages->index == PROFILES_PER_PAGE) {
590 if (!stat->pages->next)
591 goto out;
592 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400593 }
594
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400598
Steven Rostedt493762f2009-03-23 17:12:36 -0400599 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400600 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400601
602 return rec;
603}
604
Steven Rostedt493762f2009-03-23 17:12:36 -0400605static void
606function_profile_call(unsigned long ip, unsigned long parent_ip)
607{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400608 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400609 struct ftrace_profile *rec;
610 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400611
612 if (!ftrace_profile_enabled)
613 return;
614
Steven Rostedt493762f2009-03-23 17:12:36 -0400615 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400616
617 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400618 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400619 goto out;
620
621 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400622 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400623 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 if (!rec)
625 goto out;
626 }
627
628 rec->counter++;
629 out:
630 local_irq_restore(flags);
631}
632
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633#ifdef CONFIG_FUNCTION_GRAPH_TRACER
634static int profile_graph_entry(struct ftrace_graph_ent *trace)
635{
636 function_profile_call(trace->func, 0);
637 return 1;
638}
639
640static void profile_graph_return(struct ftrace_graph_ret *trace)
641{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400643 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400644 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400646
647 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400649 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 goto out;
651
Steven Rostedta2a16d62009-03-24 23:17:58 -0400652 calltime = trace->rettime - trace->calltime;
653
654 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
655 int index;
656
657 index = trace->depth;
658
659 /* Append this call time to the parent time to subtract */
660 if (index)
661 current->ret_stack[index - 1].subtime += calltime;
662
663 if (current->ret_stack[index].subtime < calltime)
664 calltime -= current->ret_stack[index].subtime;
665 else
666 calltime = 0;
667 }
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400670 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400671 rec->time += calltime;
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400674 local_irq_restore(flags);
675}
676
677static int register_ftrace_profiler(void)
678{
679 return register_ftrace_graph(&profile_graph_return,
680 &profile_graph_entry);
681}
682
683static void unregister_ftrace_profiler(void)
684{
685 unregister_ftrace_graph();
686}
687#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400688static struct ftrace_ops ftrace_profile_ops __read_mostly =
689{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400690 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400691};
692
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400693static int register_ftrace_profiler(void)
694{
695 return register_ftrace_function(&ftrace_profile_ops);
696}
697
698static void unregister_ftrace_profiler(void)
699{
700 unregister_ftrace_function(&ftrace_profile_ops);
701}
702#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
703
Steven Rostedt493762f2009-03-23 17:12:36 -0400704static ssize_t
705ftrace_profile_write(struct file *filp, const char __user *ubuf,
706 size_t cnt, loff_t *ppos)
707{
708 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400709 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400710 int ret;
711
712 if (cnt >= sizeof(buf))
713 return -EINVAL;
714
715 if (copy_from_user(&buf, ubuf, cnt))
716 return -EFAULT;
717
718 buf[cnt] = 0;
719
720 ret = strict_strtoul(buf, 10, &val);
721 if (ret < 0)
722 return ret;
723
724 val = !!val;
725
726 mutex_lock(&ftrace_profile_lock);
727 if (ftrace_profile_enabled ^ val) {
728 if (val) {
729 ret = ftrace_profile_init();
730 if (ret < 0) {
731 cnt = ret;
732 goto out;
733 }
734
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400735 ret = register_ftrace_profiler();
736 if (ret < 0) {
737 cnt = ret;
738 goto out;
739 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 ftrace_profile_enabled = 1;
741 } else {
742 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400743 /*
744 * unregister_ftrace_profiler calls stop_machine
745 * so this acts like an synchronize_sched.
746 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400747 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 }
749 }
750 out:
751 mutex_unlock(&ftrace_profile_lock);
752
Jiri Olsacf8517c2009-10-23 19:36:16 -0400753 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400754
755 return cnt;
756}
757
758static ssize_t
759ftrace_profile_read(struct file *filp, char __user *ubuf,
760 size_t cnt, loff_t *ppos)
761{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400762 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400763 int r;
764
765 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
766 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
767}
768
769static const struct file_operations ftrace_profile_fops = {
770 .open = tracing_open_generic,
771 .read = ftrace_profile_read,
772 .write = ftrace_profile_write,
773};
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775/* used to initialize the real stat files */
776static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400777 .name = "functions",
778 .stat_start = function_stat_start,
779 .stat_next = function_stat_next,
780 .stat_cmp = function_stat_cmp,
781 .stat_headers = function_stat_headers,
782 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400783};
784
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400785static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400786{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400787 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400788 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400789 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400790 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400792
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 for_each_possible_cpu(cpu) {
794 stat = &per_cpu(ftrace_profile_stats, cpu);
795
796 /* allocate enough for function name + cpu number */
797 name = kmalloc(32, GFP_KERNEL);
798 if (!name) {
799 /*
800 * The files created are permanent, if something happens
801 * we still do not free memory.
802 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400803 WARN(1,
804 "Could not allocate stat file for cpu %d\n",
805 cpu);
806 return;
807 }
808 stat->stat = function_stats;
809 snprintf(name, 32, "function%d", cpu);
810 stat->stat.name = name;
811 ret = register_stat_tracer(&stat->stat);
812 if (ret) {
813 WARN(1,
814 "Could not register function stat for cpu %d\n",
815 cpu);
816 kfree(name);
817 return;
818 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 }
820
821 entry = debugfs_create_file("function_profile_enabled", 0644,
822 d_tracer, NULL, &ftrace_profile_fops);
823 if (!entry)
824 pr_warning("Could not create debugfs "
825 "'function_profile_enabled' entry\n");
826}
827
828#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400829static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400830{
831}
832#endif /* CONFIG_FUNCTION_PROFILER */
833
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100834static struct pid * const ftrace_swapper_pid = &init_struct_pid;
835
Steven Rostedt3d083392008-05-12 21:20:42 +0200836#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100837
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400838#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400839# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400840#endif
841
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500842static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
843
Steven Rostedtb6887d72009-02-17 12:32:04 -0500844struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500845 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500846 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500847 unsigned long flags;
848 unsigned long ip;
849 void *data;
850 struct rcu_head rcu;
851};
852
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200853enum {
854 FTRACE_ENABLE_CALLS = (1 << 0),
855 FTRACE_DISABLE_CALLS = (1 << 1),
856 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
857 FTRACE_ENABLE_MCOUNT = (1 << 3),
858 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500859 FTRACE_START_FUNC_RET = (1 << 5),
860 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200861};
862
Steven Rostedt5072c592008-05-12 21:20:43 +0200863static int ftrace_filtered;
864
Lai Jiangshane94142a2009-03-13 17:51:27 +0800865static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200866
Steven Rostedt41c52c02008-05-22 11:46:33 -0400867static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200868
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200869struct ftrace_page {
870 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500871 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200872 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700873};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200874
875#define ENTRIES_PER_PAGE \
876 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
877
878/* estimate from running different kernels */
879#define NR_TO_INIT 10000
880
881static struct ftrace_page *ftrace_pages_start;
882static struct ftrace_page *ftrace_pages;
883
Steven Rostedt37ad5082008-05-12 21:20:48 +0200884static struct dyn_ftrace *ftrace_free_records;
885
Steven Rostedt265c8312009-02-13 12:43:56 -0500886/*
887 * This is a double for. Do not use 'break' to break out of the loop,
888 * you must use a goto.
889 */
890#define do_for_each_ftrace_rec(pg, rec) \
891 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
892 int _____i; \
893 for (_____i = 0; _____i < pg->index; _____i++) { \
894 rec = &pg->records[_____i];
895
896#define while_for_each_ftrace_rec() \
897 } \
898 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530899
Ingo Molnare309b412008-05-12 21:20:51 +0200900static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200901{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800902 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200903 ftrace_free_records = rec;
904 rec->flags |= FTRACE_FL_FREE;
905}
906
Ingo Molnare309b412008-05-12 21:20:51 +0200907static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200908{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200909 struct dyn_ftrace *rec;
910
911 /* First check for freed records */
912 if (ftrace_free_records) {
913 rec = ftrace_free_records;
914
Steven Rostedt37ad5082008-05-12 21:20:48 +0200915 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400916 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200917 ftrace_free_records = NULL;
918 return NULL;
919 }
920
Lai Jiangshanee000b72009-03-24 13:38:06 +0800921 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200922 memset(rec, 0, sizeof(*rec));
923 return rec;
924 }
925
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200926 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400927 if (!ftrace_pages->next) {
928 /* allocate another page */
929 ftrace_pages->next =
930 (void *)get_zeroed_page(GFP_KERNEL);
931 if (!ftrace_pages->next)
932 return NULL;
933 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200934 ftrace_pages = ftrace_pages->next;
935 }
936
937 return &ftrace_pages->records[ftrace_pages->index++];
938}
939
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400940static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200941ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200942{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400943 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200944
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800945 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400946 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200947
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400948 rec = ftrace_alloc_dyn_node(ip);
949 if (!rec)
950 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200951
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400952 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800953 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800954 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200955
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400956 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200957}
958
Steven Rostedt05736a42008-09-22 14:55:47 -0700959static void print_ip_ins(const char *fmt, unsigned char *p)
960{
961 int i;
962
963 printk(KERN_CONT "%s", fmt);
964
965 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
966 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
967}
968
Steven Rostedt31e88902008-11-14 16:21:19 -0800969static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800970{
971 switch (failed) {
972 case -EFAULT:
973 FTRACE_WARN_ON_ONCE(1);
974 pr_info("ftrace faulted on modifying ");
975 print_ip_sym(ip);
976 break;
977 case -EINVAL:
978 FTRACE_WARN_ON_ONCE(1);
979 pr_info("ftrace failed to modify ");
980 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800981 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800982 printk(KERN_CONT "\n");
983 break;
984 case -EPERM:
985 FTRACE_WARN_ON_ONCE(1);
986 pr_info("ftrace faulted on writing ");
987 print_ip_sym(ip);
988 break;
989 default:
990 FTRACE_WARN_ON_ONCE(1);
991 pr_info("ftrace faulted on unknown error ");
992 print_ip_sym(ip);
993 }
994}
995
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200996
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -0500997/* Return 1 if the address range is reserved for ftrace */
998int ftrace_text_reserved(void *start, void *end)
999{
1000 struct dyn_ftrace *rec;
1001 struct ftrace_page *pg;
1002
1003 do_for_each_ftrace_rec(pg, rec) {
1004 if (rec->ip <= (unsigned long)end &&
1005 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1006 return 1;
1007 } while_for_each_ftrace_rec();
1008 return 0;
1009}
1010
1011
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301012static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001013__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001014{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001015 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001016 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001017
Shaohua Lif0001202009-01-09 11:29:42 +08001018 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001019
Steven Rostedt982c3502008-11-15 16:31:41 -05001020 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001021 * If this record is not to be traced or we want to disable it,
1022 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001023 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001024 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001025 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001026 * If we want to enable it and filtering is on, enable it only if
1027 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001028 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001029 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1030 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1031 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001032 }
1033
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001034 /* If the state of this record hasn't changed, then do nothing */
1035 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1036 return 0;
1037
1038 if (flag) {
1039 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001040 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001041 }
1042
1043 rec->flags &= ~FTRACE_FL_ENABLED;
1044 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001045}
1046
1047static void ftrace_replace_code(int enable)
1048{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001049 struct dyn_ftrace *rec;
1050 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001051 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001052
Steven Rostedt265c8312009-02-13 12:43:56 -05001053 do_for_each_ftrace_rec(pg, rec) {
1054 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001055 * Skip over free records, records that have
1056 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001057 */
1058 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001059 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001060 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001061 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001062
Steven Rostedt265c8312009-02-13 12:43:56 -05001063 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001064 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001065 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001066 ftrace_bug(failed, rec->ip);
1067 /* Stop processing */
1068 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001069 }
1070 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001071}
1072
Ingo Molnare309b412008-05-12 21:20:51 +02001073static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001074ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001075{
1076 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001077 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001078
1079 ip = rec->ip;
1080
Shaohua Li25aac9d2009-01-09 11:29:40 +08001081 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001082 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001083 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001084 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301085 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001086 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301087 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001088}
1089
Steven Rostedt000ab692009-02-17 13:35:06 -05001090/*
1091 * archs can override this function if they must do something
1092 * before the modifying code is performed.
1093 */
1094int __weak ftrace_arch_code_modify_prepare(void)
1095{
1096 return 0;
1097}
1098
1099/*
1100 * archs can override this function if they must do something
1101 * after the modifying code is performed.
1102 */
1103int __weak ftrace_arch_code_modify_post_process(void)
1104{
1105 return 0;
1106}
1107
Ingo Molnare309b412008-05-12 21:20:51 +02001108static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001109{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001110 int *command = data;
1111
Steven Rostedta3583242008-11-11 15:01:42 -05001112 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001113 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001114 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001115 ftrace_replace_code(0);
1116
1117 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1118 ftrace_update_ftrace_func(ftrace_trace_function);
1119
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001120 if (*command & FTRACE_START_FUNC_RET)
1121 ftrace_enable_ftrace_graph_caller();
1122 else if (*command & FTRACE_STOP_FUNC_RET)
1123 ftrace_disable_ftrace_graph_caller();
1124
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001125 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001126}
1127
Ingo Molnare309b412008-05-12 21:20:51 +02001128static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001129{
Steven Rostedt000ab692009-02-17 13:35:06 -05001130 int ret;
1131
1132 ret = ftrace_arch_code_modify_prepare();
1133 FTRACE_WARN_ON(ret);
1134 if (ret)
1135 return;
1136
Rusty Russell784e2d72008-07-28 12:16:31 -05001137 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001138
1139 ret = ftrace_arch_code_modify_post_process();
1140 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001141}
1142
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001143static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001144static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001145
1146static void ftrace_startup_enable(int command)
1147{
1148 if (saved_ftrace_func != ftrace_trace_function) {
1149 saved_ftrace_func = ftrace_trace_function;
1150 command |= FTRACE_UPDATE_TRACE_FUNC;
1151 }
1152
1153 if (!command || !ftrace_enabled)
1154 return;
1155
1156 ftrace_run_update_code(command);
1157}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001158
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001159static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001160{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001161 if (unlikely(ftrace_disabled))
1162 return;
1163
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001164 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001165 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001166
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001167 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001168}
1169
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001170static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001171{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001172 if (unlikely(ftrace_disabled))
1173 return;
1174
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001175 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001176 /*
1177 * Just warn in case of unbalance, no need to kill ftrace, it's not
1178 * critical but the ftrace_call callers may be never nopped again after
1179 * further ftrace uses.
1180 */
1181 WARN_ON_ONCE(ftrace_start_up < 0);
1182
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001183 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001184 command |= FTRACE_DISABLE_CALLS;
1185
1186 if (saved_ftrace_func != ftrace_trace_function) {
1187 saved_ftrace_func = ftrace_trace_function;
1188 command |= FTRACE_UPDATE_TRACE_FUNC;
1189 }
1190
1191 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001192 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001193
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001194 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001195}
1196
Ingo Molnare309b412008-05-12 21:20:51 +02001197static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001198{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001199 int command = FTRACE_ENABLE_MCOUNT;
1200
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001201 if (unlikely(ftrace_disabled))
1202 return;
1203
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001204 /* Force update next time */
1205 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001206 /* ftrace_start_up is true if we want ftrace running */
1207 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001208 command |= FTRACE_ENABLE_CALLS;
1209
1210 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001211}
1212
Ingo Molnare309b412008-05-12 21:20:51 +02001213static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001214{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001215 int command = FTRACE_DISABLE_MCOUNT;
1216
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001217 if (unlikely(ftrace_disabled))
1218 return;
1219
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001220 /* ftrace_start_up is true if ftrace is running */
1221 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001222 command |= FTRACE_DISABLE_CALLS;
1223
1224 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001225}
1226
Steven Rostedt3d083392008-05-12 21:20:42 +02001227static cycle_t ftrace_update_time;
1228static unsigned long ftrace_update_cnt;
1229unsigned long ftrace_update_tot_cnt;
1230
Steven Rostedt31e88902008-11-14 16:21:19 -08001231static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001232{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001233 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301234 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001235
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001236 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001237 ftrace_update_cnt = 0;
1238
Lai Jiangshane94142a2009-03-13 17:51:27 +08001239 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301240
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001241 /* If something went wrong, bail without enabling anything */
1242 if (unlikely(ftrace_disabled))
1243 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001244
Lai Jiangshane94142a2009-03-13 17:51:27 +08001245 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001246 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001247 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301248
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001249 /*
1250 * Do the initial record convertion from mcount jump
1251 * to the NOP instructions.
1252 */
1253 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001254 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001255 continue;
1256 }
1257
1258 p->flags |= FTRACE_FL_CONVERTED;
1259 ftrace_update_cnt++;
1260
1261 /*
1262 * If the tracing is enabled, go ahead and enable the record.
1263 *
1264 * The reason not to enable the record immediatelly is the
1265 * inherent check of ftrace_make_nop/ftrace_make_call for
1266 * correct previous instructions. Making first the NOP
1267 * conversion puts the module to the correct state, thus
1268 * passing the ftrace_make_call check.
1269 */
1270 if (ftrace_start_up) {
1271 int failed = __ftrace_replace_code(p, 1);
1272 if (failed) {
1273 ftrace_bug(failed, p->ip);
1274 ftrace_free_rec(p);
1275 }
1276 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001277 }
1278
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001279 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001280 ftrace_update_time = stop - start;
1281 ftrace_update_tot_cnt += ftrace_update_cnt;
1282
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001283 return 0;
1284}
1285
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001286static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001287{
1288 struct ftrace_page *pg;
1289 int cnt;
1290 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001291
1292 /* allocate a few pages */
1293 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1294 if (!ftrace_pages_start)
1295 return -1;
1296
1297 /*
1298 * Allocate a few more pages.
1299 *
1300 * TODO: have some parser search vmlinux before
1301 * final linking to find all calls to ftrace.
1302 * Then we can:
1303 * a) know how many pages to allocate.
1304 * and/or
1305 * b) set up the table then.
1306 *
1307 * The dynamic code is still necessary for
1308 * modules.
1309 */
1310
1311 pg = ftrace_pages = ftrace_pages_start;
1312
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001313 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001314 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001315 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001316
1317 for (i = 0; i < cnt; i++) {
1318 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1319
1320 /* If we fail, we'll try later anyway */
1321 if (!pg->next)
1322 break;
1323
1324 pg = pg->next;
1325 }
1326
1327 return 0;
1328}
1329
Steven Rostedt5072c592008-05-12 21:20:43 +02001330enum {
1331 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001332 FTRACE_ITER_NOTRACE = (1 << 1),
1333 FTRACE_ITER_FAILURES = (1 << 2),
1334 FTRACE_ITER_PRINTALL = (1 << 3),
1335 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001336};
1337
1338#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1339
1340struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001341 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001342 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001343 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001344 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001345 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001346};
1347
Ingo Molnare309b412008-05-12 21:20:51 +02001348static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001349t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1350{
1351 struct ftrace_iterator *iter = m->private;
1352 struct hlist_node *hnd = v;
1353 struct hlist_head *hhd;
1354
1355 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1356
1357 (*pos)++;
1358
1359 retry:
1360 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1361 return NULL;
1362
1363 hhd = &ftrace_func_hash[iter->hidx];
1364
1365 if (hlist_empty(hhd)) {
1366 iter->hidx++;
1367 hnd = NULL;
1368 goto retry;
1369 }
1370
1371 if (!hnd)
1372 hnd = hhd->first;
1373 else {
1374 hnd = hnd->next;
1375 if (!hnd) {
1376 iter->hidx++;
1377 goto retry;
1378 }
1379 }
1380
1381 return hnd;
1382}
1383
1384static void *t_hash_start(struct seq_file *m, loff_t *pos)
1385{
1386 struct ftrace_iterator *iter = m->private;
1387 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001388 loff_t l;
1389
1390 if (!(iter->flags & FTRACE_ITER_HASH))
1391 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001392
1393 iter->flags |= FTRACE_ITER_HASH;
1394
Li Zefand82d6242009-06-24 09:54:54 +08001395 iter->hidx = 0;
1396 for (l = 0; l <= *pos; ) {
1397 p = t_hash_next(m, p, &l);
1398 if (!p)
1399 break;
1400 }
1401 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001402}
1403
1404static int t_hash_show(struct seq_file *m, void *v)
1405{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001406 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001407 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001408
Steven Rostedtb6887d72009-02-17 12:32:04 -05001409 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001410
Steven Rostedt809dcf22009-02-16 23:06:01 -05001411 if (rec->ops->print)
1412 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1413
Steven Rostedtb375a112009-09-17 00:05:58 -04001414 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001415
1416 if (rec->data)
1417 seq_printf(m, ":%p", rec->data);
1418 seq_putc(m, '\n');
1419
1420 return 0;
1421}
1422
1423static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001424t_next(struct seq_file *m, void *v, loff_t *pos)
1425{
1426 struct ftrace_iterator *iter = m->private;
1427 struct dyn_ftrace *rec = NULL;
1428
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001429 if (iter->flags & FTRACE_ITER_HASH)
1430 return t_hash_next(m, v, pos);
1431
Steven Rostedt5072c592008-05-12 21:20:43 +02001432 (*pos)++;
1433
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001434 if (iter->flags & FTRACE_ITER_PRINTALL)
1435 return NULL;
1436
Steven Rostedt5072c592008-05-12 21:20:43 +02001437 retry:
1438 if (iter->idx >= iter->pg->index) {
1439 if (iter->pg->next) {
1440 iter->pg = iter->pg->next;
1441 iter->idx = 0;
1442 goto retry;
1443 }
1444 } else {
1445 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001446 if ((rec->flags & FTRACE_FL_FREE) ||
1447
1448 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301449 (rec->flags & FTRACE_FL_FAILED)) ||
1450
1451 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001452 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301453
Steven Rostedt0183fb12008-11-07 22:36:02 -05001454 ((iter->flags & FTRACE_ITER_FILTER) &&
1455 !(rec->flags & FTRACE_FL_FILTER)) ||
1456
Steven Rostedt41c52c02008-05-22 11:46:33 -04001457 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1458 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001459 rec = NULL;
1460 goto retry;
1461 }
1462 }
1463
Steven Rostedt5072c592008-05-12 21:20:43 +02001464 return rec;
1465}
1466
1467static void *t_start(struct seq_file *m, loff_t *pos)
1468{
1469 struct ftrace_iterator *iter = m->private;
1470 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001471 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001472
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001473 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001474 /*
1475 * For set_ftrace_filter reading, if we have the filter
1476 * off, we can short cut and just print out that all
1477 * functions are enabled.
1478 */
1479 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1480 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001481 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001482 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001483 return iter;
1484 }
1485
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001486 if (iter->flags & FTRACE_ITER_HASH)
1487 return t_hash_start(m, pos);
1488
Li Zefan694ce0a2009-06-24 09:54:19 +08001489 iter->pg = ftrace_pages_start;
1490 iter->idx = 0;
1491 for (l = 0; l <= *pos; ) {
1492 p = t_next(m, p, &l);
1493 if (!p)
1494 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001495 }
walimis5821e1b2008-11-15 15:19:06 +08001496
Li Zefan694ce0a2009-06-24 09:54:19 +08001497 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001498 return t_hash_start(m, pos);
1499
Steven Rostedt5072c592008-05-12 21:20:43 +02001500 return p;
1501}
1502
1503static void t_stop(struct seq_file *m, void *p)
1504{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001505 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001506}
1507
1508static int t_show(struct seq_file *m, void *v)
1509{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001510 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001511 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001512
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001513 if (iter->flags & FTRACE_ITER_HASH)
1514 return t_hash_show(m, v);
1515
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001516 if (iter->flags & FTRACE_ITER_PRINTALL) {
1517 seq_printf(m, "#### all functions enabled ####\n");
1518 return 0;
1519 }
1520
Steven Rostedt5072c592008-05-12 21:20:43 +02001521 if (!rec)
1522 return 0;
1523
Steven Rostedtb375a112009-09-17 00:05:58 -04001524 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001525
1526 return 0;
1527}
1528
James Morris88e9d342009-09-22 16:43:43 -07001529static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001530 .start = t_start,
1531 .next = t_next,
1532 .stop = t_stop,
1533 .show = t_show,
1534};
1535
Ingo Molnare309b412008-05-12 21:20:51 +02001536static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001537ftrace_avail_open(struct inode *inode, struct file *file)
1538{
1539 struct ftrace_iterator *iter;
1540 int ret;
1541
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001542 if (unlikely(ftrace_disabled))
1543 return -ENODEV;
1544
Steven Rostedt5072c592008-05-12 21:20:43 +02001545 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1546 if (!iter)
1547 return -ENOMEM;
1548
1549 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001550
1551 ret = seq_open(file, &show_ftrace_seq_ops);
1552 if (!ret) {
1553 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001554
Steven Rostedt5072c592008-05-12 21:20:43 +02001555 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001556 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001557 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001558 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001559
1560 return ret;
1561}
1562
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301563static int
1564ftrace_failures_open(struct inode *inode, struct file *file)
1565{
1566 int ret;
1567 struct seq_file *m;
1568 struct ftrace_iterator *iter;
1569
1570 ret = ftrace_avail_open(inode, file);
1571 if (!ret) {
1572 m = (struct seq_file *)file->private_data;
1573 iter = (struct ftrace_iterator *)m->private;
1574 iter->flags = FTRACE_ITER_FAILURES;
1575 }
1576
1577 return ret;
1578}
1579
1580
Steven Rostedt41c52c02008-05-22 11:46:33 -04001581static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001582{
1583 struct ftrace_page *pg;
1584 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001585 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001586
Steven Rostedt52baf112009-02-14 01:15:39 -05001587 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001588 if (enable)
1589 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001590 do_for_each_ftrace_rec(pg, rec) {
1591 if (rec->flags & FTRACE_FL_FAILED)
1592 continue;
1593 rec->flags &= ~type;
1594 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001595 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001596}
1597
Ingo Molnare309b412008-05-12 21:20:51 +02001598static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001599ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001600{
1601 struct ftrace_iterator *iter;
1602 int ret = 0;
1603
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001604 if (unlikely(ftrace_disabled))
1605 return -ENODEV;
1606
Steven Rostedt5072c592008-05-12 21:20:43 +02001607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1608 if (!iter)
1609 return -ENOMEM;
1610
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001611 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1612 kfree(iter);
1613 return -ENOMEM;
1614 }
1615
Steven Rostedt41c52c02008-05-22 11:46:33 -04001616 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001617 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001618 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001619 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001620
1621 if (file->f_mode & FMODE_READ) {
1622 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001623 iter->flags = enable ? FTRACE_ITER_FILTER :
1624 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001625
1626 ret = seq_open(file, &show_ftrace_seq_ops);
1627 if (!ret) {
1628 struct seq_file *m = file->private_data;
1629 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001630 } else {
1631 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001632 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001633 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001634 } else
1635 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001636 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001637
1638 return ret;
1639}
1640
Steven Rostedt41c52c02008-05-22 11:46:33 -04001641static int
1642ftrace_filter_open(struct inode *inode, struct file *file)
1643{
1644 return ftrace_regex_open(inode, file, 1);
1645}
1646
1647static int
1648ftrace_notrace_open(struct inode *inode, struct file *file)
1649{
1650 return ftrace_regex_open(inode, file, 0);
1651}
1652
Ingo Molnare309b412008-05-12 21:20:51 +02001653static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001654ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001655{
1656 loff_t ret;
1657
1658 if (file->f_mode & FMODE_READ)
1659 ret = seq_lseek(file, offset, origin);
1660 else
1661 file->f_pos = ret = 1;
1662
1663 return ret;
1664}
1665
Steven Rostedt64e7c442009-02-13 17:08:48 -05001666static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001667{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001668 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001669 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001670
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001671 switch (type) {
1672 case MATCH_FULL:
1673 if (strcmp(str, regex) == 0)
1674 matched = 1;
1675 break;
1676 case MATCH_FRONT_ONLY:
1677 if (strncmp(str, regex, len) == 0)
1678 matched = 1;
1679 break;
1680 case MATCH_MIDDLE_ONLY:
1681 if (strstr(str, regex))
1682 matched = 1;
1683 break;
1684 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001685 slen = strlen(str);
1686 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001687 matched = 1;
1688 break;
1689 }
1690
1691 return matched;
1692}
1693
Steven Rostedt64e7c442009-02-13 17:08:48 -05001694static int
1695ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1696{
1697 char str[KSYM_SYMBOL_LEN];
1698
1699 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1700 return ftrace_match(str, regex, len, type);
1701}
1702
Li Zefan311d16d2009-12-08 11:15:11 +08001703static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001704{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001705 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001706 struct ftrace_page *pg;
1707 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001708 unsigned long flag;
1709 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001710 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001711 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001712 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001713
Steven Rostedt6a24a242009-02-17 11:20:26 -05001714 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001715 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001716
1717 search_len = strlen(search);
1718
Steven Rostedt52baf112009-02-14 01:15:39 -05001719 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001720 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001721
Steven Rostedt265c8312009-02-13 12:43:56 -05001722 if (rec->flags & FTRACE_FL_FAILED)
1723 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001724
1725 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001726 if (not)
1727 rec->flags &= ~flag;
1728 else
1729 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001730 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001731 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001732 /*
1733 * Only enable filtering if we have a function that
1734 * is filtered on.
1735 */
1736 if (enable && (rec->flags & FTRACE_FL_FILTER))
1737 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001738 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001739 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001740
1741 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001742}
1743
Steven Rostedt64e7c442009-02-13 17:08:48 -05001744static int
1745ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1746 char *regex, int len, int type)
1747{
1748 char str[KSYM_SYMBOL_LEN];
1749 char *modname;
1750
1751 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1752
1753 if (!modname || strcmp(modname, mod))
1754 return 0;
1755
1756 /* blank search means to match all funcs in the mod */
1757 if (len)
1758 return ftrace_match(str, regex, len, type);
1759 else
1760 return 1;
1761}
1762
Li Zefan311d16d2009-12-08 11:15:11 +08001763static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001764{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001765 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001766 struct ftrace_page *pg;
1767 struct dyn_ftrace *rec;
1768 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001769 char *search = buff;
1770 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001771 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001772 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001773
Steven Rostedt6a24a242009-02-17 11:20:26 -05001774 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1775
Steven Rostedt64e7c442009-02-13 17:08:48 -05001776 /* blank or '*' mean the same */
1777 if (strcmp(buff, "*") == 0)
1778 buff[0] = 0;
1779
1780 /* handle the case of 'dont filter this module' */
1781 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1782 buff[0] = 0;
1783 not = 1;
1784 }
1785
1786 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001787 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001788 search_len = strlen(search);
1789 }
1790
Steven Rostedt52baf112009-02-14 01:15:39 -05001791 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001792 do_for_each_ftrace_rec(pg, rec) {
1793
1794 if (rec->flags & FTRACE_FL_FAILED)
1795 continue;
1796
1797 if (ftrace_match_module_record(rec, mod,
1798 search, search_len, type)) {
1799 if (not)
1800 rec->flags &= ~flag;
1801 else
1802 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001803 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001804 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001805 if (enable && (rec->flags & FTRACE_FL_FILTER))
1806 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001807
1808 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001809 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001810
1811 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001812}
1813
Steven Rostedtf6180772009-02-14 00:40:25 -05001814/*
1815 * We register the module command as a template to show others how
1816 * to register the a command as well.
1817 */
1818
1819static int
1820ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1821{
1822 char *mod;
1823
1824 /*
1825 * cmd == 'mod' because we only registered this func
1826 * for the 'mod' ftrace_func_command.
1827 * But if you register one func with multiple commands,
1828 * you can tell which command was used by the cmd
1829 * parameter.
1830 */
1831
1832 /* we must have a module name */
1833 if (!param)
1834 return -EINVAL;
1835
1836 mod = strsep(&param, ":");
1837 if (!strlen(mod))
1838 return -EINVAL;
1839
Li Zefan311d16d2009-12-08 11:15:11 +08001840 if (ftrace_match_module_records(func, mod, enable))
1841 return 0;
1842 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001843}
1844
1845static struct ftrace_func_command ftrace_mod_cmd = {
1846 .name = "mod",
1847 .func = ftrace_mod_callback,
1848};
1849
1850static int __init ftrace_mod_cmd_init(void)
1851{
1852 return register_ftrace_command(&ftrace_mod_cmd);
1853}
1854device_initcall(ftrace_mod_cmd_init);
1855
Steven Rostedt59df055f2009-02-14 15:29:06 -05001856static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001857function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001858{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001859 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001860 struct hlist_head *hhd;
1861 struct hlist_node *n;
1862 unsigned long key;
1863 int resched;
1864
1865 key = hash_long(ip, FTRACE_HASH_BITS);
1866
1867 hhd = &ftrace_func_hash[key];
1868
1869 if (hlist_empty(hhd))
1870 return;
1871
1872 /*
1873 * Disable preemption for these calls to prevent a RCU grace
1874 * period. This syncs the hash iteration and freeing of items
1875 * on the hash. rcu_read_lock is too dangerous here.
1876 */
1877 resched = ftrace_preempt_disable();
1878 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1879 if (entry->ip == ip)
1880 entry->ops->func(ip, parent_ip, &entry->data);
1881 }
1882 ftrace_preempt_enable(resched);
1883}
1884
Steven Rostedtb6887d72009-02-17 12:32:04 -05001885static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001886{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001887 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001888};
1889
Steven Rostedtb6887d72009-02-17 12:32:04 -05001890static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001891
Steven Rostedtb6887d72009-02-17 12:32:04 -05001892static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001893{
1894 int i;
1895
Steven Rostedtb6887d72009-02-17 12:32:04 -05001896 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001897 return;
1898
1899 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1900 struct hlist_head *hhd = &ftrace_func_hash[i];
1901 if (hhd->first)
1902 break;
1903 }
1904 /* Nothing registered? */
1905 if (i == FTRACE_FUNC_HASHSIZE)
1906 return;
1907
Steven Rostedtb6887d72009-02-17 12:32:04 -05001908 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001909 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001910 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001911}
1912
Steven Rostedtb6887d72009-02-17 12:32:04 -05001913static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001914{
1915 int i;
1916
Steven Rostedtb6887d72009-02-17 12:32:04 -05001917 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001918 return;
1919
1920 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1921 struct hlist_head *hhd = &ftrace_func_hash[i];
1922 if (hhd->first)
1923 return;
1924 }
1925
1926 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001927 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001928 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001929 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001930}
1931
1932
1933static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1934{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001935 struct ftrace_func_probe *entry =
1936 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001937
1938 if (entry->ops->free)
1939 entry->ops->free(&entry->data);
1940 kfree(entry);
1941}
1942
1943
1944int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001945register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001946 void *data)
1947{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001948 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001949 struct ftrace_page *pg;
1950 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001951 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001952 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001953 int count = 0;
1954 char *search;
1955
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001956 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001957 len = strlen(search);
1958
Steven Rostedtb6887d72009-02-17 12:32:04 -05001959 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001960 if (WARN_ON(not))
1961 return -EINVAL;
1962
1963 mutex_lock(&ftrace_lock);
1964 do_for_each_ftrace_rec(pg, rec) {
1965
1966 if (rec->flags & FTRACE_FL_FAILED)
1967 continue;
1968
1969 if (!ftrace_match_record(rec, search, len, type))
1970 continue;
1971
1972 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1973 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001974 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001975 if (!count)
1976 count = -ENOMEM;
1977 goto out_unlock;
1978 }
1979
1980 count++;
1981
1982 entry->data = data;
1983
1984 /*
1985 * The caller might want to do something special
1986 * for each function we find. We call the callback
1987 * to give the caller an opportunity to do so.
1988 */
1989 if (ops->callback) {
1990 if (ops->callback(rec->ip, &entry->data) < 0) {
1991 /* caller does not like this func */
1992 kfree(entry);
1993 continue;
1994 }
1995 }
1996
1997 entry->ops = ops;
1998 entry->ip = rec->ip;
1999
2000 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2001 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2002
2003 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002004 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002005
2006 out_unlock:
2007 mutex_unlock(&ftrace_lock);
2008
2009 return count;
2010}
2011
2012enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002013 PROBE_TEST_FUNC = 1,
2014 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002015};
2016
2017static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002018__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002019 void *data, int flags)
2020{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002021 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002022 struct hlist_node *n, *tmp;
2023 char str[KSYM_SYMBOL_LEN];
2024 int type = MATCH_FULL;
2025 int i, len = 0;
2026 char *search;
2027
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002028 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002029 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002030 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002031 int not;
2032
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002033 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002034 len = strlen(search);
2035
Steven Rostedtb6887d72009-02-17 12:32:04 -05002036 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002037 if (WARN_ON(not))
2038 return;
2039 }
2040
2041 mutex_lock(&ftrace_lock);
2042 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2043 struct hlist_head *hhd = &ftrace_func_hash[i];
2044
2045 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2046
2047 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002048 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002049 continue;
2050
Steven Rostedtb6887d72009-02-17 12:32:04 -05002051 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002052 continue;
2053
2054 /* do this last, since it is the most expensive */
2055 if (glob) {
2056 kallsyms_lookup(entry->ip, NULL, NULL,
2057 NULL, str);
2058 if (!ftrace_match(str, glob, len, type))
2059 continue;
2060 }
2061
2062 hlist_del(&entry->node);
2063 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2064 }
2065 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002066 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002067 mutex_unlock(&ftrace_lock);
2068}
2069
2070void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002071unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002072 void *data)
2073{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002074 __unregister_ftrace_function_probe(glob, ops, data,
2075 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002076}
2077
2078void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002079unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002080{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002081 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002082}
2083
Steven Rostedtb6887d72009-02-17 12:32:04 -05002084void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002085{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002086 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002087}
2088
Steven Rostedtf6180772009-02-14 00:40:25 -05002089static LIST_HEAD(ftrace_commands);
2090static DEFINE_MUTEX(ftrace_cmd_mutex);
2091
2092int register_ftrace_command(struct ftrace_func_command *cmd)
2093{
2094 struct ftrace_func_command *p;
2095 int ret = 0;
2096
2097 mutex_lock(&ftrace_cmd_mutex);
2098 list_for_each_entry(p, &ftrace_commands, list) {
2099 if (strcmp(cmd->name, p->name) == 0) {
2100 ret = -EBUSY;
2101 goto out_unlock;
2102 }
2103 }
2104 list_add(&cmd->list, &ftrace_commands);
2105 out_unlock:
2106 mutex_unlock(&ftrace_cmd_mutex);
2107
2108 return ret;
2109}
2110
2111int unregister_ftrace_command(struct ftrace_func_command *cmd)
2112{
2113 struct ftrace_func_command *p, *n;
2114 int ret = -ENODEV;
2115
2116 mutex_lock(&ftrace_cmd_mutex);
2117 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2118 if (strcmp(cmd->name, p->name) == 0) {
2119 ret = 0;
2120 list_del_init(&p->list);
2121 goto out_unlock;
2122 }
2123 }
2124 out_unlock:
2125 mutex_unlock(&ftrace_cmd_mutex);
2126
2127 return ret;
2128}
2129
Steven Rostedt64e7c442009-02-13 17:08:48 -05002130static int ftrace_process_regex(char *buff, int len, int enable)
2131{
Steven Rostedtf6180772009-02-14 00:40:25 -05002132 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002133 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002134 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002135
2136 func = strsep(&next, ":");
2137
2138 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002139 if (ftrace_match_records(func, len, enable))
2140 return 0;
2141 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002142 }
2143
Steven Rostedtf6180772009-02-14 00:40:25 -05002144 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002145
2146 command = strsep(&next, ":");
2147
Steven Rostedtf6180772009-02-14 00:40:25 -05002148 mutex_lock(&ftrace_cmd_mutex);
2149 list_for_each_entry(p, &ftrace_commands, list) {
2150 if (strcmp(p->name, command) == 0) {
2151 ret = p->func(func, command, next, enable);
2152 goto out_unlock;
2153 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002154 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002155 out_unlock:
2156 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002157
Steven Rostedtf6180772009-02-14 00:40:25 -05002158 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002159}
2160
Ingo Molnare309b412008-05-12 21:20:51 +02002161static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002162ftrace_regex_write(struct file *file, const char __user *ubuf,
2163 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002164{
2165 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002166 struct trace_parser *parser;
2167 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002168
Li Zefan4ba79782009-09-22 13:52:20 +08002169 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002170 return 0;
2171
Steven Rostedt41c52c02008-05-22 11:46:33 -04002172 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002173
2174 if (file->f_mode & FMODE_READ) {
2175 struct seq_file *m = file->private_data;
2176 iter = m->private;
2177 } else
2178 iter = file->private_data;
2179
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002180 parser = &iter->parser;
2181 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002182
Li Zefan4ba79782009-09-22 13:52:20 +08002183 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002184 !trace_parser_cont(parser)) {
2185 ret = ftrace_process_regex(parser->buffer,
2186 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002187 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002188 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002189 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002190 }
2191
Steven Rostedt5072c592008-05-12 21:20:43 +02002192 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002193out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002194 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002195
Steven Rostedt5072c592008-05-12 21:20:43 +02002196 return ret;
2197}
2198
Steven Rostedt41c52c02008-05-22 11:46:33 -04002199static ssize_t
2200ftrace_filter_write(struct file *file, const char __user *ubuf,
2201 size_t cnt, loff_t *ppos)
2202{
2203 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2204}
2205
2206static ssize_t
2207ftrace_notrace_write(struct file *file, const char __user *ubuf,
2208 size_t cnt, loff_t *ppos)
2209{
2210 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2211}
2212
2213static void
2214ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2215{
2216 if (unlikely(ftrace_disabled))
2217 return;
2218
2219 mutex_lock(&ftrace_regex_lock);
2220 if (reset)
2221 ftrace_filter_reset(enable);
2222 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002223 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002224 mutex_unlock(&ftrace_regex_lock);
2225}
2226
Steven Rostedt77a2b372008-05-12 21:20:45 +02002227/**
2228 * ftrace_set_filter - set a function to filter on in ftrace
2229 * @buf - the string that holds the function filter text.
2230 * @len - the length of the string.
2231 * @reset - non zero to reset all filters before applying this filter.
2232 *
2233 * Filters denote which functions should be enabled when tracing is enabled.
2234 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2235 */
Ingo Molnare309b412008-05-12 21:20:51 +02002236void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002237{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002238 ftrace_set_regex(buf, len, reset, 1);
2239}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002240
Steven Rostedt41c52c02008-05-22 11:46:33 -04002241/**
2242 * ftrace_set_notrace - set a function to not trace in ftrace
2243 * @buf - the string that holds the function notrace text.
2244 * @len - the length of the string.
2245 * @reset - non zero to reset all filters before applying this filter.
2246 *
2247 * Notrace Filters denote which functions should not be enabled when tracing
2248 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2249 * for tracing.
2250 */
2251void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2252{
2253 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002254}
2255
Steven Rostedt2af15d62009-05-28 13:37:24 -04002256/*
2257 * command line interface to allow users to set filters on boot up.
2258 */
2259#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2260static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2261static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2262
2263static int __init set_ftrace_notrace(char *str)
2264{
2265 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2266 return 1;
2267}
2268__setup("ftrace_notrace=", set_ftrace_notrace);
2269
2270static int __init set_ftrace_filter(char *str)
2271{
2272 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2273 return 1;
2274}
2275__setup("ftrace_filter=", set_ftrace_filter);
2276
Stefan Assmann369bc182009-10-12 22:17:21 +02002277#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002278static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002279static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2280
Stefan Assmann369bc182009-10-12 22:17:21 +02002281static int __init set_graph_function(char *str)
2282{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002283 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002284 return 1;
2285}
2286__setup("ftrace_graph_filter=", set_graph_function);
2287
2288static void __init set_ftrace_early_graph(char *buf)
2289{
2290 int ret;
2291 char *func;
2292
2293 while (buf) {
2294 func = strsep(&buf, ",");
2295 /* we allow only one expression at a time */
2296 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2297 func);
2298 if (ret)
2299 printk(KERN_DEBUG "ftrace: function %s not "
2300 "traceable\n", func);
2301 }
2302}
2303#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2304
Steven Rostedt2af15d62009-05-28 13:37:24 -04002305static void __init set_ftrace_early_filter(char *buf, int enable)
2306{
2307 char *func;
2308
2309 while (buf) {
2310 func = strsep(&buf, ",");
2311 ftrace_set_regex(func, strlen(func), 0, enable);
2312 }
2313}
2314
2315static void __init set_ftrace_early_filters(void)
2316{
2317 if (ftrace_filter_buf[0])
2318 set_ftrace_early_filter(ftrace_filter_buf, 1);
2319 if (ftrace_notrace_buf[0])
2320 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002321#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2322 if (ftrace_graph_buf[0])
2323 set_ftrace_early_graph(ftrace_graph_buf);
2324#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002325}
2326
Ingo Molnare309b412008-05-12 21:20:51 +02002327static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002328ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002329{
2330 struct seq_file *m = (struct seq_file *)file->private_data;
2331 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002332 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002333
Steven Rostedt41c52c02008-05-22 11:46:33 -04002334 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002335 if (file->f_mode & FMODE_READ) {
2336 iter = m->private;
2337
2338 seq_release(inode, file);
2339 } else
2340 iter = file->private_data;
2341
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002342 parser = &iter->parser;
2343 if (trace_parser_loaded(parser)) {
2344 parser->buffer[parser->idx] = 0;
2345 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002346 }
2347
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002348 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002349 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002350 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002351 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002352
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002353 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002354 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002355
Steven Rostedt41c52c02008-05-22 11:46:33 -04002356 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002357 return 0;
2358}
2359
Steven Rostedt41c52c02008-05-22 11:46:33 -04002360static int
2361ftrace_filter_release(struct inode *inode, struct file *file)
2362{
2363 return ftrace_regex_release(inode, file, 1);
2364}
2365
2366static int
2367ftrace_notrace_release(struct inode *inode, struct file *file)
2368{
2369 return ftrace_regex_release(inode, file, 0);
2370}
2371
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002372static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002373 .open = ftrace_avail_open,
2374 .read = seq_read,
2375 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002376 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002377};
2378
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002379static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302380 .open = ftrace_failures_open,
2381 .read = seq_read,
2382 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002383 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302384};
2385
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002386static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002387 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002388 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002389 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002390 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002391 .release = ftrace_filter_release,
2392};
2393
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002394static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002395 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002396 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002397 .write = ftrace_notrace_write,
2398 .llseek = ftrace_regex_lseek,
2399 .release = ftrace_notrace_release,
2400};
2401
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002402#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2403
2404static DEFINE_MUTEX(graph_lock);
2405
2406int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002407int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002408unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2409
2410static void *
Li Zefan85951842009-06-24 09:54:00 +08002411__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002412{
Li Zefan85951842009-06-24 09:54:00 +08002413 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002414 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002415 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002416}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002417
Li Zefan85951842009-06-24 09:54:00 +08002418static void *
2419g_next(struct seq_file *m, void *v, loff_t *pos)
2420{
2421 (*pos)++;
2422 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002423}
2424
2425static void *g_start(struct seq_file *m, loff_t *pos)
2426{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002427 mutex_lock(&graph_lock);
2428
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002429 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002430 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002431 return (void *)1;
2432
Li Zefan85951842009-06-24 09:54:00 +08002433 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002434}
2435
2436static void g_stop(struct seq_file *m, void *p)
2437{
2438 mutex_unlock(&graph_lock);
2439}
2440
2441static int g_show(struct seq_file *m, void *v)
2442{
2443 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002444
2445 if (!ptr)
2446 return 0;
2447
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002448 if (ptr == (unsigned long *)1) {
2449 seq_printf(m, "#### all functions enabled ####\n");
2450 return 0;
2451 }
2452
Steven Rostedtb375a112009-09-17 00:05:58 -04002453 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002454
2455 return 0;
2456}
2457
James Morris88e9d342009-09-22 16:43:43 -07002458static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002459 .start = g_start,
2460 .next = g_next,
2461 .stop = g_stop,
2462 .show = g_show,
2463};
2464
2465static int
2466ftrace_graph_open(struct inode *inode, struct file *file)
2467{
2468 int ret = 0;
2469
2470 if (unlikely(ftrace_disabled))
2471 return -ENODEV;
2472
2473 mutex_lock(&graph_lock);
2474 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002475 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002476 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002477 ftrace_graph_count = 0;
2478 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2479 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002480 mutex_unlock(&graph_lock);
2481
Li Zefana4ec5e02009-09-18 14:06:28 +08002482 if (file->f_mode & FMODE_READ)
2483 ret = seq_open(file, &ftrace_graph_seq_ops);
2484
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002485 return ret;
2486}
2487
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002488static int
Li Zefan87827112009-07-23 11:29:11 +08002489ftrace_graph_release(struct inode *inode, struct file *file)
2490{
2491 if (file->f_mode & FMODE_READ)
2492 seq_release(inode, file);
2493 return 0;
2494}
2495
2496static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002497ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002498{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002499 struct dyn_ftrace *rec;
2500 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002501 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002502 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002503 int type, not;
2504 char *search;
2505 bool exists;
2506 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002507
2508 if (ftrace_disabled)
2509 return -ENODEV;
2510
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002511 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002512 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002513 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2514 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002515
2516 search_len = strlen(search);
2517
Steven Rostedt52baf112009-02-14 01:15:39 -05002518 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002519 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002520
Steven Rostedt265c8312009-02-13 12:43:56 -05002521 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2522 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002523
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002524 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002525 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002526 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002527 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002528 if (array[i] == rec->ip) {
2529 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002530 break;
2531 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002532 }
2533
2534 if (!not) {
2535 fail = 0;
2536 if (!exists) {
2537 array[(*idx)++] = rec->ip;
2538 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2539 goto out;
2540 }
2541 } else {
2542 if (exists) {
2543 array[i] = array[--(*idx)];
2544 array[*idx] = 0;
2545 fail = 0;
2546 }
2547 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002548 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002549 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002550out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002551 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002552
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002553 if (fail)
2554 return -EINVAL;
2555
2556 ftrace_graph_filter_enabled = 1;
2557 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002558}
2559
2560static ssize_t
2561ftrace_graph_write(struct file *file, const char __user *ubuf,
2562 size_t cnt, loff_t *ppos)
2563{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002564 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002565 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002566
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002567 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002568 return 0;
2569
2570 mutex_lock(&graph_lock);
2571
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002572 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2573 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002574 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002575 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002576
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002577 read = trace_get_user(&parser, ubuf, cnt, ppos);
2578
Li Zefan4ba79782009-09-22 13:52:20 +08002579 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002580 parser.buffer[parser.idx] = 0;
2581
2582 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002583 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002584 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002585 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002586 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002587 }
2588
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002589 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002590
2591out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002592 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002593out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002594 mutex_unlock(&graph_lock);
2595
2596 return ret;
2597}
2598
2599static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002600 .open = ftrace_graph_open,
2601 .read = seq_read,
2602 .write = ftrace_graph_write,
2603 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002604};
2605#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2606
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002607static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002608{
Steven Rostedt5072c592008-05-12 21:20:43 +02002609
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002610 trace_create_file("available_filter_functions", 0444,
2611 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002612
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002613 trace_create_file("failures", 0444,
2614 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302615
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002616 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2617 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002618
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002619 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002620 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002621
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002622#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002623 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002624 NULL,
2625 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002626#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2627
Steven Rostedt5072c592008-05-12 21:20:43 +02002628 return 0;
2629}
2630
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002631static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002632 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002633 unsigned long *end)
2634{
2635 unsigned long *p;
2636 unsigned long addr;
2637 unsigned long flags;
2638
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002639 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002640 p = start;
2641 while (p < end) {
2642 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002643 /*
2644 * Some architecture linkers will pad between
2645 * the different mcount_loc sections of different
2646 * object files to satisfy alignments.
2647 * Skip any NULL pointers.
2648 */
2649 if (!addr)
2650 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002651 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002652 }
2653
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002654 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002655 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002656 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002657 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002658 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002659
2660 return 0;
2661}
2662
Steven Rostedt93eb6772009-04-15 13:24:06 -04002663#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002664void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002665{
2666 struct dyn_ftrace *rec;
2667 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002668
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002669 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002670 return;
2671
2672 mutex_lock(&ftrace_lock);
2673 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002674 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002675 /*
2676 * rec->ip is changed in ftrace_free_rec()
2677 * It should not between s and e if record was freed.
2678 */
2679 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2680 ftrace_free_rec(rec);
2681 }
2682 } while_for_each_ftrace_rec();
2683 mutex_unlock(&ftrace_lock);
2684}
2685
2686static void ftrace_init_module(struct module *mod,
2687 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002688{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002689 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002690 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002691 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002692}
2693
Steven Rostedt93eb6772009-04-15 13:24:06 -04002694static int ftrace_module_notify(struct notifier_block *self,
2695 unsigned long val, void *data)
2696{
2697 struct module *mod = data;
2698
2699 switch (val) {
2700 case MODULE_STATE_COMING:
2701 ftrace_init_module(mod, mod->ftrace_callsites,
2702 mod->ftrace_callsites +
2703 mod->num_ftrace_callsites);
2704 break;
2705 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002706 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002707 break;
2708 }
2709
2710 return 0;
2711}
2712#else
2713static int ftrace_module_notify(struct notifier_block *self,
2714 unsigned long val, void *data)
2715{
2716 return 0;
2717}
2718#endif /* CONFIG_MODULES */
2719
2720struct notifier_block ftrace_module_nb = {
2721 .notifier_call = ftrace_module_notify,
2722 .priority = 0,
2723};
2724
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002725extern unsigned long __start_mcount_loc[];
2726extern unsigned long __stop_mcount_loc[];
2727
2728void __init ftrace_init(void)
2729{
2730 unsigned long count, addr, flags;
2731 int ret;
2732
2733 /* Keep the ftrace pointer to the stub */
2734 addr = (unsigned long)ftrace_stub;
2735
2736 local_irq_save(flags);
2737 ftrace_dyn_arch_init(&addr);
2738 local_irq_restore(flags);
2739
2740 /* ftrace_dyn_arch_init places the return code in addr */
2741 if (addr)
2742 goto failed;
2743
2744 count = __stop_mcount_loc - __start_mcount_loc;
2745
2746 ret = ftrace_dyn_table_alloc(count);
2747 if (ret)
2748 goto failed;
2749
2750 last_ftrace_enabled = ftrace_enabled = 1;
2751
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002752 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002753 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002754 __stop_mcount_loc);
2755
Steven Rostedt93eb6772009-04-15 13:24:06 -04002756 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002757 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002758 pr_warning("Failed to register trace ftrace module notifier\n");
2759
Steven Rostedt2af15d62009-05-28 13:37:24 -04002760 set_ftrace_early_filters();
2761
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002762 return;
2763 failed:
2764 ftrace_disabled = 1;
2765}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002766
Steven Rostedt3d083392008-05-12 21:20:42 +02002767#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002768
2769static int __init ftrace_nodyn_init(void)
2770{
2771 ftrace_enabled = 1;
2772 return 0;
2773}
2774device_initcall(ftrace_nodyn_init);
2775
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002776static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2777static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002778/* Keep as macros so we do not need to define the commands */
2779# define ftrace_startup(command) do { } while (0)
2780# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002781# define ftrace_startup_sysctl() do { } while (0)
2782# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002783#endif /* CONFIG_DYNAMIC_FTRACE */
2784
Steven Rostedte32d8952008-12-04 00:26:41 -05002785static void clear_ftrace_swapper(void)
2786{
2787 struct task_struct *p;
2788 int cpu;
2789
2790 get_online_cpus();
2791 for_each_online_cpu(cpu) {
2792 p = idle_task(cpu);
2793 clear_tsk_trace_trace(p);
2794 }
2795 put_online_cpus();
2796}
2797
2798static void set_ftrace_swapper(void)
2799{
2800 struct task_struct *p;
2801 int cpu;
2802
2803 get_online_cpus();
2804 for_each_online_cpu(cpu) {
2805 p = idle_task(cpu);
2806 set_tsk_trace_trace(p);
2807 }
2808 put_online_cpus();
2809}
2810
2811static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002812{
2813 struct task_struct *p;
2814
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002815 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002816 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002817 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002818 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002819 rcu_read_unlock();
2820
Steven Rostedte32d8952008-12-04 00:26:41 -05002821 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002822}
2823
Steven Rostedte32d8952008-12-04 00:26:41 -05002824static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002825{
2826 struct task_struct *p;
2827
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002828 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002829 do_each_pid_task(pid, PIDTYPE_PID, p) {
2830 set_tsk_trace_trace(p);
2831 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002832 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002833}
2834
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002835static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002836{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002837 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002838 clear_ftrace_swapper();
2839 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002840 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002841}
2842
2843static void set_ftrace_pid_task(struct pid *pid)
2844{
2845 if (pid == ftrace_swapper_pid)
2846 set_ftrace_swapper();
2847 else
2848 set_ftrace_pid(pid);
2849}
2850
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002851static int ftrace_pid_add(int p)
2852{
2853 struct pid *pid;
2854 struct ftrace_pid *fpid;
2855 int ret = -EINVAL;
2856
2857 mutex_lock(&ftrace_lock);
2858
2859 if (!p)
2860 pid = ftrace_swapper_pid;
2861 else
2862 pid = find_get_pid(p);
2863
2864 if (!pid)
2865 goto out;
2866
2867 ret = 0;
2868
2869 list_for_each_entry(fpid, &ftrace_pids, list)
2870 if (fpid->pid == pid)
2871 goto out_put;
2872
2873 ret = -ENOMEM;
2874
2875 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2876 if (!fpid)
2877 goto out_put;
2878
2879 list_add(&fpid->list, &ftrace_pids);
2880 fpid->pid = pid;
2881
2882 set_ftrace_pid_task(pid);
2883
2884 ftrace_update_pid_func();
2885 ftrace_startup_enable(0);
2886
2887 mutex_unlock(&ftrace_lock);
2888 return 0;
2889
2890out_put:
2891 if (pid != ftrace_swapper_pid)
2892 put_pid(pid);
2893
2894out:
2895 mutex_unlock(&ftrace_lock);
2896 return ret;
2897}
2898
2899static void ftrace_pid_reset(void)
2900{
2901 struct ftrace_pid *fpid, *safe;
2902
2903 mutex_lock(&ftrace_lock);
2904 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2905 struct pid *pid = fpid->pid;
2906
2907 clear_ftrace_pid_task(pid);
2908
2909 list_del(&fpid->list);
2910 kfree(fpid);
2911 }
2912
2913 ftrace_update_pid_func();
2914 ftrace_startup_enable(0);
2915
2916 mutex_unlock(&ftrace_lock);
2917}
2918
2919static void *fpid_start(struct seq_file *m, loff_t *pos)
2920{
2921 mutex_lock(&ftrace_lock);
2922
2923 if (list_empty(&ftrace_pids) && (!*pos))
2924 return (void *) 1;
2925
2926 return seq_list_start(&ftrace_pids, *pos);
2927}
2928
2929static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2930{
2931 if (v == (void *)1)
2932 return NULL;
2933
2934 return seq_list_next(v, &ftrace_pids, pos);
2935}
2936
2937static void fpid_stop(struct seq_file *m, void *p)
2938{
2939 mutex_unlock(&ftrace_lock);
2940}
2941
2942static int fpid_show(struct seq_file *m, void *v)
2943{
2944 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2945
2946 if (v == (void *)1) {
2947 seq_printf(m, "no pid\n");
2948 return 0;
2949 }
2950
2951 if (fpid->pid == ftrace_swapper_pid)
2952 seq_printf(m, "swapper tasks\n");
2953 else
2954 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2955
2956 return 0;
2957}
2958
2959static const struct seq_operations ftrace_pid_sops = {
2960 .start = fpid_start,
2961 .next = fpid_next,
2962 .stop = fpid_stop,
2963 .show = fpid_show,
2964};
2965
2966static int
2967ftrace_pid_open(struct inode *inode, struct file *file)
2968{
2969 int ret = 0;
2970
2971 if ((file->f_mode & FMODE_WRITE) &&
2972 (file->f_flags & O_TRUNC))
2973 ftrace_pid_reset();
2974
2975 if (file->f_mode & FMODE_READ)
2976 ret = seq_open(file, &ftrace_pid_sops);
2977
2978 return ret;
2979}
2980
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002981static ssize_t
2982ftrace_pid_write(struct file *filp, const char __user *ubuf,
2983 size_t cnt, loff_t *ppos)
2984{
Ingo Molnar457dc922009-11-23 11:03:28 +01002985 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002986 long val;
2987 int ret;
2988
2989 if (cnt >= sizeof(buf))
2990 return -EINVAL;
2991
2992 if (copy_from_user(&buf, ubuf, cnt))
2993 return -EFAULT;
2994
2995 buf[cnt] = 0;
2996
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002997 /*
2998 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
2999 * to clean the filter quietly.
3000 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003001 tmp = strstrip(buf);
3002 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003003 return 1;
3004
Ingo Molnar457dc922009-11-23 11:03:28 +01003005 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003006 if (ret < 0)
3007 return ret;
3008
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003009 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003010
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003011 return ret ? ret : cnt;
3012}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003013
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003014static int
3015ftrace_pid_release(struct inode *inode, struct file *file)
3016{
3017 if (file->f_mode & FMODE_READ)
3018 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003019
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003020 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003021}
3022
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003023static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003024 .open = ftrace_pid_open,
3025 .write = ftrace_pid_write,
3026 .read = seq_read,
3027 .llseek = seq_lseek,
3028 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003029};
3030
3031static __init int ftrace_init_debugfs(void)
3032{
3033 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003034
3035 d_tracer = tracing_init_dentry();
3036 if (!d_tracer)
3037 return 0;
3038
3039 ftrace_init_dyn_debugfs(d_tracer);
3040
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003041 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3042 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003043
3044 ftrace_profile_debugfs(d_tracer);
3045
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003046 return 0;
3047}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003048fs_initcall(ftrace_init_debugfs);
3049
Steven Rostedt3d083392008-05-12 21:20:42 +02003050/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003051 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003052 *
3053 * This function should be used by panic code. It stops ftrace
3054 * but in a not so nice way. If you need to simply kill ftrace
3055 * from a non-atomic section, use ftrace_kill.
3056 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003057void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003058{
3059 ftrace_disabled = 1;
3060 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003061 clear_ftrace_function();
3062}
3063
3064/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003065 * register_ftrace_function - register a function for profiling
3066 * @ops - ops structure that holds the function for profiling.
3067 *
3068 * Register a function to be called by all functions in the
3069 * kernel.
3070 *
3071 * Note: @ops->func and all the functions it calls must be labeled
3072 * with "notrace", otherwise it will go into a
3073 * recursive loop.
3074 */
3075int register_ftrace_function(struct ftrace_ops *ops)
3076{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003077 int ret;
3078
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003079 if (unlikely(ftrace_disabled))
3080 return -1;
3081
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003082 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003083
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003084 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003085 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003086
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003087 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003088 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003089}
3090
3091/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003092 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003093 * @ops - ops structure that holds the function to unregister
3094 *
3095 * Unregister a function that was added to be called by ftrace profiling.
3096 */
3097int unregister_ftrace_function(struct ftrace_ops *ops)
3098{
3099 int ret;
3100
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003101 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003102 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003103 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003104 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003105
3106 return ret;
3107}
3108
Ingo Molnare309b412008-05-12 21:20:51 +02003109int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003110ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003111 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003112 loff_t *ppos)
3113{
3114 int ret;
3115
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003116 if (unlikely(ftrace_disabled))
3117 return -ENODEV;
3118
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003119 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003120
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003121 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003122
Li Zefana32c7762009-06-26 16:55:51 +08003123 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003124 goto out;
3125
Li Zefana32c7762009-06-26 16:55:51 +08003126 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003127
3128 if (ftrace_enabled) {
3129
3130 ftrace_startup_sysctl();
3131
3132 /* we are starting ftrace again */
3133 if (ftrace_list != &ftrace_list_end) {
3134 if (ftrace_list->next == &ftrace_list_end)
3135 ftrace_trace_function = ftrace_list->func;
3136 else
3137 ftrace_trace_function = ftrace_list_func;
3138 }
3139
3140 } else {
3141 /* stopping ftrace calls (just send to ftrace_stub) */
3142 ftrace_trace_function = ftrace_stub;
3143
3144 ftrace_shutdown_sysctl();
3145 }
3146
3147 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003148 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003149 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003150}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003151
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003152#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003153
Steven Rostedt597af812009-04-03 15:24:12 -04003154static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003155static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003156
Steven Rostedte49dc192008-12-02 23:50:05 -05003157int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3158{
3159 return 0;
3160}
3161
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003162/* The callbacks that hook a function */
3163trace_func_graph_ret_t ftrace_graph_return =
3164 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003165trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003166
3167/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3168static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3169{
3170 int i;
3171 int ret = 0;
3172 unsigned long flags;
3173 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3174 struct task_struct *g, *t;
3175
3176 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3177 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3178 * sizeof(struct ftrace_ret_stack),
3179 GFP_KERNEL);
3180 if (!ret_stack_list[i]) {
3181 start = 0;
3182 end = i;
3183 ret = -ENOMEM;
3184 goto free;
3185 }
3186 }
3187
3188 read_lock_irqsave(&tasklist_lock, flags);
3189 do_each_thread(g, t) {
3190 if (start == end) {
3191 ret = -EAGAIN;
3192 goto unlock;
3193 }
3194
3195 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003196 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003197 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003198 t->curr_ret_stack = -1;
3199 /* Make sure the tasks see the -1 first: */
3200 smp_wmb();
3201 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003202 }
3203 } while_each_thread(g, t);
3204
3205unlock:
3206 read_unlock_irqrestore(&tasklist_lock, flags);
3207free:
3208 for (i = start; i < end; i++)
3209 kfree(ret_stack_list[i]);
3210 return ret;
3211}
3212
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003213static void
3214ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3215 struct task_struct *next)
3216{
3217 unsigned long long timestamp;
3218 int index;
3219
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003220 /*
3221 * Does the user want to count the time a function was asleep.
3222 * If so, do not update the time stamps.
3223 */
3224 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3225 return;
3226
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003227 timestamp = trace_clock_local();
3228
3229 prev->ftrace_timestamp = timestamp;
3230
3231 /* only process tasks that we timestamped */
3232 if (!next->ftrace_timestamp)
3233 return;
3234
3235 /*
3236 * Update all the counters in next to make up for the
3237 * time next was sleeping.
3238 */
3239 timestamp -= next->ftrace_timestamp;
3240
3241 for (index = next->curr_ret_stack; index >= 0; index--)
3242 next->ret_stack[index].calltime += timestamp;
3243}
3244
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003245/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003246static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003247{
3248 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003249 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003250
3251 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3252 sizeof(struct ftrace_ret_stack *),
3253 GFP_KERNEL);
3254
3255 if (!ret_stack_list)
3256 return -ENOMEM;
3257
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003258 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003259 for_each_online_cpu(cpu) {
3260 if (!idle_task(cpu)->ret_stack)
3261 ftrace_graph_init_task(idle_task(cpu));
3262 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003263
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003264 do {
3265 ret = alloc_retstack_tasklist(ret_stack_list);
3266 } while (ret == -EAGAIN);
3267
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003268 if (!ret) {
3269 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3270 if (ret)
3271 pr_info("ftrace_graph: Couldn't activate tracepoint"
3272 " probe to kernel_sched_switch\n");
3273 }
3274
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003275 kfree(ret_stack_list);
3276 return ret;
3277}
3278
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003279/*
3280 * Hibernation protection.
3281 * The state of the current task is too much unstable during
3282 * suspend/restore to disk. We want to protect against that.
3283 */
3284static int
3285ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3286 void *unused)
3287{
3288 switch (state) {
3289 case PM_HIBERNATION_PREPARE:
3290 pause_graph_tracing();
3291 break;
3292
3293 case PM_POST_HIBERNATION:
3294 unpause_graph_tracing();
3295 break;
3296 }
3297 return NOTIFY_DONE;
3298}
3299
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003300int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3301 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003302{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003303 int ret = 0;
3304
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003305 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003306
Steven Rostedt05ce5812009-03-24 00:18:31 -04003307 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003308 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003309 ret = -EBUSY;
3310 goto out;
3311 }
3312
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003313 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3314 register_pm_notifier(&ftrace_suspend_notifier);
3315
Steven Rostedt597af812009-04-03 15:24:12 -04003316 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003317 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003318 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003319 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003320 goto out;
3321 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003322
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003323 ftrace_graph_return = retfunc;
3324 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003325
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003326 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003327
3328out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003329 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003330 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003331}
3332
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003333void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003334{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003335 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003336
Steven Rostedt597af812009-04-03 15:24:12 -04003337 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003338 goto out;
3339
Steven Rostedt597af812009-04-03 15:24:12 -04003340 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003341 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003342 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003343 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003344 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003345 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003346
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003347 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003348 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003349}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003350
3351/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003352void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003353{
Steven Rostedt84047e32009-06-02 16:51:55 -04003354 /* Make sure we do not use the parent ret_stack */
3355 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05003356 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04003357
Steven Rostedt597af812009-04-03 15:24:12 -04003358 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003359 struct ftrace_ret_stack *ret_stack;
3360
3361 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003362 * sizeof(struct ftrace_ret_stack),
3363 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003364 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003365 return;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003366 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003367 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003368 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003369 /* make curr_ret_stack visable before we add the ret_stack */
3370 smp_wmb();
3371 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003372 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003373}
3374
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003375void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003376{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003377 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3378
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003379 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003380 /* NULL must become visible to IRQs before we free it: */
3381 barrier();
3382
3383 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003384}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003385
3386void ftrace_graph_stop(void)
3387{
3388 ftrace_stop();
3389}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003390#endif