blob: 789cbec24e8135eda34ad829008a3de9b1d81100 [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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
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>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040067static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -040069 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040070};
71
Steven Rostedt4eebcc82008-05-12 21:20:48 +020072/* ftrace_enabled is a method to turn ftrace on or off */
73int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020074static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020075
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050076/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077int function_trace_stop __read_mostly;
78
79/* Current function tracing op */
80struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050081
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040082/* List for set_ftrace_pid's pids. */
83LIST_HEAD(ftrace_pids);
84struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
87};
88
Steven Rostedt4eebcc82008-05-12 21:20:48 +020089/*
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
92 */
93static int ftrace_disabled __read_mostly;
94
Steven Rostedt52baf112009-02-14 01:15:39 -050095static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010098static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040099static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200100ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500101ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400102static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100103static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400105#if ARCH_SUPPORTS_FTRACE_OPS
106static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400108#else
109/* See comment below, where ftrace_ops_list_func is defined */
110static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400113
Steven Rostedtea701f12012-07-20 13:08:05 -0400114/**
115 * ftrace_nr_registered_ops - return number of ops registered
116 *
117 * Returns the number of ftrace_ops registered and tracing functions
118 */
119int ftrace_nr_registered_ops(void)
120{
121 struct ftrace_ops *ops;
122 int cnt = 0;
123
124 mutex_lock(&ftrace_lock);
125
126 for (ops = ftrace_ops_list;
127 ops != &ftrace_list_end; ops = ops->next)
128 cnt++;
129
130 mutex_unlock(&ftrace_lock);
131
132 return cnt;
133}
134
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800135/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400136 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800137 * can use rcu_dereference_raw() is that elements removed from this list
138 * are simply leaked, so there is no need to interact with a grace-period
139 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400140 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800141 *
142 * Silly Alpha and silly pointer-speculation compiler optimizations!
143 */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400144static void
145ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400146 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400148 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
149 return;
150
151 trace_recursion_set(TRACE_GLOBAL_BIT);
152 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200153 while (op != &ftrace_list_end) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400154 op->func(ip, parent_ip, op, regs);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800155 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400157 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158}
159
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400160static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400161 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500162{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500163 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500164 return;
165
Steven Rostedta1e2e312011-08-09 12:50:46 -0400166 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500167}
168
169static void set_ftrace_pid_function(ftrace_func_t func)
170{
171 /* do not set ftrace_pid_function to itself! */
172 if (func != ftrace_pid_func)
173 ftrace_pid_function = func;
174}
175
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200176/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200177 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200178 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200179 * This NULLs the ftrace function and in essence stops
180 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200181 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200182void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200183{
Steven Rostedt3d083392008-05-12 21:20:42 +0200184 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500185 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200186}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187
Jiri Olsae2484912012-02-15 15:51:48 +0100188static void control_ops_disable_all(struct ftrace_ops *ops)
189{
190 int cpu;
191
192 for_each_possible_cpu(cpu)
193 *per_cpu_ptr(ops->disabled, cpu) = 1;
194}
195
196static int control_ops_alloc(struct ftrace_ops *ops)
197{
198 int __percpu *disabled;
199
200 disabled = alloc_percpu(int);
201 if (!disabled)
202 return -ENOMEM;
203
204 ops->disabled = disabled;
205 control_ops_disable_all(ops);
206 return 0;
207}
208
209static void control_ops_free(struct ftrace_ops *ops)
210{
211 free_percpu(ops->disabled);
212}
213
Steven Rostedt2b499382011-05-03 22:49:52 -0400214static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400215{
216 ftrace_func_t func;
217
218 /*
219 * If there's only one function registered, then call that
220 * function directly. Otherwise, we need to iterate over the
221 * registered callers.
222 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400223 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400224 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400225 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400226 /*
227 * As we are calling the function directly.
228 * If it does not have recursion protection,
229 * the function_trace_op needs to be updated
230 * accordingly.
231 */
232 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
233 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
234 else
235 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
236 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400237 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400238 /* The list has its own recursion protection. */
239 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
240 }
241
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400242
243 /* If we filter on pids, update to use the pid function */
244 if (!list_empty(&ftrace_pids)) {
245 set_ftrace_pid_function(func);
246 func = ftrace_pid_func;
247 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400248
249 global_ops.func = func;
250}
251
252static void update_ftrace_function(void)
253{
254 ftrace_func_t func;
255
256 update_global_ops();
257
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400258 /*
259 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400260 * recursion safe and not dynamic and the arch supports passing ops,
261 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400262 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400263 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400264 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400265 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400266 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400267 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400268 /* Set the ftrace_ops that the arch callback uses */
269 if (ftrace_ops_list == &global_ops)
270 function_trace_op = ftrace_global_list;
271 else
272 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400273 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400274 } else {
275 /* Just use the default ftrace_ops */
276 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400277 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400278 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400279
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400280 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400281}
282
Steven Rostedt2b499382011-05-03 22:49:52 -0400283static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200284{
Steven Rostedt2b499382011-05-03 22:49:52 -0400285 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200286 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400287 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200288 * CPU might be walking that list. We need to make sure
289 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400290 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200291 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400292 rcu_assign_pointer(*list, ops);
293}
Steven Rostedt3d083392008-05-12 21:20:42 +0200294
Steven Rostedt2b499382011-05-03 22:49:52 -0400295static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
296{
297 struct ftrace_ops **p;
298
299 /*
300 * If we are removing the last function, then simply point
301 * to the ftrace_stub.
302 */
303 if (*list == ops && ops->next == &ftrace_list_end) {
304 *list = &ftrace_list_end;
305 return 0;
306 }
307
308 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
309 if (*p == ops)
310 break;
311
312 if (*p != ops)
313 return -1;
314
315 *p = (*p)->next;
316 return 0;
317}
318
Jiri Olsae2484912012-02-15 15:51:48 +0100319static void add_ftrace_list_ops(struct ftrace_ops **list,
320 struct ftrace_ops *main_ops,
321 struct ftrace_ops *ops)
322{
323 int first = *list == &ftrace_list_end;
324 add_ftrace_ops(list, ops);
325 if (first)
326 add_ftrace_ops(&ftrace_ops_list, main_ops);
327}
328
329static int remove_ftrace_list_ops(struct ftrace_ops **list,
330 struct ftrace_ops *main_ops,
331 struct ftrace_ops *ops)
332{
333 int ret = remove_ftrace_ops(list, ops);
334 if (!ret && *list == &ftrace_list_end)
335 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
336 return ret;
337}
338
Steven Rostedt2b499382011-05-03 22:49:52 -0400339static int __register_ftrace_function(struct ftrace_ops *ops)
340{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200341 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400342 return -ENODEV;
343
344 if (FTRACE_WARN_ON(ops == &global_ops))
345 return -EINVAL;
346
Steven Rostedtb8489142011-05-04 09:27:52 -0400347 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
348 return -EBUSY;
349
Jiri Olsae2484912012-02-15 15:51:48 +0100350 /* We don't support both control and global flags set. */
351 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
352 return -EINVAL;
353
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900354#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400355 /*
356 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
357 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
358 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
359 */
360 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
361 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
362 return -EINVAL;
363
364 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
365 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
366#endif
367
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400368 if (!core_kernel_data((unsigned long)ops))
369 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
370
Steven Rostedtb8489142011-05-04 09:27:52 -0400371 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100372 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400373 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100374 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
375 if (control_ops_alloc(ops))
376 return -ENOMEM;
377 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400378 } else
379 add_ftrace_ops(&ftrace_ops_list, ops);
380
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400381 if (ftrace_enabled)
382 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200383
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200384 return 0;
385}
386
Ingo Molnare309b412008-05-12 21:20:51 +0200387static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200388{
Steven Rostedt2b499382011-05-03 22:49:52 -0400389 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200390
Steven Rostedt2b499382011-05-03 22:49:52 -0400391 if (ftrace_disabled)
392 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200393
Steven Rostedtb8489142011-05-04 09:27:52 -0400394 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
395 return -EBUSY;
396
Steven Rostedt2b499382011-05-03 22:49:52 -0400397 if (FTRACE_WARN_ON(ops == &global_ops))
398 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200399
Steven Rostedtb8489142011-05-04 09:27:52 -0400400 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100401 ret = remove_ftrace_list_ops(&ftrace_global_list,
402 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400403 if (!ret)
404 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100405 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
406 ret = remove_ftrace_list_ops(&ftrace_control_list,
407 &control_ops, ops);
408 if (!ret) {
409 /*
410 * The ftrace_ops is now removed from the list,
411 * so there'll be no new users. We must ensure
412 * all current users are done before we free
413 * the control data.
414 */
415 synchronize_sched();
416 control_ops_free(ops);
417 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400418 } else
419 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
420
Steven Rostedt2b499382011-05-03 22:49:52 -0400421 if (ret < 0)
422 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400423
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400424 if (ftrace_enabled)
425 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200426
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400427 /*
428 * Dynamic ops may be freed, we must make sure that all
429 * callers are done before leaving this function.
430 */
431 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
432 synchronize_sched();
433
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500434 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200435}
436
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500437static void ftrace_update_pid_func(void)
438{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400439 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500440 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900441 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500442
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400443 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500444}
445
Steven Rostedt493762f2009-03-23 17:12:36 -0400446#ifdef CONFIG_FUNCTION_PROFILER
447struct ftrace_profile {
448 struct hlist_node node;
449 unsigned long ip;
450 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400451#ifdef CONFIG_FUNCTION_GRAPH_TRACER
452 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400453 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400454#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400455};
456
457struct ftrace_profile_page {
458 struct ftrace_profile_page *next;
459 unsigned long index;
460 struct ftrace_profile records[];
461};
462
Steven Rostedtcafb1682009-03-24 20:50:39 -0400463struct ftrace_profile_stat {
464 atomic_t disabled;
465 struct hlist_head *hash;
466 struct ftrace_profile_page *pages;
467 struct ftrace_profile_page *start;
468 struct tracer_stat stat;
469};
470
Steven Rostedt493762f2009-03-23 17:12:36 -0400471#define PROFILE_RECORDS_SIZE \
472 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
473
474#define PROFILES_PER_PAGE \
475 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
476
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400477static int ftrace_profile_bits __read_mostly;
478static int ftrace_profile_enabled __read_mostly;
479
480/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400481static DEFINE_MUTEX(ftrace_profile_lock);
482
Steven Rostedtcafb1682009-03-24 20:50:39 -0400483static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400484
485#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
486
Steven Rostedt493762f2009-03-23 17:12:36 -0400487static void *
488function_stat_next(void *v, int idx)
489{
490 struct ftrace_profile *rec = v;
491 struct ftrace_profile_page *pg;
492
493 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
494
495 again:
Li Zefan0296e422009-06-26 11:15:37 +0800496 if (idx != 0)
497 rec++;
498
Steven Rostedt493762f2009-03-23 17:12:36 -0400499 if ((void *)rec >= (void *)&pg->records[pg->index]) {
500 pg = pg->next;
501 if (!pg)
502 return NULL;
503 rec = &pg->records[0];
504 if (!rec->counter)
505 goto again;
506 }
507
508 return rec;
509}
510
511static void *function_stat_start(struct tracer_stat *trace)
512{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513 struct ftrace_profile_stat *stat =
514 container_of(trace, struct ftrace_profile_stat, stat);
515
516 if (!stat || !stat->start)
517 return NULL;
518
519 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400520}
521
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400522#ifdef CONFIG_FUNCTION_GRAPH_TRACER
523/* function graph compares on total time */
524static int function_stat_cmp(void *p1, void *p2)
525{
526 struct ftrace_profile *a = p1;
527 struct ftrace_profile *b = p2;
528
529 if (a->time < b->time)
530 return -1;
531 if (a->time > b->time)
532 return 1;
533 else
534 return 0;
535}
536#else
537/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400538static int function_stat_cmp(void *p1, void *p2)
539{
540 struct ftrace_profile *a = p1;
541 struct ftrace_profile *b = p2;
542
543 if (a->counter < b->counter)
544 return -1;
545 if (a->counter > b->counter)
546 return 1;
547 else
548 return 0;
549}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400551
552static int function_stat_headers(struct seq_file *m)
553{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400555 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400556 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400557 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400558 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 seq_printf(m, " Function Hit\n"
561 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400562#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400563 return 0;
564}
565
566static int function_stat_show(struct seq_file *m, void *v)
567{
568 struct ftrace_profile *rec = v;
569 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800570 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400572 static struct trace_seq s;
573 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400574 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800576 mutex_lock(&ftrace_profile_lock);
577
578 /* we raced with function_profile_reset() */
579 if (unlikely(rec->counter == 0)) {
580 ret = -EBUSY;
581 goto out;
582 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400583
584 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400585 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400586
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400587#ifdef CONFIG_FUNCTION_GRAPH_TRACER
588 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400589 avg = rec->time;
590 do_div(avg, rec->counter);
591
Chase Douglase330b3b2010-04-26 14:02:05 -0400592 /* Sample standard deviation (s^2) */
593 if (rec->counter <= 1)
594 stddev = 0;
595 else {
596 stddev = rec->time_squared - rec->counter * avg * avg;
597 /*
598 * Divide only 1000 for ns^2 -> us^2 conversion.
599 * trace_print_graph_duration will divide 1000 again.
600 */
601 do_div(stddev, (rec->counter - 1) * 1000);
602 }
603
Steven Rostedt34886c82009-03-25 21:00:47 -0400604 trace_seq_init(&s);
605 trace_print_graph_duration(rec->time, &s);
606 trace_seq_puts(&s, " ");
607 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400608 trace_seq_puts(&s, " ");
609 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400610 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400611#endif
612 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800613out:
614 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400615
Li Zefan3aaba202010-08-23 16:50:12 +0800616 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400617}
618
Steven Rostedtcafb1682009-03-24 20:50:39 -0400619static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400620{
621 struct ftrace_profile_page *pg;
622
Steven Rostedtcafb1682009-03-24 20:50:39 -0400623 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400624
625 while (pg) {
626 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
627 pg->index = 0;
628 pg = pg->next;
629 }
630
Steven Rostedtcafb1682009-03-24 20:50:39 -0400631 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400632 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
633}
634
Steven Rostedtcafb1682009-03-24 20:50:39 -0400635int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400636{
637 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400638 int functions;
639 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400640 int i;
641
642 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400644 return 0;
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
647 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 return -ENOMEM;
649
Steven Rostedt318e0a72009-03-25 20:06:34 -0400650#ifdef CONFIG_DYNAMIC_FTRACE
651 functions = ftrace_update_tot_cnt;
652#else
653 /*
654 * We do not know the number of functions that exist because
655 * dynamic tracing is what counts them. With past experience
656 * we have around 20K functions. That should be more than enough.
657 * It is highly unlikely we will execute every function in
658 * the kernel.
659 */
660 functions = 20000;
661#endif
662
Steven Rostedtcafb1682009-03-24 20:50:39 -0400663 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400664
Steven Rostedt318e0a72009-03-25 20:06:34 -0400665 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
666
667 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400668 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400670 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 pg = pg->next;
672 }
673
674 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400675
676 out_free:
677 pg = stat->start;
678 while (pg) {
679 unsigned long tmp = (unsigned long)pg;
680
681 pg = pg->next;
682 free_page(tmp);
683 }
684
685 free_page((unsigned long)stat->pages);
686 stat->pages = NULL;
687 stat->start = NULL;
688
689 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690}
691
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400693{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695 int size;
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 stat = &per_cpu(ftrace_profile_stats, cpu);
698
699 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400702 return 0;
703 }
704
705 /*
706 * We are profiling all functions, but usually only a few thousand
707 * functions are hit. We'll make a hash of 1024 items.
708 */
709 size = FTRACE_PROFILE_HASH_SIZE;
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400712
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400714 return -ENOMEM;
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 if (!ftrace_profile_bits) {
717 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 for (; size; size >>= 1)
720 ftrace_profile_bits++;
721 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400722
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400724 if (ftrace_profile_pages_init(stat) < 0) {
725 kfree(stat->hash);
726 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400727 return -ENOMEM;
728 }
729
730 return 0;
731}
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733static int ftrace_profile_init(void)
734{
735 int cpu;
736 int ret = 0;
737
738 for_each_online_cpu(cpu) {
739 ret = ftrace_profile_init_cpu(cpu);
740 if (ret)
741 break;
742 }
743
744 return ret;
745}
746
Steven Rostedt493762f2009-03-23 17:12:36 -0400747/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748static struct ftrace_profile *
749ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400750{
751 struct ftrace_profile *rec;
752 struct hlist_head *hhd;
753 struct hlist_node *n;
754 unsigned long key;
755
756 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400757 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400758
759 if (hlist_empty(hhd))
760 return NULL;
761
762 hlist_for_each_entry_rcu(rec, n, hhd, node) {
763 if (rec->ip == ip)
764 return rec;
765 }
766
767 return NULL;
768}
769
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770static void ftrace_add_profile(struct ftrace_profile_stat *stat,
771 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400772{
773 unsigned long key;
774
775 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400777}
778
Steven Rostedt318e0a72009-03-25 20:06:34 -0400779/*
780 * The memory is already allocated, this simply finds a new record to use.
781 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400782static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400783ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400784{
785 struct ftrace_profile *rec = NULL;
786
Steven Rostedt318e0a72009-03-25 20:06:34 -0400787 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 goto out;
790
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400792 * Try to find the function again since an NMI
793 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400794 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400795 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400796 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400798
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799 if (stat->pages->index == PROFILES_PER_PAGE) {
800 if (!stat->pages->next)
801 goto out;
802 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 }
804
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400808
Steven Rostedt493762f2009-03-23 17:12:36 -0400809 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400811
812 return rec;
813}
814
Steven Rostedt493762f2009-03-23 17:12:36 -0400815static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400816function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400817 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400818{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 struct ftrace_profile *rec;
821 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400822
823 if (!ftrace_profile_enabled)
824 return;
825
Steven Rostedt493762f2009-03-23 17:12:36 -0400826 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827
828 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400829 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400830 goto out;
831
832 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400834 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 if (!rec)
836 goto out;
837 }
838
839 rec->counter++;
840 out:
841 local_irq_restore(flags);
842}
843
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400844#ifdef CONFIG_FUNCTION_GRAPH_TRACER
845static int profile_graph_entry(struct ftrace_graph_ent *trace)
846{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400847 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400848 return 1;
849}
850
851static void profile_graph_return(struct ftrace_graph_ret *trace)
852{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400854 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400855 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400857
858 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400859 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400860 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400861 goto out;
862
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400863 /* If the calltime was zero'd ignore it */
864 if (!trace->calltime)
865 goto out;
866
Steven Rostedta2a16d62009-03-24 23:17:58 -0400867 calltime = trace->rettime - trace->calltime;
868
869 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
870 int index;
871
872 index = trace->depth;
873
874 /* Append this call time to the parent time to subtract */
875 if (index)
876 current->ret_stack[index - 1].subtime += calltime;
877
878 if (current->ret_stack[index].subtime < calltime)
879 calltime -= current->ret_stack[index].subtime;
880 else
881 calltime = 0;
882 }
883
Steven Rostedtcafb1682009-03-24 20:50:39 -0400884 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400885 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400886 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400887 rec->time_squared += calltime * calltime;
888 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400889
Steven Rostedtcafb1682009-03-24 20:50:39 -0400890 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400891 local_irq_restore(flags);
892}
893
894static int register_ftrace_profiler(void)
895{
896 return register_ftrace_graph(&profile_graph_return,
897 &profile_graph_entry);
898}
899
900static void unregister_ftrace_profiler(void)
901{
902 unregister_ftrace_graph();
903}
904#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100905static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400906 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400907 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400908};
909
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400910static int register_ftrace_profiler(void)
911{
912 return register_ftrace_function(&ftrace_profile_ops);
913}
914
915static void unregister_ftrace_profiler(void)
916{
917 unregister_ftrace_function(&ftrace_profile_ops);
918}
919#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
920
Steven Rostedt493762f2009-03-23 17:12:36 -0400921static ssize_t
922ftrace_profile_write(struct file *filp, const char __user *ubuf,
923 size_t cnt, loff_t *ppos)
924{
925 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400926 int ret;
927
Peter Huewe22fe9b52011-06-07 21:58:27 +0200928 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
929 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400930 return ret;
931
932 val = !!val;
933
934 mutex_lock(&ftrace_profile_lock);
935 if (ftrace_profile_enabled ^ val) {
936 if (val) {
937 ret = ftrace_profile_init();
938 if (ret < 0) {
939 cnt = ret;
940 goto out;
941 }
942
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400943 ret = register_ftrace_profiler();
944 if (ret < 0) {
945 cnt = ret;
946 goto out;
947 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400948 ftrace_profile_enabled = 1;
949 } else {
950 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400951 /*
952 * unregister_ftrace_profiler calls stop_machine
953 * so this acts like an synchronize_sched.
954 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400955 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400956 }
957 }
958 out:
959 mutex_unlock(&ftrace_profile_lock);
960
Jiri Olsacf8517c2009-10-23 19:36:16 -0400961 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400962
963 return cnt;
964}
965
966static ssize_t
967ftrace_profile_read(struct file *filp, char __user *ubuf,
968 size_t cnt, loff_t *ppos)
969{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400970 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400971 int r;
972
973 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
974 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
975}
976
977static const struct file_operations ftrace_profile_fops = {
978 .open = tracing_open_generic,
979 .read = ftrace_profile_read,
980 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200981 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400982};
983
Steven Rostedtcafb1682009-03-24 20:50:39 -0400984/* used to initialize the real stat files */
985static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400986 .name = "functions",
987 .stat_start = function_stat_start,
988 .stat_next = function_stat_next,
989 .stat_cmp = function_stat_cmp,
990 .stat_headers = function_stat_headers,
991 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400992};
993
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400994static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400995{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400996 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400997 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400998 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400999 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001000 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001001
Steven Rostedtcafb1682009-03-24 20:50:39 -04001002 for_each_possible_cpu(cpu) {
1003 stat = &per_cpu(ftrace_profile_stats, cpu);
1004
1005 /* allocate enough for function name + cpu number */
1006 name = kmalloc(32, GFP_KERNEL);
1007 if (!name) {
1008 /*
1009 * The files created are permanent, if something happens
1010 * we still do not free memory.
1011 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001012 WARN(1,
1013 "Could not allocate stat file for cpu %d\n",
1014 cpu);
1015 return;
1016 }
1017 stat->stat = function_stats;
1018 snprintf(name, 32, "function%d", cpu);
1019 stat->stat.name = name;
1020 ret = register_stat_tracer(&stat->stat);
1021 if (ret) {
1022 WARN(1,
1023 "Could not register function stat for cpu %d\n",
1024 cpu);
1025 kfree(name);
1026 return;
1027 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001028 }
1029
1030 entry = debugfs_create_file("function_profile_enabled", 0644,
1031 d_tracer, NULL, &ftrace_profile_fops);
1032 if (!entry)
1033 pr_warning("Could not create debugfs "
1034 "'function_profile_enabled' entry\n");
1035}
1036
1037#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001038static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001039{
1040}
1041#endif /* CONFIG_FUNCTION_PROFILER */
1042
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001043static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1044
Steven Rostedt3d083392008-05-12 21:20:42 +02001045#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001046
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001047#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001048# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001049#endif
1050
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001051static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1052
Steven Rostedtb6887d72009-02-17 12:32:04 -05001053struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001054 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001055 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001056 unsigned long flags;
1057 unsigned long ip;
1058 void *data;
1059 struct rcu_head rcu;
1060};
1061
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001062struct ftrace_func_entry {
1063 struct hlist_node hlist;
1064 unsigned long ip;
1065};
1066
1067struct ftrace_hash {
1068 unsigned long size_bits;
1069 struct hlist_head *buckets;
1070 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001071 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001072};
1073
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001074/*
1075 * We make these constant because no one should touch them,
1076 * but they are used as the default "empty hash", to avoid allocating
1077 * it all the time. These are in a read only section such that if
1078 * anyone does try to modify it, it will cause an exception.
1079 */
1080static const struct hlist_head empty_buckets[1];
1081static const struct ftrace_hash empty_hash = {
1082 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001083};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001084#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001085
Steven Rostedt2b499382011-05-03 22:49:52 -04001086static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001087 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001088 .notrace_hash = EMPTY_HASH,
1089 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001090 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001091};
1092
Steven Rostedt41c52c02008-05-22 11:46:33 -04001093static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001094
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095struct ftrace_page {
1096 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001097 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001098 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001099 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001100};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001102static struct ftrace_page *ftrace_new_pgs;
1103
Steven Rostedta7900872011-12-16 16:23:44 -05001104#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1105#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001106
1107/* estimate from running different kernels */
1108#define NR_TO_INIT 10000
1109
1110static struct ftrace_page *ftrace_pages_start;
1111static struct ftrace_page *ftrace_pages;
1112
Steven Rostedt06a51d92011-12-19 19:07:36 -05001113static bool ftrace_hash_empty(struct ftrace_hash *hash)
1114{
1115 return !hash || !hash->count;
1116}
1117
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001118static struct ftrace_func_entry *
1119ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1120{
1121 unsigned long key;
1122 struct ftrace_func_entry *entry;
1123 struct hlist_head *hhd;
1124 struct hlist_node *n;
1125
Steven Rostedt06a51d92011-12-19 19:07:36 -05001126 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001127 return NULL;
1128
1129 if (hash->size_bits > 0)
1130 key = hash_long(ip, hash->size_bits);
1131 else
1132 key = 0;
1133
1134 hhd = &hash->buckets[key];
1135
1136 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1137 if (entry->ip == ip)
1138 return entry;
1139 }
1140 return NULL;
1141}
1142
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001143static void __add_hash_entry(struct ftrace_hash *hash,
1144 struct ftrace_func_entry *entry)
1145{
1146 struct hlist_head *hhd;
1147 unsigned long key;
1148
1149 if (hash->size_bits)
1150 key = hash_long(entry->ip, hash->size_bits);
1151 else
1152 key = 0;
1153
1154 hhd = &hash->buckets[key];
1155 hlist_add_head(&entry->hlist, hhd);
1156 hash->count++;
1157}
1158
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001159static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1160{
1161 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001162
1163 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1164 if (!entry)
1165 return -ENOMEM;
1166
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001167 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001168 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001169
1170 return 0;
1171}
1172
1173static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001174free_hash_entry(struct ftrace_hash *hash,
1175 struct ftrace_func_entry *entry)
1176{
1177 hlist_del(&entry->hlist);
1178 kfree(entry);
1179 hash->count--;
1180}
1181
1182static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001183remove_hash_entry(struct ftrace_hash *hash,
1184 struct ftrace_func_entry *entry)
1185{
1186 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001187 hash->count--;
1188}
1189
1190static void ftrace_hash_clear(struct ftrace_hash *hash)
1191{
1192 struct hlist_head *hhd;
1193 struct hlist_node *tp, *tn;
1194 struct ftrace_func_entry *entry;
1195 int size = 1 << hash->size_bits;
1196 int i;
1197
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001198 if (!hash->count)
1199 return;
1200
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001201 for (i = 0; i < size; i++) {
1202 hhd = &hash->buckets[i];
1203 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001204 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205 }
1206 FTRACE_WARN_ON(hash->count);
1207}
1208
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001209static void free_ftrace_hash(struct ftrace_hash *hash)
1210{
1211 if (!hash || hash == EMPTY_HASH)
1212 return;
1213 ftrace_hash_clear(hash);
1214 kfree(hash->buckets);
1215 kfree(hash);
1216}
1217
Steven Rostedt07fd5512011-05-05 18:03:47 -04001218static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1219{
1220 struct ftrace_hash *hash;
1221
1222 hash = container_of(rcu, struct ftrace_hash, rcu);
1223 free_ftrace_hash(hash);
1224}
1225
1226static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1227{
1228 if (!hash || hash == EMPTY_HASH)
1229 return;
1230 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1231}
1232
Jiri Olsa5500fa52012-02-15 15:51:54 +01001233void ftrace_free_filter(struct ftrace_ops *ops)
1234{
1235 free_ftrace_hash(ops->filter_hash);
1236 free_ftrace_hash(ops->notrace_hash);
1237}
1238
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001239static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1240{
1241 struct ftrace_hash *hash;
1242 int size;
1243
1244 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1245 if (!hash)
1246 return NULL;
1247
1248 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001249 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001250
1251 if (!hash->buckets) {
1252 kfree(hash);
1253 return NULL;
1254 }
1255
1256 hash->size_bits = size_bits;
1257
1258 return hash;
1259}
1260
1261static struct ftrace_hash *
1262alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1263{
1264 struct ftrace_func_entry *entry;
1265 struct ftrace_hash *new_hash;
1266 struct hlist_node *tp;
1267 int size;
1268 int ret;
1269 int i;
1270
1271 new_hash = alloc_ftrace_hash(size_bits);
1272 if (!new_hash)
1273 return NULL;
1274
1275 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001276 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001277 return new_hash;
1278
1279 size = 1 << hash->size_bits;
1280 for (i = 0; i < size; i++) {
1281 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1282 ret = add_hash_entry(new_hash, entry->ip);
1283 if (ret < 0)
1284 goto free_hash;
1285 }
1286 }
1287
1288 FTRACE_WARN_ON(new_hash->count != hash->count);
1289
1290 return new_hash;
1291
1292 free_hash:
1293 free_ftrace_hash(new_hash);
1294 return NULL;
1295}
1296
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001297static void
1298ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1299static void
1300ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1301
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001303ftrace_hash_move(struct ftrace_ops *ops, int enable,
1304 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305{
1306 struct ftrace_func_entry *entry;
1307 struct hlist_node *tp, *tn;
1308 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001309 struct ftrace_hash *old_hash;
1310 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001311 unsigned long key;
1312 int size = src->count;
1313 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001314 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315 int i;
1316
1317 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001318 * Remove the current set, update the hash and add
1319 * them back.
1320 */
1321 ftrace_hash_rec_disable(ops, enable);
1322
1323 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324 * If the new source is empty, just free dst and assign it
1325 * the empty_hash.
1326 */
1327 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001328 free_ftrace_hash_rcu(*dst);
1329 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001330 /* still need to update the function records */
1331 ret = 0;
1332 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 }
1334
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001335 /*
1336 * Make the hash size about 1/2 the # found
1337 */
1338 for (size /= 2; size; size >>= 1)
1339 bits++;
1340
1341 /* Don't allocate too much */
1342 if (bits > FTRACE_HASH_MAX_BITS)
1343 bits = FTRACE_HASH_MAX_BITS;
1344
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001345 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001346 new_hash = alloc_ftrace_hash(bits);
1347 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001348 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349
1350 size = 1 << src->size_bits;
1351 for (i = 0; i < size; i++) {
1352 hhd = &src->buckets[i];
1353 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1354 if (bits > 0)
1355 key = hash_long(entry->ip, bits);
1356 else
1357 key = 0;
1358 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001359 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360 }
1361 }
1362
Steven Rostedt07fd5512011-05-05 18:03:47 -04001363 old_hash = *dst;
1364 rcu_assign_pointer(*dst, new_hash);
1365 free_ftrace_hash_rcu(old_hash);
1366
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001367 ret = 0;
1368 out:
1369 /*
1370 * Enable regardless of ret:
1371 * On success, we enable the new hash.
1372 * On failure, we re-enable the original hash.
1373 */
1374 ftrace_hash_rec_enable(ops, enable);
1375
1376 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001377}
1378
Steven Rostedt265c8312009-02-13 12:43:56 -05001379/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001380 * Test the hashes for this ops to see if we want to call
1381 * the ops->func or not.
1382 *
1383 * It's a match if the ip is in the ops->filter_hash or
1384 * the filter_hash does not exist or is empty,
1385 * AND
1386 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001387 *
1388 * This needs to be called with preemption disabled as
1389 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001390 */
1391static int
1392ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1393{
1394 struct ftrace_hash *filter_hash;
1395 struct ftrace_hash *notrace_hash;
1396 int ret;
1397
Steven Rostedtb8489142011-05-04 09:27:52 -04001398 filter_hash = rcu_dereference_raw(ops->filter_hash);
1399 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1400
Steven Rostedt06a51d92011-12-19 19:07:36 -05001401 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001402 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001403 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001404 !ftrace_lookup_ip(notrace_hash, ip)))
1405 ret = 1;
1406 else
1407 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001408
1409 return ret;
1410}
1411
1412/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001413 * This is a double for. Do not use 'break' to break out of the loop,
1414 * you must use a goto.
1415 */
1416#define do_for_each_ftrace_rec(pg, rec) \
1417 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1418 int _____i; \
1419 for (_____i = 0; _____i < pg->index; _____i++) { \
1420 rec = &pg->records[_____i];
1421
1422#define while_for_each_ftrace_rec() \
1423 } \
1424 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301425
Steven Rostedt5855fea2011-12-16 19:27:42 -05001426
1427static int ftrace_cmp_recs(const void *a, const void *b)
1428{
Steven Rostedta650e022012-04-25 13:48:13 -04001429 const struct dyn_ftrace *key = a;
1430 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001431
Steven Rostedta650e022012-04-25 13:48:13 -04001432 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001433 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001434 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1435 return 1;
1436 return 0;
1437}
1438
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001439static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001440{
1441 struct ftrace_page *pg;
1442 struct dyn_ftrace *rec;
1443 struct dyn_ftrace key;
1444
1445 key.ip = start;
1446 key.flags = end; /* overload flags, as it is unsigned long */
1447
1448 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1449 if (end < pg->records[0].ip ||
1450 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1451 continue;
1452 rec = bsearch(&key, pg->records, pg->index,
1453 sizeof(struct dyn_ftrace),
1454 ftrace_cmp_recs);
1455 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001456 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001457 }
1458
Steven Rostedt5855fea2011-12-16 19:27:42 -05001459 return 0;
1460}
1461
Steven Rostedtc88fd862011-08-16 09:53:39 -04001462/**
1463 * ftrace_location - return true if the ip giving is a traced location
1464 * @ip: the instruction pointer to check
1465 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001466 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001467 * That is, the instruction that is either a NOP or call to
1468 * the function tracer. It checks the ftrace internal tables to
1469 * determine if the address belongs or not.
1470 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001471unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001472{
Steven Rostedta650e022012-04-25 13:48:13 -04001473 return ftrace_location_range(ip, ip);
1474}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001475
Steven Rostedta650e022012-04-25 13:48:13 -04001476/**
1477 * ftrace_text_reserved - return true if range contains an ftrace location
1478 * @start: start of range to search
1479 * @end: end of range to search (inclusive). @end points to the last byte to check.
1480 *
1481 * Returns 1 if @start and @end contains a ftrace location.
1482 * That is, the instruction that is either a NOP or call to
1483 * the function tracer. It checks the ftrace internal tables to
1484 * determine if the address belongs or not.
1485 */
1486int ftrace_text_reserved(void *start, void *end)
1487{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001488 unsigned long ret;
1489
1490 ret = ftrace_location_range((unsigned long)start,
1491 (unsigned long)end);
1492
1493 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001494}
1495
Steven Rostedted926f92011-05-03 13:25:24 -04001496static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1497 int filter_hash,
1498 bool inc)
1499{
1500 struct ftrace_hash *hash;
1501 struct ftrace_hash *other_hash;
1502 struct ftrace_page *pg;
1503 struct dyn_ftrace *rec;
1504 int count = 0;
1505 int all = 0;
1506
1507 /* Only update if the ops has been registered */
1508 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1509 return;
1510
1511 /*
1512 * In the filter_hash case:
1513 * If the count is zero, we update all records.
1514 * Otherwise we just update the items in the hash.
1515 *
1516 * In the notrace_hash case:
1517 * We enable the update in the hash.
1518 * As disabling notrace means enabling the tracing,
1519 * and enabling notrace means disabling, the inc variable
1520 * gets inversed.
1521 */
1522 if (filter_hash) {
1523 hash = ops->filter_hash;
1524 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001525 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001526 all = 1;
1527 } else {
1528 inc = !inc;
1529 hash = ops->notrace_hash;
1530 other_hash = ops->filter_hash;
1531 /*
1532 * If the notrace hash has no items,
1533 * then there's nothing to do.
1534 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001535 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001536 return;
1537 }
1538
1539 do_for_each_ftrace_rec(pg, rec) {
1540 int in_other_hash = 0;
1541 int in_hash = 0;
1542 int match = 0;
1543
1544 if (all) {
1545 /*
1546 * Only the filter_hash affects all records.
1547 * Update if the record is not in the notrace hash.
1548 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001549 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001550 match = 1;
1551 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001552 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1553 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001554
1555 /*
1556 *
1557 */
1558 if (filter_hash && in_hash && !in_other_hash)
1559 match = 1;
1560 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001561 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001562 match = 1;
1563 }
1564 if (!match)
1565 continue;
1566
1567 if (inc) {
1568 rec->flags++;
1569 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1570 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001571 /*
1572 * If any ops wants regs saved for this function
1573 * then all ops will get saved regs.
1574 */
1575 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1576 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001577 } else {
1578 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1579 return;
1580 rec->flags--;
1581 }
1582 count++;
1583 /* Shortcut, if we handled all records, we are done. */
1584 if (!all && count == hash->count)
1585 return;
1586 } while_for_each_ftrace_rec();
1587}
1588
1589static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1590 int filter_hash)
1591{
1592 __ftrace_hash_rec_update(ops, filter_hash, 0);
1593}
1594
1595static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1596 int filter_hash)
1597{
1598 __ftrace_hash_rec_update(ops, filter_hash, 1);
1599}
1600
Steven Rostedt05736a42008-09-22 14:55:47 -07001601static void print_ip_ins(const char *fmt, unsigned char *p)
1602{
1603 int i;
1604
1605 printk(KERN_CONT "%s", fmt);
1606
1607 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1608 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1609}
1610
Steven Rostedtc88fd862011-08-16 09:53:39 -04001611/**
1612 * ftrace_bug - report and shutdown function tracer
1613 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1614 * @ip: The address that failed
1615 *
1616 * The arch code that enables or disables the function tracing
1617 * can call ftrace_bug() when it has detected a problem in
1618 * modifying the code. @failed should be one of either:
1619 * EFAULT - if the problem happens on reading the @ip address
1620 * EINVAL - if what is read at @ip is not what was expected
1621 * EPERM - if the problem happens on writting to the @ip address
1622 */
1623void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001624{
1625 switch (failed) {
1626 case -EFAULT:
1627 FTRACE_WARN_ON_ONCE(1);
1628 pr_info("ftrace faulted on modifying ");
1629 print_ip_sym(ip);
1630 break;
1631 case -EINVAL:
1632 FTRACE_WARN_ON_ONCE(1);
1633 pr_info("ftrace failed to modify ");
1634 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001635 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001636 printk(KERN_CONT "\n");
1637 break;
1638 case -EPERM:
1639 FTRACE_WARN_ON_ONCE(1);
1640 pr_info("ftrace faulted on writing ");
1641 print_ip_sym(ip);
1642 break;
1643 default:
1644 FTRACE_WARN_ON_ONCE(1);
1645 pr_info("ftrace faulted on unknown error ");
1646 print_ip_sym(ip);
1647 }
1648}
1649
Steven Rostedtc88fd862011-08-16 09:53:39 -04001650static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001651{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001652 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001653
Steven Rostedt982c3502008-11-15 16:31:41 -05001654 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001655 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001656 *
Steven Rostedted926f92011-05-03 13:25:24 -04001657 * If the record has a ref count, then we need to enable it
1658 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001659 *
Steven Rostedted926f92011-05-03 13:25:24 -04001660 * Otherwise we make sure its disabled.
1661 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001662 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001663 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001664 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001665 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001666 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001667
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001668 /*
1669 * If enabling and the REGS flag does not match the REGS_EN, then
1670 * do not ignore this record. Set flags to fail the compare against
1671 * ENABLED.
1672 */
1673 if (flag &&
1674 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1675 flag |= FTRACE_FL_REGS;
1676
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001677 /* If the state of this record hasn't changed, then do nothing */
1678 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001679 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001680
1681 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001682 /* Save off if rec is being enabled (for return value) */
1683 flag ^= rec->flags & FTRACE_FL_ENABLED;
1684
1685 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001686 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001687 if (flag & FTRACE_FL_REGS) {
1688 if (rec->flags & FTRACE_FL_REGS)
1689 rec->flags |= FTRACE_FL_REGS_EN;
1690 else
1691 rec->flags &= ~FTRACE_FL_REGS_EN;
1692 }
1693 }
1694
1695 /*
1696 * If this record is being updated from a nop, then
1697 * return UPDATE_MAKE_CALL.
1698 * Otherwise, if the EN flag is set, then return
1699 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1700 * from the non-save regs, to a save regs function.
1701 * Otherwise,
1702 * return UPDATE_MODIFY_CALL to tell the caller to convert
1703 * from the save regs, to a non-save regs function.
1704 */
1705 if (flag & FTRACE_FL_ENABLED)
1706 return FTRACE_UPDATE_MAKE_CALL;
1707 else if (rec->flags & FTRACE_FL_REGS_EN)
1708 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1709 else
1710 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001711 }
1712
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001713 if (update) {
1714 /* If there's no more users, clear all flags */
1715 if (!(rec->flags & ~FTRACE_FL_MASK))
1716 rec->flags = 0;
1717 else
1718 /* Just disable the record (keep REGS state) */
1719 rec->flags &= ~FTRACE_FL_ENABLED;
1720 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001721
1722 return FTRACE_UPDATE_MAKE_NOP;
1723}
1724
1725/**
1726 * ftrace_update_record, set a record that now is tracing or not
1727 * @rec: the record to update
1728 * @enable: set to 1 if the record is tracing, zero to force disable
1729 *
1730 * The records that represent all functions that can be traced need
1731 * to be updated when tracing has been enabled.
1732 */
1733int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1734{
1735 return ftrace_check_record(rec, enable, 1);
1736}
1737
1738/**
1739 * ftrace_test_record, check if the record has been enabled or not
1740 * @rec: the record to test
1741 * @enable: set to 1 to check if enabled, 0 if it is disabled
1742 *
1743 * The arch code may need to test if a record is already set to
1744 * tracing to determine how to modify the function code that it
1745 * represents.
1746 */
1747int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1748{
1749 return ftrace_check_record(rec, enable, 0);
1750}
1751
1752static int
1753__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1754{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001755 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001756 unsigned long ftrace_addr;
1757 int ret;
1758
Steven Rostedtc88fd862011-08-16 09:53:39 -04001759 ret = ftrace_update_record(rec, enable);
1760
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001761 if (rec->flags & FTRACE_FL_REGS)
1762 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1763 else
1764 ftrace_addr = (unsigned long)FTRACE_ADDR;
1765
Steven Rostedtc88fd862011-08-16 09:53:39 -04001766 switch (ret) {
1767 case FTRACE_UPDATE_IGNORE:
1768 return 0;
1769
1770 case FTRACE_UPDATE_MAKE_CALL:
1771 return ftrace_make_call(rec, ftrace_addr);
1772
1773 case FTRACE_UPDATE_MAKE_NOP:
1774 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001775
1776 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1777 case FTRACE_UPDATE_MODIFY_CALL:
1778 if (rec->flags & FTRACE_FL_REGS)
1779 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1780 else
1781 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1782
1783 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001784 }
1785
1786 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001787}
1788
Steven Rostedte4f5d542012-04-27 09:13:18 -04001789void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001790{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001791 struct dyn_ftrace *rec;
1792 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001793 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001794
Steven Rostedt45a4a232011-04-21 23:16:46 -04001795 if (unlikely(ftrace_disabled))
1796 return;
1797
Steven Rostedt265c8312009-02-13 12:43:56 -05001798 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001799 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001800 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001801 ftrace_bug(failed, rec->ip);
1802 /* Stop processing */
1803 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001804 }
1805 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001806}
1807
Steven Rostedtc88fd862011-08-16 09:53:39 -04001808struct ftrace_rec_iter {
1809 struct ftrace_page *pg;
1810 int index;
1811};
1812
1813/**
1814 * ftrace_rec_iter_start, start up iterating over traced functions
1815 *
1816 * Returns an iterator handle that is used to iterate over all
1817 * the records that represent address locations where functions
1818 * are traced.
1819 *
1820 * May return NULL if no records are available.
1821 */
1822struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1823{
1824 /*
1825 * We only use a single iterator.
1826 * Protected by the ftrace_lock mutex.
1827 */
1828 static struct ftrace_rec_iter ftrace_rec_iter;
1829 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1830
1831 iter->pg = ftrace_pages_start;
1832 iter->index = 0;
1833
1834 /* Could have empty pages */
1835 while (iter->pg && !iter->pg->index)
1836 iter->pg = iter->pg->next;
1837
1838 if (!iter->pg)
1839 return NULL;
1840
1841 return iter;
1842}
1843
1844/**
1845 * ftrace_rec_iter_next, get the next record to process.
1846 * @iter: The handle to the iterator.
1847 *
1848 * Returns the next iterator after the given iterator @iter.
1849 */
1850struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1851{
1852 iter->index++;
1853
1854 if (iter->index >= iter->pg->index) {
1855 iter->pg = iter->pg->next;
1856 iter->index = 0;
1857
1858 /* Could have empty pages */
1859 while (iter->pg && !iter->pg->index)
1860 iter->pg = iter->pg->next;
1861 }
1862
1863 if (!iter->pg)
1864 return NULL;
1865
1866 return iter;
1867}
1868
1869/**
1870 * ftrace_rec_iter_record, get the record at the iterator location
1871 * @iter: The current iterator location
1872 *
1873 * Returns the record that the current @iter is at.
1874 */
1875struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1876{
1877 return &iter->pg->records[iter->index];
1878}
1879
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301880static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001881ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001882{
1883 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001884 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001885
1886 ip = rec->ip;
1887
Steven Rostedt45a4a232011-04-21 23:16:46 -04001888 if (unlikely(ftrace_disabled))
1889 return 0;
1890
Shaohua Li25aac9d2009-01-09 11:29:40 +08001891 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001892 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001893 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301894 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001895 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301896 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001897}
1898
Steven Rostedt000ab692009-02-17 13:35:06 -05001899/*
1900 * archs can override this function if they must do something
1901 * before the modifying code is performed.
1902 */
1903int __weak ftrace_arch_code_modify_prepare(void)
1904{
1905 return 0;
1906}
1907
1908/*
1909 * archs can override this function if they must do something
1910 * after the modifying code is performed.
1911 */
1912int __weak ftrace_arch_code_modify_post_process(void)
1913{
1914 return 0;
1915}
1916
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001917void ftrace_modify_all_code(int command)
1918{
1919 if (command & FTRACE_UPDATE_CALLS)
1920 ftrace_replace_code(1);
1921 else if (command & FTRACE_DISABLE_CALLS)
1922 ftrace_replace_code(0);
1923
1924 if (command & FTRACE_UPDATE_TRACE_FUNC)
1925 ftrace_update_ftrace_func(ftrace_trace_function);
1926
1927 if (command & FTRACE_START_FUNC_RET)
1928 ftrace_enable_ftrace_graph_caller();
1929 else if (command & FTRACE_STOP_FUNC_RET)
1930 ftrace_disable_ftrace_graph_caller();
1931}
1932
Ingo Molnare309b412008-05-12 21:20:51 +02001933static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001934{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001935 int *command = data;
1936
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001937 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001938
Steven Rostedtc88fd862011-08-16 09:53:39 -04001939 return 0;
1940}
1941
1942/**
1943 * ftrace_run_stop_machine, go back to the stop machine method
1944 * @command: The command to tell ftrace what to do
1945 *
1946 * If an arch needs to fall back to the stop machine method, the
1947 * it can call this function.
1948 */
1949void ftrace_run_stop_machine(int command)
1950{
1951 stop_machine(__ftrace_modify_code, &command, NULL);
1952}
1953
1954/**
1955 * arch_ftrace_update_code, modify the code to trace or not trace
1956 * @command: The command that needs to be done
1957 *
1958 * Archs can override this function if it does not need to
1959 * run stop_machine() to modify code.
1960 */
1961void __weak arch_ftrace_update_code(int command)
1962{
1963 ftrace_run_stop_machine(command);
1964}
1965
1966static void ftrace_run_update_code(int command)
1967{
1968 int ret;
1969
1970 ret = ftrace_arch_code_modify_prepare();
1971 FTRACE_WARN_ON(ret);
1972 if (ret)
1973 return;
1974 /*
1975 * Do not call function tracer while we update the code.
1976 * We are in stop machine.
1977 */
1978 function_trace_stop++;
1979
1980 /*
1981 * By default we use stop_machine() to modify the code.
1982 * But archs can do what ever they want as long as it
1983 * is safe. The stop_machine() is the safest, but also
1984 * produces the most overhead.
1985 */
1986 arch_ftrace_update_code(command);
1987
Steven Rostedt6331c282011-07-13 15:11:02 -04001988 function_trace_stop--;
1989
Steven Rostedt000ab692009-02-17 13:35:06 -05001990 ret = ftrace_arch_code_modify_post_process();
1991 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001992}
1993
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001994static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001995static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001996static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001997
1998static void ftrace_startup_enable(int command)
1999{
2000 if (saved_ftrace_func != ftrace_trace_function) {
2001 saved_ftrace_func = ftrace_trace_function;
2002 command |= FTRACE_UPDATE_TRACE_FUNC;
2003 }
2004
2005 if (!command || !ftrace_enabled)
2006 return;
2007
2008 ftrace_run_update_code(command);
2009}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002010
Steven Rostedta1cd6172011-05-23 15:24:25 -04002011static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002012{
Steven Rostedtb8489142011-05-04 09:27:52 -04002013 bool hash_enable = true;
2014
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002015 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002016 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002017
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002018 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002019 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002020
Steven Rostedtb8489142011-05-04 09:27:52 -04002021 /* ops marked global share the filter hashes */
2022 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2023 ops = &global_ops;
2024 /* Don't update hash if global is already set */
2025 if (global_start_up)
2026 hash_enable = false;
2027 global_start_up++;
2028 }
2029
Steven Rostedted926f92011-05-03 13:25:24 -04002030 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002031 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002032 ftrace_hash_rec_enable(ops, 1);
2033
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002034 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002035
2036 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002037}
2038
Steven Rostedtbd69c302011-05-03 21:55:54 -04002039static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002040{
Steven Rostedtb8489142011-05-04 09:27:52 -04002041 bool hash_disable = true;
2042
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002043 if (unlikely(ftrace_disabled))
2044 return;
2045
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002046 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002047 /*
2048 * Just warn in case of unbalance, no need to kill ftrace, it's not
2049 * critical but the ftrace_call callers may be never nopped again after
2050 * further ftrace uses.
2051 */
2052 WARN_ON_ONCE(ftrace_start_up < 0);
2053
Steven Rostedtb8489142011-05-04 09:27:52 -04002054 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2055 ops = &global_ops;
2056 global_start_up--;
2057 WARN_ON_ONCE(global_start_up < 0);
2058 /* Don't update hash if global still has users */
2059 if (global_start_up) {
2060 WARN_ON_ONCE(!ftrace_start_up);
2061 hash_disable = false;
2062 }
2063 }
2064
2065 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002066 ftrace_hash_rec_disable(ops, 1);
2067
Steven Rostedtb8489142011-05-04 09:27:52 -04002068 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002069 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002070
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002071 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002072
2073 if (saved_ftrace_func != ftrace_trace_function) {
2074 saved_ftrace_func = ftrace_trace_function;
2075 command |= FTRACE_UPDATE_TRACE_FUNC;
2076 }
2077
2078 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002079 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002080
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002081 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002082}
2083
Ingo Molnare309b412008-05-12 21:20:51 +02002084static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002085{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002086 if (unlikely(ftrace_disabled))
2087 return;
2088
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002089 /* Force update next time */
2090 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002091 /* ftrace_start_up is true if we want ftrace running */
2092 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002093 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002094}
2095
Ingo Molnare309b412008-05-12 21:20:51 +02002096static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002097{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002098 if (unlikely(ftrace_disabled))
2099 return;
2100
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002101 /* ftrace_start_up is true if ftrace is running */
2102 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002103 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002104}
2105
Steven Rostedt3d083392008-05-12 21:20:42 +02002106static cycle_t ftrace_update_time;
2107static unsigned long ftrace_update_cnt;
2108unsigned long ftrace_update_tot_cnt;
2109
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002110static int ops_traces_mod(struct ftrace_ops *ops)
2111{
2112 struct ftrace_hash *hash;
2113
2114 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002115 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002116}
2117
Steven Rostedt31e88902008-11-14 16:21:19 -08002118static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002119{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002120 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002121 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302122 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002123 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002124 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002125
2126 /*
2127 * When adding a module, we need to check if tracers are
2128 * currently enabled and if they are set to trace all functions.
2129 * If they are, we need to enable the module functions as well
2130 * as update the reference counts for those function records.
2131 */
2132 if (mod) {
2133 struct ftrace_ops *ops;
2134
2135 for (ops = ftrace_ops_list;
2136 ops != &ftrace_list_end; ops = ops->next) {
2137 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2138 ops_traces_mod(ops))
2139 ref++;
2140 }
2141 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002142
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002143 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002144 ftrace_update_cnt = 0;
2145
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002146 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302147
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002148 for (i = 0; i < pg->index; i++) {
2149 /* If something went wrong, bail without enabling anything */
2150 if (unlikely(ftrace_disabled))
2151 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002152
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002153 p = &pg->records[i];
2154 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302155
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002156 /*
2157 * Do the initial record conversion from mcount jump
2158 * to the NOP instructions.
2159 */
2160 if (!ftrace_code_disable(mod, p))
2161 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002162
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002163 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002164
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002165 /*
2166 * If the tracing is enabled, go ahead and enable the record.
2167 *
2168 * The reason not to enable the record immediatelly is the
2169 * inherent check of ftrace_make_nop/ftrace_make_call for
2170 * correct previous instructions. Making first the NOP
2171 * conversion puts the module to the correct state, thus
2172 * passing the ftrace_make_call check.
2173 */
2174 if (ftrace_start_up && ref) {
2175 int failed = __ftrace_replace_code(p, 1);
2176 if (failed)
2177 ftrace_bug(failed, p->ip);
2178 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002179 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002180 }
2181
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002182 ftrace_new_pgs = NULL;
2183
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002184 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002185 ftrace_update_time = stop - start;
2186 ftrace_update_tot_cnt += ftrace_update_cnt;
2187
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002188 return 0;
2189}
2190
Steven Rostedta7900872011-12-16 16:23:44 -05002191static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002192{
Steven Rostedta7900872011-12-16 16:23:44 -05002193 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002194 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002195
Steven Rostedta7900872011-12-16 16:23:44 -05002196 if (WARN_ON(!count))
2197 return -EINVAL;
2198
2199 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002200
2201 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002202 * We want to fill as much as possible. No more than a page
2203 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002204 */
Steven Rostedta7900872011-12-16 16:23:44 -05002205 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2206 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002207
Steven Rostedta7900872011-12-16 16:23:44 -05002208 again:
2209 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2210
2211 if (!pg->records) {
2212 /* if we can't allocate this size, try something smaller */
2213 if (!order)
2214 return -ENOMEM;
2215 order >>= 1;
2216 goto again;
2217 }
2218
2219 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2220 pg->size = cnt;
2221
2222 if (cnt > count)
2223 cnt = count;
2224
2225 return cnt;
2226}
2227
2228static struct ftrace_page *
2229ftrace_allocate_pages(unsigned long num_to_init)
2230{
2231 struct ftrace_page *start_pg;
2232 struct ftrace_page *pg;
2233 int order;
2234 int cnt;
2235
2236 if (!num_to_init)
2237 return 0;
2238
2239 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2240 if (!pg)
2241 return NULL;
2242
2243 /*
2244 * Try to allocate as much as possible in one continues
2245 * location that fills in all of the space. We want to
2246 * waste as little space as possible.
2247 */
2248 for (;;) {
2249 cnt = ftrace_allocate_records(pg, num_to_init);
2250 if (cnt < 0)
2251 goto free_pages;
2252
2253 num_to_init -= cnt;
2254 if (!num_to_init)
2255 break;
2256
2257 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2258 if (!pg->next)
2259 goto free_pages;
2260
2261 pg = pg->next;
2262 }
2263
2264 return start_pg;
2265
2266 free_pages:
2267 while (start_pg) {
2268 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2269 free_pages((unsigned long)pg->records, order);
2270 start_pg = pg->next;
2271 kfree(pg);
2272 pg = start_pg;
2273 }
2274 pr_info("ftrace: FAILED to allocate memory for functions\n");
2275 return NULL;
2276}
2277
2278static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2279{
2280 int cnt;
2281
2282 if (!num_to_init) {
2283 pr_info("ftrace: No functions to be traced?\n");
2284 return -1;
2285 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002286
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002287 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002288 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002289 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002290
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002291 return 0;
2292}
2293
Steven Rostedt5072c592008-05-12 21:20:43 +02002294#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2295
2296struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002297 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002298 loff_t func_pos;
2299 struct ftrace_page *pg;
2300 struct dyn_ftrace *func;
2301 struct ftrace_func_probe *probe;
2302 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002303 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002304 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002305 int hidx;
2306 int idx;
2307 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002308};
2309
Ingo Molnare309b412008-05-12 21:20:51 +02002310static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002311t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002312{
2313 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002314 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002315 struct hlist_head *hhd;
2316
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002317 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002318 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002319
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002320 if (iter->probe)
2321 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002322 retry:
2323 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2324 return NULL;
2325
2326 hhd = &ftrace_func_hash[iter->hidx];
2327
2328 if (hlist_empty(hhd)) {
2329 iter->hidx++;
2330 hnd = NULL;
2331 goto retry;
2332 }
2333
2334 if (!hnd)
2335 hnd = hhd->first;
2336 else {
2337 hnd = hnd->next;
2338 if (!hnd) {
2339 iter->hidx++;
2340 goto retry;
2341 }
2342 }
2343
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002344 if (WARN_ON_ONCE(!hnd))
2345 return NULL;
2346
2347 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2348
2349 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002350}
2351
2352static void *t_hash_start(struct seq_file *m, loff_t *pos)
2353{
2354 struct ftrace_iterator *iter = m->private;
2355 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002356 loff_t l;
2357
Steven Rostedt69a30832011-12-19 15:21:16 -05002358 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2359 return NULL;
2360
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002361 if (iter->func_pos > *pos)
2362 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002363
Li Zefand82d6242009-06-24 09:54:54 +08002364 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002365 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002366 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002367 if (!p)
2368 break;
2369 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002370 if (!p)
2371 return NULL;
2372
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002373 /* Only set this if we have an item */
2374 iter->flags |= FTRACE_ITER_HASH;
2375
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002377}
2378
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002379static int
2380t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002381{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002382 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002383
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002384 rec = iter->probe;
2385 if (WARN_ON_ONCE(!rec))
2386 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002387
Steven Rostedt809dcf22009-02-16 23:06:01 -05002388 if (rec->ops->print)
2389 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2390
Steven Rostedtb375a112009-09-17 00:05:58 -04002391 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002392
2393 if (rec->data)
2394 seq_printf(m, ":%p", rec->data);
2395 seq_putc(m, '\n');
2396
2397 return 0;
2398}
2399
2400static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002401t_next(struct seq_file *m, void *v, loff_t *pos)
2402{
2403 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002404 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002405 struct dyn_ftrace *rec = NULL;
2406
Steven Rostedt45a4a232011-04-21 23:16:46 -04002407 if (unlikely(ftrace_disabled))
2408 return NULL;
2409
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002410 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002411 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002412
Steven Rostedt5072c592008-05-12 21:20:43 +02002413 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002414 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002415
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002416 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002417 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002418
Steven Rostedt5072c592008-05-12 21:20:43 +02002419 retry:
2420 if (iter->idx >= iter->pg->index) {
2421 if (iter->pg->next) {
2422 iter->pg = iter->pg->next;
2423 iter->idx = 0;
2424 goto retry;
2425 }
2426 } else {
2427 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002428 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002429 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002430
Steven Rostedt41c52c02008-05-22 11:46:33 -04002431 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002432 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2433
2434 ((iter->flags & FTRACE_ITER_ENABLED) &&
2435 !(rec->flags & ~FTRACE_FL_MASK))) {
2436
Steven Rostedt5072c592008-05-12 21:20:43 +02002437 rec = NULL;
2438 goto retry;
2439 }
2440 }
2441
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002442 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002443 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002444
2445 iter->func = rec;
2446
2447 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002448}
2449
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002450static void reset_iter_read(struct ftrace_iterator *iter)
2451{
2452 iter->pos = 0;
2453 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002454 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002455}
2456
2457static void *t_start(struct seq_file *m, loff_t *pos)
2458{
2459 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002460 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002461 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002462 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002463
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002464 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002465
2466 if (unlikely(ftrace_disabled))
2467 return NULL;
2468
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002469 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002470 * If an lseek was done, then reset and start from beginning.
2471 */
2472 if (*pos < iter->pos)
2473 reset_iter_read(iter);
2474
2475 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002476 * For set_ftrace_filter reading, if we have the filter
2477 * off, we can short cut and just print out that all
2478 * functions are enabled.
2479 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002480 if (iter->flags & FTRACE_ITER_FILTER &&
2481 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002482 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002483 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002484 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002485 /* reset in case of seek/pread */
2486 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002487 return iter;
2488 }
2489
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002490 if (iter->flags & FTRACE_ITER_HASH)
2491 return t_hash_start(m, pos);
2492
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002493 /*
2494 * Unfortunately, we need to restart at ftrace_pages_start
2495 * every time we let go of the ftrace_mutex. This is because
2496 * those pointers can change without the lock.
2497 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002498 iter->pg = ftrace_pages_start;
2499 iter->idx = 0;
2500 for (l = 0; l <= *pos; ) {
2501 p = t_next(m, p, &l);
2502 if (!p)
2503 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002504 }
walimis5821e1b2008-11-15 15:19:06 +08002505
Steven Rostedt69a30832011-12-19 15:21:16 -05002506 if (!p)
2507 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002508
2509 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002510}
2511
2512static void t_stop(struct seq_file *m, void *p)
2513{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002514 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002515}
2516
2517static int t_show(struct seq_file *m, void *v)
2518{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002519 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002520 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002521
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002522 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002523 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002524
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002525 if (iter->flags & FTRACE_ITER_PRINTALL) {
2526 seq_printf(m, "#### all functions enabled ####\n");
2527 return 0;
2528 }
2529
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002530 rec = iter->func;
2531
Steven Rostedt5072c592008-05-12 21:20:43 +02002532 if (!rec)
2533 return 0;
2534
Steven Rostedt647bcd02011-05-03 14:39:21 -04002535 seq_printf(m, "%ps", (void *)rec->ip);
2536 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002537 seq_printf(m, " (%ld)%s",
2538 rec->flags & ~FTRACE_FL_MASK,
2539 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002540 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002541
2542 return 0;
2543}
2544
James Morris88e9d342009-09-22 16:43:43 -07002545static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002546 .start = t_start,
2547 .next = t_next,
2548 .stop = t_stop,
2549 .show = t_show,
2550};
2551
Ingo Molnare309b412008-05-12 21:20:51 +02002552static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002553ftrace_avail_open(struct inode *inode, struct file *file)
2554{
2555 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002556
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002557 if (unlikely(ftrace_disabled))
2558 return -ENODEV;
2559
Jiri Olsa50e18b92012-04-25 10:23:39 +02002560 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2561 if (iter) {
2562 iter->pg = ftrace_pages_start;
2563 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002564 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002565
Jiri Olsa50e18b92012-04-25 10:23:39 +02002566 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002567}
2568
Steven Rostedt647bcd02011-05-03 14:39:21 -04002569static int
2570ftrace_enabled_open(struct inode *inode, struct file *file)
2571{
2572 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002573
2574 if (unlikely(ftrace_disabled))
2575 return -ENODEV;
2576
Jiri Olsa50e18b92012-04-25 10:23:39 +02002577 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2578 if (iter) {
2579 iter->pg = ftrace_pages_start;
2580 iter->flags = FTRACE_ITER_ENABLED;
2581 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002582 }
2583
Jiri Olsa50e18b92012-04-25 10:23:39 +02002584 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002585}
2586
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002587static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002588{
Steven Rostedt52baf112009-02-14 01:15:39 -05002589 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002590 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002591 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002592}
2593
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002594/**
2595 * ftrace_regex_open - initialize function tracer filter files
2596 * @ops: The ftrace_ops that hold the hash filters
2597 * @flag: The type of filter to process
2598 * @inode: The inode, usually passed in to your open routine
2599 * @file: The file, usually passed in to your open routine
2600 *
2601 * ftrace_regex_open() initializes the filter files for the
2602 * @ops. Depending on @flag it may process the filter hash or
2603 * the notrace hash of @ops. With this called from the open
2604 * routine, you can use ftrace_filter_write() for the write
2605 * routine if @flag has FTRACE_ITER_FILTER set, or
2606 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2607 * ftrace_regex_lseek() should be used as the lseek routine, and
2608 * release must call ftrace_regex_release().
2609 */
2610int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002611ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002612 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002613{
2614 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002615 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002616 int ret = 0;
2617
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002618 if (unlikely(ftrace_disabled))
2619 return -ENODEV;
2620
Steven Rostedt5072c592008-05-12 21:20:43 +02002621 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2622 if (!iter)
2623 return -ENOMEM;
2624
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002625 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2626 kfree(iter);
2627 return -ENOMEM;
2628 }
2629
Steven Rostedtf45948e2011-05-02 12:29:25 -04002630 if (flag & FTRACE_ITER_NOTRACE)
2631 hash = ops->notrace_hash;
2632 else
2633 hash = ops->filter_hash;
2634
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002635 iter->ops = ops;
2636 iter->flags = flag;
2637
2638 if (file->f_mode & FMODE_WRITE) {
2639 mutex_lock(&ftrace_lock);
2640 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2641 mutex_unlock(&ftrace_lock);
2642
2643 if (!iter->hash) {
2644 trace_parser_put(&iter->parser);
2645 kfree(iter);
2646 return -ENOMEM;
2647 }
2648 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002649
Steven Rostedt41c52c02008-05-22 11:46:33 -04002650 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002651
Steven Rostedt5072c592008-05-12 21:20:43 +02002652 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002653 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002654 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002655
2656 if (file->f_mode & FMODE_READ) {
2657 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002658
2659 ret = seq_open(file, &show_ftrace_seq_ops);
2660 if (!ret) {
2661 struct seq_file *m = file->private_data;
2662 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002663 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002664 /* Failed */
2665 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002666 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002667 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002668 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002669 } else
2670 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002671 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002672
2673 return ret;
2674}
2675
Steven Rostedt41c52c02008-05-22 11:46:33 -04002676static int
2677ftrace_filter_open(struct inode *inode, struct file *file)
2678{
Steven Rostedt69a30832011-12-19 15:21:16 -05002679 return ftrace_regex_open(&global_ops,
2680 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2681 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002682}
2683
2684static int
2685ftrace_notrace_open(struct inode *inode, struct file *file)
2686{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002687 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002688 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002689}
2690
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002691loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -08002692ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
Steven Rostedt5072c592008-05-12 21:20:43 +02002693{
2694 loff_t ret;
2695
2696 if (file->f_mode & FMODE_READ)
Andrew Morton965c8e52012-12-17 15:59:39 -08002697 ret = seq_lseek(file, offset, whence);
Steven Rostedt5072c592008-05-12 21:20:43 +02002698 else
2699 file->f_pos = ret = 1;
2700
2701 return ret;
2702}
2703
Steven Rostedt64e7c442009-02-13 17:08:48 -05002704static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002705{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002706 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002707 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002708
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002709 switch (type) {
2710 case MATCH_FULL:
2711 if (strcmp(str, regex) == 0)
2712 matched = 1;
2713 break;
2714 case MATCH_FRONT_ONLY:
2715 if (strncmp(str, regex, len) == 0)
2716 matched = 1;
2717 break;
2718 case MATCH_MIDDLE_ONLY:
2719 if (strstr(str, regex))
2720 matched = 1;
2721 break;
2722 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002723 slen = strlen(str);
2724 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002725 matched = 1;
2726 break;
2727 }
2728
2729 return matched;
2730}
2731
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002732static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002733enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002734{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002735 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002736 int ret = 0;
2737
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002738 entry = ftrace_lookup_ip(hash, rec->ip);
2739 if (not) {
2740 /* Do nothing if it doesn't exist */
2741 if (!entry)
2742 return 0;
2743
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002744 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002745 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002746 /* Do nothing if it exists */
2747 if (entry)
2748 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002749
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002750 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002751 }
2752 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002753}
2754
Steven Rostedt64e7c442009-02-13 17:08:48 -05002755static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002756ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2757 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002758{
2759 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002760 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002761
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002762 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2763
2764 if (mod) {
2765 /* module lookup requires matching the module */
2766 if (!modname || strcmp(modname, mod))
2767 return 0;
2768
2769 /* blank search means to match all funcs in the mod */
2770 if (!len)
2771 return 1;
2772 }
2773
Steven Rostedt64e7c442009-02-13 17:08:48 -05002774 return ftrace_match(str, regex, len, type);
2775}
2776
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002777static int
2778match_records(struct ftrace_hash *hash, char *buff,
2779 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002780{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002781 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002782 struct ftrace_page *pg;
2783 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002784 int type = MATCH_FULL;
2785 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002786 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002787 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002788
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002789 if (len) {
2790 type = filter_parse_regex(buff, len, &search, &not);
2791 search_len = strlen(search);
2792 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002793
Steven Rostedt52baf112009-02-14 01:15:39 -05002794 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002795
2796 if (unlikely(ftrace_disabled))
2797 goto out_unlock;
2798
Steven Rostedt265c8312009-02-13 12:43:56 -05002799 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002800 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002801 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002802 if (ret < 0) {
2803 found = ret;
2804 goto out_unlock;
2805 }
Li Zefan311d16d2009-12-08 11:15:11 +08002806 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002807 }
2808 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002809 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002810 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002811
2812 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002813}
2814
Steven Rostedt64e7c442009-02-13 17:08:48 -05002815static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002816ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002817{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002818 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002819}
2820
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002821static int
2822ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002823{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002824 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002825
Steven Rostedt64e7c442009-02-13 17:08:48 -05002826 /* blank or '*' mean the same */
2827 if (strcmp(buff, "*") == 0)
2828 buff[0] = 0;
2829
2830 /* handle the case of 'dont filter this module' */
2831 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2832 buff[0] = 0;
2833 not = 1;
2834 }
2835
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002836 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002837}
2838
Steven Rostedtf6180772009-02-14 00:40:25 -05002839/*
2840 * We register the module command as a template to show others how
2841 * to register the a command as well.
2842 */
2843
2844static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002845ftrace_mod_callback(struct ftrace_hash *hash,
2846 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002847{
2848 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002849 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002850
2851 /*
2852 * cmd == 'mod' because we only registered this func
2853 * for the 'mod' ftrace_func_command.
2854 * But if you register one func with multiple commands,
2855 * you can tell which command was used by the cmd
2856 * parameter.
2857 */
2858
2859 /* we must have a module name */
2860 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002861 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002862
2863 mod = strsep(&param, ":");
2864 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002865 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002866
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002867 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002868 if (!ret)
2869 ret = -EINVAL;
2870 if (ret < 0)
2871 return ret;
2872
2873 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002874}
2875
2876static struct ftrace_func_command ftrace_mod_cmd = {
2877 .name = "mod",
2878 .func = ftrace_mod_callback,
2879};
2880
2881static int __init ftrace_mod_cmd_init(void)
2882{
2883 return register_ftrace_command(&ftrace_mod_cmd);
2884}
Steven Rostedt6f415672012-10-05 12:13:07 -04002885core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002886
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002887static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002888 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002890 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002891 struct hlist_head *hhd;
2892 struct hlist_node *n;
2893 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002894
2895 key = hash_long(ip, FTRACE_HASH_BITS);
2896
2897 hhd = &ftrace_func_hash[key];
2898
2899 if (hlist_empty(hhd))
2900 return;
2901
2902 /*
2903 * Disable preemption for these calls to prevent a RCU grace
2904 * period. This syncs the hash iteration and freeing of items
2905 * on the hash. rcu_read_lock is too dangerous here.
2906 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002907 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2909 if (entry->ip == ip)
2910 entry->ops->func(ip, parent_ip, &entry->data);
2911 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002912 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913}
2914
Steven Rostedtb6887d72009-02-17 12:32:04 -05002915static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002917 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002918};
2919
Steven Rostedtb6887d72009-02-17 12:32:04 -05002920static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002921
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923{
Steven Rostedtb8489142011-05-04 09:27:52 -04002924 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925 int i;
2926
Steven Rostedtb6887d72009-02-17 12:32:04 -05002927 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928 return;
2929
2930 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2931 struct hlist_head *hhd = &ftrace_func_hash[i];
2932 if (hhd->first)
2933 break;
2934 }
2935 /* Nothing registered? */
2936 if (i == FTRACE_FUNC_HASHSIZE)
2937 return;
2938
Steven Rostedtb8489142011-05-04 09:27:52 -04002939 ret = __register_ftrace_function(&trace_probe_ops);
2940 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002941 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002942
Steven Rostedtb6887d72009-02-17 12:32:04 -05002943 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002944}
2945
Steven Rostedtb6887d72009-02-17 12:32:04 -05002946static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947{
Steven Rostedtb8489142011-05-04 09:27:52 -04002948 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002949 int i;
2950
Steven Rostedtb6887d72009-02-17 12:32:04 -05002951 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952 return;
2953
2954 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2955 struct hlist_head *hhd = &ftrace_func_hash[i];
2956 if (hhd->first)
2957 return;
2958 }
2959
2960 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002961 ret = __unregister_ftrace_function(&trace_probe_ops);
2962 if (!ret)
2963 ftrace_shutdown(&trace_probe_ops, 0);
2964
Steven Rostedtb6887d72009-02-17 12:32:04 -05002965 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966}
2967
2968
2969static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2970{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002971 struct ftrace_func_probe *entry =
2972 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973
2974 if (entry->ops->free)
2975 entry->ops->free(&entry->data);
2976 kfree(entry);
2977}
2978
2979
2980int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002981register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002982 void *data)
2983{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002984 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002985 struct ftrace_page *pg;
2986 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002987 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002988 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002989 int count = 0;
2990 char *search;
2991
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002992 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002993 len = strlen(search);
2994
Steven Rostedtb6887d72009-02-17 12:32:04 -05002995 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996 if (WARN_ON(not))
2997 return -EINVAL;
2998
2999 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000
Steven Rostedt45a4a232011-04-21 23:16:46 -04003001 if (unlikely(ftrace_disabled))
3002 goto out_unlock;
3003
Steven Rostedt59df055f2009-02-14 15:29:06 -05003004 do_for_each_ftrace_rec(pg, rec) {
3005
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003006 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007 continue;
3008
3009 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3010 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003011 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003012 if (!count)
3013 count = -ENOMEM;
3014 goto out_unlock;
3015 }
3016
3017 count++;
3018
3019 entry->data = data;
3020
3021 /*
3022 * The caller might want to do something special
3023 * for each function we find. We call the callback
3024 * to give the caller an opportunity to do so.
3025 */
3026 if (ops->callback) {
3027 if (ops->callback(rec->ip, &entry->data) < 0) {
3028 /* caller does not like this func */
3029 kfree(entry);
3030 continue;
3031 }
3032 }
3033
3034 entry->ops = ops;
3035 entry->ip = rec->ip;
3036
3037 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3038 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3039
3040 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003041 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042
3043 out_unlock:
3044 mutex_unlock(&ftrace_lock);
3045
3046 return count;
3047}
3048
3049enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003050 PROBE_TEST_FUNC = 1,
3051 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003052};
3053
3054static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003055__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003056 void *data, int flags)
3057{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003058 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003059 struct hlist_node *n, *tmp;
3060 char str[KSYM_SYMBOL_LEN];
3061 int type = MATCH_FULL;
3062 int i, len = 0;
3063 char *search;
3064
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003065 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003066 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003067 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003068 int not;
3069
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003070 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071 len = strlen(search);
3072
Steven Rostedtb6887d72009-02-17 12:32:04 -05003073 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074 if (WARN_ON(not))
3075 return;
3076 }
3077
3078 mutex_lock(&ftrace_lock);
3079 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3080 struct hlist_head *hhd = &ftrace_func_hash[i];
3081
3082 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3083
3084 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003085 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086 continue;
3087
Steven Rostedtb6887d72009-02-17 12:32:04 -05003088 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089 continue;
3090
3091 /* do this last, since it is the most expensive */
3092 if (glob) {
3093 kallsyms_lookup(entry->ip, NULL, NULL,
3094 NULL, str);
3095 if (!ftrace_match(str, glob, len, type))
3096 continue;
3097 }
3098
3099 hlist_del(&entry->node);
3100 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3101 }
3102 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003103 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003104 mutex_unlock(&ftrace_lock);
3105}
3106
3107void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003108unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003109 void *data)
3110{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003111 __unregister_ftrace_function_probe(glob, ops, data,
3112 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003113}
3114
3115void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003116unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003117{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003118 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003119}
3120
Steven Rostedtb6887d72009-02-17 12:32:04 -05003121void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003122{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003123 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003124}
3125
Steven Rostedtf6180772009-02-14 00:40:25 -05003126static LIST_HEAD(ftrace_commands);
3127static DEFINE_MUTEX(ftrace_cmd_mutex);
3128
3129int register_ftrace_command(struct ftrace_func_command *cmd)
3130{
3131 struct ftrace_func_command *p;
3132 int ret = 0;
3133
3134 mutex_lock(&ftrace_cmd_mutex);
3135 list_for_each_entry(p, &ftrace_commands, list) {
3136 if (strcmp(cmd->name, p->name) == 0) {
3137 ret = -EBUSY;
3138 goto out_unlock;
3139 }
3140 }
3141 list_add(&cmd->list, &ftrace_commands);
3142 out_unlock:
3143 mutex_unlock(&ftrace_cmd_mutex);
3144
3145 return ret;
3146}
3147
3148int unregister_ftrace_command(struct ftrace_func_command *cmd)
3149{
3150 struct ftrace_func_command *p, *n;
3151 int ret = -ENODEV;
3152
3153 mutex_lock(&ftrace_cmd_mutex);
3154 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3155 if (strcmp(cmd->name, p->name) == 0) {
3156 ret = 0;
3157 list_del_init(&p->list);
3158 goto out_unlock;
3159 }
3160 }
3161 out_unlock:
3162 mutex_unlock(&ftrace_cmd_mutex);
3163
3164 return ret;
3165}
3166
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003167static int ftrace_process_regex(struct ftrace_hash *hash,
3168 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003169{
Steven Rostedtf6180772009-02-14 00:40:25 -05003170 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003171 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003172 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003173
3174 func = strsep(&next, ":");
3175
3176 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003177 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003178 if (!ret)
3179 ret = -EINVAL;
3180 if (ret < 0)
3181 return ret;
3182 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003183 }
3184
Steven Rostedtf6180772009-02-14 00:40:25 -05003185 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003186
3187 command = strsep(&next, ":");
3188
Steven Rostedtf6180772009-02-14 00:40:25 -05003189 mutex_lock(&ftrace_cmd_mutex);
3190 list_for_each_entry(p, &ftrace_commands, list) {
3191 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003192 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003193 goto out_unlock;
3194 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003195 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003196 out_unlock:
3197 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003198
Steven Rostedtf6180772009-02-14 00:40:25 -05003199 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003200}
3201
Ingo Molnare309b412008-05-12 21:20:51 +02003202static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003203ftrace_regex_write(struct file *file, const char __user *ubuf,
3204 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003205{
3206 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003207 struct trace_parser *parser;
3208 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003209
Li Zefan4ba79782009-09-22 13:52:20 +08003210 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003211 return 0;
3212
Steven Rostedt41c52c02008-05-22 11:46:33 -04003213 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003214
Steven Rostedt45a4a232011-04-21 23:16:46 -04003215 ret = -ENODEV;
3216 if (unlikely(ftrace_disabled))
3217 goto out_unlock;
3218
Steven Rostedt5072c592008-05-12 21:20:43 +02003219 if (file->f_mode & FMODE_READ) {
3220 struct seq_file *m = file->private_data;
3221 iter = m->private;
3222 } else
3223 iter = file->private_data;
3224
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003225 parser = &iter->parser;
3226 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003227
Li Zefan4ba79782009-09-22 13:52:20 +08003228 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003229 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003230 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003231 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003232 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003233 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003234 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003235 }
3236
Steven Rostedt5072c592008-05-12 21:20:43 +02003237 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003238out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003239 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003240
Steven Rostedt5072c592008-05-12 21:20:43 +02003241 return ret;
3242}
3243
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003244ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003245ftrace_filter_write(struct file *file, const char __user *ubuf,
3246 size_t cnt, loff_t *ppos)
3247{
3248 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3249}
3250
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003251ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003252ftrace_notrace_write(struct file *file, const char __user *ubuf,
3253 size_t cnt, loff_t *ppos)
3254{
3255 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3256}
3257
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003258static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003259ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3260{
3261 struct ftrace_func_entry *entry;
3262
3263 if (!ftrace_location(ip))
3264 return -EINVAL;
3265
3266 if (remove) {
3267 entry = ftrace_lookup_ip(hash, ip);
3268 if (!entry)
3269 return -ENOENT;
3270 free_hash_entry(hash, entry);
3271 return 0;
3272 }
3273
3274 return add_hash_entry(hash, ip);
3275}
3276
3277static int
3278ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3279 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003280{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003281 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003282 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003283 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003284
Steven Rostedt936e0742011-05-05 22:54:01 -04003285 /* All global ops uses the global ops filters */
3286 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3287 ops = &global_ops;
3288
Steven Rostedt41c52c02008-05-22 11:46:33 -04003289 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003290 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003291
Steven Rostedtf45948e2011-05-02 12:29:25 -04003292 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003293 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003294 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003295 orig_hash = &ops->notrace_hash;
3296
3297 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3298 if (!hash)
3299 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003300
Steven Rostedt41c52c02008-05-22 11:46:33 -04003301 mutex_lock(&ftrace_regex_lock);
3302 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003303 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003304 if (buf && !ftrace_match_records(hash, buf, len)) {
3305 ret = -EINVAL;
3306 goto out_regex_unlock;
3307 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003308 if (ip) {
3309 ret = ftrace_match_addr(hash, ip, remove);
3310 if (ret < 0)
3311 goto out_regex_unlock;
3312 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003313
3314 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003315 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003316 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3317 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003318 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003319
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003320 mutex_unlock(&ftrace_lock);
3321
Jiri Olsaac483c42012-01-02 10:04:14 +01003322 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003323 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003324
3325 free_ftrace_hash(hash);
3326 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003327}
3328
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003329static int
3330ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3331 int reset, int enable)
3332{
3333 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3334}
3335
3336/**
3337 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3338 * @ops - the ops to set the filter with
3339 * @ip - the address to add to or remove from the filter.
3340 * @remove - non zero to remove the ip from the filter
3341 * @reset - non zero to reset all filters before applying this filter.
3342 *
3343 * Filters denote which functions should be enabled when tracing is enabled
3344 * If @ip is NULL, it failes to update filter.
3345 */
3346int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3347 int remove, int reset)
3348{
3349 return ftrace_set_addr(ops, ip, remove, reset, 1);
3350}
3351EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3352
3353static int
3354ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3355 int reset, int enable)
3356{
3357 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3358}
3359
Steven Rostedt77a2b372008-05-12 21:20:45 +02003360/**
3361 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003362 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003363 * @buf - the string that holds the function filter text.
3364 * @len - the length of the string.
3365 * @reset - non zero to reset all filters before applying this filter.
3366 *
3367 * Filters denote which functions should be enabled when tracing is enabled.
3368 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3369 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003370int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003371 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003372{
Jiri Olsaac483c42012-01-02 10:04:14 +01003373 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003374}
Steven Rostedt936e0742011-05-05 22:54:01 -04003375EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003376
Steven Rostedt41c52c02008-05-22 11:46:33 -04003377/**
3378 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003379 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003380 * @buf - the string that holds the function notrace text.
3381 * @len - the length of the string.
3382 * @reset - non zero to reset all filters before applying this filter.
3383 *
3384 * Notrace Filters denote which functions should not be enabled when tracing
3385 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3386 * for tracing.
3387 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003388int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003389 int len, int reset)
3390{
Jiri Olsaac483c42012-01-02 10:04:14 +01003391 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003392}
3393EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3394/**
3395 * ftrace_set_filter - set a function to filter on in ftrace
3396 * @ops - the ops to set the filter with
3397 * @buf - the string that holds the function filter text.
3398 * @len - the length of the string.
3399 * @reset - non zero to reset all filters before applying this filter.
3400 *
3401 * Filters denote which functions should be enabled when tracing is enabled.
3402 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3403 */
3404void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3405{
3406 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3407}
3408EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3409
3410/**
3411 * ftrace_set_notrace - set a function to not trace in ftrace
3412 * @ops - the ops to set the notrace filter with
3413 * @buf - the string that holds the function notrace text.
3414 * @len - the length of the string.
3415 * @reset - non zero to reset all filters before applying this filter.
3416 *
3417 * Notrace Filters denote which functions should not be enabled when tracing
3418 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3419 * for tracing.
3420 */
3421void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003422{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003423 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003424}
Steven Rostedt936e0742011-05-05 22:54:01 -04003425EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003426
Steven Rostedt2af15d62009-05-28 13:37:24 -04003427/*
3428 * command line interface to allow users to set filters on boot up.
3429 */
3430#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3431static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3432static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3433
3434static int __init set_ftrace_notrace(char *str)
3435{
3436 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3437 return 1;
3438}
3439__setup("ftrace_notrace=", set_ftrace_notrace);
3440
3441static int __init set_ftrace_filter(char *str)
3442{
3443 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3444 return 1;
3445}
3446__setup("ftrace_filter=", set_ftrace_filter);
3447
Stefan Assmann369bc182009-10-12 22:17:21 +02003448#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003449static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003450static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3451
Stefan Assmann369bc182009-10-12 22:17:21 +02003452static int __init set_graph_function(char *str)
3453{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003454 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003455 return 1;
3456}
3457__setup("ftrace_graph_filter=", set_graph_function);
3458
3459static void __init set_ftrace_early_graph(char *buf)
3460{
3461 int ret;
3462 char *func;
3463
3464 while (buf) {
3465 func = strsep(&buf, ",");
3466 /* we allow only one expression at a time */
3467 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3468 func);
3469 if (ret)
3470 printk(KERN_DEBUG "ftrace: function %s not "
3471 "traceable\n", func);
3472 }
3473}
3474#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3475
Steven Rostedt2a85a372011-12-19 21:57:44 -05003476void __init
3477ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003478{
3479 char *func;
3480
3481 while (buf) {
3482 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003483 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003484 }
3485}
3486
3487static void __init set_ftrace_early_filters(void)
3488{
3489 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003490 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003491 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003492 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003493#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3494 if (ftrace_graph_buf[0])
3495 set_ftrace_early_graph(ftrace_graph_buf);
3496#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003497}
3498
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003499int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003500{
3501 struct seq_file *m = (struct seq_file *)file->private_data;
3502 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003503 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003504 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003505 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003506 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003507
Steven Rostedt41c52c02008-05-22 11:46:33 -04003508 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003509 if (file->f_mode & FMODE_READ) {
3510 iter = m->private;
3511
3512 seq_release(inode, file);
3513 } else
3514 iter = file->private_data;
3515
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003516 parser = &iter->parser;
3517 if (trace_parser_loaded(parser)) {
3518 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003519 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003520 }
3521
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003522 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003523
Steven Rostedt058e2972011-04-29 22:35:33 -04003524 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003525 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3526
3527 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003528 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003529 else
3530 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003531
Steven Rostedt058e2972011-04-29 22:35:33 -04003532 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003533 ret = ftrace_hash_move(iter->ops, filter_hash,
3534 orig_hash, iter->hash);
3535 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3536 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003537 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003538
Steven Rostedt058e2972011-04-29 22:35:33 -04003539 mutex_unlock(&ftrace_lock);
3540 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003541 free_ftrace_hash(iter->hash);
3542 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003543
Steven Rostedt41c52c02008-05-22 11:46:33 -04003544 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003545 return 0;
3546}
3547
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003548static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003549 .open = ftrace_avail_open,
3550 .read = seq_read,
3551 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003552 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003553};
3554
Steven Rostedt647bcd02011-05-03 14:39:21 -04003555static const struct file_operations ftrace_enabled_fops = {
3556 .open = ftrace_enabled_open,
3557 .read = seq_read,
3558 .llseek = seq_lseek,
3559 .release = seq_release_private,
3560};
3561
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003562static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003563 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003564 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003565 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003566 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003567 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003568};
3569
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003570static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003571 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003572 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003573 .write = ftrace_notrace_write,
3574 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003575 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003576};
3577
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003578#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3579
3580static DEFINE_MUTEX(graph_lock);
3581
3582int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003583int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003584unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3585
3586static void *
Li Zefan85951842009-06-24 09:54:00 +08003587__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003588{
Li Zefan85951842009-06-24 09:54:00 +08003589 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003590 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003591 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003592}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003593
Li Zefan85951842009-06-24 09:54:00 +08003594static void *
3595g_next(struct seq_file *m, void *v, loff_t *pos)
3596{
3597 (*pos)++;
3598 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003599}
3600
3601static void *g_start(struct seq_file *m, loff_t *pos)
3602{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003603 mutex_lock(&graph_lock);
3604
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003605 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003606 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003607 return (void *)1;
3608
Li Zefan85951842009-06-24 09:54:00 +08003609 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003610}
3611
3612static void g_stop(struct seq_file *m, void *p)
3613{
3614 mutex_unlock(&graph_lock);
3615}
3616
3617static int g_show(struct seq_file *m, void *v)
3618{
3619 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003620
3621 if (!ptr)
3622 return 0;
3623
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003624 if (ptr == (unsigned long *)1) {
3625 seq_printf(m, "#### all functions enabled ####\n");
3626 return 0;
3627 }
3628
Steven Rostedtb375a112009-09-17 00:05:58 -04003629 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003630
3631 return 0;
3632}
3633
James Morris88e9d342009-09-22 16:43:43 -07003634static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003635 .start = g_start,
3636 .next = g_next,
3637 .stop = g_stop,
3638 .show = g_show,
3639};
3640
3641static int
3642ftrace_graph_open(struct inode *inode, struct file *file)
3643{
3644 int ret = 0;
3645
3646 if (unlikely(ftrace_disabled))
3647 return -ENODEV;
3648
3649 mutex_lock(&graph_lock);
3650 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003651 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003652 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003653 ftrace_graph_count = 0;
3654 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3655 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003656 mutex_unlock(&graph_lock);
3657
Li Zefana4ec5e02009-09-18 14:06:28 +08003658 if (file->f_mode & FMODE_READ)
3659 ret = seq_open(file, &ftrace_graph_seq_ops);
3660
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003661 return ret;
3662}
3663
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003664static int
Li Zefan87827112009-07-23 11:29:11 +08003665ftrace_graph_release(struct inode *inode, struct file *file)
3666{
3667 if (file->f_mode & FMODE_READ)
3668 seq_release(inode, file);
3669 return 0;
3670}
3671
3672static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003673ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003674{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003675 struct dyn_ftrace *rec;
3676 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003677 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003678 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003679 int type, not;
3680 char *search;
3681 bool exists;
3682 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003683
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003684 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003685 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003686 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3687 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003688
3689 search_len = strlen(search);
3690
Steven Rostedt52baf112009-02-14 01:15:39 -05003691 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003692
3693 if (unlikely(ftrace_disabled)) {
3694 mutex_unlock(&ftrace_lock);
3695 return -ENODEV;
3696 }
3697
Steven Rostedt265c8312009-02-13 12:43:56 -05003698 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003699
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003700 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003701 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003702 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003703 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003704 if (array[i] == rec->ip) {
3705 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003706 break;
3707 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003708 }
3709
3710 if (!not) {
3711 fail = 0;
3712 if (!exists) {
3713 array[(*idx)++] = rec->ip;
3714 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3715 goto out;
3716 }
3717 } else {
3718 if (exists) {
3719 array[i] = array[--(*idx)];
3720 array[*idx] = 0;
3721 fail = 0;
3722 }
3723 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003724 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003725 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003726out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003727 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003728
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003729 if (fail)
3730 return -EINVAL;
3731
3732 ftrace_graph_filter_enabled = 1;
3733 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003734}
3735
3736static ssize_t
3737ftrace_graph_write(struct file *file, const char __user *ubuf,
3738 size_t cnt, loff_t *ppos)
3739{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003740 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003741 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003742
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003743 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003744 return 0;
3745
3746 mutex_lock(&graph_lock);
3747
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003748 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3749 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003750 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003751 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003752
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003753 read = trace_get_user(&parser, ubuf, cnt, ppos);
3754
Li Zefan4ba79782009-09-22 13:52:20 +08003755 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003756 parser.buffer[parser.idx] = 0;
3757
3758 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003759 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003760 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003761 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003762 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003763 }
3764
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003765 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003766
3767out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003768 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003769out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003770 mutex_unlock(&graph_lock);
3771
3772 return ret;
3773}
3774
3775static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003776 .open = ftrace_graph_open,
3777 .read = seq_read,
3778 .write = ftrace_graph_write,
3779 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003780 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003781};
3782#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3783
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003784static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003785{
Steven Rostedt5072c592008-05-12 21:20:43 +02003786
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003787 trace_create_file("available_filter_functions", 0444,
3788 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003789
Steven Rostedt647bcd02011-05-03 14:39:21 -04003790 trace_create_file("enabled_functions", 0444,
3791 d_tracer, NULL, &ftrace_enabled_fops);
3792
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003793 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3794 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003795
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003796 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003797 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003798
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003799#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003800 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003801 NULL,
3802 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003803#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3804
Steven Rostedt5072c592008-05-12 21:20:43 +02003805 return 0;
3806}
3807
Steven Rostedt9fd49322012-04-24 22:32:06 -04003808static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003809{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003810 const unsigned long *ipa = a;
3811 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003812
Steven Rostedt9fd49322012-04-24 22:32:06 -04003813 if (*ipa > *ipb)
3814 return 1;
3815 if (*ipa < *ipb)
3816 return -1;
3817 return 0;
3818}
3819
3820static void ftrace_swap_ips(void *a, void *b, int size)
3821{
3822 unsigned long *ipa = a;
3823 unsigned long *ipb = b;
3824 unsigned long t;
3825
3826 t = *ipa;
3827 *ipa = *ipb;
3828 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003829}
3830
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003831static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003832 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003833 unsigned long *end)
3834{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003835 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003836 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003837 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003838 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003839 unsigned long *p;
3840 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003841 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003842 int ret = -ENOMEM;
3843
3844 count = end - start;
3845
3846 if (!count)
3847 return 0;
3848
Steven Rostedt9fd49322012-04-24 22:32:06 -04003849 sort(start, count, sizeof(*start),
3850 ftrace_cmp_ips, ftrace_swap_ips);
3851
Steven Rostedt706c81f2012-04-24 23:45:26 -04003852 start_pg = ftrace_allocate_pages(count);
3853 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003854 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003855
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003856 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003857
Steven Rostedt32082302011-12-16 14:42:37 -05003858 /*
3859 * Core and each module needs their own pages, as
3860 * modules will free them when they are removed.
3861 * Force a new page to be allocated for modules.
3862 */
Steven Rostedta7900872011-12-16 16:23:44 -05003863 if (!mod) {
3864 WARN_ON(ftrace_pages || ftrace_pages_start);
3865 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003866 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003867 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003868 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003869 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003870
Steven Rostedta7900872011-12-16 16:23:44 -05003871 if (WARN_ON(ftrace_pages->next)) {
3872 /* Hmm, we have free pages? */
3873 while (ftrace_pages->next)
3874 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003875 }
Steven Rostedta7900872011-12-16 16:23:44 -05003876
Steven Rostedt706c81f2012-04-24 23:45:26 -04003877 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003878 }
3879
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003880 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003881 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003882 while (p < end) {
3883 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003884 /*
3885 * Some architecture linkers will pad between
3886 * the different mcount_loc sections of different
3887 * object files to satisfy alignments.
3888 * Skip any NULL pointers.
3889 */
3890 if (!addr)
3891 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003892
3893 if (pg->index == pg->size) {
3894 /* We should have allocated enough */
3895 if (WARN_ON(!pg->next))
3896 break;
3897 pg = pg->next;
3898 }
3899
3900 rec = &pg->records[pg->index++];
3901 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003902 }
3903
Steven Rostedt706c81f2012-04-24 23:45:26 -04003904 /* We should have used all pages */
3905 WARN_ON(pg->next);
3906
3907 /* Assign the last page to ftrace_pages */
3908 ftrace_pages = pg;
3909
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003910 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003911 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003912
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003913 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003914 * We only need to disable interrupts on start up
3915 * because we are modifying code that an interrupt
3916 * may execute, and the modification is not atomic.
3917 * But for modules, nothing runs the code we modify
3918 * until we are finished with it, and there's no
3919 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003920 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003921 if (!mod)
3922 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003923 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003924 if (!mod)
3925 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003926 ret = 0;
3927 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003928 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003929
Steven Rostedta7900872011-12-16 16:23:44 -05003930 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003931}
3932
Steven Rostedt93eb6772009-04-15 13:24:06 -04003933#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003934
3935#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3936
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003937void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003938{
3939 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003940 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003941 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003942 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003943
Steven Rostedt93eb6772009-04-15 13:24:06 -04003944 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003945
3946 if (ftrace_disabled)
3947 goto out_unlock;
3948
Steven Rostedt32082302011-12-16 14:42:37 -05003949 /*
3950 * Each module has its own ftrace_pages, remove
3951 * them from the list.
3952 */
3953 last_pg = &ftrace_pages_start;
3954 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3955 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003956 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003957 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003958 * As core pages are first, the first
3959 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003960 */
Steven Rostedt32082302011-12-16 14:42:37 -05003961 if (WARN_ON(pg == ftrace_pages_start))
3962 goto out_unlock;
3963
3964 /* Check if we are deleting the last page */
3965 if (pg == ftrace_pages)
3966 ftrace_pages = next_to_ftrace_page(last_pg);
3967
3968 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003969 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3970 free_pages((unsigned long)pg->records, order);
3971 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003972 } else
3973 last_pg = &pg->next;
3974 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003975 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003976 mutex_unlock(&ftrace_lock);
3977}
3978
3979static void ftrace_init_module(struct module *mod,
3980 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003981{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003982 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003983 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003984 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003985}
3986
Steven Rostedt93eb6772009-04-15 13:24:06 -04003987static int ftrace_module_notify(struct notifier_block *self,
3988 unsigned long val, void *data)
3989{
3990 struct module *mod = data;
3991
3992 switch (val) {
3993 case MODULE_STATE_COMING:
3994 ftrace_init_module(mod, mod->ftrace_callsites,
3995 mod->ftrace_callsites +
3996 mod->num_ftrace_callsites);
3997 break;
3998 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003999 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004000 break;
4001 }
4002
4003 return 0;
4004}
4005#else
4006static int ftrace_module_notify(struct notifier_block *self,
4007 unsigned long val, void *data)
4008{
4009 return 0;
4010}
4011#endif /* CONFIG_MODULES */
4012
4013struct notifier_block ftrace_module_nb = {
4014 .notifier_call = ftrace_module_notify,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004015 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004016};
4017
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004018extern unsigned long __start_mcount_loc[];
4019extern unsigned long __stop_mcount_loc[];
4020
4021void __init ftrace_init(void)
4022{
4023 unsigned long count, addr, flags;
4024 int ret;
4025
4026 /* Keep the ftrace pointer to the stub */
4027 addr = (unsigned long)ftrace_stub;
4028
4029 local_irq_save(flags);
4030 ftrace_dyn_arch_init(&addr);
4031 local_irq_restore(flags);
4032
4033 /* ftrace_dyn_arch_init places the return code in addr */
4034 if (addr)
4035 goto failed;
4036
4037 count = __stop_mcount_loc - __start_mcount_loc;
4038
4039 ret = ftrace_dyn_table_alloc(count);
4040 if (ret)
4041 goto failed;
4042
4043 last_ftrace_enabled = ftrace_enabled = 1;
4044
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004045 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004046 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004047 __stop_mcount_loc);
4048
Steven Rostedt93eb6772009-04-15 13:24:06 -04004049 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004050 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004051 pr_warning("Failed to register trace ftrace module notifier\n");
4052
Steven Rostedt2af15d62009-05-28 13:37:24 -04004053 set_ftrace_early_filters();
4054
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004055 return;
4056 failed:
4057 ftrace_disabled = 1;
4058}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004059
Steven Rostedt3d083392008-05-12 21:20:42 +02004060#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004061
Steven Rostedt2b499382011-05-03 22:49:52 -04004062static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004063 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004064 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004065};
4066
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004067static int __init ftrace_nodyn_init(void)
4068{
4069 ftrace_enabled = 1;
4070 return 0;
4071}
Steven Rostedt6f415672012-10-05 12:13:07 -04004072core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004073
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004074static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4075static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004076/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004077# define ftrace_startup(ops, command) \
4078 ({ \
4079 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4080 0; \
4081 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004082# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004083# define ftrace_startup_sysctl() do { } while (0)
4084# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004085
4086static inline int
4087ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4088{
4089 return 1;
4090}
4091
Steven Rostedt3d083392008-05-12 21:20:42 +02004092#endif /* CONFIG_DYNAMIC_FTRACE */
4093
Steven Rostedtb8489142011-05-04 09:27:52 -04004094static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004095ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004096 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004097{
Jiri Olsae2484912012-02-15 15:51:48 +01004098 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4099 return;
4100
4101 /*
4102 * Some of the ops may be dynamically allocated,
4103 * they must be freed after a synchronize_sched().
4104 */
4105 preempt_disable_notrace();
4106 trace_recursion_set(TRACE_CONTROL_BIT);
4107 op = rcu_dereference_raw(ftrace_control_list);
4108 while (op != &ftrace_list_end) {
4109 if (!ftrace_function_local_disabled(op) &&
4110 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004111 op->func(ip, parent_ip, op, regs);
Jiri Olsae2484912012-02-15 15:51:48 +01004112
4113 op = rcu_dereference_raw(op->next);
4114 };
4115 trace_recursion_clear(TRACE_CONTROL_BIT);
4116 preempt_enable_notrace();
4117}
4118
4119static struct ftrace_ops control_ops = {
4120 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004121 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004122};
4123
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004124static inline void
4125__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004126 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004127{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004128 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004129
Steven Rostedtccf36722012-06-05 09:44:25 -04004130 if (function_trace_stop)
4131 return;
4132
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004133 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4134 return;
4135
4136 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004137 /*
4138 * Some of the ops may be dynamically allocated,
4139 * they must be freed after a synchronize_sched().
4140 */
4141 preempt_disable_notrace();
4142 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004143 while (op != &ftrace_list_end) {
4144 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004145 op->func(ip, parent_ip, op, regs);
Steven Rostedtb8489142011-05-04 09:27:52 -04004146 op = rcu_dereference_raw(op->next);
4147 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004148 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004149 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004150}
4151
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004152/*
4153 * Some archs only support passing ip and parent_ip. Even though
4154 * the list function ignores the op parameter, we do not want any
4155 * C side effects, where a function is called without the caller
4156 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004157 * Archs are to support both the regs and ftrace_ops at the same time.
4158 * If they support ftrace_ops, it is assumed they support regs.
4159 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004160 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4161 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004162 * An architecture can pass partial regs with ftrace_ops and still
4163 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004164 */
4165#if ARCH_SUPPORTS_FTRACE_OPS
4166static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004167 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004168{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004169 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004170}
4171#else
4172static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4173{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004174 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004175}
4176#endif
4177
Steven Rostedte32d8952008-12-04 00:26:41 -05004178static void clear_ftrace_swapper(void)
4179{
4180 struct task_struct *p;
4181 int cpu;
4182
4183 get_online_cpus();
4184 for_each_online_cpu(cpu) {
4185 p = idle_task(cpu);
4186 clear_tsk_trace_trace(p);
4187 }
4188 put_online_cpus();
4189}
4190
4191static void set_ftrace_swapper(void)
4192{
4193 struct task_struct *p;
4194 int cpu;
4195
4196 get_online_cpus();
4197 for_each_online_cpu(cpu) {
4198 p = idle_task(cpu);
4199 set_tsk_trace_trace(p);
4200 }
4201 put_online_cpus();
4202}
4203
4204static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004205{
4206 struct task_struct *p;
4207
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004208 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004209 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004210 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004211 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004212 rcu_read_unlock();
4213
Steven Rostedte32d8952008-12-04 00:26:41 -05004214 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004215}
4216
Steven Rostedte32d8952008-12-04 00:26:41 -05004217static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004218{
4219 struct task_struct *p;
4220
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004221 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004222 do_each_pid_task(pid, PIDTYPE_PID, p) {
4223 set_tsk_trace_trace(p);
4224 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004225 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004226}
4227
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004228static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004229{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004230 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004231 clear_ftrace_swapper();
4232 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004233 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004234}
4235
4236static void set_ftrace_pid_task(struct pid *pid)
4237{
4238 if (pid == ftrace_swapper_pid)
4239 set_ftrace_swapper();
4240 else
4241 set_ftrace_pid(pid);
4242}
4243
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004244static int ftrace_pid_add(int p)
4245{
4246 struct pid *pid;
4247 struct ftrace_pid *fpid;
4248 int ret = -EINVAL;
4249
4250 mutex_lock(&ftrace_lock);
4251
4252 if (!p)
4253 pid = ftrace_swapper_pid;
4254 else
4255 pid = find_get_pid(p);
4256
4257 if (!pid)
4258 goto out;
4259
4260 ret = 0;
4261
4262 list_for_each_entry(fpid, &ftrace_pids, list)
4263 if (fpid->pid == pid)
4264 goto out_put;
4265
4266 ret = -ENOMEM;
4267
4268 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4269 if (!fpid)
4270 goto out_put;
4271
4272 list_add(&fpid->list, &ftrace_pids);
4273 fpid->pid = pid;
4274
4275 set_ftrace_pid_task(pid);
4276
4277 ftrace_update_pid_func();
4278 ftrace_startup_enable(0);
4279
4280 mutex_unlock(&ftrace_lock);
4281 return 0;
4282
4283out_put:
4284 if (pid != ftrace_swapper_pid)
4285 put_pid(pid);
4286
4287out:
4288 mutex_unlock(&ftrace_lock);
4289 return ret;
4290}
4291
4292static void ftrace_pid_reset(void)
4293{
4294 struct ftrace_pid *fpid, *safe;
4295
4296 mutex_lock(&ftrace_lock);
4297 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4298 struct pid *pid = fpid->pid;
4299
4300 clear_ftrace_pid_task(pid);
4301
4302 list_del(&fpid->list);
4303 kfree(fpid);
4304 }
4305
4306 ftrace_update_pid_func();
4307 ftrace_startup_enable(0);
4308
4309 mutex_unlock(&ftrace_lock);
4310}
4311
4312static void *fpid_start(struct seq_file *m, loff_t *pos)
4313{
4314 mutex_lock(&ftrace_lock);
4315
4316 if (list_empty(&ftrace_pids) && (!*pos))
4317 return (void *) 1;
4318
4319 return seq_list_start(&ftrace_pids, *pos);
4320}
4321
4322static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4323{
4324 if (v == (void *)1)
4325 return NULL;
4326
4327 return seq_list_next(v, &ftrace_pids, pos);
4328}
4329
4330static void fpid_stop(struct seq_file *m, void *p)
4331{
4332 mutex_unlock(&ftrace_lock);
4333}
4334
4335static int fpid_show(struct seq_file *m, void *v)
4336{
4337 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4338
4339 if (v == (void *)1) {
4340 seq_printf(m, "no pid\n");
4341 return 0;
4342 }
4343
4344 if (fpid->pid == ftrace_swapper_pid)
4345 seq_printf(m, "swapper tasks\n");
4346 else
4347 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4348
4349 return 0;
4350}
4351
4352static const struct seq_operations ftrace_pid_sops = {
4353 .start = fpid_start,
4354 .next = fpid_next,
4355 .stop = fpid_stop,
4356 .show = fpid_show,
4357};
4358
4359static int
4360ftrace_pid_open(struct inode *inode, struct file *file)
4361{
4362 int ret = 0;
4363
4364 if ((file->f_mode & FMODE_WRITE) &&
4365 (file->f_flags & O_TRUNC))
4366 ftrace_pid_reset();
4367
4368 if (file->f_mode & FMODE_READ)
4369 ret = seq_open(file, &ftrace_pid_sops);
4370
4371 return ret;
4372}
4373
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004374static ssize_t
4375ftrace_pid_write(struct file *filp, const char __user *ubuf,
4376 size_t cnt, loff_t *ppos)
4377{
Ingo Molnar457dc922009-11-23 11:03:28 +01004378 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004379 long val;
4380 int ret;
4381
4382 if (cnt >= sizeof(buf))
4383 return -EINVAL;
4384
4385 if (copy_from_user(&buf, ubuf, cnt))
4386 return -EFAULT;
4387
4388 buf[cnt] = 0;
4389
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004390 /*
4391 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4392 * to clean the filter quietly.
4393 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004394 tmp = strstrip(buf);
4395 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004396 return 1;
4397
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004398 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004399 if (ret < 0)
4400 return ret;
4401
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004402 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004403
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004404 return ret ? ret : cnt;
4405}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004406
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004407static int
4408ftrace_pid_release(struct inode *inode, struct file *file)
4409{
4410 if (file->f_mode & FMODE_READ)
4411 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004412
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004413 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004414}
4415
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004416static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004417 .open = ftrace_pid_open,
4418 .write = ftrace_pid_write,
4419 .read = seq_read,
4420 .llseek = seq_lseek,
4421 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004422};
4423
4424static __init int ftrace_init_debugfs(void)
4425{
4426 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004427
4428 d_tracer = tracing_init_dentry();
4429 if (!d_tracer)
4430 return 0;
4431
4432 ftrace_init_dyn_debugfs(d_tracer);
4433
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004434 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4435 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004436
4437 ftrace_profile_debugfs(d_tracer);
4438
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004439 return 0;
4440}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004441fs_initcall(ftrace_init_debugfs);
4442
Steven Rostedt3d083392008-05-12 21:20:42 +02004443/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004444 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004445 *
4446 * This function should be used by panic code. It stops ftrace
4447 * but in a not so nice way. If you need to simply kill ftrace
4448 * from a non-atomic section, use ftrace_kill.
4449 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004450void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004451{
4452 ftrace_disabled = 1;
4453 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004454 clear_ftrace_function();
4455}
4456
4457/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004458 * Test if ftrace is dead or not.
4459 */
4460int ftrace_is_dead(void)
4461{
4462 return ftrace_disabled;
4463}
4464
4465/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004466 * register_ftrace_function - register a function for profiling
4467 * @ops - ops structure that holds the function for profiling.
4468 *
4469 * Register a function to be called by all functions in the
4470 * kernel.
4471 *
4472 * Note: @ops->func and all the functions it calls must be labeled
4473 * with "notrace", otherwise it will go into a
4474 * recursive loop.
4475 */
4476int register_ftrace_function(struct ftrace_ops *ops)
4477{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004478 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004479
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004480 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004481
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004482 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004483 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004484 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004485
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004486 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004487
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004488 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004489}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004490EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004491
4492/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004493 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004494 * @ops - ops structure that holds the function to unregister
4495 *
4496 * Unregister a function that was added to be called by ftrace profiling.
4497 */
4498int unregister_ftrace_function(struct ftrace_ops *ops)
4499{
4500 int ret;
4501
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004502 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004503 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004504 if (!ret)
4505 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004506 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004507
4508 return ret;
4509}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004510EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004511
Ingo Molnare309b412008-05-12 21:20:51 +02004512int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004513ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004514 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004515 loff_t *ppos)
4516{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004517 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004518
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004519 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004520
Steven Rostedt45a4a232011-04-21 23:16:46 -04004521 if (unlikely(ftrace_disabled))
4522 goto out;
4523
4524 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004525
Li Zefana32c7762009-06-26 16:55:51 +08004526 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004527 goto out;
4528
Li Zefana32c7762009-06-26 16:55:51 +08004529 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004530
4531 if (ftrace_enabled) {
4532
4533 ftrace_startup_sysctl();
4534
4535 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004536 if (ftrace_ops_list != &ftrace_list_end) {
4537 if (ftrace_ops_list->next == &ftrace_list_end)
4538 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004539 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004540 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004541 }
4542
4543 } else {
4544 /* stopping ftrace calls (just send to ftrace_stub) */
4545 ftrace_trace_function = ftrace_stub;
4546
4547 ftrace_shutdown_sysctl();
4548 }
4549
4550 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004551 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004552 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004553}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004554
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004555#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004556
Steven Rostedt597af812009-04-03 15:24:12 -04004557static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004558static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004559
Steven Rostedte49dc192008-12-02 23:50:05 -05004560int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4561{
4562 return 0;
4563}
4564
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004565/* The callbacks that hook a function */
4566trace_func_graph_ret_t ftrace_graph_return =
4567 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004568trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004569
4570/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4571static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4572{
4573 int i;
4574 int ret = 0;
4575 unsigned long flags;
4576 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4577 struct task_struct *g, *t;
4578
4579 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4580 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4581 * sizeof(struct ftrace_ret_stack),
4582 GFP_KERNEL);
4583 if (!ret_stack_list[i]) {
4584 start = 0;
4585 end = i;
4586 ret = -ENOMEM;
4587 goto free;
4588 }
4589 }
4590
4591 read_lock_irqsave(&tasklist_lock, flags);
4592 do_each_thread(g, t) {
4593 if (start == end) {
4594 ret = -EAGAIN;
4595 goto unlock;
4596 }
4597
4598 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004599 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004600 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004601 t->curr_ret_stack = -1;
4602 /* Make sure the tasks see the -1 first: */
4603 smp_wmb();
4604 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004605 }
4606 } while_each_thread(g, t);
4607
4608unlock:
4609 read_unlock_irqrestore(&tasklist_lock, flags);
4610free:
4611 for (i = start; i < end; i++)
4612 kfree(ret_stack_list[i]);
4613 return ret;
4614}
4615
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004616static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004617ftrace_graph_probe_sched_switch(void *ignore,
4618 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004619{
4620 unsigned long long timestamp;
4621 int index;
4622
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004623 /*
4624 * Does the user want to count the time a function was asleep.
4625 * If so, do not update the time stamps.
4626 */
4627 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4628 return;
4629
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004630 timestamp = trace_clock_local();
4631
4632 prev->ftrace_timestamp = timestamp;
4633
4634 /* only process tasks that we timestamped */
4635 if (!next->ftrace_timestamp)
4636 return;
4637
4638 /*
4639 * Update all the counters in next to make up for the
4640 * time next was sleeping.
4641 */
4642 timestamp -= next->ftrace_timestamp;
4643
4644 for (index = next->curr_ret_stack; index >= 0; index--)
4645 next->ret_stack[index].calltime += timestamp;
4646}
4647
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004648/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004649static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004650{
4651 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004652 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004653
4654 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4655 sizeof(struct ftrace_ret_stack *),
4656 GFP_KERNEL);
4657
4658 if (!ret_stack_list)
4659 return -ENOMEM;
4660
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004661 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004662 for_each_online_cpu(cpu) {
4663 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004664 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004665 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004666
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004667 do {
4668 ret = alloc_retstack_tasklist(ret_stack_list);
4669 } while (ret == -EAGAIN);
4670
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004671 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004672 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004673 if (ret)
4674 pr_info("ftrace_graph: Couldn't activate tracepoint"
4675 " probe to kernel_sched_switch\n");
4676 }
4677
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004678 kfree(ret_stack_list);
4679 return ret;
4680}
4681
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004682/*
4683 * Hibernation protection.
4684 * The state of the current task is too much unstable during
4685 * suspend/restore to disk. We want to protect against that.
4686 */
4687static int
4688ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4689 void *unused)
4690{
4691 switch (state) {
4692 case PM_HIBERNATION_PREPARE:
4693 pause_graph_tracing();
4694 break;
4695
4696 case PM_POST_HIBERNATION:
4697 unpause_graph_tracing();
4698 break;
4699 }
4700 return NOTIFY_DONE;
4701}
4702
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004703int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4704 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004705{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004706 int ret = 0;
4707
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004708 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004709
Steven Rostedt05ce5812009-03-24 00:18:31 -04004710 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004711 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004712 ret = -EBUSY;
4713 goto out;
4714 }
4715
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004716 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4717 register_pm_notifier(&ftrace_suspend_notifier);
4718
Steven Rostedt597af812009-04-03 15:24:12 -04004719 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004720 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004721 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004722 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004723 goto out;
4724 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004725
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004726 ftrace_graph_return = retfunc;
4727 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004728
Steven Rostedta1cd6172011-05-23 15:24:25 -04004729 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004730
4731out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004732 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004733 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004734}
4735
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004736void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004737{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004738 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004739
Steven Rostedt597af812009-04-03 15:24:12 -04004740 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004741 goto out;
4742
Steven Rostedt597af812009-04-03 15:24:12 -04004743 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004744 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004745 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004746 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004747 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004748 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004749
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004750 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004751 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004752}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004753
Steven Rostedt868baf02011-02-10 21:26:13 -05004754static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4755
4756static void
4757graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4758{
4759 atomic_set(&t->tracing_graph_pause, 0);
4760 atomic_set(&t->trace_overrun, 0);
4761 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004762 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004763 smp_wmb();
4764 t->ret_stack = ret_stack;
4765}
4766
4767/*
4768 * Allocate a return stack for the idle task. May be the first
4769 * time through, or it may be done by CPU hotplug online.
4770 */
4771void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4772{
4773 t->curr_ret_stack = -1;
4774 /*
4775 * The idle task has no parent, it either has its own
4776 * stack or no stack at all.
4777 */
4778 if (t->ret_stack)
4779 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4780
4781 if (ftrace_graph_active) {
4782 struct ftrace_ret_stack *ret_stack;
4783
4784 ret_stack = per_cpu(idle_ret_stack, cpu);
4785 if (!ret_stack) {
4786 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4787 * sizeof(struct ftrace_ret_stack),
4788 GFP_KERNEL);
4789 if (!ret_stack)
4790 return;
4791 per_cpu(idle_ret_stack, cpu) = ret_stack;
4792 }
4793 graph_init_task(t, ret_stack);
4794 }
4795}
4796
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004797/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004798void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004799{
Steven Rostedt84047e32009-06-02 16:51:55 -04004800 /* Make sure we do not use the parent ret_stack */
4801 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004802 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004803
Steven Rostedt597af812009-04-03 15:24:12 -04004804 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004805 struct ftrace_ret_stack *ret_stack;
4806
4807 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004808 * sizeof(struct ftrace_ret_stack),
4809 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004810 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004811 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004812 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004813 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004814}
4815
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004816void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004817{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004818 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4819
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004820 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004821 /* NULL must become visible to IRQs before we free it: */
4822 barrier();
4823
4824 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004825}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004826
4827void ftrace_graph_stop(void)
4828{
4829 ftrace_stop();
4830}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004831#endif