blob: a9ccd71fc9226434cb45c4f0d3a5b1d5c6f276e9 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Abhishek Sagarf22f9a82008-06-21 23:50:29 +053025#include <linux/kprobes.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010026#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020027#include <linux/sysctl.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020028#include <linux/ctype.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020029#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050030#include <linux/hash.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020031
Steven Rostedt8aef2d22009-03-24 01:10:15 -040032#include <trace/sched.h>
33
Abhishek Sagar395a59d2008-06-21 23:47:27 +053034#include <asm/ftrace.h>
35
Steven Rostedt0706f1c2009-03-23 23:12:58 -040036#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040037#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020038
Steven Rostedt69128962008-10-23 09:33:03 -040039#define FTRACE_WARN_ON(cond) \
40 do { \
41 if (WARN_ON(cond)) \
42 ftrace_kill(); \
43 } while (0)
44
45#define FTRACE_WARN_ON_ONCE(cond) \
46 do { \
47 if (WARN_ON_ONCE(cond)) \
48 ftrace_kill(); \
49 } while (0)
50
Steven Rostedt8fc0c702009-02-16 15:28:00 -050051/* hash bits for specific function selection */
52#define FTRACE_HASH_BITS 7
53#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
54
Steven Rostedt4eebcc82008-05-12 21:20:48 +020055/* ftrace_enabled is a method to turn ftrace on or off */
56int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020057static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020058
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050059/* Quick disabling of function tracer. */
60int function_trace_stop;
61
Steven Rostedt4eebcc82008-05-12 21:20:48 +020062/*
63 * ftrace_disabled is set when an anomaly is discovered.
64 * ftrace_disabled is much stronger than ftrace_enabled.
65 */
66static int ftrace_disabled __read_mostly;
67
Steven Rostedt52baf112009-02-14 01:15:39 -050068static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020069
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020070static struct ftrace_ops ftrace_list_end __read_mostly =
71{
72 .func = ftrace_stub,
73};
74
75static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
76ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050077ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050078ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020079
Ingo Molnarf2252932008-05-22 10:37:48 +020080static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020081{
82 struct ftrace_ops *op = ftrace_list;
83
84 /* in case someone actually ports this to alpha! */
85 read_barrier_depends();
86
87 while (op != &ftrace_list_end) {
88 /* silly alpha */
89 read_barrier_depends();
90 op->func(ip, parent_ip);
91 op = op->next;
92 };
93}
94
Steven Rostedtdf4fc312008-11-26 00:16:23 -050095static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
96{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -050097 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -050098 return;
99
100 ftrace_pid_function(ip, parent_ip);
101}
102
103static void set_ftrace_pid_function(ftrace_func_t func)
104{
105 /* do not set ftrace_pid_function to itself! */
106 if (func != ftrace_pid_func)
107 ftrace_pid_function = func;
108}
109
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200110/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200111 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200112 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200113 * This NULLs the ftrace function and in essence stops
114 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200116void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117{
Steven Rostedt3d083392008-05-12 21:20:42 +0200118 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500119 __ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500120 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200121}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500123#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
124/*
125 * For those archs that do not test ftrace_trace_stop in their
126 * mcount call site, we need to do it from C.
127 */
128static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
129{
130 if (function_trace_stop)
131 return;
132
133 __ftrace_trace_function(ip, parent_ip);
134}
135#endif
136
Ingo Molnare309b412008-05-12 21:20:51 +0200137static int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200138{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200139 ops->next = ftrace_list;
140 /*
141 * We are entering ops into the ftrace_list but another
142 * CPU might be walking that list. We need to make sure
143 * the ops->next pointer is valid before another CPU sees
144 * the ops pointer included into the ftrace_list.
145 */
146 smp_wmb();
147 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +0200148
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200149 if (ftrace_enabled) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500150 ftrace_func_t func;
151
152 if (ops->next == &ftrace_list_end)
153 func = ops->func;
154 else
155 func = ftrace_list_func;
156
Steven Rostedt978f3a42008-12-04 00:26:40 -0500157 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158 set_ftrace_pid_function(func);
159 func = ftrace_pid_func;
160 }
161
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200162 /*
163 * For one func, simply call it directly.
164 * For more than one func, call the chain.
165 */
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500166#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500167 ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500168#else
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500169 __ftrace_trace_function = func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500170 ftrace_trace_function = ftrace_test_stop_func;
171#endif
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200172 }
Steven Rostedt3d083392008-05-12 21:20:42 +0200173
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200174 return 0;
175}
176
Ingo Molnare309b412008-05-12 21:20:51 +0200177static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200178{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200179 struct ftrace_ops **p;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200180
181 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200182 * If we are removing the last function, then simply point
183 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200184 */
185 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
186 ftrace_trace_function = ftrace_stub;
187 ftrace_list = &ftrace_list_end;
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500188 return 0;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189 }
190
191 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
192 if (*p == ops)
193 break;
194
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500195 if (*p != ops)
196 return -1;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197
198 *p = (*p)->next;
199
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200200 if (ftrace_enabled) {
201 /* If we only have one func left, then call that directly */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500202 if (ftrace_list->next == &ftrace_list_end) {
203 ftrace_func_t func = ftrace_list->func;
204
Steven Rostedt978f3a42008-12-04 00:26:40 -0500205 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500206 set_ftrace_pid_function(func);
207 func = ftrace_pid_func;
208 }
209#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
210 ftrace_trace_function = func;
211#else
212 __ftrace_trace_function = func;
213#endif
214 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200215 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200216
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500217 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200218}
219
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500220static void ftrace_update_pid_func(void)
221{
222 ftrace_func_t func;
223
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500224 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900225 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500226
227 func = ftrace_trace_function;
228
Steven Rostedt978f3a42008-12-04 00:26:40 -0500229 if (ftrace_pid_trace) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500230 set_ftrace_pid_function(func);
231 func = ftrace_pid_func;
232 } else {
Liming Wang66eafeb2008-12-02 10:33:08 +0800233 if (func == ftrace_pid_func)
234 func = ftrace_pid_function;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500235 }
236
237#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
238 ftrace_trace_function = func;
239#else
240 __ftrace_trace_function = func;
241#endif
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500242}
243
Steven Rostedt493762f2009-03-23 17:12:36 -0400244#ifdef CONFIG_FUNCTION_PROFILER
245struct ftrace_profile {
246 struct hlist_node node;
247 unsigned long ip;
248 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400249#ifdef CONFIG_FUNCTION_GRAPH_TRACER
250 unsigned long long time;
251#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400252};
253
254struct ftrace_profile_page {
255 struct ftrace_profile_page *next;
256 unsigned long index;
257 struct ftrace_profile records[];
258};
259
260#define PROFILE_RECORDS_SIZE \
261 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
262
263#define PROFILES_PER_PAGE \
264 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
265
266/* TODO: make these percpu, to prevent cache line bouncing */
267static struct ftrace_profile_page *profile_pages_start;
268static struct ftrace_profile_page *profile_pages;
269
270static struct hlist_head *ftrace_profile_hash;
271static int ftrace_profile_bits;
272static int ftrace_profile_enabled;
273static DEFINE_MUTEX(ftrace_profile_lock);
274
275static DEFINE_PER_CPU(atomic_t, ftrace_profile_disable);
276
277#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
278
279static raw_spinlock_t ftrace_profile_rec_lock =
280 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
281
282static void *
283function_stat_next(void *v, int idx)
284{
285 struct ftrace_profile *rec = v;
286 struct ftrace_profile_page *pg;
287
288 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
289
290 again:
291 rec++;
292 if ((void *)rec >= (void *)&pg->records[pg->index]) {
293 pg = pg->next;
294 if (!pg)
295 return NULL;
296 rec = &pg->records[0];
297 if (!rec->counter)
298 goto again;
299 }
300
301 return rec;
302}
303
304static void *function_stat_start(struct tracer_stat *trace)
305{
306 return function_stat_next(&profile_pages_start->records[0], 0);
307}
308
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400309#ifdef CONFIG_FUNCTION_GRAPH_TRACER
310/* function graph compares on total time */
311static int function_stat_cmp(void *p1, void *p2)
312{
313 struct ftrace_profile *a = p1;
314 struct ftrace_profile *b = p2;
315
316 if (a->time < b->time)
317 return -1;
318 if (a->time > b->time)
319 return 1;
320 else
321 return 0;
322}
323#else
324/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400325static int function_stat_cmp(void *p1, void *p2)
326{
327 struct ftrace_profile *a = p1;
328 struct ftrace_profile *b = p2;
329
330 if (a->counter < b->counter)
331 return -1;
332 if (a->counter > b->counter)
333 return 1;
334 else
335 return 0;
336}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400337#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400338
339static int function_stat_headers(struct seq_file *m)
340{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400341#ifdef CONFIG_FUNCTION_GRAPH_TRACER
342 seq_printf(m, " Function Hit Time\n"
343 " -------- --- ----\n");
344#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400345 seq_printf(m, " Function Hit\n"
346 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400347#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400348 return 0;
349}
350
351static int function_stat_show(struct seq_file *m, void *v)
352{
353 struct ftrace_profile *rec = v;
354 char str[KSYM_SYMBOL_LEN];
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400355#ifdef CONFIG_FUNCTION_GRAPH_TRACER
356 static struct trace_seq s;
357 static DEFINE_MUTEX(mutex);
358
359 mutex_lock(&mutex);
360 trace_seq_init(&s);
361 trace_print_graph_duration(rec->time, &s);
362#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400363
364 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400365 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400366
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400367#ifdef CONFIG_FUNCTION_GRAPH_TRACER
368 seq_printf(m, " ");
369 trace_print_seq(m, &s);
370 mutex_unlock(&mutex);
371#endif
372 seq_putc(m, '\n');
373
Steven Rostedt493762f2009-03-23 17:12:36 -0400374 return 0;
375}
376
377static struct tracer_stat function_stats = {
378 .name = "functions",
379 .stat_start = function_stat_start,
380 .stat_next = function_stat_next,
381 .stat_cmp = function_stat_cmp,
382 .stat_headers = function_stat_headers,
383 .stat_show = function_stat_show
384};
385
386static void ftrace_profile_reset(void)
387{
388 struct ftrace_profile_page *pg;
389
390 pg = profile_pages = profile_pages_start;
391
392 while (pg) {
393 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
394 pg->index = 0;
395 pg = pg->next;
396 }
397
398 memset(ftrace_profile_hash, 0,
399 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
400}
401
402int ftrace_profile_pages_init(void)
403{
404 struct ftrace_profile_page *pg;
405 int i;
406
407 /* If we already allocated, do nothing */
408 if (profile_pages)
409 return 0;
410
411 profile_pages = (void *)get_zeroed_page(GFP_KERNEL);
412 if (!profile_pages)
413 return -ENOMEM;
414
415 pg = profile_pages_start = profile_pages;
416
417 /* allocate 10 more pages to start */
418 for (i = 0; i < 10; i++) {
419 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
420 /*
421 * We only care about allocating profile_pages, if
422 * we failed to allocate here, hopefully we will allocate
423 * later.
424 */
425 if (!pg->next)
426 break;
427 pg = pg->next;
428 }
429
430 return 0;
431}
432
433static int ftrace_profile_init(void)
434{
435 int size;
436
437 if (ftrace_profile_hash) {
438 /* If the profile is already created, simply reset it */
439 ftrace_profile_reset();
440 return 0;
441 }
442
443 /*
444 * We are profiling all functions, but usually only a few thousand
445 * functions are hit. We'll make a hash of 1024 items.
446 */
447 size = FTRACE_PROFILE_HASH_SIZE;
448
449 ftrace_profile_hash =
450 kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
451
452 if (!ftrace_profile_hash)
453 return -ENOMEM;
454
455 size--;
456
457 for (; size; size >>= 1)
458 ftrace_profile_bits++;
459
460 /* Preallocate a few pages */
461 if (ftrace_profile_pages_init() < 0) {
462 kfree(ftrace_profile_hash);
463 ftrace_profile_hash = NULL;
464 return -ENOMEM;
465 }
466
467 return 0;
468}
469
470/* interrupts must be disabled */
471static struct ftrace_profile *ftrace_find_profiled_func(unsigned long ip)
472{
473 struct ftrace_profile *rec;
474 struct hlist_head *hhd;
475 struct hlist_node *n;
476 unsigned long key;
477
478 key = hash_long(ip, ftrace_profile_bits);
479 hhd = &ftrace_profile_hash[key];
480
481 if (hlist_empty(hhd))
482 return NULL;
483
484 hlist_for_each_entry_rcu(rec, n, hhd, node) {
485 if (rec->ip == ip)
486 return rec;
487 }
488
489 return NULL;
490}
491
492static void ftrace_add_profile(struct ftrace_profile *rec)
493{
494 unsigned long key;
495
496 key = hash_long(rec->ip, ftrace_profile_bits);
497 hlist_add_head_rcu(&rec->node, &ftrace_profile_hash[key]);
498}
499
500/* Interrupts must be disabled calling this */
501static struct ftrace_profile *
502ftrace_profile_alloc(unsigned long ip, bool alloc_safe)
503{
504 struct ftrace_profile *rec = NULL;
505
506 /* prevent recursion */
507 if (atomic_inc_return(&__get_cpu_var(ftrace_profile_disable)) != 1)
508 goto out;
509
510 __raw_spin_lock(&ftrace_profile_rec_lock);
511
512 /* Try to always keep another page available */
513 if (!profile_pages->next && alloc_safe)
514 profile_pages->next = (void *)get_zeroed_page(GFP_ATOMIC);
515
516 /*
517 * Try to find the function again since another
518 * task on another CPU could have added it
519 */
520 rec = ftrace_find_profiled_func(ip);
521 if (rec)
522 goto out_unlock;
523
524 if (profile_pages->index == PROFILES_PER_PAGE) {
525 if (!profile_pages->next)
526 goto out_unlock;
527 profile_pages = profile_pages->next;
528 }
529
530 rec = &profile_pages->records[profile_pages->index++];
531 rec->ip = ip;
532 ftrace_add_profile(rec);
533
534 out_unlock:
535 __raw_spin_unlock(&ftrace_profile_rec_lock);
536 out:
537 atomic_dec(&__get_cpu_var(ftrace_profile_disable));
538
539 return rec;
540}
541
542/*
543 * If we are not in an interrupt, or softirq and
544 * and interrupts are disabled and preemption is not enabled
545 * (not in a spinlock) then it should be safe to allocate memory.
546 */
547static bool ftrace_safe_to_allocate(void)
548{
549 return !in_interrupt() && irqs_disabled() && !preempt_count();
550}
551
552static void
553function_profile_call(unsigned long ip, unsigned long parent_ip)
554{
555 struct ftrace_profile *rec;
556 unsigned long flags;
557 bool alloc_safe;
558
559 if (!ftrace_profile_enabled)
560 return;
561
562 alloc_safe = ftrace_safe_to_allocate();
563
564 local_irq_save(flags);
565 rec = ftrace_find_profiled_func(ip);
566 if (!rec) {
567 rec = ftrace_profile_alloc(ip, alloc_safe);
568 if (!rec)
569 goto out;
570 }
571
572 rec->counter++;
573 out:
574 local_irq_restore(flags);
575}
576
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400577#ifdef CONFIG_FUNCTION_GRAPH_TRACER
578static int profile_graph_entry(struct ftrace_graph_ent *trace)
579{
580 function_profile_call(trace->func, 0);
581 return 1;
582}
583
584static void profile_graph_return(struct ftrace_graph_ret *trace)
585{
586 unsigned long flags;
587 struct ftrace_profile *rec;
588
589 local_irq_save(flags);
590 rec = ftrace_find_profiled_func(trace->func);
591 if (rec)
592 rec->time += trace->rettime - trace->calltime;
593 local_irq_restore(flags);
594}
595
596static int register_ftrace_profiler(void)
597{
598 return register_ftrace_graph(&profile_graph_return,
599 &profile_graph_entry);
600}
601
602static void unregister_ftrace_profiler(void)
603{
604 unregister_ftrace_graph();
605}
606#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400607static struct ftrace_ops ftrace_profile_ops __read_mostly =
608{
609 .func = function_profile_call,
610};
611
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400612static int register_ftrace_profiler(void)
613{
614 return register_ftrace_function(&ftrace_profile_ops);
615}
616
617static void unregister_ftrace_profiler(void)
618{
619 unregister_ftrace_function(&ftrace_profile_ops);
620}
621#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
622
Steven Rostedt493762f2009-03-23 17:12:36 -0400623static ssize_t
624ftrace_profile_write(struct file *filp, const char __user *ubuf,
625 size_t cnt, loff_t *ppos)
626{
627 unsigned long val;
628 char buf[64];
629 int ret;
630
631 if (cnt >= sizeof(buf))
632 return -EINVAL;
633
634 if (copy_from_user(&buf, ubuf, cnt))
635 return -EFAULT;
636
637 buf[cnt] = 0;
638
639 ret = strict_strtoul(buf, 10, &val);
640 if (ret < 0)
641 return ret;
642
643 val = !!val;
644
645 mutex_lock(&ftrace_profile_lock);
646 if (ftrace_profile_enabled ^ val) {
647 if (val) {
648 ret = ftrace_profile_init();
649 if (ret < 0) {
650 cnt = ret;
651 goto out;
652 }
653
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400654 ret = register_ftrace_profiler();
655 if (ret < 0) {
656 cnt = ret;
657 goto out;
658 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400659 ftrace_profile_enabled = 1;
660 } else {
661 ftrace_profile_enabled = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400662 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 }
664 }
665 out:
666 mutex_unlock(&ftrace_profile_lock);
667
668 filp->f_pos += cnt;
669
670 return cnt;
671}
672
673static ssize_t
674ftrace_profile_read(struct file *filp, char __user *ubuf,
675 size_t cnt, loff_t *ppos)
676{
677 char buf[64];
678 int r;
679
680 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
681 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
682}
683
684static const struct file_operations ftrace_profile_fops = {
685 .open = tracing_open_generic,
686 .read = ftrace_profile_read,
687 .write = ftrace_profile_write,
688};
689
690static void ftrace_profile_debugfs(struct dentry *d_tracer)
691{
692 struct dentry *entry;
693 int ret;
694
695 ret = register_stat_tracer(&function_stats);
696 if (ret) {
697 pr_warning("Warning: could not register "
698 "function stats\n");
699 return;
700 }
701
702 entry = debugfs_create_file("function_profile_enabled", 0644,
703 d_tracer, NULL, &ftrace_profile_fops);
704 if (!entry)
705 pr_warning("Could not create debugfs "
706 "'function_profile_enabled' entry\n");
707}
708
709#else /* CONFIG_FUNCTION_PROFILER */
710static void ftrace_profile_debugfs(struct dentry *d_tracer)
711{
712}
713#endif /* CONFIG_FUNCTION_PROFILER */
714
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100715/* set when tracing only a pid */
716struct pid *ftrace_pid_trace;
717static struct pid * const ftrace_swapper_pid = &init_struct_pid;
718
Steven Rostedt3d083392008-05-12 21:20:42 +0200719#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100720
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400721#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -0400722# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -0400723#endif
724
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500725static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
726
Steven Rostedtb6887d72009-02-17 12:32:04 -0500727struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500728 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -0500729 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -0500730 unsigned long flags;
731 unsigned long ip;
732 void *data;
733 struct rcu_head rcu;
734};
735
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200736enum {
737 FTRACE_ENABLE_CALLS = (1 << 0),
738 FTRACE_DISABLE_CALLS = (1 << 1),
739 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
740 FTRACE_ENABLE_MCOUNT = (1 << 3),
741 FTRACE_DISABLE_MCOUNT = (1 << 4),
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500742 FTRACE_START_FUNC_RET = (1 << 5),
743 FTRACE_STOP_FUNC_RET = (1 << 6),
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200744};
745
Steven Rostedt5072c592008-05-12 21:20:43 +0200746static int ftrace_filtered;
747
Lai Jiangshane94142a2009-03-13 17:51:27 +0800748static struct dyn_ftrace *ftrace_new_addrs;
Steven Rostedt3d083392008-05-12 21:20:42 +0200749
Steven Rostedt41c52c02008-05-22 11:46:33 -0400750static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200751
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200752struct ftrace_page {
753 struct ftrace_page *next;
Steven Rostedt431aa3f2009-01-06 12:43:01 -0500754 int index;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200755 struct dyn_ftrace records[];
David Milleraa5e5ce2008-05-13 22:06:56 -0700756};
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200757
758#define ENTRIES_PER_PAGE \
759 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
760
761/* estimate from running different kernels */
762#define NR_TO_INIT 10000
763
764static struct ftrace_page *ftrace_pages_start;
765static struct ftrace_page *ftrace_pages;
766
Steven Rostedt37ad5082008-05-12 21:20:48 +0200767static struct dyn_ftrace *ftrace_free_records;
768
Steven Rostedt265c8312009-02-13 12:43:56 -0500769/*
770 * This is a double for. Do not use 'break' to break out of the loop,
771 * you must use a goto.
772 */
773#define do_for_each_ftrace_rec(pg, rec) \
774 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
775 int _____i; \
776 for (_____i = 0; _____i < pg->index; _____i++) { \
777 rec = &pg->records[_____i];
778
779#define while_for_each_ftrace_rec() \
780 } \
781 }
Abhishek Sagarecea6562008-06-21 23:47:53 +0530782
783#ifdef CONFIG_KPROBES
Ingo Molnarf17845e2008-10-24 12:47:10 +0200784
785static int frozen_record_count;
786
Abhishek Sagarecea6562008-06-21 23:47:53 +0530787static inline void freeze_record(struct dyn_ftrace *rec)
788{
789 if (!(rec->flags & FTRACE_FL_FROZEN)) {
790 rec->flags |= FTRACE_FL_FROZEN;
791 frozen_record_count++;
792 }
793}
794
795static inline void unfreeze_record(struct dyn_ftrace *rec)
796{
797 if (rec->flags & FTRACE_FL_FROZEN) {
798 rec->flags &= ~FTRACE_FL_FROZEN;
799 frozen_record_count--;
800 }
801}
802
803static inline int record_frozen(struct dyn_ftrace *rec)
804{
805 return rec->flags & FTRACE_FL_FROZEN;
806}
807#else
808# define freeze_record(rec) ({ 0; })
809# define unfreeze_record(rec) ({ 0; })
810# define record_frozen(rec) ({ 0; })
811#endif /* CONFIG_KPROBES */
812
Ingo Molnare309b412008-05-12 21:20:51 +0200813static void ftrace_free_rec(struct dyn_ftrace *rec)
Steven Rostedt37ad5082008-05-12 21:20:48 +0200814{
Lai Jiangshanee000b72009-03-24 13:38:06 +0800815 rec->freelist = ftrace_free_records;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200816 ftrace_free_records = rec;
817 rec->flags |= FTRACE_FL_FREE;
818}
819
Steven Rostedtfed19392008-08-14 22:47:19 -0400820void ftrace_release(void *start, unsigned long size)
821{
822 struct dyn_ftrace *rec;
823 struct ftrace_page *pg;
824 unsigned long s = (unsigned long)start;
825 unsigned long e = s + size;
Steven Rostedtfed19392008-08-14 22:47:19 -0400826
Steven Rostedt00fd61a2008-08-15 21:40:04 -0400827 if (ftrace_disabled || !start)
Steven Rostedtfed19392008-08-14 22:47:19 -0400828 return;
829
Steven Rostedt52baf112009-02-14 01:15:39 -0500830 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -0500831 do_for_each_ftrace_rec(pg, rec) {
Zhaoleib00f0b62009-03-13 17:14:01 +0800832 if ((rec->ip >= s) && (rec->ip < e) &&
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 !(rec->flags & FTRACE_FL_FREE))
Steven Rostedt265c8312009-02-13 12:43:56 -0500834 ftrace_free_rec(rec);
835 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -0500836 mutex_unlock(&ftrace_lock);
Steven Rostedtfed19392008-08-14 22:47:19 -0400837}
838
Ingo Molnare309b412008-05-12 21:20:51 +0200839static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200840{
Steven Rostedt37ad5082008-05-12 21:20:48 +0200841 struct dyn_ftrace *rec;
842
843 /* First check for freed records */
844 if (ftrace_free_records) {
845 rec = ftrace_free_records;
846
Steven Rostedt37ad5082008-05-12 21:20:48 +0200847 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
Steven Rostedt69128962008-10-23 09:33:03 -0400848 FTRACE_WARN_ON_ONCE(1);
Steven Rostedt37ad5082008-05-12 21:20:48 +0200849 ftrace_free_records = NULL;
850 return NULL;
851 }
852
Lai Jiangshanee000b72009-03-24 13:38:06 +0800853 ftrace_free_records = rec->freelist;
Steven Rostedt37ad5082008-05-12 21:20:48 +0200854 memset(rec, 0, sizeof(*rec));
855 return rec;
856 }
857
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200858 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400859 if (!ftrace_pages->next) {
860 /* allocate another page */
861 ftrace_pages->next =
862 (void *)get_zeroed_page(GFP_KERNEL);
863 if (!ftrace_pages->next)
864 return NULL;
865 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200866 ftrace_pages = ftrace_pages->next;
867 }
868
869 return &ftrace_pages->records[ftrace_pages->index++];
870}
871
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400872static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200873ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200874{
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400875 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200876
Steven Rostedtf3c7ac42008-11-14 16:21:19 -0800877 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400878 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200879
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400880 rec = ftrace_alloc_dyn_node(ip);
881 if (!rec)
882 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +0200883
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400884 rec->ip = ip;
Lai Jiangshanee000b72009-03-24 13:38:06 +0800885 rec->newlist = ftrace_new_addrs;
Lai Jiangshane94142a2009-03-13 17:51:27 +0800886 ftrace_new_addrs = rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200887
Steven Rostedt08f5ac902008-10-23 09:33:07 -0400888 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +0200889}
890
Steven Rostedt05736a42008-09-22 14:55:47 -0700891static void print_ip_ins(const char *fmt, unsigned char *p)
892{
893 int i;
894
895 printk(KERN_CONT "%s", fmt);
896
897 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
898 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
899}
900
Steven Rostedt31e88902008-11-14 16:21:19 -0800901static void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800902{
903 switch (failed) {
904 case -EFAULT:
905 FTRACE_WARN_ON_ONCE(1);
906 pr_info("ftrace faulted on modifying ");
907 print_ip_sym(ip);
908 break;
909 case -EINVAL:
910 FTRACE_WARN_ON_ONCE(1);
911 pr_info("ftrace failed to modify ");
912 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800913 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -0800914 printk(KERN_CONT "\n");
915 break;
916 case -EPERM:
917 FTRACE_WARN_ON_ONCE(1);
918 pr_info("ftrace faulted on writing ");
919 print_ip_sym(ip);
920 break;
921 default:
922 FTRACE_WARN_ON_ONCE(1);
923 pr_info("ftrace faulted on unknown error ");
924 print_ip_sym(ip);
925 }
926}
927
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200928
Abhishek Sagar492a7ea2008-05-25 00:10:04 +0530929static int
Steven Rostedt31e88902008-11-14 16:21:19 -0800930__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200931{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100932 unsigned long ftrace_addr;
Steven Rostedt6a24a242009-02-17 11:20:26 -0500933 unsigned long ip, fl;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100934
Shaohua Lif0001202009-01-09 11:29:42 +0800935 ftrace_addr = (unsigned long)FTRACE_ADDR;
Steven Rostedt5072c592008-05-12 21:20:43 +0200936
937 ip = rec->ip;
938
Steven Rostedt982c3502008-11-15 16:31:41 -0500939 /*
940 * If this record is not to be traced and
941 * it is not enabled then do nothing.
942 *
943 * If this record is not to be traced and
Wenji Huang57794a92009-02-06 17:33:27 +0800944 * it is enabled then disable it.
Steven Rostedt982c3502008-11-15 16:31:41 -0500945 *
946 */
947 if (rec->flags & FTRACE_FL_NOTRACE) {
948 if (rec->flags & FTRACE_FL_ENABLED)
949 rec->flags &= ~FTRACE_FL_ENABLED;
950 else
Steven Rostedt5072c592008-05-12 21:20:43 +0200951 return 0;
952
Steven Rostedt982c3502008-11-15 16:31:41 -0500953 } else if (ftrace_filtered && enable) {
Steven Rostedt5072c592008-05-12 21:20:43 +0200954 /*
Steven Rostedt982c3502008-11-15 16:31:41 -0500955 * Filtering is on:
Steven Rostedt5072c592008-05-12 21:20:43 +0200956 */
Steven Rostedt982c3502008-11-15 16:31:41 -0500957
958 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
959
960 /* Record is filtered and enabled, do nothing */
961 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
962 return 0;
963
Wenji Huang57794a92009-02-06 17:33:27 +0800964 /* Record is not filtered or enabled, do nothing */
Steven Rostedt982c3502008-11-15 16:31:41 -0500965 if (!fl)
966 return 0;
967
968 /* Record is not filtered but enabled, disable it */
969 if (fl == FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +0200970 rec->flags &= ~FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -0500971 else
972 /* Otherwise record is filtered but not enabled, enable it */
Steven Rostedt5072c592008-05-12 21:20:43 +0200973 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +0200974 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -0500975 /* Disable or not filtered */
Steven Rostedt5072c592008-05-12 21:20:43 +0200976
977 if (enable) {
Steven Rostedt982c3502008-11-15 16:31:41 -0500978 /* if record is enabled, do nothing */
Steven Rostedt41c52c02008-05-22 11:46:33 -0400979 if (rec->flags & FTRACE_FL_ENABLED)
Steven Rostedt5072c592008-05-12 21:20:43 +0200980 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -0500981
Steven Rostedt41c52c02008-05-22 11:46:33 -0400982 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt982c3502008-11-15 16:31:41 -0500983
Steven Rostedt5072c592008-05-12 21:20:43 +0200984 } else {
Steven Rostedt982c3502008-11-15 16:31:41 -0500985
Wenji Huang57794a92009-02-06 17:33:27 +0800986 /* if record is not enabled, do nothing */
Steven Rostedt5072c592008-05-12 21:20:43 +0200987 if (!(rec->flags & FTRACE_FL_ENABLED))
988 return 0;
Steven Rostedt982c3502008-11-15 16:31:41 -0500989
Steven Rostedt5072c592008-05-12 21:20:43 +0200990 rec->flags &= ~FTRACE_FL_ENABLED;
991 }
992 }
993
Steven Rostedt982c3502008-11-15 16:31:41 -0500994 if (rec->flags & FTRACE_FL_ENABLED)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100995 return ftrace_make_call(rec, ftrace_addr);
Steven Rostedt31e88902008-11-14 16:21:19 -0800996 else
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100997 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt5072c592008-05-12 21:20:43 +0200998}
999
1000static void ftrace_replace_code(int enable)
1001{
Steven Rostedt37ad5082008-05-12 21:20:48 +02001002 struct dyn_ftrace *rec;
1003 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001004 int failed;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001005
Steven Rostedt265c8312009-02-13 12:43:56 -05001006 do_for_each_ftrace_rec(pg, rec) {
1007 /*
Zhaoleifa9d13c2009-03-13 17:16:34 +08001008 * Skip over free records, records that have
1009 * failed and not converted.
Steven Rostedt265c8312009-02-13 12:43:56 -05001010 */
1011 if (rec->flags & FTRACE_FL_FREE ||
Zhaoleifa9d13c2009-03-13 17:16:34 +08001012 rec->flags & FTRACE_FL_FAILED ||
Frederic Weisbecker03303542009-03-16 22:41:00 +01001013 !(rec->flags & FTRACE_FL_CONVERTED))
Steven Rostedt265c8312009-02-13 12:43:56 -05001014 continue;
Steven Rostedt5072c592008-05-12 21:20:43 +02001015
Steven Rostedt265c8312009-02-13 12:43:56 -05001016 /* ignore updates to this record's mcount site */
1017 if (get_kprobe((void *)rec->ip)) {
1018 freeze_record(rec);
1019 continue;
1020 } else {
1021 unfreeze_record(rec);
Steven Rostedt5072c592008-05-12 21:20:43 +02001022 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001023
1024 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001025 if (failed) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001026 rec->flags |= FTRACE_FL_FAILED;
1027 if ((system_state == SYSTEM_BOOTING) ||
1028 !core_kernel_text(rec->ip)) {
1029 ftrace_free_rec(rec);
Steven Rostedt43772452009-02-19 13:41:27 -05001030 } else {
Steven Rostedt265c8312009-02-13 12:43:56 -05001031 ftrace_bug(failed, rec->ip);
Steven Rostedt43772452009-02-19 13:41:27 -05001032 /* Stop processing */
1033 return;
1034 }
Steven Rostedt265c8312009-02-13 12:43:56 -05001035 }
1036 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001037}
1038
Ingo Molnare309b412008-05-12 21:20:51 +02001039static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001040ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001041{
1042 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001043 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001044
1045 ip = rec->ip;
1046
Shaohua Li25aac9d2009-01-09 11:29:40 +08001047 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001048 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001049 ftrace_bug(ret, ip);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001050 rec->flags |= FTRACE_FL_FAILED;
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301051 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001052 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301053 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001054}
1055
Steven Rostedt000ab692009-02-17 13:35:06 -05001056/*
1057 * archs can override this function if they must do something
1058 * before the modifying code is performed.
1059 */
1060int __weak ftrace_arch_code_modify_prepare(void)
1061{
1062 return 0;
1063}
1064
1065/*
1066 * archs can override this function if they must do something
1067 * after the modifying code is performed.
1068 */
1069int __weak ftrace_arch_code_modify_post_process(void)
1070{
1071 return 0;
1072}
1073
Ingo Molnare309b412008-05-12 21:20:51 +02001074static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001075{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001076 int *command = data;
1077
Steven Rostedta3583242008-11-11 15:01:42 -05001078 if (*command & FTRACE_ENABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001079 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001080 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001081 ftrace_replace_code(0);
1082
1083 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1084 ftrace_update_ftrace_func(ftrace_trace_function);
1085
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001086 if (*command & FTRACE_START_FUNC_RET)
1087 ftrace_enable_ftrace_graph_caller();
1088 else if (*command & FTRACE_STOP_FUNC_RET)
1089 ftrace_disable_ftrace_graph_caller();
1090
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001091 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001092}
1093
Ingo Molnare309b412008-05-12 21:20:51 +02001094static void ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001095{
Steven Rostedt000ab692009-02-17 13:35:06 -05001096 int ret;
1097
1098 ret = ftrace_arch_code_modify_prepare();
1099 FTRACE_WARN_ON(ret);
1100 if (ret)
1101 return;
1102
Rusty Russell784e2d72008-07-28 12:16:31 -05001103 stop_machine(__ftrace_modify_code, &command, NULL);
Steven Rostedt000ab692009-02-17 13:35:06 -05001104
1105 ret = ftrace_arch_code_modify_post_process();
1106 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001107}
1108
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001109static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001110static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001111
1112static void ftrace_startup_enable(int command)
1113{
1114 if (saved_ftrace_func != ftrace_trace_function) {
1115 saved_ftrace_func = ftrace_trace_function;
1116 command |= FTRACE_UPDATE_TRACE_FUNC;
1117 }
1118
1119 if (!command || !ftrace_enabled)
1120 return;
1121
1122 ftrace_run_update_code(command);
1123}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001124
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001125static void ftrace_startup(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001126{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001127 if (unlikely(ftrace_disabled))
1128 return;
1129
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001130 ftrace_start_up++;
Steven Rostedt982c3502008-11-15 16:31:41 -05001131 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001132
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001133 ftrace_startup_enable(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001134}
1135
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001136static void ftrace_shutdown(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001137{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001138 if (unlikely(ftrace_disabled))
1139 return;
1140
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001141 ftrace_start_up--;
1142 if (!ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001143 command |= FTRACE_DISABLE_CALLS;
1144
1145 if (saved_ftrace_func != ftrace_trace_function) {
1146 saved_ftrace_func = ftrace_trace_function;
1147 command |= FTRACE_UPDATE_TRACE_FUNC;
1148 }
1149
1150 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001151 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001152
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001153 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001154}
1155
Ingo Molnare309b412008-05-12 21:20:51 +02001156static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001157{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001158 int command = FTRACE_ENABLE_MCOUNT;
1159
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001160 if (unlikely(ftrace_disabled))
1161 return;
1162
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001163 /* Force update next time */
1164 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001165 /* ftrace_start_up is true if we want ftrace running */
1166 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001167 command |= FTRACE_ENABLE_CALLS;
1168
1169 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001170}
1171
Ingo Molnare309b412008-05-12 21:20:51 +02001172static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001173{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001174 int command = FTRACE_DISABLE_MCOUNT;
1175
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001176 if (unlikely(ftrace_disabled))
1177 return;
1178
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001179 /* ftrace_start_up is true if ftrace is running */
1180 if (ftrace_start_up)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001181 command |= FTRACE_DISABLE_CALLS;
1182
1183 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02001184}
1185
Steven Rostedt3d083392008-05-12 21:20:42 +02001186static cycle_t ftrace_update_time;
1187static unsigned long ftrace_update_cnt;
1188unsigned long ftrace_update_tot_cnt;
1189
Steven Rostedt31e88902008-11-14 16:21:19 -08001190static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02001191{
Lai Jiangshane94142a2009-03-13 17:51:27 +08001192 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301193 cycle_t start, stop;
Steven Rostedt3d083392008-05-12 21:20:42 +02001194
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001195 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001196 ftrace_update_cnt = 0;
1197
Lai Jiangshane94142a2009-03-13 17:51:27 +08001198 while (ftrace_new_addrs) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05301199
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001200 /* If something went wrong, bail without enabling anything */
1201 if (unlikely(ftrace_disabled))
1202 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02001203
Lai Jiangshane94142a2009-03-13 17:51:27 +08001204 p = ftrace_new_addrs;
Lai Jiangshanee000b72009-03-24 13:38:06 +08001205 ftrace_new_addrs = p->newlist;
Lai Jiangshane94142a2009-03-13 17:51:27 +08001206 p->flags = 0L;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05301207
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001208 /* convert record (i.e, patch mcount-call with NOP) */
Steven Rostedt31e88902008-11-14 16:21:19 -08001209 if (ftrace_code_disable(mod, p)) {
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001210 p->flags |= FTRACE_FL_CONVERTED;
1211 ftrace_update_cnt++;
1212 } else
1213 ftrace_free_rec(p);
Steven Rostedt3d083392008-05-12 21:20:42 +02001214 }
1215
Ingo Molnar750ed1a2008-05-12 21:20:46 +02001216 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02001217 ftrace_update_time = stop - start;
1218 ftrace_update_tot_cnt += ftrace_update_cnt;
1219
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001220 return 0;
1221}
1222
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001223static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001224{
1225 struct ftrace_page *pg;
1226 int cnt;
1227 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001228
1229 /* allocate a few pages */
1230 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1231 if (!ftrace_pages_start)
1232 return -1;
1233
1234 /*
1235 * Allocate a few more pages.
1236 *
1237 * TODO: have some parser search vmlinux before
1238 * final linking to find all calls to ftrace.
1239 * Then we can:
1240 * a) know how many pages to allocate.
1241 * and/or
1242 * b) set up the table then.
1243 *
1244 * The dynamic code is still necessary for
1245 * modules.
1246 */
1247
1248 pg = ftrace_pages = ftrace_pages_start;
1249
Steven Rostedt68bf21a2008-08-14 15:45:08 -04001250 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001251 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08001252 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001253
1254 for (i = 0; i < cnt; i++) {
1255 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1256
1257 /* If we fail, we'll try later anyway */
1258 if (!pg->next)
1259 break;
1260
1261 pg = pg->next;
1262 }
1263
1264 return 0;
1265}
1266
Steven Rostedt5072c592008-05-12 21:20:43 +02001267enum {
1268 FTRACE_ITER_FILTER = (1 << 0),
1269 FTRACE_ITER_CONT = (1 << 1),
Steven Rostedt41c52c02008-05-22 11:46:33 -04001270 FTRACE_ITER_NOTRACE = (1 << 2),
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301271 FTRACE_ITER_FAILURES = (1 << 3),
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001272 FTRACE_ITER_PRINTALL = (1 << 4),
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001273 FTRACE_ITER_HASH = (1 << 5),
Steven Rostedt5072c592008-05-12 21:20:43 +02001274};
1275
1276#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1277
1278struct ftrace_iterator {
Steven Rostedt5072c592008-05-12 21:20:43 +02001279 struct ftrace_page *pg;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001280 int hidx;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001281 int idx;
Steven Rostedt5072c592008-05-12 21:20:43 +02001282 unsigned flags;
1283 unsigned char buffer[FTRACE_BUFF_MAX+1];
1284 unsigned buffer_idx;
1285 unsigned filtered;
1286};
1287
Ingo Molnare309b412008-05-12 21:20:51 +02001288static void *
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001289t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1290{
1291 struct ftrace_iterator *iter = m->private;
1292 struct hlist_node *hnd = v;
1293 struct hlist_head *hhd;
1294
1295 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1296
1297 (*pos)++;
1298
1299 retry:
1300 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1301 return NULL;
1302
1303 hhd = &ftrace_func_hash[iter->hidx];
1304
1305 if (hlist_empty(hhd)) {
1306 iter->hidx++;
1307 hnd = NULL;
1308 goto retry;
1309 }
1310
1311 if (!hnd)
1312 hnd = hhd->first;
1313 else {
1314 hnd = hnd->next;
1315 if (!hnd) {
1316 iter->hidx++;
1317 goto retry;
1318 }
1319 }
1320
1321 return hnd;
1322}
1323
1324static void *t_hash_start(struct seq_file *m, loff_t *pos)
1325{
1326 struct ftrace_iterator *iter = m->private;
1327 void *p = NULL;
1328
1329 iter->flags |= FTRACE_ITER_HASH;
1330
1331 return t_hash_next(m, p, pos);
1332}
1333
1334static int t_hash_show(struct seq_file *m, void *v)
1335{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001336 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001337 struct hlist_node *hnd = v;
1338 char str[KSYM_SYMBOL_LEN];
1339
Steven Rostedtb6887d72009-02-17 12:32:04 -05001340 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001341
Steven Rostedt809dcf22009-02-16 23:06:01 -05001342 if (rec->ops->print)
1343 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1344
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001345 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1346 seq_printf(m, "%s:", str);
1347
1348 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1349 seq_printf(m, "%s", str);
1350
1351 if (rec->data)
1352 seq_printf(m, ":%p", rec->data);
1353 seq_putc(m, '\n');
1354
1355 return 0;
1356}
1357
1358static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02001359t_next(struct seq_file *m, void *v, loff_t *pos)
1360{
1361 struct ftrace_iterator *iter = m->private;
1362 struct dyn_ftrace *rec = NULL;
1363
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001364 if (iter->flags & FTRACE_ITER_HASH)
1365 return t_hash_next(m, v, pos);
1366
Steven Rostedt5072c592008-05-12 21:20:43 +02001367 (*pos)++;
1368
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001369 if (iter->flags & FTRACE_ITER_PRINTALL)
1370 return NULL;
1371
Steven Rostedt5072c592008-05-12 21:20:43 +02001372 retry:
1373 if (iter->idx >= iter->pg->index) {
1374 if (iter->pg->next) {
1375 iter->pg = iter->pg->next;
1376 iter->idx = 0;
1377 goto retry;
Liming Wang50cdaf02008-11-28 12:13:21 +08001378 } else {
1379 iter->idx = -1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001380 }
1381 } else {
1382 rec = &iter->pg->records[iter->idx++];
Steven Rostedta9fdda32008-08-14 22:47:17 -04001383 if ((rec->flags & FTRACE_FL_FREE) ||
1384
1385 (!(iter->flags & FTRACE_ITER_FAILURES) &&
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301386 (rec->flags & FTRACE_FL_FAILED)) ||
1387
1388 ((iter->flags & FTRACE_ITER_FAILURES) &&
Steven Rostedta9fdda32008-08-14 22:47:17 -04001389 !(rec->flags & FTRACE_FL_FAILED)) ||
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301390
Steven Rostedt0183fb12008-11-07 22:36:02 -05001391 ((iter->flags & FTRACE_ITER_FILTER) &&
1392 !(rec->flags & FTRACE_FL_FILTER)) ||
1393
Steven Rostedt41c52c02008-05-22 11:46:33 -04001394 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1395 !(rec->flags & FTRACE_FL_NOTRACE))) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001396 rec = NULL;
1397 goto retry;
1398 }
1399 }
1400
Steven Rostedt5072c592008-05-12 21:20:43 +02001401 return rec;
1402}
1403
1404static void *t_start(struct seq_file *m, loff_t *pos)
1405{
1406 struct ftrace_iterator *iter = m->private;
1407 void *p = NULL;
Steven Rostedt5072c592008-05-12 21:20:43 +02001408
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001409 mutex_lock(&ftrace_lock);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001410 /*
1411 * For set_ftrace_filter reading, if we have the filter
1412 * off, we can short cut and just print out that all
1413 * functions are enabled.
1414 */
1415 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1416 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001417 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001418 iter->flags |= FTRACE_ITER_PRINTALL;
1419 (*pos)++;
1420 return iter;
1421 }
1422
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001423 if (iter->flags & FTRACE_ITER_HASH)
1424 return t_hash_start(m, pos);
1425
Liming Wang50cdaf02008-11-28 12:13:21 +08001426 if (*pos > 0) {
1427 if (iter->idx < 0)
1428 return p;
1429 (*pos)--;
1430 iter->idx--;
1431 }
walimis5821e1b2008-11-15 15:19:06 +08001432
Liming Wang50cdaf02008-11-28 12:13:21 +08001433 p = t_next(m, p, pos);
Steven Rostedt5072c592008-05-12 21:20:43 +02001434
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001435 if (!p)
1436 return t_hash_start(m, pos);
1437
Steven Rostedt5072c592008-05-12 21:20:43 +02001438 return p;
1439}
1440
1441static void t_stop(struct seq_file *m, void *p)
1442{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001443 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001444}
1445
1446static int t_show(struct seq_file *m, void *v)
1447{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001448 struct ftrace_iterator *iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02001449 struct dyn_ftrace *rec = v;
1450 char str[KSYM_SYMBOL_LEN];
1451
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001452 if (iter->flags & FTRACE_ITER_HASH)
1453 return t_hash_show(m, v);
1454
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05001455 if (iter->flags & FTRACE_ITER_PRINTALL) {
1456 seq_printf(m, "#### all functions enabled ####\n");
1457 return 0;
1458 }
1459
Steven Rostedt5072c592008-05-12 21:20:43 +02001460 if (!rec)
1461 return 0;
1462
1463 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1464
Liming Wang50cdaf02008-11-28 12:13:21 +08001465 seq_printf(m, "%s\n", str);
Steven Rostedt5072c592008-05-12 21:20:43 +02001466
1467 return 0;
1468}
1469
1470static struct seq_operations show_ftrace_seq_ops = {
1471 .start = t_start,
1472 .next = t_next,
1473 .stop = t_stop,
1474 .show = t_show,
1475};
1476
Ingo Molnare309b412008-05-12 21:20:51 +02001477static int
Steven Rostedt5072c592008-05-12 21:20:43 +02001478ftrace_avail_open(struct inode *inode, struct file *file)
1479{
1480 struct ftrace_iterator *iter;
1481 int ret;
1482
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001483 if (unlikely(ftrace_disabled))
1484 return -ENODEV;
1485
Steven Rostedt5072c592008-05-12 21:20:43 +02001486 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1487 if (!iter)
1488 return -ENOMEM;
1489
1490 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02001491
1492 ret = seq_open(file, &show_ftrace_seq_ops);
1493 if (!ret) {
1494 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001495
Steven Rostedt5072c592008-05-12 21:20:43 +02001496 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001497 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02001498 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001499 }
Steven Rostedt5072c592008-05-12 21:20:43 +02001500
1501 return ret;
1502}
1503
1504int ftrace_avail_release(struct inode *inode, struct file *file)
1505{
1506 struct seq_file *m = (struct seq_file *)file->private_data;
1507 struct ftrace_iterator *iter = m->private;
1508
1509 seq_release(inode, file);
1510 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001511
Steven Rostedt5072c592008-05-12 21:20:43 +02001512 return 0;
1513}
1514
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05301515static int
1516ftrace_failures_open(struct inode *inode, struct file *file)
1517{
1518 int ret;
1519 struct seq_file *m;
1520 struct ftrace_iterator *iter;
1521
1522 ret = ftrace_avail_open(inode, file);
1523 if (!ret) {
1524 m = (struct seq_file *)file->private_data;
1525 iter = (struct ftrace_iterator *)m->private;
1526 iter->flags = FTRACE_ITER_FAILURES;
1527 }
1528
1529 return ret;
1530}
1531
1532
Steven Rostedt41c52c02008-05-22 11:46:33 -04001533static void ftrace_filter_reset(int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001534{
1535 struct ftrace_page *pg;
1536 struct dyn_ftrace *rec;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001537 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001538
Steven Rostedt52baf112009-02-14 01:15:39 -05001539 mutex_lock(&ftrace_lock);
Steven Rostedt41c52c02008-05-22 11:46:33 -04001540 if (enable)
1541 ftrace_filtered = 0;
Steven Rostedt265c8312009-02-13 12:43:56 -05001542 do_for_each_ftrace_rec(pg, rec) {
1543 if (rec->flags & FTRACE_FL_FAILED)
1544 continue;
1545 rec->flags &= ~type;
1546 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001547 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001548}
1549
Ingo Molnare309b412008-05-12 21:20:51 +02001550static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04001551ftrace_regex_open(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02001552{
1553 struct ftrace_iterator *iter;
1554 int ret = 0;
1555
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001556 if (unlikely(ftrace_disabled))
1557 return -ENODEV;
1558
Steven Rostedt5072c592008-05-12 21:20:43 +02001559 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1560 if (!iter)
1561 return -ENOMEM;
1562
Steven Rostedt41c52c02008-05-22 11:46:33 -04001563 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001564 if ((file->f_mode & FMODE_WRITE) &&
1565 !(file->f_flags & O_APPEND))
Steven Rostedt41c52c02008-05-22 11:46:33 -04001566 ftrace_filter_reset(enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02001567
1568 if (file->f_mode & FMODE_READ) {
1569 iter->pg = ftrace_pages_start;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001570 iter->flags = enable ? FTRACE_ITER_FILTER :
1571 FTRACE_ITER_NOTRACE;
Steven Rostedt5072c592008-05-12 21:20:43 +02001572
1573 ret = seq_open(file, &show_ftrace_seq_ops);
1574 if (!ret) {
1575 struct seq_file *m = file->private_data;
1576 m->private = iter;
1577 } else
1578 kfree(iter);
1579 } else
1580 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04001581 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001582
1583 return ret;
1584}
1585
Steven Rostedt41c52c02008-05-22 11:46:33 -04001586static int
1587ftrace_filter_open(struct inode *inode, struct file *file)
1588{
1589 return ftrace_regex_open(inode, file, 1);
1590}
1591
1592static int
1593ftrace_notrace_open(struct inode *inode, struct file *file)
1594{
1595 return ftrace_regex_open(inode, file, 0);
1596}
1597
Ingo Molnare309b412008-05-12 21:20:51 +02001598static loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04001599ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02001600{
1601 loff_t ret;
1602
1603 if (file->f_mode & FMODE_READ)
1604 ret = seq_lseek(file, offset, origin);
1605 else
1606 file->f_pos = ret = 1;
1607
1608 return ret;
1609}
1610
1611enum {
1612 MATCH_FULL,
1613 MATCH_FRONT_ONLY,
1614 MATCH_MIDDLE_ONLY,
1615 MATCH_END_ONLY,
1616};
1617
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001618/*
1619 * (static function - no need for kernel doc)
1620 *
1621 * Pass in a buffer containing a glob and this function will
1622 * set search to point to the search part of the buffer and
1623 * return the type of search it is (see enum above).
1624 * This does modify buff.
1625 *
1626 * Returns enum type.
1627 * search returns the pointer to use for comparison.
1628 * not returns 1 if buff started with a '!'
1629 * 0 otherwise.
1630 */
1631static int
Steven Rostedt64e7c442009-02-13 17:08:48 -05001632ftrace_setup_glob(char *buff, int len, char **search, int *not)
Steven Rostedt5072c592008-05-12 21:20:43 +02001633{
Steven Rostedt5072c592008-05-12 21:20:43 +02001634 int type = MATCH_FULL;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001635 int i;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001636
1637 if (buff[0] == '!') {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001638 *not = 1;
Steven Rostedtea3a6d62008-12-17 15:05:36 -05001639 buff++;
1640 len--;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001641 } else
1642 *not = 0;
1643
1644 *search = buff;
Steven Rostedt5072c592008-05-12 21:20:43 +02001645
1646 for (i = 0; i < len; i++) {
1647 if (buff[i] == '*') {
1648 if (!i) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001649 *search = buff + 1;
Steven Rostedt5072c592008-05-12 21:20:43 +02001650 type = MATCH_END_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001651 } else {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001652 if (type == MATCH_END_ONLY)
Steven Rostedt5072c592008-05-12 21:20:43 +02001653 type = MATCH_MIDDLE_ONLY;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001654 else
Steven Rostedt5072c592008-05-12 21:20:43 +02001655 type = MATCH_FRONT_ONLY;
Steven Rostedt5072c592008-05-12 21:20:43 +02001656 buff[i] = 0;
1657 break;
1658 }
1659 }
1660 }
1661
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001662 return type;
1663}
1664
Steven Rostedt64e7c442009-02-13 17:08:48 -05001665static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001666{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001667 int matched = 0;
1668 char *ptr;
1669
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001670 switch (type) {
1671 case MATCH_FULL:
1672 if (strcmp(str, regex) == 0)
1673 matched = 1;
1674 break;
1675 case MATCH_FRONT_ONLY:
1676 if (strncmp(str, regex, len) == 0)
1677 matched = 1;
1678 break;
1679 case MATCH_MIDDLE_ONLY:
1680 if (strstr(str, regex))
1681 matched = 1;
1682 break;
1683 case MATCH_END_ONLY:
1684 ptr = strstr(str, regex);
1685 if (ptr && (ptr[len] == 0))
1686 matched = 1;
1687 break;
1688 }
1689
1690 return matched;
1691}
1692
Steven Rostedt64e7c442009-02-13 17:08:48 -05001693static int
1694ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1695{
1696 char str[KSYM_SYMBOL_LEN];
1697
1698 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1699 return ftrace_match(str, regex, len, type);
1700}
1701
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001702static void ftrace_match_records(char *buff, int len, int enable)
1703{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001704 unsigned int search_len;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001705 struct ftrace_page *pg;
1706 struct dyn_ftrace *rec;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001707 unsigned long flag;
1708 char *search;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001709 int type;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001710 int not;
1711
Steven Rostedt6a24a242009-02-17 11:20:26 -05001712 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001713 type = ftrace_setup_glob(buff, len, &search, &not);
1714
1715 search_len = strlen(search);
1716
Steven Rostedt52baf112009-02-14 01:15:39 -05001717 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05001718 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt5072c592008-05-12 21:20:43 +02001719
Steven Rostedt265c8312009-02-13 12:43:56 -05001720 if (rec->flags & FTRACE_FL_FAILED)
1721 continue;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05001722
1723 if (ftrace_match_record(rec, search, search_len, type)) {
Steven Rostedt265c8312009-02-13 12:43:56 -05001724 if (not)
1725 rec->flags &= ~flag;
1726 else
1727 rec->flags |= flag;
1728 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001729 /*
1730 * Only enable filtering if we have a function that
1731 * is filtered on.
1732 */
1733 if (enable && (rec->flags & FTRACE_FL_FILTER))
1734 ftrace_filtered = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05001735 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001736 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02001737}
1738
Steven Rostedt64e7c442009-02-13 17:08:48 -05001739static int
1740ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1741 char *regex, int len, int type)
1742{
1743 char str[KSYM_SYMBOL_LEN];
1744 char *modname;
1745
1746 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1747
1748 if (!modname || strcmp(modname, mod))
1749 return 0;
1750
1751 /* blank search means to match all funcs in the mod */
1752 if (len)
1753 return ftrace_match(str, regex, len, type);
1754 else
1755 return 1;
1756}
1757
1758static void ftrace_match_module_records(char *buff, char *mod, int enable)
1759{
Steven Rostedt6a24a242009-02-17 11:20:26 -05001760 unsigned search_len = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001761 struct ftrace_page *pg;
1762 struct dyn_ftrace *rec;
1763 int type = MATCH_FULL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001764 char *search = buff;
1765 unsigned long flag;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001766 int not = 0;
1767
Steven Rostedt6a24a242009-02-17 11:20:26 -05001768 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1769
Steven Rostedt64e7c442009-02-13 17:08:48 -05001770 /* blank or '*' mean the same */
1771 if (strcmp(buff, "*") == 0)
1772 buff[0] = 0;
1773
1774 /* handle the case of 'dont filter this module' */
1775 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1776 buff[0] = 0;
1777 not = 1;
1778 }
1779
1780 if (strlen(buff)) {
1781 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1782 search_len = strlen(search);
1783 }
1784
Steven Rostedt52baf112009-02-14 01:15:39 -05001785 mutex_lock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001786 do_for_each_ftrace_rec(pg, rec) {
1787
1788 if (rec->flags & FTRACE_FL_FAILED)
1789 continue;
1790
1791 if (ftrace_match_module_record(rec, mod,
1792 search, search_len, type)) {
1793 if (not)
1794 rec->flags &= ~flag;
1795 else
1796 rec->flags |= flag;
1797 }
Steven Rostedte68746a2009-02-13 20:53:42 -05001798 if (enable && (rec->flags & FTRACE_FL_FILTER))
1799 ftrace_filtered = 1;
Steven Rostedt64e7c442009-02-13 17:08:48 -05001800
1801 } while_for_each_ftrace_rec();
Steven Rostedt52baf112009-02-14 01:15:39 -05001802 mutex_unlock(&ftrace_lock);
Steven Rostedt64e7c442009-02-13 17:08:48 -05001803}
1804
Steven Rostedtf6180772009-02-14 00:40:25 -05001805/*
1806 * We register the module command as a template to show others how
1807 * to register the a command as well.
1808 */
1809
1810static int
1811ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1812{
1813 char *mod;
1814
1815 /*
1816 * cmd == 'mod' because we only registered this func
1817 * for the 'mod' ftrace_func_command.
1818 * But if you register one func with multiple commands,
1819 * you can tell which command was used by the cmd
1820 * parameter.
1821 */
1822
1823 /* we must have a module name */
1824 if (!param)
1825 return -EINVAL;
1826
1827 mod = strsep(&param, ":");
1828 if (!strlen(mod))
1829 return -EINVAL;
1830
1831 ftrace_match_module_records(func, mod, enable);
1832 return 0;
1833}
1834
1835static struct ftrace_func_command ftrace_mod_cmd = {
1836 .name = "mod",
1837 .func = ftrace_mod_callback,
1838};
1839
1840static int __init ftrace_mod_cmd_init(void)
1841{
1842 return register_ftrace_command(&ftrace_mod_cmd);
1843}
1844device_initcall(ftrace_mod_cmd_init);
1845
Steven Rostedt59df055f2009-02-14 15:29:06 -05001846static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05001847function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001848{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001849 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001850 struct hlist_head *hhd;
1851 struct hlist_node *n;
1852 unsigned long key;
1853 int resched;
1854
1855 key = hash_long(ip, FTRACE_HASH_BITS);
1856
1857 hhd = &ftrace_func_hash[key];
1858
1859 if (hlist_empty(hhd))
1860 return;
1861
1862 /*
1863 * Disable preemption for these calls to prevent a RCU grace
1864 * period. This syncs the hash iteration and freeing of items
1865 * on the hash. rcu_read_lock is too dangerous here.
1866 */
1867 resched = ftrace_preempt_disable();
1868 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1869 if (entry->ip == ip)
1870 entry->ops->func(ip, parent_ip, &entry->data);
1871 }
1872 ftrace_preempt_enable(resched);
1873}
1874
Steven Rostedtb6887d72009-02-17 12:32:04 -05001875static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05001876{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001877 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001878};
1879
Steven Rostedtb6887d72009-02-17 12:32:04 -05001880static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001881
Steven Rostedtb6887d72009-02-17 12:32:04 -05001882static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001883{
1884 int i;
1885
Steven Rostedtb6887d72009-02-17 12:32:04 -05001886 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001887 return;
1888
1889 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1890 struct hlist_head *hhd = &ftrace_func_hash[i];
1891 if (hhd->first)
1892 break;
1893 }
1894 /* Nothing registered? */
1895 if (i == FTRACE_FUNC_HASHSIZE)
1896 return;
1897
Steven Rostedtb6887d72009-02-17 12:32:04 -05001898 __register_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001899 ftrace_startup(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001900 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001901}
1902
Steven Rostedtb6887d72009-02-17 12:32:04 -05001903static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001904{
1905 int i;
1906
Steven Rostedtb6887d72009-02-17 12:32:04 -05001907 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05001908 return;
1909
1910 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1911 struct hlist_head *hhd = &ftrace_func_hash[i];
1912 if (hhd->first)
1913 return;
1914 }
1915
1916 /* no more funcs left */
Steven Rostedtb6887d72009-02-17 12:32:04 -05001917 __unregister_ftrace_function(&trace_probe_ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001918 ftrace_shutdown(0);
Steven Rostedtb6887d72009-02-17 12:32:04 -05001919 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001920}
1921
1922
1923static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1924{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001925 struct ftrace_func_probe *entry =
1926 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05001927
1928 if (entry->ops->free)
1929 entry->ops->free(&entry->data);
1930 kfree(entry);
1931}
1932
1933
1934int
Steven Rostedtb6887d72009-02-17 12:32:04 -05001935register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05001936 void *data)
1937{
Steven Rostedtb6887d72009-02-17 12:32:04 -05001938 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001939 struct ftrace_page *pg;
1940 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001941 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001942 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05001943 int count = 0;
1944 char *search;
1945
1946 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1947 len = strlen(search);
1948
Steven Rostedtb6887d72009-02-17 12:32:04 -05001949 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001950 if (WARN_ON(not))
1951 return -EINVAL;
1952
1953 mutex_lock(&ftrace_lock);
1954 do_for_each_ftrace_rec(pg, rec) {
1955
1956 if (rec->flags & FTRACE_FL_FAILED)
1957 continue;
1958
1959 if (!ftrace_match_record(rec, search, len, type))
1960 continue;
1961
1962 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1963 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05001964 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05001965 if (!count)
1966 count = -ENOMEM;
1967 goto out_unlock;
1968 }
1969
1970 count++;
1971
1972 entry->data = data;
1973
1974 /*
1975 * The caller might want to do something special
1976 * for each function we find. We call the callback
1977 * to give the caller an opportunity to do so.
1978 */
1979 if (ops->callback) {
1980 if (ops->callback(rec->ip, &entry->data) < 0) {
1981 /* caller does not like this func */
1982 kfree(entry);
1983 continue;
1984 }
1985 }
1986
1987 entry->ops = ops;
1988 entry->ip = rec->ip;
1989
1990 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1991 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1992
1993 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05001994 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05001995
1996 out_unlock:
1997 mutex_unlock(&ftrace_lock);
1998
1999 return count;
2000}
2001
2002enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002003 PROBE_TEST_FUNC = 1,
2004 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002005};
2006
2007static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002008__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002009 void *data, int flags)
2010{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002011 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002012 struct hlist_node *n, *tmp;
2013 char str[KSYM_SYMBOL_LEN];
2014 int type = MATCH_FULL;
2015 int i, len = 0;
2016 char *search;
2017
2018 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2019 glob = NULL;
2020 else {
2021 int not;
2022
2023 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2024 len = strlen(search);
2025
Steven Rostedtb6887d72009-02-17 12:32:04 -05002026 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002027 if (WARN_ON(not))
2028 return;
2029 }
2030
2031 mutex_lock(&ftrace_lock);
2032 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2033 struct hlist_head *hhd = &ftrace_func_hash[i];
2034
2035 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2036
2037 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05002038 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002039 continue;
2040
Steven Rostedtb6887d72009-02-17 12:32:04 -05002041 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002042 continue;
2043
2044 /* do this last, since it is the most expensive */
2045 if (glob) {
2046 kallsyms_lookup(entry->ip, NULL, NULL,
2047 NULL, str);
2048 if (!ftrace_match(str, glob, len, type))
2049 continue;
2050 }
2051
2052 hlist_del(&entry->node);
2053 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2054 }
2055 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05002056 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002057 mutex_unlock(&ftrace_lock);
2058}
2059
2060void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002061unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002062 void *data)
2063{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002064 __unregister_ftrace_function_probe(glob, ops, data,
2065 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002066}
2067
2068void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002069unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002070{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002071 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002072}
2073
Steven Rostedtb6887d72009-02-17 12:32:04 -05002074void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002075{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002076 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002077}
2078
Steven Rostedtf6180772009-02-14 00:40:25 -05002079static LIST_HEAD(ftrace_commands);
2080static DEFINE_MUTEX(ftrace_cmd_mutex);
2081
2082int register_ftrace_command(struct ftrace_func_command *cmd)
2083{
2084 struct ftrace_func_command *p;
2085 int ret = 0;
2086
2087 mutex_lock(&ftrace_cmd_mutex);
2088 list_for_each_entry(p, &ftrace_commands, list) {
2089 if (strcmp(cmd->name, p->name) == 0) {
2090 ret = -EBUSY;
2091 goto out_unlock;
2092 }
2093 }
2094 list_add(&cmd->list, &ftrace_commands);
2095 out_unlock:
2096 mutex_unlock(&ftrace_cmd_mutex);
2097
2098 return ret;
2099}
2100
2101int unregister_ftrace_command(struct ftrace_func_command *cmd)
2102{
2103 struct ftrace_func_command *p, *n;
2104 int ret = -ENODEV;
2105
2106 mutex_lock(&ftrace_cmd_mutex);
2107 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2108 if (strcmp(cmd->name, p->name) == 0) {
2109 ret = 0;
2110 list_del_init(&p->list);
2111 goto out_unlock;
2112 }
2113 }
2114 out_unlock:
2115 mutex_unlock(&ftrace_cmd_mutex);
2116
2117 return ret;
2118}
2119
Steven Rostedt64e7c442009-02-13 17:08:48 -05002120static int ftrace_process_regex(char *buff, int len, int enable)
2121{
Steven Rostedtf6180772009-02-14 00:40:25 -05002122 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002123 struct ftrace_func_command *p;
Steven Rostedtf6180772009-02-14 00:40:25 -05002124 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002125
2126 func = strsep(&next, ":");
2127
2128 if (!next) {
2129 ftrace_match_records(func, len, enable);
2130 return 0;
2131 }
2132
Steven Rostedtf6180772009-02-14 00:40:25 -05002133 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05002134
2135 command = strsep(&next, ":");
2136
Steven Rostedtf6180772009-02-14 00:40:25 -05002137 mutex_lock(&ftrace_cmd_mutex);
2138 list_for_each_entry(p, &ftrace_commands, list) {
2139 if (strcmp(p->name, command) == 0) {
2140 ret = p->func(func, command, next, enable);
2141 goto out_unlock;
2142 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05002143 }
Steven Rostedtf6180772009-02-14 00:40:25 -05002144 out_unlock:
2145 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002146
Steven Rostedtf6180772009-02-14 00:40:25 -05002147 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002148}
2149
Ingo Molnare309b412008-05-12 21:20:51 +02002150static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002151ftrace_regex_write(struct file *file, const char __user *ubuf,
2152 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002153{
2154 struct ftrace_iterator *iter;
2155 char ch;
2156 size_t read = 0;
2157 ssize_t ret;
2158
2159 if (!cnt || cnt < 0)
2160 return 0;
2161
Steven Rostedt41c52c02008-05-22 11:46:33 -04002162 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002163
2164 if (file->f_mode & FMODE_READ) {
2165 struct seq_file *m = file->private_data;
2166 iter = m->private;
2167 } else
2168 iter = file->private_data;
2169
2170 if (!*ppos) {
2171 iter->flags &= ~FTRACE_ITER_CONT;
2172 iter->buffer_idx = 0;
2173 }
2174
2175 ret = get_user(ch, ubuf++);
2176 if (ret)
2177 goto out;
2178 read++;
2179 cnt--;
2180
2181 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2182 /* skip white space */
2183 while (cnt && isspace(ch)) {
2184 ret = get_user(ch, ubuf++);
2185 if (ret)
2186 goto out;
2187 read++;
2188 cnt--;
2189 }
2190
Steven Rostedt5072c592008-05-12 21:20:43 +02002191 if (isspace(ch)) {
2192 file->f_pos += read;
2193 ret = read;
2194 goto out;
2195 }
2196
2197 iter->buffer_idx = 0;
2198 }
2199
2200 while (cnt && !isspace(ch)) {
2201 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2202 iter->buffer[iter->buffer_idx++] = ch;
2203 else {
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207 ret = get_user(ch, ubuf++);
2208 if (ret)
2209 goto out;
2210 read++;
2211 cnt--;
2212 }
2213
2214 if (isspace(ch)) {
2215 iter->filtered++;
2216 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002217 ret = ftrace_process_regex(iter->buffer,
2218 iter->buffer_idx, enable);
2219 if (ret)
2220 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02002221 iter->buffer_idx = 0;
2222 } else
2223 iter->flags |= FTRACE_ITER_CONT;
2224
2225
2226 file->f_pos += read;
2227
2228 ret = read;
2229 out:
Steven Rostedt41c52c02008-05-22 11:46:33 -04002230 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002231
2232 return ret;
2233}
2234
Steven Rostedt41c52c02008-05-22 11:46:33 -04002235static ssize_t
2236ftrace_filter_write(struct file *file, const char __user *ubuf,
2237 size_t cnt, loff_t *ppos)
2238{
2239 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2240}
2241
2242static ssize_t
2243ftrace_notrace_write(struct file *file, const char __user *ubuf,
2244 size_t cnt, loff_t *ppos)
2245{
2246 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2247}
2248
2249static void
2250ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2251{
2252 if (unlikely(ftrace_disabled))
2253 return;
2254
2255 mutex_lock(&ftrace_regex_lock);
2256 if (reset)
2257 ftrace_filter_reset(enable);
2258 if (buf)
Steven Rostedt7f24b312009-02-13 14:37:33 -05002259 ftrace_match_records(buf, len, enable);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002260 mutex_unlock(&ftrace_regex_lock);
2261}
2262
Steven Rostedt77a2b372008-05-12 21:20:45 +02002263/**
2264 * ftrace_set_filter - set a function to filter on in ftrace
2265 * @buf - the string that holds the function filter text.
2266 * @len - the length of the string.
2267 * @reset - non zero to reset all filters before applying this filter.
2268 *
2269 * Filters denote which functions should be enabled when tracing is enabled.
2270 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2271 */
Ingo Molnare309b412008-05-12 21:20:51 +02002272void ftrace_set_filter(unsigned char *buf, int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02002273{
Steven Rostedt41c52c02008-05-22 11:46:33 -04002274 ftrace_set_regex(buf, len, reset, 1);
2275}
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002276
Steven Rostedt41c52c02008-05-22 11:46:33 -04002277/**
2278 * ftrace_set_notrace - set a function to not trace in ftrace
2279 * @buf - the string that holds the function notrace text.
2280 * @len - the length of the string.
2281 * @reset - non zero to reset all filters before applying this filter.
2282 *
2283 * Notrace Filters denote which functions should not be enabled when tracing
2284 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2285 * for tracing.
2286 */
2287void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2288{
2289 ftrace_set_regex(buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02002290}
2291
Ingo Molnare309b412008-05-12 21:20:51 +02002292static int
Steven Rostedt41c52c02008-05-22 11:46:33 -04002293ftrace_regex_release(struct inode *inode, struct file *file, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02002294{
2295 struct seq_file *m = (struct seq_file *)file->private_data;
2296 struct ftrace_iterator *iter;
2297
Steven Rostedt41c52c02008-05-22 11:46:33 -04002298 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002299 if (file->f_mode & FMODE_READ) {
2300 iter = m->private;
2301
2302 seq_release(inode, file);
2303 } else
2304 iter = file->private_data;
2305
2306 if (iter->buffer_idx) {
2307 iter->filtered++;
2308 iter->buffer[iter->buffer_idx] = 0;
Steven Rostedt7f24b312009-02-13 14:37:33 -05002309 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02002310 }
2311
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002312 mutex_lock(&ftrace_lock);
Steven Rostedtee02a2e2008-11-15 16:31:41 -05002313 if (ftrace_start_up && ftrace_enabled)
Steven Rostedt5072c592008-05-12 21:20:43 +02002314 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002315 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002316
2317 kfree(iter);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002318 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002319 return 0;
2320}
2321
Steven Rostedt41c52c02008-05-22 11:46:33 -04002322static int
2323ftrace_filter_release(struct inode *inode, struct file *file)
2324{
2325 return ftrace_regex_release(inode, file, 1);
2326}
2327
2328static int
2329ftrace_notrace_release(struct inode *inode, struct file *file)
2330{
2331 return ftrace_regex_release(inode, file, 0);
2332}
2333
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002334static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002335 .open = ftrace_avail_open,
2336 .read = seq_read,
2337 .llseek = seq_lseek,
2338 .release = ftrace_avail_release,
2339};
2340
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002341static const struct file_operations ftrace_failures_fops = {
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302342 .open = ftrace_failures_open,
2343 .read = seq_read,
2344 .llseek = seq_lseek,
2345 .release = ftrace_avail_release,
2346};
2347
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002348static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002349 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002350 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02002351 .write = ftrace_filter_write,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002352 .llseek = ftrace_regex_lseek,
Steven Rostedt5072c592008-05-12 21:20:43 +02002353 .release = ftrace_filter_release,
2354};
2355
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002356static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04002357 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002358 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04002359 .write = ftrace_notrace_write,
2360 .llseek = ftrace_regex_lseek,
2361 .release = ftrace_notrace_release,
2362};
2363
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002364#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2365
2366static DEFINE_MUTEX(graph_lock);
2367
2368int ftrace_graph_count;
2369unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2370
2371static void *
2372g_next(struct seq_file *m, void *v, loff_t *pos)
2373{
2374 unsigned long *array = m->private;
2375 int index = *pos;
2376
2377 (*pos)++;
2378
2379 if (index >= ftrace_graph_count)
2380 return NULL;
2381
2382 return &array[index];
2383}
2384
2385static void *g_start(struct seq_file *m, loff_t *pos)
2386{
2387 void *p = NULL;
2388
2389 mutex_lock(&graph_lock);
2390
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002391 /* Nothing, tell g_show to print all functions are enabled */
2392 if (!ftrace_graph_count && !*pos)
2393 return (void *)1;
2394
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002395 p = g_next(m, p, pos);
2396
2397 return p;
2398}
2399
2400static void g_stop(struct seq_file *m, void *p)
2401{
2402 mutex_unlock(&graph_lock);
2403}
2404
2405static int g_show(struct seq_file *m, void *v)
2406{
2407 unsigned long *ptr = v;
2408 char str[KSYM_SYMBOL_LEN];
2409
2410 if (!ptr)
2411 return 0;
2412
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002413 if (ptr == (unsigned long *)1) {
2414 seq_printf(m, "#### all functions enabled ####\n");
2415 return 0;
2416 }
2417
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002418 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2419
2420 seq_printf(m, "%s\n", str);
2421
2422 return 0;
2423}
2424
2425static struct seq_operations ftrace_graph_seq_ops = {
2426 .start = g_start,
2427 .next = g_next,
2428 .stop = g_stop,
2429 .show = g_show,
2430};
2431
2432static int
2433ftrace_graph_open(struct inode *inode, struct file *file)
2434{
2435 int ret = 0;
2436
2437 if (unlikely(ftrace_disabled))
2438 return -ENODEV;
2439
2440 mutex_lock(&graph_lock);
2441 if ((file->f_mode & FMODE_WRITE) &&
2442 !(file->f_flags & O_APPEND)) {
2443 ftrace_graph_count = 0;
2444 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2445 }
2446
2447 if (file->f_mode & FMODE_READ) {
2448 ret = seq_open(file, &ftrace_graph_seq_ops);
2449 if (!ret) {
2450 struct seq_file *m = file->private_data;
2451 m->private = ftrace_graph_funcs;
2452 }
2453 } else
2454 file->private_data = ftrace_graph_funcs;
2455 mutex_unlock(&graph_lock);
2456
2457 return ret;
2458}
2459
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002460static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002461ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002462{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002463 struct dyn_ftrace *rec;
2464 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002465 int search_len;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002466 int found = 0;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002467 int type, not;
2468 char *search;
2469 bool exists;
2470 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002471
2472 if (ftrace_disabled)
2473 return -ENODEV;
2474
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002475 /* decode regex */
2476 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2477 if (not)
2478 return -EINVAL;
2479
2480 search_len = strlen(search);
2481
Steven Rostedt52baf112009-02-14 01:15:39 -05002482 mutex_lock(&ftrace_lock);
Steven Rostedt265c8312009-02-13 12:43:56 -05002483 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002484
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002485 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2486 break;
2487
Steven Rostedt265c8312009-02-13 12:43:56 -05002488 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2489 continue;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002490
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002491 if (ftrace_match_record(rec, search, search_len, type)) {
2492 /* ensure it is not already in the array */
2493 exists = false;
2494 for (i = 0; i < *idx; i++)
2495 if (array[i] == rec->ip) {
2496 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05002497 break;
2498 }
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002499 if (!exists) {
2500 array[(*idx)++] = rec->ip;
2501 found = 1;
2502 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002503 }
Steven Rostedt265c8312009-02-13 12:43:56 -05002504 } while_for_each_ftrace_rec();
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002505
Steven Rostedt52baf112009-02-14 01:15:39 -05002506 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002507
2508 return found ? 0 : -EINVAL;
2509}
2510
2511static ssize_t
2512ftrace_graph_write(struct file *file, const char __user *ubuf,
2513 size_t cnt, loff_t *ppos)
2514{
2515 unsigned char buffer[FTRACE_BUFF_MAX+1];
2516 unsigned long *array;
2517 size_t read = 0;
2518 ssize_t ret;
2519 int index = 0;
2520 char ch;
2521
2522 if (!cnt || cnt < 0)
2523 return 0;
2524
2525 mutex_lock(&graph_lock);
2526
2527 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2528 ret = -EBUSY;
2529 goto out;
2530 }
2531
2532 if (file->f_mode & FMODE_READ) {
2533 struct seq_file *m = file->private_data;
2534 array = m->private;
2535 } else
2536 array = file->private_data;
2537
2538 ret = get_user(ch, ubuf++);
2539 if (ret)
2540 goto out;
2541 read++;
2542 cnt--;
2543
2544 /* skip white space */
2545 while (cnt && isspace(ch)) {
2546 ret = get_user(ch, ubuf++);
2547 if (ret)
2548 goto out;
2549 read++;
2550 cnt--;
2551 }
2552
2553 if (isspace(ch)) {
2554 *ppos += read;
2555 ret = read;
2556 goto out;
2557 }
2558
2559 while (cnt && !isspace(ch)) {
2560 if (index < FTRACE_BUFF_MAX)
2561 buffer[index++] = ch;
2562 else {
2563 ret = -EINVAL;
2564 goto out;
2565 }
2566 ret = get_user(ch, ubuf++);
2567 if (ret)
2568 goto out;
2569 read++;
2570 cnt--;
2571 }
2572 buffer[index] = 0;
2573
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01002574 /* we allow only one expression at a time */
2575 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002576 if (ret)
2577 goto out;
2578
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002579 file->f_pos += read;
2580
2581 ret = read;
2582 out:
2583 mutex_unlock(&graph_lock);
2584
2585 return ret;
2586}
2587
2588static const struct file_operations ftrace_graph_fops = {
2589 .open = ftrace_graph_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08002590 .read = seq_read,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002591 .write = ftrace_graph_write,
2592};
2593#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2594
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002595static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02002596{
Steven Rostedt5072c592008-05-12 21:20:43 +02002597 struct dentry *entry;
2598
Steven Rostedt5072c592008-05-12 21:20:43 +02002599 entry = debugfs_create_file("available_filter_functions", 0444,
2600 d_tracer, NULL, &ftrace_avail_fops);
2601 if (!entry)
2602 pr_warning("Could not create debugfs "
2603 "'available_filter_functions' entry\n");
2604
Abhishek Sagareb9a7bf2008-06-01 21:47:54 +05302605 entry = debugfs_create_file("failures", 0444,
2606 d_tracer, NULL, &ftrace_failures_fops);
2607 if (!entry)
2608 pr_warning("Could not create debugfs 'failures' entry\n");
2609
Steven Rostedt5072c592008-05-12 21:20:43 +02002610 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2611 NULL, &ftrace_filter_fops);
2612 if (!entry)
2613 pr_warning("Could not create debugfs "
2614 "'set_ftrace_filter' entry\n");
Steven Rostedt41c52c02008-05-22 11:46:33 -04002615
2616 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2617 NULL, &ftrace_notrace_fops);
2618 if (!entry)
2619 pr_warning("Could not create debugfs "
2620 "'set_ftrace_notrace' entry\n");
Steven Rostedtad90c0e2008-05-27 20:48:37 -04002621
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05002622#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2623 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2624 NULL,
2625 &ftrace_graph_fops);
2626 if (!entry)
2627 pr_warning("Could not create debugfs "
2628 "'set_graph_function' entry\n");
2629#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2630
Steven Rostedt5072c592008-05-12 21:20:43 +02002631 return 0;
2632}
2633
Steven Rostedt31e88902008-11-14 16:21:19 -08002634static int ftrace_convert_nops(struct module *mod,
2635 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002636 unsigned long *end)
2637{
2638 unsigned long *p;
2639 unsigned long addr;
2640 unsigned long flags;
2641
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002642 mutex_lock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002643 p = start;
2644 while (p < end) {
2645 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08002646 /*
2647 * Some architecture linkers will pad between
2648 * the different mcount_loc sections of different
2649 * object files to satisfy alignments.
2650 * Skip any NULL pointers.
2651 */
2652 if (!addr)
2653 continue;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002654 ftrace_record_ip(addr);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002655 }
2656
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002657 /* disable interrupts to prevent kstop machine */
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002658 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08002659 ftrace_update_code(mod);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002660 local_irq_restore(flags);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002661 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002662
2663 return 0;
2664}
2665
Steven Rostedt31e88902008-11-14 16:21:19 -08002666void ftrace_init_module(struct module *mod,
2667 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04002668{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04002669 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04002670 return;
Steven Rostedt31e88902008-11-14 16:21:19 -08002671 ftrace_convert_nops(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04002672}
2673
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002674extern unsigned long __start_mcount_loc[];
2675extern unsigned long __stop_mcount_loc[];
2676
2677void __init ftrace_init(void)
2678{
2679 unsigned long count, addr, flags;
2680 int ret;
2681
2682 /* Keep the ftrace pointer to the stub */
2683 addr = (unsigned long)ftrace_stub;
2684
2685 local_irq_save(flags);
2686 ftrace_dyn_arch_init(&addr);
2687 local_irq_restore(flags);
2688
2689 /* ftrace_dyn_arch_init places the return code in addr */
2690 if (addr)
2691 goto failed;
2692
2693 count = __stop_mcount_loc - __start_mcount_loc;
2694
2695 ret = ftrace_dyn_table_alloc(count);
2696 if (ret)
2697 goto failed;
2698
2699 last_ftrace_enabled = ftrace_enabled = 1;
2700
Steven Rostedt31e88902008-11-14 16:21:19 -08002701 ret = ftrace_convert_nops(NULL,
2702 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002703 __stop_mcount_loc);
2704
2705 return;
2706 failed:
2707 ftrace_disabled = 1;
2708}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002709
Steven Rostedt3d083392008-05-12 21:20:42 +02002710#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01002711
2712static int __init ftrace_nodyn_init(void)
2713{
2714 ftrace_enabled = 1;
2715 return 0;
2716}
2717device_initcall(ftrace_nodyn_init);
2718
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002719static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2720static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002721/* Keep as macros so we do not need to define the commands */
2722# define ftrace_startup(command) do { } while (0)
2723# define ftrace_shutdown(command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002724# define ftrace_startup_sysctl() do { } while (0)
2725# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +02002726#endif /* CONFIG_DYNAMIC_FTRACE */
2727
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002728static ssize_t
2729ftrace_pid_read(struct file *file, char __user *ubuf,
2730 size_t cnt, loff_t *ppos)
2731{
2732 char buf[64];
2733 int r;
2734
Steven Rostedte32d8952008-12-04 00:26:41 -05002735 if (ftrace_pid_trace == ftrace_swapper_pid)
2736 r = sprintf(buf, "swapper tasks\n");
2737 else if (ftrace_pid_trace)
Lai Jiangshancc59c9e2009-03-24 11:03:01 +08002738 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002739 else
2740 r = sprintf(buf, "no pid\n");
2741
2742 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2743}
2744
Steven Rostedte32d8952008-12-04 00:26:41 -05002745static void clear_ftrace_swapper(void)
2746{
2747 struct task_struct *p;
2748 int cpu;
2749
2750 get_online_cpus();
2751 for_each_online_cpu(cpu) {
2752 p = idle_task(cpu);
2753 clear_tsk_trace_trace(p);
2754 }
2755 put_online_cpus();
2756}
2757
2758static void set_ftrace_swapper(void)
2759{
2760 struct task_struct *p;
2761 int cpu;
2762
2763 get_online_cpus();
2764 for_each_online_cpu(cpu) {
2765 p = idle_task(cpu);
2766 set_tsk_trace_trace(p);
2767 }
2768 put_online_cpus();
2769}
2770
2771static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002772{
2773 struct task_struct *p;
2774
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002775 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05002776 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05002777 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05002778 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002779 rcu_read_unlock();
2780
Steven Rostedte32d8952008-12-04 00:26:41 -05002781 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002782}
2783
Steven Rostedte32d8952008-12-04 00:26:41 -05002784static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05002785{
2786 struct task_struct *p;
2787
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002788 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002789 do_each_pid_task(pid, PIDTYPE_PID, p) {
2790 set_tsk_trace_trace(p);
2791 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01002792 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05002793}
2794
Steven Rostedte32d8952008-12-04 00:26:41 -05002795static void clear_ftrace_pid_task(struct pid **pid)
2796{
2797 if (*pid == ftrace_swapper_pid)
2798 clear_ftrace_swapper();
2799 else
2800 clear_ftrace_pid(*pid);
2801
2802 *pid = NULL;
2803}
2804
2805static void set_ftrace_pid_task(struct pid *pid)
2806{
2807 if (pid == ftrace_swapper_pid)
2808 set_ftrace_swapper();
2809 else
2810 set_ftrace_pid(pid);
2811}
2812
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002813static ssize_t
2814ftrace_pid_write(struct file *filp, const char __user *ubuf,
2815 size_t cnt, loff_t *ppos)
2816{
Steven Rostedt978f3a42008-12-04 00:26:40 -05002817 struct pid *pid;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002818 char buf[64];
2819 long val;
2820 int ret;
2821
2822 if (cnt >= sizeof(buf))
2823 return -EINVAL;
2824
2825 if (copy_from_user(&buf, ubuf, cnt))
2826 return -EFAULT;
2827
2828 buf[cnt] = 0;
2829
2830 ret = strict_strtol(buf, 10, &val);
2831 if (ret < 0)
2832 return ret;
2833
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002834 mutex_lock(&ftrace_lock);
Steven Rostedt978f3a42008-12-04 00:26:40 -05002835 if (val < 0) {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002836 /* disable pid tracing */
Steven Rostedt978f3a42008-12-04 00:26:40 -05002837 if (!ftrace_pid_trace)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002838 goto out;
Steven Rostedt978f3a42008-12-04 00:26:40 -05002839
2840 clear_ftrace_pid_task(&ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002841
2842 } else {
Steven Rostedte32d8952008-12-04 00:26:41 -05002843 /* swapper task is special */
2844 if (!val) {
2845 pid = ftrace_swapper_pid;
2846 if (pid == ftrace_pid_trace)
2847 goto out;
2848 } else {
2849 pid = find_get_pid(val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002850
Steven Rostedte32d8952008-12-04 00:26:41 -05002851 if (pid == ftrace_pid_trace) {
2852 put_pid(pid);
2853 goto out;
2854 }
Steven Rostedt978f3a42008-12-04 00:26:40 -05002855 }
2856
2857 if (ftrace_pid_trace)
2858 clear_ftrace_pid_task(&ftrace_pid_trace);
2859
2860 if (!pid)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002861 goto out;
2862
Steven Rostedt978f3a42008-12-04 00:26:40 -05002863 ftrace_pid_trace = pid;
Steven Rostedt0ef8cde2008-12-03 15:36:58 -05002864
Steven Rostedt978f3a42008-12-04 00:26:40 -05002865 set_ftrace_pid_task(ftrace_pid_trace);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002866 }
2867
2868 /* update the function call */
2869 ftrace_update_pid_func();
2870 ftrace_startup_enable(0);
2871
2872 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002873 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002874
2875 return cnt;
2876}
2877
Steven Rostedt5e2336a02009-03-05 21:44:55 -05002878static const struct file_operations ftrace_pid_fops = {
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002879 .read = ftrace_pid_read,
2880 .write = ftrace_pid_write,
2881};
2882
2883static __init int ftrace_init_debugfs(void)
2884{
2885 struct dentry *d_tracer;
2886 struct dentry *entry;
2887
2888 d_tracer = tracing_init_dentry();
2889 if (!d_tracer)
2890 return 0;
2891
2892 ftrace_init_dyn_debugfs(d_tracer);
2893
2894 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2895 NULL, &ftrace_pid_fops);
2896 if (!entry)
2897 pr_warning("Could not create debugfs "
2898 "'set_ftrace_pid' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04002899
2900 ftrace_profile_debugfs(d_tracer);
2901
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002902 return 0;
2903}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002904fs_initcall(ftrace_init_debugfs);
2905
Steven Rostedt3d083392008-05-12 21:20:42 +02002906/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002907 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002908 *
2909 * This function should be used by panic code. It stops ftrace
2910 * but in a not so nice way. If you need to simply kill ftrace
2911 * from a non-atomic section, use ftrace_kill.
2912 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04002913void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002914{
2915 ftrace_disabled = 1;
2916 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04002917 clear_ftrace_function();
2918}
2919
2920/**
Steven Rostedt3d083392008-05-12 21:20:42 +02002921 * register_ftrace_function - register a function for profiling
2922 * @ops - ops structure that holds the function for profiling.
2923 *
2924 * Register a function to be called by all functions in the
2925 * kernel.
2926 *
2927 * Note: @ops->func and all the functions it calls must be labeled
2928 * with "notrace", otherwise it will go into a
2929 * recursive loop.
2930 */
2931int register_ftrace_function(struct ftrace_ops *ops)
2932{
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002933 int ret;
2934
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002935 if (unlikely(ftrace_disabled))
2936 return -1;
2937
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002938 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002939
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002940 ret = __register_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002941 ftrace_startup(0);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002942
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002943 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002944 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02002945}
2946
2947/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01002948 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02002949 * @ops - ops structure that holds the function to unregister
2950 *
2951 * Unregister a function that was added to be called by ftrace profiling.
2952 */
2953int unregister_ftrace_function(struct ftrace_ops *ops)
2954{
2955 int ret;
2956
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002957 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02002958 ret = __unregister_ftrace_function(ops);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002959 ftrace_shutdown(0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002960 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002961
2962 return ret;
2963}
2964
Ingo Molnare309b412008-05-12 21:20:51 +02002965int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002966ftrace_enable_sysctl(struct ctl_table *table, int write,
Steven Rostedt5072c592008-05-12 21:20:43 +02002967 struct file *file, void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002968 loff_t *ppos)
2969{
2970 int ret;
2971
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002972 if (unlikely(ftrace_disabled))
2973 return -ENODEV;
2974
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002975 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002976
Steven Rostedt5072c592008-05-12 21:20:43 +02002977 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002978
2979 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2980 goto out;
2981
2982 last_ftrace_enabled = ftrace_enabled;
2983
2984 if (ftrace_enabled) {
2985
2986 ftrace_startup_sysctl();
2987
2988 /* we are starting ftrace again */
2989 if (ftrace_list != &ftrace_list_end) {
2990 if (ftrace_list->next == &ftrace_list_end)
2991 ftrace_trace_function = ftrace_list->func;
2992 else
2993 ftrace_trace_function = ftrace_list_func;
2994 }
2995
2996 } else {
2997 /* stopping ftrace calls (just send to ftrace_stub) */
2998 ftrace_trace_function = ftrace_stub;
2999
3000 ftrace_shutdown_sysctl();
3001 }
3002
3003 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003004 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02003005 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003006}
Ingo Molnarf17845e2008-10-24 12:47:10 +02003007
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003008#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003009
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003010static atomic_t ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003011static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003012
Steven Rostedte49dc192008-12-02 23:50:05 -05003013int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3014{
3015 return 0;
3016}
3017
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003018/* The callbacks that hook a function */
3019trace_func_graph_ret_t ftrace_graph_return =
3020 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003021trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003022
3023/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3024static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3025{
3026 int i;
3027 int ret = 0;
3028 unsigned long flags;
3029 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3030 struct task_struct *g, *t;
3031
3032 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3033 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3034 * sizeof(struct ftrace_ret_stack),
3035 GFP_KERNEL);
3036 if (!ret_stack_list[i]) {
3037 start = 0;
3038 end = i;
3039 ret = -ENOMEM;
3040 goto free;
3041 }
3042 }
3043
3044 read_lock_irqsave(&tasklist_lock, flags);
3045 do_each_thread(g, t) {
3046 if (start == end) {
3047 ret = -EAGAIN;
3048 goto unlock;
3049 }
3050
3051 if (t->ret_stack == NULL) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003052 t->curr_ret_stack = -1;
Frederic Weisbecker48d68b22008-12-02 00:20:39 +01003053 /* Make sure IRQs see the -1 first: */
3054 barrier();
3055 t->ret_stack = ret_stack_list[start++];
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003056 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003057 atomic_set(&t->trace_overrun, 0);
3058 }
3059 } while_each_thread(g, t);
3060
3061unlock:
3062 read_unlock_irqrestore(&tasklist_lock, flags);
3063free:
3064 for (i = start; i < end; i++)
3065 kfree(ret_stack_list[i]);
3066 return ret;
3067}
3068
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003069static void
3070ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3071 struct task_struct *next)
3072{
3073 unsigned long long timestamp;
3074 int index;
3075
Steven Rostedtbe6f1642009-03-24 11:06:24 -04003076 /*
3077 * Does the user want to count the time a function was asleep.
3078 * If so, do not update the time stamps.
3079 */
3080 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3081 return;
3082
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003083 timestamp = trace_clock_local();
3084
3085 prev->ftrace_timestamp = timestamp;
3086
3087 /* only process tasks that we timestamped */
3088 if (!next->ftrace_timestamp)
3089 return;
3090
3091 /*
3092 * Update all the counters in next to make up for the
3093 * time next was sleeping.
3094 */
3095 timestamp -= next->ftrace_timestamp;
3096
3097 for (index = next->curr_ret_stack; index >= 0; index--)
3098 next->ret_stack[index].calltime += timestamp;
3099}
3100
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003101/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003102static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003103{
3104 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003105 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003106
3107 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3108 sizeof(struct ftrace_ret_stack *),
3109 GFP_KERNEL);
3110
3111 if (!ret_stack_list)
3112 return -ENOMEM;
3113
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01003114 /* The cpu_boot init_task->ret_stack will never be freed */
3115 for_each_online_cpu(cpu)
3116 ftrace_graph_init_task(idle_task(cpu));
3117
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003118 do {
3119 ret = alloc_retstack_tasklist(ret_stack_list);
3120 } while (ret == -EAGAIN);
3121
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003122 if (!ret) {
3123 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3124 if (ret)
3125 pr_info("ftrace_graph: Couldn't activate tracepoint"
3126 " probe to kernel_sched_switch\n");
3127 }
3128
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003129 kfree(ret_stack_list);
3130 return ret;
3131}
3132
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003133/*
3134 * Hibernation protection.
3135 * The state of the current task is too much unstable during
3136 * suspend/restore to disk. We want to protect against that.
3137 */
3138static int
3139ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3140 void *unused)
3141{
3142 switch (state) {
3143 case PM_HIBERNATION_PREPARE:
3144 pause_graph_tracing();
3145 break;
3146
3147 case PM_POST_HIBERNATION:
3148 unpause_graph_tracing();
3149 break;
3150 }
3151 return NOTIFY_DONE;
3152}
3153
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003154int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3155 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003156{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003157 int ret = 0;
3158
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003159 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003160
Steven Rostedt05ce5812009-03-24 00:18:31 -04003161 /* we currently allow only one tracer registered at a time */
3162 if (atomic_read(&ftrace_graph_active)) {
3163 ret = -EBUSY;
3164 goto out;
3165 }
3166
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003167 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3168 register_pm_notifier(&ftrace_suspend_notifier);
3169
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003170 atomic_inc(&ftrace_graph_active);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003171 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003172 if (ret) {
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003173 atomic_dec(&ftrace_graph_active);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003174 goto out;
3175 }
Steven Rostedte53a6312008-11-26 00:16:25 -05003176
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003177 ftrace_graph_return = retfunc;
3178 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05003179
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003180 ftrace_startup(FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003181
3182out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003183 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003184 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003185}
3186
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003187void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003188{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003189 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003190
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003191 atomic_dec(&ftrace_graph_active);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003192 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003193 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05003194 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003195 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08003196 unregister_pm_notifier(&ftrace_suspend_notifier);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01003197
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003198 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003199}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003200
3201/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003202void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003203{
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01003204 if (atomic_read(&ftrace_graph_active)) {
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003205 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3206 * sizeof(struct ftrace_ret_stack),
3207 GFP_KERNEL);
3208 if (!t->ret_stack)
3209 return;
3210 t->curr_ret_stack = -1;
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003211 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003212 atomic_set(&t->trace_overrun, 0);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04003213 t->ftrace_timestamp = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003214 } else
3215 t->ret_stack = NULL;
3216}
3217
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01003218void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003219{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003220 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3221
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003222 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01003223 /* NULL must become visible to IRQs before we free it: */
3224 barrier();
3225
3226 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01003227}
Steven Rostedt14a866c2008-12-02 23:50:02 -05003228
3229void ftrace_graph_stop(void)
3230{
3231 ftrace_stop();
3232}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01003233#endif
3234