blob: d0407c9f368cf7af5651b544144a05ba78593243 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Abhishek Sagarf22f9a82008-06-21 23:50:29 +053025#include <linux/kprobes.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010026#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020027#include <linux/sysctl.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.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
Ingo Molnarf2252932008-05-22 10:37:48 +020088static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020089{
90 struct ftrace_ops *op = ftrace_list;
91
92 /* in case someone actually ports this to alpha! */
93 read_barrier_depends();
94
95 while (op != &ftrace_list_end) {
96 /* silly alpha */
97 read_barrier_depends();
98 op->func(ip, parent_ip);
99 op = op->next;
100 };
101}
102
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500103static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
104{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500105 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500106 return;
107
108 ftrace_pid_function(ip, parent_ip);
109}
110
111static void set_ftrace_pid_function(ftrace_func_t func)
112{
113 /* do not set ftrace_pid_function to itself! */
114 if (func != ftrace_pid_func)
115 ftrace_pid_function = func;
116}
117
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200119 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200121 * This NULLs the ftrace function and in essence stops
122 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200124void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125{
Steven Rostedt3d083392008-05-12 21:20:42 +0200126 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500127 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500128 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200129}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500131#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
132/*
133 * For those archs that do not test ftrace_trace_stop in their
134 * mcount call site, we need to do it from C.
135 */
136static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
137{
138 if (function_trace_stop)
139 return;
140
141 __ftrace_trace_function(ip, parent_ip);
142}
143#endif
144
Ingo Molnare309b412008-05-12 21:20:51 +0200145static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200146{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147 ops->next = ftrace_list;
148 /*
149 * We are entering ops into the ftrace_list but another
150 * CPU might be walking that list. We need to make sure
151 * the ops->next pointer is valid before another CPU sees
152 * the ops pointer included into the ftrace_list.
153 */
154 smp_wmb();
155 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200156
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200157 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158 ftrace_func_t func;
159
160 if (ops->next == &ftrace_list_end)
161 func = ops->func;
162 else
163 func = ftrace_list_func;
164
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400165 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500166 set_ftrace_pid_function(func);
167 func = ftrace_pid_func;
168 }
169
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200170 /*
171 * For one func, simply call it directly.
172 * For more than one func, call the chain.
173 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500174#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500175 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500176#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500177 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500178 ftrace_trace_function = ftrace_test_stop_func;
179#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200180 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200181
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200182 return 0;
183}
184
Ingo Molnare309b412008-05-12 21:20:51 +0200185static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200188
189 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200190 * If we are removing the last function, then simply point
191 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192 */
193 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
194 ftrace_trace_function = ftrace_stub;
195 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500196 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197 }
198
199 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
200 if (*p == ops)
201 break;
202
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500203 if (*p != ops)
204 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205
206 *p = (*p)->next;
207
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200208 if (ftrace_enabled) {
209 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500210 if (ftrace_list->next == &ftrace_list_end) {
211 ftrace_func_t func = ftrace_list->func;
212
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400213 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 set_ftrace_pid_function(func);
215 func = ftrace_pid_func;
216 }
217#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
218 ftrace_trace_function = func;
219#else
220 __ftrace_trace_function = func;
221#endif
222 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200223 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200224
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500225 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200226}
227
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500228static void ftrace_update_pid_func(void)
229{
230 ftrace_func_t func;
231
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900233 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500234
Matt Fleming33974092009-09-28 16:43:01 +0100235#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100237#else
238 func = __ftrace_trace_function;
239#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500240
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400241 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500242 set_ftrace_pid_function(func);
243 func = ftrace_pid_func;
244 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800245 if (func == ftrace_pid_func)
246 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500247 }
248
249#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
250 ftrace_trace_function = func;
251#else
252 __ftrace_trace_function = func;
253#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500254}
255
Steven Rostedt493762f2009-03-23 17:12:36 -0400256#ifdef CONFIG_FUNCTION_PROFILER
257struct ftrace_profile {
258 struct hlist_node node;
259 unsigned long ip;
260 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400261#ifdef CONFIG_FUNCTION_GRAPH_TRACER
262 unsigned long long time;
263#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400264};
265
266struct ftrace_profile_page {
267 struct ftrace_profile_page *next;
268 unsigned long index;
269 struct ftrace_profile records[];
270};
271
Steven Rostedtcafb1682009-03-24 20:50:39 -0400272struct ftrace_profile_stat {
273 atomic_t disabled;
274 struct hlist_head *hash;
275 struct ftrace_profile_page *pages;
276 struct ftrace_profile_page *start;
277 struct tracer_stat stat;
278};
279
Steven Rostedt493762f2009-03-23 17:12:36 -0400280#define PROFILE_RECORDS_SIZE \
281 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
282
283#define PROFILES_PER_PAGE \
284 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
285
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400286static int ftrace_profile_bits __read_mostly;
287static int ftrace_profile_enabled __read_mostly;
288
289/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400290static DEFINE_MUTEX(ftrace_profile_lock);
291
Steven Rostedtcafb1682009-03-24 20:50:39 -0400292static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400293
294#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
295
Steven Rostedt493762f2009-03-23 17:12:36 -0400296static void *
297function_stat_next(void *v, int idx)
298{
299 struct ftrace_profile *rec = v;
300 struct ftrace_profile_page *pg;
301
302 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
303
304 again:
Li Zefan0296e422009-06-26 11:15:37 +0800305 if (idx != 0)
306 rec++;
307
Steven Rostedt493762f2009-03-23 17:12:36 -0400308 if ((void *)rec >= (void *)&pg->records[pg->index]) {
309 pg = pg->next;
310 if (!pg)
311 return NULL;
312 rec = &pg->records[0];
313 if (!rec->counter)
314 goto again;
315 }
316
317 return rec;
318}
319
320static void *function_stat_start(struct tracer_stat *trace)
321{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400322 struct ftrace_profile_stat *stat =
323 container_of(trace, struct ftrace_profile_stat, stat);
324
325 if (!stat || !stat->start)
326 return NULL;
327
328 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400329}
330
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400331#ifdef CONFIG_FUNCTION_GRAPH_TRACER
332/* function graph compares on total time */
333static int function_stat_cmp(void *p1, void *p2)
334{
335 struct ftrace_profile *a = p1;
336 struct ftrace_profile *b = p2;
337
338 if (a->time < b->time)
339 return -1;
340 if (a->time > b->time)
341 return 1;
342 else
343 return 0;
344}
345#else
346/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400347static int function_stat_cmp(void *p1, void *p2)
348{
349 struct ftrace_profile *a = p1;
350 struct ftrace_profile *b = p2;
351
352 if (a->counter < b->counter)
353 return -1;
354 if (a->counter > b->counter)
355 return 1;
356 else
357 return 0;
358}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400359#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400360
361static int function_stat_headers(struct seq_file *m)
362{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400363#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400364 seq_printf(m, " Function "
365 "Hit Time Avg\n"
366 " -------- "
367 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400369 seq_printf(m, " Function Hit\n"
370 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400371#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400372 return 0;
373}
374
375static int function_stat_show(struct seq_file *m, void *v)
376{
377 struct ftrace_profile *rec = v;
378 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400379#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400380 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400381 static struct trace_seq s;
382 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400383#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400384
385 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400386 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400387
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400388#ifdef CONFIG_FUNCTION_GRAPH_TRACER
389 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400390 avg = rec->time;
391 do_div(avg, rec->counter);
392
393 mutex_lock(&mutex);
394 trace_seq_init(&s);
395 trace_print_graph_duration(rec->time, &s);
396 trace_seq_puts(&s, " ");
397 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400398 trace_print_seq(m, &s);
399 mutex_unlock(&mutex);
400#endif
401 seq_putc(m, '\n');
402
Steven Rostedt493762f2009-03-23 17:12:36 -0400403 return 0;
404}
405
Steven Rostedtcafb1682009-03-24 20:50:39 -0400406static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400407{
408 struct ftrace_profile_page *pg;
409
Steven Rostedtcafb1682009-03-24 20:50:39 -0400410 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400411
412 while (pg) {
413 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
414 pg->index = 0;
415 pg = pg->next;
416 }
417
Steven Rostedtcafb1682009-03-24 20:50:39 -0400418 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400419 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
420}
421
Steven Rostedtcafb1682009-03-24 20:50:39 -0400422int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400423{
424 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400425 int functions;
426 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400427 int i;
428
429 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400430 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400431 return 0;
432
Steven Rostedtcafb1682009-03-24 20:50:39 -0400433 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
434 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400435 return -ENOMEM;
436
Steven Rostedt318e0a72009-03-25 20:06:34 -0400437#ifdef CONFIG_DYNAMIC_FTRACE
438 functions = ftrace_update_tot_cnt;
439#else
440 /*
441 * We do not know the number of functions that exist because
442 * dynamic tracing is what counts them. With past experience
443 * we have around 20K functions. That should be more than enough.
444 * It is highly unlikely we will execute every function in
445 * the kernel.
446 */
447 functions = 20000;
448#endif
449
Steven Rostedtcafb1682009-03-24 20:50:39 -0400450 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400451
Steven Rostedt318e0a72009-03-25 20:06:34 -0400452 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
453
454 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400455 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400456 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400457 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400458 pg = pg->next;
459 }
460
461 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400462
463 out_free:
464 pg = stat->start;
465 while (pg) {
466 unsigned long tmp = (unsigned long)pg;
467
468 pg = pg->next;
469 free_page(tmp);
470 }
471
472 free_page((unsigned long)stat->pages);
473 stat->pages = NULL;
474 stat->start = NULL;
475
476 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400477}
478
Steven Rostedtcafb1682009-03-24 20:50:39 -0400479static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400480{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400481 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400482 int size;
483
Steven Rostedtcafb1682009-03-24 20:50:39 -0400484 stat = &per_cpu(ftrace_profile_stats, cpu);
485
486 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400487 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400488 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400489 return 0;
490 }
491
492 /*
493 * We are profiling all functions, but usually only a few thousand
494 * functions are hit. We'll make a hash of 1024 items.
495 */
496 size = FTRACE_PROFILE_HASH_SIZE;
497
Steven Rostedtcafb1682009-03-24 20:50:39 -0400498 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400499
Steven Rostedtcafb1682009-03-24 20:50:39 -0400500 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400501 return -ENOMEM;
502
Steven Rostedtcafb1682009-03-24 20:50:39 -0400503 if (!ftrace_profile_bits) {
504 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400505
Steven Rostedtcafb1682009-03-24 20:50:39 -0400506 for (; size; size >>= 1)
507 ftrace_profile_bits++;
508 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400509
Steven Rostedt318e0a72009-03-25 20:06:34 -0400510 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400511 if (ftrace_profile_pages_init(stat) < 0) {
512 kfree(stat->hash);
513 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400514 return -ENOMEM;
515 }
516
517 return 0;
518}
519
Steven Rostedtcafb1682009-03-24 20:50:39 -0400520static int ftrace_profile_init(void)
521{
522 int cpu;
523 int ret = 0;
524
525 for_each_online_cpu(cpu) {
526 ret = ftrace_profile_init_cpu(cpu);
527 if (ret)
528 break;
529 }
530
531 return ret;
532}
533
Steven Rostedt493762f2009-03-23 17:12:36 -0400534/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400535static struct ftrace_profile *
536ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400537{
538 struct ftrace_profile *rec;
539 struct hlist_head *hhd;
540 struct hlist_node *n;
541 unsigned long key;
542
543 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400544 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400545
546 if (hlist_empty(hhd))
547 return NULL;
548
549 hlist_for_each_entry_rcu(rec, n, hhd, node) {
550 if (rec->ip == ip)
551 return rec;
552 }
553
554 return NULL;
555}
556
Steven Rostedtcafb1682009-03-24 20:50:39 -0400557static void ftrace_add_profile(struct ftrace_profile_stat *stat,
558 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400559{
560 unsigned long key;
561
562 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400563 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400564}
565
Steven Rostedt318e0a72009-03-25 20:06:34 -0400566/*
567 * The memory is already allocated, this simply finds a new record to use.
568 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400569static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400570ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400571{
572 struct ftrace_profile *rec = NULL;
573
Steven Rostedt318e0a72009-03-25 20:06:34 -0400574 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400575 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400576 goto out;
577
Steven Rostedt493762f2009-03-23 17:12:36 -0400578 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400579 * Try to find the function again since an NMI
580 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400581 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400582 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400583 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400585
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 if (stat->pages->index == PROFILES_PER_PAGE) {
587 if (!stat->pages->next)
588 goto out;
589 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400590 }
591
Steven Rostedtcafb1682009-03-24 20:50:39 -0400592 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400593 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400595
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400598
599 return rec;
600}
601
Steven Rostedt493762f2009-03-23 17:12:36 -0400602static void
603function_profile_call(unsigned long ip, unsigned long parent_ip)
604{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400606 struct ftrace_profile *rec;
607 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400608
609 if (!ftrace_profile_enabled)
610 return;
611
Steven Rostedt493762f2009-03-23 17:12:36 -0400612 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400613
614 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400615 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400616 goto out;
617
618 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400620 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400621 if (!rec)
622 goto out;
623 }
624
625 rec->counter++;
626 out:
627 local_irq_restore(flags);
628}
629
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400630#ifdef CONFIG_FUNCTION_GRAPH_TRACER
631static int profile_graph_entry(struct ftrace_graph_ent *trace)
632{
633 function_profile_call(trace->func, 0);
634 return 1;
635}
636
637static void profile_graph_return(struct ftrace_graph_ret *trace)
638{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400639 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400640 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400641 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400643
644 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400646 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 goto out;
648
Steven Rostedta2a16d62009-03-24 23:17:58 -0400649 calltime = trace->rettime - trace->calltime;
650
651 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
652 int index;
653
654 index = trace->depth;
655
656 /* Append this call time to the parent time to subtract */
657 if (index)
658 current->ret_stack[index - 1].subtime += calltime;
659
660 if (current->ret_stack[index].subtime < calltime)
661 calltime -= current->ret_stack[index].subtime;
662 else
663 calltime = 0;
664 }
665
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400667 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400668 rec->time += calltime;
669
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400671 local_irq_restore(flags);
672}
673
674static int register_ftrace_profiler(void)
675{
676 return register_ftrace_graph(&profile_graph_return,
677 &profile_graph_entry);
678}
679
680static void unregister_ftrace_profiler(void)
681{
682 unregister_ftrace_graph();
683}
684#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400685static struct ftrace_ops ftrace_profile_ops __read_mostly =
686{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400687 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400688};
689
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400690static int register_ftrace_profiler(void)
691{
692 return register_ftrace_function(&ftrace_profile_ops);
693}
694
695static void unregister_ftrace_profiler(void)
696{
697 unregister_ftrace_function(&ftrace_profile_ops);
698}
699#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
700
Steven Rostedt493762f2009-03-23 17:12:36 -0400701static ssize_t
702ftrace_profile_write(struct file *filp, const char __user *ubuf,
703 size_t cnt, loff_t *ppos)
704{
705 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400706 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 int ret;
708
709 if (cnt >= sizeof(buf))
710 return -EINVAL;
711
712 if (copy_from_user(&buf, ubuf, cnt))
713 return -EFAULT;
714
715 buf[cnt] = 0;
716
717 ret = strict_strtoul(buf, 10, &val);
718 if (ret < 0)
719 return ret;
720
721 val = !!val;
722
723 mutex_lock(&ftrace_profile_lock);
724 if (ftrace_profile_enabled ^ val) {
725 if (val) {
726 ret = ftrace_profile_init();
727 if (ret < 0) {
728 cnt = ret;
729 goto out;
730 }
731
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400732 ret = register_ftrace_profiler();
733 if (ret < 0) {
734 cnt = ret;
735 goto out;
736 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 ftrace_profile_enabled = 1;
738 } else {
739 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400740 /*
741 * unregister_ftrace_profiler calls stop_machine
742 * so this acts like an synchronize_sched.
743 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400744 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400745 }
746 }
747 out:
748 mutex_unlock(&ftrace_profile_lock);
749
Jiri Olsacf8517c2009-10-23 19:36:16 -0400750 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400751
752 return cnt;
753}
754
755static ssize_t
756ftrace_profile_read(struct file *filp, char __user *ubuf,
757 size_t cnt, loff_t *ppos)
758{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400759 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400760 int r;
761
762 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
763 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
764}
765
766static const struct file_operations ftrace_profile_fops = {
767 .open = tracing_open_generic,
768 .read = ftrace_profile_read,
769 .write = ftrace_profile_write,
770};
771
Steven Rostedtcafb1682009-03-24 20:50:39 -0400772/* used to initialize the real stat files */
773static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400774 .name = "functions",
775 .stat_start = function_stat_start,
776 .stat_next = function_stat_next,
777 .stat_cmp = function_stat_cmp,
778 .stat_headers = function_stat_headers,
779 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780};
781
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400782static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400783{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400787 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400789
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 for_each_possible_cpu(cpu) {
791 stat = &per_cpu(ftrace_profile_stats, cpu);
792
793 /* allocate enough for function name + cpu number */
794 name = kmalloc(32, GFP_KERNEL);
795 if (!name) {
796 /*
797 * The files created are permanent, if something happens
798 * we still do not free memory.
799 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 WARN(1,
801 "Could not allocate stat file for cpu %d\n",
802 cpu);
803 return;
804 }
805 stat->stat = function_stats;
806 snprintf(name, 32, "function%d", cpu);
807 stat->stat.name = name;
808 ret = register_stat_tracer(&stat->stat);
809 if (ret) {
810 WARN(1,
811 "Could not register function stat for cpu %d\n",
812 cpu);
813 kfree(name);
814 return;
815 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 }
817
818 entry = debugfs_create_file("function_profile_enabled", 0644,
819 d_tracer, NULL, &ftrace_profile_fops);
820 if (!entry)
821 pr_warning("Could not create debugfs "
822 "'function_profile_enabled' entry\n");
823}
824
825#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400826static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400827{
828}
829#endif /* CONFIG_FUNCTION_PROFILER */
830
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100831static struct pid * const ftrace_swapper_pid = &init_struct_pid;
832
Steven Rostedt3d083392008-05-12 21:20:42 +0200833#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100834
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400835#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400836# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400837#endif
838
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500839static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
840
Steven Rostedtb6887d72009-02-17 12:32:04 -0500841struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500842 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500843 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500844 unsigned long flags;
845 unsigned long ip;
846 void *data;
847 struct rcu_head rcu;
848};
849
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200850enum {
851 FTRACE_ENABLE_CALLS = (1 << 0),
852 FTRACE_DISABLE_CALLS = (1 << 1),
853 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
854 FTRACE_ENABLE_MCOUNT = (1 << 3),
855 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500856 FTRACE_START_FUNC_RET = (1 << 5),
857 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200858};
859
Steven Rostedt5072c592008-05-12 21:20:43 +0200860static int ftrace_filtered;
861
Lai Jiangshane94142a2009-03-13 17:51:27 +0800862static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200863
Steven Rostedt41c52c02008-05-22 11:46:33 -0400864static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200865
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200866struct ftrace_page {
867 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500868 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200869 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700870};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200871
872#define ENTRIES_PER_PAGE \
873 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
874
875/* estimate from running different kernels */
876#define NR_TO_INIT 10000
877
878static struct ftrace_page *ftrace_pages_start;
879static struct ftrace_page *ftrace_pages;
880
Steven Rostedt37ad5082008-05-12 21:20:48 +0200881static struct dyn_ftrace *ftrace_free_records;
882
Steven Rostedt265c8312009-02-13 12:43:56 -0500883/*
884 * This is a double for. Do not use 'break' to break out of the loop,
885 * you must use a goto.
886 */
887#define do_for_each_ftrace_rec(pg, rec) \
888 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
889 int _____i; \
890 for (_____i = 0; _____i < pg->index; _____i++) { \
891 rec = &pg->records[_____i];
892
893#define while_for_each_ftrace_rec() \
894 } \
895 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530896
897#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200898
899static int frozen_record_count;
900
Abhishek Sagarecea6562008-06-21 23:47:53 +0530901static inline void freeze_record(struct dyn_ftrace *rec)
902{
903 if (!(rec->flags & FTRACE_FL_FROZEN)) {
904 rec->flags |= FTRACE_FL_FROZEN;
905 frozen_record_count++;
906 }
907}
908
909static inline void unfreeze_record(struct dyn_ftrace *rec)
910{
911 if (rec->flags & FTRACE_FL_FROZEN) {
912 rec->flags &= ~FTRACE_FL_FROZEN;
913 frozen_record_count--;
914 }
915}
916
917static inline int record_frozen(struct dyn_ftrace *rec)
918{
919 return rec->flags & FTRACE_FL_FROZEN;
920}
921#else
922# define freeze_record(rec) ({ 0; })
923# define unfreeze_record(rec) ({ 0; })
924# define record_frozen(rec) ({ 0; })
925#endif /* CONFIG_KPROBES */
926
Ingo Molnare309b412008-05-12 21:20:51 +0200927static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200928{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800929 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200930 ftrace_free_records = rec;
931 rec->flags |= FTRACE_FL_FREE;
932}
933
Ingo Molnare309b412008-05-12 21:20:51 +0200934static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200935{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200936 struct dyn_ftrace *rec;
937
938 /* First check for freed records */
939 if (ftrace_free_records) {
940 rec = ftrace_free_records;
941
Steven Rostedt37ad5082008-05-12 21:20:48 +0200942 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400943 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200944 ftrace_free_records = NULL;
945 return NULL;
946 }
947
Lai Jiangshanee000b72009-03-24 13:38:06 +0800948 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200949 memset(rec, 0, sizeof(*rec));
950 return rec;
951 }
952
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200953 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400954 if (!ftrace_pages->next) {
955 /* allocate another page */
956 ftrace_pages->next =
957 (void *)get_zeroed_page(GFP_KERNEL);
958 if (!ftrace_pages->next)
959 return NULL;
960 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200961 ftrace_pages = ftrace_pages->next;
962 }
963
964 return &ftrace_pages->records[ftrace_pages->index++];
965}
966
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400967static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200968ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200969{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400970 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200971
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800972 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400973 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200974
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400975 rec = ftrace_alloc_dyn_node(ip);
976 if (!rec)
977 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200978
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400979 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800980 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800981 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200982
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400983 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200984}
985
Steven Rostedt05736a42008-09-22 14:55:47 -0700986static void print_ip_ins(const char *fmt, unsigned char *p)
987{
988 int i;
989
990 printk(KERN_CONT "%s", fmt);
991
992 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
993 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
994}
995
Steven Rostedt31e88902008-11-14 16:21:19 -0800996static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800997{
998 switch (failed) {
999 case -EFAULT:
1000 FTRACE_WARN_ON_ONCE(1);
1001 pr_info("ftrace faulted on modifying ");
1002 print_ip_sym(ip);
1003 break;
1004 case -EINVAL:
1005 FTRACE_WARN_ON_ONCE(1);
1006 pr_info("ftrace failed to modify ");
1007 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001008 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001009 printk(KERN_CONT "\n");
1010 break;
1011 case -EPERM:
1012 FTRACE_WARN_ON_ONCE(1);
1013 pr_info("ftrace faulted on writing ");
1014 print_ip_sym(ip);
1015 break;
1016 default:
1017 FTRACE_WARN_ON_ONCE(1);
1018 pr_info("ftrace faulted on unknown error ");
1019 print_ip_sym(ip);
1020 }
1021}
1022
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001023
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301024static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001025__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001026{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001027 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001028 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001029
Shaohua Lif0001202009-01-09 11:29:42 +08001030 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001031
Steven Rostedt982c3502008-11-15 16:31:41 -05001032 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001033 * If this record is not to be traced or we want to disable it,
1034 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001035 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001036 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001037 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001038 * If we want to enable it and filtering is on, enable it only if
1039 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001040 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001041 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1042 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1043 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001044 }
1045
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001046 /* If the state of this record hasn't changed, then do nothing */
1047 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1048 return 0;
1049
1050 if (flag) {
1051 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001052 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001053 }
1054
1055 rec->flags &= ~FTRACE_FL_ENABLED;
1056 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001057}
1058
1059static void ftrace_replace_code(int enable)
1060{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001061 struct dyn_ftrace *rec;
1062 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001063 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001064
Steven Rostedt265c8312009-02-13 12:43:56 -05001065 do_for_each_ftrace_rec(pg, rec) {
1066 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001067 * Skip over free records, records that have
1068 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001069 */
1070 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001071 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001072 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001073 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001074
Steven Rostedt265c8312009-02-13 12:43:56 -05001075 /* ignore updates to this record's mcount site */
1076 if (get_kprobe((void *)rec->ip)) {
1077 freeze_record(rec);
1078 continue;
1079 } else {
1080 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +02001081 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001082
1083 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001084 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001085 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001086 ftrace_bug(failed, rec->ip);
1087 /* Stop processing */
1088 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001089 }
1090 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001091}
1092
Ingo Molnare309b412008-05-12 21:20:51 +02001093static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001094ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095{
1096 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001097 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098
1099 ip = rec->ip;
1100
Shaohua Li25aac9d2009-01-09 11:29:40 +08001101 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001102 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001103 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001104 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301105 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001106 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301107 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001108}
1109
Steven Rostedt000ab692009-02-17 13:35:06 -05001110/*
1111 * archs can override this function if they must do something
1112 * before the modifying code is performed.
1113 */
1114int __weak ftrace_arch_code_modify_prepare(void)
1115{
1116 return 0;
1117}
1118
1119/*
1120 * archs can override this function if they must do something
1121 * after the modifying code is performed.
1122 */
1123int __weak ftrace_arch_code_modify_post_process(void)
1124{
1125 return 0;
1126}
1127
Ingo Molnare309b412008-05-12 21:20:51 +02001128static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001129{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001130 int *command = data;
1131
Steven Rostedta3583242008-11-11 15:01:42 -05001132 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001133 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001134 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001135 ftrace_replace_code(0);
1136
1137 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1138 ftrace_update_ftrace_func(ftrace_trace_function);
1139
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001140 if (*command & FTRACE_START_FUNC_RET)
1141 ftrace_enable_ftrace_graph_caller();
1142 else if (*command & FTRACE_STOP_FUNC_RET)
1143 ftrace_disable_ftrace_graph_caller();
1144
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001145 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001146}
1147
Ingo Molnare309b412008-05-12 21:20:51 +02001148static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001149{
Steven Rostedt000ab692009-02-17 13:35:06 -05001150 int ret;
1151
1152 ret = ftrace_arch_code_modify_prepare();
1153 FTRACE_WARN_ON(ret);
1154 if (ret)
1155 return;
1156
Rusty Russell784e2d72008-07-28 12:16:31 -05001157 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001158
1159 ret = ftrace_arch_code_modify_post_process();
1160 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001161}
1162
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001163static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001164static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001165
1166static void ftrace_startup_enable(int command)
1167{
1168 if (saved_ftrace_func != ftrace_trace_function) {
1169 saved_ftrace_func = ftrace_trace_function;
1170 command |= FTRACE_UPDATE_TRACE_FUNC;
1171 }
1172
1173 if (!command || !ftrace_enabled)
1174 return;
1175
1176 ftrace_run_update_code(command);
1177}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001178
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001179static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001180{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001181 if (unlikely(ftrace_disabled))
1182 return;
1183
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001184 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001185 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001186
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001187 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001188}
1189
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001190static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001191{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001192 if (unlikely(ftrace_disabled))
1193 return;
1194
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001195 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001196 /*
1197 * Just warn in case of unbalance, no need to kill ftrace, it's not
1198 * critical but the ftrace_call callers may be never nopped again after
1199 * further ftrace uses.
1200 */
1201 WARN_ON_ONCE(ftrace_start_up < 0);
1202
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001203 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001204 command |= FTRACE_DISABLE_CALLS;
1205
1206 if (saved_ftrace_func != ftrace_trace_function) {
1207 saved_ftrace_func = ftrace_trace_function;
1208 command |= FTRACE_UPDATE_TRACE_FUNC;
1209 }
1210
1211 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001212 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001213
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001214 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001215}
1216
Ingo Molnare309b412008-05-12 21:20:51 +02001217static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001218{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001219 int command = FTRACE_ENABLE_MCOUNT;
1220
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001221 if (unlikely(ftrace_disabled))
1222 return;
1223
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001224 /* Force update next time */
1225 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001226 /* ftrace_start_up is true if we want ftrace running */
1227 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001228 command |= FTRACE_ENABLE_CALLS;
1229
1230 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001231}
1232
Ingo Molnare309b412008-05-12 21:20:51 +02001233static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001234{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001235 int command = FTRACE_DISABLE_MCOUNT;
1236
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001237 if (unlikely(ftrace_disabled))
1238 return;
1239
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001240 /* ftrace_start_up is true if ftrace is running */
1241 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001242 command |= FTRACE_DISABLE_CALLS;
1243
1244 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001245}
1246
Steven Rostedt3d083392008-05-12 21:20:42 +02001247static cycle_t ftrace_update_time;
1248static unsigned long ftrace_update_cnt;
1249unsigned long ftrace_update_tot_cnt;
1250
Steven Rostedt31e88902008-11-14 16:21:19 -08001251static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001252{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001253 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301254 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001255
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001256 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001257 ftrace_update_cnt = 0;
1258
Lai Jiangshane94142a2009-03-13 17:51:27 +08001259 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301260
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001261 /* If something went wrong, bail without enabling anything */
1262 if (unlikely(ftrace_disabled))
1263 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001264
Lai Jiangshane94142a2009-03-13 17:51:27 +08001265 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001266 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001267 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301268
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001269 /*
1270 * Do the initial record convertion from mcount jump
1271 * to the NOP instructions.
1272 */
1273 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001274 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001275 continue;
1276 }
1277
1278 p->flags |= FTRACE_FL_CONVERTED;
1279 ftrace_update_cnt++;
1280
1281 /*
1282 * If the tracing is enabled, go ahead and enable the record.
1283 *
1284 * The reason not to enable the record immediatelly is the
1285 * inherent check of ftrace_make_nop/ftrace_make_call for
1286 * correct previous instructions. Making first the NOP
1287 * conversion puts the module to the correct state, thus
1288 * passing the ftrace_make_call check.
1289 */
1290 if (ftrace_start_up) {
1291 int failed = __ftrace_replace_code(p, 1);
1292 if (failed) {
1293 ftrace_bug(failed, p->ip);
1294 ftrace_free_rec(p);
1295 }
1296 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001297 }
1298
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001299 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001300 ftrace_update_time = stop - start;
1301 ftrace_update_tot_cnt += ftrace_update_cnt;
1302
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001303 return 0;
1304}
1305
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001306static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001307{
1308 struct ftrace_page *pg;
1309 int cnt;
1310 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001311
1312 /* allocate a few pages */
1313 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1314 if (!ftrace_pages_start)
1315 return -1;
1316
1317 /*
1318 * Allocate a few more pages.
1319 *
1320 * TODO: have some parser search vmlinux before
1321 * final linking to find all calls to ftrace.
1322 * Then we can:
1323 * a) know how many pages to allocate.
1324 * and/or
1325 * b) set up the table then.
1326 *
1327 * The dynamic code is still necessary for
1328 * modules.
1329 */
1330
1331 pg = ftrace_pages = ftrace_pages_start;
1332
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001333 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001334 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001335 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001336
1337 for (i = 0; i < cnt; i++) {
1338 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1339
1340 /* If we fail, we'll try later anyway */
1341 if (!pg->next)
1342 break;
1343
1344 pg = pg->next;
1345 }
1346
1347 return 0;
1348}
1349
Steven Rostedt5072c592008-05-12 21:20:43 +02001350enum {
1351 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001352 FTRACE_ITER_NOTRACE = (1 << 1),
1353 FTRACE_ITER_FAILURES = (1 << 2),
1354 FTRACE_ITER_PRINTALL = (1 << 3),
1355 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001356};
1357
1358#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1359
1360struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001361 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001362 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001363 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001364 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001365 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001366};
1367
Ingo Molnare309b412008-05-12 21:20:51 +02001368static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001369t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1370{
1371 struct ftrace_iterator *iter = m->private;
1372 struct hlist_node *hnd = v;
1373 struct hlist_head *hhd;
1374
1375 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1376
1377 (*pos)++;
1378
1379 retry:
1380 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1381 return NULL;
1382
1383 hhd = &ftrace_func_hash[iter->hidx];
1384
1385 if (hlist_empty(hhd)) {
1386 iter->hidx++;
1387 hnd = NULL;
1388 goto retry;
1389 }
1390
1391 if (!hnd)
1392 hnd = hhd->first;
1393 else {
1394 hnd = hnd->next;
1395 if (!hnd) {
1396 iter->hidx++;
1397 goto retry;
1398 }
1399 }
1400
1401 return hnd;
1402}
1403
1404static void *t_hash_start(struct seq_file *m, loff_t *pos)
1405{
1406 struct ftrace_iterator *iter = m->private;
1407 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001408 loff_t l;
1409
1410 if (!(iter->flags & FTRACE_ITER_HASH))
1411 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001412
1413 iter->flags |= FTRACE_ITER_HASH;
1414
Li Zefand82d6242009-06-24 09:54:54 +08001415 iter->hidx = 0;
1416 for (l = 0; l <= *pos; ) {
1417 p = t_hash_next(m, p, &l);
1418 if (!p)
1419 break;
1420 }
1421 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001422}
1423
1424static int t_hash_show(struct seq_file *m, void *v)
1425{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001426 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001427 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001428
Steven Rostedtb6887d72009-02-17 12:32:04 -05001429 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001430
Steven Rostedt809dcf22009-02-16 23:06:01 -05001431 if (rec->ops->print)
1432 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1433
Steven Rostedtb375a112009-09-17 00:05:58 -04001434 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001435
1436 if (rec->data)
1437 seq_printf(m, ":%p", rec->data);
1438 seq_putc(m, '\n');
1439
1440 return 0;
1441}
1442
1443static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001444t_next(struct seq_file *m, void *v, loff_t *pos)
1445{
1446 struct ftrace_iterator *iter = m->private;
1447 struct dyn_ftrace *rec = NULL;
1448
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001449 if (iter->flags & FTRACE_ITER_HASH)
1450 return t_hash_next(m, v, pos);
1451
Steven Rostedt5072c592008-05-12 21:20:43 +02001452 (*pos)++;
1453
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001454 if (iter->flags & FTRACE_ITER_PRINTALL)
1455 return NULL;
1456
Steven Rostedt5072c592008-05-12 21:20:43 +02001457 retry:
1458 if (iter->idx >= iter->pg->index) {
1459 if (iter->pg->next) {
1460 iter->pg = iter->pg->next;
1461 iter->idx = 0;
1462 goto retry;
1463 }
1464 } else {
1465 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001466 if ((rec->flags & FTRACE_FL_FREE) ||
1467
1468 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301469 (rec->flags & FTRACE_FL_FAILED)) ||
1470
1471 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001472 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301473
Steven Rostedt0183fb12008-11-07 22:36:02 -05001474 ((iter->flags & FTRACE_ITER_FILTER) &&
1475 !(rec->flags & FTRACE_FL_FILTER)) ||
1476
Steven Rostedt41c52c02008-05-22 11:46:33 -04001477 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1478 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001479 rec = NULL;
1480 goto retry;
1481 }
1482 }
1483
Steven Rostedt5072c592008-05-12 21:20:43 +02001484 return rec;
1485}
1486
1487static void *t_start(struct seq_file *m, loff_t *pos)
1488{
1489 struct ftrace_iterator *iter = m->private;
1490 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001491 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001492
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001493 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001494 /*
1495 * For set_ftrace_filter reading, if we have the filter
1496 * off, we can short cut and just print out that all
1497 * functions are enabled.
1498 */
1499 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1500 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001501 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001502 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001503 return iter;
1504 }
1505
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001506 if (iter->flags & FTRACE_ITER_HASH)
1507 return t_hash_start(m, pos);
1508
Li Zefan694ce0a2009-06-24 09:54:19 +08001509 iter->pg = ftrace_pages_start;
1510 iter->idx = 0;
1511 for (l = 0; l <= *pos; ) {
1512 p = t_next(m, p, &l);
1513 if (!p)
1514 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001515 }
walimis5821e1b2008-11-15 15:19:06 +08001516
Li Zefan694ce0a2009-06-24 09:54:19 +08001517 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001518 return t_hash_start(m, pos);
1519
Steven Rostedt5072c592008-05-12 21:20:43 +02001520 return p;
1521}
1522
1523static void t_stop(struct seq_file *m, void *p)
1524{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001525 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001526}
1527
1528static int t_show(struct seq_file *m, void *v)
1529{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001530 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001531 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001532
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001533 if (iter->flags & FTRACE_ITER_HASH)
1534 return t_hash_show(m, v);
1535
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001536 if (iter->flags & FTRACE_ITER_PRINTALL) {
1537 seq_printf(m, "#### all functions enabled ####\n");
1538 return 0;
1539 }
1540
Steven Rostedt5072c592008-05-12 21:20:43 +02001541 if (!rec)
1542 return 0;
1543
Steven Rostedtb375a112009-09-17 00:05:58 -04001544 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001545
1546 return 0;
1547}
1548
James Morris88e9d342009-09-22 16:43:43 -07001549static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001550 .start = t_start,
1551 .next = t_next,
1552 .stop = t_stop,
1553 .show = t_show,
1554};
1555
Ingo Molnare309b412008-05-12 21:20:51 +02001556static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001557ftrace_avail_open(struct inode *inode, struct file *file)
1558{
1559 struct ftrace_iterator *iter;
1560 int ret;
1561
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001562 if (unlikely(ftrace_disabled))
1563 return -ENODEV;
1564
Steven Rostedt5072c592008-05-12 21:20:43 +02001565 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1566 if (!iter)
1567 return -ENOMEM;
1568
1569 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001570
1571 ret = seq_open(file, &show_ftrace_seq_ops);
1572 if (!ret) {
1573 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001574
Steven Rostedt5072c592008-05-12 21:20:43 +02001575 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001576 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001577 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001578 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001579
1580 return ret;
1581}
1582
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301583static int
1584ftrace_failures_open(struct inode *inode, struct file *file)
1585{
1586 int ret;
1587 struct seq_file *m;
1588 struct ftrace_iterator *iter;
1589
1590 ret = ftrace_avail_open(inode, file);
1591 if (!ret) {
1592 m = (struct seq_file *)file->private_data;
1593 iter = (struct ftrace_iterator *)m->private;
1594 iter->flags = FTRACE_ITER_FAILURES;
1595 }
1596
1597 return ret;
1598}
1599
1600
Steven Rostedt41c52c02008-05-22 11:46:33 -04001601static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001602{
1603 struct ftrace_page *pg;
1604 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001605 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001606
Steven Rostedt52baf112009-02-14 01:15:39 -05001607 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001608 if (enable)
1609 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001610 do_for_each_ftrace_rec(pg, rec) {
1611 if (rec->flags & FTRACE_FL_FAILED)
1612 continue;
1613 rec->flags &= ~type;
1614 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001615 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001616}
1617
Ingo Molnare309b412008-05-12 21:20:51 +02001618static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001619ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001620{
1621 struct ftrace_iterator *iter;
1622 int ret = 0;
1623
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001624 if (unlikely(ftrace_disabled))
1625 return -ENODEV;
1626
Steven Rostedt5072c592008-05-12 21:20:43 +02001627 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1628 if (!iter)
1629 return -ENOMEM;
1630
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001631 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1632 kfree(iter);
1633 return -ENOMEM;
1634 }
1635
Steven Rostedt41c52c02008-05-22 11:46:33 -04001636 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001637 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001638 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001639 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001640
1641 if (file->f_mode & FMODE_READ) {
1642 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001643 iter->flags = enable ? FTRACE_ITER_FILTER :
1644 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001645
1646 ret = seq_open(file, &show_ftrace_seq_ops);
1647 if (!ret) {
1648 struct seq_file *m = file->private_data;
1649 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001650 } else {
1651 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001652 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001653 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001654 } else
1655 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001656 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001657
1658 return ret;
1659}
1660
Steven Rostedt41c52c02008-05-22 11:46:33 -04001661static int
1662ftrace_filter_open(struct inode *inode, struct file *file)
1663{
1664 return ftrace_regex_open(inode, file, 1);
1665}
1666
1667static int
1668ftrace_notrace_open(struct inode *inode, struct file *file)
1669{
1670 return ftrace_regex_open(inode, file, 0);
1671}
1672
Ingo Molnare309b412008-05-12 21:20:51 +02001673static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001674ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001675{
1676 loff_t ret;
1677
1678 if (file->f_mode & FMODE_READ)
1679 ret = seq_lseek(file, offset, origin);
1680 else
1681 file->f_pos = ret = 1;
1682
1683 return ret;
1684}
1685
Steven Rostedt64e7c442009-02-13 17:08:48 -05001686static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001687{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001688 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08001689 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001690
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001691 switch (type) {
1692 case MATCH_FULL:
1693 if (strcmp(str, regex) == 0)
1694 matched = 1;
1695 break;
1696 case MATCH_FRONT_ONLY:
1697 if (strncmp(str, regex, len) == 0)
1698 matched = 1;
1699 break;
1700 case MATCH_MIDDLE_ONLY:
1701 if (strstr(str, regex))
1702 matched = 1;
1703 break;
1704 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08001705 slen = strlen(str);
1706 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001707 matched = 1;
1708 break;
1709 }
1710
1711 return matched;
1712}
1713
Steven Rostedt64e7c442009-02-13 17:08:48 -05001714static int
1715ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1716{
1717 char str[KSYM_SYMBOL_LEN];
1718
1719 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1720 return ftrace_match(str, regex, len, type);
1721}
1722
Li Zefan311d16d2009-12-08 11:15:11 +08001723static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001724{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001725 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001726 struct ftrace_page *pg;
1727 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001728 unsigned long flag;
1729 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001730 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001731 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001732 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001733
Steven Rostedt6a24a242009-02-17 11:20:26 -05001734 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001735 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001736
1737 search_len = strlen(search);
1738
Steven Rostedt52baf112009-02-14 01:15:39 -05001739 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001740 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001741
Steven Rostedt265c8312009-02-13 12:43:56 -05001742 if (rec->flags & FTRACE_FL_FAILED)
1743 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001744
1745 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001746 if (not)
1747 rec->flags &= ~flag;
1748 else
1749 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001750 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001751 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001752 /*
1753 * Only enable filtering if we have a function that
1754 * is filtered on.
1755 */
1756 if (enable && (rec->flags & FTRACE_FL_FILTER))
1757 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001758 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001759 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001760
1761 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001762}
1763
Steven Rostedt64e7c442009-02-13 17:08:48 -05001764static int
1765ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1766 char *regex, int len, int type)
1767{
1768 char str[KSYM_SYMBOL_LEN];
1769 char *modname;
1770
1771 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1772
1773 if (!modname || strcmp(modname, mod))
1774 return 0;
1775
1776 /* blank search means to match all funcs in the mod */
1777 if (len)
1778 return ftrace_match(str, regex, len, type);
1779 else
1780 return 1;
1781}
1782
Li Zefan311d16d2009-12-08 11:15:11 +08001783static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001784{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001785 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001786 struct ftrace_page *pg;
1787 struct dyn_ftrace *rec;
1788 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001789 char *search = buff;
1790 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001791 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001792 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001793
Steven Rostedt6a24a242009-02-17 11:20:26 -05001794 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1795
Steven Rostedt64e7c442009-02-13 17:08:48 -05001796 /* blank or '*' mean the same */
1797 if (strcmp(buff, "*") == 0)
1798 buff[0] = 0;
1799
1800 /* handle the case of 'dont filter this module' */
1801 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1802 buff[0] = 0;
1803 not = 1;
1804 }
1805
1806 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001807 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001808 search_len = strlen(search);
1809 }
1810
Steven Rostedt52baf112009-02-14 01:15:39 -05001811 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001812 do_for_each_ftrace_rec(pg, rec) {
1813
1814 if (rec->flags & FTRACE_FL_FAILED)
1815 continue;
1816
1817 if (ftrace_match_module_record(rec, mod,
1818 search, search_len, type)) {
1819 if (not)
1820 rec->flags &= ~flag;
1821 else
1822 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001823 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001824 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001825 if (enable && (rec->flags & FTRACE_FL_FILTER))
1826 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001827
1828 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001829 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001830
1831 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001832}
1833
Steven Rostedtf6180772009-02-14 00:40:25 -05001834/*
1835 * We register the module command as a template to show others how
1836 * to register the a command as well.
1837 */
1838
1839static int
1840ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1841{
1842 char *mod;
1843
1844 /*
1845 * cmd == 'mod' because we only registered this func
1846 * for the 'mod' ftrace_func_command.
1847 * But if you register one func with multiple commands,
1848 * you can tell which command was used by the cmd
1849 * parameter.
1850 */
1851
1852 /* we must have a module name */
1853 if (!param)
1854 return -EINVAL;
1855
1856 mod = strsep(&param, ":");
1857 if (!strlen(mod))
1858 return -EINVAL;
1859
Li Zefan311d16d2009-12-08 11:15:11 +08001860 if (ftrace_match_module_records(func, mod, enable))
1861 return 0;
1862 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001863}
1864
1865static struct ftrace_func_command ftrace_mod_cmd = {
1866 .name = "mod",
1867 .func = ftrace_mod_callback,
1868};
1869
1870static int __init ftrace_mod_cmd_init(void)
1871{
1872 return register_ftrace_command(&ftrace_mod_cmd);
1873}
1874device_initcall(ftrace_mod_cmd_init);
1875
Steven Rostedt59df055f2009-02-14 15:29:06 -05001876static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001877function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001878{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001879 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001880 struct hlist_head *hhd;
1881 struct hlist_node *n;
1882 unsigned long key;
1883 int resched;
1884
1885 key = hash_long(ip, FTRACE_HASH_BITS);
1886
1887 hhd = &ftrace_func_hash[key];
1888
1889 if (hlist_empty(hhd))
1890 return;
1891
1892 /*
1893 * Disable preemption for these calls to prevent a RCU grace
1894 * period. This syncs the hash iteration and freeing of items
1895 * on the hash. rcu_read_lock is too dangerous here.
1896 */
1897 resched = ftrace_preempt_disable();
1898 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1899 if (entry->ip == ip)
1900 entry->ops->func(ip, parent_ip, &entry->data);
1901 }
1902 ftrace_preempt_enable(resched);
1903}
1904
Steven Rostedtb6887d72009-02-17 12:32:04 -05001905static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001906{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001907 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001908};
1909
Steven Rostedtb6887d72009-02-17 12:32:04 -05001910static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001911
Steven Rostedtb6887d72009-02-17 12:32:04 -05001912static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001913{
1914 int i;
1915
Steven Rostedtb6887d72009-02-17 12:32:04 -05001916 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001917 return;
1918
1919 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1920 struct hlist_head *hhd = &ftrace_func_hash[i];
1921 if (hhd->first)
1922 break;
1923 }
1924 /* Nothing registered? */
1925 if (i == FTRACE_FUNC_HASHSIZE)
1926 return;
1927
Steven Rostedtb6887d72009-02-17 12:32:04 -05001928 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001929 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001930 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001931}
1932
Steven Rostedtb6887d72009-02-17 12:32:04 -05001933static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001934{
1935 int i;
1936
Steven Rostedtb6887d72009-02-17 12:32:04 -05001937 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001938 return;
1939
1940 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1941 struct hlist_head *hhd = &ftrace_func_hash[i];
1942 if (hhd->first)
1943 return;
1944 }
1945
1946 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001947 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001948 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001949 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950}
1951
1952
1953static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1954{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001955 struct ftrace_func_probe *entry =
1956 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001957
1958 if (entry->ops->free)
1959 entry->ops->free(&entry->data);
1960 kfree(entry);
1961}
1962
1963
1964int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001965register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001966 void *data)
1967{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001968 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001969 struct ftrace_page *pg;
1970 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001971 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001972 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001973 int count = 0;
1974 char *search;
1975
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001976 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001977 len = strlen(search);
1978
Steven Rostedtb6887d72009-02-17 12:32:04 -05001979 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001980 if (WARN_ON(not))
1981 return -EINVAL;
1982
1983 mutex_lock(&ftrace_lock);
1984 do_for_each_ftrace_rec(pg, rec) {
1985
1986 if (rec->flags & FTRACE_FL_FAILED)
1987 continue;
1988
1989 if (!ftrace_match_record(rec, search, len, type))
1990 continue;
1991
1992 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1993 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001994 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001995 if (!count)
1996 count = -ENOMEM;
1997 goto out_unlock;
1998 }
1999
2000 count++;
2001
2002 entry->data = data;
2003
2004 /*
2005 * The caller might want to do something special
2006 * for each function we find. We call the callback
2007 * to give the caller an opportunity to do so.
2008 */
2009 if (ops->callback) {
2010 if (ops->callback(rec->ip, &entry->data) < 0) {
2011 /* caller does not like this func */
2012 kfree(entry);
2013 continue;
2014 }
2015 }
2016
2017 entry->ops = ops;
2018 entry->ip = rec->ip;
2019
2020 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2021 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2022
2023 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002024 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002025
2026 out_unlock:
2027 mutex_unlock(&ftrace_lock);
2028
2029 return count;
2030}
2031
2032enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002033 PROBE_TEST_FUNC = 1,
2034 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002035};
2036
2037static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002038__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002039 void *data, int flags)
2040{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002041 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002042 struct hlist_node *n, *tmp;
2043 char str[KSYM_SYMBOL_LEN];
2044 int type = MATCH_FULL;
2045 int i, len = 0;
2046 char *search;
2047
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002048 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002049 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002050 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051 int not;
2052
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002053 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002054 len = strlen(search);
2055
Steven Rostedtb6887d72009-02-17 12:32:04 -05002056 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002057 if (WARN_ON(not))
2058 return;
2059 }
2060
2061 mutex_lock(&ftrace_lock);
2062 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2063 struct hlist_head *hhd = &ftrace_func_hash[i];
2064
2065 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2066
2067 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002068 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002069 continue;
2070
Steven Rostedtb6887d72009-02-17 12:32:04 -05002071 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002072 continue;
2073
2074 /* do this last, since it is the most expensive */
2075 if (glob) {
2076 kallsyms_lookup(entry->ip, NULL, NULL,
2077 NULL, str);
2078 if (!ftrace_match(str, glob, len, type))
2079 continue;
2080 }
2081
2082 hlist_del(&entry->node);
2083 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2084 }
2085 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002086 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002087 mutex_unlock(&ftrace_lock);
2088}
2089
2090void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002091unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002092 void *data)
2093{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002094 __unregister_ftrace_function_probe(glob, ops, data,
2095 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002096}
2097
2098void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002099unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002100{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002101 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002102}
2103
Steven Rostedtb6887d72009-02-17 12:32:04 -05002104void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002105{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002106 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002107}
2108
Steven Rostedtf6180772009-02-14 00:40:25 -05002109static LIST_HEAD(ftrace_commands);
2110static DEFINE_MUTEX(ftrace_cmd_mutex);
2111
2112int register_ftrace_command(struct ftrace_func_command *cmd)
2113{
2114 struct ftrace_func_command *p;
2115 int ret = 0;
2116
2117 mutex_lock(&ftrace_cmd_mutex);
2118 list_for_each_entry(p, &ftrace_commands, list) {
2119 if (strcmp(cmd->name, p->name) == 0) {
2120 ret = -EBUSY;
2121 goto out_unlock;
2122 }
2123 }
2124 list_add(&cmd->list, &ftrace_commands);
2125 out_unlock:
2126 mutex_unlock(&ftrace_cmd_mutex);
2127
2128 return ret;
2129}
2130
2131int unregister_ftrace_command(struct ftrace_func_command *cmd)
2132{
2133 struct ftrace_func_command *p, *n;
2134 int ret = -ENODEV;
2135
2136 mutex_lock(&ftrace_cmd_mutex);
2137 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2138 if (strcmp(cmd->name, p->name) == 0) {
2139 ret = 0;
2140 list_del_init(&p->list);
2141 goto out_unlock;
2142 }
2143 }
2144 out_unlock:
2145 mutex_unlock(&ftrace_cmd_mutex);
2146
2147 return ret;
2148}
2149
Steven Rostedt64e7c442009-02-13 17:08:48 -05002150static int ftrace_process_regex(char *buff, int len, int enable)
2151{
Steven Rostedtf6180772009-02-14 00:40:25 -05002152 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002153 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002154 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002155
2156 func = strsep(&next, ":");
2157
2158 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002159 if (ftrace_match_records(func, len, enable))
2160 return 0;
2161 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002162 }
2163
Steven Rostedtf6180772009-02-14 00:40:25 -05002164 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002165
2166 command = strsep(&next, ":");
2167
Steven Rostedtf6180772009-02-14 00:40:25 -05002168 mutex_lock(&ftrace_cmd_mutex);
2169 list_for_each_entry(p, &ftrace_commands, list) {
2170 if (strcmp(p->name, command) == 0) {
2171 ret = p->func(func, command, next, enable);
2172 goto out_unlock;
2173 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002174 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002175 out_unlock:
2176 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002177
Steven Rostedtf6180772009-02-14 00:40:25 -05002178 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002179}
2180
Ingo Molnare309b412008-05-12 21:20:51 +02002181static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002182ftrace_regex_write(struct file *file, const char __user *ubuf,
2183 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002184{
2185 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002186 struct trace_parser *parser;
2187 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002188
Li Zefan4ba79782009-09-22 13:52:20 +08002189 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002190 return 0;
2191
Steven Rostedt41c52c02008-05-22 11:46:33 -04002192 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002193
2194 if (file->f_mode & FMODE_READ) {
2195 struct seq_file *m = file->private_data;
2196 iter = m->private;
2197 } else
2198 iter = file->private_data;
2199
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002200 parser = &iter->parser;
2201 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002202
Li Zefan4ba79782009-09-22 13:52:20 +08002203 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002204 !trace_parser_cont(parser)) {
2205 ret = ftrace_process_regex(parser->buffer,
2206 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08002207 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002208 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002209 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02002210 }
2211
Steven Rostedt5072c592008-05-12 21:20:43 +02002212 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002213out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002214 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002215
Steven Rostedt5072c592008-05-12 21:20:43 +02002216 return ret;
2217}
2218
Steven Rostedt41c52c02008-05-22 11:46:33 -04002219static ssize_t
2220ftrace_filter_write(struct file *file, const char __user *ubuf,
2221 size_t cnt, loff_t *ppos)
2222{
2223 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2224}
2225
2226static ssize_t
2227ftrace_notrace_write(struct file *file, const char __user *ubuf,
2228 size_t cnt, loff_t *ppos)
2229{
2230 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2231}
2232
2233static void
2234ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2235{
2236 if (unlikely(ftrace_disabled))
2237 return;
2238
2239 mutex_lock(&ftrace_regex_lock);
2240 if (reset)
2241 ftrace_filter_reset(enable);
2242 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002243 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002244 mutex_unlock(&ftrace_regex_lock);
2245}
2246
Steven Rostedt77a2b372008-05-12 21:20:45 +02002247/**
2248 * ftrace_set_filter - set a function to filter on in ftrace
2249 * @buf - the string that holds the function filter text.
2250 * @len - the length of the string.
2251 * @reset - non zero to reset all filters before applying this filter.
2252 *
2253 * Filters denote which functions should be enabled when tracing is enabled.
2254 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2255 */
Ingo Molnare309b412008-05-12 21:20:51 +02002256void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002257{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002258 ftrace_set_regex(buf, len, reset, 1);
2259}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002260
Steven Rostedt41c52c02008-05-22 11:46:33 -04002261/**
2262 * ftrace_set_notrace - set a function to not trace in ftrace
2263 * @buf - the string that holds the function notrace text.
2264 * @len - the length of the string.
2265 * @reset - non zero to reset all filters before applying this filter.
2266 *
2267 * Notrace Filters denote which functions should not be enabled when tracing
2268 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2269 * for tracing.
2270 */
2271void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2272{
2273 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002274}
2275
Steven Rostedt2af15d62009-05-28 13:37:24 -04002276/*
2277 * command line interface to allow users to set filters on boot up.
2278 */
2279#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2280static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2281static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2282
2283static int __init set_ftrace_notrace(char *str)
2284{
2285 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2286 return 1;
2287}
2288__setup("ftrace_notrace=", set_ftrace_notrace);
2289
2290static int __init set_ftrace_filter(char *str)
2291{
2292 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2293 return 1;
2294}
2295__setup("ftrace_filter=", set_ftrace_filter);
2296
Stefan Assmann369bc182009-10-12 22:17:21 +02002297#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002298static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05002299static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2300
Stefan Assmann369bc182009-10-12 22:17:21 +02002301static int __init set_graph_function(char *str)
2302{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002303 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002304 return 1;
2305}
2306__setup("ftrace_graph_filter=", set_graph_function);
2307
2308static void __init set_ftrace_early_graph(char *buf)
2309{
2310 int ret;
2311 char *func;
2312
2313 while (buf) {
2314 func = strsep(&buf, ",");
2315 /* we allow only one expression at a time */
2316 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2317 func);
2318 if (ret)
2319 printk(KERN_DEBUG "ftrace: function %s not "
2320 "traceable\n", func);
2321 }
2322}
2323#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2324
Steven Rostedt2af15d62009-05-28 13:37:24 -04002325static void __init set_ftrace_early_filter(char *buf, int enable)
2326{
2327 char *func;
2328
2329 while (buf) {
2330 func = strsep(&buf, ",");
2331 ftrace_set_regex(func, strlen(func), 0, enable);
2332 }
2333}
2334
2335static void __init set_ftrace_early_filters(void)
2336{
2337 if (ftrace_filter_buf[0])
2338 set_ftrace_early_filter(ftrace_filter_buf, 1);
2339 if (ftrace_notrace_buf[0])
2340 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002341#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2342 if (ftrace_graph_buf[0])
2343 set_ftrace_early_graph(ftrace_graph_buf);
2344#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002345}
2346
Ingo Molnare309b412008-05-12 21:20:51 +02002347static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002348ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002349{
2350 struct seq_file *m = (struct seq_file *)file->private_data;
2351 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002352 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002353
Steven Rostedt41c52c02008-05-22 11:46:33 -04002354 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002355 if (file->f_mode & FMODE_READ) {
2356 iter = m->private;
2357
2358 seq_release(inode, file);
2359 } else
2360 iter = file->private_data;
2361
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002362 parser = &iter->parser;
2363 if (trace_parser_loaded(parser)) {
2364 parser->buffer[parser->idx] = 0;
2365 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002366 }
2367
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002368 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002369 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002370 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002371 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002372
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002373 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002374 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002375
Steven Rostedt41c52c02008-05-22 11:46:33 -04002376 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002377 return 0;
2378}
2379
Steven Rostedt41c52c02008-05-22 11:46:33 -04002380static int
2381ftrace_filter_release(struct inode *inode, struct file *file)
2382{
2383 return ftrace_regex_release(inode, file, 1);
2384}
2385
2386static int
2387ftrace_notrace_release(struct inode *inode, struct file *file)
2388{
2389 return ftrace_regex_release(inode, file, 0);
2390}
2391
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002392static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002393 .open = ftrace_avail_open,
2394 .read = seq_read,
2395 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002396 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002397};
2398
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002399static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302400 .open = ftrace_failures_open,
2401 .read = seq_read,
2402 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002403 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302404};
2405
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002406static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002407 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002408 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002409 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002410 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002411 .release = ftrace_filter_release,
2412};
2413
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002414static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002415 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002416 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002417 .write = ftrace_notrace_write,
2418 .llseek = ftrace_regex_lseek,
2419 .release = ftrace_notrace_release,
2420};
2421
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002422#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2423
2424static DEFINE_MUTEX(graph_lock);
2425
2426int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002427int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002428unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2429
2430static void *
Li Zefan85951842009-06-24 09:54:00 +08002431__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002432{
Li Zefan85951842009-06-24 09:54:00 +08002433 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002434 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002435 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002436}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002437
Li Zefan85951842009-06-24 09:54:00 +08002438static void *
2439g_next(struct seq_file *m, void *v, loff_t *pos)
2440{
2441 (*pos)++;
2442 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002443}
2444
2445static void *g_start(struct seq_file *m, loff_t *pos)
2446{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002447 mutex_lock(&graph_lock);
2448
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002449 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002450 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002451 return (void *)1;
2452
Li Zefan85951842009-06-24 09:54:00 +08002453 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002454}
2455
2456static void g_stop(struct seq_file *m, void *p)
2457{
2458 mutex_unlock(&graph_lock);
2459}
2460
2461static int g_show(struct seq_file *m, void *v)
2462{
2463 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002464
2465 if (!ptr)
2466 return 0;
2467
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002468 if (ptr == (unsigned long *)1) {
2469 seq_printf(m, "#### all functions enabled ####\n");
2470 return 0;
2471 }
2472
Steven Rostedtb375a112009-09-17 00:05:58 -04002473 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002474
2475 return 0;
2476}
2477
James Morris88e9d342009-09-22 16:43:43 -07002478static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002479 .start = g_start,
2480 .next = g_next,
2481 .stop = g_stop,
2482 .show = g_show,
2483};
2484
2485static int
2486ftrace_graph_open(struct inode *inode, struct file *file)
2487{
2488 int ret = 0;
2489
2490 if (unlikely(ftrace_disabled))
2491 return -ENODEV;
2492
2493 mutex_lock(&graph_lock);
2494 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002495 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002496 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002497 ftrace_graph_count = 0;
2498 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2499 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002500 mutex_unlock(&graph_lock);
2501
Li Zefana4ec5e02009-09-18 14:06:28 +08002502 if (file->f_mode & FMODE_READ)
2503 ret = seq_open(file, &ftrace_graph_seq_ops);
2504
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002505 return ret;
2506}
2507
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002508static int
Li Zefan87827112009-07-23 11:29:11 +08002509ftrace_graph_release(struct inode *inode, struct file *file)
2510{
2511 if (file->f_mode & FMODE_READ)
2512 seq_release(inode, file);
2513 return 0;
2514}
2515
2516static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002517ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002518{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002519 struct dyn_ftrace *rec;
2520 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002521 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002522 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002523 int type, not;
2524 char *search;
2525 bool exists;
2526 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002527
2528 if (ftrace_disabled)
2529 return -ENODEV;
2530
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002531 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002532 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002533 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
2534 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002535
2536 search_len = strlen(search);
2537
Steven Rostedt52baf112009-02-14 01:15:39 -05002538 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002539 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002540
Steven Rostedt265c8312009-02-13 12:43:56 -05002541 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2542 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002543
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002544 if (ftrace_match_record(rec, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002545 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002546 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002547 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002548 if (array[i] == rec->ip) {
2549 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002550 break;
2551 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002552 }
2553
2554 if (!not) {
2555 fail = 0;
2556 if (!exists) {
2557 array[(*idx)++] = rec->ip;
2558 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2559 goto out;
2560 }
2561 } else {
2562 if (exists) {
2563 array[i] = array[--(*idx)];
2564 array[*idx] = 0;
2565 fail = 0;
2566 }
2567 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002568 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002569 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002570out:
Steven Rostedt52baf112009-02-14 01:15:39 -05002571 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002572
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002573 if (fail)
2574 return -EINVAL;
2575
2576 ftrace_graph_filter_enabled = 1;
2577 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002578}
2579
2580static ssize_t
2581ftrace_graph_write(struct file *file, const char __user *ubuf,
2582 size_t cnt, loff_t *ppos)
2583{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002584 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002585 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002586
Li Zefanc7c6b1f2010-02-10 15:43:04 +08002587 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002588 return 0;
2589
2590 mutex_lock(&graph_lock);
2591
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002592 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2593 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002594 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002595 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002596
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002597 read = trace_get_user(&parser, ubuf, cnt, ppos);
2598
Li Zefan4ba79782009-09-22 13:52:20 +08002599 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002600 parser.buffer[parser.idx] = 0;
2601
2602 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002603 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002604 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002605 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002606 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002607 }
2608
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002609 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002610
2611out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002612 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002613out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002614 mutex_unlock(&graph_lock);
2615
2616 return ret;
2617}
2618
2619static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002620 .open = ftrace_graph_open,
2621 .read = seq_read,
2622 .write = ftrace_graph_write,
2623 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002624};
2625#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2626
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002627static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002628{
Steven Rostedt5072c592008-05-12 21:20:43 +02002629
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002630 trace_create_file("available_filter_functions", 0444,
2631 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002632
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002633 trace_create_file("failures", 0444,
2634 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302635
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002636 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2637 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002638
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002639 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002640 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002641
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002642#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002643 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002644 NULL,
2645 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002646#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2647
Steven Rostedt5072c592008-05-12 21:20:43 +02002648 return 0;
2649}
2650
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002651static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002652 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002653 unsigned long *end)
2654{
2655 unsigned long *p;
2656 unsigned long addr;
2657 unsigned long flags;
2658
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002659 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002660 p = start;
2661 while (p < end) {
2662 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002663 /*
2664 * Some architecture linkers will pad between
2665 * the different mcount_loc sections of different
2666 * object files to satisfy alignments.
2667 * Skip any NULL pointers.
2668 */
2669 if (!addr)
2670 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002671 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002672 }
2673
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002674 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002675 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002676 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002677 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002678 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002679
2680 return 0;
2681}
2682
Steven Rostedt93eb6772009-04-15 13:24:06 -04002683#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002684void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002685{
2686 struct dyn_ftrace *rec;
2687 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002688
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002689 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002690 return;
2691
2692 mutex_lock(&ftrace_lock);
2693 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002694 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002695 /*
2696 * rec->ip is changed in ftrace_free_rec()
2697 * It should not between s and e if record was freed.
2698 */
2699 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2700 ftrace_free_rec(rec);
2701 }
2702 } while_for_each_ftrace_rec();
2703 mutex_unlock(&ftrace_lock);
2704}
2705
2706static void ftrace_init_module(struct module *mod,
2707 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002708{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002709 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002710 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002711 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002712}
2713
Steven Rostedt93eb6772009-04-15 13:24:06 -04002714static int ftrace_module_notify(struct notifier_block *self,
2715 unsigned long val, void *data)
2716{
2717 struct module *mod = data;
2718
2719 switch (val) {
2720 case MODULE_STATE_COMING:
2721 ftrace_init_module(mod, mod->ftrace_callsites,
2722 mod->ftrace_callsites +
2723 mod->num_ftrace_callsites);
2724 break;
2725 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002726 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002727 break;
2728 }
2729
2730 return 0;
2731}
2732#else
2733static int ftrace_module_notify(struct notifier_block *self,
2734 unsigned long val, void *data)
2735{
2736 return 0;
2737}
2738#endif /* CONFIG_MODULES */
2739
2740struct notifier_block ftrace_module_nb = {
2741 .notifier_call = ftrace_module_notify,
2742 .priority = 0,
2743};
2744
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002745extern unsigned long __start_mcount_loc[];
2746extern unsigned long __stop_mcount_loc[];
2747
2748void __init ftrace_init(void)
2749{
2750 unsigned long count, addr, flags;
2751 int ret;
2752
2753 /* Keep the ftrace pointer to the stub */
2754 addr = (unsigned long)ftrace_stub;
2755
2756 local_irq_save(flags);
2757 ftrace_dyn_arch_init(&addr);
2758 local_irq_restore(flags);
2759
2760 /* ftrace_dyn_arch_init places the return code in addr */
2761 if (addr)
2762 goto failed;
2763
2764 count = __stop_mcount_loc - __start_mcount_loc;
2765
2766 ret = ftrace_dyn_table_alloc(count);
2767 if (ret)
2768 goto failed;
2769
2770 last_ftrace_enabled = ftrace_enabled = 1;
2771
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002772 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002773 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002774 __stop_mcount_loc);
2775
Steven Rostedt93eb6772009-04-15 13:24:06 -04002776 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002777 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002778 pr_warning("Failed to register trace ftrace module notifier\n");
2779
Steven Rostedt2af15d62009-05-28 13:37:24 -04002780 set_ftrace_early_filters();
2781
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002782 return;
2783 failed:
2784 ftrace_disabled = 1;
2785}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002786
Steven Rostedt3d083392008-05-12 21:20:42 +02002787#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002788
2789static int __init ftrace_nodyn_init(void)
2790{
2791 ftrace_enabled = 1;
2792 return 0;
2793}
2794device_initcall(ftrace_nodyn_init);
2795
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002796static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2797static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002798/* Keep as macros so we do not need to define the commands */
2799# define ftrace_startup(command) do { } while (0)
2800# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002801# define ftrace_startup_sysctl() do { } while (0)
2802# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002803#endif /* CONFIG_DYNAMIC_FTRACE */
2804
Steven Rostedte32d8952008-12-04 00:26:41 -05002805static void clear_ftrace_swapper(void)
2806{
2807 struct task_struct *p;
2808 int cpu;
2809
2810 get_online_cpus();
2811 for_each_online_cpu(cpu) {
2812 p = idle_task(cpu);
2813 clear_tsk_trace_trace(p);
2814 }
2815 put_online_cpus();
2816}
2817
2818static void set_ftrace_swapper(void)
2819{
2820 struct task_struct *p;
2821 int cpu;
2822
2823 get_online_cpus();
2824 for_each_online_cpu(cpu) {
2825 p = idle_task(cpu);
2826 set_tsk_trace_trace(p);
2827 }
2828 put_online_cpus();
2829}
2830
2831static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002832{
2833 struct task_struct *p;
2834
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002835 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002836 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002837 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002838 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002839 rcu_read_unlock();
2840
Steven Rostedte32d8952008-12-04 00:26:41 -05002841 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002842}
2843
Steven Rostedte32d8952008-12-04 00:26:41 -05002844static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002845{
2846 struct task_struct *p;
2847
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002848 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002849 do_each_pid_task(pid, PIDTYPE_PID, p) {
2850 set_tsk_trace_trace(p);
2851 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002852 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002853}
2854
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002855static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002856{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002857 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002858 clear_ftrace_swapper();
2859 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002860 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002861}
2862
2863static void set_ftrace_pid_task(struct pid *pid)
2864{
2865 if (pid == ftrace_swapper_pid)
2866 set_ftrace_swapper();
2867 else
2868 set_ftrace_pid(pid);
2869}
2870
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002871static int ftrace_pid_add(int p)
2872{
2873 struct pid *pid;
2874 struct ftrace_pid *fpid;
2875 int ret = -EINVAL;
2876
2877 mutex_lock(&ftrace_lock);
2878
2879 if (!p)
2880 pid = ftrace_swapper_pid;
2881 else
2882 pid = find_get_pid(p);
2883
2884 if (!pid)
2885 goto out;
2886
2887 ret = 0;
2888
2889 list_for_each_entry(fpid, &ftrace_pids, list)
2890 if (fpid->pid == pid)
2891 goto out_put;
2892
2893 ret = -ENOMEM;
2894
2895 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2896 if (!fpid)
2897 goto out_put;
2898
2899 list_add(&fpid->list, &ftrace_pids);
2900 fpid->pid = pid;
2901
2902 set_ftrace_pid_task(pid);
2903
2904 ftrace_update_pid_func();
2905 ftrace_startup_enable(0);
2906
2907 mutex_unlock(&ftrace_lock);
2908 return 0;
2909
2910out_put:
2911 if (pid != ftrace_swapper_pid)
2912 put_pid(pid);
2913
2914out:
2915 mutex_unlock(&ftrace_lock);
2916 return ret;
2917}
2918
2919static void ftrace_pid_reset(void)
2920{
2921 struct ftrace_pid *fpid, *safe;
2922
2923 mutex_lock(&ftrace_lock);
2924 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2925 struct pid *pid = fpid->pid;
2926
2927 clear_ftrace_pid_task(pid);
2928
2929 list_del(&fpid->list);
2930 kfree(fpid);
2931 }
2932
2933 ftrace_update_pid_func();
2934 ftrace_startup_enable(0);
2935
2936 mutex_unlock(&ftrace_lock);
2937}
2938
2939static void *fpid_start(struct seq_file *m, loff_t *pos)
2940{
2941 mutex_lock(&ftrace_lock);
2942
2943 if (list_empty(&ftrace_pids) && (!*pos))
2944 return (void *) 1;
2945
2946 return seq_list_start(&ftrace_pids, *pos);
2947}
2948
2949static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2950{
2951 if (v == (void *)1)
2952 return NULL;
2953
2954 return seq_list_next(v, &ftrace_pids, pos);
2955}
2956
2957static void fpid_stop(struct seq_file *m, void *p)
2958{
2959 mutex_unlock(&ftrace_lock);
2960}
2961
2962static int fpid_show(struct seq_file *m, void *v)
2963{
2964 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2965
2966 if (v == (void *)1) {
2967 seq_printf(m, "no pid\n");
2968 return 0;
2969 }
2970
2971 if (fpid->pid == ftrace_swapper_pid)
2972 seq_printf(m, "swapper tasks\n");
2973 else
2974 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2975
2976 return 0;
2977}
2978
2979static const struct seq_operations ftrace_pid_sops = {
2980 .start = fpid_start,
2981 .next = fpid_next,
2982 .stop = fpid_stop,
2983 .show = fpid_show,
2984};
2985
2986static int
2987ftrace_pid_open(struct inode *inode, struct file *file)
2988{
2989 int ret = 0;
2990
2991 if ((file->f_mode & FMODE_WRITE) &&
2992 (file->f_flags & O_TRUNC))
2993 ftrace_pid_reset();
2994
2995 if (file->f_mode & FMODE_READ)
2996 ret = seq_open(file, &ftrace_pid_sops);
2997
2998 return ret;
2999}
3000
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003001static ssize_t
3002ftrace_pid_write(struct file *filp, const char __user *ubuf,
3003 size_t cnt, loff_t *ppos)
3004{
Ingo Molnar457dc922009-11-23 11:03:28 +01003005 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003006 long val;
3007 int ret;
3008
3009 if (cnt >= sizeof(buf))
3010 return -EINVAL;
3011
3012 if (copy_from_user(&buf, ubuf, cnt))
3013 return -EFAULT;
3014
3015 buf[cnt] = 0;
3016
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003017 /*
3018 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3019 * to clean the filter quietly.
3020 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003021 tmp = strstrip(buf);
3022 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003023 return 1;
3024
Ingo Molnar457dc922009-11-23 11:03:28 +01003025 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003026 if (ret < 0)
3027 return ret;
3028
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003029 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003030
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003031 return ret ? ret : cnt;
3032}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003033
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003034static int
3035ftrace_pid_release(struct inode *inode, struct file *file)
3036{
3037 if (file->f_mode & FMODE_READ)
3038 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003039
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003040 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003041}
3042
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003043static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003044 .open = ftrace_pid_open,
3045 .write = ftrace_pid_write,
3046 .read = seq_read,
3047 .llseek = seq_lseek,
3048 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003049};
3050
3051static __init int ftrace_init_debugfs(void)
3052{
3053 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003054
3055 d_tracer = tracing_init_dentry();
3056 if (!d_tracer)
3057 return 0;
3058
3059 ftrace_init_dyn_debugfs(d_tracer);
3060
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003061 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3062 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003063
3064 ftrace_profile_debugfs(d_tracer);
3065
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003066 return 0;
3067}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003068fs_initcall(ftrace_init_debugfs);
3069
Steven Rostedt3d083392008-05-12 21:20:42 +02003070/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003071 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003072 *
3073 * This function should be used by panic code. It stops ftrace
3074 * but in a not so nice way. If you need to simply kill ftrace
3075 * from a non-atomic section, use ftrace_kill.
3076 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003077void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003078{
3079 ftrace_disabled = 1;
3080 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003081 clear_ftrace_function();
3082}
3083
3084/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003085 * register_ftrace_function - register a function for profiling
3086 * @ops - ops structure that holds the function for profiling.
3087 *
3088 * Register a function to be called by all functions in the
3089 * kernel.
3090 *
3091 * Note: @ops->func and all the functions it calls must be labeled
3092 * with "notrace", otherwise it will go into a
3093 * recursive loop.
3094 */
3095int register_ftrace_function(struct ftrace_ops *ops)
3096{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003097 int ret;
3098
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003099 if (unlikely(ftrace_disabled))
3100 return -1;
3101
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003102 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003103
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003104 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003105 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003106
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003107 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003108 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003109}
3110
3111/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003112 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003113 * @ops - ops structure that holds the function to unregister
3114 *
3115 * Unregister a function that was added to be called by ftrace profiling.
3116 */
3117int unregister_ftrace_function(struct ftrace_ops *ops)
3118{
3119 int ret;
3120
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003121 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003122 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003123 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003124 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003125
3126 return ret;
3127}
3128
Ingo Molnare309b412008-05-12 21:20:51 +02003129int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003130ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003131 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003132 loff_t *ppos)
3133{
3134 int ret;
3135
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003136 if (unlikely(ftrace_disabled))
3137 return -ENODEV;
3138
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003139 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003140
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003141 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003142
Li Zefana32c7762009-06-26 16:55:51 +08003143 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003144 goto out;
3145
Li Zefana32c7762009-06-26 16:55:51 +08003146 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003147
3148 if (ftrace_enabled) {
3149
3150 ftrace_startup_sysctl();
3151
3152 /* we are starting ftrace again */
3153 if (ftrace_list != &ftrace_list_end) {
3154 if (ftrace_list->next == &ftrace_list_end)
3155 ftrace_trace_function = ftrace_list->func;
3156 else
3157 ftrace_trace_function = ftrace_list_func;
3158 }
3159
3160 } else {
3161 /* stopping ftrace calls (just send to ftrace_stub) */
3162 ftrace_trace_function = ftrace_stub;
3163
3164 ftrace_shutdown_sysctl();
3165 }
3166
3167 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003168 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003169 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003170}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003171
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003172#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003173
Steven Rostedt597af812009-04-03 15:24:12 -04003174static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003175static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003176
Steven Rostedte49dc192008-12-02 23:50:05 -05003177int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3178{
3179 return 0;
3180}
3181
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003182/* The callbacks that hook a function */
3183trace_func_graph_ret_t ftrace_graph_return =
3184 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003185trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003186
3187/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3188static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3189{
3190 int i;
3191 int ret = 0;
3192 unsigned long flags;
3193 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3194 struct task_struct *g, *t;
3195
3196 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3197 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3198 * sizeof(struct ftrace_ret_stack),
3199 GFP_KERNEL);
3200 if (!ret_stack_list[i]) {
3201 start = 0;
3202 end = i;
3203 ret = -ENOMEM;
3204 goto free;
3205 }
3206 }
3207
3208 read_lock_irqsave(&tasklist_lock, flags);
3209 do_each_thread(g, t) {
3210 if (start == end) {
3211 ret = -EAGAIN;
3212 goto unlock;
3213 }
3214
3215 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003216 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003217 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003218 t->curr_ret_stack = -1;
3219 /* Make sure the tasks see the -1 first: */
3220 smp_wmb();
3221 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003222 }
3223 } while_each_thread(g, t);
3224
3225unlock:
3226 read_unlock_irqrestore(&tasklist_lock, flags);
3227free:
3228 for (i = start; i < end; i++)
3229 kfree(ret_stack_list[i]);
3230 return ret;
3231}
3232
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003233static void
3234ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3235 struct task_struct *next)
3236{
3237 unsigned long long timestamp;
3238 int index;
3239
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003240 /*
3241 * Does the user want to count the time a function was asleep.
3242 * If so, do not update the time stamps.
3243 */
3244 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3245 return;
3246
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003247 timestamp = trace_clock_local();
3248
3249 prev->ftrace_timestamp = timestamp;
3250
3251 /* only process tasks that we timestamped */
3252 if (!next->ftrace_timestamp)
3253 return;
3254
3255 /*
3256 * Update all the counters in next to make up for the
3257 * time next was sleeping.
3258 */
3259 timestamp -= next->ftrace_timestamp;
3260
3261 for (index = next->curr_ret_stack; index >= 0; index--)
3262 next->ret_stack[index].calltime += timestamp;
3263}
3264
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003265/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003266static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003267{
3268 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003269 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003270
3271 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3272 sizeof(struct ftrace_ret_stack *),
3273 GFP_KERNEL);
3274
3275 if (!ret_stack_list)
3276 return -ENOMEM;
3277
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003278 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003279 for_each_online_cpu(cpu) {
3280 if (!idle_task(cpu)->ret_stack)
3281 ftrace_graph_init_task(idle_task(cpu));
3282 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003283
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003284 do {
3285 ret = alloc_retstack_tasklist(ret_stack_list);
3286 } while (ret == -EAGAIN);
3287
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003288 if (!ret) {
3289 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3290 if (ret)
3291 pr_info("ftrace_graph: Couldn't activate tracepoint"
3292 " probe to kernel_sched_switch\n");
3293 }
3294
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003295 kfree(ret_stack_list);
3296 return ret;
3297}
3298
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003299/*
3300 * Hibernation protection.
3301 * The state of the current task is too much unstable during
3302 * suspend/restore to disk. We want to protect against that.
3303 */
3304static int
3305ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3306 void *unused)
3307{
3308 switch (state) {
3309 case PM_HIBERNATION_PREPARE:
3310 pause_graph_tracing();
3311 break;
3312
3313 case PM_POST_HIBERNATION:
3314 unpause_graph_tracing();
3315 break;
3316 }
3317 return NOTIFY_DONE;
3318}
3319
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003320int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3321 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003322{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003323 int ret = 0;
3324
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003325 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003326
Steven Rostedt05ce5812009-03-24 00:18:31 -04003327 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003328 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003329 ret = -EBUSY;
3330 goto out;
3331 }
3332
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003333 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3334 register_pm_notifier(&ftrace_suspend_notifier);
3335
Steven Rostedt597af812009-04-03 15:24:12 -04003336 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003337 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003338 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003339 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003340 goto out;
3341 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003342
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003343 ftrace_graph_return = retfunc;
3344 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003345
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003346 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003347
3348out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003349 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003350 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003351}
3352
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003353void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003354{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003355 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003356
Steven Rostedt597af812009-04-03 15:24:12 -04003357 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003358 goto out;
3359
Steven Rostedt597af812009-04-03 15:24:12 -04003360 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003361 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003362 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003363 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003364 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003365 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003366
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003367 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003368 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003369}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003370
3371/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003372void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003373{
Steven Rostedt84047e32009-06-02 16:51:55 -04003374 /* Make sure we do not use the parent ret_stack */
3375 t->ret_stack = NULL;
3376
Steven Rostedt597af812009-04-03 15:24:12 -04003377 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003378 struct ftrace_ret_stack *ret_stack;
3379
3380 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003381 * sizeof(struct ftrace_ret_stack),
3382 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003383 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003384 return;
3385 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003386 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003387 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003388 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003389 /* make curr_ret_stack visable before we add the ret_stack */
3390 smp_wmb();
3391 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003392 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003393}
3394
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003395void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003396{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003397 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3398
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003399 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003400 /* NULL must become visible to IRQs before we free it: */
3401 barrier();
3402
3403 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003404}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003405
3406void ftrace_graph_stop(void)
3407{
3408 ftrace_stop();
3409}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003410#endif