blob: 8c5adc0e5db3f2199fa54cd9b422bb65a163d940 [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
Stefan Assmann369bc182009-10-12 22:17:21 +020088#ifdef CONFIG_FUNCTION_GRAPH_TRACER
89static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
90#endif
91
Paul E. McKenney3f379b02010-03-05 15:03:25 -080092/*
93 * Traverse the ftrace_list, invoking all entries. The reason that we
94 * can use rcu_dereference_raw() is that elements removed from this list
95 * are simply leaked, so there is no need to interact with a grace-period
96 * mechanism. The rcu_dereference_raw() calls are needed to handle
97 * concurrent insertions into the ftrace_list.
98 *
99 * Silly Alpha and silly pointer-speculation compiler optimizations!
100 */
Ingo Molnarf2252932008-05-22 10:37:48 +0200101static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200102{
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800103 struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104
105 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200106 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800107 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200108 };
109}
110
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500111static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
112{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500113 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500114 return;
115
116 ftrace_pid_function(ip, parent_ip);
117}
118
119static void set_ftrace_pid_function(ftrace_func_t func)
120{
121 /* do not set ftrace_pid_function to itself! */
122 if (func != ftrace_pid_func)
123 ftrace_pid_function = func;
124}
125
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200127 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200129 * This NULLs the ftrace function and in essence stops
130 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200131 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200132void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200133{
Steven Rostedt3d083392008-05-12 21:20:42 +0200134 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500135 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500136 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200137}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200138
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500139#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
140/*
141 * For those archs that do not test ftrace_trace_stop in their
142 * mcount call site, we need to do it from C.
143 */
144static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
145{
146 if (function_trace_stop)
147 return;
148
149 __ftrace_trace_function(ip, parent_ip);
150}
151#endif
152
Ingo Molnare309b412008-05-12 21:20:51 +0200153static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200154{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200155 ops->next = ftrace_list;
156 /*
157 * We are entering ops into the ftrace_list but another
158 * CPU might be walking that list. We need to make sure
159 * the ops->next pointer is valid before another CPU sees
160 * the ops pointer included into the ftrace_list.
161 */
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800162 rcu_assign_pointer(ftrace_list, ops);
Steven Rostedt3d083392008-05-12 21:20:42 +0200163
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200164 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500165 ftrace_func_t func;
166
167 if (ops->next == &ftrace_list_end)
168 func = ops->func;
169 else
170 func = ftrace_list_func;
171
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400172 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500173 set_ftrace_pid_function(func);
174 func = ftrace_pid_func;
175 }
176
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200177 /*
178 * For one func, simply call it directly.
179 * For more than one func, call the chain.
180 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500181#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500183#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500185 ftrace_trace_function = ftrace_test_stop_func;
186#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200187 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200188
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189 return 0;
190}
191
Ingo Molnare309b412008-05-12 21:20:51 +0200192static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200193{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200194 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195
196 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 * If we are removing the last function, then simply point
198 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200199 */
200 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
201 ftrace_trace_function = ftrace_stub;
202 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500203 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200204 }
205
206 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
207 if (*p == ops)
208 break;
209
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500210 if (*p != ops)
211 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212
213 *p = (*p)->next;
214
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200215 if (ftrace_enabled) {
216 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500217 if (ftrace_list->next == &ftrace_list_end) {
218 ftrace_func_t func = ftrace_list->func;
219
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400220 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500221 set_ftrace_pid_function(func);
222 func = ftrace_pid_func;
223 }
224#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
225 ftrace_trace_function = func;
226#else
227 __ftrace_trace_function = func;
228#endif
229 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200230 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200231
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500232 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200233}
234
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500235static void ftrace_update_pid_func(void)
236{
237 ftrace_func_t func;
238
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500239 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900240 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500241
Matt Fleming33974092009-09-28 16:43:01 +0100242#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500243 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100244#else
245 func = __ftrace_trace_function;
246#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500247
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400248 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500249 set_ftrace_pid_function(func);
250 func = ftrace_pid_func;
251 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800252 if (func == ftrace_pid_func)
253 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500254 }
255
256#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
257 ftrace_trace_function = func;
258#else
259 __ftrace_trace_function = func;
260#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500261}
262
Steven Rostedt493762f2009-03-23 17:12:36 -0400263#ifdef CONFIG_FUNCTION_PROFILER
264struct ftrace_profile {
265 struct hlist_node node;
266 unsigned long ip;
267 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400268#ifdef CONFIG_FUNCTION_GRAPH_TRACER
269 unsigned long long time;
270#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400271};
272
273struct ftrace_profile_page {
274 struct ftrace_profile_page *next;
275 unsigned long index;
276 struct ftrace_profile records[];
277};
278
Steven Rostedtcafb1682009-03-24 20:50:39 -0400279struct ftrace_profile_stat {
280 atomic_t disabled;
281 struct hlist_head *hash;
282 struct ftrace_profile_page *pages;
283 struct ftrace_profile_page *start;
284 struct tracer_stat stat;
285};
286
Steven Rostedt493762f2009-03-23 17:12:36 -0400287#define PROFILE_RECORDS_SIZE \
288 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
289
290#define PROFILES_PER_PAGE \
291 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
292
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400293static int ftrace_profile_bits __read_mostly;
294static int ftrace_profile_enabled __read_mostly;
295
296/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400297static DEFINE_MUTEX(ftrace_profile_lock);
298
Steven Rostedtcafb1682009-03-24 20:50:39 -0400299static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400300
301#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
302
Steven Rostedt493762f2009-03-23 17:12:36 -0400303static void *
304function_stat_next(void *v, int idx)
305{
306 struct ftrace_profile *rec = v;
307 struct ftrace_profile_page *pg;
308
309 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
310
311 again:
Li Zefan0296e422009-06-26 11:15:37 +0800312 if (idx != 0)
313 rec++;
314
Steven Rostedt493762f2009-03-23 17:12:36 -0400315 if ((void *)rec >= (void *)&pg->records[pg->index]) {
316 pg = pg->next;
317 if (!pg)
318 return NULL;
319 rec = &pg->records[0];
320 if (!rec->counter)
321 goto again;
322 }
323
324 return rec;
325}
326
327static void *function_stat_start(struct tracer_stat *trace)
328{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400329 struct ftrace_profile_stat *stat =
330 container_of(trace, struct ftrace_profile_stat, stat);
331
332 if (!stat || !stat->start)
333 return NULL;
334
335 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400336}
337
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400338#ifdef CONFIG_FUNCTION_GRAPH_TRACER
339/* function graph compares on total time */
340static int function_stat_cmp(void *p1, void *p2)
341{
342 struct ftrace_profile *a = p1;
343 struct ftrace_profile *b = p2;
344
345 if (a->time < b->time)
346 return -1;
347 if (a->time > b->time)
348 return 1;
349 else
350 return 0;
351}
352#else
353/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400354static int function_stat_cmp(void *p1, void *p2)
355{
356 struct ftrace_profile *a = p1;
357 struct ftrace_profile *b = p2;
358
359 if (a->counter < b->counter)
360 return -1;
361 if (a->counter > b->counter)
362 return 1;
363 else
364 return 0;
365}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400366#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400367
368static int function_stat_headers(struct seq_file *m)
369{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400370#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400371 seq_printf(m, " Function "
372 "Hit Time Avg\n"
373 " -------- "
374 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400375#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400376 seq_printf(m, " Function Hit\n"
377 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400378#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400379 return 0;
380}
381
382static int function_stat_show(struct seq_file *m, void *v)
383{
384 struct ftrace_profile *rec = v;
385 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400386#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400387 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400388 static struct trace_seq s;
389 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400390#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400391
392 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400393 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400394
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400395#ifdef CONFIG_FUNCTION_GRAPH_TRACER
396 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400397 avg = rec->time;
398 do_div(avg, rec->counter);
399
400 mutex_lock(&mutex);
401 trace_seq_init(&s);
402 trace_print_graph_duration(rec->time, &s);
403 trace_seq_puts(&s, " ");
404 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400405 trace_print_seq(m, &s);
406 mutex_unlock(&mutex);
407#endif
408 seq_putc(m, '\n');
409
Steven Rostedt493762f2009-03-23 17:12:36 -0400410 return 0;
411}
412
Steven Rostedtcafb1682009-03-24 20:50:39 -0400413static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400414{
415 struct ftrace_profile_page *pg;
416
Steven Rostedtcafb1682009-03-24 20:50:39 -0400417 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400418
419 while (pg) {
420 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
421 pg->index = 0;
422 pg = pg->next;
423 }
424
Steven Rostedtcafb1682009-03-24 20:50:39 -0400425 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400426 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
427}
428
Steven Rostedtcafb1682009-03-24 20:50:39 -0400429int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400430{
431 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400432 int functions;
433 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400434 int i;
435
436 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400438 return 0;
439
Steven Rostedtcafb1682009-03-24 20:50:39 -0400440 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
441 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400442 return -ENOMEM;
443
Steven Rostedt318e0a72009-03-25 20:06:34 -0400444#ifdef CONFIG_DYNAMIC_FTRACE
445 functions = ftrace_update_tot_cnt;
446#else
447 /*
448 * We do not know the number of functions that exist because
449 * dynamic tracing is what counts them. With past experience
450 * we have around 20K functions. That should be more than enough.
451 * It is highly unlikely we will execute every function in
452 * the kernel.
453 */
454 functions = 20000;
455#endif
456
Steven Rostedtcafb1682009-03-24 20:50:39 -0400457 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400458
Steven Rostedt318e0a72009-03-25 20:06:34 -0400459 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
460
461 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400463 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400464 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400465 pg = pg->next;
466 }
467
468 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400469
470 out_free:
471 pg = stat->start;
472 while (pg) {
473 unsigned long tmp = (unsigned long)pg;
474
475 pg = pg->next;
476 free_page(tmp);
477 }
478
479 free_page((unsigned long)stat->pages);
480 stat->pages = NULL;
481 stat->start = NULL;
482
483 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400484}
485
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400487{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400488 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400489 int size;
490
Steven Rostedtcafb1682009-03-24 20:50:39 -0400491 stat = &per_cpu(ftrace_profile_stats, cpu);
492
493 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400494 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400496 return 0;
497 }
498
499 /*
500 * We are profiling all functions, but usually only a few thousand
501 * functions are hit. We'll make a hash of 1024 items.
502 */
503 size = FTRACE_PROFILE_HASH_SIZE;
504
Steven Rostedtcafb1682009-03-24 20:50:39 -0400505 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400506
Steven Rostedtcafb1682009-03-24 20:50:39 -0400507 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400508 return -ENOMEM;
509
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510 if (!ftrace_profile_bits) {
511 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400512
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513 for (; size; size >>= 1)
514 ftrace_profile_bits++;
515 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400516
Steven Rostedt318e0a72009-03-25 20:06:34 -0400517 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400518 if (ftrace_profile_pages_init(stat) < 0) {
519 kfree(stat->hash);
520 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400521 return -ENOMEM;
522 }
523
524 return 0;
525}
526
Steven Rostedtcafb1682009-03-24 20:50:39 -0400527static int ftrace_profile_init(void)
528{
529 int cpu;
530 int ret = 0;
531
532 for_each_online_cpu(cpu) {
533 ret = ftrace_profile_init_cpu(cpu);
534 if (ret)
535 break;
536 }
537
538 return ret;
539}
540
Steven Rostedt493762f2009-03-23 17:12:36 -0400541/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400542static struct ftrace_profile *
543ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400544{
545 struct ftrace_profile *rec;
546 struct hlist_head *hhd;
547 struct hlist_node *n;
548 unsigned long key;
549
550 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400551 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400552
553 if (hlist_empty(hhd))
554 return NULL;
555
556 hlist_for_each_entry_rcu(rec, n, hhd, node) {
557 if (rec->ip == ip)
558 return rec;
559 }
560
561 return NULL;
562}
563
Steven Rostedtcafb1682009-03-24 20:50:39 -0400564static void ftrace_add_profile(struct ftrace_profile_stat *stat,
565 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400566{
567 unsigned long key;
568
569 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400570 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400571}
572
Steven Rostedt318e0a72009-03-25 20:06:34 -0400573/*
574 * The memory is already allocated, this simply finds a new record to use.
575 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400576static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400577ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400578{
579 struct ftrace_profile *rec = NULL;
580
Steven Rostedt318e0a72009-03-25 20:06:34 -0400581 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400582 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400583 goto out;
584
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400586 * Try to find the function again since an NMI
587 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400588 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400590 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400592
Steven Rostedtcafb1682009-03-24 20:50:39 -0400593 if (stat->pages->index == PROFILES_PER_PAGE) {
594 if (!stat->pages->next)
595 goto out;
596 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 }
598
Steven Rostedtcafb1682009-03-24 20:50:39 -0400599 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400600 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400602
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400604 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400605
606 return rec;
607}
608
Steven Rostedt493762f2009-03-23 17:12:36 -0400609static void
610function_profile_call(unsigned long ip, unsigned long parent_ip)
611{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400612 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400613 struct ftrace_profile *rec;
614 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400615
616 if (!ftrace_profile_enabled)
617 return;
618
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400620
621 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400622 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400623 goto out;
624
625 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400627 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400628 if (!rec)
629 goto out;
630 }
631
632 rec->counter++;
633 out:
634 local_irq_restore(flags);
635}
636
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400637#ifdef CONFIG_FUNCTION_GRAPH_TRACER
638static int profile_graph_entry(struct ftrace_graph_ent *trace)
639{
640 function_profile_call(trace->func, 0);
641 return 1;
642}
643
644static void profile_graph_return(struct ftrace_graph_ret *trace)
645{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400647 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400650
651 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400653 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 goto out;
655
Steven Rostedta2a16d62009-03-24 23:17:58 -0400656 calltime = trace->rettime - trace->calltime;
657
658 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
659 int index;
660
661 index = trace->depth;
662
663 /* Append this call time to the parent time to subtract */
664 if (index)
665 current->ret_stack[index - 1].subtime += calltime;
666
667 if (current->ret_stack[index].subtime < calltime)
668 calltime -= current->ret_stack[index].subtime;
669 else
670 calltime = 0;
671 }
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400674 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400675 rec->time += calltime;
676
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400678 local_irq_restore(flags);
679}
680
681static int register_ftrace_profiler(void)
682{
683 return register_ftrace_graph(&profile_graph_return,
684 &profile_graph_entry);
685}
686
687static void unregister_ftrace_profiler(void)
688{
689 unregister_ftrace_graph();
690}
691#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400692static struct ftrace_ops ftrace_profile_ops __read_mostly =
693{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400694 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400695};
696
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400697static int register_ftrace_profiler(void)
698{
699 return register_ftrace_function(&ftrace_profile_ops);
700}
701
702static void unregister_ftrace_profiler(void)
703{
704 unregister_ftrace_function(&ftrace_profile_ops);
705}
706#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
707
Steven Rostedt493762f2009-03-23 17:12:36 -0400708static ssize_t
709ftrace_profile_write(struct file *filp, const char __user *ubuf,
710 size_t cnt, loff_t *ppos)
711{
712 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400713 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400714 int ret;
715
716 if (cnt >= sizeof(buf))
717 return -EINVAL;
718
719 if (copy_from_user(&buf, ubuf, cnt))
720 return -EFAULT;
721
722 buf[cnt] = 0;
723
724 ret = strict_strtoul(buf, 10, &val);
725 if (ret < 0)
726 return ret;
727
728 val = !!val;
729
730 mutex_lock(&ftrace_profile_lock);
731 if (ftrace_profile_enabled ^ val) {
732 if (val) {
733 ret = ftrace_profile_init();
734 if (ret < 0) {
735 cnt = ret;
736 goto out;
737 }
738
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400739 ret = register_ftrace_profiler();
740 if (ret < 0) {
741 cnt = ret;
742 goto out;
743 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 ftrace_profile_enabled = 1;
745 } else {
746 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400747 /*
748 * unregister_ftrace_profiler calls stop_machine
749 * so this acts like an synchronize_sched.
750 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400751 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 }
753 }
754 out:
755 mutex_unlock(&ftrace_profile_lock);
756
Jiri Olsacf8517c2009-10-23 19:36:16 -0400757 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400758
759 return cnt;
760}
761
762static ssize_t
763ftrace_profile_read(struct file *filp, char __user *ubuf,
764 size_t cnt, loff_t *ppos)
765{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400766 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 int r;
768
769 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
770 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
771}
772
773static const struct file_operations ftrace_profile_fops = {
774 .open = tracing_open_generic,
775 .read = ftrace_profile_read,
776 .write = ftrace_profile_write,
777};
778
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779/* used to initialize the real stat files */
780static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400781 .name = "functions",
782 .stat_start = function_stat_start,
783 .stat_next = function_stat_next,
784 .stat_cmp = function_stat_cmp,
785 .stat_headers = function_stat_headers,
786 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400787};
788
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400789static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400790{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400794 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400795 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400796
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 for_each_possible_cpu(cpu) {
798 stat = &per_cpu(ftrace_profile_stats, cpu);
799
800 /* allocate enough for function name + cpu number */
801 name = kmalloc(32, GFP_KERNEL);
802 if (!name) {
803 /*
804 * The files created are permanent, if something happens
805 * we still do not free memory.
806 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 WARN(1,
808 "Could not allocate stat file for cpu %d\n",
809 cpu);
810 return;
811 }
812 stat->stat = function_stats;
813 snprintf(name, 32, "function%d", cpu);
814 stat->stat.name = name;
815 ret = register_stat_tracer(&stat->stat);
816 if (ret) {
817 WARN(1,
818 "Could not register function stat for cpu %d\n",
819 cpu);
820 kfree(name);
821 return;
822 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 }
824
825 entry = debugfs_create_file("function_profile_enabled", 0644,
826 d_tracer, NULL, &ftrace_profile_fops);
827 if (!entry)
828 pr_warning("Could not create debugfs "
829 "'function_profile_enabled' entry\n");
830}
831
832#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400833static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400834{
835}
836#endif /* CONFIG_FUNCTION_PROFILER */
837
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100838static struct pid * const ftrace_swapper_pid = &init_struct_pid;
839
Steven Rostedt3d083392008-05-12 21:20:42 +0200840#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100841
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400842#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400843# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400844#endif
845
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500846static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
847
Steven Rostedtb6887d72009-02-17 12:32:04 -0500848struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500849 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500850 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500851 unsigned long flags;
852 unsigned long ip;
853 void *data;
854 struct rcu_head rcu;
855};
856
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200857enum {
858 FTRACE_ENABLE_CALLS = (1 << 0),
859 FTRACE_DISABLE_CALLS = (1 << 1),
860 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
861 FTRACE_ENABLE_MCOUNT = (1 << 3),
862 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500863 FTRACE_START_FUNC_RET = (1 << 5),
864 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200865};
866
Steven Rostedt5072c592008-05-12 21:20:43 +0200867static int ftrace_filtered;
868
Lai Jiangshane94142a2009-03-13 17:51:27 +0800869static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200870
Steven Rostedt41c52c02008-05-22 11:46:33 -0400871static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200872
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200873struct ftrace_page {
874 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500875 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200876 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700877};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200878
879#define ENTRIES_PER_PAGE \
880 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
881
882/* estimate from running different kernels */
883#define NR_TO_INIT 10000
884
885static struct ftrace_page *ftrace_pages_start;
886static struct ftrace_page *ftrace_pages;
887
Steven Rostedt37ad5082008-05-12 21:20:48 +0200888static struct dyn_ftrace *ftrace_free_records;
889
Steven Rostedt265c8312009-02-13 12:43:56 -0500890/*
891 * This is a double for. Do not use 'break' to break out of the loop,
892 * you must use a goto.
893 */
894#define do_for_each_ftrace_rec(pg, rec) \
895 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
896 int _____i; \
897 for (_____i = 0; _____i < pg->index; _____i++) { \
898 rec = &pg->records[_____i];
899
900#define while_for_each_ftrace_rec() \
901 } \
902 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530903
Ingo Molnare309b412008-05-12 21:20:51 +0200904static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200905{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800906 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200907 ftrace_free_records = rec;
908 rec->flags |= FTRACE_FL_FREE;
909}
910
Ingo Molnare309b412008-05-12 21:20:51 +0200911static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200912{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200913 struct dyn_ftrace *rec;
914
915 /* First check for freed records */
916 if (ftrace_free_records) {
917 rec = ftrace_free_records;
918
Steven Rostedt37ad5082008-05-12 21:20:48 +0200919 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400920 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200921 ftrace_free_records = NULL;
922 return NULL;
923 }
924
Lai Jiangshanee000b72009-03-24 13:38:06 +0800925 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200926 memset(rec, 0, sizeof(*rec));
927 return rec;
928 }
929
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200930 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400931 if (!ftrace_pages->next) {
932 /* allocate another page */
933 ftrace_pages->next =
934 (void *)get_zeroed_page(GFP_KERNEL);
935 if (!ftrace_pages->next)
936 return NULL;
937 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200938 ftrace_pages = ftrace_pages->next;
939 }
940
941 return &ftrace_pages->records[ftrace_pages->index++];
942}
943
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400944static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200945ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200946{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400947 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200948
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800949 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400950 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200951
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400952 rec = ftrace_alloc_dyn_node(ip);
953 if (!rec)
954 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200955
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400956 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800957 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800958 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200959
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400960 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200961}
962
Steven Rostedt05736a42008-09-22 14:55:47 -0700963static void print_ip_ins(const char *fmt, unsigned char *p)
964{
965 int i;
966
967 printk(KERN_CONT "%s", fmt);
968
969 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
970 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
971}
972
Steven Rostedt31e88902008-11-14 16:21:19 -0800973static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800974{
975 switch (failed) {
976 case -EFAULT:
977 FTRACE_WARN_ON_ONCE(1);
978 pr_info("ftrace faulted on modifying ");
979 print_ip_sym(ip);
980 break;
981 case -EINVAL:
982 FTRACE_WARN_ON_ONCE(1);
983 pr_info("ftrace failed to modify ");
984 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800985 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800986 printk(KERN_CONT "\n");
987 break;
988 case -EPERM:
989 FTRACE_WARN_ON_ONCE(1);
990 pr_info("ftrace faulted on writing ");
991 print_ip_sym(ip);
992 break;
993 default:
994 FTRACE_WARN_ON_ONCE(1);
995 pr_info("ftrace faulted on unknown error ");
996 print_ip_sym(ip);
997 }
998}
999
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001000
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001001/* Return 1 if the address range is reserved for ftrace */
1002int ftrace_text_reserved(void *start, void *end)
1003{
1004 struct dyn_ftrace *rec;
1005 struct ftrace_page *pg;
1006
1007 do_for_each_ftrace_rec(pg, rec) {
1008 if (rec->ip <= (unsigned long)end &&
1009 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1010 return 1;
1011 } while_for_each_ftrace_rec();
1012 return 0;
1013}
1014
1015
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301016static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001017__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001018{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001019 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001020 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001021
Shaohua Lif0001202009-01-09 11:29:42 +08001022 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001023
Steven Rostedt982c3502008-11-15 16:31:41 -05001024 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001025 * If this record is not to be traced or we want to disable it,
1026 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001027 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001028 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001029 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001030 * If we want to enable it and filtering is on, enable it only if
1031 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001032 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001033 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1034 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1035 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001036 }
1037
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001038 /* If the state of this record hasn't changed, then do nothing */
1039 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1040 return 0;
1041
1042 if (flag) {
1043 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001044 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001045 }
1046
1047 rec->flags &= ~FTRACE_FL_ENABLED;
1048 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001049}
1050
1051static void ftrace_replace_code(int enable)
1052{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001053 struct dyn_ftrace *rec;
1054 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001055 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001056
Steven Rostedt265c8312009-02-13 12:43:56 -05001057 do_for_each_ftrace_rec(pg, rec) {
1058 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001059 * Skip over free records, records that have
1060 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001061 */
1062 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001063 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001064 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001065 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001066
Steven Rostedt265c8312009-02-13 12:43:56 -05001067 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001068 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001069 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001070 ftrace_bug(failed, rec->ip);
1071 /* Stop processing */
1072 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001073 }
1074 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001075}
1076
Ingo Molnare309b412008-05-12 21:20:51 +02001077static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001078ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001079{
1080 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001081 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001082
1083 ip = rec->ip;
1084
Shaohua Li25aac9d2009-01-09 11:29:40 +08001085 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001086 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001087 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001088 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301089 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001090 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301091 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001092}
1093
Steven Rostedt000ab692009-02-17 13:35:06 -05001094/*
1095 * archs can override this function if they must do something
1096 * before the modifying code is performed.
1097 */
1098int __weak ftrace_arch_code_modify_prepare(void)
1099{
1100 return 0;
1101}
1102
1103/*
1104 * archs can override this function if they must do something
1105 * after the modifying code is performed.
1106 */
1107int __weak ftrace_arch_code_modify_post_process(void)
1108{
1109 return 0;
1110}
1111
Ingo Molnare309b412008-05-12 21:20:51 +02001112static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001113{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001114 int *command = data;
1115
Steven Rostedta3583242008-11-11 15:01:42 -05001116 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001117 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001118 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001119 ftrace_replace_code(0);
1120
1121 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1122 ftrace_update_ftrace_func(ftrace_trace_function);
1123
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001124 if (*command & FTRACE_START_FUNC_RET)
1125 ftrace_enable_ftrace_graph_caller();
1126 else if (*command & FTRACE_STOP_FUNC_RET)
1127 ftrace_disable_ftrace_graph_caller();
1128
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001129 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001130}
1131
Ingo Molnare309b412008-05-12 21:20:51 +02001132static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001133{
Steven Rostedt000ab692009-02-17 13:35:06 -05001134 int ret;
1135
1136 ret = ftrace_arch_code_modify_prepare();
1137 FTRACE_WARN_ON(ret);
1138 if (ret)
1139 return;
1140
Rusty Russell784e2d72008-07-28 12:16:31 -05001141 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001142
1143 ret = ftrace_arch_code_modify_post_process();
1144 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001145}
1146
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001147static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001148static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001149
1150static void ftrace_startup_enable(int command)
1151{
1152 if (saved_ftrace_func != ftrace_trace_function) {
1153 saved_ftrace_func = ftrace_trace_function;
1154 command |= FTRACE_UPDATE_TRACE_FUNC;
1155 }
1156
1157 if (!command || !ftrace_enabled)
1158 return;
1159
1160 ftrace_run_update_code(command);
1161}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001162
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001163static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001164{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001165 if (unlikely(ftrace_disabled))
1166 return;
1167
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001168 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001169 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001170
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001171 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001172}
1173
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001174static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001175{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001176 if (unlikely(ftrace_disabled))
1177 return;
1178
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001179 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001180 /*
1181 * Just warn in case of unbalance, no need to kill ftrace, it's not
1182 * critical but the ftrace_call callers may be never nopped again after
1183 * further ftrace uses.
1184 */
1185 WARN_ON_ONCE(ftrace_start_up < 0);
1186
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001187 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001188 command |= FTRACE_DISABLE_CALLS;
1189
1190 if (saved_ftrace_func != ftrace_trace_function) {
1191 saved_ftrace_func = ftrace_trace_function;
1192 command |= FTRACE_UPDATE_TRACE_FUNC;
1193 }
1194
1195 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001196 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001197
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001198 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001199}
1200
Ingo Molnare309b412008-05-12 21:20:51 +02001201static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001202{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001203 int command = FTRACE_ENABLE_MCOUNT;
1204
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001205 if (unlikely(ftrace_disabled))
1206 return;
1207
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001208 /* Force update next time */
1209 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001210 /* ftrace_start_up is true if we want ftrace running */
1211 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001212 command |= FTRACE_ENABLE_CALLS;
1213
1214 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001215}
1216
Ingo Molnare309b412008-05-12 21:20:51 +02001217static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001218{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001219 int command = FTRACE_DISABLE_MCOUNT;
1220
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001221 if (unlikely(ftrace_disabled))
1222 return;
1223
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001224 /* ftrace_start_up is true if ftrace is running */
1225 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001226 command |= FTRACE_DISABLE_CALLS;
1227
1228 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001229}
1230
Steven Rostedt3d083392008-05-12 21:20:42 +02001231static cycle_t ftrace_update_time;
1232static unsigned long ftrace_update_cnt;
1233unsigned long ftrace_update_tot_cnt;
1234
Steven Rostedt31e88902008-11-14 16:21:19 -08001235static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001236{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001237 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301238 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001239
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001240 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001241 ftrace_update_cnt = 0;
1242
Lai Jiangshane94142a2009-03-13 17:51:27 +08001243 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301244
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001245 /* If something went wrong, bail without enabling anything */
1246 if (unlikely(ftrace_disabled))
1247 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001248
Lai Jiangshane94142a2009-03-13 17:51:27 +08001249 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001250 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001251 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301252
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001253 /*
1254 * Do the initial record convertion from mcount jump
1255 * to the NOP instructions.
1256 */
1257 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001258 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001259 continue;
1260 }
1261
1262 p->flags |= FTRACE_FL_CONVERTED;
1263 ftrace_update_cnt++;
1264
1265 /*
1266 * If the tracing is enabled, go ahead and enable the record.
1267 *
1268 * The reason not to enable the record immediatelly is the
1269 * inherent check of ftrace_make_nop/ftrace_make_call for
1270 * correct previous instructions. Making first the NOP
1271 * conversion puts the module to the correct state, thus
1272 * passing the ftrace_make_call check.
1273 */
1274 if (ftrace_start_up) {
1275 int failed = __ftrace_replace_code(p, 1);
1276 if (failed) {
1277 ftrace_bug(failed, p->ip);
1278 ftrace_free_rec(p);
1279 }
1280 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001281 }
1282
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001283 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001284 ftrace_update_time = stop - start;
1285 ftrace_update_tot_cnt += ftrace_update_cnt;
1286
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001287 return 0;
1288}
1289
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001290static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001291{
1292 struct ftrace_page *pg;
1293 int cnt;
1294 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001295
1296 /* allocate a few pages */
1297 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1298 if (!ftrace_pages_start)
1299 return -1;
1300
1301 /*
1302 * Allocate a few more pages.
1303 *
1304 * TODO: have some parser search vmlinux before
1305 * final linking to find all calls to ftrace.
1306 * Then we can:
1307 * a) know how many pages to allocate.
1308 * and/or
1309 * b) set up the table then.
1310 *
1311 * The dynamic code is still necessary for
1312 * modules.
1313 */
1314
1315 pg = ftrace_pages = ftrace_pages_start;
1316
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001317 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001318 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001319 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001320
1321 for (i = 0; i < cnt; i++) {
1322 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1323
1324 /* If we fail, we'll try later anyway */
1325 if (!pg->next)
1326 break;
1327
1328 pg = pg->next;
1329 }
1330
1331 return 0;
1332}
1333
Steven Rostedt5072c592008-05-12 21:20:43 +02001334enum {
1335 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001336 FTRACE_ITER_NOTRACE = (1 << 1),
1337 FTRACE_ITER_FAILURES = (1 << 2),
1338 FTRACE_ITER_PRINTALL = (1 << 3),
1339 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001340};
1341
1342#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1343
1344struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001345 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001346 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001347 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001348 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001349 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001350};
1351
Ingo Molnare309b412008-05-12 21:20:51 +02001352static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001353t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1354{
1355 struct ftrace_iterator *iter = m->private;
1356 struct hlist_node *hnd = v;
1357 struct hlist_head *hhd;
1358
1359 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1360
1361 (*pos)++;
1362
1363 retry:
1364 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1365 return NULL;
1366
1367 hhd = &ftrace_func_hash[iter->hidx];
1368
1369 if (hlist_empty(hhd)) {
1370 iter->hidx++;
1371 hnd = NULL;
1372 goto retry;
1373 }
1374
1375 if (!hnd)
1376 hnd = hhd->first;
1377 else {
1378 hnd = hnd->next;
1379 if (!hnd) {
1380 iter->hidx++;
1381 goto retry;
1382 }
1383 }
1384
1385 return hnd;
1386}
1387
1388static void *t_hash_start(struct seq_file *m, loff_t *pos)
1389{
1390 struct ftrace_iterator *iter = m->private;
1391 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001392 loff_t l;
1393
1394 if (!(iter->flags & FTRACE_ITER_HASH))
1395 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001396
1397 iter->flags |= FTRACE_ITER_HASH;
1398
Li Zefand82d6242009-06-24 09:54:54 +08001399 iter->hidx = 0;
1400 for (l = 0; l <= *pos; ) {
1401 p = t_hash_next(m, p, &l);
1402 if (!p)
1403 break;
1404 }
1405 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001406}
1407
1408static int t_hash_show(struct seq_file *m, void *v)
1409{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001410 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001411 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001412
Steven Rostedtb6887d72009-02-17 12:32:04 -05001413 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001414
Steven Rostedt809dcf22009-02-16 23:06:01 -05001415 if (rec->ops->print)
1416 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1417
Steven Rostedtb375a112009-09-17 00:05:58 -04001418 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001419
1420 if (rec->data)
1421 seq_printf(m, ":%p", rec->data);
1422 seq_putc(m, '\n');
1423
1424 return 0;
1425}
1426
1427static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001428t_next(struct seq_file *m, void *v, loff_t *pos)
1429{
1430 struct ftrace_iterator *iter = m->private;
1431 struct dyn_ftrace *rec = NULL;
1432
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001433 if (iter->flags & FTRACE_ITER_HASH)
1434 return t_hash_next(m, v, pos);
1435
Steven Rostedt5072c592008-05-12 21:20:43 +02001436 (*pos)++;
1437
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001438 if (iter->flags & FTRACE_ITER_PRINTALL)
1439 return NULL;
1440
Steven Rostedt5072c592008-05-12 21:20:43 +02001441 retry:
1442 if (iter->idx >= iter->pg->index) {
1443 if (iter->pg->next) {
1444 iter->pg = iter->pg->next;
1445 iter->idx = 0;
1446 goto retry;
1447 }
1448 } else {
1449 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001450 if ((rec->flags & FTRACE_FL_FREE) ||
1451
1452 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301453 (rec->flags & FTRACE_FL_FAILED)) ||
1454
1455 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001456 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301457
Steven Rostedt0183fb12008-11-07 22:36:02 -05001458 ((iter->flags & FTRACE_ITER_FILTER) &&
1459 !(rec->flags & FTRACE_FL_FILTER)) ||
1460
Steven Rostedt41c52c02008-05-22 11:46:33 -04001461 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1462 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001463 rec = NULL;
1464 goto retry;
1465 }
1466 }
1467
Steven Rostedt5072c592008-05-12 21:20:43 +02001468 return rec;
1469}
1470
1471static void *t_start(struct seq_file *m, loff_t *pos)
1472{
1473 struct ftrace_iterator *iter = m->private;
1474 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001475 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001476
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001477 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001478 /*
1479 * For set_ftrace_filter reading, if we have the filter
1480 * off, we can short cut and just print out that all
1481 * functions are enabled.
1482 */
1483 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1484 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001485 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001486 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001487 return iter;
1488 }
1489
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001490 if (iter->flags & FTRACE_ITER_HASH)
1491 return t_hash_start(m, pos);
1492
Li Zefan694ce0a2009-06-24 09:54:19 +08001493 iter->pg = ftrace_pages_start;
1494 iter->idx = 0;
1495 for (l = 0; l <= *pos; ) {
1496 p = t_next(m, p, &l);
1497 if (!p)
1498 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001499 }
walimis5821e1b2008-11-15 15:19:06 +08001500
Li Zefan694ce0a2009-06-24 09:54:19 +08001501 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001502 return t_hash_start(m, pos);
1503
Steven Rostedt5072c592008-05-12 21:20:43 +02001504 return p;
1505}
1506
1507static void t_stop(struct seq_file *m, void *p)
1508{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001509 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001510}
1511
1512static int t_show(struct seq_file *m, void *v)
1513{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001514 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001515 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001516
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001517 if (iter->flags & FTRACE_ITER_HASH)
1518 return t_hash_show(m, v);
1519
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001520 if (iter->flags & FTRACE_ITER_PRINTALL) {
1521 seq_printf(m, "#### all functions enabled ####\n");
1522 return 0;
1523 }
1524
Steven Rostedt5072c592008-05-12 21:20:43 +02001525 if (!rec)
1526 return 0;
1527
Steven Rostedtb375a112009-09-17 00:05:58 -04001528 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001529
1530 return 0;
1531}
1532
James Morris88e9d342009-09-22 16:43:43 -07001533static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001534 .start = t_start,
1535 .next = t_next,
1536 .stop = t_stop,
1537 .show = t_show,
1538};
1539
Ingo Molnare309b412008-05-12 21:20:51 +02001540static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001541ftrace_avail_open(struct inode *inode, struct file *file)
1542{
1543 struct ftrace_iterator *iter;
1544 int ret;
1545
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001546 if (unlikely(ftrace_disabled))
1547 return -ENODEV;
1548
Steven Rostedt5072c592008-05-12 21:20:43 +02001549 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1550 if (!iter)
1551 return -ENOMEM;
1552
1553 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001554
1555 ret = seq_open(file, &show_ftrace_seq_ops);
1556 if (!ret) {
1557 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001558
Steven Rostedt5072c592008-05-12 21:20:43 +02001559 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001560 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001561 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001562 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001563
1564 return ret;
1565}
1566
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301567static int
1568ftrace_failures_open(struct inode *inode, struct file *file)
1569{
1570 int ret;
1571 struct seq_file *m;
1572 struct ftrace_iterator *iter;
1573
1574 ret = ftrace_avail_open(inode, file);
1575 if (!ret) {
1576 m = (struct seq_file *)file->private_data;
1577 iter = (struct ftrace_iterator *)m->private;
1578 iter->flags = FTRACE_ITER_FAILURES;
1579 }
1580
1581 return ret;
1582}
1583
1584
Steven Rostedt41c52c02008-05-22 11:46:33 -04001585static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001586{
1587 struct ftrace_page *pg;
1588 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001589 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001590
Steven Rostedt52baf112009-02-14 01:15:39 -05001591 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001592 if (enable)
1593 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001594 do_for_each_ftrace_rec(pg, rec) {
1595 if (rec->flags & FTRACE_FL_FAILED)
1596 continue;
1597 rec->flags &= ~type;
1598 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001599 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001600}
1601
Ingo Molnare309b412008-05-12 21:20:51 +02001602static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001603ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001604{
1605 struct ftrace_iterator *iter;
1606 int ret = 0;
1607
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001608 if (unlikely(ftrace_disabled))
1609 return -ENODEV;
1610
Steven Rostedt5072c592008-05-12 21:20:43 +02001611 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1612 if (!iter)
1613 return -ENOMEM;
1614
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001615 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1616 kfree(iter);
1617 return -ENOMEM;
1618 }
1619
Steven Rostedt41c52c02008-05-22 11:46:33 -04001620 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001621 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001622 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001623 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001624
1625 if (file->f_mode & FMODE_READ) {
1626 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001627 iter->flags = enable ? FTRACE_ITER_FILTER :
1628 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001629
1630 ret = seq_open(file, &show_ftrace_seq_ops);
1631 if (!ret) {
1632 struct seq_file *m = file->private_data;
1633 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001634 } else {
1635 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001636 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001637 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001638 } else
1639 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001640 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001641
1642 return ret;
1643}
1644
Steven Rostedt41c52c02008-05-22 11:46:33 -04001645static int
1646ftrace_filter_open(struct inode *inode, struct file *file)
1647{
1648 return ftrace_regex_open(inode, file, 1);
1649}
1650
1651static int
1652ftrace_notrace_open(struct inode *inode, struct file *file)
1653{
1654 return ftrace_regex_open(inode, file, 0);
1655}
1656
Ingo Molnare309b412008-05-12 21:20:51 +02001657static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001658ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001659{
1660 loff_t ret;
1661
1662 if (file->f_mode & FMODE_READ)
1663 ret = seq_lseek(file, offset, origin);
1664 else
1665 file->f_pos = ret = 1;
1666
1667 return ret;
1668}
1669
Steven Rostedt64e7c442009-02-13 17:08:48 -05001670static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001671{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001672 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001673 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001674
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001675 switch (type) {
1676 case MATCH_FULL:
1677 if (strcmp(str, regex) == 0)
1678 matched = 1;
1679 break;
1680 case MATCH_FRONT_ONLY:
1681 if (strncmp(str, regex, len) == 0)
1682 matched = 1;
1683 break;
1684 case MATCH_MIDDLE_ONLY:
1685 if (strstr(str, regex))
1686 matched = 1;
1687 break;
1688 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001689 slen = strlen(str);
1690 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001691 matched = 1;
1692 break;
1693 }
1694
1695 return matched;
1696}
1697
Steven Rostedt64e7c442009-02-13 17:08:48 -05001698static int
1699ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1700{
1701 char str[KSYM_SYMBOL_LEN];
1702
1703 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1704 return ftrace_match(str, regex, len, type);
1705}
1706
Li Zefan311d16d2009-12-08 11:15:11 +08001707static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001708{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001709 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001710 struct ftrace_page *pg;
1711 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001712 unsigned long flag;
1713 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001714 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001715 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001716 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001717
Steven Rostedt6a24a242009-02-17 11:20:26 -05001718 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001719 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001720
1721 search_len = strlen(search);
1722
Steven Rostedt52baf112009-02-14 01:15:39 -05001723 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001724 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001725
Steven Rostedt265c8312009-02-13 12:43:56 -05001726 if (rec->flags & FTRACE_FL_FAILED)
1727 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001728
1729 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001730 if (not)
1731 rec->flags &= ~flag;
1732 else
1733 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001734 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001735 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001736 /*
1737 * Only enable filtering if we have a function that
1738 * is filtered on.
1739 */
1740 if (enable && (rec->flags & FTRACE_FL_FILTER))
1741 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001742 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001743 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001744
1745 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001746}
1747
Steven Rostedt64e7c442009-02-13 17:08:48 -05001748static int
1749ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1750 char *regex, int len, int type)
1751{
1752 char str[KSYM_SYMBOL_LEN];
1753 char *modname;
1754
1755 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1756
1757 if (!modname || strcmp(modname, mod))
1758 return 0;
1759
1760 /* blank search means to match all funcs in the mod */
1761 if (len)
1762 return ftrace_match(str, regex, len, type);
1763 else
1764 return 1;
1765}
1766
Li Zefan311d16d2009-12-08 11:15:11 +08001767static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001768{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001769 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001770 struct ftrace_page *pg;
1771 struct dyn_ftrace *rec;
1772 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001773 char *search = buff;
1774 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001775 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001776 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001777
Steven Rostedt6a24a242009-02-17 11:20:26 -05001778 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1779
Steven Rostedt64e7c442009-02-13 17:08:48 -05001780 /* blank or '*' mean the same */
1781 if (strcmp(buff, "*") == 0)
1782 buff[0] = 0;
1783
1784 /* handle the case of 'dont filter this module' */
1785 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1786 buff[0] = 0;
1787 not = 1;
1788 }
1789
1790 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001791 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001792 search_len = strlen(search);
1793 }
1794
Steven Rostedt52baf112009-02-14 01:15:39 -05001795 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001796 do_for_each_ftrace_rec(pg, rec) {
1797
1798 if (rec->flags & FTRACE_FL_FAILED)
1799 continue;
1800
1801 if (ftrace_match_module_record(rec, mod,
1802 search, search_len, type)) {
1803 if (not)
1804 rec->flags &= ~flag;
1805 else
1806 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001807 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001808 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001809 if (enable && (rec->flags & FTRACE_FL_FILTER))
1810 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001811
1812 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001813 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001814
1815 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001816}
1817
Steven Rostedtf6180772009-02-14 00:40:25 -05001818/*
1819 * We register the module command as a template to show others how
1820 * to register the a command as well.
1821 */
1822
1823static int
1824ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1825{
1826 char *mod;
1827
1828 /*
1829 * cmd == 'mod' because we only registered this func
1830 * for the 'mod' ftrace_func_command.
1831 * But if you register one func with multiple commands,
1832 * you can tell which command was used by the cmd
1833 * parameter.
1834 */
1835
1836 /* we must have a module name */
1837 if (!param)
1838 return -EINVAL;
1839
1840 mod = strsep(&param, ":");
1841 if (!strlen(mod))
1842 return -EINVAL;
1843
Li Zefan311d16d2009-12-08 11:15:11 +08001844 if (ftrace_match_module_records(func, mod, enable))
1845 return 0;
1846 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001847}
1848
1849static struct ftrace_func_command ftrace_mod_cmd = {
1850 .name = "mod",
1851 .func = ftrace_mod_callback,
1852};
1853
1854static int __init ftrace_mod_cmd_init(void)
1855{
1856 return register_ftrace_command(&ftrace_mod_cmd);
1857}
1858device_initcall(ftrace_mod_cmd_init);
1859
Steven Rostedt59df055f2009-02-14 15:29:06 -05001860static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001861function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001862{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001863 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001864 struct hlist_head *hhd;
1865 struct hlist_node *n;
1866 unsigned long key;
1867 int resched;
1868
1869 key = hash_long(ip, FTRACE_HASH_BITS);
1870
1871 hhd = &ftrace_func_hash[key];
1872
1873 if (hlist_empty(hhd))
1874 return;
1875
1876 /*
1877 * Disable preemption for these calls to prevent a RCU grace
1878 * period. This syncs the hash iteration and freeing of items
1879 * on the hash. rcu_read_lock is too dangerous here.
1880 */
1881 resched = ftrace_preempt_disable();
1882 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1883 if (entry->ip == ip)
1884 entry->ops->func(ip, parent_ip, &entry->data);
1885 }
1886 ftrace_preempt_enable(resched);
1887}
1888
Steven Rostedtb6887d72009-02-17 12:32:04 -05001889static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001890{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001891 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001892};
1893
Steven Rostedtb6887d72009-02-17 12:32:04 -05001894static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001895
Steven Rostedtb6887d72009-02-17 12:32:04 -05001896static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001897{
1898 int i;
1899
Steven Rostedtb6887d72009-02-17 12:32:04 -05001900 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001901 return;
1902
1903 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1904 struct hlist_head *hhd = &ftrace_func_hash[i];
1905 if (hhd->first)
1906 break;
1907 }
1908 /* Nothing registered? */
1909 if (i == FTRACE_FUNC_HASHSIZE)
1910 return;
1911
Steven Rostedtb6887d72009-02-17 12:32:04 -05001912 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001913 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001914 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915}
1916
Steven Rostedtb6887d72009-02-17 12:32:04 -05001917static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001918{
1919 int i;
1920
Steven Rostedtb6887d72009-02-17 12:32:04 -05001921 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001922 return;
1923
1924 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1925 struct hlist_head *hhd = &ftrace_func_hash[i];
1926 if (hhd->first)
1927 return;
1928 }
1929
1930 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001931 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001932 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001933 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001934}
1935
1936
1937static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1938{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001939 struct ftrace_func_probe *entry =
1940 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001941
1942 if (entry->ops->free)
1943 entry->ops->free(&entry->data);
1944 kfree(entry);
1945}
1946
1947
1948int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001949register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950 void *data)
1951{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001952 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001953 struct ftrace_page *pg;
1954 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001955 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001956 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001957 int count = 0;
1958 char *search;
1959
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001960 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001961 len = strlen(search);
1962
Steven Rostedtb6887d72009-02-17 12:32:04 -05001963 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001964 if (WARN_ON(not))
1965 return -EINVAL;
1966
1967 mutex_lock(&ftrace_lock);
1968 do_for_each_ftrace_rec(pg, rec) {
1969
1970 if (rec->flags & FTRACE_FL_FAILED)
1971 continue;
1972
1973 if (!ftrace_match_record(rec, search, len, type))
1974 continue;
1975
1976 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1977 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001978 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001979 if (!count)
1980 count = -ENOMEM;
1981 goto out_unlock;
1982 }
1983
1984 count++;
1985
1986 entry->data = data;
1987
1988 /*
1989 * The caller might want to do something special
1990 * for each function we find. We call the callback
1991 * to give the caller an opportunity to do so.
1992 */
1993 if (ops->callback) {
1994 if (ops->callback(rec->ip, &entry->data) < 0) {
1995 /* caller does not like this func */
1996 kfree(entry);
1997 continue;
1998 }
1999 }
2000
2001 entry->ops = ops;
2002 entry->ip = rec->ip;
2003
2004 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2005 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2006
2007 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002008 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002009
2010 out_unlock:
2011 mutex_unlock(&ftrace_lock);
2012
2013 return count;
2014}
2015
2016enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002017 PROBE_TEST_FUNC = 1,
2018 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002019};
2020
2021static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002022__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002023 void *data, int flags)
2024{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002025 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002026 struct hlist_node *n, *tmp;
2027 char str[KSYM_SYMBOL_LEN];
2028 int type = MATCH_FULL;
2029 int i, len = 0;
2030 char *search;
2031
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002032 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002033 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002034 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002035 int not;
2036
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002037 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002038 len = strlen(search);
2039
Steven Rostedtb6887d72009-02-17 12:32:04 -05002040 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002041 if (WARN_ON(not))
2042 return;
2043 }
2044
2045 mutex_lock(&ftrace_lock);
2046 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2047 struct hlist_head *hhd = &ftrace_func_hash[i];
2048
2049 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2050
2051 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002052 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002053 continue;
2054
Steven Rostedtb6887d72009-02-17 12:32:04 -05002055 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002056 continue;
2057
2058 /* do this last, since it is the most expensive */
2059 if (glob) {
2060 kallsyms_lookup(entry->ip, NULL, NULL,
2061 NULL, str);
2062 if (!ftrace_match(str, glob, len, type))
2063 continue;
2064 }
2065
2066 hlist_del(&entry->node);
2067 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2068 }
2069 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002070 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002071 mutex_unlock(&ftrace_lock);
2072}
2073
2074void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002075unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002076 void *data)
2077{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002078 __unregister_ftrace_function_probe(glob, ops, data,
2079 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002080}
2081
2082void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002083unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002084{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002085 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002086}
2087
Steven Rostedtb6887d72009-02-17 12:32:04 -05002088void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002089{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002090 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002091}
2092
Steven Rostedtf6180772009-02-14 00:40:25 -05002093static LIST_HEAD(ftrace_commands);
2094static DEFINE_MUTEX(ftrace_cmd_mutex);
2095
2096int register_ftrace_command(struct ftrace_func_command *cmd)
2097{
2098 struct ftrace_func_command *p;
2099 int ret = 0;
2100
2101 mutex_lock(&ftrace_cmd_mutex);
2102 list_for_each_entry(p, &ftrace_commands, list) {
2103 if (strcmp(cmd->name, p->name) == 0) {
2104 ret = -EBUSY;
2105 goto out_unlock;
2106 }
2107 }
2108 list_add(&cmd->list, &ftrace_commands);
2109 out_unlock:
2110 mutex_unlock(&ftrace_cmd_mutex);
2111
2112 return ret;
2113}
2114
2115int unregister_ftrace_command(struct ftrace_func_command *cmd)
2116{
2117 struct ftrace_func_command *p, *n;
2118 int ret = -ENODEV;
2119
2120 mutex_lock(&ftrace_cmd_mutex);
2121 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2122 if (strcmp(cmd->name, p->name) == 0) {
2123 ret = 0;
2124 list_del_init(&p->list);
2125 goto out_unlock;
2126 }
2127 }
2128 out_unlock:
2129 mutex_unlock(&ftrace_cmd_mutex);
2130
2131 return ret;
2132}
2133
Steven Rostedt64e7c442009-02-13 17:08:48 -05002134static int ftrace_process_regex(char *buff, int len, int enable)
2135{
Steven Rostedtf6180772009-02-14 00:40:25 -05002136 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002137 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002138 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002139
2140 func = strsep(&next, ":");
2141
2142 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002143 if (ftrace_match_records(func, len, enable))
2144 return 0;
2145 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002146 }
2147
Steven Rostedtf6180772009-02-14 00:40:25 -05002148 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002149
2150 command = strsep(&next, ":");
2151
Steven Rostedtf6180772009-02-14 00:40:25 -05002152 mutex_lock(&ftrace_cmd_mutex);
2153 list_for_each_entry(p, &ftrace_commands, list) {
2154 if (strcmp(p->name, command) == 0) {
2155 ret = p->func(func, command, next, enable);
2156 goto out_unlock;
2157 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002158 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002159 out_unlock:
2160 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002161
Steven Rostedtf6180772009-02-14 00:40:25 -05002162 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002163}
2164
Ingo Molnare309b412008-05-12 21:20:51 +02002165static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002166ftrace_regex_write(struct file *file, const char __user *ubuf,
2167 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002168{
2169 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002170 struct trace_parser *parser;
2171 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002172
Li Zefan4ba79782009-09-22 13:52:20 +08002173 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002174 return 0;
2175
Steven Rostedt41c52c02008-05-22 11:46:33 -04002176 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002177
2178 if (file->f_mode & FMODE_READ) {
2179 struct seq_file *m = file->private_data;
2180 iter = m->private;
2181 } else
2182 iter = file->private_data;
2183
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002184 parser = &iter->parser;
2185 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002186
Li Zefan4ba79782009-09-22 13:52:20 +08002187 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002188 !trace_parser_cont(parser)) {
2189 ret = ftrace_process_regex(parser->buffer,
2190 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002191 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002192 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002193 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002194 }
2195
Steven Rostedt5072c592008-05-12 21:20:43 +02002196 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002197out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002198 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002199
Steven Rostedt5072c592008-05-12 21:20:43 +02002200 return ret;
2201}
2202
Steven Rostedt41c52c02008-05-22 11:46:33 -04002203static ssize_t
2204ftrace_filter_write(struct file *file, const char __user *ubuf,
2205 size_t cnt, loff_t *ppos)
2206{
2207 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2208}
2209
2210static ssize_t
2211ftrace_notrace_write(struct file *file, const char __user *ubuf,
2212 size_t cnt, loff_t *ppos)
2213{
2214 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2215}
2216
2217static void
2218ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2219{
2220 if (unlikely(ftrace_disabled))
2221 return;
2222
2223 mutex_lock(&ftrace_regex_lock);
2224 if (reset)
2225 ftrace_filter_reset(enable);
2226 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002227 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002228 mutex_unlock(&ftrace_regex_lock);
2229}
2230
Steven Rostedt77a2b372008-05-12 21:20:45 +02002231/**
2232 * ftrace_set_filter - set a function to filter on in ftrace
2233 * @buf - the string that holds the function filter text.
2234 * @len - the length of the string.
2235 * @reset - non zero to reset all filters before applying this filter.
2236 *
2237 * Filters denote which functions should be enabled when tracing is enabled.
2238 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2239 */
Ingo Molnare309b412008-05-12 21:20:51 +02002240void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002241{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002242 ftrace_set_regex(buf, len, reset, 1);
2243}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002244
Steven Rostedt41c52c02008-05-22 11:46:33 -04002245/**
2246 * ftrace_set_notrace - set a function to not trace in ftrace
2247 * @buf - the string that holds the function notrace text.
2248 * @len - the length of the string.
2249 * @reset - non zero to reset all filters before applying this filter.
2250 *
2251 * Notrace Filters denote which functions should not be enabled when tracing
2252 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2253 * for tracing.
2254 */
2255void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2256{
2257 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002258}
2259
Steven Rostedt2af15d62009-05-28 13:37:24 -04002260/*
2261 * command line interface to allow users to set filters on boot up.
2262 */
2263#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2264static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2265static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2266
2267static int __init set_ftrace_notrace(char *str)
2268{
2269 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2270 return 1;
2271}
2272__setup("ftrace_notrace=", set_ftrace_notrace);
2273
2274static int __init set_ftrace_filter(char *str)
2275{
2276 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2277 return 1;
2278}
2279__setup("ftrace_filter=", set_ftrace_filter);
2280
Stefan Assmann369bc182009-10-12 22:17:21 +02002281#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002282static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Stefan Assmann369bc182009-10-12 22:17:21 +02002283static int __init set_graph_function(char *str)
2284{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002285 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002286 return 1;
2287}
2288__setup("ftrace_graph_filter=", set_graph_function);
2289
2290static void __init set_ftrace_early_graph(char *buf)
2291{
2292 int ret;
2293 char *func;
2294
2295 while (buf) {
2296 func = strsep(&buf, ",");
2297 /* we allow only one expression at a time */
2298 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2299 func);
2300 if (ret)
2301 printk(KERN_DEBUG "ftrace: function %s not "
2302 "traceable\n", func);
2303 }
2304}
2305#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2306
Steven Rostedt2af15d62009-05-28 13:37:24 -04002307static void __init set_ftrace_early_filter(char *buf, int enable)
2308{
2309 char *func;
2310
2311 while (buf) {
2312 func = strsep(&buf, ",");
2313 ftrace_set_regex(func, strlen(func), 0, enable);
2314 }
2315}
2316
2317static void __init set_ftrace_early_filters(void)
2318{
2319 if (ftrace_filter_buf[0])
2320 set_ftrace_early_filter(ftrace_filter_buf, 1);
2321 if (ftrace_notrace_buf[0])
2322 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002323#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2324 if (ftrace_graph_buf[0])
2325 set_ftrace_early_graph(ftrace_graph_buf);
2326#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002327}
2328
Ingo Molnare309b412008-05-12 21:20:51 +02002329static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002330ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002331{
2332 struct seq_file *m = (struct seq_file *)file->private_data;
2333 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002334 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002335
Steven Rostedt41c52c02008-05-22 11:46:33 -04002336 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002337 if (file->f_mode & FMODE_READ) {
2338 iter = m->private;
2339
2340 seq_release(inode, file);
2341 } else
2342 iter = file->private_data;
2343
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002344 parser = &iter->parser;
2345 if (trace_parser_loaded(parser)) {
2346 parser->buffer[parser->idx] = 0;
2347 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002348 }
2349
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002350 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002351 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002352 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002353 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002354
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002355 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002356 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002357
Steven Rostedt41c52c02008-05-22 11:46:33 -04002358 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002359 return 0;
2360}
2361
Steven Rostedt41c52c02008-05-22 11:46:33 -04002362static int
2363ftrace_filter_release(struct inode *inode, struct file *file)
2364{
2365 return ftrace_regex_release(inode, file, 1);
2366}
2367
2368static int
2369ftrace_notrace_release(struct inode *inode, struct file *file)
2370{
2371 return ftrace_regex_release(inode, file, 0);
2372}
2373
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002374static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002375 .open = ftrace_avail_open,
2376 .read = seq_read,
2377 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002378 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002379};
2380
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002381static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302382 .open = ftrace_failures_open,
2383 .read = seq_read,
2384 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002385 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302386};
2387
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002388static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002389 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002390 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002391 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002392 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002393 .release = ftrace_filter_release,
2394};
2395
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002396static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002397 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002398 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002399 .write = ftrace_notrace_write,
2400 .llseek = ftrace_regex_lseek,
2401 .release = ftrace_notrace_release,
2402};
2403
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002404#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2405
2406static DEFINE_MUTEX(graph_lock);
2407
2408int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002409int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002410unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2411
2412static void *
Li Zefan85951842009-06-24 09:54:00 +08002413__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002414{
Li Zefan85951842009-06-24 09:54:00 +08002415 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002416 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002417 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002418}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002419
Li Zefan85951842009-06-24 09:54:00 +08002420static void *
2421g_next(struct seq_file *m, void *v, loff_t *pos)
2422{
2423 (*pos)++;
2424 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002425}
2426
2427static void *g_start(struct seq_file *m, loff_t *pos)
2428{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002429 mutex_lock(&graph_lock);
2430
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002431 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002432 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002433 return (void *)1;
2434
Li Zefan85951842009-06-24 09:54:00 +08002435 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002436}
2437
2438static void g_stop(struct seq_file *m, void *p)
2439{
2440 mutex_unlock(&graph_lock);
2441}
2442
2443static int g_show(struct seq_file *m, void *v)
2444{
2445 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002446
2447 if (!ptr)
2448 return 0;
2449
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002450 if (ptr == (unsigned long *)1) {
2451 seq_printf(m, "#### all functions enabled ####\n");
2452 return 0;
2453 }
2454
Steven Rostedtb375a112009-09-17 00:05:58 -04002455 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002456
2457 return 0;
2458}
2459
James Morris88e9d342009-09-22 16:43:43 -07002460static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002461 .start = g_start,
2462 .next = g_next,
2463 .stop = g_stop,
2464 .show = g_show,
2465};
2466
2467static int
2468ftrace_graph_open(struct inode *inode, struct file *file)
2469{
2470 int ret = 0;
2471
2472 if (unlikely(ftrace_disabled))
2473 return -ENODEV;
2474
2475 mutex_lock(&graph_lock);
2476 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002477 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002478 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002479 ftrace_graph_count = 0;
2480 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2481 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002482 mutex_unlock(&graph_lock);
2483
Li Zefana4ec5e02009-09-18 14:06:28 +08002484 if (file->f_mode & FMODE_READ)
2485 ret = seq_open(file, &ftrace_graph_seq_ops);
2486
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002487 return ret;
2488}
2489
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002490static int
Li Zefan87827112009-07-23 11:29:11 +08002491ftrace_graph_release(struct inode *inode, struct file *file)
2492{
2493 if (file->f_mode & FMODE_READ)
2494 seq_release(inode, file);
2495 return 0;
2496}
2497
2498static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002499ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002500{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002501 struct dyn_ftrace *rec;
2502 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002503 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002504 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002505 int type, not;
2506 char *search;
2507 bool exists;
2508 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002509
2510 if (ftrace_disabled)
2511 return -ENODEV;
2512
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002513 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002514 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002515 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2516 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002517
2518 search_len = strlen(search);
2519
Steven Rostedt52baf112009-02-14 01:15:39 -05002520 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002521 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002522
Steven Rostedt265c8312009-02-13 12:43:56 -05002523 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2524 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002525
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002526 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002527 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002528 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002529 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002530 if (array[i] == rec->ip) {
2531 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002532 break;
2533 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002534 }
2535
2536 if (!not) {
2537 fail = 0;
2538 if (!exists) {
2539 array[(*idx)++] = rec->ip;
2540 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2541 goto out;
2542 }
2543 } else {
2544 if (exists) {
2545 array[i] = array[--(*idx)];
2546 array[*idx] = 0;
2547 fail = 0;
2548 }
2549 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002550 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002551 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002552out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002553 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002554
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002555 if (fail)
2556 return -EINVAL;
2557
2558 ftrace_graph_filter_enabled = 1;
2559 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002560}
2561
2562static ssize_t
2563ftrace_graph_write(struct file *file, const char __user *ubuf,
2564 size_t cnt, loff_t *ppos)
2565{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002566 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002567 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002568
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002569 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002570 return 0;
2571
2572 mutex_lock(&graph_lock);
2573
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002574 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2575 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002576 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002577 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002578
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002579 read = trace_get_user(&parser, ubuf, cnt, ppos);
2580
Li Zefan4ba79782009-09-22 13:52:20 +08002581 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002582 parser.buffer[parser.idx] = 0;
2583
2584 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002585 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002586 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002587 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002588 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002589 }
2590
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002591 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002592
2593out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002594 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002595out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002596 mutex_unlock(&graph_lock);
2597
2598 return ret;
2599}
2600
2601static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002602 .open = ftrace_graph_open,
2603 .read = seq_read,
2604 .write = ftrace_graph_write,
2605 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002606};
2607#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2608
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002609static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002610{
Steven Rostedt5072c592008-05-12 21:20:43 +02002611
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002612 trace_create_file("available_filter_functions", 0444,
2613 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002614
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002615 trace_create_file("failures", 0444,
2616 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302617
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002618 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2619 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002620
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002621 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002622 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002623
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002624#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002625 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002626 NULL,
2627 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002628#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2629
Steven Rostedt5072c592008-05-12 21:20:43 +02002630 return 0;
2631}
2632
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002633static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002634 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002635 unsigned long *end)
2636{
2637 unsigned long *p;
2638 unsigned long addr;
2639 unsigned long flags;
2640
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002641 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002642 p = start;
2643 while (p < end) {
2644 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002645 /*
2646 * Some architecture linkers will pad between
2647 * the different mcount_loc sections of different
2648 * object files to satisfy alignments.
2649 * Skip any NULL pointers.
2650 */
2651 if (!addr)
2652 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002653 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002654 }
2655
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002656 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002657 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002658 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002659 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002660 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002661
2662 return 0;
2663}
2664
Steven Rostedt93eb6772009-04-15 13:24:06 -04002665#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002666void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002667{
2668 struct dyn_ftrace *rec;
2669 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002670
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002671 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002672 return;
2673
2674 mutex_lock(&ftrace_lock);
2675 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002676 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002677 /*
2678 * rec->ip is changed in ftrace_free_rec()
2679 * It should not between s and e if record was freed.
2680 */
2681 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2682 ftrace_free_rec(rec);
2683 }
2684 } while_for_each_ftrace_rec();
2685 mutex_unlock(&ftrace_lock);
2686}
2687
2688static void ftrace_init_module(struct module *mod,
2689 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002690{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002691 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002692 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002693 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002694}
2695
Steven Rostedt93eb6772009-04-15 13:24:06 -04002696static int ftrace_module_notify(struct notifier_block *self,
2697 unsigned long val, void *data)
2698{
2699 struct module *mod = data;
2700
2701 switch (val) {
2702 case MODULE_STATE_COMING:
2703 ftrace_init_module(mod, mod->ftrace_callsites,
2704 mod->ftrace_callsites +
2705 mod->num_ftrace_callsites);
2706 break;
2707 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002708 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002709 break;
2710 }
2711
2712 return 0;
2713}
2714#else
2715static int ftrace_module_notify(struct notifier_block *self,
2716 unsigned long val, void *data)
2717{
2718 return 0;
2719}
2720#endif /* CONFIG_MODULES */
2721
2722struct notifier_block ftrace_module_nb = {
2723 .notifier_call = ftrace_module_notify,
2724 .priority = 0,
2725};
2726
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002727extern unsigned long __start_mcount_loc[];
2728extern unsigned long __stop_mcount_loc[];
2729
2730void __init ftrace_init(void)
2731{
2732 unsigned long count, addr, flags;
2733 int ret;
2734
2735 /* Keep the ftrace pointer to the stub */
2736 addr = (unsigned long)ftrace_stub;
2737
2738 local_irq_save(flags);
2739 ftrace_dyn_arch_init(&addr);
2740 local_irq_restore(flags);
2741
2742 /* ftrace_dyn_arch_init places the return code in addr */
2743 if (addr)
2744 goto failed;
2745
2746 count = __stop_mcount_loc - __start_mcount_loc;
2747
2748 ret = ftrace_dyn_table_alloc(count);
2749 if (ret)
2750 goto failed;
2751
2752 last_ftrace_enabled = ftrace_enabled = 1;
2753
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002754 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002755 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002756 __stop_mcount_loc);
2757
Steven Rostedt93eb6772009-04-15 13:24:06 -04002758 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002759 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002760 pr_warning("Failed to register trace ftrace module notifier\n");
2761
Steven Rostedt2af15d62009-05-28 13:37:24 -04002762 set_ftrace_early_filters();
2763
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002764 return;
2765 failed:
2766 ftrace_disabled = 1;
2767}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002768
Steven Rostedt3d083392008-05-12 21:20:42 +02002769#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002770
2771static int __init ftrace_nodyn_init(void)
2772{
2773 ftrace_enabled = 1;
2774 return 0;
2775}
2776device_initcall(ftrace_nodyn_init);
2777
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002778static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2779static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002780/* Keep as macros so we do not need to define the commands */
2781# define ftrace_startup(command) do { } while (0)
2782# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002783# define ftrace_startup_sysctl() do { } while (0)
2784# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002785#endif /* CONFIG_DYNAMIC_FTRACE */
2786
Steven Rostedte32d8952008-12-04 00:26:41 -05002787static void clear_ftrace_swapper(void)
2788{
2789 struct task_struct *p;
2790 int cpu;
2791
2792 get_online_cpus();
2793 for_each_online_cpu(cpu) {
2794 p = idle_task(cpu);
2795 clear_tsk_trace_trace(p);
2796 }
2797 put_online_cpus();
2798}
2799
2800static void set_ftrace_swapper(void)
2801{
2802 struct task_struct *p;
2803 int cpu;
2804
2805 get_online_cpus();
2806 for_each_online_cpu(cpu) {
2807 p = idle_task(cpu);
2808 set_tsk_trace_trace(p);
2809 }
2810 put_online_cpus();
2811}
2812
2813static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002814{
2815 struct task_struct *p;
2816
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002817 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002818 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002819 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002820 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002821 rcu_read_unlock();
2822
Steven Rostedte32d8952008-12-04 00:26:41 -05002823 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002824}
2825
Steven Rostedte32d8952008-12-04 00:26:41 -05002826static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002827{
2828 struct task_struct *p;
2829
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002830 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002831 do_each_pid_task(pid, PIDTYPE_PID, p) {
2832 set_tsk_trace_trace(p);
2833 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002834 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002835}
2836
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002837static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002838{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002839 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002840 clear_ftrace_swapper();
2841 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002842 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002843}
2844
2845static void set_ftrace_pid_task(struct pid *pid)
2846{
2847 if (pid == ftrace_swapper_pid)
2848 set_ftrace_swapper();
2849 else
2850 set_ftrace_pid(pid);
2851}
2852
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002853static int ftrace_pid_add(int p)
2854{
2855 struct pid *pid;
2856 struct ftrace_pid *fpid;
2857 int ret = -EINVAL;
2858
2859 mutex_lock(&ftrace_lock);
2860
2861 if (!p)
2862 pid = ftrace_swapper_pid;
2863 else
2864 pid = find_get_pid(p);
2865
2866 if (!pid)
2867 goto out;
2868
2869 ret = 0;
2870
2871 list_for_each_entry(fpid, &ftrace_pids, list)
2872 if (fpid->pid == pid)
2873 goto out_put;
2874
2875 ret = -ENOMEM;
2876
2877 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2878 if (!fpid)
2879 goto out_put;
2880
2881 list_add(&fpid->list, &ftrace_pids);
2882 fpid->pid = pid;
2883
2884 set_ftrace_pid_task(pid);
2885
2886 ftrace_update_pid_func();
2887 ftrace_startup_enable(0);
2888
2889 mutex_unlock(&ftrace_lock);
2890 return 0;
2891
2892out_put:
2893 if (pid != ftrace_swapper_pid)
2894 put_pid(pid);
2895
2896out:
2897 mutex_unlock(&ftrace_lock);
2898 return ret;
2899}
2900
2901static void ftrace_pid_reset(void)
2902{
2903 struct ftrace_pid *fpid, *safe;
2904
2905 mutex_lock(&ftrace_lock);
2906 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2907 struct pid *pid = fpid->pid;
2908
2909 clear_ftrace_pid_task(pid);
2910
2911 list_del(&fpid->list);
2912 kfree(fpid);
2913 }
2914
2915 ftrace_update_pid_func();
2916 ftrace_startup_enable(0);
2917
2918 mutex_unlock(&ftrace_lock);
2919}
2920
2921static void *fpid_start(struct seq_file *m, loff_t *pos)
2922{
2923 mutex_lock(&ftrace_lock);
2924
2925 if (list_empty(&ftrace_pids) && (!*pos))
2926 return (void *) 1;
2927
2928 return seq_list_start(&ftrace_pids, *pos);
2929}
2930
2931static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2932{
2933 if (v == (void *)1)
2934 return NULL;
2935
2936 return seq_list_next(v, &ftrace_pids, pos);
2937}
2938
2939static void fpid_stop(struct seq_file *m, void *p)
2940{
2941 mutex_unlock(&ftrace_lock);
2942}
2943
2944static int fpid_show(struct seq_file *m, void *v)
2945{
2946 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2947
2948 if (v == (void *)1) {
2949 seq_printf(m, "no pid\n");
2950 return 0;
2951 }
2952
2953 if (fpid->pid == ftrace_swapper_pid)
2954 seq_printf(m, "swapper tasks\n");
2955 else
2956 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2957
2958 return 0;
2959}
2960
2961static const struct seq_operations ftrace_pid_sops = {
2962 .start = fpid_start,
2963 .next = fpid_next,
2964 .stop = fpid_stop,
2965 .show = fpid_show,
2966};
2967
2968static int
2969ftrace_pid_open(struct inode *inode, struct file *file)
2970{
2971 int ret = 0;
2972
2973 if ((file->f_mode & FMODE_WRITE) &&
2974 (file->f_flags & O_TRUNC))
2975 ftrace_pid_reset();
2976
2977 if (file->f_mode & FMODE_READ)
2978 ret = seq_open(file, &ftrace_pid_sops);
2979
2980 return ret;
2981}
2982
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002983static ssize_t
2984ftrace_pid_write(struct file *filp, const char __user *ubuf,
2985 size_t cnt, loff_t *ppos)
2986{
Ingo Molnar457dc922009-11-23 11:03:28 +01002987 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002988 long val;
2989 int ret;
2990
2991 if (cnt >= sizeof(buf))
2992 return -EINVAL;
2993
2994 if (copy_from_user(&buf, ubuf, cnt))
2995 return -EFAULT;
2996
2997 buf[cnt] = 0;
2998
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002999 /*
3000 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3001 * to clean the filter quietly.
3002 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003003 tmp = strstrip(buf);
3004 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003005 return 1;
3006
Ingo Molnar457dc922009-11-23 11:03:28 +01003007 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003008 if (ret < 0)
3009 return ret;
3010
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003011 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003012
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003013 return ret ? ret : cnt;
3014}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003015
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003016static int
3017ftrace_pid_release(struct inode *inode, struct file *file)
3018{
3019 if (file->f_mode & FMODE_READ)
3020 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003021
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003022 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003023}
3024
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003025static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003026 .open = ftrace_pid_open,
3027 .write = ftrace_pid_write,
3028 .read = seq_read,
3029 .llseek = seq_lseek,
3030 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003031};
3032
3033static __init int ftrace_init_debugfs(void)
3034{
3035 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003036
3037 d_tracer = tracing_init_dentry();
3038 if (!d_tracer)
3039 return 0;
3040
3041 ftrace_init_dyn_debugfs(d_tracer);
3042
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003043 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3044 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003045
3046 ftrace_profile_debugfs(d_tracer);
3047
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003048 return 0;
3049}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003050fs_initcall(ftrace_init_debugfs);
3051
Steven Rostedt3d083392008-05-12 21:20:42 +02003052/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003053 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003054 *
3055 * This function should be used by panic code. It stops ftrace
3056 * but in a not so nice way. If you need to simply kill ftrace
3057 * from a non-atomic section, use ftrace_kill.
3058 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003059void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003060{
3061 ftrace_disabled = 1;
3062 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003063 clear_ftrace_function();
3064}
3065
3066/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003067 * register_ftrace_function - register a function for profiling
3068 * @ops - ops structure that holds the function for profiling.
3069 *
3070 * Register a function to be called by all functions in the
3071 * kernel.
3072 *
3073 * Note: @ops->func and all the functions it calls must be labeled
3074 * with "notrace", otherwise it will go into a
3075 * recursive loop.
3076 */
3077int register_ftrace_function(struct ftrace_ops *ops)
3078{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003079 int ret;
3080
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003081 if (unlikely(ftrace_disabled))
3082 return -1;
3083
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003084 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003085
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003086 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003087 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003088
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003089 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003090 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003091}
3092
3093/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003094 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003095 * @ops - ops structure that holds the function to unregister
3096 *
3097 * Unregister a function that was added to be called by ftrace profiling.
3098 */
3099int unregister_ftrace_function(struct ftrace_ops *ops)
3100{
3101 int ret;
3102
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003103 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003104 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003105 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003106 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003107
3108 return ret;
3109}
3110
Ingo Molnare309b412008-05-12 21:20:51 +02003111int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003112ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003113 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003114 loff_t *ppos)
3115{
3116 int ret;
3117
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003118 if (unlikely(ftrace_disabled))
3119 return -ENODEV;
3120
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003121 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003122
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003123 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003124
Li Zefana32c7762009-06-26 16:55:51 +08003125 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003126 goto out;
3127
Li Zefana32c7762009-06-26 16:55:51 +08003128 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003129
3130 if (ftrace_enabled) {
3131
3132 ftrace_startup_sysctl();
3133
3134 /* we are starting ftrace again */
3135 if (ftrace_list != &ftrace_list_end) {
3136 if (ftrace_list->next == &ftrace_list_end)
3137 ftrace_trace_function = ftrace_list->func;
3138 else
3139 ftrace_trace_function = ftrace_list_func;
3140 }
3141
3142 } else {
3143 /* stopping ftrace calls (just send to ftrace_stub) */
3144 ftrace_trace_function = ftrace_stub;
3145
3146 ftrace_shutdown_sysctl();
3147 }
3148
3149 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003150 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003151 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003152}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003153
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003154#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003155
Steven Rostedt597af812009-04-03 15:24:12 -04003156static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003157static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003158
Steven Rostedte49dc192008-12-02 23:50:05 -05003159int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3160{
3161 return 0;
3162}
3163
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003164/* The callbacks that hook a function */
3165trace_func_graph_ret_t ftrace_graph_return =
3166 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003167trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003168
3169/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3170static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3171{
3172 int i;
3173 int ret = 0;
3174 unsigned long flags;
3175 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3176 struct task_struct *g, *t;
3177
3178 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3179 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3180 * sizeof(struct ftrace_ret_stack),
3181 GFP_KERNEL);
3182 if (!ret_stack_list[i]) {
3183 start = 0;
3184 end = i;
3185 ret = -ENOMEM;
3186 goto free;
3187 }
3188 }
3189
3190 read_lock_irqsave(&tasklist_lock, flags);
3191 do_each_thread(g, t) {
3192 if (start == end) {
3193 ret = -EAGAIN;
3194 goto unlock;
3195 }
3196
3197 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003198 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003199 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003200 t->curr_ret_stack = -1;
3201 /* Make sure the tasks see the -1 first: */
3202 smp_wmb();
3203 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003204 }
3205 } while_each_thread(g, t);
3206
3207unlock:
3208 read_unlock_irqrestore(&tasklist_lock, flags);
3209free:
3210 for (i = start; i < end; i++)
3211 kfree(ret_stack_list[i]);
3212 return ret;
3213}
3214
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003215static void
3216ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3217 struct task_struct *next)
3218{
3219 unsigned long long timestamp;
3220 int index;
3221
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003222 /*
3223 * Does the user want to count the time a function was asleep.
3224 * If so, do not update the time stamps.
3225 */
3226 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3227 return;
3228
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003229 timestamp = trace_clock_local();
3230
3231 prev->ftrace_timestamp = timestamp;
3232
3233 /* only process tasks that we timestamped */
3234 if (!next->ftrace_timestamp)
3235 return;
3236
3237 /*
3238 * Update all the counters in next to make up for the
3239 * time next was sleeping.
3240 */
3241 timestamp -= next->ftrace_timestamp;
3242
3243 for (index = next->curr_ret_stack; index >= 0; index--)
3244 next->ret_stack[index].calltime += timestamp;
3245}
3246
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003247/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003248static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003249{
3250 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003251 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003252
3253 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3254 sizeof(struct ftrace_ret_stack *),
3255 GFP_KERNEL);
3256
3257 if (!ret_stack_list)
3258 return -ENOMEM;
3259
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003260 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003261 for_each_online_cpu(cpu) {
3262 if (!idle_task(cpu)->ret_stack)
3263 ftrace_graph_init_task(idle_task(cpu));
3264 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003265
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003266 do {
3267 ret = alloc_retstack_tasklist(ret_stack_list);
3268 } while (ret == -EAGAIN);
3269
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003270 if (!ret) {
3271 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3272 if (ret)
3273 pr_info("ftrace_graph: Couldn't activate tracepoint"
3274 " probe to kernel_sched_switch\n");
3275 }
3276
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003277 kfree(ret_stack_list);
3278 return ret;
3279}
3280
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003281/*
3282 * Hibernation protection.
3283 * The state of the current task is too much unstable during
3284 * suspend/restore to disk. We want to protect against that.
3285 */
3286static int
3287ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3288 void *unused)
3289{
3290 switch (state) {
3291 case PM_HIBERNATION_PREPARE:
3292 pause_graph_tracing();
3293 break;
3294
3295 case PM_POST_HIBERNATION:
3296 unpause_graph_tracing();
3297 break;
3298 }
3299 return NOTIFY_DONE;
3300}
3301
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003302int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3303 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003304{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003305 int ret = 0;
3306
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003307 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003308
Steven Rostedt05ce5812009-03-24 00:18:31 -04003309 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003310 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003311 ret = -EBUSY;
3312 goto out;
3313 }
3314
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003315 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3316 register_pm_notifier(&ftrace_suspend_notifier);
3317
Steven Rostedt597af812009-04-03 15:24:12 -04003318 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003319 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003320 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003321 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003322 goto out;
3323 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003324
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003325 ftrace_graph_return = retfunc;
3326 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003327
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003328 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003329
3330out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003331 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003332 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003333}
3334
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003335void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003336{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003337 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003338
Steven Rostedt597af812009-04-03 15:24:12 -04003339 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003340 goto out;
3341
Steven Rostedt597af812009-04-03 15:24:12 -04003342 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003343 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003344 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003345 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003346 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003347 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003348
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003349 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003350 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003351}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003352
3353/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003354void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003355{
Steven Rostedt84047e32009-06-02 16:51:55 -04003356 /* Make sure we do not use the parent ret_stack */
3357 t->ret_stack = NULL;
3358
Steven Rostedt597af812009-04-03 15:24:12 -04003359 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003360 struct ftrace_ret_stack *ret_stack;
3361
3362 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003363 * sizeof(struct ftrace_ret_stack),
3364 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003365 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003366 return;
3367 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003368 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003369 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003370 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003371 /* make curr_ret_stack visable before we add the ret_stack */
3372 smp_wmb();
3373 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003374 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003375}
3376
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003377void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003378{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003379 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3380
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003381 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003382 /* NULL must become visible to IRQs before we free it: */
3383 barrier();
3384
3385 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003386}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003387
3388void ftrace_graph_stop(void)
3389{
3390 ftrace_stop();
3391}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003392#endif