blob: ed1fc5021d441d15e97f0c6e3da932347b6727c9 [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 Rostedt8aef2d22009-03-24 01:10:15 -040032#include <trace/sched.h>
33
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034#include <asm/ftrace.h>
35
Steven Rostedt0706f1c2009-03-23 23:12:58 -040036#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040037#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020038
Steven Rostedt69128962008-10-23 09:33:03 -040039#define FTRACE_WARN_ON(cond) \
40 do { \
41 if (WARN_ON(cond)) \
42 ftrace_kill(); \
43 } while (0)
44
45#define FTRACE_WARN_ON_ONCE(cond) \
46 do { \
47 if (WARN_ON_ONCE(cond)) \
48 ftrace_kill(); \
49 } while (0)
50
Steven Rostedt8fc0c702009-02-16 15:28:00 -050051/* hash bits for specific function selection */
52#define FTRACE_HASH_BITS 7
53#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
54
Steven Rostedt4eebcc82008-05-12 21:20:48 +020055/* ftrace_enabled is a method to turn ftrace on or off */
56int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020057static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020058
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050059/* Quick disabling of function tracer. */
60int function_trace_stop;
61
Steven Rostedt4eebcc82008-05-12 21:20:48 +020062/*
63 * ftrace_disabled is set when an anomaly is discovered.
64 * ftrace_disabled is much stronger than ftrace_enabled.
65 */
66static int ftrace_disabled __read_mostly;
67
Steven Rostedt52baf112009-02-14 01:15:39 -050068static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020069
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020070static struct ftrace_ops ftrace_list_end __read_mostly =
71{
72 .func = ftrace_stub,
73};
74
75static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
76ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050077ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050078ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020079
Ingo Molnarf2252932008-05-22 10:37:48 +020080static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020081{
82 struct ftrace_ops *op = ftrace_list;
83
84 /* in case someone actually ports this to alpha! */
85 read_barrier_depends();
86
87 while (op != &ftrace_list_end) {
88 /* silly alpha */
89 read_barrier_depends();
90 op->func(ip, parent_ip);
91 op = op->next;
92 };
93}
94
Steven Rostedtdf4fc312008-11-26 00:16:23 -050095static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
96{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -050097 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -050098 return;
99
100 ftrace_pid_function(ip, parent_ip);
101}
102
103static void set_ftrace_pid_function(ftrace_func_t func)
104{
105 /* do not set ftrace_pid_function to itself! */
106 if (func != ftrace_pid_func)
107 ftrace_pid_function = func;
108}
109
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200110/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200111 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200112 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200113 * This NULLs the ftrace function and in essence stops
114 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200116void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117{
Steven Rostedt3d083392008-05-12 21:20:42 +0200118 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500119 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500120 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200121}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500123#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
124/*
125 * For those archs that do not test ftrace_trace_stop in their
126 * mcount call site, we need to do it from C.
127 */
128static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
129{
130 if (function_trace_stop)
131 return;
132
133 __ftrace_trace_function(ip, parent_ip);
134}
135#endif
136
Ingo Molnare309b412008-05-12 21:20:51 +0200137static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200138{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200139 ops->next = ftrace_list;
140 /*
141 * We are entering ops into the ftrace_list but another
142 * CPU might be walking that list. We need to make sure
143 * the ops->next pointer is valid before another CPU sees
144 * the ops pointer included into the ftrace_list.
145 */
146 smp_wmb();
147 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200148
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200149 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500150 ftrace_func_t func;
151
152 if (ops->next == &ftrace_list_end)
153 func = ops->func;
154 else
155 func = ftrace_list_func;
156
Steven Rostedt978f3a42008-12-04 00:26:40 -0500157 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158 set_ftrace_pid_function(func);
159 func = ftrace_pid_func;
160 }
161
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200162 /*
163 * For one func, simply call it directly.
164 * For more than one func, call the chain.
165 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500166#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500167 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500168#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500169 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500170 ftrace_trace_function = ftrace_test_stop_func;
171#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200172 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200173
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200174 return 0;
175}
176
Ingo Molnare309b412008-05-12 21:20:51 +0200177static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200178{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200179 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200180
181 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200182 * If we are removing the last function, then simply point
183 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200184 */
185 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
186 ftrace_trace_function = ftrace_stub;
187 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500188 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189 }
190
191 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
192 if (*p == ops)
193 break;
194
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500195 if (*p != ops)
196 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197
198 *p = (*p)->next;
199
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200200 if (ftrace_enabled) {
201 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500202 if (ftrace_list->next == &ftrace_list_end) {
203 ftrace_func_t func = ftrace_list->func;
204
Steven Rostedt978f3a42008-12-04 00:26:40 -0500205 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500206 set_ftrace_pid_function(func);
207 func = ftrace_pid_func;
208 }
209#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
210 ftrace_trace_function = func;
211#else
212 __ftrace_trace_function = func;
213#endif
214 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200215 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200216
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500217 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200218}
219
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500220static void ftrace_update_pid_func(void)
221{
222 ftrace_func_t func;
223
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500224 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900225 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500226
227 func = ftrace_trace_function;
228
Steven Rostedt978f3a42008-12-04 00:26:40 -0500229 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500230 set_ftrace_pid_function(func);
231 func = ftrace_pid_func;
232 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800233 if (func == ftrace_pid_func)
234 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500235 }
236
237#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
238 ftrace_trace_function = func;
239#else
240 __ftrace_trace_function = func;
241#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500242}
243
Steven Rostedt493762f2009-03-23 17:12:36 -0400244#ifdef CONFIG_FUNCTION_PROFILER
245struct ftrace_profile {
246 struct hlist_node node;
247 unsigned long ip;
248 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400249#ifdef CONFIG_FUNCTION_GRAPH_TRACER
250 unsigned long long time;
251#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400252};
253
254struct ftrace_profile_page {
255 struct ftrace_profile_page *next;
256 unsigned long index;
257 struct ftrace_profile records[];
258};
259
Steven Rostedtcafb1682009-03-24 20:50:39 -0400260struct ftrace_profile_stat {
261 atomic_t disabled;
262 struct hlist_head *hash;
263 struct ftrace_profile_page *pages;
264 struct ftrace_profile_page *start;
265 struct tracer_stat stat;
266};
267
Steven Rostedt493762f2009-03-23 17:12:36 -0400268#define PROFILE_RECORDS_SIZE \
269 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
270
271#define PROFILES_PER_PAGE \
272 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
273
Steven Rostedt493762f2009-03-23 17:12:36 -0400274static int ftrace_profile_bits;
275static int ftrace_profile_enabled;
276static DEFINE_MUTEX(ftrace_profile_lock);
277
Steven Rostedtcafb1682009-03-24 20:50:39 -0400278static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400279
280#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
281
Steven Rostedt493762f2009-03-23 17:12:36 -0400282static void *
283function_stat_next(void *v, int idx)
284{
285 struct ftrace_profile *rec = v;
286 struct ftrace_profile_page *pg;
287
288 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
289
290 again:
291 rec++;
292 if ((void *)rec >= (void *)&pg->records[pg->index]) {
293 pg = pg->next;
294 if (!pg)
295 return NULL;
296 rec = &pg->records[0];
297 if (!rec->counter)
298 goto again;
299 }
300
301 return rec;
302}
303
304static void *function_stat_start(struct tracer_stat *trace)
305{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400306 struct ftrace_profile_stat *stat =
307 container_of(trace, struct ftrace_profile_stat, stat);
308
309 if (!stat || !stat->start)
310 return NULL;
311
312 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400313}
314
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400315#ifdef CONFIG_FUNCTION_GRAPH_TRACER
316/* function graph compares on total time */
317static int function_stat_cmp(void *p1, void *p2)
318{
319 struct ftrace_profile *a = p1;
320 struct ftrace_profile *b = p2;
321
322 if (a->time < b->time)
323 return -1;
324 if (a->time > b->time)
325 return 1;
326 else
327 return 0;
328}
329#else
330/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400331static int function_stat_cmp(void *p1, void *p2)
332{
333 struct ftrace_profile *a = p1;
334 struct ftrace_profile *b = p2;
335
336 if (a->counter < b->counter)
337 return -1;
338 if (a->counter > b->counter)
339 return 1;
340 else
341 return 0;
342}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400343#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400344
345static int function_stat_headers(struct seq_file *m)
346{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400347#ifdef CONFIG_FUNCTION_GRAPH_TRACER
348 seq_printf(m, " Function Hit Time\n"
349 " -------- --- ----\n");
350#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400351 seq_printf(m, " Function Hit\n"
352 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400353#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400354 return 0;
355}
356
357static int function_stat_show(struct seq_file *m, void *v)
358{
359 struct ftrace_profile *rec = v;
360 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400361#ifdef CONFIG_FUNCTION_GRAPH_TRACER
362 static struct trace_seq s;
363 static DEFINE_MUTEX(mutex);
364
365 mutex_lock(&mutex);
366 trace_seq_init(&s);
367 trace_print_graph_duration(rec->time, &s);
368#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400369
370 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400371 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400372
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400373#ifdef CONFIG_FUNCTION_GRAPH_TRACER
374 seq_printf(m, " ");
375 trace_print_seq(m, &s);
376 mutex_unlock(&mutex);
377#endif
378 seq_putc(m, '\n');
379
Steven Rostedt493762f2009-03-23 17:12:36 -0400380 return 0;
381}
382
Steven Rostedtcafb1682009-03-24 20:50:39 -0400383static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400384{
385 struct ftrace_profile_page *pg;
386
Steven Rostedtcafb1682009-03-24 20:50:39 -0400387 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400388
389 while (pg) {
390 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
391 pg->index = 0;
392 pg = pg->next;
393 }
394
Steven Rostedtcafb1682009-03-24 20:50:39 -0400395 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400396 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
397}
398
Steven Rostedtcafb1682009-03-24 20:50:39 -0400399int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400400{
401 struct ftrace_profile_page *pg;
402 int i;
403
404 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400405 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400406 return 0;
407
Steven Rostedtcafb1682009-03-24 20:50:39 -0400408 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
409 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400410 return -ENOMEM;
411
Steven Rostedtcafb1682009-03-24 20:50:39 -0400412 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400413
414 /* allocate 10 more pages to start */
415 for (i = 0; i < 10; i++) {
416 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
417 /*
418 * We only care about allocating profile_pages, if
419 * we failed to allocate here, hopefully we will allocate
420 * later.
421 */
422 if (!pg->next)
423 break;
424 pg = pg->next;
425 }
426
427 return 0;
428}
429
Steven Rostedtcafb1682009-03-24 20:50:39 -0400430static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400431{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400432 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400433 int size;
434
Steven Rostedtcafb1682009-03-24 20:50:39 -0400435 stat = &per_cpu(ftrace_profile_stats, cpu);
436
437 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400438 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400439 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400440 return 0;
441 }
442
443 /*
444 * We are profiling all functions, but usually only a few thousand
445 * functions are hit. We'll make a hash of 1024 items.
446 */
447 size = FTRACE_PROFILE_HASH_SIZE;
448
Steven Rostedtcafb1682009-03-24 20:50:39 -0400449 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400450
Steven Rostedtcafb1682009-03-24 20:50:39 -0400451 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400452 return -ENOMEM;
453
Steven Rostedtcafb1682009-03-24 20:50:39 -0400454 if (!ftrace_profile_bits) {
455 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400456
Steven Rostedtcafb1682009-03-24 20:50:39 -0400457 for (; size; size >>= 1)
458 ftrace_profile_bits++;
459 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400460
461 /* Preallocate a few pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400462 if (ftrace_profile_pages_init(stat) < 0) {
463 kfree(stat->hash);
464 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400465 return -ENOMEM;
466 }
467
468 return 0;
469}
470
Steven Rostedtcafb1682009-03-24 20:50:39 -0400471static int ftrace_profile_init(void)
472{
473 int cpu;
474 int ret = 0;
475
476 for_each_online_cpu(cpu) {
477 ret = ftrace_profile_init_cpu(cpu);
478 if (ret)
479 break;
480 }
481
482 return ret;
483}
484
Steven Rostedt493762f2009-03-23 17:12:36 -0400485/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486static struct ftrace_profile *
487ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400488{
489 struct ftrace_profile *rec;
490 struct hlist_head *hhd;
491 struct hlist_node *n;
492 unsigned long key;
493
494 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400496
497 if (hlist_empty(hhd))
498 return NULL;
499
500 hlist_for_each_entry_rcu(rec, n, hhd, node) {
501 if (rec->ip == ip)
502 return rec;
503 }
504
505 return NULL;
506}
507
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508static void ftrace_add_profile(struct ftrace_profile_stat *stat,
509 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400510{
511 unsigned long key;
512
513 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400514 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400515}
516
517/* Interrupts must be disabled calling this */
518static struct ftrace_profile *
Steven Rostedtcafb1682009-03-24 20:50:39 -0400519ftrace_profile_alloc(struct ftrace_profile_stat *stat,
520 unsigned long ip, bool alloc_safe)
Steven Rostedt493762f2009-03-23 17:12:36 -0400521{
522 struct ftrace_profile *rec = NULL;
523
524 /* prevent recursion */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400526 goto out;
527
Steven Rostedt493762f2009-03-23 17:12:36 -0400528 /* Try to always keep another page available */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400529 if (!stat->pages->next && alloc_safe)
530 stat->pages->next = (void *)get_zeroed_page(GFP_ATOMIC);
Steven Rostedt493762f2009-03-23 17:12:36 -0400531
532 /*
533 * Try to find the function again since another
534 * task on another CPU could have added it
535 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400536 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400537 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400539
Steven Rostedtcafb1682009-03-24 20:50:39 -0400540 if (stat->pages->index == PROFILES_PER_PAGE) {
541 if (!stat->pages->next)
542 goto out;
543 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400544 }
545
Steven Rostedtcafb1682009-03-24 20:50:39 -0400546 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400547 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400548 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400549
Steven Rostedt493762f2009-03-23 17:12:36 -0400550 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400551 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400552
553 return rec;
554}
555
556/*
557 * If we are not in an interrupt, or softirq and
558 * and interrupts are disabled and preemption is not enabled
559 * (not in a spinlock) then it should be safe to allocate memory.
560 */
561static bool ftrace_safe_to_allocate(void)
562{
563 return !in_interrupt() && irqs_disabled() && !preempt_count();
564}
565
566static void
567function_profile_call(unsigned long ip, unsigned long parent_ip)
568{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400569 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400570 struct ftrace_profile *rec;
571 unsigned long flags;
572 bool alloc_safe;
573
574 if (!ftrace_profile_enabled)
575 return;
576
577 alloc_safe = ftrace_safe_to_allocate();
578
579 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400580
581 stat = &__get_cpu_var(ftrace_profile_stats);
582 if (!stat->hash)
583 goto out;
584
585 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 if (!rec) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587 rec = ftrace_profile_alloc(stat, ip, alloc_safe);
Steven Rostedt493762f2009-03-23 17:12:36 -0400588 if (!rec)
589 goto out;
590 }
591
592 rec->counter++;
593 out:
594 local_irq_restore(flags);
595}
596
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597#ifdef CONFIG_FUNCTION_GRAPH_TRACER
598static int profile_graph_entry(struct ftrace_graph_ent *trace)
599{
600 function_profile_call(trace->func, 0);
601 return 1;
602}
603
604static void profile_graph_return(struct ftrace_graph_ret *trace)
605{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 struct ftrace_profile_stat *stat;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400607 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400608 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400609
610 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 stat = &__get_cpu_var(ftrace_profile_stats);
612 if (!stat->hash)
613 goto out;
614
615 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400616 if (rec)
617 rec->time += trace->rettime - trace->calltime;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400618 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619 local_irq_restore(flags);
620}
621
622static int register_ftrace_profiler(void)
623{
624 return register_ftrace_graph(&profile_graph_return,
625 &profile_graph_entry);
626}
627
628static void unregister_ftrace_profiler(void)
629{
630 unregister_ftrace_graph();
631}
632#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400633static struct ftrace_ops ftrace_profile_ops __read_mostly =
634{
635 .func = function_profile_call,
636};
637
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400638static int register_ftrace_profiler(void)
639{
640 return register_ftrace_function(&ftrace_profile_ops);
641}
642
643static void unregister_ftrace_profiler(void)
644{
645 unregister_ftrace_function(&ftrace_profile_ops);
646}
647#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
648
Steven Rostedt493762f2009-03-23 17:12:36 -0400649static ssize_t
650ftrace_profile_write(struct file *filp, const char __user *ubuf,
651 size_t cnt, loff_t *ppos)
652{
653 unsigned long val;
654 char buf[64];
655 int ret;
656
657 if (cnt >= sizeof(buf))
658 return -EINVAL;
659
660 if (copy_from_user(&buf, ubuf, cnt))
661 return -EFAULT;
662
663 buf[cnt] = 0;
664
665 ret = strict_strtoul(buf, 10, &val);
666 if (ret < 0)
667 return ret;
668
669 val = !!val;
670
671 mutex_lock(&ftrace_profile_lock);
672 if (ftrace_profile_enabled ^ val) {
673 if (val) {
674 ret = ftrace_profile_init();
675 if (ret < 0) {
676 cnt = ret;
677 goto out;
678 }
679
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400680 ret = register_ftrace_profiler();
681 if (ret < 0) {
682 cnt = ret;
683 goto out;
684 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400685 ftrace_profile_enabled = 1;
686 } else {
687 ftrace_profile_enabled = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400688 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400689 }
690 }
691 out:
692 mutex_unlock(&ftrace_profile_lock);
693
694 filp->f_pos += cnt;
695
696 return cnt;
697}
698
699static ssize_t
700ftrace_profile_read(struct file *filp, char __user *ubuf,
701 size_t cnt, loff_t *ppos)
702{
703 char buf[64];
704 int r;
705
706 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
707 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
708}
709
710static const struct file_operations ftrace_profile_fops = {
711 .open = tracing_open_generic,
712 .read = ftrace_profile_read,
713 .write = ftrace_profile_write,
714};
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716/* used to initialize the real stat files */
717static struct tracer_stat function_stats __initdata = {
718 .name = "functions",
719 .stat_start = function_stat_start,
720 .stat_next = function_stat_next,
721 .stat_cmp = function_stat_cmp,
722 .stat_headers = function_stat_headers,
723 .stat_show = function_stat_show
724};
725
Steven Rostedt493762f2009-03-23 17:12:36 -0400726static void ftrace_profile_debugfs(struct dentry *d_tracer)
727{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400731 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400732 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400733
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 for_each_possible_cpu(cpu) {
735 stat = &per_cpu(ftrace_profile_stats, cpu);
736
737 /* allocate enough for function name + cpu number */
738 name = kmalloc(32, GFP_KERNEL);
739 if (!name) {
740 /*
741 * The files created are permanent, if something happens
742 * we still do not free memory.
743 */
744 kfree(stat);
745 WARN(1,
746 "Could not allocate stat file for cpu %d\n",
747 cpu);
748 return;
749 }
750 stat->stat = function_stats;
751 snprintf(name, 32, "function%d", cpu);
752 stat->stat.name = name;
753 ret = register_stat_tracer(&stat->stat);
754 if (ret) {
755 WARN(1,
756 "Could not register function stat for cpu %d\n",
757 cpu);
758 kfree(name);
759 return;
760 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 }
762
763 entry = debugfs_create_file("function_profile_enabled", 0644,
764 d_tracer, NULL, &ftrace_profile_fops);
765 if (!entry)
766 pr_warning("Could not create debugfs "
767 "'function_profile_enabled' entry\n");
768}
769
770#else /* CONFIG_FUNCTION_PROFILER */
771static void ftrace_profile_debugfs(struct dentry *d_tracer)
772{
773}
774#endif /* CONFIG_FUNCTION_PROFILER */
775
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100776/* set when tracing only a pid */
777struct pid *ftrace_pid_trace;
778static struct pid * const ftrace_swapper_pid = &init_struct_pid;
779
Steven Rostedt3d083392008-05-12 21:20:42 +0200780#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100781
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400782#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400783# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400784#endif
785
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500786static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
787
Steven Rostedtb6887d72009-02-17 12:32:04 -0500788struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500789 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500790 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500791 unsigned long flags;
792 unsigned long ip;
793 void *data;
794 struct rcu_head rcu;
795};
796
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200797enum {
798 FTRACE_ENABLE_CALLS = (1 << 0),
799 FTRACE_DISABLE_CALLS = (1 << 1),
800 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
801 FTRACE_ENABLE_MCOUNT = (1 << 3),
802 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500803 FTRACE_START_FUNC_RET = (1 << 5),
804 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200805};
806
Steven Rostedt5072c592008-05-12 21:20:43 +0200807static int ftrace_filtered;
808
Lai Jiangshane94142a2009-03-13 17:51:27 +0800809static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200810
Steven Rostedt41c52c02008-05-22 11:46:33 -0400811static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200812
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200813struct ftrace_page {
814 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500815 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200816 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700817};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200818
819#define ENTRIES_PER_PAGE \
820 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
821
822/* estimate from running different kernels */
823#define NR_TO_INIT 10000
824
825static struct ftrace_page *ftrace_pages_start;
826static struct ftrace_page *ftrace_pages;
827
Steven Rostedt37ad5082008-05-12 21:20:48 +0200828static struct dyn_ftrace *ftrace_free_records;
829
Steven Rostedt265c8312009-02-13 12:43:56 -0500830/*
831 * This is a double for. Do not use 'break' to break out of the loop,
832 * you must use a goto.
833 */
834#define do_for_each_ftrace_rec(pg, rec) \
835 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
836 int _____i; \
837 for (_____i = 0; _____i < pg->index; _____i++) { \
838 rec = &pg->records[_____i];
839
840#define while_for_each_ftrace_rec() \
841 } \
842 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530843
844#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200845
846static int frozen_record_count;
847
Abhishek Sagarecea6562008-06-21 23:47:53 +0530848static inline void freeze_record(struct dyn_ftrace *rec)
849{
850 if (!(rec->flags & FTRACE_FL_FROZEN)) {
851 rec->flags |= FTRACE_FL_FROZEN;
852 frozen_record_count++;
853 }
854}
855
856static inline void unfreeze_record(struct dyn_ftrace *rec)
857{
858 if (rec->flags & FTRACE_FL_FROZEN) {
859 rec->flags &= ~FTRACE_FL_FROZEN;
860 frozen_record_count--;
861 }
862}
863
864static inline int record_frozen(struct dyn_ftrace *rec)
865{
866 return rec->flags & FTRACE_FL_FROZEN;
867}
868#else
869# define freeze_record(rec) ({ 0; })
870# define unfreeze_record(rec) ({ 0; })
871# define record_frozen(rec) ({ 0; })
872#endif /* CONFIG_KPROBES */
873
Ingo Molnare309b412008-05-12 21:20:51 +0200874static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200875{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800876 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200877 ftrace_free_records = rec;
878 rec->flags |= FTRACE_FL_FREE;
879}
880
Steven Rostedtfed19392008-08-14 22:47:19 -0400881void ftrace_release(void *start, unsigned long size)
882{
883 struct dyn_ftrace *rec;
884 struct ftrace_page *pg;
885 unsigned long s = (unsigned long)start;
886 unsigned long e = s + size;
Steven Rostedtfed19392008-08-14 22:47:19 -0400887
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400888 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400889 return;
890
Steven Rostedt52baf112009-02-14 01:15:39 -0500891 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -0500892 do_for_each_ftrace_rec(pg, rec) {
Zhaoleib00f0b62009-03-13 17:14:01 +0800893 if ((rec->ip >= s) && (rec->ip < e) &&
Steven Rostedt493762f2009-03-23 17:12:36 -0400894 !(rec->flags & FTRACE_FL_FREE))
Steven Rostedt265c8312009-02-13 12:43:56 -0500895 ftrace_free_rec(rec);
896 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -0500897 mutex_unlock(&ftrace_lock);
Steven Rostedtfed19392008-08-14 22:47:19 -0400898}
899
Ingo Molnare309b412008-05-12 21:20:51 +0200900static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200901{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200902 struct dyn_ftrace *rec;
903
904 /* First check for freed records */
905 if (ftrace_free_records) {
906 rec = ftrace_free_records;
907
Steven Rostedt37ad5082008-05-12 21:20:48 +0200908 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400909 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200910 ftrace_free_records = NULL;
911 return NULL;
912 }
913
Lai Jiangshanee000b72009-03-24 13:38:06 +0800914 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200915 memset(rec, 0, sizeof(*rec));
916 return rec;
917 }
918
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200919 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400920 if (!ftrace_pages->next) {
921 /* allocate another page */
922 ftrace_pages->next =
923 (void *)get_zeroed_page(GFP_KERNEL);
924 if (!ftrace_pages->next)
925 return NULL;
926 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200927 ftrace_pages = ftrace_pages->next;
928 }
929
930 return &ftrace_pages->records[ftrace_pages->index++];
931}
932
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400933static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200934ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200935{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400936 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200937
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800938 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400939 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200940
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400941 rec = ftrace_alloc_dyn_node(ip);
942 if (!rec)
943 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200944
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400945 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800946 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800947 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200948
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400949 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200950}
951
Steven Rostedt05736a42008-09-22 14:55:47 -0700952static void print_ip_ins(const char *fmt, unsigned char *p)
953{
954 int i;
955
956 printk(KERN_CONT "%s", fmt);
957
958 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
959 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
960}
961
Steven Rostedt31e88902008-11-14 16:21:19 -0800962static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800963{
964 switch (failed) {
965 case -EFAULT:
966 FTRACE_WARN_ON_ONCE(1);
967 pr_info("ftrace faulted on modifying ");
968 print_ip_sym(ip);
969 break;
970 case -EINVAL:
971 FTRACE_WARN_ON_ONCE(1);
972 pr_info("ftrace failed to modify ");
973 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800974 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800975 printk(KERN_CONT "\n");
976 break;
977 case -EPERM:
978 FTRACE_WARN_ON_ONCE(1);
979 pr_info("ftrace faulted on writing ");
980 print_ip_sym(ip);
981 break;
982 default:
983 FTRACE_WARN_ON_ONCE(1);
984 pr_info("ftrace faulted on unknown error ");
985 print_ip_sym(ip);
986 }
987}
988
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200989
Abhishek Sagar492a7ea52008-05-25 00:10:04 +0530990static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800991__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200992{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100993 unsigned long ftrace_addr;
Steven Rostedt6a24a242009-02-17 11:20:26 -0500994 unsigned long ip, fl;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100995
Shaohua Lif0001202009-01-09 11:29:42 +0800996 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +0200997
998 ip = rec->ip;
999
Steven Rostedt982c3502008-11-15 16:31:41 -05001000 /*
1001 * If this record is not to be traced and
1002 * it is not enabled then do nothing.
1003 *
1004 * If this record is not to be traced and
Wenji Huang57794a92009-02-06 17:33:27 +08001005 * it is enabled then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001006 *
1007 */
1008 if (rec->flags & FTRACE_FL_NOTRACE) {
1009 if (rec->flags & FTRACE_FL_ENABLED)
1010 rec->flags &= ~FTRACE_FL_ENABLED;
1011 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001012 return 0;
1013
Steven Rostedt982c3502008-11-15 16:31:41 -05001014 } else if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001015 /*
Steven Rostedt982c3502008-11-15 16:31:41 -05001016 * Filtering is on:
Steven Rostedt5072c592008-05-12 21:20:43 +02001017 */
Steven Rostedt982c3502008-11-15 16:31:41 -05001018
1019 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1020
1021 /* Record is filtered and enabled, do nothing */
1022 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1023 return 0;
1024
Wenji Huang57794a92009-02-06 17:33:27 +08001025 /* Record is not filtered or enabled, do nothing */
Steven Rostedt982c3502008-11-15 16:31:41 -05001026 if (!fl)
1027 return 0;
1028
1029 /* Record is not filtered but enabled, disable it */
1030 if (fl == FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +02001031 rec->flags &= ~FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -05001032 else
1033 /* Otherwise record is filtered but not enabled, enable it */
Steven Rostedt5072c592008-05-12 21:20:43 +02001034 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001035 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -05001036 /* Disable or not filtered */
Steven Rostedt5072c592008-05-12 21:20:43 +02001037
1038 if (enable) {
Steven Rostedt982c3502008-11-15 16:31:41 -05001039 /* if record is enabled, do nothing */
Steven Rostedt41c52c02008-05-22 11:46:33 -04001040 if (rec->flags & FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +02001041 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -05001042
Steven Rostedt41c52c02008-05-22 11:46:33 -04001043 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -05001044
Steven Rostedt5072c592008-05-12 21:20:43 +02001045 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -05001046
Wenji Huang57794a92009-02-06 17:33:27 +08001047 /* if record is not enabled, do nothing */
Steven Rostedt5072c592008-05-12 21:20:43 +02001048 if (!(rec->flags & FTRACE_FL_ENABLED))
1049 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -05001050
Steven Rostedt5072c592008-05-12 21:20:43 +02001051 rec->flags &= ~FTRACE_FL_ENABLED;
1052 }
1053 }
1054
Steven Rostedt982c3502008-11-15 16:31:41 -05001055 if (rec->flags & FTRACE_FL_ENABLED)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001056 return ftrace_make_call(rec, ftrace_addr);
Steven Rostedt31e88902008-11-14 16:21:19 -08001057 else
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001058 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001059}
1060
1061static void ftrace_replace_code(int enable)
1062{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001063 struct dyn_ftrace *rec;
1064 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001065 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001066
Steven Rostedt265c8312009-02-13 12:43:56 -05001067 do_for_each_ftrace_rec(pg, rec) {
1068 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001069 * Skip over free records, records that have
1070 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001071 */
1072 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001073 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001074 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001075 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001076
Steven Rostedt265c8312009-02-13 12:43:56 -05001077 /* ignore updates to this record's mcount site */
1078 if (get_kprobe((void *)rec->ip)) {
1079 freeze_record(rec);
1080 continue;
1081 } else {
1082 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +02001083 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001084
1085 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001086 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001087 rec->flags |= FTRACE_FL_FAILED;
1088 if ((system_state == SYSTEM_BOOTING) ||
1089 !core_kernel_text(rec->ip)) {
1090 ftrace_free_rec(rec);
Steven Rostedt43772452009-02-19 13:41:27 -05001091 } else {
Steven Rostedt265c8312009-02-13 12:43:56 -05001092 ftrace_bug(failed, rec->ip);
Steven Rostedt43772452009-02-19 13:41:27 -05001093 /* Stop processing */
1094 return;
1095 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001096 }
1097 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098}
1099
Ingo Molnare309b412008-05-12 21:20:51 +02001100static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001101ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001102{
1103 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001104 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105
1106 ip = rec->ip;
1107
Shaohua Li25aac9d2009-01-09 11:29:40 +08001108 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001109 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001110 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001111 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301112 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001113 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301114 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001115}
1116
Steven Rostedt000ab692009-02-17 13:35:06 -05001117/*
1118 * archs can override this function if they must do something
1119 * before the modifying code is performed.
1120 */
1121int __weak ftrace_arch_code_modify_prepare(void)
1122{
1123 return 0;
1124}
1125
1126/*
1127 * archs can override this function if they must do something
1128 * after the modifying code is performed.
1129 */
1130int __weak ftrace_arch_code_modify_post_process(void)
1131{
1132 return 0;
1133}
1134
Ingo Molnare309b412008-05-12 21:20:51 +02001135static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001136{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001137 int *command = data;
1138
Steven Rostedta3583242008-11-11 15:01:42 -05001139 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001140 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001141 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001142 ftrace_replace_code(0);
1143
1144 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1145 ftrace_update_ftrace_func(ftrace_trace_function);
1146
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001147 if (*command & FTRACE_START_FUNC_RET)
1148 ftrace_enable_ftrace_graph_caller();
1149 else if (*command & FTRACE_STOP_FUNC_RET)
1150 ftrace_disable_ftrace_graph_caller();
1151
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001152 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001153}
1154
Ingo Molnare309b412008-05-12 21:20:51 +02001155static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001156{
Steven Rostedt000ab692009-02-17 13:35:06 -05001157 int ret;
1158
1159 ret = ftrace_arch_code_modify_prepare();
1160 FTRACE_WARN_ON(ret);
1161 if (ret)
1162 return;
1163
Rusty Russell784e2d72008-07-28 12:16:31 -05001164 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001165
1166 ret = ftrace_arch_code_modify_post_process();
1167 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001168}
1169
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001170static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001171static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001172
1173static void ftrace_startup_enable(int command)
1174{
1175 if (saved_ftrace_func != ftrace_trace_function) {
1176 saved_ftrace_func = ftrace_trace_function;
1177 command |= FTRACE_UPDATE_TRACE_FUNC;
1178 }
1179
1180 if (!command || !ftrace_enabled)
1181 return;
1182
1183 ftrace_run_update_code(command);
1184}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001185
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001186static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001187{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001188 if (unlikely(ftrace_disabled))
1189 return;
1190
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001191 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001192 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001193
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001194 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001195}
1196
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001197static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001198{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001199 if (unlikely(ftrace_disabled))
1200 return;
1201
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001202 ftrace_start_up--;
1203 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001204 command |= FTRACE_DISABLE_CALLS;
1205
1206 if (saved_ftrace_func != ftrace_trace_function) {
1207 saved_ftrace_func = ftrace_trace_function;
1208 command |= FTRACE_UPDATE_TRACE_FUNC;
1209 }
1210
1211 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001212 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001213
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001214 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001215}
1216
Ingo Molnare309b412008-05-12 21:20:51 +02001217static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001218{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001219 int command = FTRACE_ENABLE_MCOUNT;
1220
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001221 if (unlikely(ftrace_disabled))
1222 return;
1223
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001224 /* Force update next time */
1225 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001226 /* ftrace_start_up is true if we want ftrace running */
1227 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001228 command |= FTRACE_ENABLE_CALLS;
1229
1230 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001231}
1232
Ingo Molnare309b412008-05-12 21:20:51 +02001233static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001234{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001235 int command = FTRACE_DISABLE_MCOUNT;
1236
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001237 if (unlikely(ftrace_disabled))
1238 return;
1239
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001240 /* ftrace_start_up is true if ftrace is running */
1241 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001242 command |= FTRACE_DISABLE_CALLS;
1243
1244 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001245}
1246
Steven Rostedt3d083392008-05-12 21:20:42 +02001247static cycle_t ftrace_update_time;
1248static unsigned long ftrace_update_cnt;
1249unsigned long ftrace_update_tot_cnt;
1250
Steven Rostedt31e88902008-11-14 16:21:19 -08001251static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001252{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001253 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301254 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001255
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001256 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001257 ftrace_update_cnt = 0;
1258
Lai Jiangshane94142a2009-03-13 17:51:27 +08001259 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301260
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001261 /* If something went wrong, bail without enabling anything */
1262 if (unlikely(ftrace_disabled))
1263 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001264
Lai Jiangshane94142a2009-03-13 17:51:27 +08001265 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001266 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001267 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301268
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001269 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -08001270 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001271 p->flags |= FTRACE_FL_CONVERTED;
1272 ftrace_update_cnt++;
1273 } else
1274 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +02001275 }
1276
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001277 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001278 ftrace_update_time = stop - start;
1279 ftrace_update_tot_cnt += ftrace_update_cnt;
1280
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001281 return 0;
1282}
1283
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001284static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001285{
1286 struct ftrace_page *pg;
1287 int cnt;
1288 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001289
1290 /* allocate a few pages */
1291 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1292 if (!ftrace_pages_start)
1293 return -1;
1294
1295 /*
1296 * Allocate a few more pages.
1297 *
1298 * TODO: have some parser search vmlinux before
1299 * final linking to find all calls to ftrace.
1300 * Then we can:
1301 * a) know how many pages to allocate.
1302 * and/or
1303 * b) set up the table then.
1304 *
1305 * The dynamic code is still necessary for
1306 * modules.
1307 */
1308
1309 pg = ftrace_pages = ftrace_pages_start;
1310
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001311 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001312 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001313 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001314
1315 for (i = 0; i < cnt; i++) {
1316 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1317
1318 /* If we fail, we'll try later anyway */
1319 if (!pg->next)
1320 break;
1321
1322 pg = pg->next;
1323 }
1324
1325 return 0;
1326}
1327
Steven Rostedt5072c592008-05-12 21:20:43 +02001328enum {
1329 FTRACE_ITER_FILTER = (1 << 0),
1330 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -04001331 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301332 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001333 FTRACE_ITER_PRINTALL = (1 << 4),
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001334 FTRACE_ITER_HASH = (1 << 5),
Steven Rostedt5072c592008-05-12 21:20:43 +02001335};
1336
1337#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1338
1339struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001340 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001341 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001342 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001343 unsigned flags;
1344 unsigned char buffer[FTRACE_BUFF_MAX+1];
1345 unsigned buffer_idx;
1346 unsigned filtered;
1347};
1348
Ingo Molnare309b412008-05-12 21:20:51 +02001349static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001350t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1351{
1352 struct ftrace_iterator *iter = m->private;
1353 struct hlist_node *hnd = v;
1354 struct hlist_head *hhd;
1355
1356 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1357
1358 (*pos)++;
1359
1360 retry:
1361 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1362 return NULL;
1363
1364 hhd = &ftrace_func_hash[iter->hidx];
1365
1366 if (hlist_empty(hhd)) {
1367 iter->hidx++;
1368 hnd = NULL;
1369 goto retry;
1370 }
1371
1372 if (!hnd)
1373 hnd = hhd->first;
1374 else {
1375 hnd = hnd->next;
1376 if (!hnd) {
1377 iter->hidx++;
1378 goto retry;
1379 }
1380 }
1381
1382 return hnd;
1383}
1384
1385static void *t_hash_start(struct seq_file *m, loff_t *pos)
1386{
1387 struct ftrace_iterator *iter = m->private;
1388 void *p = NULL;
1389
1390 iter->flags |= FTRACE_ITER_HASH;
1391
1392 return t_hash_next(m, p, pos);
1393}
1394
1395static int t_hash_show(struct seq_file *m, void *v)
1396{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001397 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001398 struct hlist_node *hnd = v;
1399 char str[KSYM_SYMBOL_LEN];
1400
Steven Rostedtb6887d72009-02-17 12:32:04 -05001401 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001402
Steven Rostedt809dcf22009-02-16 23:06:01 -05001403 if (rec->ops->print)
1404 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1405
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001406 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1407 seq_printf(m, "%s:", str);
1408
1409 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1410 seq_printf(m, "%s", str);
1411
1412 if (rec->data)
1413 seq_printf(m, ":%p", rec->data);
1414 seq_putc(m, '\n');
1415
1416 return 0;
1417}
1418
1419static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001420t_next(struct seq_file *m, void *v, loff_t *pos)
1421{
1422 struct ftrace_iterator *iter = m->private;
1423 struct dyn_ftrace *rec = NULL;
1424
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001425 if (iter->flags & FTRACE_ITER_HASH)
1426 return t_hash_next(m, v, pos);
1427
Steven Rostedt5072c592008-05-12 21:20:43 +02001428 (*pos)++;
1429
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001430 if (iter->flags & FTRACE_ITER_PRINTALL)
1431 return NULL;
1432
Steven Rostedt5072c592008-05-12 21:20:43 +02001433 retry:
1434 if (iter->idx >= iter->pg->index) {
1435 if (iter->pg->next) {
1436 iter->pg = iter->pg->next;
1437 iter->idx = 0;
1438 goto retry;
Liming Wang50cdaf02008-11-28 12:13:21 +08001439 } else {
1440 iter->idx = -1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001441 }
1442 } else {
1443 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001444 if ((rec->flags & FTRACE_FL_FREE) ||
1445
1446 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301447 (rec->flags & FTRACE_FL_FAILED)) ||
1448
1449 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001450 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301451
Steven Rostedt0183fb12008-11-07 22:36:02 -05001452 ((iter->flags & FTRACE_ITER_FILTER) &&
1453 !(rec->flags & FTRACE_FL_FILTER)) ||
1454
Steven Rostedt41c52c02008-05-22 11:46:33 -04001455 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1456 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001457 rec = NULL;
1458 goto retry;
1459 }
1460 }
1461
Steven Rostedt5072c592008-05-12 21:20:43 +02001462 return rec;
1463}
1464
1465static void *t_start(struct seq_file *m, loff_t *pos)
1466{
1467 struct ftrace_iterator *iter = m->private;
1468 void *p = NULL;
Steven Rostedt5072c592008-05-12 21:20:43 +02001469
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001470 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001471 /*
1472 * For set_ftrace_filter reading, if we have the filter
1473 * off, we can short cut and just print out that all
1474 * functions are enabled.
1475 */
1476 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1477 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001478 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001479 iter->flags |= FTRACE_ITER_PRINTALL;
1480 (*pos)++;
1481 return iter;
1482 }
1483
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001484 if (iter->flags & FTRACE_ITER_HASH)
1485 return t_hash_start(m, pos);
1486
Liming Wang50cdaf02008-11-28 12:13:21 +08001487 if (*pos > 0) {
1488 if (iter->idx < 0)
1489 return p;
1490 (*pos)--;
1491 iter->idx--;
1492 }
walimis5821e1b2008-11-15 15:19:06 +08001493
Liming Wang50cdaf02008-11-28 12:13:21 +08001494 p = t_next(m, p, pos);
Steven Rostedt5072c592008-05-12 21:20:43 +02001495
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001496 if (!p)
1497 return t_hash_start(m, pos);
1498
Steven Rostedt5072c592008-05-12 21:20:43 +02001499 return p;
1500}
1501
1502static void t_stop(struct seq_file *m, void *p)
1503{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001504 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001505}
1506
1507static int t_show(struct seq_file *m, void *v)
1508{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001509 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001510 struct dyn_ftrace *rec = v;
1511 char str[KSYM_SYMBOL_LEN];
1512
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001513 if (iter->flags & FTRACE_ITER_HASH)
1514 return t_hash_show(m, v);
1515
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001516 if (iter->flags & FTRACE_ITER_PRINTALL) {
1517 seq_printf(m, "#### all functions enabled ####\n");
1518 return 0;
1519 }
1520
Steven Rostedt5072c592008-05-12 21:20:43 +02001521 if (!rec)
1522 return 0;
1523
1524 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1525
Liming Wang50cdaf02008-11-28 12:13:21 +08001526 seq_printf(m, "%s\n", str);
Steven Rostedt5072c592008-05-12 21:20:43 +02001527
1528 return 0;
1529}
1530
1531static struct seq_operations show_ftrace_seq_ops = {
1532 .start = t_start,
1533 .next = t_next,
1534 .stop = t_stop,
1535 .show = t_show,
1536};
1537
Ingo Molnare309b412008-05-12 21:20:51 +02001538static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001539ftrace_avail_open(struct inode *inode, struct file *file)
1540{
1541 struct ftrace_iterator *iter;
1542 int ret;
1543
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001544 if (unlikely(ftrace_disabled))
1545 return -ENODEV;
1546
Steven Rostedt5072c592008-05-12 21:20:43 +02001547 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1548 if (!iter)
1549 return -ENOMEM;
1550
1551 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001552
1553 ret = seq_open(file, &show_ftrace_seq_ops);
1554 if (!ret) {
1555 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001556
Steven Rostedt5072c592008-05-12 21:20:43 +02001557 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001558 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001559 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001560 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001561
1562 return ret;
1563}
1564
1565int ftrace_avail_release(struct inode *inode, struct file *file)
1566{
1567 struct seq_file *m = (struct seq_file *)file->private_data;
1568 struct ftrace_iterator *iter = m->private;
1569
1570 seq_release(inode, file);
1571 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001572
Steven Rostedt5072c592008-05-12 21:20:43 +02001573 return 0;
1574}
1575
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301576static int
1577ftrace_failures_open(struct inode *inode, struct file *file)
1578{
1579 int ret;
1580 struct seq_file *m;
1581 struct ftrace_iterator *iter;
1582
1583 ret = ftrace_avail_open(inode, file);
1584 if (!ret) {
1585 m = (struct seq_file *)file->private_data;
1586 iter = (struct ftrace_iterator *)m->private;
1587 iter->flags = FTRACE_ITER_FAILURES;
1588 }
1589
1590 return ret;
1591}
1592
1593
Steven Rostedt41c52c02008-05-22 11:46:33 -04001594static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001595{
1596 struct ftrace_page *pg;
1597 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001598 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001599
Steven Rostedt52baf112009-02-14 01:15:39 -05001600 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001601 if (enable)
1602 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001603 do_for_each_ftrace_rec(pg, rec) {
1604 if (rec->flags & FTRACE_FL_FAILED)
1605 continue;
1606 rec->flags &= ~type;
1607 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001608 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001609}
1610
Ingo Molnare309b412008-05-12 21:20:51 +02001611static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001612ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001613{
1614 struct ftrace_iterator *iter;
1615 int ret = 0;
1616
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001617 if (unlikely(ftrace_disabled))
1618 return -ENODEV;
1619
Steven Rostedt5072c592008-05-12 21:20:43 +02001620 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1621 if (!iter)
1622 return -ENOMEM;
1623
Steven Rostedt41c52c02008-05-22 11:46:33 -04001624 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001625 if ((file->f_mode & FMODE_WRITE) &&
1626 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001627 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001628
1629 if (file->f_mode & FMODE_READ) {
1630 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001631 iter->flags = enable ? FTRACE_ITER_FILTER :
1632 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001633
1634 ret = seq_open(file, &show_ftrace_seq_ops);
1635 if (!ret) {
1636 struct seq_file *m = file->private_data;
1637 m->private = iter;
1638 } else
1639 kfree(iter);
1640 } else
1641 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001642 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001643
1644 return ret;
1645}
1646
Steven Rostedt41c52c02008-05-22 11:46:33 -04001647static int
1648ftrace_filter_open(struct inode *inode, struct file *file)
1649{
1650 return ftrace_regex_open(inode, file, 1);
1651}
1652
1653static int
1654ftrace_notrace_open(struct inode *inode, struct file *file)
1655{
1656 return ftrace_regex_open(inode, file, 0);
1657}
1658
Ingo Molnare309b412008-05-12 21:20:51 +02001659static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001660ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001661{
1662 loff_t ret;
1663
1664 if (file->f_mode & FMODE_READ)
1665 ret = seq_lseek(file, offset, origin);
1666 else
1667 file->f_pos = ret = 1;
1668
1669 return ret;
1670}
1671
1672enum {
1673 MATCH_FULL,
1674 MATCH_FRONT_ONLY,
1675 MATCH_MIDDLE_ONLY,
1676 MATCH_END_ONLY,
1677};
1678
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001679/*
1680 * (static function - no need for kernel doc)
1681 *
1682 * Pass in a buffer containing a glob and this function will
1683 * set search to point to the search part of the buffer and
1684 * return the type of search it is (see enum above).
1685 * This does modify buff.
1686 *
1687 * Returns enum type.
1688 * search returns the pointer to use for comparison.
1689 * not returns 1 if buff started with a '!'
1690 * 0 otherwise.
1691 */
1692static int
Steven Rostedt64e7c442009-02-13 17:08:48 -05001693ftrace_setup_glob(char *buff, int len, char **search, int *not)
Steven Rostedt5072c592008-05-12 21:20:43 +02001694{
Steven Rostedt5072c592008-05-12 21:20:43 +02001695 int type = MATCH_FULL;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001696 int i;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001697
1698 if (buff[0] == '!') {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001699 *not = 1;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001700 buff++;
1701 len--;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001702 } else
1703 *not = 0;
1704
1705 *search = buff;
Steven Rostedt5072c592008-05-12 21:20:43 +02001706
1707 for (i = 0; i < len; i++) {
1708 if (buff[i] == '*') {
1709 if (!i) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001710 *search = buff + 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001711 type = MATCH_END_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001712 } else {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001713 if (type == MATCH_END_ONLY)
Steven Rostedt5072c592008-05-12 21:20:43 +02001714 type = MATCH_MIDDLE_ONLY;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001715 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001716 type = MATCH_FRONT_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001717 buff[i] = 0;
1718 break;
1719 }
1720 }
1721 }
1722
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001723 return type;
1724}
1725
Steven Rostedt64e7c442009-02-13 17:08:48 -05001726static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001727{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001728 int matched = 0;
1729 char *ptr;
1730
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001731 switch (type) {
1732 case MATCH_FULL:
1733 if (strcmp(str, regex) == 0)
1734 matched = 1;
1735 break;
1736 case MATCH_FRONT_ONLY:
1737 if (strncmp(str, regex, len) == 0)
1738 matched = 1;
1739 break;
1740 case MATCH_MIDDLE_ONLY:
1741 if (strstr(str, regex))
1742 matched = 1;
1743 break;
1744 case MATCH_END_ONLY:
1745 ptr = strstr(str, regex);
1746 if (ptr && (ptr[len] == 0))
1747 matched = 1;
1748 break;
1749 }
1750
1751 return matched;
1752}
1753
Steven Rostedt64e7c442009-02-13 17:08:48 -05001754static int
1755ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1756{
1757 char str[KSYM_SYMBOL_LEN];
1758
1759 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1760 return ftrace_match(str, regex, len, type);
1761}
1762
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001763static void ftrace_match_records(char *buff, int len, int enable)
1764{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001765 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001766 struct ftrace_page *pg;
1767 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001768 unsigned long flag;
1769 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001770 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001771 int not;
1772
Steven Rostedt6a24a242009-02-17 11:20:26 -05001773 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001774 type = ftrace_setup_glob(buff, len, &search, &not);
1775
1776 search_len = strlen(search);
1777
Steven Rostedt52baf112009-02-14 01:15:39 -05001778 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001779 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001780
Steven Rostedt265c8312009-02-13 12:43:56 -05001781 if (rec->flags & FTRACE_FL_FAILED)
1782 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001783
1784 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001785 if (not)
1786 rec->flags &= ~flag;
1787 else
1788 rec->flags |= flag;
1789 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001790 /*
1791 * Only enable filtering if we have a function that
1792 * is filtered on.
1793 */
1794 if (enable && (rec->flags & FTRACE_FL_FILTER))
1795 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001796 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001797 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001798}
1799
Steven Rostedt64e7c442009-02-13 17:08:48 -05001800static int
1801ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1802 char *regex, int len, int type)
1803{
1804 char str[KSYM_SYMBOL_LEN];
1805 char *modname;
1806
1807 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1808
1809 if (!modname || strcmp(modname, mod))
1810 return 0;
1811
1812 /* blank search means to match all funcs in the mod */
1813 if (len)
1814 return ftrace_match(str, regex, len, type);
1815 else
1816 return 1;
1817}
1818
1819static void ftrace_match_module_records(char *buff, char *mod, int enable)
1820{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001821 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001822 struct ftrace_page *pg;
1823 struct dyn_ftrace *rec;
1824 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001825 char *search = buff;
1826 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001827 int not = 0;
1828
Steven Rostedt6a24a242009-02-17 11:20:26 -05001829 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1830
Steven Rostedt64e7c442009-02-13 17:08:48 -05001831 /* blank or '*' mean the same */
1832 if (strcmp(buff, "*") == 0)
1833 buff[0] = 0;
1834
1835 /* handle the case of 'dont filter this module' */
1836 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1837 buff[0] = 0;
1838 not = 1;
1839 }
1840
1841 if (strlen(buff)) {
1842 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1843 search_len = strlen(search);
1844 }
1845
Steven Rostedt52baf112009-02-14 01:15:39 -05001846 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001847 do_for_each_ftrace_rec(pg, rec) {
1848
1849 if (rec->flags & FTRACE_FL_FAILED)
1850 continue;
1851
1852 if (ftrace_match_module_record(rec, mod,
1853 search, search_len, type)) {
1854 if (not)
1855 rec->flags &= ~flag;
1856 else
1857 rec->flags |= flag;
1858 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001859 if (enable && (rec->flags & FTRACE_FL_FILTER))
1860 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001861
1862 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001863 mutex_unlock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001864}
1865
Steven Rostedtf6180772009-02-14 00:40:25 -05001866/*
1867 * We register the module command as a template to show others how
1868 * to register the a command as well.
1869 */
1870
1871static int
1872ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1873{
1874 char *mod;
1875
1876 /*
1877 * cmd == 'mod' because we only registered this func
1878 * for the 'mod' ftrace_func_command.
1879 * But if you register one func with multiple commands,
1880 * you can tell which command was used by the cmd
1881 * parameter.
1882 */
1883
1884 /* we must have a module name */
1885 if (!param)
1886 return -EINVAL;
1887
1888 mod = strsep(&param, ":");
1889 if (!strlen(mod))
1890 return -EINVAL;
1891
1892 ftrace_match_module_records(func, mod, enable);
1893 return 0;
1894}
1895
1896static struct ftrace_func_command ftrace_mod_cmd = {
1897 .name = "mod",
1898 .func = ftrace_mod_callback,
1899};
1900
1901static int __init ftrace_mod_cmd_init(void)
1902{
1903 return register_ftrace_command(&ftrace_mod_cmd);
1904}
1905device_initcall(ftrace_mod_cmd_init);
1906
Steven Rostedt59df055f2009-02-14 15:29:06 -05001907static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001908function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001909{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001910 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001911 struct hlist_head *hhd;
1912 struct hlist_node *n;
1913 unsigned long key;
1914 int resched;
1915
1916 key = hash_long(ip, FTRACE_HASH_BITS);
1917
1918 hhd = &ftrace_func_hash[key];
1919
1920 if (hlist_empty(hhd))
1921 return;
1922
1923 /*
1924 * Disable preemption for these calls to prevent a RCU grace
1925 * period. This syncs the hash iteration and freeing of items
1926 * on the hash. rcu_read_lock is too dangerous here.
1927 */
1928 resched = ftrace_preempt_disable();
1929 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1930 if (entry->ip == ip)
1931 entry->ops->func(ip, parent_ip, &entry->data);
1932 }
1933 ftrace_preempt_enable(resched);
1934}
1935
Steven Rostedtb6887d72009-02-17 12:32:04 -05001936static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001937{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001938 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001939};
1940
Steven Rostedtb6887d72009-02-17 12:32:04 -05001941static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001942
Steven Rostedtb6887d72009-02-17 12:32:04 -05001943static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001944{
1945 int i;
1946
Steven Rostedtb6887d72009-02-17 12:32:04 -05001947 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001948 return;
1949
1950 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1951 struct hlist_head *hhd = &ftrace_func_hash[i];
1952 if (hhd->first)
1953 break;
1954 }
1955 /* Nothing registered? */
1956 if (i == FTRACE_FUNC_HASHSIZE)
1957 return;
1958
Steven Rostedtb6887d72009-02-17 12:32:04 -05001959 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001960 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001961 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001962}
1963
Steven Rostedtb6887d72009-02-17 12:32:04 -05001964static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001965{
1966 int i;
1967
Steven Rostedtb6887d72009-02-17 12:32:04 -05001968 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001969 return;
1970
1971 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1972 struct hlist_head *hhd = &ftrace_func_hash[i];
1973 if (hhd->first)
1974 return;
1975 }
1976
1977 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001978 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001979 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001980 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001981}
1982
1983
1984static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1985{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001986 struct ftrace_func_probe *entry =
1987 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001988
1989 if (entry->ops->free)
1990 entry->ops->free(&entry->data);
1991 kfree(entry);
1992}
1993
1994
1995int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001996register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001997 void *data)
1998{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001999 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002000 struct ftrace_page *pg;
2001 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002002 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002003 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002004 int count = 0;
2005 char *search;
2006
2007 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2008 len = strlen(search);
2009
Steven Rostedtb6887d72009-02-17 12:32:04 -05002010 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002011 if (WARN_ON(not))
2012 return -EINVAL;
2013
2014 mutex_lock(&ftrace_lock);
2015 do_for_each_ftrace_rec(pg, rec) {
2016
2017 if (rec->flags & FTRACE_FL_FAILED)
2018 continue;
2019
2020 if (!ftrace_match_record(rec, search, len, type))
2021 continue;
2022
2023 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2024 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002025 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002026 if (!count)
2027 count = -ENOMEM;
2028 goto out_unlock;
2029 }
2030
2031 count++;
2032
2033 entry->data = data;
2034
2035 /*
2036 * The caller might want to do something special
2037 * for each function we find. We call the callback
2038 * to give the caller an opportunity to do so.
2039 */
2040 if (ops->callback) {
2041 if (ops->callback(rec->ip, &entry->data) < 0) {
2042 /* caller does not like this func */
2043 kfree(entry);
2044 continue;
2045 }
2046 }
2047
2048 entry->ops = ops;
2049 entry->ip = rec->ip;
2050
2051 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2052 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2053
2054 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002055 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002056
2057 out_unlock:
2058 mutex_unlock(&ftrace_lock);
2059
2060 return count;
2061}
2062
2063enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002064 PROBE_TEST_FUNC = 1,
2065 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002066};
2067
2068static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002069__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002070 void *data, int flags)
2071{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002072 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002073 struct hlist_node *n, *tmp;
2074 char str[KSYM_SYMBOL_LEN];
2075 int type = MATCH_FULL;
2076 int i, len = 0;
2077 char *search;
2078
2079 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2080 glob = NULL;
2081 else {
2082 int not;
2083
2084 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2085 len = strlen(search);
2086
Steven Rostedtb6887d72009-02-17 12:32:04 -05002087 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002088 if (WARN_ON(not))
2089 return;
2090 }
2091
2092 mutex_lock(&ftrace_lock);
2093 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2094 struct hlist_head *hhd = &ftrace_func_hash[i];
2095
2096 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2097
2098 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002099 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002100 continue;
2101
Steven Rostedtb6887d72009-02-17 12:32:04 -05002102 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002103 continue;
2104
2105 /* do this last, since it is the most expensive */
2106 if (glob) {
2107 kallsyms_lookup(entry->ip, NULL, NULL,
2108 NULL, str);
2109 if (!ftrace_match(str, glob, len, type))
2110 continue;
2111 }
2112
2113 hlist_del(&entry->node);
2114 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2115 }
2116 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002117 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002118 mutex_unlock(&ftrace_lock);
2119}
2120
2121void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002122unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002123 void *data)
2124{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002125 __unregister_ftrace_function_probe(glob, ops, data,
2126 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002127}
2128
2129void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002130unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002131{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002132 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002133}
2134
Steven Rostedtb6887d72009-02-17 12:32:04 -05002135void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002136{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002137 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002138}
2139
Steven Rostedtf6180772009-02-14 00:40:25 -05002140static LIST_HEAD(ftrace_commands);
2141static DEFINE_MUTEX(ftrace_cmd_mutex);
2142
2143int register_ftrace_command(struct ftrace_func_command *cmd)
2144{
2145 struct ftrace_func_command *p;
2146 int ret = 0;
2147
2148 mutex_lock(&ftrace_cmd_mutex);
2149 list_for_each_entry(p, &ftrace_commands, list) {
2150 if (strcmp(cmd->name, p->name) == 0) {
2151 ret = -EBUSY;
2152 goto out_unlock;
2153 }
2154 }
2155 list_add(&cmd->list, &ftrace_commands);
2156 out_unlock:
2157 mutex_unlock(&ftrace_cmd_mutex);
2158
2159 return ret;
2160}
2161
2162int unregister_ftrace_command(struct ftrace_func_command *cmd)
2163{
2164 struct ftrace_func_command *p, *n;
2165 int ret = -ENODEV;
2166
2167 mutex_lock(&ftrace_cmd_mutex);
2168 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2169 if (strcmp(cmd->name, p->name) == 0) {
2170 ret = 0;
2171 list_del_init(&p->list);
2172 goto out_unlock;
2173 }
2174 }
2175 out_unlock:
2176 mutex_unlock(&ftrace_cmd_mutex);
2177
2178 return ret;
2179}
2180
Steven Rostedt64e7c442009-02-13 17:08:48 -05002181static int ftrace_process_regex(char *buff, int len, int enable)
2182{
Steven Rostedtf6180772009-02-14 00:40:25 -05002183 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002184 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002185 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002186
2187 func = strsep(&next, ":");
2188
2189 if (!next) {
2190 ftrace_match_records(func, len, enable);
2191 return 0;
2192 }
2193
Steven Rostedtf6180772009-02-14 00:40:25 -05002194 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002195
2196 command = strsep(&next, ":");
2197
Steven Rostedtf6180772009-02-14 00:40:25 -05002198 mutex_lock(&ftrace_cmd_mutex);
2199 list_for_each_entry(p, &ftrace_commands, list) {
2200 if (strcmp(p->name, command) == 0) {
2201 ret = p->func(func, command, next, enable);
2202 goto out_unlock;
2203 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002204 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002205 out_unlock:
2206 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002207
Steven Rostedtf6180772009-02-14 00:40:25 -05002208 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002209}
2210
Ingo Molnare309b412008-05-12 21:20:51 +02002211static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002212ftrace_regex_write(struct file *file, const char __user *ubuf,
2213 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002214{
2215 struct ftrace_iterator *iter;
2216 char ch;
2217 size_t read = 0;
2218 ssize_t ret;
2219
2220 if (!cnt || cnt < 0)
2221 return 0;
2222
Steven Rostedt41c52c02008-05-22 11:46:33 -04002223 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002224
2225 if (file->f_mode & FMODE_READ) {
2226 struct seq_file *m = file->private_data;
2227 iter = m->private;
2228 } else
2229 iter = file->private_data;
2230
2231 if (!*ppos) {
2232 iter->flags &= ~FTRACE_ITER_CONT;
2233 iter->buffer_idx = 0;
2234 }
2235
2236 ret = get_user(ch, ubuf++);
2237 if (ret)
2238 goto out;
2239 read++;
2240 cnt--;
2241
2242 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2243 /* skip white space */
2244 while (cnt && isspace(ch)) {
2245 ret = get_user(ch, ubuf++);
2246 if (ret)
2247 goto out;
2248 read++;
2249 cnt--;
2250 }
2251
Steven Rostedt5072c592008-05-12 21:20:43 +02002252 if (isspace(ch)) {
2253 file->f_pos += read;
2254 ret = read;
2255 goto out;
2256 }
2257
2258 iter->buffer_idx = 0;
2259 }
2260
2261 while (cnt && !isspace(ch)) {
2262 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2263 iter->buffer[iter->buffer_idx++] = ch;
2264 else {
2265 ret = -EINVAL;
2266 goto out;
2267 }
2268 ret = get_user(ch, ubuf++);
2269 if (ret)
2270 goto out;
2271 read++;
2272 cnt--;
2273 }
2274
2275 if (isspace(ch)) {
2276 iter->filtered++;
2277 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002278 ret = ftrace_process_regex(iter->buffer,
2279 iter->buffer_idx, enable);
2280 if (ret)
2281 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02002282 iter->buffer_idx = 0;
2283 } else
2284 iter->flags |= FTRACE_ITER_CONT;
2285
2286
2287 file->f_pos += read;
2288
2289 ret = read;
2290 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04002291 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002292
2293 return ret;
2294}
2295
Steven Rostedt41c52c02008-05-22 11:46:33 -04002296static ssize_t
2297ftrace_filter_write(struct file *file, const char __user *ubuf,
2298 size_t cnt, loff_t *ppos)
2299{
2300 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2301}
2302
2303static ssize_t
2304ftrace_notrace_write(struct file *file, const char __user *ubuf,
2305 size_t cnt, loff_t *ppos)
2306{
2307 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2308}
2309
2310static void
2311ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2312{
2313 if (unlikely(ftrace_disabled))
2314 return;
2315
2316 mutex_lock(&ftrace_regex_lock);
2317 if (reset)
2318 ftrace_filter_reset(enable);
2319 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002320 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002321 mutex_unlock(&ftrace_regex_lock);
2322}
2323
Steven Rostedt77a2b372008-05-12 21:20:45 +02002324/**
2325 * ftrace_set_filter - set a function to filter on in ftrace
2326 * @buf - the string that holds the function filter text.
2327 * @len - the length of the string.
2328 * @reset - non zero to reset all filters before applying this filter.
2329 *
2330 * Filters denote which functions should be enabled when tracing is enabled.
2331 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2332 */
Ingo Molnare309b412008-05-12 21:20:51 +02002333void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002334{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002335 ftrace_set_regex(buf, len, reset, 1);
2336}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002337
Steven Rostedt41c52c02008-05-22 11:46:33 -04002338/**
2339 * ftrace_set_notrace - set a function to not trace in ftrace
2340 * @buf - the string that holds the function notrace text.
2341 * @len - the length of the string.
2342 * @reset - non zero to reset all filters before applying this filter.
2343 *
2344 * Notrace Filters denote which functions should not be enabled when tracing
2345 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2346 * for tracing.
2347 */
2348void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2349{
2350 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002351}
2352
Ingo Molnare309b412008-05-12 21:20:51 +02002353static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002354ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002355{
2356 struct seq_file *m = (struct seq_file *)file->private_data;
2357 struct ftrace_iterator *iter;
2358
Steven Rostedt41c52c02008-05-22 11:46:33 -04002359 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002360 if (file->f_mode & FMODE_READ) {
2361 iter = m->private;
2362
2363 seq_release(inode, file);
2364 } else
2365 iter = file->private_data;
2366
2367 if (iter->buffer_idx) {
2368 iter->filtered++;
2369 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt7f24b312009-02-13 14:37:33 -05002370 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002371 }
2372
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002373 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002374 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002375 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002376 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002377
2378 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002379 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002380 return 0;
2381}
2382
Steven Rostedt41c52c02008-05-22 11:46:33 -04002383static int
2384ftrace_filter_release(struct inode *inode, struct file *file)
2385{
2386 return ftrace_regex_release(inode, file, 1);
2387}
2388
2389static int
2390ftrace_notrace_release(struct inode *inode, struct file *file)
2391{
2392 return ftrace_regex_release(inode, file, 0);
2393}
2394
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002395static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002396 .open = ftrace_avail_open,
2397 .read = seq_read,
2398 .llseek = seq_lseek,
2399 .release = ftrace_avail_release,
2400};
2401
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002402static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302403 .open = ftrace_failures_open,
2404 .read = seq_read,
2405 .llseek = seq_lseek,
2406 .release = ftrace_avail_release,
2407};
2408
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002409static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002410 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002411 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002412 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002413 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002414 .release = ftrace_filter_release,
2415};
2416
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002417static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002418 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002419 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002420 .write = ftrace_notrace_write,
2421 .llseek = ftrace_regex_lseek,
2422 .release = ftrace_notrace_release,
2423};
2424
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002425#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2426
2427static DEFINE_MUTEX(graph_lock);
2428
2429int ftrace_graph_count;
2430unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2431
2432static void *
2433g_next(struct seq_file *m, void *v, loff_t *pos)
2434{
2435 unsigned long *array = m->private;
2436 int index = *pos;
2437
2438 (*pos)++;
2439
2440 if (index >= ftrace_graph_count)
2441 return NULL;
2442
2443 return &array[index];
2444}
2445
2446static void *g_start(struct seq_file *m, loff_t *pos)
2447{
2448 void *p = NULL;
2449
2450 mutex_lock(&graph_lock);
2451
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002452 /* Nothing, tell g_show to print all functions are enabled */
2453 if (!ftrace_graph_count && !*pos)
2454 return (void *)1;
2455
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002456 p = g_next(m, p, pos);
2457
2458 return p;
2459}
2460
2461static void g_stop(struct seq_file *m, void *p)
2462{
2463 mutex_unlock(&graph_lock);
2464}
2465
2466static int g_show(struct seq_file *m, void *v)
2467{
2468 unsigned long *ptr = v;
2469 char str[KSYM_SYMBOL_LEN];
2470
2471 if (!ptr)
2472 return 0;
2473
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002474 if (ptr == (unsigned long *)1) {
2475 seq_printf(m, "#### all functions enabled ####\n");
2476 return 0;
2477 }
2478
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002479 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2480
2481 seq_printf(m, "%s\n", str);
2482
2483 return 0;
2484}
2485
2486static struct seq_operations ftrace_graph_seq_ops = {
2487 .start = g_start,
2488 .next = g_next,
2489 .stop = g_stop,
2490 .show = g_show,
2491};
2492
2493static int
2494ftrace_graph_open(struct inode *inode, struct file *file)
2495{
2496 int ret = 0;
2497
2498 if (unlikely(ftrace_disabled))
2499 return -ENODEV;
2500
2501 mutex_lock(&graph_lock);
2502 if ((file->f_mode & FMODE_WRITE) &&
2503 !(file->f_flags & O_APPEND)) {
2504 ftrace_graph_count = 0;
2505 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2506 }
2507
2508 if (file->f_mode & FMODE_READ) {
2509 ret = seq_open(file, &ftrace_graph_seq_ops);
2510 if (!ret) {
2511 struct seq_file *m = file->private_data;
2512 m->private = ftrace_graph_funcs;
2513 }
2514 } else
2515 file->private_data = ftrace_graph_funcs;
2516 mutex_unlock(&graph_lock);
2517
2518 return ret;
2519}
2520
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002521static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002522ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002523{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002524 struct dyn_ftrace *rec;
2525 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002526 int search_len;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002527 int found = 0;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002528 int type, not;
2529 char *search;
2530 bool exists;
2531 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002532
2533 if (ftrace_disabled)
2534 return -ENODEV;
2535
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002536 /* decode regex */
2537 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2538 if (not)
2539 return -EINVAL;
2540
2541 search_len = strlen(search);
2542
Steven Rostedt52baf112009-02-14 01:15:39 -05002543 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002544 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002545
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002546 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2547 break;
2548
Steven Rostedt265c8312009-02-13 12:43:56 -05002549 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2550 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002551
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002552 if (ftrace_match_record(rec, search, search_len, type)) {
2553 /* ensure it is not already in the array */
2554 exists = false;
2555 for (i = 0; i < *idx; i++)
2556 if (array[i] == rec->ip) {
2557 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002558 break;
2559 }
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002560 if (!exists) {
2561 array[(*idx)++] = rec->ip;
2562 found = 1;
2563 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002564 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002565 } while_for_each_ftrace_rec();
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002566
Steven Rostedt52baf112009-02-14 01:15:39 -05002567 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002568
2569 return found ? 0 : -EINVAL;
2570}
2571
2572static ssize_t
2573ftrace_graph_write(struct file *file, const char __user *ubuf,
2574 size_t cnt, loff_t *ppos)
2575{
2576 unsigned char buffer[FTRACE_BUFF_MAX+1];
2577 unsigned long *array;
2578 size_t read = 0;
2579 ssize_t ret;
2580 int index = 0;
2581 char ch;
2582
2583 if (!cnt || cnt < 0)
2584 return 0;
2585
2586 mutex_lock(&graph_lock);
2587
2588 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2589 ret = -EBUSY;
2590 goto out;
2591 }
2592
2593 if (file->f_mode & FMODE_READ) {
2594 struct seq_file *m = file->private_data;
2595 array = m->private;
2596 } else
2597 array = file->private_data;
2598
2599 ret = get_user(ch, ubuf++);
2600 if (ret)
2601 goto out;
2602 read++;
2603 cnt--;
2604
2605 /* skip white space */
2606 while (cnt && isspace(ch)) {
2607 ret = get_user(ch, ubuf++);
2608 if (ret)
2609 goto out;
2610 read++;
2611 cnt--;
2612 }
2613
2614 if (isspace(ch)) {
2615 *ppos += read;
2616 ret = read;
2617 goto out;
2618 }
2619
2620 while (cnt && !isspace(ch)) {
2621 if (index < FTRACE_BUFF_MAX)
2622 buffer[index++] = ch;
2623 else {
2624 ret = -EINVAL;
2625 goto out;
2626 }
2627 ret = get_user(ch, ubuf++);
2628 if (ret)
2629 goto out;
2630 read++;
2631 cnt--;
2632 }
2633 buffer[index] = 0;
2634
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002635 /* we allow only one expression at a time */
2636 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002637 if (ret)
2638 goto out;
2639
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002640 file->f_pos += read;
2641
2642 ret = read;
2643 out:
2644 mutex_unlock(&graph_lock);
2645
2646 return ret;
2647}
2648
2649static const struct file_operations ftrace_graph_fops = {
2650 .open = ftrace_graph_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002651 .read = seq_read,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002652 .write = ftrace_graph_write,
2653};
2654#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2655
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002656static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002657{
Steven Rostedt5072c592008-05-12 21:20:43 +02002658 struct dentry *entry;
2659
Steven Rostedt5072c592008-05-12 21:20:43 +02002660 entry = debugfs_create_file("available_filter_functions", 0444,
2661 d_tracer, NULL, &ftrace_avail_fops);
2662 if (!entry)
2663 pr_warning("Could not create debugfs "
2664 "'available_filter_functions' entry\n");
2665
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302666 entry = debugfs_create_file("failures", 0444,
2667 d_tracer, NULL, &ftrace_failures_fops);
2668 if (!entry)
2669 pr_warning("Could not create debugfs 'failures' entry\n");
2670
Steven Rostedt5072c592008-05-12 21:20:43 +02002671 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2672 NULL, &ftrace_filter_fops);
2673 if (!entry)
2674 pr_warning("Could not create debugfs "
2675 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04002676
2677 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2678 NULL, &ftrace_notrace_fops);
2679 if (!entry)
2680 pr_warning("Could not create debugfs "
2681 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002682
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002683#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2684 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2685 NULL,
2686 &ftrace_graph_fops);
2687 if (!entry)
2688 pr_warning("Could not create debugfs "
2689 "'set_graph_function' entry\n");
2690#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2691
Steven Rostedt5072c592008-05-12 21:20:43 +02002692 return 0;
2693}
2694
Steven Rostedt31e88902008-11-14 16:21:19 -08002695static int ftrace_convert_nops(struct module *mod,
2696 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002697 unsigned long *end)
2698{
2699 unsigned long *p;
2700 unsigned long addr;
2701 unsigned long flags;
2702
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002703 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002704 p = start;
2705 while (p < end) {
2706 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002707 /*
2708 * Some architecture linkers will pad between
2709 * the different mcount_loc sections of different
2710 * object files to satisfy alignments.
2711 * Skip any NULL pointers.
2712 */
2713 if (!addr)
2714 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002715 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002716 }
2717
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002718 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002719 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002720 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002721 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002722 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002723
2724 return 0;
2725}
2726
Steven Rostedt31e88902008-11-14 16:21:19 -08002727void ftrace_init_module(struct module *mod,
2728 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002729{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002730 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002731 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08002732 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002733}
2734
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002735extern unsigned long __start_mcount_loc[];
2736extern unsigned long __stop_mcount_loc[];
2737
2738void __init ftrace_init(void)
2739{
2740 unsigned long count, addr, flags;
2741 int ret;
2742
2743 /* Keep the ftrace pointer to the stub */
2744 addr = (unsigned long)ftrace_stub;
2745
2746 local_irq_save(flags);
2747 ftrace_dyn_arch_init(&addr);
2748 local_irq_restore(flags);
2749
2750 /* ftrace_dyn_arch_init places the return code in addr */
2751 if (addr)
2752 goto failed;
2753
2754 count = __stop_mcount_loc - __start_mcount_loc;
2755
2756 ret = ftrace_dyn_table_alloc(count);
2757 if (ret)
2758 goto failed;
2759
2760 last_ftrace_enabled = ftrace_enabled = 1;
2761
Steven Rostedt31e88902008-11-14 16:21:19 -08002762 ret = ftrace_convert_nops(NULL,
2763 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002764 __stop_mcount_loc);
2765
2766 return;
2767 failed:
2768 ftrace_disabled = 1;
2769}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002770
Steven Rostedt3d083392008-05-12 21:20:42 +02002771#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002772
2773static int __init ftrace_nodyn_init(void)
2774{
2775 ftrace_enabled = 1;
2776 return 0;
2777}
2778device_initcall(ftrace_nodyn_init);
2779
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002780static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2781static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002782/* Keep as macros so we do not need to define the commands */
2783# define ftrace_startup(command) do { } while (0)
2784# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002785# define ftrace_startup_sysctl() do { } while (0)
2786# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002787#endif /* CONFIG_DYNAMIC_FTRACE */
2788
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002789static ssize_t
2790ftrace_pid_read(struct file *file, char __user *ubuf,
2791 size_t cnt, loff_t *ppos)
2792{
2793 char buf[64];
2794 int r;
2795
Steven Rostedte32d8952008-12-04 00:26:41 -05002796 if (ftrace_pid_trace == ftrace_swapper_pid)
2797 r = sprintf(buf, "swapper tasks\n");
2798 else if (ftrace_pid_trace)
Lai Jiangshancc59c9e2009-03-24 11:03:01 +08002799 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002800 else
2801 r = sprintf(buf, "no pid\n");
2802
2803 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2804}
2805
Steven Rostedte32d8952008-12-04 00:26:41 -05002806static void clear_ftrace_swapper(void)
2807{
2808 struct task_struct *p;
2809 int cpu;
2810
2811 get_online_cpus();
2812 for_each_online_cpu(cpu) {
2813 p = idle_task(cpu);
2814 clear_tsk_trace_trace(p);
2815 }
2816 put_online_cpus();
2817}
2818
2819static void set_ftrace_swapper(void)
2820{
2821 struct task_struct *p;
2822 int cpu;
2823
2824 get_online_cpus();
2825 for_each_online_cpu(cpu) {
2826 p = idle_task(cpu);
2827 set_tsk_trace_trace(p);
2828 }
2829 put_online_cpus();
2830}
2831
2832static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002833{
2834 struct task_struct *p;
2835
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002836 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002837 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002838 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002839 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002840 rcu_read_unlock();
2841
Steven Rostedte32d8952008-12-04 00:26:41 -05002842 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002843}
2844
Steven Rostedte32d8952008-12-04 00:26:41 -05002845static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002846{
2847 struct task_struct *p;
2848
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002849 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002850 do_each_pid_task(pid, PIDTYPE_PID, p) {
2851 set_tsk_trace_trace(p);
2852 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002853 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002854}
2855
Steven Rostedte32d8952008-12-04 00:26:41 -05002856static void clear_ftrace_pid_task(struct pid **pid)
2857{
2858 if (*pid == ftrace_swapper_pid)
2859 clear_ftrace_swapper();
2860 else
2861 clear_ftrace_pid(*pid);
2862
2863 *pid = NULL;
2864}
2865
2866static void set_ftrace_pid_task(struct pid *pid)
2867{
2868 if (pid == ftrace_swapper_pid)
2869 set_ftrace_swapper();
2870 else
2871 set_ftrace_pid(pid);
2872}
2873
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002874static ssize_t
2875ftrace_pid_write(struct file *filp, const char __user *ubuf,
2876 size_t cnt, loff_t *ppos)
2877{
Steven Rostedt978f3a42008-12-04 00:26:40 -05002878 struct pid *pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002879 char buf[64];
2880 long val;
2881 int ret;
2882
2883 if (cnt >= sizeof(buf))
2884 return -EINVAL;
2885
2886 if (copy_from_user(&buf, ubuf, cnt))
2887 return -EFAULT;
2888
2889 buf[cnt] = 0;
2890
2891 ret = strict_strtol(buf, 10, &val);
2892 if (ret < 0)
2893 return ret;
2894
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002895 mutex_lock(&ftrace_lock);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002896 if (val < 0) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002897 /* disable pid tracing */
Steven Rostedt978f3a42008-12-04 00:26:40 -05002898 if (!ftrace_pid_trace)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002899 goto out;
Steven Rostedt978f3a42008-12-04 00:26:40 -05002900
2901 clear_ftrace_pid_task(&ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002902
2903 } else {
Steven Rostedte32d8952008-12-04 00:26:41 -05002904 /* swapper task is special */
2905 if (!val) {
2906 pid = ftrace_swapper_pid;
2907 if (pid == ftrace_pid_trace)
2908 goto out;
2909 } else {
2910 pid = find_get_pid(val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002911
Steven Rostedte32d8952008-12-04 00:26:41 -05002912 if (pid == ftrace_pid_trace) {
2913 put_pid(pid);
2914 goto out;
2915 }
Steven Rostedt978f3a42008-12-04 00:26:40 -05002916 }
2917
2918 if (ftrace_pid_trace)
2919 clear_ftrace_pid_task(&ftrace_pid_trace);
2920
2921 if (!pid)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002922 goto out;
2923
Steven Rostedt978f3a42008-12-04 00:26:40 -05002924 ftrace_pid_trace = pid;
Steven Rostedt0ef8cde2008-12-03 15:36:58 -05002925
Steven Rostedt978f3a42008-12-04 00:26:40 -05002926 set_ftrace_pid_task(ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002927 }
2928
2929 /* update the function call */
2930 ftrace_update_pid_func();
2931 ftrace_startup_enable(0);
2932
2933 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002934 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002935
2936 return cnt;
2937}
2938
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002939static const struct file_operations ftrace_pid_fops = {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002940 .read = ftrace_pid_read,
2941 .write = ftrace_pid_write,
2942};
2943
2944static __init int ftrace_init_debugfs(void)
2945{
2946 struct dentry *d_tracer;
2947 struct dentry *entry;
2948
2949 d_tracer = tracing_init_dentry();
2950 if (!d_tracer)
2951 return 0;
2952
2953 ftrace_init_dyn_debugfs(d_tracer);
2954
2955 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2956 NULL, &ftrace_pid_fops);
2957 if (!entry)
2958 pr_warning("Could not create debugfs "
2959 "'set_ftrace_pid' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04002960
2961 ftrace_profile_debugfs(d_tracer);
2962
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002963 return 0;
2964}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002965fs_initcall(ftrace_init_debugfs);
2966
Steven Rostedt3d083392008-05-12 21:20:42 +02002967/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002968 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002969 *
2970 * This function should be used by panic code. It stops ftrace
2971 * but in a not so nice way. If you need to simply kill ftrace
2972 * from a non-atomic section, use ftrace_kill.
2973 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002974void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002975{
2976 ftrace_disabled = 1;
2977 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002978 clear_ftrace_function();
2979}
2980
2981/**
Steven Rostedt3d083392008-05-12 21:20:42 +02002982 * register_ftrace_function - register a function for profiling
2983 * @ops - ops structure that holds the function for profiling.
2984 *
2985 * Register a function to be called by all functions in the
2986 * kernel.
2987 *
2988 * Note: @ops->func and all the functions it calls must be labeled
2989 * with "notrace", otherwise it will go into a
2990 * recursive loop.
2991 */
2992int register_ftrace_function(struct ftrace_ops *ops)
2993{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002994 int ret;
2995
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002996 if (unlikely(ftrace_disabled))
2997 return -1;
2998
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002999 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003000
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003001 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003002 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003003
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003004 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003005 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003006}
3007
3008/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003009 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003010 * @ops - ops structure that holds the function to unregister
3011 *
3012 * Unregister a function that was added to be called by ftrace profiling.
3013 */
3014int unregister_ftrace_function(struct ftrace_ops *ops)
3015{
3016 int ret;
3017
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003018 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003019 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003020 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003021 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003022
3023 return ret;
3024}
3025
Ingo Molnare309b412008-05-12 21:20:51 +02003026int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003027ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02003028 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003029 loff_t *ppos)
3030{
3031 int ret;
3032
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003033 if (unlikely(ftrace_disabled))
3034 return -ENODEV;
3035
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003036 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003037
Steven Rostedt5072c592008-05-12 21:20:43 +02003038 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003039
3040 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3041 goto out;
3042
3043 last_ftrace_enabled = ftrace_enabled;
3044
3045 if (ftrace_enabled) {
3046
3047 ftrace_startup_sysctl();
3048
3049 /* we are starting ftrace again */
3050 if (ftrace_list != &ftrace_list_end) {
3051 if (ftrace_list->next == &ftrace_list_end)
3052 ftrace_trace_function = ftrace_list->func;
3053 else
3054 ftrace_trace_function = ftrace_list_func;
3055 }
3056
3057 } else {
3058 /* stopping ftrace calls (just send to ftrace_stub) */
3059 ftrace_trace_function = ftrace_stub;
3060
3061 ftrace_shutdown_sysctl();
3062 }
3063
3064 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003065 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003066 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003067}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003068
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003069#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003070
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003071static atomic_t ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003072static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003073
Steven Rostedte49dc192008-12-02 23:50:05 -05003074int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3075{
3076 return 0;
3077}
3078
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003079/* The callbacks that hook a function */
3080trace_func_graph_ret_t ftrace_graph_return =
3081 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003082trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003083
3084/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3085static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3086{
3087 int i;
3088 int ret = 0;
3089 unsigned long flags;
3090 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3091 struct task_struct *g, *t;
3092
3093 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3094 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3095 * sizeof(struct ftrace_ret_stack),
3096 GFP_KERNEL);
3097 if (!ret_stack_list[i]) {
3098 start = 0;
3099 end = i;
3100 ret = -ENOMEM;
3101 goto free;
3102 }
3103 }
3104
3105 read_lock_irqsave(&tasklist_lock, flags);
3106 do_each_thread(g, t) {
3107 if (start == end) {
3108 ret = -EAGAIN;
3109 goto unlock;
3110 }
3111
3112 if (t->ret_stack == NULL) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003113 t->curr_ret_stack = -1;
Frederic Weisbecker48d68b22008-12-02 00:20:39 +01003114 /* Make sure IRQs see the -1 first: */
3115 barrier();
3116 t->ret_stack = ret_stack_list[start++];
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003117 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003118 atomic_set(&t->trace_overrun, 0);
3119 }
3120 } while_each_thread(g, t);
3121
3122unlock:
3123 read_unlock_irqrestore(&tasklist_lock, flags);
3124free:
3125 for (i = start; i < end; i++)
3126 kfree(ret_stack_list[i]);
3127 return ret;
3128}
3129
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003130static void
3131ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3132 struct task_struct *next)
3133{
3134 unsigned long long timestamp;
3135 int index;
3136
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003137 /*
3138 * Does the user want to count the time a function was asleep.
3139 * If so, do not update the time stamps.
3140 */
3141 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3142 return;
3143
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003144 timestamp = trace_clock_local();
3145
3146 prev->ftrace_timestamp = timestamp;
3147
3148 /* only process tasks that we timestamped */
3149 if (!next->ftrace_timestamp)
3150 return;
3151
3152 /*
3153 * Update all the counters in next to make up for the
3154 * time next was sleeping.
3155 */
3156 timestamp -= next->ftrace_timestamp;
3157
3158 for (index = next->curr_ret_stack; index >= 0; index--)
3159 next->ret_stack[index].calltime += timestamp;
3160}
3161
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003162/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003163static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003164{
3165 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003166 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003167
3168 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3169 sizeof(struct ftrace_ret_stack *),
3170 GFP_KERNEL);
3171
3172 if (!ret_stack_list)
3173 return -ENOMEM;
3174
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003175 /* The cpu_boot init_task->ret_stack will never be freed */
3176 for_each_online_cpu(cpu)
3177 ftrace_graph_init_task(idle_task(cpu));
3178
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003179 do {
3180 ret = alloc_retstack_tasklist(ret_stack_list);
3181 } while (ret == -EAGAIN);
3182
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003183 if (!ret) {
3184 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3185 if (ret)
3186 pr_info("ftrace_graph: Couldn't activate tracepoint"
3187 " probe to kernel_sched_switch\n");
3188 }
3189
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003190 kfree(ret_stack_list);
3191 return ret;
3192}
3193
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003194/*
3195 * Hibernation protection.
3196 * The state of the current task is too much unstable during
3197 * suspend/restore to disk. We want to protect against that.
3198 */
3199static int
3200ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3201 void *unused)
3202{
3203 switch (state) {
3204 case PM_HIBERNATION_PREPARE:
3205 pause_graph_tracing();
3206 break;
3207
3208 case PM_POST_HIBERNATION:
3209 unpause_graph_tracing();
3210 break;
3211 }
3212 return NOTIFY_DONE;
3213}
3214
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003215int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3216 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003217{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003218 int ret = 0;
3219
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003220 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003221
Steven Rostedt05ce5812009-03-24 00:18:31 -04003222 /* we currently allow only one tracer registered at a time */
3223 if (atomic_read(&ftrace_graph_active)) {
3224 ret = -EBUSY;
3225 goto out;
3226 }
3227
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003228 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3229 register_pm_notifier(&ftrace_suspend_notifier);
3230
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003231 atomic_inc(&ftrace_graph_active);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003232 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003233 if (ret) {
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003234 atomic_dec(&ftrace_graph_active);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003235 goto out;
3236 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003237
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003238 ftrace_graph_return = retfunc;
3239 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003240
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003241 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003242
3243out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003244 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003245 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003246}
3247
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003248void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003249{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003250 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003251
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003252 atomic_dec(&ftrace_graph_active);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003253 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003254 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003255 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003256 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003257 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003258
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003259 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003260}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003261
3262/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003263void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003264{
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003265 if (atomic_read(&ftrace_graph_active)) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003266 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3267 * sizeof(struct ftrace_ret_stack),
3268 GFP_KERNEL);
3269 if (!t->ret_stack)
3270 return;
3271 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003272 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003273 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003274 t->ftrace_timestamp = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003275 } else
3276 t->ret_stack = NULL;
3277}
3278
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003279void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003280{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003281 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3282
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003283 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003284 /* NULL must become visible to IRQs before we free it: */
3285 barrier();
3286
3287 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003288}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003289
3290void ftrace_graph_stop(void)
3291{
3292 ftrace_stop();
3293}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003294#endif
3295