blob: a23488988581aab4b4638b9c7ca6f282aed6c7de [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Abhishek Sagarf22f9a82008-06-21 23:50:29 +053025#include <linux/kprobes.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010026#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020027#include <linux/sysctl.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020031
Steven Rostedtad8d75f2009-04-14 19:39:12 -040032#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040033
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034#include <asm/ftrace.h>
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 Rostedt6912896e2008-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{
Steven Rostedtfb9fb012009-03-25 13:26:41 -040072 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020073};
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 Rostedtfb9fb012009-03-25 13:26:41 -0400274static int ftrace_profile_bits __read_mostly;
275static int ftrace_profile_enabled __read_mostly;
276
277/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400278static DEFINE_MUTEX(ftrace_profile_lock);
279
Steven Rostedtcafb1682009-03-24 20:50:39 -0400280static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400281
282#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
283
Steven Rostedt493762f2009-03-23 17:12:36 -0400284static void *
285function_stat_next(void *v, int idx)
286{
287 struct ftrace_profile *rec = v;
288 struct ftrace_profile_page *pg;
289
290 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
291
292 again:
293 rec++;
294 if ((void *)rec >= (void *)&pg->records[pg->index]) {
295 pg = pg->next;
296 if (!pg)
297 return NULL;
298 rec = &pg->records[0];
299 if (!rec->counter)
300 goto again;
301 }
302
303 return rec;
304}
305
306static void *function_stat_start(struct tracer_stat *trace)
307{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400308 struct ftrace_profile_stat *stat =
309 container_of(trace, struct ftrace_profile_stat, stat);
310
311 if (!stat || !stat->start)
312 return NULL;
313
314 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400315}
316
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400317#ifdef CONFIG_FUNCTION_GRAPH_TRACER
318/* function graph compares on total time */
319static int function_stat_cmp(void *p1, void *p2)
320{
321 struct ftrace_profile *a = p1;
322 struct ftrace_profile *b = p2;
323
324 if (a->time < b->time)
325 return -1;
326 if (a->time > b->time)
327 return 1;
328 else
329 return 0;
330}
331#else
332/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400333static int function_stat_cmp(void *p1, void *p2)
334{
335 struct ftrace_profile *a = p1;
336 struct ftrace_profile *b = p2;
337
338 if (a->counter < b->counter)
339 return -1;
340 if (a->counter > b->counter)
341 return 1;
342 else
343 return 0;
344}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400345#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400346
347static int function_stat_headers(struct seq_file *m)
348{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400349#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400350 seq_printf(m, " Function "
351 "Hit Time Avg\n"
352 " -------- "
353 "--- ---- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400354#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400355 seq_printf(m, " Function Hit\n"
356 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400357#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400358 return 0;
359}
360
361static int function_stat_show(struct seq_file *m, void *v)
362{
363 struct ftrace_profile *rec = v;
364 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400365#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400366 static DEFINE_MUTEX(mutex);
Steven Rostedt34886c82009-03-25 21:00:47 -0400367 static struct trace_seq s;
368 unsigned long long avg;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400369#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400370
371 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400372 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400373
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400374#ifdef CONFIG_FUNCTION_GRAPH_TRACER
375 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400376 avg = rec->time;
377 do_div(avg, rec->counter);
378
379 mutex_lock(&mutex);
380 trace_seq_init(&s);
381 trace_print_graph_duration(rec->time, &s);
382 trace_seq_puts(&s, " ");
383 trace_print_graph_duration(avg, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400384 trace_print_seq(m, &s);
385 mutex_unlock(&mutex);
386#endif
387 seq_putc(m, '\n');
388
Steven Rostedt493762f2009-03-23 17:12:36 -0400389 return 0;
390}
391
Steven Rostedtcafb1682009-03-24 20:50:39 -0400392static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400393{
394 struct ftrace_profile_page *pg;
395
Steven Rostedtcafb1682009-03-24 20:50:39 -0400396 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400397
398 while (pg) {
399 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
400 pg->index = 0;
401 pg = pg->next;
402 }
403
Steven Rostedtcafb1682009-03-24 20:50:39 -0400404 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400405 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
406}
407
Steven Rostedtcafb1682009-03-24 20:50:39 -0400408int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400409{
410 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400411 int functions;
412 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400413 int i;
414
415 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400416 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400417 return 0;
418
Steven Rostedtcafb1682009-03-24 20:50:39 -0400419 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
420 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400421 return -ENOMEM;
422
Steven Rostedt318e0a72009-03-25 20:06:34 -0400423#ifdef CONFIG_DYNAMIC_FTRACE
424 functions = ftrace_update_tot_cnt;
425#else
426 /*
427 * We do not know the number of functions that exist because
428 * dynamic tracing is what counts them. With past experience
429 * we have around 20K functions. That should be more than enough.
430 * It is highly unlikely we will execute every function in
431 * the kernel.
432 */
433 functions = 20000;
434#endif
435
Steven Rostedtcafb1682009-03-24 20:50:39 -0400436 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400437
Steven Rostedt318e0a72009-03-25 20:06:34 -0400438 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
439
440 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400441 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400442 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400443 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400444 pg = pg->next;
445 }
446
447 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400448
449 out_free:
450 pg = stat->start;
451 while (pg) {
452 unsigned long tmp = (unsigned long)pg;
453
454 pg = pg->next;
455 free_page(tmp);
456 }
457
458 free_page((unsigned long)stat->pages);
459 stat->pages = NULL;
460 stat->start = NULL;
461
462 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400463}
464
Steven Rostedtcafb1682009-03-24 20:50:39 -0400465static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400466{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400467 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400468 int size;
469
Steven Rostedtcafb1682009-03-24 20:50:39 -0400470 stat = &per_cpu(ftrace_profile_stats, cpu);
471
472 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400473 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400474 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400475 return 0;
476 }
477
478 /*
479 * We are profiling all functions, but usually only a few thousand
480 * functions are hit. We'll make a hash of 1024 items.
481 */
482 size = FTRACE_PROFILE_HASH_SIZE;
483
Steven Rostedtcafb1682009-03-24 20:50:39 -0400484 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400485
Steven Rostedtcafb1682009-03-24 20:50:39 -0400486 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400487 return -ENOMEM;
488
Steven Rostedtcafb1682009-03-24 20:50:39 -0400489 if (!ftrace_profile_bits) {
490 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400491
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492 for (; size; size >>= 1)
493 ftrace_profile_bits++;
494 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400495
Steven Rostedt318e0a72009-03-25 20:06:34 -0400496 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400497 if (ftrace_profile_pages_init(stat) < 0) {
498 kfree(stat->hash);
499 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400500 return -ENOMEM;
501 }
502
503 return 0;
504}
505
Steven Rostedtcafb1682009-03-24 20:50:39 -0400506static int ftrace_profile_init(void)
507{
508 int cpu;
509 int ret = 0;
510
511 for_each_online_cpu(cpu) {
512 ret = ftrace_profile_init_cpu(cpu);
513 if (ret)
514 break;
515 }
516
517 return ret;
518}
519
Steven Rostedt493762f2009-03-23 17:12:36 -0400520/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400521static struct ftrace_profile *
522ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400523{
524 struct ftrace_profile *rec;
525 struct hlist_head *hhd;
526 struct hlist_node *n;
527 unsigned long key;
528
529 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400530 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400531
532 if (hlist_empty(hhd))
533 return NULL;
534
535 hlist_for_each_entry_rcu(rec, n, hhd, node) {
536 if (rec->ip == ip)
537 return rec;
538 }
539
540 return NULL;
541}
542
Steven Rostedtcafb1682009-03-24 20:50:39 -0400543static void ftrace_add_profile(struct ftrace_profile_stat *stat,
544 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400545{
546 unsigned long key;
547
548 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400549 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400550}
551
Steven Rostedt318e0a72009-03-25 20:06:34 -0400552/*
553 * The memory is already allocated, this simply finds a new record to use.
554 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400555static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400556ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400557{
558 struct ftrace_profile *rec = NULL;
559
Steven Rostedt318e0a72009-03-25 20:06:34 -0400560 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400561 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400562 goto out;
563
Steven Rostedt493762f2009-03-23 17:12:36 -0400564 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400565 * Try to find the function again since an NMI
566 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400567 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400568 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400569 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400570 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400571
Steven Rostedtcafb1682009-03-24 20:50:39 -0400572 if (stat->pages->index == PROFILES_PER_PAGE) {
573 if (!stat->pages->next)
574 goto out;
575 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400576 }
577
Steven Rostedtcafb1682009-03-24 20:50:39 -0400578 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400579 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400580 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400581
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400584
585 return rec;
586}
587
Steven Rostedt493762f2009-03-23 17:12:36 -0400588static void
589function_profile_call(unsigned long ip, unsigned long parent_ip)
590{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 struct ftrace_profile *rec;
593 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400594
595 if (!ftrace_profile_enabled)
596 return;
597
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400599
600 stat = &__get_cpu_var(ftrace_profile_stats);
601 if (!stat->hash)
602 goto out;
603
604 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400606 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400607 if (!rec)
608 goto out;
609 }
610
611 rec->counter++;
612 out:
613 local_irq_restore(flags);
614}
615
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400616#ifdef CONFIG_FUNCTION_GRAPH_TRACER
617static int profile_graph_entry(struct ftrace_graph_ent *trace)
618{
619 function_profile_call(trace->func, 0);
620 return 1;
621}
622
623static void profile_graph_return(struct ftrace_graph_ret *trace)
624{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400626 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400628 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400629
630 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400631 stat = &__get_cpu_var(ftrace_profile_stats);
632 if (!stat->hash)
633 goto out;
634
Steven Rostedta2a16d62009-03-24 23:17:58 -0400635 calltime = trace->rettime - trace->calltime;
636
637 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
638 int index;
639
640 index = trace->depth;
641
642 /* Append this call time to the parent time to subtract */
643 if (index)
644 current->ret_stack[index - 1].subtime += calltime;
645
646 if (current->ret_stack[index].subtime < calltime)
647 calltime -= current->ret_stack[index].subtime;
648 else
649 calltime = 0;
650 }
651
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 rec = ftrace_find_profiled_func(stat, trace->func);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400653 if (rec)
Steven Rostedta2a16d62009-03-24 23:17:58 -0400654 rec->time += calltime;
655
Steven Rostedtcafb1682009-03-24 20:50:39 -0400656 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400657 local_irq_restore(flags);
658}
659
660static int register_ftrace_profiler(void)
661{
662 return register_ftrace_graph(&profile_graph_return,
663 &profile_graph_entry);
664}
665
666static void unregister_ftrace_profiler(void)
667{
668 unregister_ftrace_graph();
669}
670#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400671static struct ftrace_ops ftrace_profile_ops __read_mostly =
672{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400673 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400674};
675
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400676static int register_ftrace_profiler(void)
677{
678 return register_ftrace_function(&ftrace_profile_ops);
679}
680
681static void unregister_ftrace_profiler(void)
682{
683 unregister_ftrace_function(&ftrace_profile_ops);
684}
685#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
686
Steven Rostedt493762f2009-03-23 17:12:36 -0400687static ssize_t
688ftrace_profile_write(struct file *filp, const char __user *ubuf,
689 size_t cnt, loff_t *ppos)
690{
691 unsigned long val;
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400692 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400693 int ret;
694
695 if (cnt >= sizeof(buf))
696 return -EINVAL;
697
698 if (copy_from_user(&buf, ubuf, cnt))
699 return -EFAULT;
700
701 buf[cnt] = 0;
702
703 ret = strict_strtoul(buf, 10, &val);
704 if (ret < 0)
705 return ret;
706
707 val = !!val;
708
709 mutex_lock(&ftrace_profile_lock);
710 if (ftrace_profile_enabled ^ val) {
711 if (val) {
712 ret = ftrace_profile_init();
713 if (ret < 0) {
714 cnt = ret;
715 goto out;
716 }
717
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400718 ret = register_ftrace_profiler();
719 if (ret < 0) {
720 cnt = ret;
721 goto out;
722 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 ftrace_profile_enabled = 1;
724 } else {
725 ftrace_profile_enabled = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400726 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400727 }
728 }
729 out:
730 mutex_unlock(&ftrace_profile_lock);
731
732 filp->f_pos += cnt;
733
734 return cnt;
735}
736
737static ssize_t
738ftrace_profile_read(struct file *filp, char __user *ubuf,
739 size_t cnt, loff_t *ppos)
740{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400741 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 int r;
743
744 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
745 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
746}
747
748static const struct file_operations ftrace_profile_fops = {
749 .open = tracing_open_generic,
750 .read = ftrace_profile_read,
751 .write = ftrace_profile_write,
752};
753
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754/* used to initialize the real stat files */
755static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400756 .name = "functions",
757 .stat_start = function_stat_start,
758 .stat_next = function_stat_next,
759 .stat_cmp = function_stat_cmp,
760 .stat_headers = function_stat_headers,
761 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762};
763
Steven Rostedt493762f2009-03-23 17:12:36 -0400764static void ftrace_profile_debugfs(struct dentry *d_tracer)
765{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400766 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400768 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400769 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400771
Steven Rostedtcafb1682009-03-24 20:50:39 -0400772 for_each_possible_cpu(cpu) {
773 stat = &per_cpu(ftrace_profile_stats, cpu);
774
775 /* allocate enough for function name + cpu number */
776 name = kmalloc(32, GFP_KERNEL);
777 if (!name) {
778 /*
779 * The files created are permanent, if something happens
780 * we still do not free memory.
781 */
782 kfree(stat);
783 WARN(1,
784 "Could not allocate stat file for cpu %d\n",
785 cpu);
786 return;
787 }
788 stat->stat = function_stats;
789 snprintf(name, 32, "function%d", cpu);
790 stat->stat.name = name;
791 ret = register_stat_tracer(&stat->stat);
792 if (ret) {
793 WARN(1,
794 "Could not register function stat for cpu %d\n",
795 cpu);
796 kfree(name);
797 return;
798 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 }
800
801 entry = debugfs_create_file("function_profile_enabled", 0644,
802 d_tracer, NULL, &ftrace_profile_fops);
803 if (!entry)
804 pr_warning("Could not create debugfs "
805 "'function_profile_enabled' entry\n");
806}
807
808#else /* CONFIG_FUNCTION_PROFILER */
809static void ftrace_profile_debugfs(struct dentry *d_tracer)
810{
811}
812#endif /* CONFIG_FUNCTION_PROFILER */
813
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100814/* set when tracing only a pid */
815struct pid *ftrace_pid_trace;
816static struct pid * const ftrace_swapper_pid = &init_struct_pid;
817
Steven Rostedt3d083392008-05-12 21:20:42 +0200818#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100819
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400820#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400821# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400822#endif
823
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500824static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
825
Steven Rostedtb6887d72009-02-17 12:32:04 -0500826struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500827 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500828 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500829 unsigned long flags;
830 unsigned long ip;
831 void *data;
832 struct rcu_head rcu;
833};
834
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200835enum {
836 FTRACE_ENABLE_CALLS = (1 << 0),
837 FTRACE_DISABLE_CALLS = (1 << 1),
838 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
839 FTRACE_ENABLE_MCOUNT = (1 << 3),
840 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500841 FTRACE_START_FUNC_RET = (1 << 5),
842 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200843};
844
Steven Rostedt5072c592008-05-12 21:20:43 +0200845static int ftrace_filtered;
846
Lai Jiangshane94142a2009-03-13 17:51:27 +0800847static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200848
Steven Rostedt41c52c02008-05-22 11:46:33 -0400849static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200850
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200851struct ftrace_page {
852 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500853 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200854 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700855};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200856
857#define ENTRIES_PER_PAGE \
858 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
859
860/* estimate from running different kernels */
861#define NR_TO_INIT 10000
862
863static struct ftrace_page *ftrace_pages_start;
864static struct ftrace_page *ftrace_pages;
865
Steven Rostedt37ad50842008-05-12 21:20:48 +0200866static struct dyn_ftrace *ftrace_free_records;
867
Steven Rostedt265c8312009-02-13 12:43:56 -0500868/*
869 * This is a double for. Do not use 'break' to break out of the loop,
870 * you must use a goto.
871 */
872#define do_for_each_ftrace_rec(pg, rec) \
873 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
874 int _____i; \
875 for (_____i = 0; _____i < pg->index; _____i++) { \
876 rec = &pg->records[_____i];
877
878#define while_for_each_ftrace_rec() \
879 } \
880 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530881
882#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200883
884static int frozen_record_count;
885
Abhishek Sagarecea6562008-06-21 23:47:53 +0530886static inline void freeze_record(struct dyn_ftrace *rec)
887{
888 if (!(rec->flags & FTRACE_FL_FROZEN)) {
889 rec->flags |= FTRACE_FL_FROZEN;
890 frozen_record_count++;
891 }
892}
893
894static inline void unfreeze_record(struct dyn_ftrace *rec)
895{
896 if (rec->flags & FTRACE_FL_FROZEN) {
897 rec->flags &= ~FTRACE_FL_FROZEN;
898 frozen_record_count--;
899 }
900}
901
902static inline int record_frozen(struct dyn_ftrace *rec)
903{
904 return rec->flags & FTRACE_FL_FROZEN;
905}
906#else
907# define freeze_record(rec) ({ 0; })
908# define unfreeze_record(rec) ({ 0; })
909# define record_frozen(rec) ({ 0; })
910#endif /* CONFIG_KPROBES */
911
Ingo Molnare309b412008-05-12 21:20:51 +0200912static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad50842008-05-12 21:20:48 +0200913{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800914 rec->freelist = ftrace_free_records;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200915 ftrace_free_records = rec;
916 rec->flags |= FTRACE_FL_FREE;
917}
918
Steven Rostedtfed19392008-08-14 22:47:19 -0400919void ftrace_release(void *start, unsigned long size)
920{
921 struct dyn_ftrace *rec;
922 struct ftrace_page *pg;
923 unsigned long s = (unsigned long)start;
924 unsigned long e = s + size;
Steven Rostedtfed19392008-08-14 22:47:19 -0400925
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400926 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400927 return;
928
Steven Rostedt52baf112009-02-14 01:15:39 -0500929 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -0500930 do_for_each_ftrace_rec(pg, rec) {
Zhaolei2a4efa42009-03-25 12:06:05 +0800931 if ((rec->ip >= s) && (rec->ip < e)) {
932 /*
933 * rec->ip is changed in ftrace_free_rec()
934 * It should not between s and e if record was freed.
935 */
936 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
Steven Rostedt265c8312009-02-13 12:43:56 -0500937 ftrace_free_rec(rec);
Zhaolei2a4efa42009-03-25 12:06:05 +0800938 }
Steven Rostedt265c8312009-02-13 12:43:56 -0500939 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -0500940 mutex_unlock(&ftrace_lock);
Steven Rostedtfed19392008-08-14 22:47:19 -0400941}
942
Ingo Molnare309b412008-05-12 21:20:51 +0200943static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200944{
Steven Rostedt37ad50842008-05-12 21:20:48 +0200945 struct dyn_ftrace *rec;
946
947 /* First check for freed records */
948 if (ftrace_free_records) {
949 rec = ftrace_free_records;
950
Steven Rostedt37ad50842008-05-12 21:20:48 +0200951 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt6912896e2008-10-23 09:33:03 -0400952 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad50842008-05-12 21:20:48 +0200953 ftrace_free_records = NULL;
954 return NULL;
955 }
956
Lai Jiangshanee000b72009-03-24 13:38:06 +0800957 ftrace_free_records = rec->freelist;
Steven Rostedt37ad50842008-05-12 21:20:48 +0200958 memset(rec, 0, sizeof(*rec));
959 return rec;
960 }
961
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200962 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400963 if (!ftrace_pages->next) {
964 /* allocate another page */
965 ftrace_pages->next =
966 (void *)get_zeroed_page(GFP_KERNEL);
967 if (!ftrace_pages->next)
968 return NULL;
969 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200970 ftrace_pages = ftrace_pages->next;
971 }
972
973 return &ftrace_pages->records[ftrace_pages->index++];
974}
975
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400976static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200977ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200978{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400979 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200980
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800981 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400982 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200983
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400984 rec = ftrace_alloc_dyn_node(ip);
985 if (!rec)
986 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200987
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400988 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800989 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800990 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200991
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400992 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200993}
994
Steven Rostedt05736a42008-09-22 14:55:47 -0700995static void print_ip_ins(const char *fmt, unsigned char *p)
996{
997 int i;
998
999 printk(KERN_CONT "%s", fmt);
1000
1001 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1002 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1003}
1004
Steven Rostedt31e88902008-11-14 16:21:19 -08001005static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001006{
1007 switch (failed) {
1008 case -EFAULT:
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on modifying ");
1011 print_ip_sym(ip);
1012 break;
1013 case -EINVAL:
1014 FTRACE_WARN_ON_ONCE(1);
1015 pr_info("ftrace failed to modify ");
1016 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001017 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001018 printk(KERN_CONT "\n");
1019 break;
1020 case -EPERM:
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on writing ");
1023 print_ip_sym(ip);
1024 break;
1025 default:
1026 FTRACE_WARN_ON_ONCE(1);
1027 pr_info("ftrace faulted on unknown error ");
1028 print_ip_sym(ip);
1029 }
1030}
1031
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001032
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301033static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001034__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001035{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001036 unsigned long ftrace_addr;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001037 unsigned long ip, fl;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001038
Shaohua Lif0001202009-01-09 11:29:42 +08001039 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +02001040
1041 ip = rec->ip;
1042
Steven Rostedt982c3502008-11-15 16:31:41 -05001043 /*
1044 * If this record is not to be traced and
1045 * it is not enabled then do nothing.
1046 *
1047 * If this record is not to be traced and
Wenji Huang57794a92009-02-06 17:33:27 +08001048 * it is enabled then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001049 *
1050 */
1051 if (rec->flags & FTRACE_FL_NOTRACE) {
1052 if (rec->flags & FTRACE_FL_ENABLED)
1053 rec->flags &= ~FTRACE_FL_ENABLED;
1054 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001055 return 0;
1056
Steven Rostedt982c3502008-11-15 16:31:41 -05001057 } else if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001058 /*
Steven Rostedt982c3502008-11-15 16:31:41 -05001059 * Filtering is on:
Steven Rostedt5072c592008-05-12 21:20:43 +02001060 */
Steven Rostedt982c3502008-11-15 16:31:41 -05001061
1062 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
1063
1064 /* Record is filtered and enabled, do nothing */
1065 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
1066 return 0;
1067
Wenji Huang57794a92009-02-06 17:33:27 +08001068 /* Record is not filtered or enabled, do nothing */
Steven Rostedt982c3502008-11-15 16:31:41 -05001069 if (!fl)
1070 return 0;
1071
1072 /* Record is not filtered but enabled, disable it */
1073 if (fl == FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +02001074 rec->flags &= ~FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -05001075 else
1076 /* Otherwise record is filtered but not enabled, enable it */
Steven Rostedt5072c592008-05-12 21:20:43 +02001077 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001078 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -05001079 /* Disable or not filtered */
Steven Rostedt5072c592008-05-12 21:20:43 +02001080
1081 if (enable) {
Steven Rostedt982c3502008-11-15 16:31:41 -05001082 /* if record is enabled, do nothing */
Steven Rostedt41c52c02008-05-22 11:46:33 -04001083 if (rec->flags & FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +02001084 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -05001085
Steven Rostedt41c52c02008-05-22 11:46:33 -04001086 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -05001087
Steven Rostedt5072c592008-05-12 21:20:43 +02001088 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -05001089
Wenji Huang57794a92009-02-06 17:33:27 +08001090 /* if record is not enabled, do nothing */
Steven Rostedt5072c592008-05-12 21:20:43 +02001091 if (!(rec->flags & FTRACE_FL_ENABLED))
1092 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -05001093
Steven Rostedt5072c592008-05-12 21:20:43 +02001094 rec->flags &= ~FTRACE_FL_ENABLED;
1095 }
1096 }
1097
Steven Rostedt982c3502008-11-15 16:31:41 -05001098 if (rec->flags & FTRACE_FL_ENABLED)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001099 return ftrace_make_call(rec, ftrace_addr);
Steven Rostedt31e88902008-11-14 16:21:19 -08001100 else
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001101 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +02001102}
1103
1104static void ftrace_replace_code(int enable)
1105{
Steven Rostedt37ad50842008-05-12 21:20:48 +02001106 struct dyn_ftrace *rec;
1107 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001108 int failed;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001109
Steven Rostedt265c8312009-02-13 12:43:56 -05001110 do_for_each_ftrace_rec(pg, rec) {
1111 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001112 * Skip over free records, records that have
1113 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001114 */
1115 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001116 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001117 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001118 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001119
Steven Rostedt265c8312009-02-13 12:43:56 -05001120 /* ignore updates to this record's mcount site */
1121 if (get_kprobe((void *)rec->ip)) {
1122 freeze_record(rec);
1123 continue;
1124 } else {
1125 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +02001126 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001127
1128 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001129 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001130 rec->flags |= FTRACE_FL_FAILED;
1131 if ((system_state == SYSTEM_BOOTING) ||
1132 !core_kernel_text(rec->ip)) {
1133 ftrace_free_rec(rec);
Steven Rostedt43772452009-02-19 13:41:27 -05001134 } else {
Steven Rostedt265c8312009-02-13 12:43:56 -05001135 ftrace_bug(failed, rec->ip);
Steven Rostedt43772452009-02-19 13:41:27 -05001136 /* Stop processing */
1137 return;
1138 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001139 }
1140 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001141}
1142
Ingo Molnare309b412008-05-12 21:20:51 +02001143static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001144ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001145{
1146 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001147 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001148
1149 ip = rec->ip;
1150
Shaohua Li25aac9d2009-01-09 11:29:40 +08001151 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001152 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001153 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001154 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301155 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001156 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301157 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001158}
1159
Steven Rostedt000ab692009-02-17 13:35:06 -05001160/*
1161 * archs can override this function if they must do something
1162 * before the modifying code is performed.
1163 */
1164int __weak ftrace_arch_code_modify_prepare(void)
1165{
1166 return 0;
1167}
1168
1169/*
1170 * archs can override this function if they must do something
1171 * after the modifying code is performed.
1172 */
1173int __weak ftrace_arch_code_modify_post_process(void)
1174{
1175 return 0;
1176}
1177
Ingo Molnare309b412008-05-12 21:20:51 +02001178static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001179{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001180 int *command = data;
1181
Steven Rostedta3583242008-11-11 15:01:42 -05001182 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001183 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001184 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001185 ftrace_replace_code(0);
1186
1187 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1188 ftrace_update_ftrace_func(ftrace_trace_function);
1189
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001190 if (*command & FTRACE_START_FUNC_RET)
1191 ftrace_enable_ftrace_graph_caller();
1192 else if (*command & FTRACE_STOP_FUNC_RET)
1193 ftrace_disable_ftrace_graph_caller();
1194
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001195 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001196}
1197
Ingo Molnare309b412008-05-12 21:20:51 +02001198static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001199{
Steven Rostedt000ab692009-02-17 13:35:06 -05001200 int ret;
1201
1202 ret = ftrace_arch_code_modify_prepare();
1203 FTRACE_WARN_ON(ret);
1204 if (ret)
1205 return;
1206
Rusty Russell784e2d72008-07-28 12:16:31 -05001207 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001208
1209 ret = ftrace_arch_code_modify_post_process();
1210 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001211}
1212
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001213static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001214static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001215
1216static void ftrace_startup_enable(int command)
1217{
1218 if (saved_ftrace_func != ftrace_trace_function) {
1219 saved_ftrace_func = ftrace_trace_function;
1220 command |= FTRACE_UPDATE_TRACE_FUNC;
1221 }
1222
1223 if (!command || !ftrace_enabled)
1224 return;
1225
1226 ftrace_run_update_code(command);
1227}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001228
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001229static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001230{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001231 if (unlikely(ftrace_disabled))
1232 return;
1233
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001234 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001235 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001236
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001237 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001238}
1239
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001240static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001241{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001242 if (unlikely(ftrace_disabled))
1243 return;
1244
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001245 ftrace_start_up--;
1246 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001247 command |= FTRACE_DISABLE_CALLS;
1248
1249 if (saved_ftrace_func != ftrace_trace_function) {
1250 saved_ftrace_func = ftrace_trace_function;
1251 command |= FTRACE_UPDATE_TRACE_FUNC;
1252 }
1253
1254 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001255 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001256
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001257 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001258}
1259
Ingo Molnare309b412008-05-12 21:20:51 +02001260static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001261{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001262 int command = FTRACE_ENABLE_MCOUNT;
1263
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001264 if (unlikely(ftrace_disabled))
1265 return;
1266
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001267 /* Force update next time */
1268 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001269 /* ftrace_start_up is true if we want ftrace running */
1270 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001271 command |= FTRACE_ENABLE_CALLS;
1272
1273 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001274}
1275
Ingo Molnare309b412008-05-12 21:20:51 +02001276static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001277{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001278 int command = FTRACE_DISABLE_MCOUNT;
1279
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001280 if (unlikely(ftrace_disabled))
1281 return;
1282
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001283 /* ftrace_start_up is true if ftrace is running */
1284 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001285 command |= FTRACE_DISABLE_CALLS;
1286
1287 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001288}
1289
Steven Rostedt3d083392008-05-12 21:20:42 +02001290static cycle_t ftrace_update_time;
1291static unsigned long ftrace_update_cnt;
1292unsigned long ftrace_update_tot_cnt;
1293
Steven Rostedt31e88902008-11-14 16:21:19 -08001294static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001295{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001296 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301297 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001298
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001299 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001300 ftrace_update_cnt = 0;
1301
Lai Jiangshane94142a2009-03-13 17:51:27 +08001302 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301303
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001304 /* If something went wrong, bail without enabling anything */
1305 if (unlikely(ftrace_disabled))
1306 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001307
Lai Jiangshane94142a2009-03-13 17:51:27 +08001308 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001309 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001310 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301311
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001312 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -08001313 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001314 p->flags |= FTRACE_FL_CONVERTED;
1315 ftrace_update_cnt++;
1316 } else
1317 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +02001318 }
1319
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001320 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001321 ftrace_update_time = stop - start;
1322 ftrace_update_tot_cnt += ftrace_update_cnt;
1323
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001324 return 0;
1325}
1326
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001327static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001328{
1329 struct ftrace_page *pg;
1330 int cnt;
1331 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001332
1333 /* allocate a few pages */
1334 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1335 if (!ftrace_pages_start)
1336 return -1;
1337
1338 /*
1339 * Allocate a few more pages.
1340 *
1341 * TODO: have some parser search vmlinux before
1342 * final linking to find all calls to ftrace.
1343 * Then we can:
1344 * a) know how many pages to allocate.
1345 * and/or
1346 * b) set up the table then.
1347 *
1348 * The dynamic code is still necessary for
1349 * modules.
1350 */
1351
1352 pg = ftrace_pages = ftrace_pages_start;
1353
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001354 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001355 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001356 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001357
1358 for (i = 0; i < cnt; i++) {
1359 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1360
1361 /* If we fail, we'll try later anyway */
1362 if (!pg->next)
1363 break;
1364
1365 pg = pg->next;
1366 }
1367
1368 return 0;
1369}
1370
Steven Rostedt5072c592008-05-12 21:20:43 +02001371enum {
1372 FTRACE_ITER_FILTER = (1 << 0),
1373 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -04001374 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301375 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001376 FTRACE_ITER_PRINTALL = (1 << 4),
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001377 FTRACE_ITER_HASH = (1 << 5),
Steven Rostedt5072c592008-05-12 21:20:43 +02001378};
1379
1380#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1381
1382struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001383 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001384 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001385 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001386 unsigned flags;
1387 unsigned char buffer[FTRACE_BUFF_MAX+1];
1388 unsigned buffer_idx;
1389 unsigned filtered;
1390};
1391
Ingo Molnare309b412008-05-12 21:20:51 +02001392static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001393t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1394{
1395 struct ftrace_iterator *iter = m->private;
1396 struct hlist_node *hnd = v;
1397 struct hlist_head *hhd;
1398
1399 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1400
1401 (*pos)++;
1402
1403 retry:
1404 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1405 return NULL;
1406
1407 hhd = &ftrace_func_hash[iter->hidx];
1408
1409 if (hlist_empty(hhd)) {
1410 iter->hidx++;
1411 hnd = NULL;
1412 goto retry;
1413 }
1414
1415 if (!hnd)
1416 hnd = hhd->first;
1417 else {
1418 hnd = hnd->next;
1419 if (!hnd) {
1420 iter->hidx++;
1421 goto retry;
1422 }
1423 }
1424
1425 return hnd;
1426}
1427
1428static void *t_hash_start(struct seq_file *m, loff_t *pos)
1429{
1430 struct ftrace_iterator *iter = m->private;
1431 void *p = NULL;
1432
1433 iter->flags |= FTRACE_ITER_HASH;
1434
1435 return t_hash_next(m, p, pos);
1436}
1437
1438static int t_hash_show(struct seq_file *m, void *v)
1439{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001440 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001441 struct hlist_node *hnd = v;
1442 char str[KSYM_SYMBOL_LEN];
1443
Steven Rostedtb6887d72009-02-17 12:32:04 -05001444 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001445
Steven Rostedt809dcf22009-02-16 23:06:01 -05001446 if (rec->ops->print)
1447 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1448
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001449 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1450 seq_printf(m, "%s:", str);
1451
1452 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1453 seq_printf(m, "%s", str);
1454
1455 if (rec->data)
1456 seq_printf(m, ":%p", rec->data);
1457 seq_putc(m, '\n');
1458
1459 return 0;
1460}
1461
1462static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001463t_next(struct seq_file *m, void *v, loff_t *pos)
1464{
1465 struct ftrace_iterator *iter = m->private;
1466 struct dyn_ftrace *rec = NULL;
1467
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001468 if (iter->flags & FTRACE_ITER_HASH)
1469 return t_hash_next(m, v, pos);
1470
Steven Rostedt5072c592008-05-12 21:20:43 +02001471 (*pos)++;
1472
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001473 if (iter->flags & FTRACE_ITER_PRINTALL)
1474 return NULL;
1475
Steven Rostedt5072c592008-05-12 21:20:43 +02001476 retry:
1477 if (iter->idx >= iter->pg->index) {
1478 if (iter->pg->next) {
1479 iter->pg = iter->pg->next;
1480 iter->idx = 0;
1481 goto retry;
Liming Wang50cdaf02008-11-28 12:13:21 +08001482 } else {
1483 iter->idx = -1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001484 }
1485 } else {
1486 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001487 if ((rec->flags & FTRACE_FL_FREE) ||
1488
1489 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301490 (rec->flags & FTRACE_FL_FAILED)) ||
1491
1492 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001493 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301494
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05001495 ((iter->flags & FTRACE_ITER_FILTER) &&
1496 !(rec->flags & FTRACE_FL_FILTER)) ||
1497
Steven Rostedt41c52c02008-05-22 11:46:33 -04001498 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1499 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001500 rec = NULL;
1501 goto retry;
1502 }
1503 }
1504
Steven Rostedt5072c592008-05-12 21:20:43 +02001505 return rec;
1506}
1507
1508static void *t_start(struct seq_file *m, loff_t *pos)
1509{
1510 struct ftrace_iterator *iter = m->private;
1511 void *p = NULL;
Steven Rostedt5072c592008-05-12 21:20:43 +02001512
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001513 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001514 /*
1515 * For set_ftrace_filter reading, if we have the filter
1516 * off, we can short cut and just print out that all
1517 * functions are enabled.
1518 */
1519 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1520 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001521 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001522 iter->flags |= FTRACE_ITER_PRINTALL;
1523 (*pos)++;
1524 return iter;
1525 }
1526
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001527 if (iter->flags & FTRACE_ITER_HASH)
1528 return t_hash_start(m, pos);
1529
Liming Wang50cdaf02008-11-28 12:13:21 +08001530 if (*pos > 0) {
1531 if (iter->idx < 0)
1532 return p;
1533 (*pos)--;
1534 iter->idx--;
1535 }
walimis5821e1b2008-11-15 15:19:06 +08001536
Liming Wang50cdaf02008-11-28 12:13:21 +08001537 p = t_next(m, p, pos);
Steven Rostedt5072c592008-05-12 21:20:43 +02001538
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001539 if (!p)
1540 return t_hash_start(m, pos);
1541
Steven Rostedt5072c592008-05-12 21:20:43 +02001542 return p;
1543}
1544
1545static void t_stop(struct seq_file *m, void *p)
1546{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001547 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001548}
1549
1550static int t_show(struct seq_file *m, void *v)
1551{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001552 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001553 struct dyn_ftrace *rec = v;
1554 char str[KSYM_SYMBOL_LEN];
1555
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001556 if (iter->flags & FTRACE_ITER_HASH)
1557 return t_hash_show(m, v);
1558
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001559 if (iter->flags & FTRACE_ITER_PRINTALL) {
1560 seq_printf(m, "#### all functions enabled ####\n");
1561 return 0;
1562 }
1563
Steven Rostedt5072c592008-05-12 21:20:43 +02001564 if (!rec)
1565 return 0;
1566
1567 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1568
Liming Wang50cdaf02008-11-28 12:13:21 +08001569 seq_printf(m, "%s\n", str);
Steven Rostedt5072c592008-05-12 21:20:43 +02001570
1571 return 0;
1572}
1573
1574static struct seq_operations show_ftrace_seq_ops = {
1575 .start = t_start,
1576 .next = t_next,
1577 .stop = t_stop,
1578 .show = t_show,
1579};
1580
Ingo Molnare309b412008-05-12 21:20:51 +02001581static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001582ftrace_avail_open(struct inode *inode, struct file *file)
1583{
1584 struct ftrace_iterator *iter;
1585 int ret;
1586
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001587 if (unlikely(ftrace_disabled))
1588 return -ENODEV;
1589
Steven Rostedt5072c592008-05-12 21:20:43 +02001590 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1591 if (!iter)
1592 return -ENOMEM;
1593
1594 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001595
1596 ret = seq_open(file, &show_ftrace_seq_ops);
1597 if (!ret) {
1598 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001599
Steven Rostedt5072c592008-05-12 21:20:43 +02001600 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001601 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001602 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001603 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001604
1605 return ret;
1606}
1607
1608int ftrace_avail_release(struct inode *inode, struct file *file)
1609{
1610 struct seq_file *m = (struct seq_file *)file->private_data;
1611 struct ftrace_iterator *iter = m->private;
1612
1613 seq_release(inode, file);
1614 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001615
Steven Rostedt5072c592008-05-12 21:20:43 +02001616 return 0;
1617}
1618
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301619static int
1620ftrace_failures_open(struct inode *inode, struct file *file)
1621{
1622 int ret;
1623 struct seq_file *m;
1624 struct ftrace_iterator *iter;
1625
1626 ret = ftrace_avail_open(inode, file);
1627 if (!ret) {
1628 m = (struct seq_file *)file->private_data;
1629 iter = (struct ftrace_iterator *)m->private;
1630 iter->flags = FTRACE_ITER_FAILURES;
1631 }
1632
1633 return ret;
1634}
1635
1636
Steven Rostedt41c52c02008-05-22 11:46:33 -04001637static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001638{
1639 struct ftrace_page *pg;
1640 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001641 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001642
Steven Rostedt52baf112009-02-14 01:15:39 -05001643 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001644 if (enable)
1645 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001646 do_for_each_ftrace_rec(pg, rec) {
1647 if (rec->flags & FTRACE_FL_FAILED)
1648 continue;
1649 rec->flags &= ~type;
1650 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001651 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001652}
1653
Ingo Molnare309b412008-05-12 21:20:51 +02001654static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001655ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001656{
1657 struct ftrace_iterator *iter;
1658 int ret = 0;
1659
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001660 if (unlikely(ftrace_disabled))
1661 return -ENODEV;
1662
Steven Rostedt5072c592008-05-12 21:20:43 +02001663 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1664 if (!iter)
1665 return -ENOMEM;
1666
Steven Rostedt41c52c02008-05-22 11:46:33 -04001667 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001668 if ((file->f_mode & FMODE_WRITE) &&
1669 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001670 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001671
1672 if (file->f_mode & FMODE_READ) {
1673 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001674 iter->flags = enable ? FTRACE_ITER_FILTER :
1675 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001676
1677 ret = seq_open(file, &show_ftrace_seq_ops);
1678 if (!ret) {
1679 struct seq_file *m = file->private_data;
1680 m->private = iter;
1681 } else
1682 kfree(iter);
1683 } else
1684 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001685 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001686
1687 return ret;
1688}
1689
Steven Rostedt41c52c02008-05-22 11:46:33 -04001690static int
1691ftrace_filter_open(struct inode *inode, struct file *file)
1692{
1693 return ftrace_regex_open(inode, file, 1);
1694}
1695
1696static int
1697ftrace_notrace_open(struct inode *inode, struct file *file)
1698{
1699 return ftrace_regex_open(inode, file, 0);
1700}
1701
Ingo Molnare309b412008-05-12 21:20:51 +02001702static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001703ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001704{
1705 loff_t ret;
1706
1707 if (file->f_mode & FMODE_READ)
1708 ret = seq_lseek(file, offset, origin);
1709 else
1710 file->f_pos = ret = 1;
1711
1712 return ret;
1713}
1714
1715enum {
1716 MATCH_FULL,
1717 MATCH_FRONT_ONLY,
1718 MATCH_MIDDLE_ONLY,
1719 MATCH_END_ONLY,
1720};
1721
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001722/*
1723 * (static function - no need for kernel doc)
1724 *
1725 * Pass in a buffer containing a glob and this function will
1726 * set search to point to the search part of the buffer and
1727 * return the type of search it is (see enum above).
1728 * This does modify buff.
1729 *
1730 * Returns enum type.
1731 * search returns the pointer to use for comparison.
1732 * not returns 1 if buff started with a '!'
1733 * 0 otherwise.
1734 */
1735static int
Steven Rostedt64e7c442009-02-13 17:08:48 -05001736ftrace_setup_glob(char *buff, int len, char **search, int *not)
Steven Rostedt5072c592008-05-12 21:20:43 +02001737{
Steven Rostedt5072c592008-05-12 21:20:43 +02001738 int type = MATCH_FULL;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001739 int i;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001740
1741 if (buff[0] == '!') {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001742 *not = 1;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001743 buff++;
1744 len--;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001745 } else
1746 *not = 0;
1747
1748 *search = buff;
Steven Rostedt5072c592008-05-12 21:20:43 +02001749
1750 for (i = 0; i < len; i++) {
1751 if (buff[i] == '*') {
1752 if (!i) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001753 *search = buff + 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001754 type = MATCH_END_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001755 } else {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001756 if (type == MATCH_END_ONLY)
Steven Rostedt5072c592008-05-12 21:20:43 +02001757 type = MATCH_MIDDLE_ONLY;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001758 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001759 type = MATCH_FRONT_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001760 buff[i] = 0;
1761 break;
1762 }
1763 }
1764 }
1765
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001766 return type;
1767}
1768
Steven Rostedt64e7c442009-02-13 17:08:48 -05001769static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001770{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001771 int matched = 0;
1772 char *ptr;
1773
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001774 switch (type) {
1775 case MATCH_FULL:
1776 if (strcmp(str, regex) == 0)
1777 matched = 1;
1778 break;
1779 case MATCH_FRONT_ONLY:
1780 if (strncmp(str, regex, len) == 0)
1781 matched = 1;
1782 break;
1783 case MATCH_MIDDLE_ONLY:
1784 if (strstr(str, regex))
1785 matched = 1;
1786 break;
1787 case MATCH_END_ONLY:
1788 ptr = strstr(str, regex);
1789 if (ptr && (ptr[len] == 0))
1790 matched = 1;
1791 break;
1792 }
1793
1794 return matched;
1795}
1796
Steven Rostedt64e7c442009-02-13 17:08:48 -05001797static int
1798ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1799{
1800 char str[KSYM_SYMBOL_LEN];
1801
1802 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1803 return ftrace_match(str, regex, len, type);
1804}
1805
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001806static void ftrace_match_records(char *buff, int len, int enable)
1807{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001808 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001809 struct ftrace_page *pg;
1810 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001811 unsigned long flag;
1812 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001813 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001814 int not;
1815
Steven Rostedt6a24a242009-02-17 11:20:26 -05001816 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001817 type = ftrace_setup_glob(buff, len, &search, &not);
1818
1819 search_len = strlen(search);
1820
Steven Rostedt52baf112009-02-14 01:15:39 -05001821 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001822 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001823
Steven Rostedt265c8312009-02-13 12:43:56 -05001824 if (rec->flags & FTRACE_FL_FAILED)
1825 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001826
1827 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001828 if (not)
1829 rec->flags &= ~flag;
1830 else
1831 rec->flags |= flag;
1832 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001833 /*
1834 * Only enable filtering if we have a function that
1835 * is filtered on.
1836 */
1837 if (enable && (rec->flags & FTRACE_FL_FILTER))
1838 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001839 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001840 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001841}
1842
Steven Rostedt64e7c442009-02-13 17:08:48 -05001843static int
1844ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1845 char *regex, int len, int type)
1846{
1847 char str[KSYM_SYMBOL_LEN];
1848 char *modname;
1849
1850 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1851
1852 if (!modname || strcmp(modname, mod))
1853 return 0;
1854
1855 /* blank search means to match all funcs in the mod */
1856 if (len)
1857 return ftrace_match(str, regex, len, type);
1858 else
1859 return 1;
1860}
1861
1862static void ftrace_match_module_records(char *buff, char *mod, int enable)
1863{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001864 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001865 struct ftrace_page *pg;
1866 struct dyn_ftrace *rec;
1867 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001868 char *search = buff;
1869 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001870 int not = 0;
1871
Steven Rostedt6a24a242009-02-17 11:20:26 -05001872 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1873
Steven Rostedt64e7c442009-02-13 17:08:48 -05001874 /* blank or '*' mean the same */
1875 if (strcmp(buff, "*") == 0)
1876 buff[0] = 0;
1877
1878 /* handle the case of 'dont filter this module' */
1879 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1880 buff[0] = 0;
1881 not = 1;
1882 }
1883
1884 if (strlen(buff)) {
1885 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1886 search_len = strlen(search);
1887 }
1888
Steven Rostedt52baf112009-02-14 01:15:39 -05001889 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001890 do_for_each_ftrace_rec(pg, rec) {
1891
1892 if (rec->flags & FTRACE_FL_FAILED)
1893 continue;
1894
1895 if (ftrace_match_module_record(rec, mod,
1896 search, search_len, type)) {
1897 if (not)
1898 rec->flags &= ~flag;
1899 else
1900 rec->flags |= flag;
1901 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001902 if (enable && (rec->flags & FTRACE_FL_FILTER))
1903 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001904
1905 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001906 mutex_unlock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001907}
1908
Steven Rostedtf6180772009-02-14 00:40:25 -05001909/*
1910 * We register the module command as a template to show others how
1911 * to register the a command as well.
1912 */
1913
1914static int
1915ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1916{
1917 char *mod;
1918
1919 /*
1920 * cmd == 'mod' because we only registered this func
1921 * for the 'mod' ftrace_func_command.
1922 * But if you register one func with multiple commands,
1923 * you can tell which command was used by the cmd
1924 * parameter.
1925 */
1926
1927 /* we must have a module name */
1928 if (!param)
1929 return -EINVAL;
1930
1931 mod = strsep(&param, ":");
1932 if (!strlen(mod))
1933 return -EINVAL;
1934
1935 ftrace_match_module_records(func, mod, enable);
1936 return 0;
1937}
1938
1939static struct ftrace_func_command ftrace_mod_cmd = {
1940 .name = "mod",
1941 .func = ftrace_mod_callback,
1942};
1943
1944static int __init ftrace_mod_cmd_init(void)
1945{
1946 return register_ftrace_command(&ftrace_mod_cmd);
1947}
1948device_initcall(ftrace_mod_cmd_init);
1949
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001951function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001952{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001953 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001954 struct hlist_head *hhd;
1955 struct hlist_node *n;
1956 unsigned long key;
1957 int resched;
1958
1959 key = hash_long(ip, FTRACE_HASH_BITS);
1960
1961 hhd = &ftrace_func_hash[key];
1962
1963 if (hlist_empty(hhd))
1964 return;
1965
1966 /*
1967 * Disable preemption for these calls to prevent a RCU grace
1968 * period. This syncs the hash iteration and freeing of items
1969 * on the hash. rcu_read_lock is too dangerous here.
1970 */
1971 resched = ftrace_preempt_disable();
1972 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1973 if (entry->ip == ip)
1974 entry->ops->func(ip, parent_ip, &entry->data);
1975 }
1976 ftrace_preempt_enable(resched);
1977}
1978
Steven Rostedtb6887d72009-02-17 12:32:04 -05001979static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001980{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001981 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001982};
1983
Steven Rostedtb6887d72009-02-17 12:32:04 -05001984static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001985
Steven Rostedtb6887d72009-02-17 12:32:04 -05001986static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001987{
1988 int i;
1989
Steven Rostedtb6887d72009-02-17 12:32:04 -05001990 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001991 return;
1992
1993 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1994 struct hlist_head *hhd = &ftrace_func_hash[i];
1995 if (hhd->first)
1996 break;
1997 }
1998 /* Nothing registered? */
1999 if (i == FTRACE_FUNC_HASHSIZE)
2000 return;
2001
Steven Rostedtb6887d72009-02-17 12:32:04 -05002002 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002003 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002004 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002005}
2006
Steven Rostedtb6887d72009-02-17 12:32:04 -05002007static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002008{
2009 int i;
2010
Steven Rostedtb6887d72009-02-17 12:32:04 -05002011 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002012 return;
2013
2014 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2015 struct hlist_head *hhd = &ftrace_func_hash[i];
2016 if (hhd->first)
2017 return;
2018 }
2019
2020 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002021 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002022 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05002023 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002024}
2025
2026
2027static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2028{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002029 struct ftrace_func_probe *entry =
2030 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002031
2032 if (entry->ops->free)
2033 entry->ops->free(&entry->data);
2034 kfree(entry);
2035}
2036
2037
2038int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002039register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002040 void *data)
2041{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002042 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002043 struct ftrace_page *pg;
2044 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002045 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002046 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002047 int count = 0;
2048 char *search;
2049
2050 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2051 len = strlen(search);
2052
Steven Rostedtb6887d72009-02-17 12:32:04 -05002053 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002054 if (WARN_ON(not))
2055 return -EINVAL;
2056
2057 mutex_lock(&ftrace_lock);
2058 do_for_each_ftrace_rec(pg, rec) {
2059
2060 if (rec->flags & FTRACE_FL_FAILED)
2061 continue;
2062
2063 if (!ftrace_match_record(rec, search, len, type))
2064 continue;
2065
2066 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2067 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002068 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002069 if (!count)
2070 count = -ENOMEM;
2071 goto out_unlock;
2072 }
2073
2074 count++;
2075
2076 entry->data = data;
2077
2078 /*
2079 * The caller might want to do something special
2080 * for each function we find. We call the callback
2081 * to give the caller an opportunity to do so.
2082 */
2083 if (ops->callback) {
2084 if (ops->callback(rec->ip, &entry->data) < 0) {
2085 /* caller does not like this func */
2086 kfree(entry);
2087 continue;
2088 }
2089 }
2090
2091 entry->ops = ops;
2092 entry->ip = rec->ip;
2093
2094 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2095 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2096
2097 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002098 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002099
2100 out_unlock:
2101 mutex_unlock(&ftrace_lock);
2102
2103 return count;
2104}
2105
2106enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002107 PROBE_TEST_FUNC = 1,
2108 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002109};
2110
2111static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002112__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002113 void *data, int flags)
2114{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002115 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002116 struct hlist_node *n, *tmp;
2117 char str[KSYM_SYMBOL_LEN];
2118 int type = MATCH_FULL;
2119 int i, len = 0;
2120 char *search;
2121
2122 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2123 glob = NULL;
2124 else {
2125 int not;
2126
2127 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2128 len = strlen(search);
2129
Steven Rostedtb6887d72009-02-17 12:32:04 -05002130 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002131 if (WARN_ON(not))
2132 return;
2133 }
2134
2135 mutex_lock(&ftrace_lock);
2136 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2137 struct hlist_head *hhd = &ftrace_func_hash[i];
2138
2139 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2140
2141 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002142 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002143 continue;
2144
Steven Rostedtb6887d72009-02-17 12:32:04 -05002145 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002146 continue;
2147
2148 /* do this last, since it is the most expensive */
2149 if (glob) {
2150 kallsyms_lookup(entry->ip, NULL, NULL,
2151 NULL, str);
2152 if (!ftrace_match(str, glob, len, type))
2153 continue;
2154 }
2155
2156 hlist_del(&entry->node);
2157 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2158 }
2159 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002160 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002161 mutex_unlock(&ftrace_lock);
2162}
2163
2164void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002165unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002166 void *data)
2167{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002168 __unregister_ftrace_function_probe(glob, ops, data,
2169 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002170}
2171
2172void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002173unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002174{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002175 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002176}
2177
Steven Rostedtb6887d72009-02-17 12:32:04 -05002178void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002179{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002180 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002181}
2182
Steven Rostedtf6180772009-02-14 00:40:25 -05002183static LIST_HEAD(ftrace_commands);
2184static DEFINE_MUTEX(ftrace_cmd_mutex);
2185
2186int register_ftrace_command(struct ftrace_func_command *cmd)
2187{
2188 struct ftrace_func_command *p;
2189 int ret = 0;
2190
2191 mutex_lock(&ftrace_cmd_mutex);
2192 list_for_each_entry(p, &ftrace_commands, list) {
2193 if (strcmp(cmd->name, p->name) == 0) {
2194 ret = -EBUSY;
2195 goto out_unlock;
2196 }
2197 }
2198 list_add(&cmd->list, &ftrace_commands);
2199 out_unlock:
2200 mutex_unlock(&ftrace_cmd_mutex);
2201
2202 return ret;
2203}
2204
2205int unregister_ftrace_command(struct ftrace_func_command *cmd)
2206{
2207 struct ftrace_func_command *p, *n;
2208 int ret = -ENODEV;
2209
2210 mutex_lock(&ftrace_cmd_mutex);
2211 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2212 if (strcmp(cmd->name, p->name) == 0) {
2213 ret = 0;
2214 list_del_init(&p->list);
2215 goto out_unlock;
2216 }
2217 }
2218 out_unlock:
2219 mutex_unlock(&ftrace_cmd_mutex);
2220
2221 return ret;
2222}
2223
Steven Rostedt64e7c442009-02-13 17:08:48 -05002224static int ftrace_process_regex(char *buff, int len, int enable)
2225{
Steven Rostedtf6180772009-02-14 00:40:25 -05002226 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002227 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002228 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002229
2230 func = strsep(&next, ":");
2231
2232 if (!next) {
2233 ftrace_match_records(func, len, enable);
2234 return 0;
2235 }
2236
Steven Rostedtf6180772009-02-14 00:40:25 -05002237 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002238
2239 command = strsep(&next, ":");
2240
Steven Rostedtf6180772009-02-14 00:40:25 -05002241 mutex_lock(&ftrace_cmd_mutex);
2242 list_for_each_entry(p, &ftrace_commands, list) {
2243 if (strcmp(p->name, command) == 0) {
2244 ret = p->func(func, command, next, enable);
2245 goto out_unlock;
2246 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002247 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002248 out_unlock:
2249 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002250
Steven Rostedtf6180772009-02-14 00:40:25 -05002251 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002252}
2253
Ingo Molnare309b412008-05-12 21:20:51 +02002254static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002255ftrace_regex_write(struct file *file, const char __user *ubuf,
2256 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002257{
2258 struct ftrace_iterator *iter;
2259 char ch;
2260 size_t read = 0;
2261 ssize_t ret;
2262
2263 if (!cnt || cnt < 0)
2264 return 0;
2265
Steven Rostedt41c52c02008-05-22 11:46:33 -04002266 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002267
2268 if (file->f_mode & FMODE_READ) {
2269 struct seq_file *m = file->private_data;
2270 iter = m->private;
2271 } else
2272 iter = file->private_data;
2273
2274 if (!*ppos) {
2275 iter->flags &= ~FTRACE_ITER_CONT;
2276 iter->buffer_idx = 0;
2277 }
2278
2279 ret = get_user(ch, ubuf++);
2280 if (ret)
2281 goto out;
2282 read++;
2283 cnt--;
2284
2285 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2286 /* skip white space */
2287 while (cnt && isspace(ch)) {
2288 ret = get_user(ch, ubuf++);
2289 if (ret)
2290 goto out;
2291 read++;
2292 cnt--;
2293 }
2294
Steven Rostedt5072c592008-05-12 21:20:43 +02002295 if (isspace(ch)) {
2296 file->f_pos += read;
2297 ret = read;
2298 goto out;
2299 }
2300
2301 iter->buffer_idx = 0;
2302 }
2303
2304 while (cnt && !isspace(ch)) {
2305 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2306 iter->buffer[iter->buffer_idx++] = ch;
2307 else {
2308 ret = -EINVAL;
2309 goto out;
2310 }
2311 ret = get_user(ch, ubuf++);
2312 if (ret)
2313 goto out;
2314 read++;
2315 cnt--;
2316 }
2317
2318 if (isspace(ch)) {
2319 iter->filtered++;
2320 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002321 ret = ftrace_process_regex(iter->buffer,
2322 iter->buffer_idx, enable);
2323 if (ret)
2324 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02002325 iter->buffer_idx = 0;
2326 } else
2327 iter->flags |= FTRACE_ITER_CONT;
2328
2329
2330 file->f_pos += read;
2331
2332 ret = read;
2333 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04002334 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002335
2336 return ret;
2337}
2338
Steven Rostedt41c52c02008-05-22 11:46:33 -04002339static ssize_t
2340ftrace_filter_write(struct file *file, const char __user *ubuf,
2341 size_t cnt, loff_t *ppos)
2342{
2343 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2344}
2345
2346static ssize_t
2347ftrace_notrace_write(struct file *file, const char __user *ubuf,
2348 size_t cnt, loff_t *ppos)
2349{
2350 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2351}
2352
2353static void
2354ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2355{
2356 if (unlikely(ftrace_disabled))
2357 return;
2358
2359 mutex_lock(&ftrace_regex_lock);
2360 if (reset)
2361 ftrace_filter_reset(enable);
2362 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002363 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002364 mutex_unlock(&ftrace_regex_lock);
2365}
2366
Steven Rostedt77a2b372008-05-12 21:20:45 +02002367/**
2368 * ftrace_set_filter - set a function to filter on in ftrace
2369 * @buf - the string that holds the function filter text.
2370 * @len - the length of the string.
2371 * @reset - non zero to reset all filters before applying this filter.
2372 *
2373 * Filters denote which functions should be enabled when tracing is enabled.
2374 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2375 */
Ingo Molnare309b412008-05-12 21:20:51 +02002376void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002377{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002378 ftrace_set_regex(buf, len, reset, 1);
2379}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002380
Steven Rostedt41c52c02008-05-22 11:46:33 -04002381/**
2382 * ftrace_set_notrace - set a function to not trace in ftrace
2383 * @buf - the string that holds the function notrace text.
2384 * @len - the length of the string.
2385 * @reset - non zero to reset all filters before applying this filter.
2386 *
2387 * Notrace Filters denote which functions should not be enabled when tracing
2388 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2389 * for tracing.
2390 */
2391void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2392{
2393 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002394}
2395
Ingo Molnare309b412008-05-12 21:20:51 +02002396static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002397ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002398{
2399 struct seq_file *m = (struct seq_file *)file->private_data;
2400 struct ftrace_iterator *iter;
2401
Steven Rostedt41c52c02008-05-22 11:46:33 -04002402 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002403 if (file->f_mode & FMODE_READ) {
2404 iter = m->private;
2405
2406 seq_release(inode, file);
2407 } else
2408 iter = file->private_data;
2409
2410 if (iter->buffer_idx) {
2411 iter->filtered++;
2412 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt7f24b312009-02-13 14:37:33 -05002413 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002414 }
2415
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002416 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002417 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002418 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002419 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002420
2421 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002422 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002423 return 0;
2424}
2425
Steven Rostedt41c52c02008-05-22 11:46:33 -04002426static int
2427ftrace_filter_release(struct inode *inode, struct file *file)
2428{
2429 return ftrace_regex_release(inode, file, 1);
2430}
2431
2432static int
2433ftrace_notrace_release(struct inode *inode, struct file *file)
2434{
2435 return ftrace_regex_release(inode, file, 0);
2436}
2437
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002438static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002439 .open = ftrace_avail_open,
2440 .read = seq_read,
2441 .llseek = seq_lseek,
2442 .release = ftrace_avail_release,
2443};
2444
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002445static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302446 .open = ftrace_failures_open,
2447 .read = seq_read,
2448 .llseek = seq_lseek,
2449 .release = ftrace_avail_release,
2450};
2451
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002452static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002453 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002454 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002455 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002456 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002457 .release = ftrace_filter_release,
2458};
2459
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002460static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002461 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002462 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002463 .write = ftrace_notrace_write,
2464 .llseek = ftrace_regex_lseek,
2465 .release = ftrace_notrace_release,
2466};
2467
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002468#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2469
2470static DEFINE_MUTEX(graph_lock);
2471
2472int ftrace_graph_count;
2473unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2474
2475static void *
2476g_next(struct seq_file *m, void *v, loff_t *pos)
2477{
2478 unsigned long *array = m->private;
2479 int index = *pos;
2480
2481 (*pos)++;
2482
2483 if (index >= ftrace_graph_count)
2484 return NULL;
2485
2486 return &array[index];
2487}
2488
2489static void *g_start(struct seq_file *m, loff_t *pos)
2490{
2491 void *p = NULL;
2492
2493 mutex_lock(&graph_lock);
2494
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002495 /* Nothing, tell g_show to print all functions are enabled */
2496 if (!ftrace_graph_count && !*pos)
2497 return (void *)1;
2498
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002499 p = g_next(m, p, pos);
2500
2501 return p;
2502}
2503
2504static void g_stop(struct seq_file *m, void *p)
2505{
2506 mutex_unlock(&graph_lock);
2507}
2508
2509static int g_show(struct seq_file *m, void *v)
2510{
2511 unsigned long *ptr = v;
2512 char str[KSYM_SYMBOL_LEN];
2513
2514 if (!ptr)
2515 return 0;
2516
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002517 if (ptr == (unsigned long *)1) {
2518 seq_printf(m, "#### all functions enabled ####\n");
2519 return 0;
2520 }
2521
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002522 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2523
2524 seq_printf(m, "%s\n", str);
2525
2526 return 0;
2527}
2528
2529static struct seq_operations ftrace_graph_seq_ops = {
2530 .start = g_start,
2531 .next = g_next,
2532 .stop = g_stop,
2533 .show = g_show,
2534};
2535
2536static int
2537ftrace_graph_open(struct inode *inode, struct file *file)
2538{
2539 int ret = 0;
2540
2541 if (unlikely(ftrace_disabled))
2542 return -ENODEV;
2543
2544 mutex_lock(&graph_lock);
2545 if ((file->f_mode & FMODE_WRITE) &&
2546 !(file->f_flags & O_APPEND)) {
2547 ftrace_graph_count = 0;
2548 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2549 }
2550
2551 if (file->f_mode & FMODE_READ) {
2552 ret = seq_open(file, &ftrace_graph_seq_ops);
2553 if (!ret) {
2554 struct seq_file *m = file->private_data;
2555 m->private = ftrace_graph_funcs;
2556 }
2557 } else
2558 file->private_data = ftrace_graph_funcs;
2559 mutex_unlock(&graph_lock);
2560
2561 return ret;
2562}
2563
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002564static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002565ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002566{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002567 struct dyn_ftrace *rec;
2568 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002569 int search_len;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002570 int found = 0;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002571 int type, not;
2572 char *search;
2573 bool exists;
2574 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002575
2576 if (ftrace_disabled)
2577 return -ENODEV;
2578
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002579 /* decode regex */
2580 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2581 if (not)
2582 return -EINVAL;
2583
2584 search_len = strlen(search);
2585
Steven Rostedt52baf112009-02-14 01:15:39 -05002586 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002587 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002588
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002589 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2590 break;
2591
Steven Rostedt265c8312009-02-13 12:43:56 -05002592 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2593 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002594
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002595 if (ftrace_match_record(rec, search, search_len, type)) {
2596 /* ensure it is not already in the array */
2597 exists = false;
2598 for (i = 0; i < *idx; i++)
2599 if (array[i] == rec->ip) {
2600 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002601 break;
2602 }
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002603 if (!exists) {
2604 array[(*idx)++] = rec->ip;
2605 found = 1;
2606 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002607 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002608 } while_for_each_ftrace_rec();
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002609
Steven Rostedt52baf112009-02-14 01:15:39 -05002610 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002611
2612 return found ? 0 : -EINVAL;
2613}
2614
2615static ssize_t
2616ftrace_graph_write(struct file *file, const char __user *ubuf,
2617 size_t cnt, loff_t *ppos)
2618{
2619 unsigned char buffer[FTRACE_BUFF_MAX+1];
2620 unsigned long *array;
2621 size_t read = 0;
2622 ssize_t ret;
2623 int index = 0;
2624 char ch;
2625
2626 if (!cnt || cnt < 0)
2627 return 0;
2628
2629 mutex_lock(&graph_lock);
2630
2631 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2632 ret = -EBUSY;
2633 goto out;
2634 }
2635
2636 if (file->f_mode & FMODE_READ) {
2637 struct seq_file *m = file->private_data;
2638 array = m->private;
2639 } else
2640 array = file->private_data;
2641
2642 ret = get_user(ch, ubuf++);
2643 if (ret)
2644 goto out;
2645 read++;
2646 cnt--;
2647
2648 /* skip white space */
2649 while (cnt && isspace(ch)) {
2650 ret = get_user(ch, ubuf++);
2651 if (ret)
2652 goto out;
2653 read++;
2654 cnt--;
2655 }
2656
2657 if (isspace(ch)) {
2658 *ppos += read;
2659 ret = read;
2660 goto out;
2661 }
2662
2663 while (cnt && !isspace(ch)) {
2664 if (index < FTRACE_BUFF_MAX)
2665 buffer[index++] = ch;
2666 else {
2667 ret = -EINVAL;
2668 goto out;
2669 }
2670 ret = get_user(ch, ubuf++);
2671 if (ret)
2672 goto out;
2673 read++;
2674 cnt--;
2675 }
2676 buffer[index] = 0;
2677
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002678 /* we allow only one expression at a time */
2679 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002680 if (ret)
2681 goto out;
2682
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002683 file->f_pos += read;
2684
2685 ret = read;
2686 out:
2687 mutex_unlock(&graph_lock);
2688
2689 return ret;
2690}
2691
2692static const struct file_operations ftrace_graph_fops = {
2693 .open = ftrace_graph_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002694 .read = seq_read,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002695 .write = ftrace_graph_write,
2696};
2697#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2698
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002699static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002700{
Steven Rostedt5072c592008-05-12 21:20:43 +02002701
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002702 trace_create_file("available_filter_functions", 0444,
2703 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02002704
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002705 trace_create_file("failures", 0444,
2706 d_tracer, NULL, &ftrace_failures_fops);
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302707
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002708 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2709 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002710
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002711 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002712 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002713
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002714#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002715 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002716 NULL,
2717 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002718#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2719
Steven Rostedt5072c592008-05-12 21:20:43 +02002720 return 0;
2721}
2722
Steven Rostedt31e88902008-11-14 16:21:19 -08002723static int ftrace_convert_nops(struct module *mod,
2724 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002725 unsigned long *end)
2726{
2727 unsigned long *p;
2728 unsigned long addr;
2729 unsigned long flags;
2730
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002731 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002732 p = start;
2733 while (p < end) {
2734 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002735 /*
2736 * Some architecture linkers will pad between
2737 * the different mcount_loc sections of different
2738 * object files to satisfy alignments.
2739 * Skip any NULL pointers.
2740 */
2741 if (!addr)
2742 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002743 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002744 }
2745
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002746 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002747 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002748 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002749 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002750 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002751
2752 return 0;
2753}
2754
Steven Rostedt31e88902008-11-14 16:21:19 -08002755void ftrace_init_module(struct module *mod,
2756 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002757{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002758 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002759 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08002760 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002761}
2762
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002763extern unsigned long __start_mcount_loc[];
2764extern unsigned long __stop_mcount_loc[];
2765
2766void __init ftrace_init(void)
2767{
2768 unsigned long count, addr, flags;
2769 int ret;
2770
2771 /* Keep the ftrace pointer to the stub */
2772 addr = (unsigned long)ftrace_stub;
2773
2774 local_irq_save(flags);
2775 ftrace_dyn_arch_init(&addr);
2776 local_irq_restore(flags);
2777
2778 /* ftrace_dyn_arch_init places the return code in addr */
2779 if (addr)
2780 goto failed;
2781
2782 count = __stop_mcount_loc - __start_mcount_loc;
2783
2784 ret = ftrace_dyn_table_alloc(count);
2785 if (ret)
2786 goto failed;
2787
2788 last_ftrace_enabled = ftrace_enabled = 1;
2789
Steven Rostedt31e88902008-11-14 16:21:19 -08002790 ret = ftrace_convert_nops(NULL,
2791 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002792 __stop_mcount_loc);
2793
2794 return;
2795 failed:
2796 ftrace_disabled = 1;
2797}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002798
Steven Rostedt3d083392008-05-12 21:20:42 +02002799#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002800
2801static int __init ftrace_nodyn_init(void)
2802{
2803 ftrace_enabled = 1;
2804 return 0;
2805}
2806device_initcall(ftrace_nodyn_init);
2807
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002808static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2809static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002810/* Keep as macros so we do not need to define the commands */
2811# define ftrace_startup(command) do { } while (0)
2812# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002813# define ftrace_startup_sysctl() do { } while (0)
2814# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002815#endif /* CONFIG_DYNAMIC_FTRACE */
2816
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002817static ssize_t
2818ftrace_pid_read(struct file *file, char __user *ubuf,
2819 size_t cnt, loff_t *ppos)
2820{
2821 char buf[64];
2822 int r;
2823
Steven Rostedte32d8952008-12-04 00:26:41 -05002824 if (ftrace_pid_trace == ftrace_swapper_pid)
2825 r = sprintf(buf, "swapper tasks\n");
2826 else if (ftrace_pid_trace)
Lai Jiangshancc59c9e2009-03-24 11:03:01 +08002827 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002828 else
2829 r = sprintf(buf, "no pid\n");
2830
2831 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2832}
2833
Steven Rostedte32d8952008-12-04 00:26:41 -05002834static void clear_ftrace_swapper(void)
2835{
2836 struct task_struct *p;
2837 int cpu;
2838
2839 get_online_cpus();
2840 for_each_online_cpu(cpu) {
2841 p = idle_task(cpu);
2842 clear_tsk_trace_trace(p);
2843 }
2844 put_online_cpus();
2845}
2846
2847static void set_ftrace_swapper(void)
2848{
2849 struct task_struct *p;
2850 int cpu;
2851
2852 get_online_cpus();
2853 for_each_online_cpu(cpu) {
2854 p = idle_task(cpu);
2855 set_tsk_trace_trace(p);
2856 }
2857 put_online_cpus();
2858}
2859
2860static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002861{
2862 struct task_struct *p;
2863
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002864 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002865 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002866 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002867 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002868 rcu_read_unlock();
2869
Steven Rostedte32d8952008-12-04 00:26:41 -05002870 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002871}
2872
Steven Rostedte32d8952008-12-04 00:26:41 -05002873static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002874{
2875 struct task_struct *p;
2876
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002877 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002878 do_each_pid_task(pid, PIDTYPE_PID, p) {
2879 set_tsk_trace_trace(p);
2880 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002881 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002882}
2883
Steven Rostedte32d8952008-12-04 00:26:41 -05002884static void clear_ftrace_pid_task(struct pid **pid)
2885{
2886 if (*pid == ftrace_swapper_pid)
2887 clear_ftrace_swapper();
2888 else
2889 clear_ftrace_pid(*pid);
2890
2891 *pid = NULL;
2892}
2893
2894static void set_ftrace_pid_task(struct pid *pid)
2895{
2896 if (pid == ftrace_swapper_pid)
2897 set_ftrace_swapper();
2898 else
2899 set_ftrace_pid(pid);
2900}
2901
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002902static ssize_t
2903ftrace_pid_write(struct file *filp, const char __user *ubuf,
2904 size_t cnt, loff_t *ppos)
2905{
Steven Rostedt978f3a42008-12-04 00:26:40 -05002906 struct pid *pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002907 char buf[64];
2908 long val;
2909 int ret;
2910
2911 if (cnt >= sizeof(buf))
2912 return -EINVAL;
2913
2914 if (copy_from_user(&buf, ubuf, cnt))
2915 return -EFAULT;
2916
2917 buf[cnt] = 0;
2918
2919 ret = strict_strtol(buf, 10, &val);
2920 if (ret < 0)
2921 return ret;
2922
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002923 mutex_lock(&ftrace_lock);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002924 if (val < 0) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002925 /* disable pid tracing */
Steven Rostedt978f3a42008-12-04 00:26:40 -05002926 if (!ftrace_pid_trace)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002927 goto out;
Steven Rostedt978f3a42008-12-04 00:26:40 -05002928
2929 clear_ftrace_pid_task(&ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002930
2931 } else {
Steven Rostedte32d8952008-12-04 00:26:41 -05002932 /* swapper task is special */
2933 if (!val) {
2934 pid = ftrace_swapper_pid;
2935 if (pid == ftrace_pid_trace)
2936 goto out;
2937 } else {
2938 pid = find_get_pid(val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002939
Steven Rostedte32d8952008-12-04 00:26:41 -05002940 if (pid == ftrace_pid_trace) {
2941 put_pid(pid);
2942 goto out;
2943 }
Steven Rostedt978f3a42008-12-04 00:26:40 -05002944 }
2945
2946 if (ftrace_pid_trace)
2947 clear_ftrace_pid_task(&ftrace_pid_trace);
2948
2949 if (!pid)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002950 goto out;
2951
Steven Rostedt978f3a42008-12-04 00:26:40 -05002952 ftrace_pid_trace = pid;
Steven Rostedt0ef8cde2008-12-03 15:36:58 -05002953
Steven Rostedt978f3a42008-12-04 00:26:40 -05002954 set_ftrace_pid_task(ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002955 }
2956
2957 /* update the function call */
2958 ftrace_update_pid_func();
2959 ftrace_startup_enable(0);
2960
2961 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002962 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002963
2964 return cnt;
2965}
2966
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002967static const struct file_operations ftrace_pid_fops = {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002968 .read = ftrace_pid_read,
2969 .write = ftrace_pid_write,
2970};
2971
2972static __init int ftrace_init_debugfs(void)
2973{
2974 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002975
2976 d_tracer = tracing_init_dentry();
2977 if (!d_tracer)
2978 return 0;
2979
2980 ftrace_init_dyn_debugfs(d_tracer);
2981
Frederic Weisbecker5452af62009-03-27 00:25:38 +01002982 trace_create_file("set_ftrace_pid", 0644, d_tracer,
2983 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04002984
2985 ftrace_profile_debugfs(d_tracer);
2986
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002987 return 0;
2988}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002989fs_initcall(ftrace_init_debugfs);
2990
Steven Rostedt3d083392008-05-12 21:20:42 +02002991/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002992 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002993 *
2994 * This function should be used by panic code. It stops ftrace
2995 * but in a not so nice way. If you need to simply kill ftrace
2996 * from a non-atomic section, use ftrace_kill.
2997 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002998void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002999{
3000 ftrace_disabled = 1;
3001 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04003002 clear_ftrace_function();
3003}
3004
3005/**
Steven Rostedt3d083392008-05-12 21:20:42 +02003006 * register_ftrace_function - register a function for profiling
3007 * @ops - ops structure that holds the function for profiling.
3008 *
3009 * Register a function to be called by all functions in the
3010 * kernel.
3011 *
3012 * Note: @ops->func and all the functions it calls must be labeled
3013 * with "notrace", otherwise it will go into a
3014 * recursive loop.
3015 */
3016int register_ftrace_function(struct ftrace_ops *ops)
3017{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003018 int ret;
3019
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003020 if (unlikely(ftrace_disabled))
3021 return -1;
3022
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003023 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003024
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003025 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003026 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003027
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003028 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003029 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02003030}
3031
3032/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01003033 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02003034 * @ops - ops structure that holds the function to unregister
3035 *
3036 * Unregister a function that was added to be called by ftrace profiling.
3037 */
3038int unregister_ftrace_function(struct ftrace_ops *ops)
3039{
3040 int ret;
3041
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003042 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003043 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003044 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003045 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003046
3047 return ret;
3048}
3049
Ingo Molnare309b412008-05-12 21:20:51 +02003050int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003051ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02003052 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003053 loff_t *ppos)
3054{
3055 int ret;
3056
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003057 if (unlikely(ftrace_disabled))
3058 return -ENODEV;
3059
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003060 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003061
Steven Rostedt5072c592008-05-12 21:20:43 +02003062 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003063
3064 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
3065 goto out;
3066
3067 last_ftrace_enabled = ftrace_enabled;
3068
3069 if (ftrace_enabled) {
3070
3071 ftrace_startup_sysctl();
3072
3073 /* we are starting ftrace again */
3074 if (ftrace_list != &ftrace_list_end) {
3075 if (ftrace_list->next == &ftrace_list_end)
3076 ftrace_trace_function = ftrace_list->func;
3077 else
3078 ftrace_trace_function = ftrace_list_func;
3079 }
3080
3081 } else {
3082 /* stopping ftrace calls (just send to ftrace_stub) */
3083 ftrace_trace_function = ftrace_stub;
3084
3085 ftrace_shutdown_sysctl();
3086 }
3087
3088 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003089 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003090 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003091}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003092
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003093#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003094
Steven Rostedt597af812009-04-03 15:24:12 -04003095static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003096static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003097
Steven Rostedte49dc192008-12-02 23:50:05 -05003098int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3099{
3100 return 0;
3101}
3102
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003103/* The callbacks that hook a function */
3104trace_func_graph_ret_t ftrace_graph_return =
3105 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003106trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003107
3108/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3109static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3110{
3111 int i;
3112 int ret = 0;
3113 unsigned long flags;
3114 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3115 struct task_struct *g, *t;
3116
3117 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3118 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3119 * sizeof(struct ftrace_ret_stack),
3120 GFP_KERNEL);
3121 if (!ret_stack_list[i]) {
3122 start = 0;
3123 end = i;
3124 ret = -ENOMEM;
3125 goto free;
3126 }
3127 }
3128
3129 read_lock_irqsave(&tasklist_lock, flags);
3130 do_each_thread(g, t) {
3131 if (start == end) {
3132 ret = -EAGAIN;
3133 goto unlock;
3134 }
3135
3136 if (t->ret_stack == NULL) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003137 t->curr_ret_stack = -1;
Frederic Weisbecker48d68b22008-12-02 00:20:39 +01003138 /* Make sure IRQs see the -1 first: */
3139 barrier();
3140 t->ret_stack = ret_stack_list[start++];
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003141 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003142 atomic_set(&t->trace_overrun, 0);
3143 }
3144 } while_each_thread(g, t);
3145
3146unlock:
3147 read_unlock_irqrestore(&tasklist_lock, flags);
3148free:
3149 for (i = start; i < end; i++)
3150 kfree(ret_stack_list[i]);
3151 return ret;
3152}
3153
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003154static void
3155ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3156 struct task_struct *next)
3157{
3158 unsigned long long timestamp;
3159 int index;
3160
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003161 /*
3162 * Does the user want to count the time a function was asleep.
3163 * If so, do not update the time stamps.
3164 */
3165 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3166 return;
3167
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003168 timestamp = trace_clock_local();
3169
3170 prev->ftrace_timestamp = timestamp;
3171
3172 /* only process tasks that we timestamped */
3173 if (!next->ftrace_timestamp)
3174 return;
3175
3176 /*
3177 * Update all the counters in next to make up for the
3178 * time next was sleeping.
3179 */
3180 timestamp -= next->ftrace_timestamp;
3181
3182 for (index = next->curr_ret_stack; index >= 0; index--)
3183 next->ret_stack[index].calltime += timestamp;
3184}
3185
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003186/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003187static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003188{
3189 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003190 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003191
3192 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3193 sizeof(struct ftrace_ret_stack *),
3194 GFP_KERNEL);
3195
3196 if (!ret_stack_list)
3197 return -ENOMEM;
3198
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003199 /* The cpu_boot init_task->ret_stack will never be freed */
3200 for_each_online_cpu(cpu)
3201 ftrace_graph_init_task(idle_task(cpu));
3202
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003203 do {
3204 ret = alloc_retstack_tasklist(ret_stack_list);
3205 } while (ret == -EAGAIN);
3206
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003207 if (!ret) {
3208 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3209 if (ret)
3210 pr_info("ftrace_graph: Couldn't activate tracepoint"
3211 " probe to kernel_sched_switch\n");
3212 }
3213
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003214 kfree(ret_stack_list);
3215 return ret;
3216}
3217
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003218/*
3219 * Hibernation protection.
3220 * The state of the current task is too much unstable during
3221 * suspend/restore to disk. We want to protect against that.
3222 */
3223static int
3224ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3225 void *unused)
3226{
3227 switch (state) {
3228 case PM_HIBERNATION_PREPARE:
3229 pause_graph_tracing();
3230 break;
3231
3232 case PM_POST_HIBERNATION:
3233 unpause_graph_tracing();
3234 break;
3235 }
3236 return NOTIFY_DONE;
3237}
3238
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003239int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3240 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003241{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003242 int ret = 0;
3243
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003244 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003245
Steven Rostedt05ce5812009-03-24 00:18:31 -04003246 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04003247 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04003248 ret = -EBUSY;
3249 goto out;
3250 }
3251
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003252 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3253 register_pm_notifier(&ftrace_suspend_notifier);
3254
Steven Rostedt597af812009-04-03 15:24:12 -04003255 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003256 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003257 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04003258 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003259 goto out;
3260 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003261
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003262 ftrace_graph_return = retfunc;
3263 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003264
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003265 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003266
3267out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003268 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003269 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003270}
3271
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003272void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003273{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003274 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003275
Steven Rostedt597af812009-04-03 15:24:12 -04003276 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003277 goto out;
3278
Steven Rostedt597af812009-04-03 15:24:12 -04003279 ftrace_graph_active--;
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003280 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003281 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003282 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003283 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003284 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003285
Steven Rostedt2aad1b72009-03-30 11:11:28 -04003286 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003287 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003288}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003289
3290/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003291void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003292{
Steven Rostedt597af812009-04-03 15:24:12 -04003293 if (ftrace_graph_active) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003294 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3295 * sizeof(struct ftrace_ret_stack),
3296 GFP_KERNEL);
3297 if (!t->ret_stack)
3298 return;
3299 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003300 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003301 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003302 t->ftrace_timestamp = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003303 } else
3304 t->ret_stack = NULL;
3305}
3306
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003307void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003308{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003309 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3310
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003311 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003312 /* NULL must become visible to IRQs before we free it: */
3313 barrier();
3314
3315 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003316}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003317
3318void ftrace_graph_stop(void)
3319{
3320 ftrace_stop();
3321}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003322#endif
3323