blob: 08a3fb5b3187b294bfb96068285f89b36458e230 [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 Rostedt6912896e2008-10-23 09:33:03 -040040#define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
45
46#define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
51
Steven Rostedt8fc0c702009-02-16 15:28:00 -050052/* hash bits for specific function selection */
53#define FTRACE_HASH_BITS 7
54#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55
Steven Rostedt4eebcc82008-05-12 21:20:48 +020056/* ftrace_enabled is a method to turn ftrace on or off */
57int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020058static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020059
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050060/* Quick disabling of function tracer. */
61int function_trace_stop;
62
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040063/* List for set_ftrace_pid's pids. */
64LIST_HEAD(ftrace_pids);
65struct ftrace_pid {
66 struct list_head list;
67 struct pid *pid;
68};
69
Steven Rostedt4eebcc82008-05-12 21:20:48 +020070/*
71 * ftrace_disabled is set when an anomaly is discovered.
72 * ftrace_disabled is much stronger than ftrace_enabled.
73 */
74static int ftrace_disabled __read_mostly;
75
Steven Rostedt52baf112009-02-14 01:15:39 -050076static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020077
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020078static struct ftrace_ops ftrace_list_end __read_mostly =
79{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040080 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020081};
82
83static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
84ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050085ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050086ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020087
Stefan Assmann369bc182009-10-12 22:17:21 +020088#ifdef CONFIG_FUNCTION_GRAPH_TRACER
89static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
90#endif
91
Ingo Molnarf2252932008-05-22 10:37:48 +020092static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020093{
94 struct ftrace_ops *op = ftrace_list;
95
96 /* in case someone actually ports this to alpha! */
97 read_barrier_depends();
98
99 while (op != &ftrace_list_end) {
100 /* silly alpha */
101 read_barrier_depends();
102 op->func(ip, parent_ip);
103 op = op->next;
104 };
105}
106
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500107static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
108{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500109 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500110 return;
111
112 ftrace_pid_function(ip, parent_ip);
113}
114
115static void set_ftrace_pid_function(ftrace_func_t func)
116{
117 /* do not set ftrace_pid_function to itself! */
118 if (func != ftrace_pid_func)
119 ftrace_pid_function = func;
120}
121
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200123 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200124 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200125 * This NULLs the ftrace function and in essence stops
126 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200127 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200128void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200129{
Steven Rostedt3d083392008-05-12 21:20:42 +0200130 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500131 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500132 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200133}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200134
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500135#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
136/*
137 * For those archs that do not test ftrace_trace_stop in their
138 * mcount call site, we need to do it from C.
139 */
140static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
141{
142 if (function_trace_stop)
143 return;
144
145 __ftrace_trace_function(ip, parent_ip);
146}
147#endif
148
Ingo Molnare309b412008-05-12 21:20:51 +0200149static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200150{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200151 ops->next = ftrace_list;
152 /*
153 * We are entering ops into the ftrace_list but another
154 * CPU might be walking that list. We need to make sure
155 * the ops->next pointer is valid before another CPU sees
156 * the ops pointer included into the ftrace_list.
157 */
158 smp_wmb();
159 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200160
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200161 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500162 ftrace_func_t func;
163
164 if (ops->next == &ftrace_list_end)
165 func = ops->func;
166 else
167 func = ftrace_list_func;
168
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400169 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500170 set_ftrace_pid_function(func);
171 func = ftrace_pid_func;
172 }
173
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200174 /*
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
177 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500178#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500180#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500181 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500182 ftrace_trace_function = ftrace_test_stop_func;
183#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200184 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200185
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186 return 0;
187}
188
Ingo Molnare309b412008-05-12 21:20:51 +0200189static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192
193 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196 */
197 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
198 ftrace_trace_function = ftrace_stub;
199 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500200 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 }
202
203 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
204 if (*p == ops)
205 break;
206
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500207 if (*p != ops)
208 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209
210 *p = (*p)->next;
211
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200212 if (ftrace_enabled) {
213 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500214 if (ftrace_list->next == &ftrace_list_end) {
215 ftrace_func_t func = ftrace_list->func;
216
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400217 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
220 }
221#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function = func;
223#else
224 __ftrace_trace_function = func;
225#endif
226 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200227 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200228
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500229 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200230}
231
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500232static void ftrace_update_pid_func(void)
233{
234 ftrace_func_t func;
235
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900237 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500238
Matt Fleming33974092009-09-28 16:43:01 +0100239#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500240 func = ftrace_trace_function;
Matt Fleming33974092009-09-28 16:43:01 +0100241#else
242 func = __ftrace_trace_function;
243#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500244
jolsa@redhat.com756d17e2009-10-13 16:33:52 -0400245 if (!list_empty(&ftrace_pids)) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500246 set_ftrace_pid_function(func);
247 func = ftrace_pid_func;
248 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800249 if (func == ftrace_pid_func)
250 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500251 }
252
253#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function = func;
255#else
256 __ftrace_trace_function = func;
257#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500258}
259
Steven Rostedt493762f2009-03-23 17:12:36 -0400260#ifdef CONFIG_FUNCTION_PROFILER
261struct ftrace_profile {
262 struct hlist_node node;
263 unsigned long ip;
264 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400265#ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time;
267#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400268};
269
270struct ftrace_profile_page {
271 struct ftrace_profile_page *next;
272 unsigned long index;
273 struct ftrace_profile records[];
274};
275
Steven Rostedtcafb1682009-03-24 20:50:39 -0400276struct ftrace_profile_stat {
277 atomic_t disabled;
278 struct hlist_head *hash;
279 struct ftrace_profile_page *pages;
280 struct ftrace_profile_page *start;
281 struct tracer_stat stat;
282};
283
Steven Rostedt493762f2009-03-23 17:12:36 -0400284#define PROFILE_RECORDS_SIZE \
285 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
286
287#define PROFILES_PER_PAGE \
288 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
289
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400290static int ftrace_profile_bits __read_mostly;
291static int ftrace_profile_enabled __read_mostly;
292
293/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400294static DEFINE_MUTEX(ftrace_profile_lock);
295
Steven Rostedtcafb1682009-03-24 20:50:39 -0400296static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400297
298#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
299
Steven Rostedt493762f2009-03-23 17:12:36 -0400300static void *
301function_stat_next(void *v, int idx)
302{
303 struct ftrace_profile *rec = v;
304 struct ftrace_profile_page *pg;
305
306 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
307
308 again:
Li Zefan0296e422009-06-26 11:15:37 +0800309 if (idx != 0)
310 rec++;
311
Steven Rostedt493762f2009-03-23 17:12:36 -0400312 if ((void *)rec >= (void *)&pg->records[pg->index]) {
313 pg = pg->next;
314 if (!pg)
315 return NULL;
316 rec = &pg->records[0];
317 if (!rec->counter)
318 goto again;
319 }
320
321 return rec;
322}
323
324static void *function_stat_start(struct tracer_stat *trace)
325{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400326 struct ftrace_profile_stat *stat =
327 container_of(trace, struct ftrace_profile_stat, stat);
328
329 if (!stat || !stat->start)
330 return NULL;
331
332 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400333}
334
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400335#ifdef CONFIG_FUNCTION_GRAPH_TRACER
336/* function graph compares on total time */
337static int function_stat_cmp(void *p1, void *p2)
338{
339 struct ftrace_profile *a = p1;
340 struct ftrace_profile *b = p2;
341
342 if (a->time < b->time)
343 return -1;
344 if (a->time > b->time)
345 return 1;
346 else
347 return 0;
348}
349#else
350/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400351static int function_stat_cmp(void *p1, void *p2)
352{
353 struct ftrace_profile *a = p1;
354 struct ftrace_profile *b = p2;
355
356 if (a->counter < b->counter)
357 return -1;
358 if (a->counter > b->counter)
359 return 1;
360 else
361 return 0;
362}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400363#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400364
365static int function_stat_headers(struct seq_file *m)
366{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400367#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400368 seq_printf(m, " Function "
369 "Hit Time Avg\n"
370 " -------- "
371 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400372#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400373 seq_printf(m, " Function Hit\n"
374 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400375#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400376 return 0;
377}
378
379static int function_stat_show(struct seq_file *m, void *v)
380{
381 struct ftrace_profile *rec = v;
382 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400383#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400384 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400385 static struct trace_seq s;
386 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400387#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400388
389 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400390 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400391
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400392#ifdef CONFIG_FUNCTION_GRAPH_TRACER
393 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400394 avg = rec->time;
395 do_div(avg, rec->counter);
396
397 mutex_lock(&mutex);
398 trace_seq_init(&s);
399 trace_print_graph_duration(rec->time, &s);
400 trace_seq_puts(&s, " ");
401 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400402 trace_print_seq(m, &s);
403 mutex_unlock(&mutex);
404#endif
405 seq_putc(m, '\n');
406
Steven Rostedt493762f2009-03-23 17:12:36 -0400407 return 0;
408}
409
Steven Rostedtcafb1682009-03-24 20:50:39 -0400410static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400411{
412 struct ftrace_profile_page *pg;
413
Steven Rostedtcafb1682009-03-24 20:50:39 -0400414 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400415
416 while (pg) {
417 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
418 pg->index = 0;
419 pg = pg->next;
420 }
421
Steven Rostedtcafb1682009-03-24 20:50:39 -0400422 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400423 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
424}
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400427{
428 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400429 int functions;
430 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400431 int i;
432
433 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400434 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400435 return 0;
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
438 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400439 return -ENOMEM;
440
Steven Rostedt318e0a72009-03-25 20:06:34 -0400441#ifdef CONFIG_DYNAMIC_FTRACE
442 functions = ftrace_update_tot_cnt;
443#else
444 /*
445 * We do not know the number of functions that exist because
446 * dynamic tracing is what counts them. With past experience
447 * we have around 20K functions. That should be more than enough.
448 * It is highly unlikely we will execute every function in
449 * the kernel.
450 */
451 functions = 20000;
452#endif
453
Steven Rostedtcafb1682009-03-24 20:50:39 -0400454 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400455
Steven Rostedt318e0a72009-03-25 20:06:34 -0400456 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
457
458 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400459 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400460 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400461 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 pg = pg->next;
463 }
464
465 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400466
467 out_free:
468 pg = stat->start;
469 while (pg) {
470 unsigned long tmp = (unsigned long)pg;
471
472 pg = pg->next;
473 free_page(tmp);
474 }
475
476 free_page((unsigned long)stat->pages);
477 stat->pages = NULL;
478 stat->start = NULL;
479
480 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400481}
482
Steven Rostedtcafb1682009-03-24 20:50:39 -0400483static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400484{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400485 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400486 int size;
487
Steven Rostedtcafb1682009-03-24 20:50:39 -0400488 stat = &per_cpu(ftrace_profile_stats, cpu);
489
490 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400491 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400493 return 0;
494 }
495
496 /*
497 * We are profiling all functions, but usually only a few thousand
498 * functions are hit. We'll make a hash of 1024 items.
499 */
500 size = FTRACE_PROFILE_HASH_SIZE;
501
Steven Rostedtcafb1682009-03-24 20:50:39 -0400502 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400503
Steven Rostedtcafb1682009-03-24 20:50:39 -0400504 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400505 return -ENOMEM;
506
Steven Rostedtcafb1682009-03-24 20:50:39 -0400507 if (!ftrace_profile_bits) {
508 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400509
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510 for (; size; size >>= 1)
511 ftrace_profile_bits++;
512 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400513
Steven Rostedt318e0a72009-03-25 20:06:34 -0400514 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400515 if (ftrace_profile_pages_init(stat) < 0) {
516 kfree(stat->hash);
517 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400518 return -ENOMEM;
519 }
520
521 return 0;
522}
523
Steven Rostedtcafb1682009-03-24 20:50:39 -0400524static int ftrace_profile_init(void)
525{
526 int cpu;
527 int ret = 0;
528
529 for_each_online_cpu(cpu) {
530 ret = ftrace_profile_init_cpu(cpu);
531 if (ret)
532 break;
533 }
534
535 return ret;
536}
537
Steven Rostedt493762f2009-03-23 17:12:36 -0400538/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400539static struct ftrace_profile *
540ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400541{
542 struct ftrace_profile *rec;
543 struct hlist_head *hhd;
544 struct hlist_node *n;
545 unsigned long key;
546
547 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400548 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400549
550 if (hlist_empty(hhd))
551 return NULL;
552
553 hlist_for_each_entry_rcu(rec, n, hhd, node) {
554 if (rec->ip == ip)
555 return rec;
556 }
557
558 return NULL;
559}
560
Steven Rostedtcafb1682009-03-24 20:50:39 -0400561static void ftrace_add_profile(struct ftrace_profile_stat *stat,
562 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400563{
564 unsigned long key;
565
566 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400567 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400568}
569
Steven Rostedt318e0a72009-03-25 20:06:34 -0400570/*
571 * The memory is already allocated, this simply finds a new record to use.
572 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400573static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400574ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400575{
576 struct ftrace_profile *rec = NULL;
577
Steven Rostedt318e0a72009-03-25 20:06:34 -0400578 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400579 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400580 goto out;
581
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400583 * Try to find the function again since an NMI
584 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 if (stat->pages->index == PROFILES_PER_PAGE) {
591 if (!stat->pages->next)
592 goto out;
593 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 }
595
Steven Rostedtcafb1682009-03-24 20:50:39 -0400596 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400599
Steven Rostedt493762f2009-03-23 17:12:36 -0400600 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400602
603 return rec;
604}
605
Steven Rostedt493762f2009-03-23 17:12:36 -0400606static void
607function_profile_call(unsigned long ip, unsigned long parent_ip)
608{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 struct ftrace_profile *rec;
611 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400612
613 if (!ftrace_profile_enabled)
614 return;
615
Steven Rostedt493762f2009-03-23 17:12:36 -0400616 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617
618 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400619 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400620 goto out;
621
622 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400625 if (!rec)
626 goto out;
627 }
628
629 rec->counter++;
630 out:
631 local_irq_restore(flags);
632}
633
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634#ifdef CONFIG_FUNCTION_GRAPH_TRACER
635static int profile_graph_entry(struct ftrace_graph_ent *trace)
636{
637 function_profile_call(trace->func, 0);
638 return 1;
639}
640
641static void profile_graph_return(struct ftrace_graph_ret *trace)
642{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400644 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400645 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647
648 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400650 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400651 goto out;
652
Steven Rostedta2a16d62009-03-24 23:17:58 -0400653 calltime = trace->rettime - trace->calltime;
654
655 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
656 int index;
657
658 index = trace->depth;
659
660 /* Append this call time to the parent time to subtract */
661 if (index)
662 current->ret_stack[index - 1].subtime += calltime;
663
664 if (current->ret_stack[index].subtime < calltime)
665 calltime -= current->ret_stack[index].subtime;
666 else
667 calltime = 0;
668 }
669
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400671 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400672 rec->time += calltime;
673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400675 local_irq_restore(flags);
676}
677
678static int register_ftrace_profiler(void)
679{
680 return register_ftrace_graph(&profile_graph_return,
681 &profile_graph_entry);
682}
683
684static void unregister_ftrace_profiler(void)
685{
686 unregister_ftrace_graph();
687}
688#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400689static struct ftrace_ops ftrace_profile_ops __read_mostly =
690{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400691 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400692};
693
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400694static int register_ftrace_profiler(void)
695{
696 return register_ftrace_function(&ftrace_profile_ops);
697}
698
699static void unregister_ftrace_profiler(void)
700{
701 unregister_ftrace_function(&ftrace_profile_ops);
702}
703#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
704
Steven Rostedt493762f2009-03-23 17:12:36 -0400705static ssize_t
706ftrace_profile_write(struct file *filp, const char __user *ubuf,
707 size_t cnt, loff_t *ppos)
708{
709 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400710 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 int ret;
712
713 if (cnt >= sizeof(buf))
714 return -EINVAL;
715
716 if (copy_from_user(&buf, ubuf, cnt))
717 return -EFAULT;
718
719 buf[cnt] = 0;
720
721 ret = strict_strtoul(buf, 10, &val);
722 if (ret < 0)
723 return ret;
724
725 val = !!val;
726
727 mutex_lock(&ftrace_profile_lock);
728 if (ftrace_profile_enabled ^ val) {
729 if (val) {
730 ret = ftrace_profile_init();
731 if (ret < 0) {
732 cnt = ret;
733 goto out;
734 }
735
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400736 ret = register_ftrace_profiler();
737 if (ret < 0) {
738 cnt = ret;
739 goto out;
740 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400741 ftrace_profile_enabled = 1;
742 } else {
743 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400744 /*
745 * unregister_ftrace_profiler calls stop_machine
746 * so this acts like an synchronize_sched.
747 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400748 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 }
750 }
751 out:
752 mutex_unlock(&ftrace_profile_lock);
753
Jiri Olsacf8517c2009-10-23 19:36:16 -0400754 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400755
756 return cnt;
757}
758
759static ssize_t
760ftrace_profile_read(struct file *filp, char __user *ubuf,
761 size_t cnt, loff_t *ppos)
762{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400763 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 int r;
765
766 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
767 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
768}
769
770static const struct file_operations ftrace_profile_fops = {
771 .open = tracing_open_generic,
772 .read = ftrace_profile_read,
773 .write = ftrace_profile_write,
774};
775
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776/* used to initialize the real stat files */
777static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400778 .name = "functions",
779 .stat_start = function_stat_start,
780 .stat_next = function_stat_next,
781 .stat_cmp = function_stat_cmp,
782 .stat_headers = function_stat_headers,
783 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784};
785
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400786static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400787{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400793
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794 for_each_possible_cpu(cpu) {
795 stat = &per_cpu(ftrace_profile_stats, cpu);
796
797 /* allocate enough for function name + cpu number */
798 name = kmalloc(32, GFP_KERNEL);
799 if (!name) {
800 /*
801 * The files created are permanent, if something happens
802 * we still do not free memory.
803 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 WARN(1,
805 "Could not allocate stat file for cpu %d\n",
806 cpu);
807 return;
808 }
809 stat->stat = function_stats;
810 snprintf(name, 32, "function%d", cpu);
811 stat->stat.name = name;
812 ret = register_stat_tracer(&stat->stat);
813 if (ret) {
814 WARN(1,
815 "Could not register function stat for cpu %d\n",
816 cpu);
817 kfree(name);
818 return;
819 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 }
821
822 entry = debugfs_create_file("function_profile_enabled", 0644,
823 d_tracer, NULL, &ftrace_profile_fops);
824 if (!entry)
825 pr_warning("Could not create debugfs "
826 "'function_profile_enabled' entry\n");
827}
828
829#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400830static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400831{
832}
833#endif /* CONFIG_FUNCTION_PROFILER */
834
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100835static struct pid * const ftrace_swapper_pid = &init_struct_pid;
836
Steven Rostedt3d083392008-05-12 21:20:42 +0200837#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100838
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400839#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400840# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400841#endif
842
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500843static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
844
Steven Rostedtb6887d72009-02-17 12:32:04 -0500845struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500846 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500847 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500848 unsigned long flags;
849 unsigned long ip;
850 void *data;
851 struct rcu_head rcu;
852};
853
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200854enum {
855 FTRACE_ENABLE_CALLS = (1 << 0),
856 FTRACE_DISABLE_CALLS = (1 << 1),
857 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
858 FTRACE_ENABLE_MCOUNT = (1 << 3),
859 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500860 FTRACE_START_FUNC_RET = (1 << 5),
861 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200862};
863
Steven Rostedt5072c592008-05-12 21:20:43 +0200864static int ftrace_filtered;
865
Lai Jiangshane94142a2009-03-13 17:51:27 +0800866static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200867
Steven Rostedt41c52c02008-05-22 11:46:33 -0400868static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200869
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200870struct ftrace_page {
871 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500872 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200873 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700874};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200875
876#define ENTRIES_PER_PAGE \
877 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
878
879/* estimate from running different kernels */
880#define NR_TO_INIT 10000
881
882static struct ftrace_page *ftrace_pages_start;
883static struct ftrace_page *ftrace_pages;
884
Steven Rostedt37ad50842008-05-12 21:20:48 +0200885static struct dyn_ftrace *ftrace_free_records;
886
Steven Rostedt265c8312009-02-13 12:43:56 -0500887/*
888 * This is a double for. Do not use 'break' to break out of the loop,
889 * you must use a goto.
890 */
891#define do_for_each_ftrace_rec(pg, rec) \
892 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
893 int _____i; \
894 for (_____i = 0; _____i < pg->index; _____i++) { \
895 rec = &pg->records[_____i];
896
897#define while_for_each_ftrace_rec() \
898 } \
899 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530900
901#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200902
903static int frozen_record_count;
904
Abhishek Sagarecea6562008-06-21 23:47:53 +0530905static inline void freeze_record(struct dyn_ftrace *rec)
906{
907 if (!(rec->flags & FTRACE_FL_FROZEN)) {
908 rec->flags |= FTRACE_FL_FROZEN;
909 frozen_record_count++;
910 }
911}
912
913static inline void unfreeze_record(struct dyn_ftrace *rec)
914{
915 if (rec->flags & FTRACE_FL_FROZEN) {
916 rec->flags &= ~FTRACE_FL_FROZEN;
917 frozen_record_count--;
918 }
919}
920
921static inline int record_frozen(struct dyn_ftrace *rec)
922{
923 return rec->flags & FTRACE_FL_FROZEN;
924}
925#else
926# define freeze_record(rec) ({ 0; })
927# define unfreeze_record(rec) ({ 0; })
928# define record_frozen(rec) ({ 0; })
929#endif /* CONFIG_KPROBES */
930
Ingo Molnare309b412008-05-12 21:20:51 +0200931static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +0200932{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800933 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200934 ftrace_free_records = rec;
935 rec->flags |= FTRACE_FL_FREE;
936}
937
Ingo Molnare309b412008-05-12 21:20:51 +0200938static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200939{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200940 struct dyn_ftrace *rec;
941
942 /* First check for freed records */
943 if (ftrace_free_records) {
944 rec = ftrace_free_records;
945
Steven Rostedt37ad50842008-05-12 21:20:48 +0200946 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400947 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +0200948 ftrace_free_records = NULL;
949 return NULL;
950 }
951
Lai Jiangshanee000b72009-03-24 13:38:06 +0800952 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200953 memset(rec, 0, sizeof(*rec));
954 return rec;
955 }
956
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200957 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400958 if (!ftrace_pages->next) {
959 /* allocate another page */
960 ftrace_pages->next =
961 (void *)get_zeroed_page(GFP_KERNEL);
962 if (!ftrace_pages->next)
963 return NULL;
964 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200965 ftrace_pages = ftrace_pages->next;
966 }
967
968 return &ftrace_pages->records[ftrace_pages->index++];
969}
970
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400971static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200972ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200973{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400974 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200975
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800976 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400977 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200978
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400979 rec = ftrace_alloc_dyn_node(ip);
980 if (!rec)
981 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200982
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400983 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800984 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800985 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200986
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400987 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200988}
989
Steven Rostedt05736a42008-09-22 14:55:47 -0700990static void print_ip_ins(const char *fmt, unsigned char *p)
991{
992 int i;
993
994 printk(KERN_CONT "%s", fmt);
995
996 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
997 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
998}
999
Steven Rostedt31e88902008-11-14 16:21:19 -08001000static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001001{
1002 switch (failed) {
1003 case -EFAULT:
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on modifying ");
1006 print_ip_sym(ip);
1007 break;
1008 case -EINVAL:
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace failed to modify ");
1011 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001012 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001013 printk(KERN_CONT "\n");
1014 break;
1015 case -EPERM:
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on writing ");
1018 print_ip_sym(ip);
1019 break;
1020 default:
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on unknown error ");
1023 print_ip_sym(ip);
1024 }
1025}
1026
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001027
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301028static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001029__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001030{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001031 unsigned long ftrace_addr;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001032 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001033
Shaohua Lif0001202009-01-09 11:29:42 +08001034 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001035
Steven Rostedt982c3502008-11-15 16:31:41 -05001036 /*
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001037 * If this record is not to be traced or we want to disable it,
1038 * then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001039 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001040 * If we want to enable it and filtering is off, then enable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001041 *
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001042 * If we want to enable it and filtering is on, enable it only if
1043 * it's filtered
Steven Rostedt982c3502008-11-15 16:31:41 -05001044 */
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001045 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1046 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1047 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001048 }
1049
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001050 /* If the state of this record hasn't changed, then do nothing */
1051 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1052 return 0;
1053
1054 if (flag) {
1055 rec->flags |= FTRACE_FL_ENABLED;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001056 return ftrace_make_call(rec, ftrace_addr);
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001057 }
1058
1059 rec->flags &= ~FTRACE_FL_ENABLED;
1060 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001061}
1062
1063static void ftrace_replace_code(int enable)
1064{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001065 struct dyn_ftrace *rec;
1066 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001067 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001068
Steven Rostedt265c8312009-02-13 12:43:56 -05001069 do_for_each_ftrace_rec(pg, rec) {
1070 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001071 * Skip over free records, records that have
1072 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001073 */
1074 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001075 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001076 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001077 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001078
Steven Rostedt265c8312009-02-13 12:43:56 -05001079 /* ignore updates to this record's mcount site */
1080 if (get_kprobe((void *)rec->ip)) {
1081 freeze_record(rec);
1082 continue;
1083 } else {
1084 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +02001085 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001086
1087 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001088 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001089 rec->flags |= FTRACE_FL_FAILED;
Steven Rostedt3279ba32009-10-07 16:57:56 -04001090 ftrace_bug(failed, rec->ip);
1091 /* Stop processing */
1092 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001093 }
1094 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095}
1096
Ingo Molnare309b412008-05-12 21:20:51 +02001097static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001098ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001099{
1100 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001101 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001102
1103 ip = rec->ip;
1104
Shaohua Li25aac9d2009-01-09 11:29:40 +08001105 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001106 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001107 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001108 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301109 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001110 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301111 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001112}
1113
Steven Rostedt000ab692009-02-17 13:35:06 -05001114/*
1115 * archs can override this function if they must do something
1116 * before the modifying code is performed.
1117 */
1118int __weak ftrace_arch_code_modify_prepare(void)
1119{
1120 return 0;
1121}
1122
1123/*
1124 * archs can override this function if they must do something
1125 * after the modifying code is performed.
1126 */
1127int __weak ftrace_arch_code_modify_post_process(void)
1128{
1129 return 0;
1130}
1131
Ingo Molnare309b412008-05-12 21:20:51 +02001132static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001133{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001134 int *command = data;
1135
Steven Rostedta3583242008-11-11 15:01:42 -05001136 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001137 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001138 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001139 ftrace_replace_code(0);
1140
1141 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1142 ftrace_update_ftrace_func(ftrace_trace_function);
1143
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001144 if (*command & FTRACE_START_FUNC_RET)
1145 ftrace_enable_ftrace_graph_caller();
1146 else if (*command & FTRACE_STOP_FUNC_RET)
1147 ftrace_disable_ftrace_graph_caller();
1148
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001149 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001150}
1151
Ingo Molnare309b412008-05-12 21:20:51 +02001152static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001153{
Steven Rostedt000ab692009-02-17 13:35:06 -05001154 int ret;
1155
1156 ret = ftrace_arch_code_modify_prepare();
1157 FTRACE_WARN_ON(ret);
1158 if (ret)
1159 return;
1160
Rusty Russell784e2d72008-07-28 12:16:31 -05001161 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001162
1163 ret = ftrace_arch_code_modify_post_process();
1164 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001165}
1166
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001167static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001168static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001169
1170static void ftrace_startup_enable(int command)
1171{
1172 if (saved_ftrace_func != ftrace_trace_function) {
1173 saved_ftrace_func = ftrace_trace_function;
1174 command |= FTRACE_UPDATE_TRACE_FUNC;
1175 }
1176
1177 if (!command || !ftrace_enabled)
1178 return;
1179
1180 ftrace_run_update_code(command);
1181}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001182
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001183static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001184{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001185 if (unlikely(ftrace_disabled))
1186 return;
1187
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001188 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001189 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001190
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001191 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001192}
1193
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001194static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001195{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001196 if (unlikely(ftrace_disabled))
1197 return;
1198
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001199 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001200 /*
1201 * Just warn in case of unbalance, no need to kill ftrace, it's not
1202 * critical but the ftrace_call callers may be never nopped again after
1203 * further ftrace uses.
1204 */
1205 WARN_ON_ONCE(ftrace_start_up < 0);
1206
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001207 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001208 command |= FTRACE_DISABLE_CALLS;
1209
1210 if (saved_ftrace_func != ftrace_trace_function) {
1211 saved_ftrace_func = ftrace_trace_function;
1212 command |= FTRACE_UPDATE_TRACE_FUNC;
1213 }
1214
1215 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001216 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001217
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001218 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001219}
1220
Ingo Molnare309b412008-05-12 21:20:51 +02001221static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001222{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001223 int command = FTRACE_ENABLE_MCOUNT;
1224
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001225 if (unlikely(ftrace_disabled))
1226 return;
1227
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001228 /* Force update next time */
1229 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001230 /* ftrace_start_up is true if we want ftrace running */
1231 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001232 command |= FTRACE_ENABLE_CALLS;
1233
1234 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001235}
1236
Ingo Molnare309b412008-05-12 21:20:51 +02001237static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001238{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001239 int command = FTRACE_DISABLE_MCOUNT;
1240
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001241 if (unlikely(ftrace_disabled))
1242 return;
1243
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001244 /* ftrace_start_up is true if ftrace is running */
1245 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001246 command |= FTRACE_DISABLE_CALLS;
1247
1248 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001249}
1250
Steven Rostedt3d083392008-05-12 21:20:42 +02001251static cycle_t ftrace_update_time;
1252static unsigned long ftrace_update_cnt;
1253unsigned long ftrace_update_tot_cnt;
1254
Steven Rostedt31e88902008-11-14 16:21:19 -08001255static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001256{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001257 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301258 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001259
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001260 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001261 ftrace_update_cnt = 0;
1262
Lai Jiangshane94142a2009-03-13 17:51:27 +08001263 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301264
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001265 /* If something went wrong, bail without enabling anything */
1266 if (unlikely(ftrace_disabled))
1267 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001268
Lai Jiangshane94142a2009-03-13 17:51:27 +08001269 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001270 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001271 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301272
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001273 /*
1274 * Do the initial record convertion from mcount jump
1275 * to the NOP instructions.
1276 */
1277 if (!ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001278 ftrace_free_rec(p);
Jiri Olsa5cb084b2009-10-13 16:33:53 -04001279 continue;
1280 }
1281
1282 p->flags |= FTRACE_FL_CONVERTED;
1283 ftrace_update_cnt++;
1284
1285 /*
1286 * If the tracing is enabled, go ahead and enable the record.
1287 *
1288 * The reason not to enable the record immediatelly is the
1289 * inherent check of ftrace_make_nop/ftrace_make_call for
1290 * correct previous instructions. Making first the NOP
1291 * conversion puts the module to the correct state, thus
1292 * passing the ftrace_make_call check.
1293 */
1294 if (ftrace_start_up) {
1295 int failed = __ftrace_replace_code(p, 1);
1296 if (failed) {
1297 ftrace_bug(failed, p->ip);
1298 ftrace_free_rec(p);
1299 }
1300 }
Steven Rostedt3d083392008-05-12 21:20:42 +02001301 }
1302
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001303 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001304 ftrace_update_time = stop - start;
1305 ftrace_update_tot_cnt += ftrace_update_cnt;
1306
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001307 return 0;
1308}
1309
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001310static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001311{
1312 struct ftrace_page *pg;
1313 int cnt;
1314 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001315
1316 /* allocate a few pages */
1317 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1318 if (!ftrace_pages_start)
1319 return -1;
1320
1321 /*
1322 * Allocate a few more pages.
1323 *
1324 * TODO: have some parser search vmlinux before
1325 * final linking to find all calls to ftrace.
1326 * Then we can:
1327 * a) know how many pages to allocate.
1328 * and/or
1329 * b) set up the table then.
1330 *
1331 * The dynamic code is still necessary for
1332 * modules.
1333 */
1334
1335 pg = ftrace_pages = ftrace_pages_start;
1336
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001337 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001338 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001339 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001340
1341 for (i = 0; i < cnt; i++) {
1342 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1343
1344 /* If we fail, we'll try later anyway */
1345 if (!pg->next)
1346 break;
1347
1348 pg = pg->next;
1349 }
1350
1351 return 0;
1352}
1353
Steven Rostedt5072c592008-05-12 21:20:43 +02001354enum {
1355 FTRACE_ITER_FILTER = (1 << 0),
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001356 FTRACE_ITER_NOTRACE = (1 << 1),
1357 FTRACE_ITER_FAILURES = (1 << 2),
1358 FTRACE_ITER_PRINTALL = (1 << 3),
1359 FTRACE_ITER_HASH = (1 << 4),
Steven Rostedt5072c592008-05-12 21:20:43 +02001360};
1361
1362#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1363
1364struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001365 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001366 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001367 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001368 unsigned flags;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001369 struct trace_parser parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02001370};
1371
Ingo Molnare309b412008-05-12 21:20:51 +02001372static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001373t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1374{
1375 struct ftrace_iterator *iter = m->private;
1376 struct hlist_node *hnd = v;
1377 struct hlist_head *hhd;
1378
1379 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1380
1381 (*pos)++;
1382
1383 retry:
1384 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1385 return NULL;
1386
1387 hhd = &ftrace_func_hash[iter->hidx];
1388
1389 if (hlist_empty(hhd)) {
1390 iter->hidx++;
1391 hnd = NULL;
1392 goto retry;
1393 }
1394
1395 if (!hnd)
1396 hnd = hhd->first;
1397 else {
1398 hnd = hnd->next;
1399 if (!hnd) {
1400 iter->hidx++;
1401 goto retry;
1402 }
1403 }
1404
1405 return hnd;
1406}
1407
1408static void *t_hash_start(struct seq_file *m, loff_t *pos)
1409{
1410 struct ftrace_iterator *iter = m->private;
1411 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001412 loff_t l;
1413
1414 if (!(iter->flags & FTRACE_ITER_HASH))
1415 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001416
1417 iter->flags |= FTRACE_ITER_HASH;
1418
Li Zefand82d6242009-06-24 09:54:54 +08001419 iter->hidx = 0;
1420 for (l = 0; l <= *pos; ) {
1421 p = t_hash_next(m, p, &l);
1422 if (!p)
1423 break;
1424 }
1425 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001426}
1427
1428static int t_hash_show(struct seq_file *m, void *v)
1429{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001430 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001431 struct hlist_node *hnd = v;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001432
Steven Rostedtb6887d72009-02-17 12:32:04 -05001433 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001434
Steven Rostedt809dcf22009-02-16 23:06:01 -05001435 if (rec->ops->print)
1436 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1437
Steven Rostedtb375a112009-09-17 00:05:58 -04001438 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001439
1440 if (rec->data)
1441 seq_printf(m, ":%p", rec->data);
1442 seq_putc(m, '\n');
1443
1444 return 0;
1445}
1446
1447static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001448t_next(struct seq_file *m, void *v, loff_t *pos)
1449{
1450 struct ftrace_iterator *iter = m->private;
1451 struct dyn_ftrace *rec = NULL;
1452
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001453 if (iter->flags & FTRACE_ITER_HASH)
1454 return t_hash_next(m, v, pos);
1455
Steven Rostedt5072c592008-05-12 21:20:43 +02001456 (*pos)++;
1457
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001458 if (iter->flags & FTRACE_ITER_PRINTALL)
1459 return NULL;
1460
Steven Rostedt5072c592008-05-12 21:20:43 +02001461 retry:
1462 if (iter->idx >= iter->pg->index) {
1463 if (iter->pg->next) {
1464 iter->pg = iter->pg->next;
1465 iter->idx = 0;
1466 goto retry;
1467 }
1468 } else {
1469 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001470 if ((rec->flags & FTRACE_FL_FREE) ||
1471
1472 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301473 (rec->flags & FTRACE_FL_FAILED)) ||
1474
1475 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001476 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301477
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001478 ((iter->flags & FTRACE_ITER_FILTER) &&
1479 !(rec->flags & FTRACE_FL_FILTER)) ||
1480
Steven Rostedt41c52c02008-05-22 11:46:33 -04001481 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1482 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001483 rec = NULL;
1484 goto retry;
1485 }
1486 }
1487
Steven Rostedt5072c592008-05-12 21:20:43 +02001488 return rec;
1489}
1490
1491static void *t_start(struct seq_file *m, loff_t *pos)
1492{
1493 struct ftrace_iterator *iter = m->private;
1494 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001495 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001496
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001497 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001498 /*
1499 * For set_ftrace_filter reading, if we have the filter
1500 * off, we can short cut and just print out that all
1501 * functions are enabled.
1502 */
1503 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1504 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001505 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001506 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001507 return iter;
1508 }
1509
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001510 if (iter->flags & FTRACE_ITER_HASH)
1511 return t_hash_start(m, pos);
1512
Li Zefan694ce0a2009-06-24 09:54:19 +08001513 iter->pg = ftrace_pages_start;
1514 iter->idx = 0;
1515 for (l = 0; l <= *pos; ) {
1516 p = t_next(m, p, &l);
1517 if (!p)
1518 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001519 }
walimis5821e1b2008-11-15 15:19:06 +08001520
Li Zefan694ce0a2009-06-24 09:54:19 +08001521 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001522 return t_hash_start(m, pos);
1523
Steven Rostedt5072c592008-05-12 21:20:43 +02001524 return p;
1525}
1526
1527static void t_stop(struct seq_file *m, void *p)
1528{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001529 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001530}
1531
1532static int t_show(struct seq_file *m, void *v)
1533{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001534 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001535 struct dyn_ftrace *rec = v;
Steven Rostedt5072c592008-05-12 21:20:43 +02001536
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001537 if (iter->flags & FTRACE_ITER_HASH)
1538 return t_hash_show(m, v);
1539
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001540 if (iter->flags & FTRACE_ITER_PRINTALL) {
1541 seq_printf(m, "#### all functions enabled ####\n");
1542 return 0;
1543 }
1544
Steven Rostedt5072c592008-05-12 21:20:43 +02001545 if (!rec)
1546 return 0;
1547
Steven Rostedtb375a112009-09-17 00:05:58 -04001548 seq_printf(m, "%ps\n", (void *)rec->ip);
Steven Rostedt5072c592008-05-12 21:20:43 +02001549
1550 return 0;
1551}
1552
James Morris88e9d342009-09-22 16:43:43 -07001553static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02001554 .start = t_start,
1555 .next = t_next,
1556 .stop = t_stop,
1557 .show = t_show,
1558};
1559
Ingo Molnare309b412008-05-12 21:20:51 +02001560static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001561ftrace_avail_open(struct inode *inode, struct file *file)
1562{
1563 struct ftrace_iterator *iter;
1564 int ret;
1565
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001566 if (unlikely(ftrace_disabled))
1567 return -ENODEV;
1568
Steven Rostedt5072c592008-05-12 21:20:43 +02001569 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1570 if (!iter)
1571 return -ENOMEM;
1572
1573 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001574
1575 ret = seq_open(file, &show_ftrace_seq_ops);
1576 if (!ret) {
1577 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001578
Steven Rostedt5072c592008-05-12 21:20:43 +02001579 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001580 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001581 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001582 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001583
1584 return ret;
1585}
1586
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301587static int
1588ftrace_failures_open(struct inode *inode, struct file *file)
1589{
1590 int ret;
1591 struct seq_file *m;
1592 struct ftrace_iterator *iter;
1593
1594 ret = ftrace_avail_open(inode, file);
1595 if (!ret) {
1596 m = (struct seq_file *)file->private_data;
1597 iter = (struct ftrace_iterator *)m->private;
1598 iter->flags = FTRACE_ITER_FAILURES;
1599 }
1600
1601 return ret;
1602}
1603
1604
Steven Rostedt41c52c02008-05-22 11:46:33 -04001605static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001606{
1607 struct ftrace_page *pg;
1608 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001609 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001610
Steven Rostedt52baf112009-02-14 01:15:39 -05001611 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001612 if (enable)
1613 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001614 do_for_each_ftrace_rec(pg, rec) {
1615 if (rec->flags & FTRACE_FL_FAILED)
1616 continue;
1617 rec->flags &= ~type;
1618 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001619 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001620}
1621
Ingo Molnare309b412008-05-12 21:20:51 +02001622static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001623ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001624{
1625 struct ftrace_iterator *iter;
1626 int ret = 0;
1627
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001628 if (unlikely(ftrace_disabled))
1629 return -ENODEV;
1630
Steven Rostedt5072c592008-05-12 21:20:43 +02001631 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1632 if (!iter)
1633 return -ENOMEM;
1634
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02001635 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1636 kfree(iter);
1637 return -ENOMEM;
1638 }
1639
Steven Rostedt41c52c02008-05-22 11:46:33 -04001640 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001641 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04001642 (file->f_flags & O_TRUNC))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001643 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001644
1645 if (file->f_mode & FMODE_READ) {
1646 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001647 iter->flags = enable ? FTRACE_ITER_FILTER :
1648 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001649
1650 ret = seq_open(file, &show_ftrace_seq_ops);
1651 if (!ret) {
1652 struct seq_file *m = file->private_data;
1653 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08001654 } else {
1655 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02001656 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08001657 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001658 } else
1659 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001660 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001661
1662 return ret;
1663}
1664
Steven Rostedt41c52c02008-05-22 11:46:33 -04001665static int
1666ftrace_filter_open(struct inode *inode, struct file *file)
1667{
1668 return ftrace_regex_open(inode, file, 1);
1669}
1670
1671static int
1672ftrace_notrace_open(struct inode *inode, struct file *file)
1673{
1674 return ftrace_regex_open(inode, file, 0);
1675}
1676
Ingo Molnare309b412008-05-12 21:20:51 +02001677static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001678ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001679{
1680 loff_t ret;
1681
1682 if (file->f_mode & FMODE_READ)
1683 ret = seq_lseek(file, offset, origin);
1684 else
1685 file->f_pos = ret = 1;
1686
1687 return ret;
1688}
1689
Steven Rostedt64e7c442009-02-13 17:08:48 -05001690static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001691{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001692 int matched = 0;
1693 char *ptr;
1694
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001695 switch (type) {
1696 case MATCH_FULL:
1697 if (strcmp(str, regex) == 0)
1698 matched = 1;
1699 break;
1700 case MATCH_FRONT_ONLY:
1701 if (strncmp(str, regex, len) == 0)
1702 matched = 1;
1703 break;
1704 case MATCH_MIDDLE_ONLY:
1705 if (strstr(str, regex))
1706 matched = 1;
1707 break;
1708 case MATCH_END_ONLY:
1709 ptr = strstr(str, regex);
1710 if (ptr && (ptr[len] == 0))
1711 matched = 1;
1712 break;
1713 }
1714
1715 return matched;
1716}
1717
Steven Rostedt64e7c442009-02-13 17:08:48 -05001718static int
1719ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1720{
1721 char str[KSYM_SYMBOL_LEN];
1722
1723 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1724 return ftrace_match(str, regex, len, type);
1725}
1726
Li Zefan311d16d2009-12-08 11:15:11 +08001727static int ftrace_match_records(char *buff, int len, int enable)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001728{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001729 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001730 struct ftrace_page *pg;
1731 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001732 unsigned long flag;
1733 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001734 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001735 int not;
Li Zefan311d16d2009-12-08 11:15:11 +08001736 int found = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001737
Steven Rostedt6a24a242009-02-17 11:20:26 -05001738 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001739 type = filter_parse_regex(buff, len, &search, &not);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001740
1741 search_len = strlen(search);
1742
Steven Rostedt52baf112009-02-14 01:15:39 -05001743 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001744 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001745
Steven Rostedt265c8312009-02-13 12:43:56 -05001746 if (rec->flags & FTRACE_FL_FAILED)
1747 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001748
1749 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001750 if (not)
1751 rec->flags &= ~flag;
1752 else
1753 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001754 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001755 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001756 /*
1757 * Only enable filtering if we have a function that
1758 * is filtered on.
1759 */
1760 if (enable && (rec->flags & FTRACE_FL_FILTER))
1761 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001762 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001763 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001764
1765 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02001766}
1767
Steven Rostedt64e7c442009-02-13 17:08:48 -05001768static int
1769ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1770 char *regex, int len, int type)
1771{
1772 char str[KSYM_SYMBOL_LEN];
1773 char *modname;
1774
1775 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1776
1777 if (!modname || strcmp(modname, mod))
1778 return 0;
1779
1780 /* blank search means to match all funcs in the mod */
1781 if (len)
1782 return ftrace_match(str, regex, len, type);
1783 else
1784 return 1;
1785}
1786
Li Zefan311d16d2009-12-08 11:15:11 +08001787static int ftrace_match_module_records(char *buff, char *mod, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05001788{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001789 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001790 struct ftrace_page *pg;
1791 struct dyn_ftrace *rec;
1792 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001793 char *search = buff;
1794 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001795 int not = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08001796 int found = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001797
Steven Rostedt6a24a242009-02-17 11:20:26 -05001798 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1799
Steven Rostedt64e7c442009-02-13 17:08:48 -05001800 /* blank or '*' mean the same */
1801 if (strcmp(buff, "*") == 0)
1802 buff[0] = 0;
1803
1804 /* handle the case of 'dont filter this module' */
1805 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1806 buff[0] = 0;
1807 not = 1;
1808 }
1809
1810 if (strlen(buff)) {
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001811 type = filter_parse_regex(buff, strlen(buff), &search, &not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001812 search_len = strlen(search);
1813 }
1814
Steven Rostedt52baf112009-02-14 01:15:39 -05001815 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001816 do_for_each_ftrace_rec(pg, rec) {
1817
1818 if (rec->flags & FTRACE_FL_FAILED)
1819 continue;
1820
1821 if (ftrace_match_module_record(rec, mod,
1822 search, search_len, type)) {
1823 if (not)
1824 rec->flags &= ~flag;
1825 else
1826 rec->flags |= flag;
Li Zefan311d16d2009-12-08 11:15:11 +08001827 found = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001828 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001829 if (enable && (rec->flags & FTRACE_FL_FILTER))
1830 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001831
1832 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001833 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08001834
1835 return found;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001836}
1837
Steven Rostedtf6180772009-02-14 00:40:25 -05001838/*
1839 * We register the module command as a template to show others how
1840 * to register the a command as well.
1841 */
1842
1843static int
1844ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1845{
1846 char *mod;
1847
1848 /*
1849 * cmd == 'mod' because we only registered this func
1850 * for the 'mod' ftrace_func_command.
1851 * But if you register one func with multiple commands,
1852 * you can tell which command was used by the cmd
1853 * parameter.
1854 */
1855
1856 /* we must have a module name */
1857 if (!param)
1858 return -EINVAL;
1859
1860 mod = strsep(&param, ":");
1861 if (!strlen(mod))
1862 return -EINVAL;
1863
Li Zefan311d16d2009-12-08 11:15:11 +08001864 if (ftrace_match_module_records(func, mod, enable))
1865 return 0;
1866 return -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05001867}
1868
1869static struct ftrace_func_command ftrace_mod_cmd = {
1870 .name = "mod",
1871 .func = ftrace_mod_callback,
1872};
1873
1874static int __init ftrace_mod_cmd_init(void)
1875{
1876 return register_ftrace_command(&ftrace_mod_cmd);
1877}
1878device_initcall(ftrace_mod_cmd_init);
1879
Steven Rostedt59df055f2009-02-14 15:29:06 -05001880static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001881function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001882{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001883 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001884 struct hlist_head *hhd;
1885 struct hlist_node *n;
1886 unsigned long key;
1887 int resched;
1888
1889 key = hash_long(ip, FTRACE_HASH_BITS);
1890
1891 hhd = &ftrace_func_hash[key];
1892
1893 if (hlist_empty(hhd))
1894 return;
1895
1896 /*
1897 * Disable preemption for these calls to prevent a RCU grace
1898 * period. This syncs the hash iteration and freeing of items
1899 * on the hash. rcu_read_lock is too dangerous here.
1900 */
1901 resched = ftrace_preempt_disable();
1902 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1903 if (entry->ip == ip)
1904 entry->ops->func(ip, parent_ip, &entry->data);
1905 }
1906 ftrace_preempt_enable(resched);
1907}
1908
Steven Rostedtb6887d72009-02-17 12:32:04 -05001909static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001910{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001911 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001912};
1913
Steven Rostedtb6887d72009-02-17 12:32:04 -05001914static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001915
Steven Rostedtb6887d72009-02-17 12:32:04 -05001916static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001917{
1918 int i;
1919
Steven Rostedtb6887d72009-02-17 12:32:04 -05001920 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001921 return;
1922
1923 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1924 struct hlist_head *hhd = &ftrace_func_hash[i];
1925 if (hhd->first)
1926 break;
1927 }
1928 /* Nothing registered? */
1929 if (i == FTRACE_FUNC_HASHSIZE)
1930 return;
1931
Steven Rostedtb6887d72009-02-17 12:32:04 -05001932 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001933 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001934 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001935}
1936
Steven Rostedtb6887d72009-02-17 12:32:04 -05001937static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001938{
1939 int i;
1940
Steven Rostedtb6887d72009-02-17 12:32:04 -05001941 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001942 return;
1943
1944 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1945 struct hlist_head *hhd = &ftrace_func_hash[i];
1946 if (hhd->first)
1947 return;
1948 }
1949
1950 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001951 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001952 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001953 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001954}
1955
1956
1957static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1958{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001959 struct ftrace_func_probe *entry =
1960 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001961
1962 if (entry->ops->free)
1963 entry->ops->free(&entry->data);
1964 kfree(entry);
1965}
1966
1967
1968int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001969register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001970 void *data)
1971{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001972 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001973 struct ftrace_page *pg;
1974 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001975 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001976 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001977 int count = 0;
1978 char *search;
1979
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02001980 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001981 len = strlen(search);
1982
Steven Rostedtb6887d72009-02-17 12:32:04 -05001983 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001984 if (WARN_ON(not))
1985 return -EINVAL;
1986
1987 mutex_lock(&ftrace_lock);
1988 do_for_each_ftrace_rec(pg, rec) {
1989
1990 if (rec->flags & FTRACE_FL_FAILED)
1991 continue;
1992
1993 if (!ftrace_match_record(rec, search, len, type))
1994 continue;
1995
1996 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1997 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001998 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001999 if (!count)
2000 count = -ENOMEM;
2001 goto out_unlock;
2002 }
2003
2004 count++;
2005
2006 entry->data = data;
2007
2008 /*
2009 * The caller might want to do something special
2010 * for each function we find. We call the callback
2011 * to give the caller an opportunity to do so.
2012 */
2013 if (ops->callback) {
2014 if (ops->callback(rec->ip, &entry->data) < 0) {
2015 /* caller does not like this func */
2016 kfree(entry);
2017 continue;
2018 }
2019 }
2020
2021 entry->ops = ops;
2022 entry->ip = rec->ip;
2023
2024 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2025 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2026
2027 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002028 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002029
2030 out_unlock:
2031 mutex_unlock(&ftrace_lock);
2032
2033 return count;
2034}
2035
2036enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002037 PROBE_TEST_FUNC = 1,
2038 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002039};
2040
2041static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002042__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002043 void *data, int flags)
2044{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002045 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002046 struct hlist_node *n, *tmp;
2047 char str[KSYM_SYMBOL_LEN];
2048 int type = MATCH_FULL;
2049 int i, len = 0;
2050 char *search;
2051
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002052 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002053 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002054 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002055 int not;
2056
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002057 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002058 len = strlen(search);
2059
Steven Rostedtb6887d72009-02-17 12:32:04 -05002060 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002061 if (WARN_ON(not))
2062 return;
2063 }
2064
2065 mutex_lock(&ftrace_lock);
2066 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2067 struct hlist_head *hhd = &ftrace_func_hash[i];
2068
2069 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2070
2071 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002072 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002073 continue;
2074
Steven Rostedtb6887d72009-02-17 12:32:04 -05002075 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002076 continue;
2077
2078 /* do this last, since it is the most expensive */
2079 if (glob) {
2080 kallsyms_lookup(entry->ip, NULL, NULL,
2081 NULL, str);
2082 if (!ftrace_match(str, glob, len, type))
2083 continue;
2084 }
2085
2086 hlist_del(&entry->node);
2087 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2088 }
2089 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002090 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002091 mutex_unlock(&ftrace_lock);
2092}
2093
2094void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002095unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002096 void *data)
2097{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002098 __unregister_ftrace_function_probe(glob, ops, data,
2099 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002100}
2101
2102void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002103unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002104{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002105 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002106}
2107
Steven Rostedtb6887d72009-02-17 12:32:04 -05002108void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002109{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002110 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002111}
2112
Steven Rostedtf6180772009-02-14 00:40:25 -05002113static LIST_HEAD(ftrace_commands);
2114static DEFINE_MUTEX(ftrace_cmd_mutex);
2115
2116int register_ftrace_command(struct ftrace_func_command *cmd)
2117{
2118 struct ftrace_func_command *p;
2119 int ret = 0;
2120
2121 mutex_lock(&ftrace_cmd_mutex);
2122 list_for_each_entry(p, &ftrace_commands, list) {
2123 if (strcmp(cmd->name, p->name) == 0) {
2124 ret = -EBUSY;
2125 goto out_unlock;
2126 }
2127 }
2128 list_add(&cmd->list, &ftrace_commands);
2129 out_unlock:
2130 mutex_unlock(&ftrace_cmd_mutex);
2131
2132 return ret;
2133}
2134
2135int unregister_ftrace_command(struct ftrace_func_command *cmd)
2136{
2137 struct ftrace_func_command *p, *n;
2138 int ret = -ENODEV;
2139
2140 mutex_lock(&ftrace_cmd_mutex);
2141 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2142 if (strcmp(cmd->name, p->name) == 0) {
2143 ret = 0;
2144 list_del_init(&p->list);
2145 goto out_unlock;
2146 }
2147 }
2148 out_unlock:
2149 mutex_unlock(&ftrace_cmd_mutex);
2150
2151 return ret;
2152}
2153
Steven Rostedt64e7c442009-02-13 17:08:48 -05002154static int ftrace_process_regex(char *buff, int len, int enable)
2155{
Steven Rostedtf6180772009-02-14 00:40:25 -05002156 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002157 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002158 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002159
2160 func = strsep(&next, ":");
2161
2162 if (!next) {
Li Zefan311d16d2009-12-08 11:15:11 +08002163 if (ftrace_match_records(func, len, enable))
2164 return 0;
2165 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002166 }
2167
Steven Rostedtf6180772009-02-14 00:40:25 -05002168 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002169
2170 command = strsep(&next, ":");
2171
Steven Rostedtf6180772009-02-14 00:40:25 -05002172 mutex_lock(&ftrace_cmd_mutex);
2173 list_for_each_entry(p, &ftrace_commands, list) {
2174 if (strcmp(p->name, command) == 0) {
2175 ret = p->func(func, command, next, enable);
2176 goto out_unlock;
2177 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002178 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002179 out_unlock:
2180 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002181
Steven Rostedtf6180772009-02-14 00:40:25 -05002182 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002183}
2184
Ingo Molnare309b412008-05-12 21:20:51 +02002185static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002186ftrace_regex_write(struct file *file, const char __user *ubuf,
2187 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002188{
2189 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002190 struct trace_parser *parser;
2191 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02002192
Li Zefan4ba79782009-09-22 13:52:20 +08002193 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02002194 return 0;
2195
Steven Rostedt41c52c02008-05-22 11:46:33 -04002196 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002197
2198 if (file->f_mode & FMODE_READ) {
2199 struct seq_file *m = file->private_data;
2200 iter = m->private;
2201 } else
2202 iter = file->private_data;
2203
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002204 parser = &iter->parser;
2205 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02002206
Li Zefan4ba79782009-09-22 13:52:20 +08002207 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002208 !trace_parser_cont(parser)) {
2209 ret = ftrace_process_regex(parser->buffer,
2210 parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002211 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08002212 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002213
2214 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002215 }
2216
Steven Rostedt5072c592008-05-12 21:20:43 +02002217 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08002218out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002219 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08002220
Steven Rostedt5072c592008-05-12 21:20:43 +02002221 return ret;
2222}
2223
Steven Rostedt41c52c02008-05-22 11:46:33 -04002224static ssize_t
2225ftrace_filter_write(struct file *file, const char __user *ubuf,
2226 size_t cnt, loff_t *ppos)
2227{
2228 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2229}
2230
2231static ssize_t
2232ftrace_notrace_write(struct file *file, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2234{
2235 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2236}
2237
2238static void
2239ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2240{
2241 if (unlikely(ftrace_disabled))
2242 return;
2243
2244 mutex_lock(&ftrace_regex_lock);
2245 if (reset)
2246 ftrace_filter_reset(enable);
2247 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002248 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002249 mutex_unlock(&ftrace_regex_lock);
2250}
2251
Steven Rostedt77a2b372008-05-12 21:20:45 +02002252/**
2253 * ftrace_set_filter - set a function to filter on in ftrace
2254 * @buf - the string that holds the function filter text.
2255 * @len - the length of the string.
2256 * @reset - non zero to reset all filters before applying this filter.
2257 *
2258 * Filters denote which functions should be enabled when tracing is enabled.
2259 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2260 */
Ingo Molnare309b412008-05-12 21:20:51 +02002261void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002262{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002263 ftrace_set_regex(buf, len, reset, 1);
2264}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002265
Steven Rostedt41c52c02008-05-22 11:46:33 -04002266/**
2267 * ftrace_set_notrace - set a function to not trace in ftrace
2268 * @buf - the string that holds the function notrace text.
2269 * @len - the length of the string.
2270 * @reset - non zero to reset all filters before applying this filter.
2271 *
2272 * Notrace Filters denote which functions should not be enabled when tracing
2273 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2274 * for tracing.
2275 */
2276void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2277{
2278 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002279}
2280
Steven Rostedt2af15d62009-05-28 13:37:24 -04002281/*
2282 * command line interface to allow users to set filters on boot up.
2283 */
2284#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2285static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2286static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2287
2288static int __init set_ftrace_notrace(char *str)
2289{
2290 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2291 return 1;
2292}
2293__setup("ftrace_notrace=", set_ftrace_notrace);
2294
2295static int __init set_ftrace_filter(char *str)
2296{
2297 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2298 return 1;
2299}
2300__setup("ftrace_filter=", set_ftrace_filter);
2301
Stefan Assmann369bc182009-10-12 22:17:21 +02002302#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08002303static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Stefan Assmann369bc182009-10-12 22:17:21 +02002304static int __init set_graph_function(char *str)
2305{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02002306 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02002307 return 1;
2308}
2309__setup("ftrace_graph_filter=", set_graph_function);
2310
2311static void __init set_ftrace_early_graph(char *buf)
2312{
2313 int ret;
2314 char *func;
2315
2316 while (buf) {
2317 func = strsep(&buf, ",");
2318 /* we allow only one expression at a time */
2319 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2320 func);
2321 if (ret)
2322 printk(KERN_DEBUG "ftrace: function %s not "
2323 "traceable\n", func);
2324 }
2325}
2326#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2327
Steven Rostedt2af15d62009-05-28 13:37:24 -04002328static void __init set_ftrace_early_filter(char *buf, int enable)
2329{
2330 char *func;
2331
2332 while (buf) {
2333 func = strsep(&buf, ",");
2334 ftrace_set_regex(func, strlen(func), 0, enable);
2335 }
2336}
2337
2338static void __init set_ftrace_early_filters(void)
2339{
2340 if (ftrace_filter_buf[0])
2341 set_ftrace_early_filter(ftrace_filter_buf, 1);
2342 if (ftrace_notrace_buf[0])
2343 set_ftrace_early_filter(ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02002344#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2345 if (ftrace_graph_buf[0])
2346 set_ftrace_early_graph(ftrace_graph_buf);
2347#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04002348}
2349
Ingo Molnare309b412008-05-12 21:20:51 +02002350static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002351ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002352{
2353 struct seq_file *m = (struct seq_file *)file->private_data;
2354 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002355 struct trace_parser *parser;
Steven Rostedt5072c592008-05-12 21:20:43 +02002356
Steven Rostedt41c52c02008-05-22 11:46:33 -04002357 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002358 if (file->f_mode & FMODE_READ) {
2359 iter = m->private;
2360
2361 seq_release(inode, file);
2362 } else
2363 iter = file->private_data;
2364
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002365 parser = &iter->parser;
2366 if (trace_parser_loaded(parser)) {
2367 parser->buffer[parser->idx] = 0;
2368 ftrace_match_records(parser->buffer, parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002369 }
2370
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002371 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002372 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002373 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002374 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002375
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002376 trace_parser_put(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002377 kfree(iter);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002378
Steven Rostedt41c52c02008-05-22 11:46:33 -04002379 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002380 return 0;
2381}
2382
Steven Rostedt41c52c02008-05-22 11:46:33 -04002383static int
2384ftrace_filter_release(struct inode *inode, struct file *file)
2385{
2386 return ftrace_regex_release(inode, file, 1);
2387}
2388
2389static int
2390ftrace_notrace_release(struct inode *inode, struct file *file)
2391{
2392 return ftrace_regex_release(inode, file, 0);
2393}
2394
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002395static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002396 .open = ftrace_avail_open,
2397 .read = seq_read,
2398 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002399 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02002400};
2401
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002402static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302403 .open = ftrace_failures_open,
2404 .read = seq_read,
2405 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08002406 .release = seq_release_private,
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302407};
2408
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002409static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002410 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002411 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002412 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002413 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002414 .release = ftrace_filter_release,
2415};
2416
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002417static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002418 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002419 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002420 .write = ftrace_notrace_write,
2421 .llseek = ftrace_regex_lseek,
2422 .release = ftrace_notrace_release,
2423};
2424
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002425#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2426
2427static DEFINE_MUTEX(graph_lock);
2428
2429int ftrace_graph_count;
2430unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2431
2432static void *
Li Zefan85951842009-06-24 09:54:00 +08002433__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002434{
Li Zefan85951842009-06-24 09:54:00 +08002435 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002436 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08002437 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08002438}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002439
Li Zefan85951842009-06-24 09:54:00 +08002440static void *
2441g_next(struct seq_file *m, void *v, loff_t *pos)
2442{
2443 (*pos)++;
2444 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002445}
2446
2447static void *g_start(struct seq_file *m, loff_t *pos)
2448{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002449 mutex_lock(&graph_lock);
2450
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002451 /* Nothing, tell g_show to print all functions are enabled */
2452 if (!ftrace_graph_count && !*pos)
2453 return (void *)1;
2454
Li Zefan85951842009-06-24 09:54:00 +08002455 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002456}
2457
2458static void g_stop(struct seq_file *m, void *p)
2459{
2460 mutex_unlock(&graph_lock);
2461}
2462
2463static int g_show(struct seq_file *m, void *v)
2464{
2465 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002466
2467 if (!ptr)
2468 return 0;
2469
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002470 if (ptr == (unsigned long *)1) {
2471 seq_printf(m, "#### all functions enabled ####\n");
2472 return 0;
2473 }
2474
Steven Rostedtb375a112009-09-17 00:05:58 -04002475 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002476
2477 return 0;
2478}
2479
James Morris88e9d342009-09-22 16:43:43 -07002480static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002481 .start = g_start,
2482 .next = g_next,
2483 .stop = g_stop,
2484 .show = g_show,
2485};
2486
2487static int
2488ftrace_graph_open(struct inode *inode, struct file *file)
2489{
2490 int ret = 0;
2491
2492 if (unlikely(ftrace_disabled))
2493 return -ENODEV;
2494
2495 mutex_lock(&graph_lock);
2496 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002497 (file->f_flags & O_TRUNC)) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002498 ftrace_graph_count = 0;
2499 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2500 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002501 mutex_unlock(&graph_lock);
2502
Li Zefana4ec5e02009-09-18 14:06:28 +08002503 if (file->f_mode & FMODE_READ)
2504 ret = seq_open(file, &ftrace_graph_seq_ops);
2505
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002506 return ret;
2507}
2508
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002509static int
Li Zefan87827112009-07-23 11:29:11 +08002510ftrace_graph_release(struct inode *inode, struct file *file)
2511{
2512 if (file->f_mode & FMODE_READ)
2513 seq_release(inode, file);
2514 return 0;
2515}
2516
2517static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002518ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002519{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002520 struct dyn_ftrace *rec;
2521 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002522 int search_len;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002523 int found = 0;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002524 int type, not;
2525 char *search;
2526 bool exists;
2527 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002528
2529 if (ftrace_disabled)
2530 return -ENODEV;
2531
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002532 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002533 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002534 if (not)
2535 return -EINVAL;
2536
2537 search_len = strlen(search);
2538
Steven Rostedt52baf112009-02-14 01:15:39 -05002539 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002540 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002541
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002542 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2543 break;
2544
Steven Rostedt265c8312009-02-13 12:43:56 -05002545 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2546 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002547
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002548 if (ftrace_match_record(rec, search, search_len, type)) {
2549 /* ensure it is not already in the array */
2550 exists = false;
2551 for (i = 0; i < *idx; i++)
2552 if (array[i] == rec->ip) {
2553 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002554 break;
2555 }
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002556 if (!exists) {
2557 array[(*idx)++] = rec->ip;
2558 found = 1;
2559 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002560 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002561 } while_for_each_ftrace_rec();
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002562
Steven Rostedt52baf112009-02-14 01:15:39 -05002563 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002564
2565 return found ? 0 : -EINVAL;
2566}
2567
2568static ssize_t
2569ftrace_graph_write(struct file *file, const char __user *ubuf,
2570 size_t cnt, loff_t *ppos)
2571{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002572 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08002573 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002574
2575 if (!cnt || cnt < 0)
2576 return 0;
2577
2578 mutex_lock(&graph_lock);
2579
2580 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2581 ret = -EBUSY;
Li Zefan1eb90f12009-09-22 13:52:57 +08002582 goto out_unlock;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002583 }
2584
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002585 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2586 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08002587 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002588 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002589
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002590 read = trace_get_user(&parser, ubuf, cnt, ppos);
2591
Li Zefan4ba79782009-09-22 13:52:20 +08002592 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002593 parser.buffer[parser.idx] = 0;
2594
2595 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08002596 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002597 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002598 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08002599 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002600 }
2601
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002602 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08002603
2604out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002605 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08002606out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002607 mutex_unlock(&graph_lock);
2608
2609 return ret;
2610}
2611
2612static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08002613 .open = ftrace_graph_open,
2614 .read = seq_read,
2615 .write = ftrace_graph_write,
2616 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002617};
2618#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2619
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002620static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002621{
Steven Rostedt5072c592008-05-12 21:20:43 +02002622
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002623 trace_create_file("available_filter_functions", 0444,
2624 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002625
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002626 trace_create_file("failures", 0444,
2627 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302628
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002629 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2630 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002631
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002632 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002633 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002634
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002635#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002636 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002637 NULL,
2638 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002639#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2640
Steven Rostedt5072c592008-05-12 21:20:43 +02002641 return 0;
2642}
2643
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002644static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08002645 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002646 unsigned long *end)
2647{
2648 unsigned long *p;
2649 unsigned long addr;
2650 unsigned long flags;
2651
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002652 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002653 p = start;
2654 while (p < end) {
2655 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002656 /*
2657 * Some architecture linkers will pad between
2658 * the different mcount_loc sections of different
2659 * object files to satisfy alignments.
2660 * Skip any NULL pointers.
2661 */
2662 if (!addr)
2663 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002664 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002665 }
2666
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002667 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002668 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002669 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002670 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002671 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002672
2673 return 0;
2674}
2675
Steven Rostedt93eb6772009-04-15 13:24:06 -04002676#ifdef CONFIG_MODULES
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002677void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002678{
2679 struct dyn_ftrace *rec;
2680 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04002681
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002682 if (ftrace_disabled)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002683 return;
2684
2685 mutex_lock(&ftrace_lock);
2686 do_for_each_ftrace_rec(pg, rec) {
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002687 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04002688 /*
2689 * rec->ip is changed in ftrace_free_rec()
2690 * It should not between s and e if record was freed.
2691 */
2692 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2693 ftrace_free_rec(rec);
2694 }
2695 } while_for_each_ftrace_rec();
2696 mutex_unlock(&ftrace_lock);
2697}
2698
2699static void ftrace_init_module(struct module *mod,
2700 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002701{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002702 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002703 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002704 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002705}
2706
Steven Rostedt93eb6772009-04-15 13:24:06 -04002707static int ftrace_module_notify(struct notifier_block *self,
2708 unsigned long val, void *data)
2709{
2710 struct module *mod = data;
2711
2712 switch (val) {
2713 case MODULE_STATE_COMING:
2714 ftrace_init_module(mod, mod->ftrace_callsites,
2715 mod->ftrace_callsites +
2716 mod->num_ftrace_callsites);
2717 break;
2718 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02002719 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04002720 break;
2721 }
2722
2723 return 0;
2724}
2725#else
2726static int ftrace_module_notify(struct notifier_block *self,
2727 unsigned long val, void *data)
2728{
2729 return 0;
2730}
2731#endif /* CONFIG_MODULES */
2732
2733struct notifier_block ftrace_module_nb = {
2734 .notifier_call = ftrace_module_notify,
2735 .priority = 0,
2736};
2737
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002738extern unsigned long __start_mcount_loc[];
2739extern unsigned long __stop_mcount_loc[];
2740
2741void __init ftrace_init(void)
2742{
2743 unsigned long count, addr, flags;
2744 int ret;
2745
2746 /* Keep the ftrace pointer to the stub */
2747 addr = (unsigned long)ftrace_stub;
2748
2749 local_irq_save(flags);
2750 ftrace_dyn_arch_init(&addr);
2751 local_irq_restore(flags);
2752
2753 /* ftrace_dyn_arch_init places the return code in addr */
2754 if (addr)
2755 goto failed;
2756
2757 count = __stop_mcount_loc - __start_mcount_loc;
2758
2759 ret = ftrace_dyn_table_alloc(count);
2760 if (ret)
2761 goto failed;
2762
2763 last_ftrace_enabled = ftrace_enabled = 1;
2764
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002765 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08002766 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002767 __stop_mcount_loc);
2768
Steven Rostedt93eb6772009-04-15 13:24:06 -04002769 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002770 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002771 pr_warning("Failed to register trace ftrace module notifier\n");
2772
Steven Rostedt2af15d62009-05-28 13:37:24 -04002773 set_ftrace_early_filters();
2774
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002775 return;
2776 failed:
2777 ftrace_disabled = 1;
2778}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002779
Steven Rostedt3d083392008-05-12 21:20:42 +02002780#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002781
2782static int __init ftrace_nodyn_init(void)
2783{
2784 ftrace_enabled = 1;
2785 return 0;
2786}
2787device_initcall(ftrace_nodyn_init);
2788
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002789static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2790static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002791/* Keep as macros so we do not need to define the commands */
2792# define ftrace_startup(command) do { } while (0)
2793# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002794# define ftrace_startup_sysctl() do { } while (0)
2795# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002796#endif /* CONFIG_DYNAMIC_FTRACE */
2797
Steven Rostedte32d8952008-12-04 00:26:41 -05002798static void clear_ftrace_swapper(void)
2799{
2800 struct task_struct *p;
2801 int cpu;
2802
2803 get_online_cpus();
2804 for_each_online_cpu(cpu) {
2805 p = idle_task(cpu);
2806 clear_tsk_trace_trace(p);
2807 }
2808 put_online_cpus();
2809}
2810
2811static void set_ftrace_swapper(void)
2812{
2813 struct task_struct *p;
2814 int cpu;
2815
2816 get_online_cpus();
2817 for_each_online_cpu(cpu) {
2818 p = idle_task(cpu);
2819 set_tsk_trace_trace(p);
2820 }
2821 put_online_cpus();
2822}
2823
2824static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002825{
2826 struct task_struct *p;
2827
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002828 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002829 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002830 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002831 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002832 rcu_read_unlock();
2833
Steven Rostedte32d8952008-12-04 00:26:41 -05002834 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002835}
2836
Steven Rostedte32d8952008-12-04 00:26:41 -05002837static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002838{
2839 struct task_struct *p;
2840
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002841 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002842 do_each_pid_task(pid, PIDTYPE_PID, p) {
2843 set_tsk_trace_trace(p);
2844 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002845 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002846}
2847
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002848static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002849{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002850 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05002851 clear_ftrace_swapper();
2852 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002853 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05002854}
2855
2856static void set_ftrace_pid_task(struct pid *pid)
2857{
2858 if (pid == ftrace_swapper_pid)
2859 set_ftrace_swapper();
2860 else
2861 set_ftrace_pid(pid);
2862}
2863
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04002864static int ftrace_pid_add(int p)
2865{
2866 struct pid *pid;
2867 struct ftrace_pid *fpid;
2868 int ret = -EINVAL;
2869
2870 mutex_lock(&ftrace_lock);
2871
2872 if (!p)
2873 pid = ftrace_swapper_pid;
2874 else
2875 pid = find_get_pid(p);
2876
2877 if (!pid)
2878 goto out;
2879
2880 ret = 0;
2881
2882 list_for_each_entry(fpid, &ftrace_pids, list)
2883 if (fpid->pid == pid)
2884 goto out_put;
2885
2886 ret = -ENOMEM;
2887
2888 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
2889 if (!fpid)
2890 goto out_put;
2891
2892 list_add(&fpid->list, &ftrace_pids);
2893 fpid->pid = pid;
2894
2895 set_ftrace_pid_task(pid);
2896
2897 ftrace_update_pid_func();
2898 ftrace_startup_enable(0);
2899
2900 mutex_unlock(&ftrace_lock);
2901 return 0;
2902
2903out_put:
2904 if (pid != ftrace_swapper_pid)
2905 put_pid(pid);
2906
2907out:
2908 mutex_unlock(&ftrace_lock);
2909 return ret;
2910}
2911
2912static void ftrace_pid_reset(void)
2913{
2914 struct ftrace_pid *fpid, *safe;
2915
2916 mutex_lock(&ftrace_lock);
2917 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
2918 struct pid *pid = fpid->pid;
2919
2920 clear_ftrace_pid_task(pid);
2921
2922 list_del(&fpid->list);
2923 kfree(fpid);
2924 }
2925
2926 ftrace_update_pid_func();
2927 ftrace_startup_enable(0);
2928
2929 mutex_unlock(&ftrace_lock);
2930}
2931
2932static void *fpid_start(struct seq_file *m, loff_t *pos)
2933{
2934 mutex_lock(&ftrace_lock);
2935
2936 if (list_empty(&ftrace_pids) && (!*pos))
2937 return (void *) 1;
2938
2939 return seq_list_start(&ftrace_pids, *pos);
2940}
2941
2942static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
2943{
2944 if (v == (void *)1)
2945 return NULL;
2946
2947 return seq_list_next(v, &ftrace_pids, pos);
2948}
2949
2950static void fpid_stop(struct seq_file *m, void *p)
2951{
2952 mutex_unlock(&ftrace_lock);
2953}
2954
2955static int fpid_show(struct seq_file *m, void *v)
2956{
2957 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
2958
2959 if (v == (void *)1) {
2960 seq_printf(m, "no pid\n");
2961 return 0;
2962 }
2963
2964 if (fpid->pid == ftrace_swapper_pid)
2965 seq_printf(m, "swapper tasks\n");
2966 else
2967 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
2968
2969 return 0;
2970}
2971
2972static const struct seq_operations ftrace_pid_sops = {
2973 .start = fpid_start,
2974 .next = fpid_next,
2975 .stop = fpid_stop,
2976 .show = fpid_show,
2977};
2978
2979static int
2980ftrace_pid_open(struct inode *inode, struct file *file)
2981{
2982 int ret = 0;
2983
2984 if ((file->f_mode & FMODE_WRITE) &&
2985 (file->f_flags & O_TRUNC))
2986 ftrace_pid_reset();
2987
2988 if (file->f_mode & FMODE_READ)
2989 ret = seq_open(file, &ftrace_pid_sops);
2990
2991 return ret;
2992}
2993
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002994static ssize_t
2995ftrace_pid_write(struct file *filp, const char __user *ubuf,
2996 size_t cnt, loff_t *ppos)
2997{
Ingo Molnar457dc922009-11-23 11:03:28 +01002998 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002999 long val;
3000 int ret;
3001
3002 if (cnt >= sizeof(buf))
3003 return -EINVAL;
3004
3005 if (copy_from_user(&buf, ubuf, cnt))
3006 return -EFAULT;
3007
3008 buf[cnt] = 0;
3009
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003010 /*
3011 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3012 * to clean the filter quietly.
3013 */
Ingo Molnar457dc922009-11-23 11:03:28 +01003014 tmp = strstrip(buf);
3015 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003016 return 1;
3017
Ingo Molnar457dc922009-11-23 11:03:28 +01003018 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003019 if (ret < 0)
3020 return ret;
3021
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003022 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003023
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003024 return ret ? ret : cnt;
3025}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003026
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003027static int
3028ftrace_pid_release(struct inode *inode, struct file *file)
3029{
3030 if (file->f_mode & FMODE_READ)
3031 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003032
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003033 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003034}
3035
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003036static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04003037 .open = ftrace_pid_open,
3038 .write = ftrace_pid_write,
3039 .read = seq_read,
3040 .llseek = seq_lseek,
3041 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003042};
3043
3044static __init int ftrace_init_debugfs(void)
3045{
3046 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003047
3048 d_tracer = tracing_init_dentry();
3049 if (!d_tracer)
3050 return 0;
3051
3052 ftrace_init_dyn_debugfs(d_tracer);
3053
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003054 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3055 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003056
3057 ftrace_profile_debugfs(d_tracer);
3058
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003059 return 0;
3060}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003061fs_initcall(ftrace_init_debugfs);
3062
Steven Rostedt3d083392008-05-12 21:20:42 +02003063/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003064 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003065 *
3066 * This function should be used by panic code. It stops ftrace
3067 * but in a not so nice way. If you need to simply kill ftrace
3068 * from a non-atomic section, use ftrace_kill.
3069 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003070void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003071{
3072 ftrace_disabled = 1;
3073 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003074 clear_ftrace_function();
3075}
3076
3077/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003078 * register_ftrace_function - register a function for profiling
3079 * @ops - ops structure that holds the function for profiling.
3080 *
3081 * Register a function to be called by all functions in the
3082 * kernel.
3083 *
3084 * Note: @ops->func and all the functions it calls must be labeled
3085 * with "notrace", otherwise it will go into a
3086 * recursive loop.
3087 */
3088int register_ftrace_function(struct ftrace_ops *ops)
3089{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003090 int ret;
3091
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003092 if (unlikely(ftrace_disabled))
3093 return -1;
3094
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003095 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003096
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003097 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003098 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003099
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003100 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003101 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003102}
3103
3104/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003105 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003106 * @ops - ops structure that holds the function to unregister
3107 *
3108 * Unregister a function that was added to be called by ftrace profiling.
3109 */
3110int unregister_ftrace_function(struct ftrace_ops *ops)
3111{
3112 int ret;
3113
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003114 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003115 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003116 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003117 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003118
3119 return ret;
3120}
3121
Ingo Molnare309b412008-05-12 21:20:51 +02003122int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003123ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003124 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003125 loff_t *ppos)
3126{
3127 int ret;
3128
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003129 if (unlikely(ftrace_disabled))
3130 return -ENODEV;
3131
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003132 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003133
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07003134 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003135
Li Zefana32c7762009-06-26 16:55:51 +08003136 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003137 goto out;
3138
Li Zefana32c7762009-06-26 16:55:51 +08003139 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003140
3141 if (ftrace_enabled) {
3142
3143 ftrace_startup_sysctl();
3144
3145 /* we are starting ftrace again */
3146 if (ftrace_list != &ftrace_list_end) {
3147 if (ftrace_list->next == &ftrace_list_end)
3148 ftrace_trace_function = ftrace_list->func;
3149 else
3150 ftrace_trace_function = ftrace_list_func;
3151 }
3152
3153 } else {
3154 /* stopping ftrace calls (just send to ftrace_stub) */
3155 ftrace_trace_function = ftrace_stub;
3156
3157 ftrace_shutdown_sysctl();
3158 }
3159
3160 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003161 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003162 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003163}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003164
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003165#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003166
Steven Rostedt597af812009-04-03 15:24:12 -04003167static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003168static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003169
Steven Rostedte49dc192008-12-02 23:50:05 -05003170int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3171{
3172 return 0;
3173}
3174
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003175/* The callbacks that hook a function */
3176trace_func_graph_ret_t ftrace_graph_return =
3177 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003178trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003179
3180/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3181static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3182{
3183 int i;
3184 int ret = 0;
3185 unsigned long flags;
3186 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3187 struct task_struct *g, *t;
3188
3189 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3190 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3191 * sizeof(struct ftrace_ret_stack),
3192 GFP_KERNEL);
3193 if (!ret_stack_list[i]) {
3194 start = 0;
3195 end = i;
3196 ret = -ENOMEM;
3197 goto free;
3198 }
3199 }
3200
3201 read_lock_irqsave(&tasklist_lock, flags);
3202 do_each_thread(g, t) {
3203 if (start == end) {
3204 ret = -EAGAIN;
3205 goto unlock;
3206 }
3207
3208 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003209 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003210 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003211 t->curr_ret_stack = -1;
3212 /* Make sure the tasks see the -1 first: */
3213 smp_wmb();
3214 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003215 }
3216 } while_each_thread(g, t);
3217
3218unlock:
3219 read_unlock_irqrestore(&tasklist_lock, flags);
3220free:
3221 for (i = start; i < end; i++)
3222 kfree(ret_stack_list[i]);
3223 return ret;
3224}
3225
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003226static void
3227ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3228 struct task_struct *next)
3229{
3230 unsigned long long timestamp;
3231 int index;
3232
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003233 /*
3234 * Does the user want to count the time a function was asleep.
3235 * If so, do not update the time stamps.
3236 */
3237 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3238 return;
3239
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003240 timestamp = trace_clock_local();
3241
3242 prev->ftrace_timestamp = timestamp;
3243
3244 /* only process tasks that we timestamped */
3245 if (!next->ftrace_timestamp)
3246 return;
3247
3248 /*
3249 * Update all the counters in next to make up for the
3250 * time next was sleeping.
3251 */
3252 timestamp -= next->ftrace_timestamp;
3253
3254 for (index = next->curr_ret_stack; index >= 0; index--)
3255 next->ret_stack[index].calltime += timestamp;
3256}
3257
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003258/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003259static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003260{
3261 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003262 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003263
3264 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3265 sizeof(struct ftrace_ret_stack *),
3266 GFP_KERNEL);
3267
3268 if (!ret_stack_list)
3269 return -ENOMEM;
3270
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003271 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003272 for_each_online_cpu(cpu) {
3273 if (!idle_task(cpu)->ret_stack)
3274 ftrace_graph_init_task(idle_task(cpu));
3275 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003276
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003277 do {
3278 ret = alloc_retstack_tasklist(ret_stack_list);
3279 } while (ret == -EAGAIN);
3280
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003281 if (!ret) {
3282 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3283 if (ret)
3284 pr_info("ftrace_graph: Couldn't activate tracepoint"
3285 " probe to kernel_sched_switch\n");
3286 }
3287
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003288 kfree(ret_stack_list);
3289 return ret;
3290}
3291
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003292/*
3293 * Hibernation protection.
3294 * The state of the current task is too much unstable during
3295 * suspend/restore to disk. We want to protect against that.
3296 */
3297static int
3298ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3299 void *unused)
3300{
3301 switch (state) {
3302 case PM_HIBERNATION_PREPARE:
3303 pause_graph_tracing();
3304 break;
3305
3306 case PM_POST_HIBERNATION:
3307 unpause_graph_tracing();
3308 break;
3309 }
3310 return NOTIFY_DONE;
3311}
3312
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003313int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3314 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003315{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003316 int ret = 0;
3317
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003318 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003319
Steven Rostedt05ce5812009-03-24 00:18:31 -04003320 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003321 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003322 ret = -EBUSY;
3323 goto out;
3324 }
3325
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003326 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3327 register_pm_notifier(&ftrace_suspend_notifier);
3328
Steven Rostedt597af812009-04-03 15:24:12 -04003329 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003330 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003331 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003332 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003333 goto out;
3334 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003335
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003336 ftrace_graph_return = retfunc;
3337 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003338
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003339 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003340
3341out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003342 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003343 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003344}
3345
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003346void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003347{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003348 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003349
Steven Rostedt597af812009-04-03 15:24:12 -04003350 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003351 goto out;
3352
Steven Rostedt597af812009-04-03 15:24:12 -04003353 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003354 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003355 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003356 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003357 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003358 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003359
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003360 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003361 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003362}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003363
3364/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003365void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003366{
Steven Rostedt84047e32009-06-02 16:51:55 -04003367 /* Make sure we do not use the parent ret_stack */
3368 t->ret_stack = NULL;
3369
Steven Rostedt597af812009-04-03 15:24:12 -04003370 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003371 struct ftrace_ret_stack *ret_stack;
3372
3373 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003374 * sizeof(struct ftrace_ret_stack),
3375 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003376 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003377 return;
3378 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003379 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003380 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003381 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003382 /* make curr_ret_stack visable before we add the ret_stack */
3383 smp_wmb();
3384 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003385 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003386}
3387
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003388void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003389{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003390 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3391
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003392 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003393 /* NULL must become visible to IRQs before we free it: */
3394 barrier();
3395
3396 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003397}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003398
3399void ftrace_graph_stop(void)
3400{
3401 ftrace_stop();
3402}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003403#endif