blob: f3716bf04df648418b5f8dc246d54c958097baaf [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
Steven Rostedt4eebcc82008-05-12 21:20:48 +020063/*
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
66 */
67static int ftrace_disabled __read_mostly;
68
Steven Rostedt52baf112009-02-14 01:15:39 -050069static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020070
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020071static struct ftrace_ops ftrace_list_end __read_mostly =
72{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040073 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020074};
75
76static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050078ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050079ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020080
Ingo Molnarf2252932008-05-22 10:37:48 +020081static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020082{
83 struct ftrace_ops *op = ftrace_list;
84
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
87
88 while (op != &ftrace_list_end) {
89 /* silly alpha */
90 read_barrier_depends();
91 op->func(ip, parent_ip);
92 op = op->next;
93 };
94}
95
Steven Rostedtdf4fc312008-11-26 00:16:23 -050096static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
97{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -050098 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -050099 return;
100
101 ftrace_pid_function(ip, parent_ip);
102}
103
104static void set_ftrace_pid_function(ftrace_func_t func)
105{
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
109}
110
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200112 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200116 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200117void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118{
Steven Rostedt3d083392008-05-12 21:20:42 +0200119 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500120 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500121 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200122}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500124#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
125/*
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
128 */
129static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
130{
131 if (function_trace_stop)
132 return;
133
134 __ftrace_trace_function(ip, parent_ip);
135}
136#endif
137
Ingo Molnare309b412008-05-12 21:20:51 +0200138static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200139{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200140 ops->next = ftrace_list;
141 /*
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
146 */
147 smp_wmb();
148 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200149
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200150 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500151 ftrace_func_t func;
152
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
157
Steven Rostedt978f3a42008-12-04 00:26:40 -0500158 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
161 }
162
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200163 /*
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
166 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500167#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500168 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500169#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500170 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500171 ftrace_trace_function = ftrace_test_stop_func;
172#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200173 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200174
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200175 return 0;
176}
177
Ingo Molnare309b412008-05-12 21:20:51 +0200178static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200179{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200180 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200181
182 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200185 */
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500189 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 }
191
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
193 if (*p == ops)
194 break;
195
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500196 if (*p != ops)
197 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198
199 *p = (*p)->next;
200
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
205
Steven Rostedt978f3a42008-12-04 00:26:40 -0500206 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
209 }
210#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212#else
213 __ftrace_trace_function = func;
214#endif
215 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200216 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200217
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500218 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200219}
220
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500221static void ftrace_update_pid_func(void)
222{
223 ftrace_func_t func;
224
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500225 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900226 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500227
228 func = ftrace_trace_function;
229
Steven Rostedt978f3a42008-12-04 00:26:40 -0500230 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
233 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800234 if (func == ftrace_pid_func)
235 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500236 }
237
238#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
240#else
241 __ftrace_trace_function = func;
242#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500243}
244
Steven Rostedt493762f2009-03-23 17:12:36 -0400245#ifdef CONFIG_FUNCTION_PROFILER
246struct ftrace_profile {
247 struct hlist_node node;
248 unsigned long ip;
249 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400250#ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 unsigned long long time;
252#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400253};
254
255struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
257 unsigned long index;
258 struct ftrace_profile records[];
259};
260
Steven Rostedtcafb1682009-03-24 20:50:39 -0400261struct ftrace_profile_stat {
262 atomic_t disabled;
263 struct hlist_head *hash;
264 struct ftrace_profile_page *pages;
265 struct ftrace_profile_page *start;
266 struct tracer_stat stat;
267};
268
Steven Rostedt493762f2009-03-23 17:12:36 -0400269#define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
271
272#define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
274
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400275static int ftrace_profile_bits __read_mostly;
276static int ftrace_profile_enabled __read_mostly;
277
278/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400279static DEFINE_MUTEX(ftrace_profile_lock);
280
Steven Rostedtcafb1682009-03-24 20:50:39 -0400281static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400282
283#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
284
Steven Rostedt493762f2009-03-23 17:12:36 -0400285static void *
286function_stat_next(void *v, int idx)
287{
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
290
291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
292
293 again:
Li Zefan0296e422009-06-26 11:15:37 +0800294 if (idx != 0)
295 rec++;
296
Steven Rostedt493762f2009-03-23 17:12:36 -0400297 if ((void *)rec >= (void *)&pg->records[pg->index]) {
298 pg = pg->next;
299 if (!pg)
300 return NULL;
301 rec = &pg->records[0];
302 if (!rec->counter)
303 goto again;
304 }
305
306 return rec;
307}
308
309static void *function_stat_start(struct tracer_stat *trace)
310{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400311 struct ftrace_profile_stat *stat =
312 container_of(trace, struct ftrace_profile_stat, stat);
313
314 if (!stat || !stat->start)
315 return NULL;
316
317 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400318}
319
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400320#ifdef CONFIG_FUNCTION_GRAPH_TRACER
321/* function graph compares on total time */
322static int function_stat_cmp(void *p1, void *p2)
323{
324 struct ftrace_profile *a = p1;
325 struct ftrace_profile *b = p2;
326
327 if (a->time < b->time)
328 return -1;
329 if (a->time > b->time)
330 return 1;
331 else
332 return 0;
333}
334#else
335/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400336static int function_stat_cmp(void *p1, void *p2)
337{
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
340
341 if (a->counter < b->counter)
342 return -1;
343 if (a->counter > b->counter)
344 return 1;
345 else
346 return 0;
347}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400348#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400349
350static int function_stat_headers(struct seq_file *m)
351{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400352#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400353 seq_printf(m, " Function "
354 "Hit Time Avg\n"
355 " -------- "
356 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400357#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400358 seq_printf(m, " Function Hit\n"
359 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400360#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400361 return 0;
362}
363
364static int function_stat_show(struct seq_file *m, void *v)
365{
366 struct ftrace_profile *rec = v;
367 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400368#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400369 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400370 static struct trace_seq s;
371 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400372#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400373
374 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400375 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400376
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400377#ifdef CONFIG_FUNCTION_GRAPH_TRACER
378 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400379 avg = rec->time;
380 do_div(avg, rec->counter);
381
382 mutex_lock(&mutex);
383 trace_seq_init(&s);
384 trace_print_graph_duration(rec->time, &s);
385 trace_seq_puts(&s, " ");
386 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400387 trace_print_seq(m, &s);
388 mutex_unlock(&mutex);
389#endif
390 seq_putc(m, '\n');
391
Steven Rostedt493762f2009-03-23 17:12:36 -0400392 return 0;
393}
394
Steven Rostedtcafb1682009-03-24 20:50:39 -0400395static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400396{
397 struct ftrace_profile_page *pg;
398
Steven Rostedtcafb1682009-03-24 20:50:39 -0400399 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400400
401 while (pg) {
402 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
403 pg->index = 0;
404 pg = pg->next;
405 }
406
Steven Rostedtcafb1682009-03-24 20:50:39 -0400407 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400408 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
409}
410
Steven Rostedtcafb1682009-03-24 20:50:39 -0400411int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400412{
413 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400414 int functions;
415 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400416 int i;
417
418 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400419 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400420 return 0;
421
Steven Rostedtcafb1682009-03-24 20:50:39 -0400422 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
423 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400424 return -ENOMEM;
425
Steven Rostedt318e0a72009-03-25 20:06:34 -0400426#ifdef CONFIG_DYNAMIC_FTRACE
427 functions = ftrace_update_tot_cnt;
428#else
429 /*
430 * We do not know the number of functions that exist because
431 * dynamic tracing is what counts them. With past experience
432 * we have around 20K functions. That should be more than enough.
433 * It is highly unlikely we will execute every function in
434 * the kernel.
435 */
436 functions = 20000;
437#endif
438
Steven Rostedtcafb1682009-03-24 20:50:39 -0400439 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400440
Steven Rostedt318e0a72009-03-25 20:06:34 -0400441 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
442
443 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400444 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400445 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400446 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400447 pg = pg->next;
448 }
449
450 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400451
452 out_free:
453 pg = stat->start;
454 while (pg) {
455 unsigned long tmp = (unsigned long)pg;
456
457 pg = pg->next;
458 free_page(tmp);
459 }
460
461 free_page((unsigned long)stat->pages);
462 stat->pages = NULL;
463 stat->start = NULL;
464
465 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400466}
467
Steven Rostedtcafb1682009-03-24 20:50:39 -0400468static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400469{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400470 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400471 int size;
472
Steven Rostedtcafb1682009-03-24 20:50:39 -0400473 stat = &per_cpu(ftrace_profile_stats, cpu);
474
475 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400476 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400477 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400478 return 0;
479 }
480
481 /*
482 * We are profiling all functions, but usually only a few thousand
483 * functions are hit. We'll make a hash of 1024 items.
484 */
485 size = FTRACE_PROFILE_HASH_SIZE;
486
Steven Rostedtcafb1682009-03-24 20:50:39 -0400487 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400488
Steven Rostedtcafb1682009-03-24 20:50:39 -0400489 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400490 return -ENOMEM;
491
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492 if (!ftrace_profile_bits) {
493 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400494
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495 for (; size; size >>= 1)
496 ftrace_profile_bits++;
497 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400498
Steven Rostedt318e0a72009-03-25 20:06:34 -0400499 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400500 if (ftrace_profile_pages_init(stat) < 0) {
501 kfree(stat->hash);
502 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400503 return -ENOMEM;
504 }
505
506 return 0;
507}
508
Steven Rostedtcafb1682009-03-24 20:50:39 -0400509static int ftrace_profile_init(void)
510{
511 int cpu;
512 int ret = 0;
513
514 for_each_online_cpu(cpu) {
515 ret = ftrace_profile_init_cpu(cpu);
516 if (ret)
517 break;
518 }
519
520 return ret;
521}
522
Steven Rostedt493762f2009-03-23 17:12:36 -0400523/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400524static struct ftrace_profile *
525ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400526{
527 struct ftrace_profile *rec;
528 struct hlist_head *hhd;
529 struct hlist_node *n;
530 unsigned long key;
531
532 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400533 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400534
535 if (hlist_empty(hhd))
536 return NULL;
537
538 hlist_for_each_entry_rcu(rec, n, hhd, node) {
539 if (rec->ip == ip)
540 return rec;
541 }
542
543 return NULL;
544}
545
Steven Rostedtcafb1682009-03-24 20:50:39 -0400546static void ftrace_add_profile(struct ftrace_profile_stat *stat,
547 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400548{
549 unsigned long key;
550
551 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400552 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400553}
554
Steven Rostedt318e0a72009-03-25 20:06:34 -0400555/*
556 * The memory is already allocated, this simply finds a new record to use.
557 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400558static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400559ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400560{
561 struct ftrace_profile *rec = NULL;
562
Steven Rostedt318e0a72009-03-25 20:06:34 -0400563 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400564 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400565 goto out;
566
Steven Rostedt493762f2009-03-23 17:12:36 -0400567 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400568 * Try to find the function again since an NMI
569 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400570 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400571 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400572 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400573 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400574
Steven Rostedtcafb1682009-03-24 20:50:39 -0400575 if (stat->pages->index == PROFILES_PER_PAGE) {
576 if (!stat->pages->next)
577 goto out;
578 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400579 }
580
Steven Rostedtcafb1682009-03-24 20:50:39 -0400581 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400584
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400587
588 return rec;
589}
590
Steven Rostedt493762f2009-03-23 17:12:36 -0400591static void
592function_profile_call(unsigned long ip, unsigned long parent_ip)
593{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 struct ftrace_profile *rec;
596 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400597
598 if (!ftrace_profile_enabled)
599 return;
600
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602
603 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400604 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605 goto out;
606
607 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400608 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400609 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400610 if (!rec)
611 goto out;
612 }
613
614 rec->counter++;
615 out:
616 local_irq_restore(flags);
617}
618
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619#ifdef CONFIG_FUNCTION_GRAPH_TRACER
620static int profile_graph_entry(struct ftrace_graph_ent *trace)
621{
622 function_profile_call(trace->func, 0);
623 return 1;
624}
625
626static void profile_graph_return(struct ftrace_graph_ret *trace)
627{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400628 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400629 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400630 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400631 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400632
633 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400634 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400635 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400636 goto out;
637
Steven Rostedta2a16d62009-03-24 23:17:58 -0400638 calltime = trace->rettime - trace->calltime;
639
640 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
641 int index;
642
643 index = trace->depth;
644
645 /* Append this call time to the parent time to subtract */
646 if (index)
647 current->ret_stack[index - 1].subtime += calltime;
648
649 if (current->ret_stack[index].subtime < calltime)
650 calltime -= current->ret_stack[index].subtime;
651 else
652 calltime = 0;
653 }
654
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400656 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400657 rec->time += calltime;
658
Steven Rostedtcafb1682009-03-24 20:50:39 -0400659 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400660 local_irq_restore(flags);
661}
662
663static int register_ftrace_profiler(void)
664{
665 return register_ftrace_graph(&profile_graph_return,
666 &profile_graph_entry);
667}
668
669static void unregister_ftrace_profiler(void)
670{
671 unregister_ftrace_graph();
672}
673#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400674static struct ftrace_ops ftrace_profile_ops __read_mostly =
675{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400676 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400677};
678
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400679static int register_ftrace_profiler(void)
680{
681 return register_ftrace_function(&ftrace_profile_ops);
682}
683
684static void unregister_ftrace_profiler(void)
685{
686 unregister_ftrace_function(&ftrace_profile_ops);
687}
688#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
689
Steven Rostedt493762f2009-03-23 17:12:36 -0400690static ssize_t
691ftrace_profile_write(struct file *filp, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
693{
694 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400695 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400696 int ret;
697
698 if (cnt >= sizeof(buf))
699 return -EINVAL;
700
701 if (copy_from_user(&buf, ubuf, cnt))
702 return -EFAULT;
703
704 buf[cnt] = 0;
705
706 ret = strict_strtoul(buf, 10, &val);
707 if (ret < 0)
708 return ret;
709
710 val = !!val;
711
712 mutex_lock(&ftrace_profile_lock);
713 if (ftrace_profile_enabled ^ val) {
714 if (val) {
715 ret = ftrace_profile_init();
716 if (ret < 0) {
717 cnt = ret;
718 goto out;
719 }
720
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400721 ret = register_ftrace_profiler();
722 if (ret < 0) {
723 cnt = ret;
724 goto out;
725 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 ftrace_profile_enabled = 1;
727 } else {
728 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400729 /*
730 * unregister_ftrace_profiler calls stop_machine
731 * so this acts like an synchronize_sched.
732 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400733 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400734 }
735 }
736 out:
737 mutex_unlock(&ftrace_profile_lock);
738
739 filp->f_pos += cnt;
740
741 return cnt;
742}
743
744static ssize_t
745ftrace_profile_read(struct file *filp, char __user *ubuf,
746 size_t cnt, loff_t *ppos)
747{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400748 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 int r;
750
751 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
753}
754
755static const struct file_operations ftrace_profile_fops = {
756 .open = tracing_open_generic,
757 .read = ftrace_profile_read,
758 .write = ftrace_profile_write,
759};
760
Steven Rostedtcafb1682009-03-24 20:50:39 -0400761/* used to initialize the real stat files */
762static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400763 .name = "functions",
764 .stat_start = function_stat_start,
765 .stat_next = function_stat_next,
766 .stat_cmp = function_stat_cmp,
767 .stat_headers = function_stat_headers,
768 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769};
770
Steven Rostedt493762f2009-03-23 17:12:36 -0400771static void ftrace_profile_debugfs(struct dentry *d_tracer)
772{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400776 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400778
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 for_each_possible_cpu(cpu) {
780 stat = &per_cpu(ftrace_profile_stats, cpu);
781
782 /* allocate enough for function name + cpu number */
783 name = kmalloc(32, GFP_KERNEL);
784 if (!name) {
785 /*
786 * The files created are permanent, if something happens
787 * we still do not free memory.
788 */
789 kfree(stat);
790 WARN(1,
791 "Could not allocate stat file for cpu %d\n",
792 cpu);
793 return;
794 }
795 stat->stat = function_stats;
796 snprintf(name, 32, "function%d", cpu);
797 stat->stat.name = name;
798 ret = register_stat_tracer(&stat->stat);
799 if (ret) {
800 WARN(1,
801 "Could not register function stat for cpu %d\n",
802 cpu);
803 kfree(name);
804 return;
805 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 }
807
808 entry = debugfs_create_file("function_profile_enabled", 0644,
809 d_tracer, NULL, &ftrace_profile_fops);
810 if (!entry)
811 pr_warning("Could not create debugfs "
812 "'function_profile_enabled' entry\n");
813}
814
815#else /* CONFIG_FUNCTION_PROFILER */
816static void ftrace_profile_debugfs(struct dentry *d_tracer)
817{
818}
819#endif /* CONFIG_FUNCTION_PROFILER */
820
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100821/* set when tracing only a pid */
822struct pid *ftrace_pid_trace;
823static struct pid * const ftrace_swapper_pid = &init_struct_pid;
824
Steven Rostedt3d083392008-05-12 21:20:42 +0200825#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100826
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400827#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400828# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400829#endif
830
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500831static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
832
Steven Rostedtb6887d72009-02-17 12:32:04 -0500833struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500834 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500835 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500836 unsigned long flags;
837 unsigned long ip;
838 void *data;
839 struct rcu_head rcu;
840};
841
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200842enum {
843 FTRACE_ENABLE_CALLS = (1 << 0),
844 FTRACE_DISABLE_CALLS = (1 << 1),
845 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
846 FTRACE_ENABLE_MCOUNT = (1 << 3),
847 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500848 FTRACE_START_FUNC_RET = (1 << 5),
849 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200850};
851
Steven Rostedt5072c592008-05-12 21:20:43 +0200852static int ftrace_filtered;
853
Lai Jiangshane94142a2009-03-13 17:51:27 +0800854static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200855
Steven Rostedt41c52c02008-05-22 11:46:33 -0400856static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200857
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200858struct ftrace_page {
859 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500860 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200861 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700862};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200863
864#define ENTRIES_PER_PAGE \
865 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
866
867/* estimate from running different kernels */
868#define NR_TO_INIT 10000
869
870static struct ftrace_page *ftrace_pages_start;
871static struct ftrace_page *ftrace_pages;
872
Steven Rostedt37ad50842008-05-12 21:20:48 +0200873static struct dyn_ftrace *ftrace_free_records;
874
Steven Rostedt265c8312009-02-13 12:43:56 -0500875/*
876 * This is a double for. Do not use 'break' to break out of the loop,
877 * you must use a goto.
878 */
879#define do_for_each_ftrace_rec(pg, rec) \
880 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
881 int _____i; \
882 for (_____i = 0; _____i < pg->index; _____i++) { \
883 rec = &pg->records[_____i];
884
885#define while_for_each_ftrace_rec() \
886 } \
887 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530888
889#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200890
891static int frozen_record_count;
892
Abhishek Sagarecea6562008-06-21 23:47:53 +0530893static inline void freeze_record(struct dyn_ftrace *rec)
894{
895 if (!(rec->flags & FTRACE_FL_FROZEN)) {
896 rec->flags |= FTRACE_FL_FROZEN;
897 frozen_record_count++;
898 }
899}
900
901static inline void unfreeze_record(struct dyn_ftrace *rec)
902{
903 if (rec->flags & FTRACE_FL_FROZEN) {
904 rec->flags &= ~FTRACE_FL_FROZEN;
905 frozen_record_count--;
906 }
907}
908
909static inline int record_frozen(struct dyn_ftrace *rec)
910{
911 return rec->flags & FTRACE_FL_FROZEN;
912}
913#else
914# define freeze_record(rec) ({ 0; })
915# define unfreeze_record(rec) ({ 0; })
916# define record_frozen(rec) ({ 0; })
917#endif /* CONFIG_KPROBES */
918
Ingo Molnare309b412008-05-12 21:20:51 +0200919static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +0200920{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800921 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200922 ftrace_free_records = rec;
923 rec->flags |= FTRACE_FL_FREE;
924}
925
Ingo Molnare309b412008-05-12 21:20:51 +0200926static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200927{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200928 struct dyn_ftrace *rec;
929
930 /* First check for freed records */
931 if (ftrace_free_records) {
932 rec = ftrace_free_records;
933
Steven Rostedt37ad50842008-05-12 21:20:48 +0200934 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400935 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +0200936 ftrace_free_records = NULL;
937 return NULL;
938 }
939
Lai Jiangshanee000b72009-03-24 13:38:06 +0800940 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200941 memset(rec, 0, sizeof(*rec));
942 return rec;
943 }
944
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200945 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400946 if (!ftrace_pages->next) {
947 /* allocate another page */
948 ftrace_pages->next =
949 (void *)get_zeroed_page(GFP_KERNEL);
950 if (!ftrace_pages->next)
951 return NULL;
952 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200953 ftrace_pages = ftrace_pages->next;
954 }
955
956 return &ftrace_pages->records[ftrace_pages->index++];
957}
958
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400959static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200960ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200961{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400962 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200963
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800964 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400965 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200966
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400967 rec = ftrace_alloc_dyn_node(ip);
968 if (!rec)
969 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200970
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400971 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800972 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800973 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200974
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400975 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200976}
977
Steven Rostedt05736a42008-09-22 14:55:47 -0700978static void print_ip_ins(const char *fmt, unsigned char *p)
979{
980 int i;
981
982 printk(KERN_CONT "%s", fmt);
983
984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
986}
987
Steven Rostedt31e88902008-11-14 16:21:19 -0800988static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800989{
990 switch (failed) {
991 case -EFAULT:
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace faulted on modifying ");
994 print_ip_sym(ip);
995 break;
996 case -EINVAL:
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace failed to modify ");
999 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001000 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001001 printk(KERN_CONT "\n");
1002 break;
1003 case -EPERM:
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on writing ");
1006 print_ip_sym(ip);
1007 break;
1008 default:
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on unknown error ");
1011 print_ip_sym(ip);
1012 }
1013}
1014
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001015
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301016static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001017__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001018{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001019 unsigned long ftrace_addr;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001020 unsigned long ip, fl;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001021
Shaohua Lif0001202009-01-09 11:29:42 +08001022 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001023
1024 ip = rec->ip;
1025
Steven Rostedt982c3502008-11-15 16:31:41 -05001026 /*
1027 * If this record is not to be traced and
1028 * it is not enabled then do nothing.
1029 *
1030 * If this record is not to be traced and
Wenji Huang57794a92009-02-06 17:33:27 +08001031 * it is enabled then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001032 *
1033 */
1034 if (rec->flags & FTRACE_FL_NOTRACE) {
1035 if (rec->flags & FTRACE_FL_ENABLED)
1036 rec->flags &= ~FTRACE_FL_ENABLED;
1037 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001038 return 0;
1039
Steven Rostedt982c3502008-11-15 16:31:41 -05001040 } else if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001041 /*
Steven Rostedt982c3502008-11-15 16:31:41 -05001042 * Filtering is on:
Steven Rostedt5072c592008-05-12 21:20:43 +02001043 */
Steven Rostedt982c3502008-11-15 16:31:41 -05001044
1045 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1046
1047 /* Record is filtered and enabled, do nothing */
1048 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1049 return 0;
1050
Wenji Huang57794a92009-02-06 17:33:27 +08001051 /* Record is not filtered or enabled, do nothing */
Steven Rostedt982c3502008-11-15 16:31:41 -05001052 if (!fl)
1053 return 0;
1054
1055 /* Record is not filtered but enabled, disable it */
1056 if (fl == FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +02001057 rec->flags &= ~FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -05001058 else
1059 /* Otherwise record is filtered but not enabled, enable it */
Steven Rostedt5072c592008-05-12 21:20:43 +02001060 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001061 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -05001062 /* Disable or not filtered */
Steven Rostedt5072c592008-05-12 21:20:43 +02001063
1064 if (enable) {
Steven Rostedt982c3502008-11-15 16:31:41 -05001065 /* if record is enabled, do nothing */
Steven Rostedt41c52c02008-05-22 11:46:33 -04001066 if (rec->flags & FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +02001067 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -05001068
Steven Rostedt41c52c02008-05-22 11:46:33 -04001069 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -05001070
Steven Rostedt5072c592008-05-12 21:20:43 +02001071 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -05001072
Wenji Huang57794a92009-02-06 17:33:27 +08001073 /* if record is not enabled, do nothing */
Steven Rostedt5072c592008-05-12 21:20:43 +02001074 if (!(rec->flags & FTRACE_FL_ENABLED))
1075 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -05001076
Steven Rostedt5072c592008-05-12 21:20:43 +02001077 rec->flags &= ~FTRACE_FL_ENABLED;
1078 }
1079 }
1080
Steven Rostedt982c3502008-11-15 16:31:41 -05001081 if (rec->flags & FTRACE_FL_ENABLED)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001082 return ftrace_make_call(rec, ftrace_addr);
Steven Rostedt31e88902008-11-14 16:21:19 -08001083 else
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001084 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001085}
1086
1087static void ftrace_replace_code(int enable)
1088{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001089 struct dyn_ftrace *rec;
1090 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001091 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001092
Steven Rostedt265c8312009-02-13 12:43:56 -05001093 do_for_each_ftrace_rec(pg, rec) {
1094 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001095 * Skip over free records, records that have
1096 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001097 */
1098 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001099 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001100 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001101 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001102
Steven Rostedt265c8312009-02-13 12:43:56 -05001103 /* ignore updates to this record's mcount site */
1104 if (get_kprobe((void *)rec->ip)) {
1105 freeze_record(rec);
1106 continue;
1107 } else {
1108 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +02001109 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001110
1111 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001112 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001113 rec->flags |= FTRACE_FL_FAILED;
1114 if ((system_state == SYSTEM_BOOTING) ||
1115 !core_kernel_text(rec->ip)) {
1116 ftrace_free_rec(rec);
Steven Rostedt43772452009-02-19 13:41:27 -05001117 } else {
Steven Rostedt265c8312009-02-13 12:43:56 -05001118 ftrace_bug(failed, rec->ip);
Steven Rostedt43772452009-02-19 13:41:27 -05001119 /* Stop processing */
1120 return;
1121 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001122 }
1123 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001124}
1125
Ingo Molnare309b412008-05-12 21:20:51 +02001126static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001127ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001128{
1129 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001130 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001131
1132 ip = rec->ip;
1133
Shaohua Li25aac9d2009-01-09 11:29:40 +08001134 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001135 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001136 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001137 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301138 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001139 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301140 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001141}
1142
Steven Rostedt000ab692009-02-17 13:35:06 -05001143/*
1144 * archs can override this function if they must do something
1145 * before the modifying code is performed.
1146 */
1147int __weak ftrace_arch_code_modify_prepare(void)
1148{
1149 return 0;
1150}
1151
1152/*
1153 * archs can override this function if they must do something
1154 * after the modifying code is performed.
1155 */
1156int __weak ftrace_arch_code_modify_post_process(void)
1157{
1158 return 0;
1159}
1160
Ingo Molnare309b412008-05-12 21:20:51 +02001161static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001162{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001163 int *command = data;
1164
Steven Rostedta3583242008-11-11 15:01:42 -05001165 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001166 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001167 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001168 ftrace_replace_code(0);
1169
1170 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1171 ftrace_update_ftrace_func(ftrace_trace_function);
1172
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001173 if (*command & FTRACE_START_FUNC_RET)
1174 ftrace_enable_ftrace_graph_caller();
1175 else if (*command & FTRACE_STOP_FUNC_RET)
1176 ftrace_disable_ftrace_graph_caller();
1177
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001178 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001179}
1180
Ingo Molnare309b412008-05-12 21:20:51 +02001181static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001182{
Steven Rostedt000ab692009-02-17 13:35:06 -05001183 int ret;
1184
1185 ret = ftrace_arch_code_modify_prepare();
1186 FTRACE_WARN_ON(ret);
1187 if (ret)
1188 return;
1189
Rusty Russell784e2d72008-07-28 12:16:31 -05001190 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001191
1192 ret = ftrace_arch_code_modify_post_process();
1193 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001194}
1195
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001196static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001197static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001198
1199static void ftrace_startup_enable(int command)
1200{
1201 if (saved_ftrace_func != ftrace_trace_function) {
1202 saved_ftrace_func = ftrace_trace_function;
1203 command |= FTRACE_UPDATE_TRACE_FUNC;
1204 }
1205
1206 if (!command || !ftrace_enabled)
1207 return;
1208
1209 ftrace_run_update_code(command);
1210}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001211
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001212static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001213{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001214 if (unlikely(ftrace_disabled))
1215 return;
1216
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001217 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001218 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001219
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001220 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001221}
1222
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001223static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001224{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001225 if (unlikely(ftrace_disabled))
1226 return;
1227
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001228 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001229 /*
1230 * Just warn in case of unbalance, no need to kill ftrace, it's not
1231 * critical but the ftrace_call callers may be never nopped again after
1232 * further ftrace uses.
1233 */
1234 WARN_ON_ONCE(ftrace_start_up < 0);
1235
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001236 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001237 command |= FTRACE_DISABLE_CALLS;
1238
1239 if (saved_ftrace_func != ftrace_trace_function) {
1240 saved_ftrace_func = ftrace_trace_function;
1241 command |= FTRACE_UPDATE_TRACE_FUNC;
1242 }
1243
1244 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001245 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001246
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001247 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001248}
1249
Ingo Molnare309b412008-05-12 21:20:51 +02001250static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001251{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001252 int command = FTRACE_ENABLE_MCOUNT;
1253
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001254 if (unlikely(ftrace_disabled))
1255 return;
1256
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001257 /* Force update next time */
1258 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001259 /* ftrace_start_up is true if we want ftrace running */
1260 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001261 command |= FTRACE_ENABLE_CALLS;
1262
1263 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001264}
1265
Ingo Molnare309b412008-05-12 21:20:51 +02001266static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001267{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001268 int command = FTRACE_DISABLE_MCOUNT;
1269
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001270 if (unlikely(ftrace_disabled))
1271 return;
1272
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001273 /* ftrace_start_up is true if ftrace is running */
1274 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001275 command |= FTRACE_DISABLE_CALLS;
1276
1277 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001278}
1279
Steven Rostedt3d083392008-05-12 21:20:42 +02001280static cycle_t ftrace_update_time;
1281static unsigned long ftrace_update_cnt;
1282unsigned long ftrace_update_tot_cnt;
1283
Steven Rostedt31e88902008-11-14 16:21:19 -08001284static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001285{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001286 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301287 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001288
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001289 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001290 ftrace_update_cnt = 0;
1291
Lai Jiangshane94142a2009-03-13 17:51:27 +08001292 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301293
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001294 /* If something went wrong, bail without enabling anything */
1295 if (unlikely(ftrace_disabled))
1296 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001297
Lai Jiangshane94142a2009-03-13 17:51:27 +08001298 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001299 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001300 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301301
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001302 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -08001303 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001304 p->flags |= FTRACE_FL_CONVERTED;
1305 ftrace_update_cnt++;
1306 } else
1307 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +02001308 }
1309
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001310 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001311 ftrace_update_time = stop - start;
1312 ftrace_update_tot_cnt += ftrace_update_cnt;
1313
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001314 return 0;
1315}
1316
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001317static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001318{
1319 struct ftrace_page *pg;
1320 int cnt;
1321 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001322
1323 /* allocate a few pages */
1324 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1325 if (!ftrace_pages_start)
1326 return -1;
1327
1328 /*
1329 * Allocate a few more pages.
1330 *
1331 * TODO: have some parser search vmlinux before
1332 * final linking to find all calls to ftrace.
1333 * Then we can:
1334 * a) know how many pages to allocate.
1335 * and/or
1336 * b) set up the table then.
1337 *
1338 * The dynamic code is still necessary for
1339 * modules.
1340 */
1341
1342 pg = ftrace_pages = ftrace_pages_start;
1343
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001344 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001345 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001346 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001347
1348 for (i = 0; i < cnt; i++) {
1349 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1350
1351 /* If we fail, we'll try later anyway */
1352 if (!pg->next)
1353 break;
1354
1355 pg = pg->next;
1356 }
1357
1358 return 0;
1359}
1360
Steven Rostedt5072c592008-05-12 21:20:43 +02001361enum {
1362 FTRACE_ITER_FILTER = (1 << 0),
1363 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -04001364 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301365 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001366 FTRACE_ITER_PRINTALL = (1 << 4),
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001367 FTRACE_ITER_HASH = (1 << 5),
Steven Rostedt5072c592008-05-12 21:20:43 +02001368};
1369
1370#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1371
1372struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001373 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001374 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001375 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001376 unsigned flags;
1377 unsigned char buffer[FTRACE_BUFF_MAX+1];
1378 unsigned buffer_idx;
1379 unsigned filtered;
1380};
1381
Ingo Molnare309b412008-05-12 21:20:51 +02001382static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001383t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1384{
1385 struct ftrace_iterator *iter = m->private;
1386 struct hlist_node *hnd = v;
1387 struct hlist_head *hhd;
1388
1389 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1390
1391 (*pos)++;
1392
1393 retry:
1394 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1395 return NULL;
1396
1397 hhd = &ftrace_func_hash[iter->hidx];
1398
1399 if (hlist_empty(hhd)) {
1400 iter->hidx++;
1401 hnd = NULL;
1402 goto retry;
1403 }
1404
1405 if (!hnd)
1406 hnd = hhd->first;
1407 else {
1408 hnd = hnd->next;
1409 if (!hnd) {
1410 iter->hidx++;
1411 goto retry;
1412 }
1413 }
1414
1415 return hnd;
1416}
1417
1418static void *t_hash_start(struct seq_file *m, loff_t *pos)
1419{
1420 struct ftrace_iterator *iter = m->private;
1421 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08001422 loff_t l;
1423
1424 if (!(iter->flags & FTRACE_ITER_HASH))
1425 *pos = 0;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001426
1427 iter->flags |= FTRACE_ITER_HASH;
1428
Li Zefand82d6242009-06-24 09:54:54 +08001429 iter->hidx = 0;
1430 for (l = 0; l <= *pos; ) {
1431 p = t_hash_next(m, p, &l);
1432 if (!p)
1433 break;
1434 }
1435 return p;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001436}
1437
1438static int t_hash_show(struct seq_file *m, void *v)
1439{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001440 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001441 struct hlist_node *hnd = v;
1442 char str[KSYM_SYMBOL_LEN];
1443
Steven Rostedtb6887d72009-02-17 12:32:04 -05001444 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001445
Steven Rostedt809dcf22009-02-16 23:06:01 -05001446 if (rec->ops->print)
1447 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1448
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001449 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1450 seq_printf(m, "%s:", str);
1451
1452 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1453 seq_printf(m, "%s", str);
1454
1455 if (rec->data)
1456 seq_printf(m, ":%p", rec->data);
1457 seq_putc(m, '\n');
1458
1459 return 0;
1460}
1461
1462static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001463t_next(struct seq_file *m, void *v, loff_t *pos)
1464{
1465 struct ftrace_iterator *iter = m->private;
1466 struct dyn_ftrace *rec = NULL;
1467
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001468 if (iter->flags & FTRACE_ITER_HASH)
1469 return t_hash_next(m, v, pos);
1470
Steven Rostedt5072c592008-05-12 21:20:43 +02001471 (*pos)++;
1472
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001473 if (iter->flags & FTRACE_ITER_PRINTALL)
1474 return NULL;
1475
Steven Rostedt5072c592008-05-12 21:20:43 +02001476 retry:
1477 if (iter->idx >= iter->pg->index) {
1478 if (iter->pg->next) {
1479 iter->pg = iter->pg->next;
1480 iter->idx = 0;
1481 goto retry;
1482 }
1483 } else {
1484 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001485 if ((rec->flags & FTRACE_FL_FREE) ||
1486
1487 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301488 (rec->flags & FTRACE_FL_FAILED)) ||
1489
1490 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001491 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301492
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001493 ((iter->flags & FTRACE_ITER_FILTER) &&
1494 !(rec->flags & FTRACE_FL_FILTER)) ||
1495
Steven Rostedt41c52c02008-05-22 11:46:33 -04001496 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1497 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001498 rec = NULL;
1499 goto retry;
1500 }
1501 }
1502
Steven Rostedt5072c592008-05-12 21:20:43 +02001503 return rec;
1504}
1505
1506static void *t_start(struct seq_file *m, loff_t *pos)
1507{
1508 struct ftrace_iterator *iter = m->private;
1509 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08001510 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02001511
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001512 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001513 /*
1514 * For set_ftrace_filter reading, if we have the filter
1515 * off, we can short cut and just print out that all
1516 * functions are enabled.
1517 */
1518 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1519 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001520 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001521 iter->flags |= FTRACE_ITER_PRINTALL;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001522 return iter;
1523 }
1524
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001525 if (iter->flags & FTRACE_ITER_HASH)
1526 return t_hash_start(m, pos);
1527
Li Zefan694ce0a2009-06-24 09:54:19 +08001528 iter->pg = ftrace_pages_start;
1529 iter->idx = 0;
1530 for (l = 0; l <= *pos; ) {
1531 p = t_next(m, p, &l);
1532 if (!p)
1533 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08001534 }
walimis5821e1b2008-11-15 15:19:06 +08001535
Li Zefan694ce0a2009-06-24 09:54:19 +08001536 if (!p && iter->flags & FTRACE_ITER_FILTER)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001537 return t_hash_start(m, pos);
1538
Steven Rostedt5072c592008-05-12 21:20:43 +02001539 return p;
1540}
1541
1542static void t_stop(struct seq_file *m, void *p)
1543{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001544 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001545}
1546
1547static int t_show(struct seq_file *m, void *v)
1548{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001549 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001550 struct dyn_ftrace *rec = v;
1551 char str[KSYM_SYMBOL_LEN];
1552
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001553 if (iter->flags & FTRACE_ITER_HASH)
1554 return t_hash_show(m, v);
1555
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001556 if (iter->flags & FTRACE_ITER_PRINTALL) {
1557 seq_printf(m, "#### all functions enabled ####\n");
1558 return 0;
1559 }
1560
Steven Rostedt5072c592008-05-12 21:20:43 +02001561 if (!rec)
1562 return 0;
1563
1564 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1565
Liming Wang50cdaf02008-11-28 12:13:21 +08001566 seq_printf(m, "%s\n", str);
Steven Rostedt5072c592008-05-12 21:20:43 +02001567
1568 return 0;
1569}
1570
1571static struct seq_operations show_ftrace_seq_ops = {
1572 .start = t_start,
1573 .next = t_next,
1574 .stop = t_stop,
1575 .show = t_show,
1576};
1577
Ingo Molnare309b412008-05-12 21:20:51 +02001578static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001579ftrace_avail_open(struct inode *inode, struct file *file)
1580{
1581 struct ftrace_iterator *iter;
1582 int ret;
1583
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001584 if (unlikely(ftrace_disabled))
1585 return -ENODEV;
1586
Steven Rostedt5072c592008-05-12 21:20:43 +02001587 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1588 if (!iter)
1589 return -ENOMEM;
1590
1591 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001592
1593 ret = seq_open(file, &show_ftrace_seq_ops);
1594 if (!ret) {
1595 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001596
Steven Rostedt5072c592008-05-12 21:20:43 +02001597 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001598 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001599 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001600 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001601
1602 return ret;
1603}
1604
1605int ftrace_avail_release(struct inode *inode, struct file *file)
1606{
1607 struct seq_file *m = (struct seq_file *)file->private_data;
1608 struct ftrace_iterator *iter = m->private;
1609
1610 seq_release(inode, file);
1611 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001612
Steven Rostedt5072c592008-05-12 21:20:43 +02001613 return 0;
1614}
1615
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301616static int
1617ftrace_failures_open(struct inode *inode, struct file *file)
1618{
1619 int ret;
1620 struct seq_file *m;
1621 struct ftrace_iterator *iter;
1622
1623 ret = ftrace_avail_open(inode, file);
1624 if (!ret) {
1625 m = (struct seq_file *)file->private_data;
1626 iter = (struct ftrace_iterator *)m->private;
1627 iter->flags = FTRACE_ITER_FAILURES;
1628 }
1629
1630 return ret;
1631}
1632
1633
Steven Rostedt41c52c02008-05-22 11:46:33 -04001634static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001635{
1636 struct ftrace_page *pg;
1637 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001638 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001639
Steven Rostedt52baf112009-02-14 01:15:39 -05001640 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001641 if (enable)
1642 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001643 do_for_each_ftrace_rec(pg, rec) {
1644 if (rec->flags & FTRACE_FL_FAILED)
1645 continue;
1646 rec->flags &= ~type;
1647 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001648 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001649}
1650
Ingo Molnare309b412008-05-12 21:20:51 +02001651static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001652ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001653{
1654 struct ftrace_iterator *iter;
1655 int ret = 0;
1656
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001657 if (unlikely(ftrace_disabled))
1658 return -ENODEV;
1659
Steven Rostedt5072c592008-05-12 21:20:43 +02001660 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1661 if (!iter)
1662 return -ENOMEM;
1663
Steven Rostedt41c52c02008-05-22 11:46:33 -04001664 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001665 if ((file->f_mode & FMODE_WRITE) &&
1666 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001667 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001668
1669 if (file->f_mode & FMODE_READ) {
1670 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001671 iter->flags = enable ? FTRACE_ITER_FILTER :
1672 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001673
1674 ret = seq_open(file, &show_ftrace_seq_ops);
1675 if (!ret) {
1676 struct seq_file *m = file->private_data;
1677 m->private = iter;
1678 } else
1679 kfree(iter);
1680 } else
1681 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001682 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001683
1684 return ret;
1685}
1686
Steven Rostedt41c52c02008-05-22 11:46:33 -04001687static int
1688ftrace_filter_open(struct inode *inode, struct file *file)
1689{
1690 return ftrace_regex_open(inode, file, 1);
1691}
1692
1693static int
1694ftrace_notrace_open(struct inode *inode, struct file *file)
1695{
1696 return ftrace_regex_open(inode, file, 0);
1697}
1698
Ingo Molnare309b412008-05-12 21:20:51 +02001699static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001700ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001701{
1702 loff_t ret;
1703
1704 if (file->f_mode & FMODE_READ)
1705 ret = seq_lseek(file, offset, origin);
1706 else
1707 file->f_pos = ret = 1;
1708
1709 return ret;
1710}
1711
1712enum {
1713 MATCH_FULL,
1714 MATCH_FRONT_ONLY,
1715 MATCH_MIDDLE_ONLY,
1716 MATCH_END_ONLY,
1717};
1718
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001719/*
1720 * (static function - no need for kernel doc)
1721 *
1722 * Pass in a buffer containing a glob and this function will
1723 * set search to point to the search part of the buffer and
1724 * return the type of search it is (see enum above).
1725 * This does modify buff.
1726 *
1727 * Returns enum type.
1728 * search returns the pointer to use for comparison.
1729 * not returns 1 if buff started with a '!'
1730 * 0 otherwise.
1731 */
1732static int
Steven Rostedt64e7c442009-02-13 17:08:48 -05001733ftrace_setup_glob(char *buff, int len, char **search, int *not)
Steven Rostedt5072c592008-05-12 21:20:43 +02001734{
Steven Rostedt5072c592008-05-12 21:20:43 +02001735 int type = MATCH_FULL;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001736 int i;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001737
1738 if (buff[0] == '!') {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001739 *not = 1;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001740 buff++;
1741 len--;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001742 } else
1743 *not = 0;
1744
1745 *search = buff;
Steven Rostedt5072c592008-05-12 21:20:43 +02001746
1747 for (i = 0; i < len; i++) {
1748 if (buff[i] == '*') {
1749 if (!i) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001750 *search = buff + 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001751 type = MATCH_END_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001752 } else {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001753 if (type == MATCH_END_ONLY)
Steven Rostedt5072c592008-05-12 21:20:43 +02001754 type = MATCH_MIDDLE_ONLY;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001755 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001756 type = MATCH_FRONT_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001757 buff[i] = 0;
1758 break;
1759 }
1760 }
1761 }
1762
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001763 return type;
1764}
1765
Steven Rostedt64e7c442009-02-13 17:08:48 -05001766static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001767{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001768 int matched = 0;
1769 char *ptr;
1770
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001771 switch (type) {
1772 case MATCH_FULL:
1773 if (strcmp(str, regex) == 0)
1774 matched = 1;
1775 break;
1776 case MATCH_FRONT_ONLY:
1777 if (strncmp(str, regex, len) == 0)
1778 matched = 1;
1779 break;
1780 case MATCH_MIDDLE_ONLY:
1781 if (strstr(str, regex))
1782 matched = 1;
1783 break;
1784 case MATCH_END_ONLY:
1785 ptr = strstr(str, regex);
1786 if (ptr && (ptr[len] == 0))
1787 matched = 1;
1788 break;
1789 }
1790
1791 return matched;
1792}
1793
Steven Rostedt64e7c442009-02-13 17:08:48 -05001794static int
1795ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1796{
1797 char str[KSYM_SYMBOL_LEN];
1798
1799 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1800 return ftrace_match(str, regex, len, type);
1801}
1802
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001803static void ftrace_match_records(char *buff, int len, int enable)
1804{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001805 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001806 struct ftrace_page *pg;
1807 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001808 unsigned long flag;
1809 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001810 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001811 int not;
1812
Steven Rostedt6a24a242009-02-17 11:20:26 -05001813 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001814 type = ftrace_setup_glob(buff, len, &search, &not);
1815
1816 search_len = strlen(search);
1817
Steven Rostedt52baf112009-02-14 01:15:39 -05001818 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001819 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001820
Steven Rostedt265c8312009-02-13 12:43:56 -05001821 if (rec->flags & FTRACE_FL_FAILED)
1822 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001823
1824 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001825 if (not)
1826 rec->flags &= ~flag;
1827 else
1828 rec->flags |= flag;
1829 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001830 /*
1831 * Only enable filtering if we have a function that
1832 * is filtered on.
1833 */
1834 if (enable && (rec->flags & FTRACE_FL_FILTER))
1835 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001836 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001837 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001838}
1839
Steven Rostedt64e7c442009-02-13 17:08:48 -05001840static int
1841ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1842 char *regex, int len, int type)
1843{
1844 char str[KSYM_SYMBOL_LEN];
1845 char *modname;
1846
1847 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1848
1849 if (!modname || strcmp(modname, mod))
1850 return 0;
1851
1852 /* blank search means to match all funcs in the mod */
1853 if (len)
1854 return ftrace_match(str, regex, len, type);
1855 else
1856 return 1;
1857}
1858
1859static void ftrace_match_module_records(char *buff, char *mod, int enable)
1860{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001861 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001862 struct ftrace_page *pg;
1863 struct dyn_ftrace *rec;
1864 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001865 char *search = buff;
1866 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001867 int not = 0;
1868
Steven Rostedt6a24a242009-02-17 11:20:26 -05001869 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1870
Steven Rostedt64e7c442009-02-13 17:08:48 -05001871 /* blank or '*' mean the same */
1872 if (strcmp(buff, "*") == 0)
1873 buff[0] = 0;
1874
1875 /* handle the case of 'dont filter this module' */
1876 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1877 buff[0] = 0;
1878 not = 1;
1879 }
1880
1881 if (strlen(buff)) {
1882 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1883 search_len = strlen(search);
1884 }
1885
Steven Rostedt52baf112009-02-14 01:15:39 -05001886 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001887 do_for_each_ftrace_rec(pg, rec) {
1888
1889 if (rec->flags & FTRACE_FL_FAILED)
1890 continue;
1891
1892 if (ftrace_match_module_record(rec, mod,
1893 search, search_len, type)) {
1894 if (not)
1895 rec->flags &= ~flag;
1896 else
1897 rec->flags |= flag;
1898 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001899 if (enable && (rec->flags & FTRACE_FL_FILTER))
1900 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001901
1902 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001903 mutex_unlock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001904}
1905
Steven Rostedtf6180772009-02-14 00:40:25 -05001906/*
1907 * We register the module command as a template to show others how
1908 * to register the a command as well.
1909 */
1910
1911static int
1912ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1913{
1914 char *mod;
1915
1916 /*
1917 * cmd == 'mod' because we only registered this func
1918 * for the 'mod' ftrace_func_command.
1919 * But if you register one func with multiple commands,
1920 * you can tell which command was used by the cmd
1921 * parameter.
1922 */
1923
1924 /* we must have a module name */
1925 if (!param)
1926 return -EINVAL;
1927
1928 mod = strsep(&param, ":");
1929 if (!strlen(mod))
1930 return -EINVAL;
1931
1932 ftrace_match_module_records(func, mod, enable);
1933 return 0;
1934}
1935
1936static struct ftrace_func_command ftrace_mod_cmd = {
1937 .name = "mod",
1938 .func = ftrace_mod_callback,
1939};
1940
1941static int __init ftrace_mod_cmd_init(void)
1942{
1943 return register_ftrace_command(&ftrace_mod_cmd);
1944}
1945device_initcall(ftrace_mod_cmd_init);
1946
Steven Rostedt59df055f2009-02-14 15:29:06 -05001947static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001948function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001949{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001950 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001951 struct hlist_head *hhd;
1952 struct hlist_node *n;
1953 unsigned long key;
1954 int resched;
1955
1956 key = hash_long(ip, FTRACE_HASH_BITS);
1957
1958 hhd = &ftrace_func_hash[key];
1959
1960 if (hlist_empty(hhd))
1961 return;
1962
1963 /*
1964 * Disable preemption for these calls to prevent a RCU grace
1965 * period. This syncs the hash iteration and freeing of items
1966 * on the hash. rcu_read_lock is too dangerous here.
1967 */
1968 resched = ftrace_preempt_disable();
1969 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1970 if (entry->ip == ip)
1971 entry->ops->func(ip, parent_ip, &entry->data);
1972 }
1973 ftrace_preempt_enable(resched);
1974}
1975
Steven Rostedtb6887d72009-02-17 12:32:04 -05001976static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001977{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001978 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001979};
1980
Steven Rostedtb6887d72009-02-17 12:32:04 -05001981static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001982
Steven Rostedtb6887d72009-02-17 12:32:04 -05001983static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001984{
1985 int i;
1986
Steven Rostedtb6887d72009-02-17 12:32:04 -05001987 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001988 return;
1989
1990 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1991 struct hlist_head *hhd = &ftrace_func_hash[i];
1992 if (hhd->first)
1993 break;
1994 }
1995 /* Nothing registered? */
1996 if (i == FTRACE_FUNC_HASHSIZE)
1997 return;
1998
Steven Rostedtb6887d72009-02-17 12:32:04 -05001999 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002000 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002001 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002002}
2003
Steven Rostedtb6887d72009-02-17 12:32:04 -05002004static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002005{
2006 int i;
2007
Steven Rostedtb6887d72009-02-17 12:32:04 -05002008 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002009 return;
2010
2011 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2012 struct hlist_head *hhd = &ftrace_func_hash[i];
2013 if (hhd->first)
2014 return;
2015 }
2016
2017 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002018 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002019 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002020 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002021}
2022
2023
2024static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2025{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002026 struct ftrace_func_probe *entry =
2027 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002028
2029 if (entry->ops->free)
2030 entry->ops->free(&entry->data);
2031 kfree(entry);
2032}
2033
2034
2035int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002036register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002037 void *data)
2038{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002039 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002040 struct ftrace_page *pg;
2041 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002042 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002043 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002044 int count = 0;
2045 char *search;
2046
2047 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2048 len = strlen(search);
2049
Steven Rostedtb6887d72009-02-17 12:32:04 -05002050 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002051 if (WARN_ON(not))
2052 return -EINVAL;
2053
2054 mutex_lock(&ftrace_lock);
2055 do_for_each_ftrace_rec(pg, rec) {
2056
2057 if (rec->flags & FTRACE_FL_FAILED)
2058 continue;
2059
2060 if (!ftrace_match_record(rec, search, len, type))
2061 continue;
2062
2063 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2064 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002065 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002066 if (!count)
2067 count = -ENOMEM;
2068 goto out_unlock;
2069 }
2070
2071 count++;
2072
2073 entry->data = data;
2074
2075 /*
2076 * The caller might want to do something special
2077 * for each function we find. We call the callback
2078 * to give the caller an opportunity to do so.
2079 */
2080 if (ops->callback) {
2081 if (ops->callback(rec->ip, &entry->data) < 0) {
2082 /* caller does not like this func */
2083 kfree(entry);
2084 continue;
2085 }
2086 }
2087
2088 entry->ops = ops;
2089 entry->ip = rec->ip;
2090
2091 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2092 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2093
2094 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002095 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002096
2097 out_unlock:
2098 mutex_unlock(&ftrace_lock);
2099
2100 return count;
2101}
2102
2103enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002104 PROBE_TEST_FUNC = 1,
2105 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002106};
2107
2108static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002109__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002110 void *data, int flags)
2111{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002112 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002113 struct hlist_node *n, *tmp;
2114 char str[KSYM_SYMBOL_LEN];
2115 int type = MATCH_FULL;
2116 int i, len = 0;
2117 char *search;
2118
2119 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2120 glob = NULL;
2121 else {
2122 int not;
2123
2124 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2125 len = strlen(search);
2126
Steven Rostedtb6887d72009-02-17 12:32:04 -05002127 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002128 if (WARN_ON(not))
2129 return;
2130 }
2131
2132 mutex_lock(&ftrace_lock);
2133 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2134 struct hlist_head *hhd = &ftrace_func_hash[i];
2135
2136 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2137
2138 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002139 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002140 continue;
2141
Steven Rostedtb6887d72009-02-17 12:32:04 -05002142 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002143 continue;
2144
2145 /* do this last, since it is the most expensive */
2146 if (glob) {
2147 kallsyms_lookup(entry->ip, NULL, NULL,
2148 NULL, str);
2149 if (!ftrace_match(str, glob, len, type))
2150 continue;
2151 }
2152
2153 hlist_del(&entry->node);
2154 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2155 }
2156 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002157 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002158 mutex_unlock(&ftrace_lock);
2159}
2160
2161void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002162unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002163 void *data)
2164{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002165 __unregister_ftrace_function_probe(glob, ops, data,
2166 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002167}
2168
2169void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002170unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002171{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002172 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002173}
2174
Steven Rostedtb6887d72009-02-17 12:32:04 -05002175void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002176{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002177 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002178}
2179
Steven Rostedtf6180772009-02-14 00:40:25 -05002180static LIST_HEAD(ftrace_commands);
2181static DEFINE_MUTEX(ftrace_cmd_mutex);
2182
2183int register_ftrace_command(struct ftrace_func_command *cmd)
2184{
2185 struct ftrace_func_command *p;
2186 int ret = 0;
2187
2188 mutex_lock(&ftrace_cmd_mutex);
2189 list_for_each_entry(p, &ftrace_commands, list) {
2190 if (strcmp(cmd->name, p->name) == 0) {
2191 ret = -EBUSY;
2192 goto out_unlock;
2193 }
2194 }
2195 list_add(&cmd->list, &ftrace_commands);
2196 out_unlock:
2197 mutex_unlock(&ftrace_cmd_mutex);
2198
2199 return ret;
2200}
2201
2202int unregister_ftrace_command(struct ftrace_func_command *cmd)
2203{
2204 struct ftrace_func_command *p, *n;
2205 int ret = -ENODEV;
2206
2207 mutex_lock(&ftrace_cmd_mutex);
2208 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2209 if (strcmp(cmd->name, p->name) == 0) {
2210 ret = 0;
2211 list_del_init(&p->list);
2212 goto out_unlock;
2213 }
2214 }
2215 out_unlock:
2216 mutex_unlock(&ftrace_cmd_mutex);
2217
2218 return ret;
2219}
2220
Steven Rostedt64e7c442009-02-13 17:08:48 -05002221static int ftrace_process_regex(char *buff, int len, int enable)
2222{
Steven Rostedtf6180772009-02-14 00:40:25 -05002223 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002224 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002225 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002226
2227 func = strsep(&next, ":");
2228
2229 if (!next) {
2230 ftrace_match_records(func, len, enable);
2231 return 0;
2232 }
2233
Steven Rostedtf6180772009-02-14 00:40:25 -05002234 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002235
2236 command = strsep(&next, ":");
2237
Steven Rostedtf6180772009-02-14 00:40:25 -05002238 mutex_lock(&ftrace_cmd_mutex);
2239 list_for_each_entry(p, &ftrace_commands, list) {
2240 if (strcmp(p->name, command) == 0) {
2241 ret = p->func(func, command, next, enable);
2242 goto out_unlock;
2243 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002244 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002245 out_unlock:
2246 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002247
Steven Rostedtf6180772009-02-14 00:40:25 -05002248 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002249}
2250
Ingo Molnare309b412008-05-12 21:20:51 +02002251static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002252ftrace_regex_write(struct file *file, const char __user *ubuf,
2253 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002254{
2255 struct ftrace_iterator *iter;
2256 char ch;
2257 size_t read = 0;
2258 ssize_t ret;
2259
2260 if (!cnt || cnt < 0)
2261 return 0;
2262
Steven Rostedt41c52c02008-05-22 11:46:33 -04002263 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002264
2265 if (file->f_mode & FMODE_READ) {
2266 struct seq_file *m = file->private_data;
2267 iter = m->private;
2268 } else
2269 iter = file->private_data;
2270
2271 if (!*ppos) {
2272 iter->flags &= ~FTRACE_ITER_CONT;
2273 iter->buffer_idx = 0;
2274 }
2275
2276 ret = get_user(ch, ubuf++);
2277 if (ret)
2278 goto out;
2279 read++;
2280 cnt--;
2281
2282 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2283 /* skip white space */
2284 while (cnt && isspace(ch)) {
2285 ret = get_user(ch, ubuf++);
2286 if (ret)
2287 goto out;
2288 read++;
2289 cnt--;
2290 }
2291
Steven Rostedt5072c592008-05-12 21:20:43 +02002292 if (isspace(ch)) {
2293 file->f_pos += read;
2294 ret = read;
2295 goto out;
2296 }
2297
2298 iter->buffer_idx = 0;
2299 }
2300
2301 while (cnt && !isspace(ch)) {
2302 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2303 iter->buffer[iter->buffer_idx++] = ch;
2304 else {
2305 ret = -EINVAL;
2306 goto out;
2307 }
2308 ret = get_user(ch, ubuf++);
2309 if (ret)
2310 goto out;
2311 read++;
2312 cnt--;
2313 }
2314
2315 if (isspace(ch)) {
2316 iter->filtered++;
2317 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002318 ret = ftrace_process_regex(iter->buffer,
2319 iter->buffer_idx, enable);
2320 if (ret)
2321 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02002322 iter->buffer_idx = 0;
2323 } else
2324 iter->flags |= FTRACE_ITER_CONT;
2325
2326
2327 file->f_pos += read;
2328
2329 ret = read;
2330 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04002331 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002332
2333 return ret;
2334}
2335
Steven Rostedt41c52c02008-05-22 11:46:33 -04002336static ssize_t
2337ftrace_filter_write(struct file *file, const char __user *ubuf,
2338 size_t cnt, loff_t *ppos)
2339{
2340 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2341}
2342
2343static ssize_t
2344ftrace_notrace_write(struct file *file, const char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2346{
2347 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2348}
2349
2350static void
2351ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2352{
2353 if (unlikely(ftrace_disabled))
2354 return;
2355
2356 mutex_lock(&ftrace_regex_lock);
2357 if (reset)
2358 ftrace_filter_reset(enable);
2359 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002360 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002361 mutex_unlock(&ftrace_regex_lock);
2362}
2363
Steven Rostedt77a2b372008-05-12 21:20:45 +02002364/**
2365 * ftrace_set_filter - set a function to filter on in ftrace
2366 * @buf - the string that holds the function filter text.
2367 * @len - the length of the string.
2368 * @reset - non zero to reset all filters before applying this filter.
2369 *
2370 * Filters denote which functions should be enabled when tracing is enabled.
2371 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2372 */
Ingo Molnare309b412008-05-12 21:20:51 +02002373void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002374{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002375 ftrace_set_regex(buf, len, reset, 1);
2376}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002377
Steven Rostedt41c52c02008-05-22 11:46:33 -04002378/**
2379 * ftrace_set_notrace - set a function to not trace in ftrace
2380 * @buf - the string that holds the function notrace text.
2381 * @len - the length of the string.
2382 * @reset - non zero to reset all filters before applying this filter.
2383 *
2384 * Notrace Filters denote which functions should not be enabled when tracing
2385 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2386 * for tracing.
2387 */
2388void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2389{
2390 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002391}
2392
Steven Rostedt2af15d62009-05-28 13:37:24 -04002393/*
2394 * command line interface to allow users to set filters on boot up.
2395 */
2396#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2397static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2398static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2399
2400static int __init set_ftrace_notrace(char *str)
2401{
2402 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2403 return 1;
2404}
2405__setup("ftrace_notrace=", set_ftrace_notrace);
2406
2407static int __init set_ftrace_filter(char *str)
2408{
2409 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2410 return 1;
2411}
2412__setup("ftrace_filter=", set_ftrace_filter);
2413
2414static void __init set_ftrace_early_filter(char *buf, int enable)
2415{
2416 char *func;
2417
2418 while (buf) {
2419 func = strsep(&buf, ",");
2420 ftrace_set_regex(func, strlen(func), 0, enable);
2421 }
2422}
2423
2424static void __init set_ftrace_early_filters(void)
2425{
2426 if (ftrace_filter_buf[0])
2427 set_ftrace_early_filter(ftrace_filter_buf, 1);
2428 if (ftrace_notrace_buf[0])
2429 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2430}
2431
Ingo Molnare309b412008-05-12 21:20:51 +02002432static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002433ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002434{
2435 struct seq_file *m = (struct seq_file *)file->private_data;
2436 struct ftrace_iterator *iter;
2437
Steven Rostedt41c52c02008-05-22 11:46:33 -04002438 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002439 if (file->f_mode & FMODE_READ) {
2440 iter = m->private;
2441
2442 seq_release(inode, file);
2443 } else
2444 iter = file->private_data;
2445
2446 if (iter->buffer_idx) {
2447 iter->filtered++;
2448 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt7f24b312009-02-13 14:37:33 -05002449 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002450 }
2451
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002452 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002453 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002454 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002455 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002456
2457 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002458 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002459 return 0;
2460}
2461
Steven Rostedt41c52c02008-05-22 11:46:33 -04002462static int
2463ftrace_filter_release(struct inode *inode, struct file *file)
2464{
2465 return ftrace_regex_release(inode, file, 1);
2466}
2467
2468static int
2469ftrace_notrace_release(struct inode *inode, struct file *file)
2470{
2471 return ftrace_regex_release(inode, file, 0);
2472}
2473
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002474static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002475 .open = ftrace_avail_open,
2476 .read = seq_read,
2477 .llseek = seq_lseek,
2478 .release = ftrace_avail_release,
2479};
2480
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002481static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302482 .open = ftrace_failures_open,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
2485 .release = ftrace_avail_release,
2486};
2487
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002488static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002489 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002490 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002491 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002492 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002493 .release = ftrace_filter_release,
2494};
2495
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002496static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002497 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002498 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002499 .write = ftrace_notrace_write,
2500 .llseek = ftrace_regex_lseek,
2501 .release = ftrace_notrace_release,
2502};
2503
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002504#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2505
2506static DEFINE_MUTEX(graph_lock);
2507
2508int ftrace_graph_count;
2509unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2510
2511static void *
Li Zefan85951842009-06-24 09:54:00 +08002512__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002513{
2514 unsigned long *array = m->private;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002515
Li Zefan85951842009-06-24 09:54:00 +08002516 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002517 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08002518 return &array[*pos];
2519}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002520
Li Zefan85951842009-06-24 09:54:00 +08002521static void *
2522g_next(struct seq_file *m, void *v, loff_t *pos)
2523{
2524 (*pos)++;
2525 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002526}
2527
2528static void *g_start(struct seq_file *m, loff_t *pos)
2529{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002530 mutex_lock(&graph_lock);
2531
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002532 /* Nothing, tell g_show to print all functions are enabled */
2533 if (!ftrace_graph_count && !*pos)
2534 return (void *)1;
2535
Li Zefan85951842009-06-24 09:54:00 +08002536 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002537}
2538
2539static void g_stop(struct seq_file *m, void *p)
2540{
2541 mutex_unlock(&graph_lock);
2542}
2543
2544static int g_show(struct seq_file *m, void *v)
2545{
2546 unsigned long *ptr = v;
2547 char str[KSYM_SYMBOL_LEN];
2548
2549 if (!ptr)
2550 return 0;
2551
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002552 if (ptr == (unsigned long *)1) {
2553 seq_printf(m, "#### all functions enabled ####\n");
2554 return 0;
2555 }
2556
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002557 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2558
2559 seq_printf(m, "%s\n", str);
2560
2561 return 0;
2562}
2563
2564static struct seq_operations ftrace_graph_seq_ops = {
2565 .start = g_start,
2566 .next = g_next,
2567 .stop = g_stop,
2568 .show = g_show,
2569};
2570
2571static int
2572ftrace_graph_open(struct inode *inode, struct file *file)
2573{
2574 int ret = 0;
2575
2576 if (unlikely(ftrace_disabled))
2577 return -ENODEV;
2578
2579 mutex_lock(&graph_lock);
2580 if ((file->f_mode & FMODE_WRITE) &&
2581 !(file->f_flags & O_APPEND)) {
2582 ftrace_graph_count = 0;
2583 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2584 }
2585
2586 if (file->f_mode & FMODE_READ) {
2587 ret = seq_open(file, &ftrace_graph_seq_ops);
2588 if (!ret) {
2589 struct seq_file *m = file->private_data;
2590 m->private = ftrace_graph_funcs;
2591 }
2592 } else
2593 file->private_data = ftrace_graph_funcs;
2594 mutex_unlock(&graph_lock);
2595
2596 return ret;
2597}
2598
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002599static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002600ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002601{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002602 struct dyn_ftrace *rec;
2603 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002604 int search_len;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002605 int found = 0;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002606 int type, not;
2607 char *search;
2608 bool exists;
2609 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002610
2611 if (ftrace_disabled)
2612 return -ENODEV;
2613
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002614 /* decode regex */
2615 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2616 if (not)
2617 return -EINVAL;
2618
2619 search_len = strlen(search);
2620
Steven Rostedt52baf112009-02-14 01:15:39 -05002621 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002622 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002623
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002624 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2625 break;
2626
Steven Rostedt265c8312009-02-13 12:43:56 -05002627 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2628 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002629
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002630 if (ftrace_match_record(rec, search, search_len, type)) {
2631 /* ensure it is not already in the array */
2632 exists = false;
2633 for (i = 0; i < *idx; i++)
2634 if (array[i] == rec->ip) {
2635 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002636 break;
2637 }
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002638 if (!exists) {
2639 array[(*idx)++] = rec->ip;
2640 found = 1;
2641 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002642 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002643 } while_for_each_ftrace_rec();
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002644
Steven Rostedt52baf112009-02-14 01:15:39 -05002645 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002646
2647 return found ? 0 : -EINVAL;
2648}
2649
2650static ssize_t
2651ftrace_graph_write(struct file *file, const char __user *ubuf,
2652 size_t cnt, loff_t *ppos)
2653{
2654 unsigned char buffer[FTRACE_BUFF_MAX+1];
2655 unsigned long *array;
2656 size_t read = 0;
2657 ssize_t ret;
2658 int index = 0;
2659 char ch;
2660
2661 if (!cnt || cnt < 0)
2662 return 0;
2663
2664 mutex_lock(&graph_lock);
2665
2666 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2667 ret = -EBUSY;
2668 goto out;
2669 }
2670
2671 if (file->f_mode & FMODE_READ) {
2672 struct seq_file *m = file->private_data;
2673 array = m->private;
2674 } else
2675 array = file->private_data;
2676
2677 ret = get_user(ch, ubuf++);
2678 if (ret)
2679 goto out;
2680 read++;
2681 cnt--;
2682
2683 /* skip white space */
2684 while (cnt && isspace(ch)) {
2685 ret = get_user(ch, ubuf++);
2686 if (ret)
2687 goto out;
2688 read++;
2689 cnt--;
2690 }
2691
2692 if (isspace(ch)) {
2693 *ppos += read;
2694 ret = read;
2695 goto out;
2696 }
2697
2698 while (cnt && !isspace(ch)) {
2699 if (index < FTRACE_BUFF_MAX)
2700 buffer[index++] = ch;
2701 else {
2702 ret = -EINVAL;
2703 goto out;
2704 }
2705 ret = get_user(ch, ubuf++);
2706 if (ret)
2707 goto out;
2708 read++;
2709 cnt--;
2710 }
2711 buffer[index] = 0;
2712
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002713 /* we allow only one expression at a time */
2714 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002715 if (ret)
2716 goto out;
2717
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002718 file->f_pos += read;
2719
2720 ret = read;
2721 out:
2722 mutex_unlock(&graph_lock);
2723
2724 return ret;
2725}
2726
2727static const struct file_operations ftrace_graph_fops = {
2728 .open = ftrace_graph_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002729 .read = seq_read,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002730 .write = ftrace_graph_write,
2731};
2732#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2733
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002734static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002735{
Steven Rostedt5072c592008-05-12 21:20:43 +02002736
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002737 trace_create_file("available_filter_functions", 0444,
2738 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002739
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002740 trace_create_file("failures", 0444,
2741 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302742
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002743 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2744 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002745
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002746 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002747 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002748
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002749#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002750 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002751 NULL,
2752 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002753#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2754
Steven Rostedt5072c592008-05-12 21:20:43 +02002755 return 0;
2756}
2757
Steven Rostedt31e88902008-11-14 16:21:19 -08002758static int ftrace_convert_nops(struct module *mod,
2759 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002760 unsigned long *end)
2761{
2762 unsigned long *p;
2763 unsigned long addr;
2764 unsigned long flags;
2765
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002766 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002767 p = start;
2768 while (p < end) {
2769 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002770 /*
2771 * Some architecture linkers will pad between
2772 * the different mcount_loc sections of different
2773 * object files to satisfy alignments.
2774 * Skip any NULL pointers.
2775 */
2776 if (!addr)
2777 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002778 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002779 }
2780
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002781 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002782 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002783 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002784 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002785 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002786
2787 return 0;
2788}
2789
Steven Rostedt93eb6772009-04-15 13:24:06 -04002790#ifdef CONFIG_MODULES
2791void ftrace_release(void *start, void *end)
2792{
2793 struct dyn_ftrace *rec;
2794 struct ftrace_page *pg;
2795 unsigned long s = (unsigned long)start;
2796 unsigned long e = (unsigned long)end;
2797
2798 if (ftrace_disabled || !start || start == end)
2799 return;
2800
2801 mutex_lock(&ftrace_lock);
2802 do_for_each_ftrace_rec(pg, rec) {
2803 if ((rec->ip >= s) && (rec->ip < e)) {
2804 /*
2805 * rec->ip is changed in ftrace_free_rec()
2806 * It should not between s and e if record was freed.
2807 */
2808 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2809 ftrace_free_rec(rec);
2810 }
2811 } while_for_each_ftrace_rec();
2812 mutex_unlock(&ftrace_lock);
2813}
2814
2815static void ftrace_init_module(struct module *mod,
2816 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002817{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002818 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002819 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08002820 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002821}
2822
Steven Rostedt93eb6772009-04-15 13:24:06 -04002823static int ftrace_module_notify(struct notifier_block *self,
2824 unsigned long val, void *data)
2825{
2826 struct module *mod = data;
2827
2828 switch (val) {
2829 case MODULE_STATE_COMING:
2830 ftrace_init_module(mod, mod->ftrace_callsites,
2831 mod->ftrace_callsites +
2832 mod->num_ftrace_callsites);
2833 break;
2834 case MODULE_STATE_GOING:
2835 ftrace_release(mod->ftrace_callsites,
2836 mod->ftrace_callsites +
2837 mod->num_ftrace_callsites);
2838 break;
2839 }
2840
2841 return 0;
2842}
2843#else
2844static int ftrace_module_notify(struct notifier_block *self,
2845 unsigned long val, void *data)
2846{
2847 return 0;
2848}
2849#endif /* CONFIG_MODULES */
2850
2851struct notifier_block ftrace_module_nb = {
2852 .notifier_call = ftrace_module_notify,
2853 .priority = 0,
2854};
2855
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002856extern unsigned long __start_mcount_loc[];
2857extern unsigned long __stop_mcount_loc[];
2858
2859void __init ftrace_init(void)
2860{
2861 unsigned long count, addr, flags;
2862 int ret;
2863
2864 /* Keep the ftrace pointer to the stub */
2865 addr = (unsigned long)ftrace_stub;
2866
2867 local_irq_save(flags);
2868 ftrace_dyn_arch_init(&addr);
2869 local_irq_restore(flags);
2870
2871 /* ftrace_dyn_arch_init places the return code in addr */
2872 if (addr)
2873 goto failed;
2874
2875 count = __stop_mcount_loc - __start_mcount_loc;
2876
2877 ret = ftrace_dyn_table_alloc(count);
2878 if (ret)
2879 goto failed;
2880
2881 last_ftrace_enabled = ftrace_enabled = 1;
2882
Steven Rostedt31e88902008-11-14 16:21:19 -08002883 ret = ftrace_convert_nops(NULL,
2884 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002885 __stop_mcount_loc);
2886
Steven Rostedt93eb6772009-04-15 13:24:06 -04002887 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08002888 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04002889 pr_warning("Failed to register trace ftrace module notifier\n");
2890
Steven Rostedt2af15d62009-05-28 13:37:24 -04002891 set_ftrace_early_filters();
2892
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002893 return;
2894 failed:
2895 ftrace_disabled = 1;
2896}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002897
Steven Rostedt3d083392008-05-12 21:20:42 +02002898#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002899
2900static int __init ftrace_nodyn_init(void)
2901{
2902 ftrace_enabled = 1;
2903 return 0;
2904}
2905device_initcall(ftrace_nodyn_init);
2906
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002907static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2908static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002909/* Keep as macros so we do not need to define the commands */
2910# define ftrace_startup(command) do { } while (0)
2911# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002912# define ftrace_startup_sysctl() do { } while (0)
2913# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002914#endif /* CONFIG_DYNAMIC_FTRACE */
2915
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002916static ssize_t
2917ftrace_pid_read(struct file *file, char __user *ubuf,
2918 size_t cnt, loff_t *ppos)
2919{
2920 char buf[64];
2921 int r;
2922
Steven Rostedte32d8952008-12-04 00:26:41 -05002923 if (ftrace_pid_trace == ftrace_swapper_pid)
2924 r = sprintf(buf, "swapper tasks\n");
2925 else if (ftrace_pid_trace)
Lai Jiangshancc59c9e2009-03-24 11:03:01 +08002926 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002927 else
2928 r = sprintf(buf, "no pid\n");
2929
2930 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2931}
2932
Steven Rostedte32d8952008-12-04 00:26:41 -05002933static void clear_ftrace_swapper(void)
2934{
2935 struct task_struct *p;
2936 int cpu;
2937
2938 get_online_cpus();
2939 for_each_online_cpu(cpu) {
2940 p = idle_task(cpu);
2941 clear_tsk_trace_trace(p);
2942 }
2943 put_online_cpus();
2944}
2945
2946static void set_ftrace_swapper(void)
2947{
2948 struct task_struct *p;
2949 int cpu;
2950
2951 get_online_cpus();
2952 for_each_online_cpu(cpu) {
2953 p = idle_task(cpu);
2954 set_tsk_trace_trace(p);
2955 }
2956 put_online_cpus();
2957}
2958
2959static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002960{
2961 struct task_struct *p;
2962
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002963 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002964 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002965 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002966 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002967 rcu_read_unlock();
2968
Steven Rostedte32d8952008-12-04 00:26:41 -05002969 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002970}
2971
Steven Rostedte32d8952008-12-04 00:26:41 -05002972static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002973{
2974 struct task_struct *p;
2975
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002976 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002977 do_each_pid_task(pid, PIDTYPE_PID, p) {
2978 set_tsk_trace_trace(p);
2979 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002980 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002981}
2982
Steven Rostedte32d8952008-12-04 00:26:41 -05002983static void clear_ftrace_pid_task(struct pid **pid)
2984{
2985 if (*pid == ftrace_swapper_pid)
2986 clear_ftrace_swapper();
2987 else
2988 clear_ftrace_pid(*pid);
2989
2990 *pid = NULL;
2991}
2992
2993static void set_ftrace_pid_task(struct pid *pid)
2994{
2995 if (pid == ftrace_swapper_pid)
2996 set_ftrace_swapper();
2997 else
2998 set_ftrace_pid(pid);
2999}
3000
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003001static ssize_t
3002ftrace_pid_write(struct file *filp, const char __user *ubuf,
3003 size_t cnt, loff_t *ppos)
3004{
Steven Rostedt978f3a42008-12-04 00:26:40 -05003005 struct pid *pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003006 char buf[64];
3007 long val;
3008 int ret;
3009
3010 if (cnt >= sizeof(buf))
3011 return -EINVAL;
3012
3013 if (copy_from_user(&buf, ubuf, cnt))
3014 return -EFAULT;
3015
3016 buf[cnt] = 0;
3017
3018 ret = strict_strtol(buf, 10, &val);
3019 if (ret < 0)
3020 return ret;
3021
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003022 mutex_lock(&ftrace_lock);
Steven Rostedt978f3a42008-12-04 00:26:40 -05003023 if (val < 0) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003024 /* disable pid tracing */
Steven Rostedt978f3a42008-12-04 00:26:40 -05003025 if (!ftrace_pid_trace)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003026 goto out;
Steven Rostedt978f3a42008-12-04 00:26:40 -05003027
3028 clear_ftrace_pid_task(&ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003029
3030 } else {
Steven Rostedte32d8952008-12-04 00:26:41 -05003031 /* swapper task is special */
3032 if (!val) {
3033 pid = ftrace_swapper_pid;
3034 if (pid == ftrace_pid_trace)
3035 goto out;
3036 } else {
3037 pid = find_get_pid(val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003038
Steven Rostedte32d8952008-12-04 00:26:41 -05003039 if (pid == ftrace_pid_trace) {
3040 put_pid(pid);
3041 goto out;
3042 }
Steven Rostedt978f3a42008-12-04 00:26:40 -05003043 }
3044
3045 if (ftrace_pid_trace)
3046 clear_ftrace_pid_task(&ftrace_pid_trace);
3047
3048 if (!pid)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003049 goto out;
3050
Steven Rostedt978f3a42008-12-04 00:26:40 -05003051 ftrace_pid_trace = pid;
Steven Rostedt0ef8cde2008-12-03 15:36:58 -05003052
Steven Rostedt978f3a42008-12-04 00:26:40 -05003053 set_ftrace_pid_task(ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003054 }
3055
3056 /* update the function call */
3057 ftrace_update_pid_func();
3058 ftrace_startup_enable(0);
3059
3060 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003061 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003062
3063 return cnt;
3064}
3065
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003066static const struct file_operations ftrace_pid_fops = {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003067 .read = ftrace_pid_read,
3068 .write = ftrace_pid_write,
3069};
3070
3071static __init int ftrace_init_debugfs(void)
3072{
3073 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003074
3075 d_tracer = tracing_init_dentry();
3076 if (!d_tracer)
3077 return 0;
3078
3079 ftrace_init_dyn_debugfs(d_tracer);
3080
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003081 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3082 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04003083
3084 ftrace_profile_debugfs(d_tracer);
3085
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003086 return 0;
3087}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003088fs_initcall(ftrace_init_debugfs);
3089
Steven Rostedt3d083392008-05-12 21:20:42 +02003090/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003091 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003092 *
3093 * This function should be used by panic code. It stops ftrace
3094 * but in a not so nice way. If you need to simply kill ftrace
3095 * from a non-atomic section, use ftrace_kill.
3096 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003097void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003098{
3099 ftrace_disabled = 1;
3100 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003101 clear_ftrace_function();
3102}
3103
3104/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003105 * register_ftrace_function - register a function for profiling
3106 * @ops - ops structure that holds the function for profiling.
3107 *
3108 * Register a function to be called by all functions in the
3109 * kernel.
3110 *
3111 * Note: @ops->func and all the functions it calls must be labeled
3112 * with "notrace", otherwise it will go into a
3113 * recursive loop.
3114 */
3115int register_ftrace_function(struct ftrace_ops *ops)
3116{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003117 int ret;
3118
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003119 if (unlikely(ftrace_disabled))
3120 return -1;
3121
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003122 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003123
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003124 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003125 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003126
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003127 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003128 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003129}
3130
3131/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003132 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003133 * @ops - ops structure that holds the function to unregister
3134 *
3135 * Unregister a function that was added to be called by ftrace profiling.
3136 */
3137int unregister_ftrace_function(struct ftrace_ops *ops)
3138{
3139 int ret;
3140
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003141 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003142 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003143 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003144 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003145
3146 return ret;
3147}
3148
Ingo Molnare309b412008-05-12 21:20:51 +02003149int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003150ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02003151 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003152 loff_t *ppos)
3153{
3154 int ret;
3155
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003156 if (unlikely(ftrace_disabled))
3157 return -ENODEV;
3158
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003159 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003160
Steven Rostedt5072c592008-05-12 21:20:43 +02003161 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003162
3163 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3164 goto out;
3165
3166 last_ftrace_enabled = ftrace_enabled;
3167
3168 if (ftrace_enabled) {
3169
3170 ftrace_startup_sysctl();
3171
3172 /* we are starting ftrace again */
3173 if (ftrace_list != &ftrace_list_end) {
3174 if (ftrace_list->next == &ftrace_list_end)
3175 ftrace_trace_function = ftrace_list->func;
3176 else
3177 ftrace_trace_function = ftrace_list_func;
3178 }
3179
3180 } else {
3181 /* stopping ftrace calls (just send to ftrace_stub) */
3182 ftrace_trace_function = ftrace_stub;
3183
3184 ftrace_shutdown_sysctl();
3185 }
3186
3187 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003188 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003189 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003190}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003191
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003192#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003193
Steven Rostedt597af812009-04-03 15:24:12 -04003194static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003195static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003196
Steven Rostedte49dc192008-12-02 23:50:05 -05003197int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3198{
3199 return 0;
3200}
3201
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003202/* The callbacks that hook a function */
3203trace_func_graph_ret_t ftrace_graph_return =
3204 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003205trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003206
3207/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3208static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3209{
3210 int i;
3211 int ret = 0;
3212 unsigned long flags;
3213 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3214 struct task_struct *g, *t;
3215
3216 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3217 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3218 * sizeof(struct ftrace_ret_stack),
3219 GFP_KERNEL);
3220 if (!ret_stack_list[i]) {
3221 start = 0;
3222 end = i;
3223 ret = -ENOMEM;
3224 goto free;
3225 }
3226 }
3227
3228 read_lock_irqsave(&tasklist_lock, flags);
3229 do_each_thread(g, t) {
3230 if (start == end) {
3231 ret = -EAGAIN;
3232 goto unlock;
3233 }
3234
3235 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003236 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003237 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04003238 t->curr_ret_stack = -1;
3239 /* Make sure the tasks see the -1 first: */
3240 smp_wmb();
3241 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003242 }
3243 } while_each_thread(g, t);
3244
3245unlock:
3246 read_unlock_irqrestore(&tasklist_lock, flags);
3247free:
3248 for (i = start; i < end; i++)
3249 kfree(ret_stack_list[i]);
3250 return ret;
3251}
3252
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003253static void
3254ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3255 struct task_struct *next)
3256{
3257 unsigned long long timestamp;
3258 int index;
3259
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003260 /*
3261 * Does the user want to count the time a function was asleep.
3262 * If so, do not update the time stamps.
3263 */
3264 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3265 return;
3266
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003267 timestamp = trace_clock_local();
3268
3269 prev->ftrace_timestamp = timestamp;
3270
3271 /* only process tasks that we timestamped */
3272 if (!next->ftrace_timestamp)
3273 return;
3274
3275 /*
3276 * Update all the counters in next to make up for the
3277 * time next was sleeping.
3278 */
3279 timestamp -= next->ftrace_timestamp;
3280
3281 for (index = next->curr_ret_stack; index >= 0; index--)
3282 next->ret_stack[index].calltime += timestamp;
3283}
3284
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003285/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003286static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003287{
3288 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003289 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003290
3291 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3292 sizeof(struct ftrace_ret_stack *),
3293 GFP_KERNEL);
3294
3295 if (!ret_stack_list)
3296 return -ENOMEM;
3297
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003298 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04003299 for_each_online_cpu(cpu) {
3300 if (!idle_task(cpu)->ret_stack)
3301 ftrace_graph_init_task(idle_task(cpu));
3302 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003303
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003304 do {
3305 ret = alloc_retstack_tasklist(ret_stack_list);
3306 } while (ret == -EAGAIN);
3307
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003308 if (!ret) {
3309 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3310 if (ret)
3311 pr_info("ftrace_graph: Couldn't activate tracepoint"
3312 " probe to kernel_sched_switch\n");
3313 }
3314
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003315 kfree(ret_stack_list);
3316 return ret;
3317}
3318
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003319/*
3320 * Hibernation protection.
3321 * The state of the current task is too much unstable during
3322 * suspend/restore to disk. We want to protect against that.
3323 */
3324static int
3325ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3326 void *unused)
3327{
3328 switch (state) {
3329 case PM_HIBERNATION_PREPARE:
3330 pause_graph_tracing();
3331 break;
3332
3333 case PM_POST_HIBERNATION:
3334 unpause_graph_tracing();
3335 break;
3336 }
3337 return NOTIFY_DONE;
3338}
3339
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003340int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3341 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003342{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003343 int ret = 0;
3344
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003345 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003346
Steven Rostedt05ce5812009-03-24 00:18:31 -04003347 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003348 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003349 ret = -EBUSY;
3350 goto out;
3351 }
3352
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003353 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3354 register_pm_notifier(&ftrace_suspend_notifier);
3355
Steven Rostedt597af812009-04-03 15:24:12 -04003356 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003357 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003358 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003359 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003360 goto out;
3361 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003362
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003363 ftrace_graph_return = retfunc;
3364 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003365
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003366 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003367
3368out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003369 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003370 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003371}
3372
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003373void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003374{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003375 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003376
Steven Rostedt597af812009-04-03 15:24:12 -04003377 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003378 goto out;
3379
Steven Rostedt597af812009-04-03 15:24:12 -04003380 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003381 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003382 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003383 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003384 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003385 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003386
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003387 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003388 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003389}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003390
3391/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003392void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003393{
Steven Rostedt84047e32009-06-02 16:51:55 -04003394 /* Make sure we do not use the parent ret_stack */
3395 t->ret_stack = NULL;
3396
Steven Rostedt597af812009-04-03 15:24:12 -04003397 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04003398 struct ftrace_ret_stack *ret_stack;
3399
3400 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003401 * sizeof(struct ftrace_ret_stack),
3402 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04003403 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003404 return;
3405 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003406 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003407 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003408 t->ftrace_timestamp = 0;
Steven Rostedt82310a32009-06-02 12:26:07 -04003409 /* make curr_ret_stack visable before we add the ret_stack */
3410 smp_wmb();
3411 t->ret_stack = ret_stack;
Steven Rostedt84047e32009-06-02 16:51:55 -04003412 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003413}
3414
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003415void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003416{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003417 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3418
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003419 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003420 /* NULL must become visible to IRQs before we free it: */
3421 barrier();
3422
3423 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003424}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003425
3426void ftrace_graph_stop(void)
3427{
3428 ftrace_stop();
3429}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003430#endif
3431