blob: 3b84fc1007883b09b0e9dc490ebac99470a5b87a [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 Rostedt0a016402012-11-02 17:03:03 -0400114/*
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
119 * concurrent insertions into the ftrace_global_list.
120 *
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
122 */
123#define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
125 do
126
127/*
128 * Optimized for just a single item in the list (as that is the normal case).
129 */
130#define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
133
Steven Rostedtea701f12012-07-20 13:08:05 -0400134/**
135 * ftrace_nr_registered_ops - return number of ops registered
136 *
137 * Returns the number of ftrace_ops registered and tracing functions
138 */
139int ftrace_nr_registered_ops(void)
140{
141 struct ftrace_ops *ops;
142 int cnt = 0;
143
144 mutex_lock(&ftrace_lock);
145
146 for (ops = ftrace_ops_list;
147 ops != &ftrace_list_end; ops = ops->next)
148 cnt++;
149
150 mutex_unlock(&ftrace_lock);
151
152 return cnt;
153}
154
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400155static void
156ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400157 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400159 int bit;
160
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400161 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
162 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400163 return;
164
Steven Rostedt0a016402012-11-02 17:03:03 -0400165 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400166 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400167 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400168
169 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200170}
171
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400172static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400173 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500174{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500175 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500176 return;
177
Steven Rostedta1e2e312011-08-09 12:50:46 -0400178 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179}
180
181static void set_ftrace_pid_function(ftrace_func_t func)
182{
183 /* do not set ftrace_pid_function to itself! */
184 if (func != ftrace_pid_func)
185 ftrace_pid_function = func;
186}
187
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200188/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200189 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200191 * This NULLs the ftrace function and in essence stops
192 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200193 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200194void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195{
Steven Rostedt3d083392008-05-12 21:20:42 +0200196 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500197 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200198}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200199
Jiri Olsae2484912012-02-15 15:51:48 +0100200static void control_ops_disable_all(struct ftrace_ops *ops)
201{
202 int cpu;
203
204 for_each_possible_cpu(cpu)
205 *per_cpu_ptr(ops->disabled, cpu) = 1;
206}
207
208static int control_ops_alloc(struct ftrace_ops *ops)
209{
210 int __percpu *disabled;
211
212 disabled = alloc_percpu(int);
213 if (!disabled)
214 return -ENOMEM;
215
216 ops->disabled = disabled;
217 control_ops_disable_all(ops);
218 return 0;
219}
220
221static void control_ops_free(struct ftrace_ops *ops)
222{
223 free_percpu(ops->disabled);
224}
225
Steven Rostedt2b499382011-05-03 22:49:52 -0400226static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400227{
228 ftrace_func_t func;
229
230 /*
231 * If there's only one function registered, then call that
232 * function directly. Otherwise, we need to iterate over the
233 * registered callers.
234 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400235 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400236 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400237 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400238 /*
239 * As we are calling the function directly.
240 * If it does not have recursion protection,
241 * the function_trace_op needs to be updated
242 * accordingly.
243 */
244 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
245 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
246 else
247 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
248 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400249 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
252 }
253
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400254
255 /* If we filter on pids, update to use the pid function */
256 if (!list_empty(&ftrace_pids)) {
257 set_ftrace_pid_function(func);
258 func = ftrace_pid_func;
259 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400260
261 global_ops.func = func;
262}
263
264static void update_ftrace_function(void)
265{
266 ftrace_func_t func;
267
268 update_global_ops();
269
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400270 /*
271 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400272 * recursion safe and not dynamic and the arch supports passing ops,
273 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400274 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400275 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400276 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400277 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400278 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400279 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400280 /* Set the ftrace_ops that the arch callback uses */
281 if (ftrace_ops_list == &global_ops)
282 function_trace_op = ftrace_global_list;
283 else
284 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400285 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400286 } else {
287 /* Just use the default ftrace_ops */
288 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400289 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400290 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400291
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400292 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400293}
294
Steven Rostedt2b499382011-05-03 22:49:52 -0400295static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200296{
Steven Rostedt2b499382011-05-03 22:49:52 -0400297 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200298 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400299 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200300 * CPU might be walking that list. We need to make sure
301 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400302 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200303 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400304 rcu_assign_pointer(*list, ops);
305}
Steven Rostedt3d083392008-05-12 21:20:42 +0200306
Steven Rostedt2b499382011-05-03 22:49:52 -0400307static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
308{
309 struct ftrace_ops **p;
310
311 /*
312 * If we are removing the last function, then simply point
313 * to the ftrace_stub.
314 */
315 if (*list == ops && ops->next == &ftrace_list_end) {
316 *list = &ftrace_list_end;
317 return 0;
318 }
319
320 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
321 if (*p == ops)
322 break;
323
324 if (*p != ops)
325 return -1;
326
327 *p = (*p)->next;
328 return 0;
329}
330
Jiri Olsae2484912012-02-15 15:51:48 +0100331static void add_ftrace_list_ops(struct ftrace_ops **list,
332 struct ftrace_ops *main_ops,
333 struct ftrace_ops *ops)
334{
335 int first = *list == &ftrace_list_end;
336 add_ftrace_ops(list, ops);
337 if (first)
338 add_ftrace_ops(&ftrace_ops_list, main_ops);
339}
340
341static int remove_ftrace_list_ops(struct ftrace_ops **list,
342 struct ftrace_ops *main_ops,
343 struct ftrace_ops *ops)
344{
345 int ret = remove_ftrace_ops(list, ops);
346 if (!ret && *list == &ftrace_list_end)
347 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
348 return ret;
349}
350
Steven Rostedt2b499382011-05-03 22:49:52 -0400351static int __register_ftrace_function(struct ftrace_ops *ops)
352{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200353 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400354 return -ENODEV;
355
356 if (FTRACE_WARN_ON(ops == &global_ops))
357 return -EINVAL;
358
Steven Rostedtb8489142011-05-04 09:27:52 -0400359 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
360 return -EBUSY;
361
Jiri Olsae2484912012-02-15 15:51:48 +0100362 /* We don't support both control and global flags set. */
363 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
364 return -EINVAL;
365
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900366#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400367 /*
368 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
369 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
370 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
371 */
372 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
373 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
374 return -EINVAL;
375
376 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
377 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
378#endif
379
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400380 if (!core_kernel_data((unsigned long)ops))
381 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
382
Steven Rostedtb8489142011-05-04 09:27:52 -0400383 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100384 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400385 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100386 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
387 if (control_ops_alloc(ops))
388 return -ENOMEM;
389 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400390 } else
391 add_ftrace_ops(&ftrace_ops_list, ops);
392
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400393 if (ftrace_enabled)
394 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200395
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200396 return 0;
397}
398
Ingo Molnare309b412008-05-12 21:20:51 +0200399static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200400{
Steven Rostedt2b499382011-05-03 22:49:52 -0400401 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200402
Steven Rostedt2b499382011-05-03 22:49:52 -0400403 if (ftrace_disabled)
404 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200405
Steven Rostedtb8489142011-05-04 09:27:52 -0400406 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
407 return -EBUSY;
408
Steven Rostedt2b499382011-05-03 22:49:52 -0400409 if (FTRACE_WARN_ON(ops == &global_ops))
410 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200411
Steven Rostedtb8489142011-05-04 09:27:52 -0400412 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100413 ret = remove_ftrace_list_ops(&ftrace_global_list,
414 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400415 if (!ret)
416 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100417 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
418 ret = remove_ftrace_list_ops(&ftrace_control_list,
419 &control_ops, ops);
420 if (!ret) {
421 /*
422 * The ftrace_ops is now removed from the list,
423 * so there'll be no new users. We must ensure
424 * all current users are done before we free
425 * the control data.
426 */
427 synchronize_sched();
428 control_ops_free(ops);
429 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400430 } else
431 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
432
Steven Rostedt2b499382011-05-03 22:49:52 -0400433 if (ret < 0)
434 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400435
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400436 if (ftrace_enabled)
437 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200438
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400439 /*
440 * Dynamic ops may be freed, we must make sure that all
441 * callers are done before leaving this function.
442 */
443 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
444 synchronize_sched();
445
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500446 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200447}
448
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500449static void ftrace_update_pid_func(void)
450{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400451 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500452 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900453 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500454
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400455 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500456}
457
Steven Rostedt493762f2009-03-23 17:12:36 -0400458#ifdef CONFIG_FUNCTION_PROFILER
459struct ftrace_profile {
460 struct hlist_node node;
461 unsigned long ip;
462 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463#ifdef CONFIG_FUNCTION_GRAPH_TRACER
464 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400465 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400466#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400467};
468
469struct ftrace_profile_page {
470 struct ftrace_profile_page *next;
471 unsigned long index;
472 struct ftrace_profile records[];
473};
474
Steven Rostedtcafb1682009-03-24 20:50:39 -0400475struct ftrace_profile_stat {
476 atomic_t disabled;
477 struct hlist_head *hash;
478 struct ftrace_profile_page *pages;
479 struct ftrace_profile_page *start;
480 struct tracer_stat stat;
481};
482
Steven Rostedt493762f2009-03-23 17:12:36 -0400483#define PROFILE_RECORDS_SIZE \
484 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
485
486#define PROFILES_PER_PAGE \
487 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
488
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400489static int ftrace_profile_bits __read_mostly;
490static int ftrace_profile_enabled __read_mostly;
491
492/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400493static DEFINE_MUTEX(ftrace_profile_lock);
494
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400496
497#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
498
Steven Rostedt493762f2009-03-23 17:12:36 -0400499static void *
500function_stat_next(void *v, int idx)
501{
502 struct ftrace_profile *rec = v;
503 struct ftrace_profile_page *pg;
504
505 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
506
507 again:
Li Zefan0296e422009-06-26 11:15:37 +0800508 if (idx != 0)
509 rec++;
510
Steven Rostedt493762f2009-03-23 17:12:36 -0400511 if ((void *)rec >= (void *)&pg->records[pg->index]) {
512 pg = pg->next;
513 if (!pg)
514 return NULL;
515 rec = &pg->records[0];
516 if (!rec->counter)
517 goto again;
518 }
519
520 return rec;
521}
522
523static void *function_stat_start(struct tracer_stat *trace)
524{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525 struct ftrace_profile_stat *stat =
526 container_of(trace, struct ftrace_profile_stat, stat);
527
528 if (!stat || !stat->start)
529 return NULL;
530
531 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400532}
533
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400534#ifdef CONFIG_FUNCTION_GRAPH_TRACER
535/* function graph compares on total time */
536static int function_stat_cmp(void *p1, void *p2)
537{
538 struct ftrace_profile *a = p1;
539 struct ftrace_profile *b = p2;
540
541 if (a->time < b->time)
542 return -1;
543 if (a->time > b->time)
544 return 1;
545 else
546 return 0;
547}
548#else
549/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400550static int function_stat_cmp(void *p1, void *p2)
551{
552 struct ftrace_profile *a = p1;
553 struct ftrace_profile *b = p2;
554
555 if (a->counter < b->counter)
556 return -1;
557 if (a->counter > b->counter)
558 return 1;
559 else
560 return 0;
561}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400562#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400563
564static int function_stat_headers(struct seq_file *m)
565{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400567 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400568 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400569 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400570 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400572 seq_printf(m, " Function Hit\n"
573 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400574#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400575 return 0;
576}
577
578static int function_stat_show(struct seq_file *m, void *v)
579{
580 struct ftrace_profile *rec = v;
581 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800582 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400583#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400584 static struct trace_seq s;
585 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400586 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400587#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800588 mutex_lock(&ftrace_profile_lock);
589
590 /* we raced with function_profile_reset() */
591 if (unlikely(rec->counter == 0)) {
592 ret = -EBUSY;
593 goto out;
594 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400595
596 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400598
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400599#ifdef CONFIG_FUNCTION_GRAPH_TRACER
600 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400601 avg = rec->time;
602 do_div(avg, rec->counter);
603
Chase Douglase330b3b2010-04-26 14:02:05 -0400604 /* Sample standard deviation (s^2) */
605 if (rec->counter <= 1)
606 stddev = 0;
607 else {
608 stddev = rec->time_squared - rec->counter * avg * avg;
609 /*
610 * Divide only 1000 for ns^2 -> us^2 conversion.
611 * trace_print_graph_duration will divide 1000 again.
612 */
613 do_div(stddev, (rec->counter - 1) * 1000);
614 }
615
Steven Rostedt34886c82009-03-25 21:00:47 -0400616 trace_seq_init(&s);
617 trace_print_graph_duration(rec->time, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400620 trace_seq_puts(&s, " ");
621 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400622 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400623#endif
624 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800625out:
626 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627
Li Zefan3aaba202010-08-23 16:50:12 +0800628 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400629}
630
Steven Rostedtcafb1682009-03-24 20:50:39 -0400631static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400632{
633 struct ftrace_profile_page *pg;
634
Steven Rostedtcafb1682009-03-24 20:50:39 -0400635 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400636
637 while (pg) {
638 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
639 pg->index = 0;
640 pg = pg->next;
641 }
642
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400644 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
645}
646
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400648{
649 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400650 int functions;
651 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400652 int i;
653
654 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 return 0;
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
659 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400660 return -ENOMEM;
661
Steven Rostedt318e0a72009-03-25 20:06:34 -0400662#ifdef CONFIG_DYNAMIC_FTRACE
663 functions = ftrace_update_tot_cnt;
664#else
665 /*
666 * We do not know the number of functions that exist because
667 * dynamic tracing is what counts them. With past experience
668 * we have around 20K functions. That should be more than enough.
669 * It is highly unlikely we will execute every function in
670 * the kernel.
671 */
672 functions = 20000;
673#endif
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400676
Steven Rostedt318e0a72009-03-25 20:06:34 -0400677 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
678
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900679 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400682 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400683 pg = pg->next;
684 }
685
686 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400687
688 out_free:
689 pg = stat->start;
690 while (pg) {
691 unsigned long tmp = (unsigned long)pg;
692
693 pg = pg->next;
694 free_page(tmp);
695 }
696
697 free_page((unsigned long)stat->pages);
698 stat->pages = NULL;
699 stat->start = NULL;
700
701 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400702}
703
Steven Rostedtcafb1682009-03-24 20:50:39 -0400704static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400705{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400706 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 int size;
708
Steven Rostedtcafb1682009-03-24 20:50:39 -0400709 stat = &per_cpu(ftrace_profile_stats, cpu);
710
711 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400712 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400714 return 0;
715 }
716
717 /*
718 * We are profiling all functions, but usually only a few thousand
719 * functions are hit. We'll make a hash of 1024 items.
720 */
721 size = FTRACE_PROFILE_HASH_SIZE;
722
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724
Steven Rostedtcafb1682009-03-24 20:50:39 -0400725 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 return -ENOMEM;
727
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 if (!ftrace_profile_bits) {
729 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400730
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731 for (; size; size >>= 1)
732 ftrace_profile_bits++;
733 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400734
Steven Rostedt318e0a72009-03-25 20:06:34 -0400735 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400736 if (ftrace_profile_pages_init(stat) < 0) {
737 kfree(stat->hash);
738 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400739 return -ENOMEM;
740 }
741
742 return 0;
743}
744
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745static int ftrace_profile_init(void)
746{
747 int cpu;
748 int ret = 0;
749
750 for_each_online_cpu(cpu) {
751 ret = ftrace_profile_init_cpu(cpu);
752 if (ret)
753 break;
754 }
755
756 return ret;
757}
758
Steven Rostedt493762f2009-03-23 17:12:36 -0400759/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760static struct ftrace_profile *
761ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400762{
763 struct ftrace_profile *rec;
764 struct hlist_head *hhd;
765 struct hlist_node *n;
766 unsigned long key;
767
768 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400770
771 if (hlist_empty(hhd))
772 return NULL;
773
774 hlist_for_each_entry_rcu(rec, n, hhd, node) {
775 if (rec->ip == ip)
776 return rec;
777 }
778
779 return NULL;
780}
781
Steven Rostedtcafb1682009-03-24 20:50:39 -0400782static void ftrace_add_profile(struct ftrace_profile_stat *stat,
783 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400784{
785 unsigned long key;
786
787 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400789}
790
Steven Rostedt318e0a72009-03-25 20:06:34 -0400791/*
792 * The memory is already allocated, this simply finds a new record to use.
793 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400794static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400795ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400796{
797 struct ftrace_profile *rec = NULL;
798
Steven Rostedt318e0a72009-03-25 20:06:34 -0400799 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 goto out;
802
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400804 * Try to find the function again since an NMI
805 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400810
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 if (stat->pages->index == PROFILES_PER_PAGE) {
812 if (!stat->pages->next)
813 goto out;
814 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400815 }
816
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400820
Steven Rostedt493762f2009-03-23 17:12:36 -0400821 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400823
824 return rec;
825}
826
Steven Rostedt493762f2009-03-23 17:12:36 -0400827static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400828function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400829 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400830{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400831 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400832 struct ftrace_profile *rec;
833 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400834
835 if (!ftrace_profile_enabled)
836 return;
837
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400839
840 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400841 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 goto out;
843
844 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400846 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847 if (!rec)
848 goto out;
849 }
850
851 rec->counter++;
852 out:
853 local_irq_restore(flags);
854}
855
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400856#ifdef CONFIG_FUNCTION_GRAPH_TRACER
857static int profile_graph_entry(struct ftrace_graph_ent *trace)
858{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400859 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860 return 1;
861}
862
863static void profile_graph_return(struct ftrace_graph_ret *trace)
864{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400865 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400866 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400867 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400868 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869
870 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400871 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400872 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400873 goto out;
874
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400875 /* If the calltime was zero'd ignore it */
876 if (!trace->calltime)
877 goto out;
878
Steven Rostedta2a16d62009-03-24 23:17:58 -0400879 calltime = trace->rettime - trace->calltime;
880
881 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
882 int index;
883
884 index = trace->depth;
885
886 /* Append this call time to the parent time to subtract */
887 if (index)
888 current->ret_stack[index - 1].subtime += calltime;
889
890 if (current->ret_stack[index].subtime < calltime)
891 calltime -= current->ret_stack[index].subtime;
892 else
893 calltime = 0;
894 }
895
Steven Rostedtcafb1682009-03-24 20:50:39 -0400896 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400897 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400898 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400899 rec->time_squared += calltime * calltime;
900 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400901
Steven Rostedtcafb1682009-03-24 20:50:39 -0400902 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400903 local_irq_restore(flags);
904}
905
906static int register_ftrace_profiler(void)
907{
908 return register_ftrace_graph(&profile_graph_return,
909 &profile_graph_entry);
910}
911
912static void unregister_ftrace_profiler(void)
913{
914 unregister_ftrace_graph();
915}
916#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100917static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400918 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400919 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400920};
921
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400922static int register_ftrace_profiler(void)
923{
924 return register_ftrace_function(&ftrace_profile_ops);
925}
926
927static void unregister_ftrace_profiler(void)
928{
929 unregister_ftrace_function(&ftrace_profile_ops);
930}
931#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
932
Steven Rostedt493762f2009-03-23 17:12:36 -0400933static ssize_t
934ftrace_profile_write(struct file *filp, const char __user *ubuf,
935 size_t cnt, loff_t *ppos)
936{
937 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400938 int ret;
939
Peter Huewe22fe9b52011-06-07 21:58:27 +0200940 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
941 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400942 return ret;
943
944 val = !!val;
945
946 mutex_lock(&ftrace_profile_lock);
947 if (ftrace_profile_enabled ^ val) {
948 if (val) {
949 ret = ftrace_profile_init();
950 if (ret < 0) {
951 cnt = ret;
952 goto out;
953 }
954
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400955 ret = register_ftrace_profiler();
956 if (ret < 0) {
957 cnt = ret;
958 goto out;
959 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400960 ftrace_profile_enabled = 1;
961 } else {
962 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400963 /*
964 * unregister_ftrace_profiler calls stop_machine
965 * so this acts like an synchronize_sched.
966 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400967 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400968 }
969 }
970 out:
971 mutex_unlock(&ftrace_profile_lock);
972
Jiri Olsacf8517c2009-10-23 19:36:16 -0400973 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400974
975 return cnt;
976}
977
978static ssize_t
979ftrace_profile_read(struct file *filp, char __user *ubuf,
980 size_t cnt, loff_t *ppos)
981{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400982 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400983 int r;
984
985 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
986 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
987}
988
989static const struct file_operations ftrace_profile_fops = {
990 .open = tracing_open_generic,
991 .read = ftrace_profile_read,
992 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200993 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400994};
995
Steven Rostedtcafb1682009-03-24 20:50:39 -0400996/* used to initialize the real stat files */
997static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400998 .name = "functions",
999 .stat_start = function_stat_start,
1000 .stat_next = function_stat_next,
1001 .stat_cmp = function_stat_cmp,
1002 .stat_headers = function_stat_headers,
1003 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001004};
1005
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001006static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001007{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001008 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001009 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001010 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001011 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001012 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001013
Steven Rostedtcafb1682009-03-24 20:50:39 -04001014 for_each_possible_cpu(cpu) {
1015 stat = &per_cpu(ftrace_profile_stats, cpu);
1016
1017 /* allocate enough for function name + cpu number */
1018 name = kmalloc(32, GFP_KERNEL);
1019 if (!name) {
1020 /*
1021 * The files created are permanent, if something happens
1022 * we still do not free memory.
1023 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001024 WARN(1,
1025 "Could not allocate stat file for cpu %d\n",
1026 cpu);
1027 return;
1028 }
1029 stat->stat = function_stats;
1030 snprintf(name, 32, "function%d", cpu);
1031 stat->stat.name = name;
1032 ret = register_stat_tracer(&stat->stat);
1033 if (ret) {
1034 WARN(1,
1035 "Could not register function stat for cpu %d\n",
1036 cpu);
1037 kfree(name);
1038 return;
1039 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001040 }
1041
1042 entry = debugfs_create_file("function_profile_enabled", 0644,
1043 d_tracer, NULL, &ftrace_profile_fops);
1044 if (!entry)
1045 pr_warning("Could not create debugfs "
1046 "'function_profile_enabled' entry\n");
1047}
1048
1049#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001050static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001051{
1052}
1053#endif /* CONFIG_FUNCTION_PROFILER */
1054
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001055static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1056
Steven Rostedt3d083392008-05-12 21:20:42 +02001057#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001058
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001059#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001060# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001061#endif
1062
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001063static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1064
Steven Rostedtb6887d72009-02-17 12:32:04 -05001065struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001066 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001067 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001068 unsigned long flags;
1069 unsigned long ip;
1070 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001071 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001072};
1073
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001074struct ftrace_func_entry {
1075 struct hlist_node hlist;
1076 unsigned long ip;
1077};
1078
1079struct ftrace_hash {
1080 unsigned long size_bits;
1081 struct hlist_head *buckets;
1082 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001083 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001084};
1085
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001086/*
1087 * We make these constant because no one should touch them,
1088 * but they are used as the default "empty hash", to avoid allocating
1089 * it all the time. These are in a read only section such that if
1090 * anyone does try to modify it, it will cause an exception.
1091 */
1092static const struct hlist_head empty_buckets[1];
1093static const struct ftrace_hash empty_hash = {
1094 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001095};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001096#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001097
Steven Rostedt2b499382011-05-03 22:49:52 -04001098static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001099 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001100 .notrace_hash = EMPTY_HASH,
1101 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001102 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001103};
1104
Steven Rostedt41c52c02008-05-22 11:46:33 -04001105static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001106
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107struct ftrace_page {
1108 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001109 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001110 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001111 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001112};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001113
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001114static struct ftrace_page *ftrace_new_pgs;
1115
Steven Rostedta7900872011-12-16 16:23:44 -05001116#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1117#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001118
1119/* estimate from running different kernels */
1120#define NR_TO_INIT 10000
1121
1122static struct ftrace_page *ftrace_pages_start;
1123static struct ftrace_page *ftrace_pages;
1124
Steven Rostedt06a51d92011-12-19 19:07:36 -05001125static bool ftrace_hash_empty(struct ftrace_hash *hash)
1126{
1127 return !hash || !hash->count;
1128}
1129
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130static struct ftrace_func_entry *
1131ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1132{
1133 unsigned long key;
1134 struct ftrace_func_entry *entry;
1135 struct hlist_head *hhd;
1136 struct hlist_node *n;
1137
Steven Rostedt06a51d92011-12-19 19:07:36 -05001138 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001139 return NULL;
1140
1141 if (hash->size_bits > 0)
1142 key = hash_long(ip, hash->size_bits);
1143 else
1144 key = 0;
1145
1146 hhd = &hash->buckets[key];
1147
1148 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1149 if (entry->ip == ip)
1150 return entry;
1151 }
1152 return NULL;
1153}
1154
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001155static void __add_hash_entry(struct ftrace_hash *hash,
1156 struct ftrace_func_entry *entry)
1157{
1158 struct hlist_head *hhd;
1159 unsigned long key;
1160
1161 if (hash->size_bits)
1162 key = hash_long(entry->ip, hash->size_bits);
1163 else
1164 key = 0;
1165
1166 hhd = &hash->buckets[key];
1167 hlist_add_head(&entry->hlist, hhd);
1168 hash->count++;
1169}
1170
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001171static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1172{
1173 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001174
1175 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1176 if (!entry)
1177 return -ENOMEM;
1178
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001179 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001180 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001181
1182 return 0;
1183}
1184
1185static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001186free_hash_entry(struct ftrace_hash *hash,
1187 struct ftrace_func_entry *entry)
1188{
1189 hlist_del(&entry->hlist);
1190 kfree(entry);
1191 hash->count--;
1192}
1193
1194static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195remove_hash_entry(struct ftrace_hash *hash,
1196 struct ftrace_func_entry *entry)
1197{
1198 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199 hash->count--;
1200}
1201
1202static void ftrace_hash_clear(struct ftrace_hash *hash)
1203{
1204 struct hlist_head *hhd;
1205 struct hlist_node *tp, *tn;
1206 struct ftrace_func_entry *entry;
1207 int size = 1 << hash->size_bits;
1208 int i;
1209
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210 if (!hash->count)
1211 return;
1212
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001213 for (i = 0; i < size; i++) {
1214 hhd = &hash->buckets[i];
1215 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001216 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001217 }
1218 FTRACE_WARN_ON(hash->count);
1219}
1220
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001221static void free_ftrace_hash(struct ftrace_hash *hash)
1222{
1223 if (!hash || hash == EMPTY_HASH)
1224 return;
1225 ftrace_hash_clear(hash);
1226 kfree(hash->buckets);
1227 kfree(hash);
1228}
1229
Steven Rostedt07fd5512011-05-05 18:03:47 -04001230static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1231{
1232 struct ftrace_hash *hash;
1233
1234 hash = container_of(rcu, struct ftrace_hash, rcu);
1235 free_ftrace_hash(hash);
1236}
1237
1238static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1239{
1240 if (!hash || hash == EMPTY_HASH)
1241 return;
1242 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1243}
1244
Jiri Olsa5500fa52012-02-15 15:51:54 +01001245void ftrace_free_filter(struct ftrace_ops *ops)
1246{
1247 free_ftrace_hash(ops->filter_hash);
1248 free_ftrace_hash(ops->notrace_hash);
1249}
1250
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001251static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1252{
1253 struct ftrace_hash *hash;
1254 int size;
1255
1256 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1257 if (!hash)
1258 return NULL;
1259
1260 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001261 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001262
1263 if (!hash->buckets) {
1264 kfree(hash);
1265 return NULL;
1266 }
1267
1268 hash->size_bits = size_bits;
1269
1270 return hash;
1271}
1272
1273static struct ftrace_hash *
1274alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1275{
1276 struct ftrace_func_entry *entry;
1277 struct ftrace_hash *new_hash;
1278 struct hlist_node *tp;
1279 int size;
1280 int ret;
1281 int i;
1282
1283 new_hash = alloc_ftrace_hash(size_bits);
1284 if (!new_hash)
1285 return NULL;
1286
1287 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001288 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289 return new_hash;
1290
1291 size = 1 << hash->size_bits;
1292 for (i = 0; i < size; i++) {
1293 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1294 ret = add_hash_entry(new_hash, entry->ip);
1295 if (ret < 0)
1296 goto free_hash;
1297 }
1298 }
1299
1300 FTRACE_WARN_ON(new_hash->count != hash->count);
1301
1302 return new_hash;
1303
1304 free_hash:
1305 free_ftrace_hash(new_hash);
1306 return NULL;
1307}
1308
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001309static void
1310ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1311static void
1312ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1313
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001315ftrace_hash_move(struct ftrace_ops *ops, int enable,
1316 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001317{
1318 struct ftrace_func_entry *entry;
1319 struct hlist_node *tp, *tn;
1320 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001321 struct ftrace_hash *old_hash;
1322 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323 int size = src->count;
1324 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001325 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326 int i;
1327
1328 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001329 * Remove the current set, update the hash and add
1330 * them back.
1331 */
1332 ftrace_hash_rec_disable(ops, enable);
1333
1334 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001335 * If the new source is empty, just free dst and assign it
1336 * the empty_hash.
1337 */
1338 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001339 free_ftrace_hash_rcu(*dst);
1340 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001341 /* still need to update the function records */
1342 ret = 0;
1343 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001344 }
1345
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 /*
1347 * Make the hash size about 1/2 the # found
1348 */
1349 for (size /= 2; size; size >>= 1)
1350 bits++;
1351
1352 /* Don't allocate too much */
1353 if (bits > FTRACE_HASH_MAX_BITS)
1354 bits = FTRACE_HASH_MAX_BITS;
1355
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001357 new_hash = alloc_ftrace_hash(bits);
1358 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001359 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360
1361 size = 1 << src->size_bits;
1362 for (i = 0; i < size; i++) {
1363 hhd = &src->buckets[i];
1364 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001366 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001367 }
1368 }
1369
Steven Rostedt07fd5512011-05-05 18:03:47 -04001370 old_hash = *dst;
1371 rcu_assign_pointer(*dst, new_hash);
1372 free_ftrace_hash_rcu(old_hash);
1373
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001374 ret = 0;
1375 out:
1376 /*
1377 * Enable regardless of ret:
1378 * On success, we enable the new hash.
1379 * On failure, we re-enable the original hash.
1380 */
1381 ftrace_hash_rec_enable(ops, enable);
1382
1383 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001384}
1385
Steven Rostedt265c8312009-02-13 12:43:56 -05001386/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001387 * Test the hashes for this ops to see if we want to call
1388 * the ops->func or not.
1389 *
1390 * It's a match if the ip is in the ops->filter_hash or
1391 * the filter_hash does not exist or is empty,
1392 * AND
1393 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001394 *
1395 * This needs to be called with preemption disabled as
1396 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001397 */
1398static int
1399ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1400{
1401 struct ftrace_hash *filter_hash;
1402 struct ftrace_hash *notrace_hash;
1403 int ret;
1404
Steven Rostedtb8489142011-05-04 09:27:52 -04001405 filter_hash = rcu_dereference_raw(ops->filter_hash);
1406 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1407
Steven Rostedt06a51d92011-12-19 19:07:36 -05001408 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001409 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001410 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001411 !ftrace_lookup_ip(notrace_hash, ip)))
1412 ret = 1;
1413 else
1414 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001415
1416 return ret;
1417}
1418
1419/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001420 * This is a double for. Do not use 'break' to break out of the loop,
1421 * you must use a goto.
1422 */
1423#define do_for_each_ftrace_rec(pg, rec) \
1424 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1425 int _____i; \
1426 for (_____i = 0; _____i < pg->index; _____i++) { \
1427 rec = &pg->records[_____i];
1428
1429#define while_for_each_ftrace_rec() \
1430 } \
1431 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301432
Steven Rostedt5855fea2011-12-16 19:27:42 -05001433
1434static int ftrace_cmp_recs(const void *a, const void *b)
1435{
Steven Rostedta650e022012-04-25 13:48:13 -04001436 const struct dyn_ftrace *key = a;
1437 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001438
Steven Rostedta650e022012-04-25 13:48:13 -04001439 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001440 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001441 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1442 return 1;
1443 return 0;
1444}
1445
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001446static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001447{
1448 struct ftrace_page *pg;
1449 struct dyn_ftrace *rec;
1450 struct dyn_ftrace key;
1451
1452 key.ip = start;
1453 key.flags = end; /* overload flags, as it is unsigned long */
1454
1455 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1456 if (end < pg->records[0].ip ||
1457 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1458 continue;
1459 rec = bsearch(&key, pg->records, pg->index,
1460 sizeof(struct dyn_ftrace),
1461 ftrace_cmp_recs);
1462 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001463 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001464 }
1465
Steven Rostedt5855fea2011-12-16 19:27:42 -05001466 return 0;
1467}
1468
Steven Rostedtc88fd862011-08-16 09:53:39 -04001469/**
1470 * ftrace_location - return true if the ip giving is a traced location
1471 * @ip: the instruction pointer to check
1472 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001473 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001474 * That is, the instruction that is either a NOP or call to
1475 * the function tracer. It checks the ftrace internal tables to
1476 * determine if the address belongs or not.
1477 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001478unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001479{
Steven Rostedta650e022012-04-25 13:48:13 -04001480 return ftrace_location_range(ip, ip);
1481}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001482
Steven Rostedta650e022012-04-25 13:48:13 -04001483/**
1484 * ftrace_text_reserved - return true if range contains an ftrace location
1485 * @start: start of range to search
1486 * @end: end of range to search (inclusive). @end points to the last byte to check.
1487 *
1488 * Returns 1 if @start and @end contains a ftrace location.
1489 * That is, the instruction that is either a NOP or call to
1490 * the function tracer. It checks the ftrace internal tables to
1491 * determine if the address belongs or not.
1492 */
1493int ftrace_text_reserved(void *start, void *end)
1494{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001495 unsigned long ret;
1496
1497 ret = ftrace_location_range((unsigned long)start,
1498 (unsigned long)end);
1499
1500 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001501}
1502
Steven Rostedted926f92011-05-03 13:25:24 -04001503static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1504 int filter_hash,
1505 bool inc)
1506{
1507 struct ftrace_hash *hash;
1508 struct ftrace_hash *other_hash;
1509 struct ftrace_page *pg;
1510 struct dyn_ftrace *rec;
1511 int count = 0;
1512 int all = 0;
1513
1514 /* Only update if the ops has been registered */
1515 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1516 return;
1517
1518 /*
1519 * In the filter_hash case:
1520 * If the count is zero, we update all records.
1521 * Otherwise we just update the items in the hash.
1522 *
1523 * In the notrace_hash case:
1524 * We enable the update in the hash.
1525 * As disabling notrace means enabling the tracing,
1526 * and enabling notrace means disabling, the inc variable
1527 * gets inversed.
1528 */
1529 if (filter_hash) {
1530 hash = ops->filter_hash;
1531 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001532 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001533 all = 1;
1534 } else {
1535 inc = !inc;
1536 hash = ops->notrace_hash;
1537 other_hash = ops->filter_hash;
1538 /*
1539 * If the notrace hash has no items,
1540 * then there's nothing to do.
1541 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001542 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001543 return;
1544 }
1545
1546 do_for_each_ftrace_rec(pg, rec) {
1547 int in_other_hash = 0;
1548 int in_hash = 0;
1549 int match = 0;
1550
1551 if (all) {
1552 /*
1553 * Only the filter_hash affects all records.
1554 * Update if the record is not in the notrace hash.
1555 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001556 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001557 match = 1;
1558 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001559 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1560 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001561
1562 /*
1563 *
1564 */
1565 if (filter_hash && in_hash && !in_other_hash)
1566 match = 1;
1567 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001568 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001569 match = 1;
1570 }
1571 if (!match)
1572 continue;
1573
1574 if (inc) {
1575 rec->flags++;
1576 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1577 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001578 /*
1579 * If any ops wants regs saved for this function
1580 * then all ops will get saved regs.
1581 */
1582 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1583 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001584 } else {
1585 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1586 return;
1587 rec->flags--;
1588 }
1589 count++;
1590 /* Shortcut, if we handled all records, we are done. */
1591 if (!all && count == hash->count)
1592 return;
1593 } while_for_each_ftrace_rec();
1594}
1595
1596static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1597 int filter_hash)
1598{
1599 __ftrace_hash_rec_update(ops, filter_hash, 0);
1600}
1601
1602static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1603 int filter_hash)
1604{
1605 __ftrace_hash_rec_update(ops, filter_hash, 1);
1606}
1607
Steven Rostedt05736a42008-09-22 14:55:47 -07001608static void print_ip_ins(const char *fmt, unsigned char *p)
1609{
1610 int i;
1611
1612 printk(KERN_CONT "%s", fmt);
1613
1614 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1615 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1616}
1617
Steven Rostedtc88fd862011-08-16 09:53:39 -04001618/**
1619 * ftrace_bug - report and shutdown function tracer
1620 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1621 * @ip: The address that failed
1622 *
1623 * The arch code that enables or disables the function tracing
1624 * can call ftrace_bug() when it has detected a problem in
1625 * modifying the code. @failed should be one of either:
1626 * EFAULT - if the problem happens on reading the @ip address
1627 * EINVAL - if what is read at @ip is not what was expected
1628 * EPERM - if the problem happens on writting to the @ip address
1629 */
1630void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001631{
1632 switch (failed) {
1633 case -EFAULT:
1634 FTRACE_WARN_ON_ONCE(1);
1635 pr_info("ftrace faulted on modifying ");
1636 print_ip_sym(ip);
1637 break;
1638 case -EINVAL:
1639 FTRACE_WARN_ON_ONCE(1);
1640 pr_info("ftrace failed to modify ");
1641 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001642 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001643 printk(KERN_CONT "\n");
1644 break;
1645 case -EPERM:
1646 FTRACE_WARN_ON_ONCE(1);
1647 pr_info("ftrace faulted on writing ");
1648 print_ip_sym(ip);
1649 break;
1650 default:
1651 FTRACE_WARN_ON_ONCE(1);
1652 pr_info("ftrace faulted on unknown error ");
1653 print_ip_sym(ip);
1654 }
1655}
1656
Steven Rostedtc88fd862011-08-16 09:53:39 -04001657static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001658{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001659 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001660
Steven Rostedt982c3502008-11-15 16:31:41 -05001661 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001662 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001663 *
Steven Rostedted926f92011-05-03 13:25:24 -04001664 * If the record has a ref count, then we need to enable it
1665 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001666 *
Steven Rostedted926f92011-05-03 13:25:24 -04001667 * Otherwise we make sure its disabled.
1668 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001669 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001670 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001671 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001672 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001673 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001674
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001675 /*
1676 * If enabling and the REGS flag does not match the REGS_EN, then
1677 * do not ignore this record. Set flags to fail the compare against
1678 * ENABLED.
1679 */
1680 if (flag &&
1681 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1682 flag |= FTRACE_FL_REGS;
1683
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001684 /* If the state of this record hasn't changed, then do nothing */
1685 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001686 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001687
1688 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001689 /* Save off if rec is being enabled (for return value) */
1690 flag ^= rec->flags & FTRACE_FL_ENABLED;
1691
1692 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001693 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001694 if (flag & FTRACE_FL_REGS) {
1695 if (rec->flags & FTRACE_FL_REGS)
1696 rec->flags |= FTRACE_FL_REGS_EN;
1697 else
1698 rec->flags &= ~FTRACE_FL_REGS_EN;
1699 }
1700 }
1701
1702 /*
1703 * If this record is being updated from a nop, then
1704 * return UPDATE_MAKE_CALL.
1705 * Otherwise, if the EN flag is set, then return
1706 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1707 * from the non-save regs, to a save regs function.
1708 * Otherwise,
1709 * return UPDATE_MODIFY_CALL to tell the caller to convert
1710 * from the save regs, to a non-save regs function.
1711 */
1712 if (flag & FTRACE_FL_ENABLED)
1713 return FTRACE_UPDATE_MAKE_CALL;
1714 else if (rec->flags & FTRACE_FL_REGS_EN)
1715 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1716 else
1717 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001718 }
1719
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001720 if (update) {
1721 /* If there's no more users, clear all flags */
1722 if (!(rec->flags & ~FTRACE_FL_MASK))
1723 rec->flags = 0;
1724 else
1725 /* Just disable the record (keep REGS state) */
1726 rec->flags &= ~FTRACE_FL_ENABLED;
1727 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001728
1729 return FTRACE_UPDATE_MAKE_NOP;
1730}
1731
1732/**
1733 * ftrace_update_record, set a record that now is tracing or not
1734 * @rec: the record to update
1735 * @enable: set to 1 if the record is tracing, zero to force disable
1736 *
1737 * The records that represent all functions that can be traced need
1738 * to be updated when tracing has been enabled.
1739 */
1740int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1741{
1742 return ftrace_check_record(rec, enable, 1);
1743}
1744
1745/**
1746 * ftrace_test_record, check if the record has been enabled or not
1747 * @rec: the record to test
1748 * @enable: set to 1 to check if enabled, 0 if it is disabled
1749 *
1750 * The arch code may need to test if a record is already set to
1751 * tracing to determine how to modify the function code that it
1752 * represents.
1753 */
1754int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1755{
1756 return ftrace_check_record(rec, enable, 0);
1757}
1758
1759static int
1760__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1761{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001762 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001763 unsigned long ftrace_addr;
1764 int ret;
1765
Steven Rostedtc88fd862011-08-16 09:53:39 -04001766 ret = ftrace_update_record(rec, enable);
1767
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001768 if (rec->flags & FTRACE_FL_REGS)
1769 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1770 else
1771 ftrace_addr = (unsigned long)FTRACE_ADDR;
1772
Steven Rostedtc88fd862011-08-16 09:53:39 -04001773 switch (ret) {
1774 case FTRACE_UPDATE_IGNORE:
1775 return 0;
1776
1777 case FTRACE_UPDATE_MAKE_CALL:
1778 return ftrace_make_call(rec, ftrace_addr);
1779
1780 case FTRACE_UPDATE_MAKE_NOP:
1781 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001782
1783 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1784 case FTRACE_UPDATE_MODIFY_CALL:
1785 if (rec->flags & FTRACE_FL_REGS)
1786 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1787 else
1788 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1789
1790 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001791 }
1792
1793 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001794}
1795
Steven Rostedte4f5d542012-04-27 09:13:18 -04001796void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001797{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001798 struct dyn_ftrace *rec;
1799 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001800 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001801
Steven Rostedt45a4a232011-04-21 23:16:46 -04001802 if (unlikely(ftrace_disabled))
1803 return;
1804
Steven Rostedt265c8312009-02-13 12:43:56 -05001805 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001806 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001807 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001808 ftrace_bug(failed, rec->ip);
1809 /* Stop processing */
1810 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001811 }
1812 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001813}
1814
Steven Rostedtc88fd862011-08-16 09:53:39 -04001815struct ftrace_rec_iter {
1816 struct ftrace_page *pg;
1817 int index;
1818};
1819
1820/**
1821 * ftrace_rec_iter_start, start up iterating over traced functions
1822 *
1823 * Returns an iterator handle that is used to iterate over all
1824 * the records that represent address locations where functions
1825 * are traced.
1826 *
1827 * May return NULL if no records are available.
1828 */
1829struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1830{
1831 /*
1832 * We only use a single iterator.
1833 * Protected by the ftrace_lock mutex.
1834 */
1835 static struct ftrace_rec_iter ftrace_rec_iter;
1836 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1837
1838 iter->pg = ftrace_pages_start;
1839 iter->index = 0;
1840
1841 /* Could have empty pages */
1842 while (iter->pg && !iter->pg->index)
1843 iter->pg = iter->pg->next;
1844
1845 if (!iter->pg)
1846 return NULL;
1847
1848 return iter;
1849}
1850
1851/**
1852 * ftrace_rec_iter_next, get the next record to process.
1853 * @iter: The handle to the iterator.
1854 *
1855 * Returns the next iterator after the given iterator @iter.
1856 */
1857struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1858{
1859 iter->index++;
1860
1861 if (iter->index >= iter->pg->index) {
1862 iter->pg = iter->pg->next;
1863 iter->index = 0;
1864
1865 /* Could have empty pages */
1866 while (iter->pg && !iter->pg->index)
1867 iter->pg = iter->pg->next;
1868 }
1869
1870 if (!iter->pg)
1871 return NULL;
1872
1873 return iter;
1874}
1875
1876/**
1877 * ftrace_rec_iter_record, get the record at the iterator location
1878 * @iter: The current iterator location
1879 *
1880 * Returns the record that the current @iter is at.
1881 */
1882struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1883{
1884 return &iter->pg->records[iter->index];
1885}
1886
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301887static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001888ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001889{
1890 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001891 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001892
1893 ip = rec->ip;
1894
Steven Rostedt45a4a232011-04-21 23:16:46 -04001895 if (unlikely(ftrace_disabled))
1896 return 0;
1897
Shaohua Li25aac9d2009-01-09 11:29:40 +08001898 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001899 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001900 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301901 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001902 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301903 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001904}
1905
Steven Rostedt000ab692009-02-17 13:35:06 -05001906/*
1907 * archs can override this function if they must do something
1908 * before the modifying code is performed.
1909 */
1910int __weak ftrace_arch_code_modify_prepare(void)
1911{
1912 return 0;
1913}
1914
1915/*
1916 * archs can override this function if they must do something
1917 * after the modifying code is performed.
1918 */
1919int __weak ftrace_arch_code_modify_post_process(void)
1920{
1921 return 0;
1922}
1923
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001924void ftrace_modify_all_code(int command)
1925{
1926 if (command & FTRACE_UPDATE_CALLS)
1927 ftrace_replace_code(1);
1928 else if (command & FTRACE_DISABLE_CALLS)
1929 ftrace_replace_code(0);
1930
1931 if (command & FTRACE_UPDATE_TRACE_FUNC)
1932 ftrace_update_ftrace_func(ftrace_trace_function);
1933
1934 if (command & FTRACE_START_FUNC_RET)
1935 ftrace_enable_ftrace_graph_caller();
1936 else if (command & FTRACE_STOP_FUNC_RET)
1937 ftrace_disable_ftrace_graph_caller();
1938}
1939
Ingo Molnare309b412008-05-12 21:20:51 +02001940static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001941{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001942 int *command = data;
1943
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001944 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001945
Steven Rostedtc88fd862011-08-16 09:53:39 -04001946 return 0;
1947}
1948
1949/**
1950 * ftrace_run_stop_machine, go back to the stop machine method
1951 * @command: The command to tell ftrace what to do
1952 *
1953 * If an arch needs to fall back to the stop machine method, the
1954 * it can call this function.
1955 */
1956void ftrace_run_stop_machine(int command)
1957{
1958 stop_machine(__ftrace_modify_code, &command, NULL);
1959}
1960
1961/**
1962 * arch_ftrace_update_code, modify the code to trace or not trace
1963 * @command: The command that needs to be done
1964 *
1965 * Archs can override this function if it does not need to
1966 * run stop_machine() to modify code.
1967 */
1968void __weak arch_ftrace_update_code(int command)
1969{
1970 ftrace_run_stop_machine(command);
1971}
1972
1973static void ftrace_run_update_code(int command)
1974{
1975 int ret;
1976
1977 ret = ftrace_arch_code_modify_prepare();
1978 FTRACE_WARN_ON(ret);
1979 if (ret)
1980 return;
1981 /*
1982 * Do not call function tracer while we update the code.
1983 * We are in stop machine.
1984 */
1985 function_trace_stop++;
1986
1987 /*
1988 * By default we use stop_machine() to modify the code.
1989 * But archs can do what ever they want as long as it
1990 * is safe. The stop_machine() is the safest, but also
1991 * produces the most overhead.
1992 */
1993 arch_ftrace_update_code(command);
1994
Steven Rostedt6331c282011-07-13 15:11:02 -04001995 function_trace_stop--;
1996
Steven Rostedt000ab692009-02-17 13:35:06 -05001997 ret = ftrace_arch_code_modify_post_process();
1998 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001999}
2000
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002001static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002002static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002003static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002004
2005static void ftrace_startup_enable(int command)
2006{
2007 if (saved_ftrace_func != ftrace_trace_function) {
2008 saved_ftrace_func = ftrace_trace_function;
2009 command |= FTRACE_UPDATE_TRACE_FUNC;
2010 }
2011
2012 if (!command || !ftrace_enabled)
2013 return;
2014
2015 ftrace_run_update_code(command);
2016}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002017
Steven Rostedta1cd6172011-05-23 15:24:25 -04002018static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002019{
Steven Rostedtb8489142011-05-04 09:27:52 -04002020 bool hash_enable = true;
2021
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002022 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002023 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002024
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002025 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002026 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002027
Steven Rostedtb8489142011-05-04 09:27:52 -04002028 /* ops marked global share the filter hashes */
2029 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2030 ops = &global_ops;
2031 /* Don't update hash if global is already set */
2032 if (global_start_up)
2033 hash_enable = false;
2034 global_start_up++;
2035 }
2036
Steven Rostedted926f92011-05-03 13:25:24 -04002037 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002038 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002039 ftrace_hash_rec_enable(ops, 1);
2040
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002041 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002042
2043 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002044}
2045
Steven Rostedtbd69c302011-05-03 21:55:54 -04002046static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002047{
Steven Rostedtb8489142011-05-04 09:27:52 -04002048 bool hash_disable = true;
2049
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002050 if (unlikely(ftrace_disabled))
2051 return;
2052
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002053 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002054 /*
2055 * Just warn in case of unbalance, no need to kill ftrace, it's not
2056 * critical but the ftrace_call callers may be never nopped again after
2057 * further ftrace uses.
2058 */
2059 WARN_ON_ONCE(ftrace_start_up < 0);
2060
Steven Rostedtb8489142011-05-04 09:27:52 -04002061 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2062 ops = &global_ops;
2063 global_start_up--;
2064 WARN_ON_ONCE(global_start_up < 0);
2065 /* Don't update hash if global still has users */
2066 if (global_start_up) {
2067 WARN_ON_ONCE(!ftrace_start_up);
2068 hash_disable = false;
2069 }
2070 }
2071
2072 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002073 ftrace_hash_rec_disable(ops, 1);
2074
Steven Rostedtb8489142011-05-04 09:27:52 -04002075 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002076 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002077
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002078 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002079
2080 if (saved_ftrace_func != ftrace_trace_function) {
2081 saved_ftrace_func = ftrace_trace_function;
2082 command |= FTRACE_UPDATE_TRACE_FUNC;
2083 }
2084
2085 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002086 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002087
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002088 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002089}
2090
Ingo Molnare309b412008-05-12 21:20:51 +02002091static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002092{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002093 if (unlikely(ftrace_disabled))
2094 return;
2095
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002096 /* Force update next time */
2097 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002098 /* ftrace_start_up is true if we want ftrace running */
2099 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002100 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002101}
2102
Ingo Molnare309b412008-05-12 21:20:51 +02002103static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002104{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002105 if (unlikely(ftrace_disabled))
2106 return;
2107
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002108 /* ftrace_start_up is true if ftrace is running */
2109 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002110 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002111}
2112
Steven Rostedt3d083392008-05-12 21:20:42 +02002113static cycle_t ftrace_update_time;
2114static unsigned long ftrace_update_cnt;
2115unsigned long ftrace_update_tot_cnt;
2116
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002117static int ops_traces_mod(struct ftrace_ops *ops)
2118{
2119 struct ftrace_hash *hash;
2120
2121 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002122 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002123}
2124
Steven Rostedt31e88902008-11-14 16:21:19 -08002125static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002126{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002127 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002128 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302129 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002130 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002131 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002132
2133 /*
2134 * When adding a module, we need to check if tracers are
2135 * currently enabled and if they are set to trace all functions.
2136 * If they are, we need to enable the module functions as well
2137 * as update the reference counts for those function records.
2138 */
2139 if (mod) {
2140 struct ftrace_ops *ops;
2141
2142 for (ops = ftrace_ops_list;
2143 ops != &ftrace_list_end; ops = ops->next) {
2144 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2145 ops_traces_mod(ops))
2146 ref++;
2147 }
2148 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002149
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002150 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002151 ftrace_update_cnt = 0;
2152
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002153 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302154
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002155 for (i = 0; i < pg->index; i++) {
2156 /* If something went wrong, bail without enabling anything */
2157 if (unlikely(ftrace_disabled))
2158 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002159
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002160 p = &pg->records[i];
2161 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302162
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002163 /*
2164 * Do the initial record conversion from mcount jump
2165 * to the NOP instructions.
2166 */
2167 if (!ftrace_code_disable(mod, p))
2168 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002169
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002170 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002171
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002172 /*
2173 * If the tracing is enabled, go ahead and enable the record.
2174 *
2175 * The reason not to enable the record immediatelly is the
2176 * inherent check of ftrace_make_nop/ftrace_make_call for
2177 * correct previous instructions. Making first the NOP
2178 * conversion puts the module to the correct state, thus
2179 * passing the ftrace_make_call check.
2180 */
2181 if (ftrace_start_up && ref) {
2182 int failed = __ftrace_replace_code(p, 1);
2183 if (failed)
2184 ftrace_bug(failed, p->ip);
2185 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002186 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002187 }
2188
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002189 ftrace_new_pgs = NULL;
2190
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002191 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002192 ftrace_update_time = stop - start;
2193 ftrace_update_tot_cnt += ftrace_update_cnt;
2194
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002195 return 0;
2196}
2197
Steven Rostedta7900872011-12-16 16:23:44 -05002198static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002199{
Steven Rostedta7900872011-12-16 16:23:44 -05002200 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002201 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002202
Steven Rostedta7900872011-12-16 16:23:44 -05002203 if (WARN_ON(!count))
2204 return -EINVAL;
2205
2206 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002207
2208 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002209 * We want to fill as much as possible. No more than a page
2210 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002211 */
Steven Rostedta7900872011-12-16 16:23:44 -05002212 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2213 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002214
Steven Rostedta7900872011-12-16 16:23:44 -05002215 again:
2216 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2217
2218 if (!pg->records) {
2219 /* if we can't allocate this size, try something smaller */
2220 if (!order)
2221 return -ENOMEM;
2222 order >>= 1;
2223 goto again;
2224 }
2225
2226 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2227 pg->size = cnt;
2228
2229 if (cnt > count)
2230 cnt = count;
2231
2232 return cnt;
2233}
2234
2235static struct ftrace_page *
2236ftrace_allocate_pages(unsigned long num_to_init)
2237{
2238 struct ftrace_page *start_pg;
2239 struct ftrace_page *pg;
2240 int order;
2241 int cnt;
2242
2243 if (!num_to_init)
2244 return 0;
2245
2246 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2247 if (!pg)
2248 return NULL;
2249
2250 /*
2251 * Try to allocate as much as possible in one continues
2252 * location that fills in all of the space. We want to
2253 * waste as little space as possible.
2254 */
2255 for (;;) {
2256 cnt = ftrace_allocate_records(pg, num_to_init);
2257 if (cnt < 0)
2258 goto free_pages;
2259
2260 num_to_init -= cnt;
2261 if (!num_to_init)
2262 break;
2263
2264 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2265 if (!pg->next)
2266 goto free_pages;
2267
2268 pg = pg->next;
2269 }
2270
2271 return start_pg;
2272
2273 free_pages:
2274 while (start_pg) {
2275 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2276 free_pages((unsigned long)pg->records, order);
2277 start_pg = pg->next;
2278 kfree(pg);
2279 pg = start_pg;
2280 }
2281 pr_info("ftrace: FAILED to allocate memory for functions\n");
2282 return NULL;
2283}
2284
2285static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2286{
2287 int cnt;
2288
2289 if (!num_to_init) {
2290 pr_info("ftrace: No functions to be traced?\n");
2291 return -1;
2292 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002293
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002294 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002295 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002296 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002297
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002298 return 0;
2299}
2300
Steven Rostedt5072c592008-05-12 21:20:43 +02002301#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2302
2303struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002304 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002305 loff_t func_pos;
2306 struct ftrace_page *pg;
2307 struct dyn_ftrace *func;
2308 struct ftrace_func_probe *probe;
2309 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002310 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002311 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002312 int hidx;
2313 int idx;
2314 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002315};
2316
Ingo Molnare309b412008-05-12 21:20:51 +02002317static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002318t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002319{
2320 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002321 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002322 struct hlist_head *hhd;
2323
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002324 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002325 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002326
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002327 if (iter->probe)
2328 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002329 retry:
2330 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2331 return NULL;
2332
2333 hhd = &ftrace_func_hash[iter->hidx];
2334
2335 if (hlist_empty(hhd)) {
2336 iter->hidx++;
2337 hnd = NULL;
2338 goto retry;
2339 }
2340
2341 if (!hnd)
2342 hnd = hhd->first;
2343 else {
2344 hnd = hnd->next;
2345 if (!hnd) {
2346 iter->hidx++;
2347 goto retry;
2348 }
2349 }
2350
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002351 if (WARN_ON_ONCE(!hnd))
2352 return NULL;
2353
2354 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2355
2356 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002357}
2358
2359static void *t_hash_start(struct seq_file *m, loff_t *pos)
2360{
2361 struct ftrace_iterator *iter = m->private;
2362 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002363 loff_t l;
2364
Steven Rostedt69a30832011-12-19 15:21:16 -05002365 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2366 return NULL;
2367
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002368 if (iter->func_pos > *pos)
2369 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002370
Li Zefand82d6242009-06-24 09:54:54 +08002371 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002372 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002373 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002374 if (!p)
2375 break;
2376 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002377 if (!p)
2378 return NULL;
2379
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002380 /* Only set this if we have an item */
2381 iter->flags |= FTRACE_ITER_HASH;
2382
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002383 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002384}
2385
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002386static int
2387t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002388{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002389 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002390
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002391 rec = iter->probe;
2392 if (WARN_ON_ONCE(!rec))
2393 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002394
Steven Rostedt809dcf22009-02-16 23:06:01 -05002395 if (rec->ops->print)
2396 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2397
Steven Rostedtb375a112009-09-17 00:05:58 -04002398 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002399
2400 if (rec->data)
2401 seq_printf(m, ":%p", rec->data);
2402 seq_putc(m, '\n');
2403
2404 return 0;
2405}
2406
2407static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002408t_next(struct seq_file *m, void *v, loff_t *pos)
2409{
2410 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002411 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002412 struct dyn_ftrace *rec = NULL;
2413
Steven Rostedt45a4a232011-04-21 23:16:46 -04002414 if (unlikely(ftrace_disabled))
2415 return NULL;
2416
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002417 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002418 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002419
Steven Rostedt5072c592008-05-12 21:20:43 +02002420 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002421 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002422
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002423 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002424 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002425
Steven Rostedt5072c592008-05-12 21:20:43 +02002426 retry:
2427 if (iter->idx >= iter->pg->index) {
2428 if (iter->pg->next) {
2429 iter->pg = iter->pg->next;
2430 iter->idx = 0;
2431 goto retry;
2432 }
2433 } else {
2434 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002435 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002436 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002437
Steven Rostedt41c52c02008-05-22 11:46:33 -04002438 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002439 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2440
2441 ((iter->flags & FTRACE_ITER_ENABLED) &&
2442 !(rec->flags & ~FTRACE_FL_MASK))) {
2443
Steven Rostedt5072c592008-05-12 21:20:43 +02002444 rec = NULL;
2445 goto retry;
2446 }
2447 }
2448
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002449 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002450 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002451
2452 iter->func = rec;
2453
2454 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002455}
2456
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002457static void reset_iter_read(struct ftrace_iterator *iter)
2458{
2459 iter->pos = 0;
2460 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002461 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002462}
2463
2464static void *t_start(struct seq_file *m, loff_t *pos)
2465{
2466 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002467 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002468 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002469 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002470
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002471 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002472
2473 if (unlikely(ftrace_disabled))
2474 return NULL;
2475
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002476 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002477 * If an lseek was done, then reset and start from beginning.
2478 */
2479 if (*pos < iter->pos)
2480 reset_iter_read(iter);
2481
2482 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002483 * For set_ftrace_filter reading, if we have the filter
2484 * off, we can short cut and just print out that all
2485 * functions are enabled.
2486 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002487 if (iter->flags & FTRACE_ITER_FILTER &&
2488 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002489 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002490 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002491 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002492 /* reset in case of seek/pread */
2493 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002494 return iter;
2495 }
2496
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002497 if (iter->flags & FTRACE_ITER_HASH)
2498 return t_hash_start(m, pos);
2499
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002500 /*
2501 * Unfortunately, we need to restart at ftrace_pages_start
2502 * every time we let go of the ftrace_mutex. This is because
2503 * those pointers can change without the lock.
2504 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002505 iter->pg = ftrace_pages_start;
2506 iter->idx = 0;
2507 for (l = 0; l <= *pos; ) {
2508 p = t_next(m, p, &l);
2509 if (!p)
2510 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002511 }
walimis5821e1b2008-11-15 15:19:06 +08002512
Steven Rostedt69a30832011-12-19 15:21:16 -05002513 if (!p)
2514 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002515
2516 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002517}
2518
2519static void t_stop(struct seq_file *m, void *p)
2520{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002521 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002522}
2523
2524static int t_show(struct seq_file *m, void *v)
2525{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002526 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002527 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002528
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002529 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002530 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002531
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002532 if (iter->flags & FTRACE_ITER_PRINTALL) {
2533 seq_printf(m, "#### all functions enabled ####\n");
2534 return 0;
2535 }
2536
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002537 rec = iter->func;
2538
Steven Rostedt5072c592008-05-12 21:20:43 +02002539 if (!rec)
2540 return 0;
2541
Steven Rostedt647bcd02011-05-03 14:39:21 -04002542 seq_printf(m, "%ps", (void *)rec->ip);
2543 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002544 seq_printf(m, " (%ld)%s",
2545 rec->flags & ~FTRACE_FL_MASK,
2546 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002547 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002548
2549 return 0;
2550}
2551
James Morris88e9d342009-09-22 16:43:43 -07002552static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002553 .start = t_start,
2554 .next = t_next,
2555 .stop = t_stop,
2556 .show = t_show,
2557};
2558
Ingo Molnare309b412008-05-12 21:20:51 +02002559static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002560ftrace_avail_open(struct inode *inode, struct file *file)
2561{
2562 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002563
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002564 if (unlikely(ftrace_disabled))
2565 return -ENODEV;
2566
Jiri Olsa50e18b92012-04-25 10:23:39 +02002567 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2568 if (iter) {
2569 iter->pg = ftrace_pages_start;
2570 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002571 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002572
Jiri Olsa50e18b92012-04-25 10:23:39 +02002573 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002574}
2575
Steven Rostedt647bcd02011-05-03 14:39:21 -04002576static int
2577ftrace_enabled_open(struct inode *inode, struct file *file)
2578{
2579 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002580
2581 if (unlikely(ftrace_disabled))
2582 return -ENODEV;
2583
Jiri Olsa50e18b92012-04-25 10:23:39 +02002584 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2585 if (iter) {
2586 iter->pg = ftrace_pages_start;
2587 iter->flags = FTRACE_ITER_ENABLED;
2588 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002589 }
2590
Jiri Olsa50e18b92012-04-25 10:23:39 +02002591 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002592}
2593
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002594static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002595{
Steven Rostedt52baf112009-02-14 01:15:39 -05002596 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002597 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002598 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002599}
2600
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002601/**
2602 * ftrace_regex_open - initialize function tracer filter files
2603 * @ops: The ftrace_ops that hold the hash filters
2604 * @flag: The type of filter to process
2605 * @inode: The inode, usually passed in to your open routine
2606 * @file: The file, usually passed in to your open routine
2607 *
2608 * ftrace_regex_open() initializes the filter files for the
2609 * @ops. Depending on @flag it may process the filter hash or
2610 * the notrace hash of @ops. With this called from the open
2611 * routine, you can use ftrace_filter_write() for the write
2612 * routine if @flag has FTRACE_ITER_FILTER set, or
2613 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2614 * ftrace_regex_lseek() should be used as the lseek routine, and
2615 * release must call ftrace_regex_release().
2616 */
2617int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002618ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002619 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002620{
2621 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002622 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002623 int ret = 0;
2624
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002625 if (unlikely(ftrace_disabled))
2626 return -ENODEV;
2627
Steven Rostedt5072c592008-05-12 21:20:43 +02002628 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2629 if (!iter)
2630 return -ENOMEM;
2631
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002632 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2633 kfree(iter);
2634 return -ENOMEM;
2635 }
2636
Steven Rostedtf45948e2011-05-02 12:29:25 -04002637 if (flag & FTRACE_ITER_NOTRACE)
2638 hash = ops->notrace_hash;
2639 else
2640 hash = ops->filter_hash;
2641
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002642 iter->ops = ops;
2643 iter->flags = flag;
2644
2645 if (file->f_mode & FMODE_WRITE) {
2646 mutex_lock(&ftrace_lock);
2647 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2648 mutex_unlock(&ftrace_lock);
2649
2650 if (!iter->hash) {
2651 trace_parser_put(&iter->parser);
2652 kfree(iter);
2653 return -ENOMEM;
2654 }
2655 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002656
Steven Rostedt41c52c02008-05-22 11:46:33 -04002657 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002658
Steven Rostedt5072c592008-05-12 21:20:43 +02002659 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002660 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002661 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002662
2663 if (file->f_mode & FMODE_READ) {
2664 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002665
2666 ret = seq_open(file, &show_ftrace_seq_ops);
2667 if (!ret) {
2668 struct seq_file *m = file->private_data;
2669 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002670 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002671 /* Failed */
2672 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002673 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002674 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002675 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002676 } else
2677 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002678 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002679
2680 return ret;
2681}
2682
Steven Rostedt41c52c02008-05-22 11:46:33 -04002683static int
2684ftrace_filter_open(struct inode *inode, struct file *file)
2685{
Steven Rostedt69a30832011-12-19 15:21:16 -05002686 return ftrace_regex_open(&global_ops,
2687 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2688 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002689}
2690
2691static int
2692ftrace_notrace_open(struct inode *inode, struct file *file)
2693{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002694 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002695 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002696}
2697
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002698loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -08002699ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
Steven Rostedt5072c592008-05-12 21:20:43 +02002700{
2701 loff_t ret;
2702
2703 if (file->f_mode & FMODE_READ)
Andrew Morton965c8e52012-12-17 15:59:39 -08002704 ret = seq_lseek(file, offset, whence);
Steven Rostedt5072c592008-05-12 21:20:43 +02002705 else
2706 file->f_pos = ret = 1;
2707
2708 return ret;
2709}
2710
Steven Rostedt64e7c442009-02-13 17:08:48 -05002711static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002712{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002713 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002714 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002715
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002716 switch (type) {
2717 case MATCH_FULL:
2718 if (strcmp(str, regex) == 0)
2719 matched = 1;
2720 break;
2721 case MATCH_FRONT_ONLY:
2722 if (strncmp(str, regex, len) == 0)
2723 matched = 1;
2724 break;
2725 case MATCH_MIDDLE_ONLY:
2726 if (strstr(str, regex))
2727 matched = 1;
2728 break;
2729 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002730 slen = strlen(str);
2731 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002732 matched = 1;
2733 break;
2734 }
2735
2736 return matched;
2737}
2738
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002739static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002740enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002741{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002742 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002743 int ret = 0;
2744
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002745 entry = ftrace_lookup_ip(hash, rec->ip);
2746 if (not) {
2747 /* Do nothing if it doesn't exist */
2748 if (!entry)
2749 return 0;
2750
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002751 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002752 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002753 /* Do nothing if it exists */
2754 if (entry)
2755 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002756
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002757 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002758 }
2759 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002760}
2761
Steven Rostedt64e7c442009-02-13 17:08:48 -05002762static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002763ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2764 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002765{
2766 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002767 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002768
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002769 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2770
2771 if (mod) {
2772 /* module lookup requires matching the module */
2773 if (!modname || strcmp(modname, mod))
2774 return 0;
2775
2776 /* blank search means to match all funcs in the mod */
2777 if (!len)
2778 return 1;
2779 }
2780
Steven Rostedt64e7c442009-02-13 17:08:48 -05002781 return ftrace_match(str, regex, len, type);
2782}
2783
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002784static int
2785match_records(struct ftrace_hash *hash, char *buff,
2786 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002787{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002788 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002789 struct ftrace_page *pg;
2790 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002791 int type = MATCH_FULL;
2792 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002793 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002794 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002795
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002796 if (len) {
2797 type = filter_parse_regex(buff, len, &search, &not);
2798 search_len = strlen(search);
2799 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002800
Steven Rostedt52baf112009-02-14 01:15:39 -05002801 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002802
2803 if (unlikely(ftrace_disabled))
2804 goto out_unlock;
2805
Steven Rostedt265c8312009-02-13 12:43:56 -05002806 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002807 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002808 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002809 if (ret < 0) {
2810 found = ret;
2811 goto out_unlock;
2812 }
Li Zefan311d16d2009-12-08 11:15:11 +08002813 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002814 }
2815 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002816 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002817 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002818
2819 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002820}
2821
Steven Rostedt64e7c442009-02-13 17:08:48 -05002822static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002823ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002824{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002825 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002826}
2827
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002828static int
2829ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002830{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002831 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002832
Steven Rostedt64e7c442009-02-13 17:08:48 -05002833 /* blank or '*' mean the same */
2834 if (strcmp(buff, "*") == 0)
2835 buff[0] = 0;
2836
2837 /* handle the case of 'dont filter this module' */
2838 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2839 buff[0] = 0;
2840 not = 1;
2841 }
2842
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002843 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002844}
2845
Steven Rostedtf6180772009-02-14 00:40:25 -05002846/*
2847 * We register the module command as a template to show others how
2848 * to register the a command as well.
2849 */
2850
2851static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002852ftrace_mod_callback(struct ftrace_hash *hash,
2853 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002854{
2855 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002856 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002857
2858 /*
2859 * cmd == 'mod' because we only registered this func
2860 * for the 'mod' ftrace_func_command.
2861 * But if you register one func with multiple commands,
2862 * you can tell which command was used by the cmd
2863 * parameter.
2864 */
2865
2866 /* we must have a module name */
2867 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002868 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002869
2870 mod = strsep(&param, ":");
2871 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002872 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002873
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002874 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002875 if (!ret)
2876 ret = -EINVAL;
2877 if (ret < 0)
2878 return ret;
2879
2880 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002881}
2882
2883static struct ftrace_func_command ftrace_mod_cmd = {
2884 .name = "mod",
2885 .func = ftrace_mod_callback,
2886};
2887
2888static int __init ftrace_mod_cmd_init(void)
2889{
2890 return register_ftrace_command(&ftrace_mod_cmd);
2891}
Steven Rostedt6f415672012-10-05 12:13:07 -04002892core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002893
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002894static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002895 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002896{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002897 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002898 struct hlist_head *hhd;
2899 struct hlist_node *n;
2900 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002901
2902 key = hash_long(ip, FTRACE_HASH_BITS);
2903
2904 hhd = &ftrace_func_hash[key];
2905
2906 if (hlist_empty(hhd))
2907 return;
2908
2909 /*
2910 * Disable preemption for these calls to prevent a RCU grace
2911 * period. This syncs the hash iteration and freeing of items
2912 * on the hash. rcu_read_lock is too dangerous here.
2913 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002914 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002915 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2916 if (entry->ip == ip)
2917 entry->ops->func(ip, parent_ip, &entry->data);
2918 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002919 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002920}
2921
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002924 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925};
2926
Steven Rostedtb6887d72009-02-17 12:32:04 -05002927static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928
Steven Rostedtb6887d72009-02-17 12:32:04 -05002929static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930{
Steven Rostedtb8489142011-05-04 09:27:52 -04002931 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002932 int i;
2933
Steven Rostedtb6887d72009-02-17 12:32:04 -05002934 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002935 return;
2936
2937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2938 struct hlist_head *hhd = &ftrace_func_hash[i];
2939 if (hhd->first)
2940 break;
2941 }
2942 /* Nothing registered? */
2943 if (i == FTRACE_FUNC_HASHSIZE)
2944 return;
2945
Steven Rostedtb8489142011-05-04 09:27:52 -04002946 ret = __register_ftrace_function(&trace_probe_ops);
2947 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002948 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002949
Steven Rostedtb6887d72009-02-17 12:32:04 -05002950 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002951}
2952
Steven Rostedtb6887d72009-02-17 12:32:04 -05002953static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002954{
Steven Rostedtb8489142011-05-04 09:27:52 -04002955 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002956 int i;
2957
Steven Rostedtb6887d72009-02-17 12:32:04 -05002958 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002959 return;
2960
2961 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2962 struct hlist_head *hhd = &ftrace_func_hash[i];
2963 if (hhd->first)
2964 return;
2965 }
2966
2967 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002968 ret = __unregister_ftrace_function(&trace_probe_ops);
2969 if (!ret)
2970 ftrace_shutdown(&trace_probe_ops, 0);
2971
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973}
2974
2975
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04002976static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002977{
Steven Rostedt59df055f2009-02-14 15:29:06 -05002978 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04002979 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002980 kfree(entry);
2981}
2982
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002984register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002985 void *data)
2986{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002987 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002988 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
2989 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002990 struct ftrace_page *pg;
2991 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002992 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002993 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002994 int count = 0;
2995 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002996 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002997
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002998 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002999 len = strlen(search);
3000
Steven Rostedtb6887d72009-02-17 12:32:04 -05003001 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002 if (WARN_ON(not))
3003 return -EINVAL;
3004
3005 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003006
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003007 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3008 if (!hash) {
3009 count = -ENOMEM;
Steven Rostedt45a4a232011-04-21 23:16:46 -04003010 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003011 }
3012
3013 if (unlikely(ftrace_disabled)) {
3014 count = -ENODEV;
3015 goto out_unlock;
3016 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003017
Steven Rostedt59df055f2009-02-14 15:29:06 -05003018 do_for_each_ftrace_rec(pg, rec) {
3019
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003020 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003021 continue;
3022
3023 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3024 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003025 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026 if (!count)
3027 count = -ENOMEM;
3028 goto out_unlock;
3029 }
3030
3031 count++;
3032
3033 entry->data = data;
3034
3035 /*
3036 * The caller might want to do something special
3037 * for each function we find. We call the callback
3038 * to give the caller an opportunity to do so.
3039 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003040 if (ops->init) {
3041 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042 /* caller does not like this func */
3043 kfree(entry);
3044 continue;
3045 }
3046 }
3047
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003048 ret = enter_record(hash, rec, 0);
3049 if (ret < 0) {
3050 kfree(entry);
3051 count = ret;
3052 goto out_unlock;
3053 }
3054
Steven Rostedt59df055f2009-02-14 15:29:06 -05003055 entry->ops = ops;
3056 entry->ip = rec->ip;
3057
3058 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3059 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3060
3061 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003062
3063 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3064 if (ret < 0)
3065 count = ret;
3066
Steven Rostedtb6887d72009-02-17 12:32:04 -05003067 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003068
3069 out_unlock:
3070 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003071 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003072
3073 return count;
3074}
3075
3076enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003077 PROBE_TEST_FUNC = 1,
3078 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003079};
3080
3081static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003082__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003083 void *data, int flags)
3084{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003085 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003086 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003087 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003088 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003089 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003090 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003091 struct hlist_node *n, *tmp;
3092 char str[KSYM_SYMBOL_LEN];
3093 int type = MATCH_FULL;
3094 int i, len = 0;
3095 char *search;
3096
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003097 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003099 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100 int not;
3101
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003102 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103 len = strlen(search);
3104
Steven Rostedtb6887d72009-02-17 12:32:04 -05003105 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 if (WARN_ON(not))
3107 return;
3108 }
3109
3110 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003111
3112 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3113 if (!hash)
3114 /* Hmm, should report this somehow */
3115 goto out_unlock;
3116
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003117 INIT_LIST_HEAD(&free_list);
3118
Steven Rostedt59df055f2009-02-14 15:29:06 -05003119 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3120 struct hlist_head *hhd = &ftrace_func_hash[i];
3121
3122 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3123
3124 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003125 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003126 continue;
3127
Steven Rostedtb6887d72009-02-17 12:32:04 -05003128 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003129 continue;
3130
3131 /* do this last, since it is the most expensive */
3132 if (glob) {
3133 kallsyms_lookup(entry->ip, NULL, NULL,
3134 NULL, str);
3135 if (!ftrace_match(str, glob, len, type))
3136 continue;
3137 }
3138
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003139 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3140 /* It is possible more than one entry had this ip */
3141 if (rec_entry)
3142 free_hash_entry(hash, rec_entry);
3143
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003144 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003145 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003146 }
3147 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003148 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003149 /*
3150 * Remove after the disable is called. Otherwise, if the last
3151 * probe is removed, a null hash means *all enabled*.
3152 */
3153 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003154 synchronize_sched();
3155 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3156 list_del(&entry->free_list);
3157 ftrace_free_entry(entry);
3158 }
3159
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003160 out_unlock:
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003162 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003163}
3164
3165void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003166unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003167 void *data)
3168{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003169 __unregister_ftrace_function_probe(glob, ops, data,
3170 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003171}
3172
3173void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003174unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003175{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003176 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003177}
3178
Steven Rostedtb6887d72009-02-17 12:32:04 -05003179void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003180{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003181 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003182}
3183
Steven Rostedtf6180772009-02-14 00:40:25 -05003184static LIST_HEAD(ftrace_commands);
3185static DEFINE_MUTEX(ftrace_cmd_mutex);
3186
3187int register_ftrace_command(struct ftrace_func_command *cmd)
3188{
3189 struct ftrace_func_command *p;
3190 int ret = 0;
3191
3192 mutex_lock(&ftrace_cmd_mutex);
3193 list_for_each_entry(p, &ftrace_commands, list) {
3194 if (strcmp(cmd->name, p->name) == 0) {
3195 ret = -EBUSY;
3196 goto out_unlock;
3197 }
3198 }
3199 list_add(&cmd->list, &ftrace_commands);
3200 out_unlock:
3201 mutex_unlock(&ftrace_cmd_mutex);
3202
3203 return ret;
3204}
3205
3206int unregister_ftrace_command(struct ftrace_func_command *cmd)
3207{
3208 struct ftrace_func_command *p, *n;
3209 int ret = -ENODEV;
3210
3211 mutex_lock(&ftrace_cmd_mutex);
3212 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3213 if (strcmp(cmd->name, p->name) == 0) {
3214 ret = 0;
3215 list_del_init(&p->list);
3216 goto out_unlock;
3217 }
3218 }
3219 out_unlock:
3220 mutex_unlock(&ftrace_cmd_mutex);
3221
3222 return ret;
3223}
3224
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003225static int ftrace_process_regex(struct ftrace_hash *hash,
3226 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003227{
Steven Rostedtf6180772009-02-14 00:40:25 -05003228 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003229 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003230 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003231
3232 func = strsep(&next, ":");
3233
3234 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003235 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003236 if (!ret)
3237 ret = -EINVAL;
3238 if (ret < 0)
3239 return ret;
3240 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003241 }
3242
Steven Rostedtf6180772009-02-14 00:40:25 -05003243 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003244
3245 command = strsep(&next, ":");
3246
Steven Rostedtf6180772009-02-14 00:40:25 -05003247 mutex_lock(&ftrace_cmd_mutex);
3248 list_for_each_entry(p, &ftrace_commands, list) {
3249 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003250 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003251 goto out_unlock;
3252 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003253 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003254 out_unlock:
3255 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003256
Steven Rostedtf6180772009-02-14 00:40:25 -05003257 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003258}
3259
Ingo Molnare309b412008-05-12 21:20:51 +02003260static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003261ftrace_regex_write(struct file *file, const char __user *ubuf,
3262 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003263{
3264 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003265 struct trace_parser *parser;
3266 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003267
Li Zefan4ba79782009-09-22 13:52:20 +08003268 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003269 return 0;
3270
Steven Rostedt41c52c02008-05-22 11:46:33 -04003271 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003272
Steven Rostedt45a4a232011-04-21 23:16:46 -04003273 ret = -ENODEV;
3274 if (unlikely(ftrace_disabled))
3275 goto out_unlock;
3276
Steven Rostedt5072c592008-05-12 21:20:43 +02003277 if (file->f_mode & FMODE_READ) {
3278 struct seq_file *m = file->private_data;
3279 iter = m->private;
3280 } else
3281 iter = file->private_data;
3282
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003283 parser = &iter->parser;
3284 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003285
Li Zefan4ba79782009-09-22 13:52:20 +08003286 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003287 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003288 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003289 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003290 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003291 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003292 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003293 }
3294
Steven Rostedt5072c592008-05-12 21:20:43 +02003295 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003296out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003297 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003298
Steven Rostedt5072c592008-05-12 21:20:43 +02003299 return ret;
3300}
3301
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003302ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003303ftrace_filter_write(struct file *file, const char __user *ubuf,
3304 size_t cnt, loff_t *ppos)
3305{
3306 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3307}
3308
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003309ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003310ftrace_notrace_write(struct file *file, const char __user *ubuf,
3311 size_t cnt, loff_t *ppos)
3312{
3313 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3314}
3315
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003316static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003317ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3318{
3319 struct ftrace_func_entry *entry;
3320
3321 if (!ftrace_location(ip))
3322 return -EINVAL;
3323
3324 if (remove) {
3325 entry = ftrace_lookup_ip(hash, ip);
3326 if (!entry)
3327 return -ENOENT;
3328 free_hash_entry(hash, entry);
3329 return 0;
3330 }
3331
3332 return add_hash_entry(hash, ip);
3333}
3334
3335static int
3336ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3337 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003338{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003339 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003340 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003341 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003342
Steven Rostedt936e0742011-05-05 22:54:01 -04003343 /* All global ops uses the global ops filters */
3344 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3345 ops = &global_ops;
3346
Steven Rostedt41c52c02008-05-22 11:46:33 -04003347 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003348 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003349
Steven Rostedtf45948e2011-05-02 12:29:25 -04003350 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003351 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003352 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003353 orig_hash = &ops->notrace_hash;
3354
3355 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3356 if (!hash)
3357 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003358
Steven Rostedt41c52c02008-05-22 11:46:33 -04003359 mutex_lock(&ftrace_regex_lock);
3360 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003361 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003362 if (buf && !ftrace_match_records(hash, buf, len)) {
3363 ret = -EINVAL;
3364 goto out_regex_unlock;
3365 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003366 if (ip) {
3367 ret = ftrace_match_addr(hash, ip, remove);
3368 if (ret < 0)
3369 goto out_regex_unlock;
3370 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003371
3372 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003373 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003374 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3375 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003376 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003377
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003378 mutex_unlock(&ftrace_lock);
3379
Jiri Olsaac483c42012-01-02 10:04:14 +01003380 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003381 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003382
3383 free_ftrace_hash(hash);
3384 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003385}
3386
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003387static int
3388ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3389 int reset, int enable)
3390{
3391 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3392}
3393
3394/**
3395 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3396 * @ops - the ops to set the filter with
3397 * @ip - the address to add to or remove from the filter.
3398 * @remove - non zero to remove the ip from the filter
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 @ip is NULL, it failes to update filter.
3403 */
3404int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3405 int remove, int reset)
3406{
3407 return ftrace_set_addr(ops, ip, remove, reset, 1);
3408}
3409EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3410
3411static int
3412ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3413 int reset, int enable)
3414{
3415 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3416}
3417
Steven Rostedt77a2b372008-05-12 21:20:45 +02003418/**
3419 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003420 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003421 * @buf - the string that holds the function filter text.
3422 * @len - the length of the string.
3423 * @reset - non zero to reset all filters before applying this filter.
3424 *
3425 * Filters denote which functions should be enabled when tracing is enabled.
3426 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3427 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003428int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003429 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003430{
Jiri Olsaac483c42012-01-02 10:04:14 +01003431 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003432}
Steven Rostedt936e0742011-05-05 22:54:01 -04003433EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003434
Steven Rostedt41c52c02008-05-22 11:46:33 -04003435/**
3436 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003437 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003438 * @buf - the string that holds the function notrace text.
3439 * @len - the length of the string.
3440 * @reset - non zero to reset all filters before applying this filter.
3441 *
3442 * Notrace Filters denote which functions should not be enabled when tracing
3443 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3444 * for tracing.
3445 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003446int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003447 int len, int reset)
3448{
Jiri Olsaac483c42012-01-02 10:04:14 +01003449 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003450}
3451EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3452/**
3453 * ftrace_set_filter - set a function to filter on in ftrace
3454 * @ops - the ops to set the filter with
3455 * @buf - the string that holds the function filter text.
3456 * @len - the length of the string.
3457 * @reset - non zero to reset all filters before applying this filter.
3458 *
3459 * Filters denote which functions should be enabled when tracing is enabled.
3460 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3461 */
3462void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3463{
3464 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3465}
3466EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3467
3468/**
3469 * ftrace_set_notrace - set a function to not trace in ftrace
3470 * @ops - the ops to set the notrace filter with
3471 * @buf - the string that holds the function notrace text.
3472 * @len - the length of the string.
3473 * @reset - non zero to reset all filters before applying this filter.
3474 *
3475 * Notrace Filters denote which functions should not be enabled when tracing
3476 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3477 * for tracing.
3478 */
3479void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003480{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003481 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003482}
Steven Rostedt936e0742011-05-05 22:54:01 -04003483EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003484
Steven Rostedt2af15d62009-05-28 13:37:24 -04003485/*
3486 * command line interface to allow users to set filters on boot up.
3487 */
3488#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3489static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3490static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3491
3492static int __init set_ftrace_notrace(char *str)
3493{
Chen Gang9607a862013-04-08 12:06:44 +08003494 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003495 return 1;
3496}
3497__setup("ftrace_notrace=", set_ftrace_notrace);
3498
3499static int __init set_ftrace_filter(char *str)
3500{
Chen Gang9607a862013-04-08 12:06:44 +08003501 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003502 return 1;
3503}
3504__setup("ftrace_filter=", set_ftrace_filter);
3505
Stefan Assmann369bc182009-10-12 22:17:21 +02003506#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003507static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003508static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3509
Stefan Assmann369bc182009-10-12 22:17:21 +02003510static int __init set_graph_function(char *str)
3511{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003512 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003513 return 1;
3514}
3515__setup("ftrace_graph_filter=", set_graph_function);
3516
3517static void __init set_ftrace_early_graph(char *buf)
3518{
3519 int ret;
3520 char *func;
3521
3522 while (buf) {
3523 func = strsep(&buf, ",");
3524 /* we allow only one expression at a time */
3525 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3526 func);
3527 if (ret)
3528 printk(KERN_DEBUG "ftrace: function %s not "
3529 "traceable\n", func);
3530 }
3531}
3532#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3533
Steven Rostedt2a85a372011-12-19 21:57:44 -05003534void __init
3535ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003536{
3537 char *func;
3538
3539 while (buf) {
3540 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003541 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003542 }
3543}
3544
3545static void __init set_ftrace_early_filters(void)
3546{
3547 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003548 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003549 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003550 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003551#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3552 if (ftrace_graph_buf[0])
3553 set_ftrace_early_graph(ftrace_graph_buf);
3554#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003555}
3556
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003557int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003558{
3559 struct seq_file *m = (struct seq_file *)file->private_data;
3560 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003561 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003562 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003563 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003564 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003565
Steven Rostedt41c52c02008-05-22 11:46:33 -04003566 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003567 if (file->f_mode & FMODE_READ) {
3568 iter = m->private;
3569
3570 seq_release(inode, file);
3571 } else
3572 iter = file->private_data;
3573
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003574 parser = &iter->parser;
3575 if (trace_parser_loaded(parser)) {
3576 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003577 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003578 }
3579
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003580 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003581
Steven Rostedt058e2972011-04-29 22:35:33 -04003582 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003583 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3584
3585 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003586 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003587 else
3588 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003589
Steven Rostedt058e2972011-04-29 22:35:33 -04003590 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003591 ret = ftrace_hash_move(iter->ops, filter_hash,
3592 orig_hash, iter->hash);
3593 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3594 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003595 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003596
Steven Rostedt058e2972011-04-29 22:35:33 -04003597 mutex_unlock(&ftrace_lock);
3598 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003599 free_ftrace_hash(iter->hash);
3600 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003601
Steven Rostedt41c52c02008-05-22 11:46:33 -04003602 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003603 return 0;
3604}
3605
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003606static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003607 .open = ftrace_avail_open,
3608 .read = seq_read,
3609 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003610 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003611};
3612
Steven Rostedt647bcd02011-05-03 14:39:21 -04003613static const struct file_operations ftrace_enabled_fops = {
3614 .open = ftrace_enabled_open,
3615 .read = seq_read,
3616 .llseek = seq_lseek,
3617 .release = seq_release_private,
3618};
3619
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003620static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003621 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003622 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003623 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003624 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003625 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003626};
3627
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003628static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003629 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003630 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003631 .write = ftrace_notrace_write,
3632 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003633 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003634};
3635
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003636#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3637
3638static DEFINE_MUTEX(graph_lock);
3639
3640int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003641int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003642unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3643
3644static void *
Li Zefan85951842009-06-24 09:54:00 +08003645__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003646{
Li Zefan85951842009-06-24 09:54:00 +08003647 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003648 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003649 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003650}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003651
Li Zefan85951842009-06-24 09:54:00 +08003652static void *
3653g_next(struct seq_file *m, void *v, loff_t *pos)
3654{
3655 (*pos)++;
3656 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003657}
3658
3659static void *g_start(struct seq_file *m, loff_t *pos)
3660{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003661 mutex_lock(&graph_lock);
3662
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003663 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003664 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003665 return (void *)1;
3666
Li Zefan85951842009-06-24 09:54:00 +08003667 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003668}
3669
3670static void g_stop(struct seq_file *m, void *p)
3671{
3672 mutex_unlock(&graph_lock);
3673}
3674
3675static int g_show(struct seq_file *m, void *v)
3676{
3677 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003678
3679 if (!ptr)
3680 return 0;
3681
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003682 if (ptr == (unsigned long *)1) {
3683 seq_printf(m, "#### all functions enabled ####\n");
3684 return 0;
3685 }
3686
Steven Rostedtb375a112009-09-17 00:05:58 -04003687 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003688
3689 return 0;
3690}
3691
James Morris88e9d342009-09-22 16:43:43 -07003692static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003693 .start = g_start,
3694 .next = g_next,
3695 .stop = g_stop,
3696 .show = g_show,
3697};
3698
3699static int
3700ftrace_graph_open(struct inode *inode, struct file *file)
3701{
3702 int ret = 0;
3703
3704 if (unlikely(ftrace_disabled))
3705 return -ENODEV;
3706
3707 mutex_lock(&graph_lock);
3708 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003709 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003710 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003711 ftrace_graph_count = 0;
3712 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3713 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003714 mutex_unlock(&graph_lock);
3715
Li Zefana4ec5e02009-09-18 14:06:28 +08003716 if (file->f_mode & FMODE_READ)
3717 ret = seq_open(file, &ftrace_graph_seq_ops);
3718
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003719 return ret;
3720}
3721
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003722static int
Li Zefan87827112009-07-23 11:29:11 +08003723ftrace_graph_release(struct inode *inode, struct file *file)
3724{
3725 if (file->f_mode & FMODE_READ)
3726 seq_release(inode, file);
3727 return 0;
3728}
3729
3730static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003731ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003732{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003733 struct dyn_ftrace *rec;
3734 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003735 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003736 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003737 int type, not;
3738 char *search;
3739 bool exists;
3740 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003741
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003742 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003743 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003744 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3745 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003746
3747 search_len = strlen(search);
3748
Steven Rostedt52baf112009-02-14 01:15:39 -05003749 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003750
3751 if (unlikely(ftrace_disabled)) {
3752 mutex_unlock(&ftrace_lock);
3753 return -ENODEV;
3754 }
3755
Steven Rostedt265c8312009-02-13 12:43:56 -05003756 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003757
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003758 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003759 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003760 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003761 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003762 if (array[i] == rec->ip) {
3763 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003764 break;
3765 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003766 }
3767
3768 if (!not) {
3769 fail = 0;
3770 if (!exists) {
3771 array[(*idx)++] = rec->ip;
3772 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3773 goto out;
3774 }
3775 } else {
3776 if (exists) {
3777 array[i] = array[--(*idx)];
3778 array[*idx] = 0;
3779 fail = 0;
3780 }
3781 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003782 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003783 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003784out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003785 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003786
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003787 if (fail)
3788 return -EINVAL;
3789
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003790 ftrace_graph_filter_enabled = !!(*idx);
3791
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003792 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003793}
3794
3795static ssize_t
3796ftrace_graph_write(struct file *file, const char __user *ubuf,
3797 size_t cnt, loff_t *ppos)
3798{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003799 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003800 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003801
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003802 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003803 return 0;
3804
3805 mutex_lock(&graph_lock);
3806
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003807 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3808 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003809 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003810 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003811
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003812 read = trace_get_user(&parser, ubuf, cnt, ppos);
3813
Li Zefan4ba79782009-09-22 13:52:20 +08003814 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003815 parser.buffer[parser.idx] = 0;
3816
3817 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003818 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003819 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003820 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003821 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003822 }
3823
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003824 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003825
3826out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003827 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003828out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003829 mutex_unlock(&graph_lock);
3830
3831 return ret;
3832}
3833
3834static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003835 .open = ftrace_graph_open,
3836 .read = seq_read,
3837 .write = ftrace_graph_write,
3838 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003839 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003840};
3841#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3842
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003843static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003844{
Steven Rostedt5072c592008-05-12 21:20:43 +02003845
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003846 trace_create_file("available_filter_functions", 0444,
3847 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003848
Steven Rostedt647bcd02011-05-03 14:39:21 -04003849 trace_create_file("enabled_functions", 0444,
3850 d_tracer, NULL, &ftrace_enabled_fops);
3851
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003852 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3853 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003854
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003855 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003856 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003857
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003858#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003859 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003860 NULL,
3861 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003862#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3863
Steven Rostedt5072c592008-05-12 21:20:43 +02003864 return 0;
3865}
3866
Steven Rostedt9fd49322012-04-24 22:32:06 -04003867static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003868{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003869 const unsigned long *ipa = a;
3870 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003871
Steven Rostedt9fd49322012-04-24 22:32:06 -04003872 if (*ipa > *ipb)
3873 return 1;
3874 if (*ipa < *ipb)
3875 return -1;
3876 return 0;
3877}
3878
3879static void ftrace_swap_ips(void *a, void *b, int size)
3880{
3881 unsigned long *ipa = a;
3882 unsigned long *ipb = b;
3883 unsigned long t;
3884
3885 t = *ipa;
3886 *ipa = *ipb;
3887 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003888}
3889
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003890static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003891 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003892 unsigned long *end)
3893{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003894 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003895 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003896 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003897 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003898 unsigned long *p;
3899 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003900 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003901 int ret = -ENOMEM;
3902
3903 count = end - start;
3904
3905 if (!count)
3906 return 0;
3907
Steven Rostedt9fd49322012-04-24 22:32:06 -04003908 sort(start, count, sizeof(*start),
3909 ftrace_cmp_ips, ftrace_swap_ips);
3910
Steven Rostedt706c81f2012-04-24 23:45:26 -04003911 start_pg = ftrace_allocate_pages(count);
3912 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003913 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003914
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003915 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003916
Steven Rostedt32082302011-12-16 14:42:37 -05003917 /*
3918 * Core and each module needs their own pages, as
3919 * modules will free them when they are removed.
3920 * Force a new page to be allocated for modules.
3921 */
Steven Rostedta7900872011-12-16 16:23:44 -05003922 if (!mod) {
3923 WARN_ON(ftrace_pages || ftrace_pages_start);
3924 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003925 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003926 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003927 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003928 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003929
Steven Rostedta7900872011-12-16 16:23:44 -05003930 if (WARN_ON(ftrace_pages->next)) {
3931 /* Hmm, we have free pages? */
3932 while (ftrace_pages->next)
3933 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003934 }
Steven Rostedta7900872011-12-16 16:23:44 -05003935
Steven Rostedt706c81f2012-04-24 23:45:26 -04003936 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003937 }
3938
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003939 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003940 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003941 while (p < end) {
3942 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003943 /*
3944 * Some architecture linkers will pad between
3945 * the different mcount_loc sections of different
3946 * object files to satisfy alignments.
3947 * Skip any NULL pointers.
3948 */
3949 if (!addr)
3950 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003951
3952 if (pg->index == pg->size) {
3953 /* We should have allocated enough */
3954 if (WARN_ON(!pg->next))
3955 break;
3956 pg = pg->next;
3957 }
3958
3959 rec = &pg->records[pg->index++];
3960 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003961 }
3962
Steven Rostedt706c81f2012-04-24 23:45:26 -04003963 /* We should have used all pages */
3964 WARN_ON(pg->next);
3965
3966 /* Assign the last page to ftrace_pages */
3967 ftrace_pages = pg;
3968
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003969 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003970 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003971
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003972 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003973 * We only need to disable interrupts on start up
3974 * because we are modifying code that an interrupt
3975 * may execute, and the modification is not atomic.
3976 * But for modules, nothing runs the code we modify
3977 * until we are finished with it, and there's no
3978 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003979 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003980 if (!mod)
3981 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003982 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003983 if (!mod)
3984 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003985 ret = 0;
3986 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003987 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003988
Steven Rostedta7900872011-12-16 16:23:44 -05003989 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003990}
3991
Steven Rostedt93eb6772009-04-15 13:24:06 -04003992#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003993
3994#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3995
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003996void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003997{
3998 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003999 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004000 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004001 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004002
Steven Rostedt93eb6772009-04-15 13:24:06 -04004003 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004004
4005 if (ftrace_disabled)
4006 goto out_unlock;
4007
Steven Rostedt32082302011-12-16 14:42:37 -05004008 /*
4009 * Each module has its own ftrace_pages, remove
4010 * them from the list.
4011 */
4012 last_pg = &ftrace_pages_start;
4013 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4014 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004015 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004016 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004017 * As core pages are first, the first
4018 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004019 */
Steven Rostedt32082302011-12-16 14:42:37 -05004020 if (WARN_ON(pg == ftrace_pages_start))
4021 goto out_unlock;
4022
4023 /* Check if we are deleting the last page */
4024 if (pg == ftrace_pages)
4025 ftrace_pages = next_to_ftrace_page(last_pg);
4026
4027 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004028 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4029 free_pages((unsigned long)pg->records, order);
4030 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004031 } else
4032 last_pg = &pg->next;
4033 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004034 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004035 mutex_unlock(&ftrace_lock);
4036}
4037
4038static void ftrace_init_module(struct module *mod,
4039 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004040{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004041 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004042 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004043 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004044}
4045
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004046static int ftrace_module_notify_enter(struct notifier_block *self,
4047 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004048{
4049 struct module *mod = data;
4050
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004051 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004052 ftrace_init_module(mod, mod->ftrace_callsites,
4053 mod->ftrace_callsites +
4054 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004055 return 0;
4056}
4057
4058static int ftrace_module_notify_exit(struct notifier_block *self,
4059 unsigned long val, void *data)
4060{
4061 struct module *mod = data;
4062
4063 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004064 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004065
4066 return 0;
4067}
4068#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004069static int ftrace_module_notify_enter(struct notifier_block *self,
4070 unsigned long val, void *data)
4071{
4072 return 0;
4073}
4074static int ftrace_module_notify_exit(struct notifier_block *self,
4075 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004076{
4077 return 0;
4078}
4079#endif /* CONFIG_MODULES */
4080
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004081struct notifier_block ftrace_module_enter_nb = {
4082 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004083 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004084};
4085
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004086struct notifier_block ftrace_module_exit_nb = {
4087 .notifier_call = ftrace_module_notify_exit,
4088 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4089};
4090
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004091extern unsigned long __start_mcount_loc[];
4092extern unsigned long __stop_mcount_loc[];
4093
4094void __init ftrace_init(void)
4095{
4096 unsigned long count, addr, flags;
4097 int ret;
4098
4099 /* Keep the ftrace pointer to the stub */
4100 addr = (unsigned long)ftrace_stub;
4101
4102 local_irq_save(flags);
4103 ftrace_dyn_arch_init(&addr);
4104 local_irq_restore(flags);
4105
4106 /* ftrace_dyn_arch_init places the return code in addr */
4107 if (addr)
4108 goto failed;
4109
4110 count = __stop_mcount_loc - __start_mcount_loc;
4111
4112 ret = ftrace_dyn_table_alloc(count);
4113 if (ret)
4114 goto failed;
4115
4116 last_ftrace_enabled = ftrace_enabled = 1;
4117
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004118 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004119 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004120 __stop_mcount_loc);
4121
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004122 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004123 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004124 pr_warning("Failed to register trace ftrace module enter notifier\n");
4125
4126 ret = register_module_notifier(&ftrace_module_exit_nb);
4127 if (ret)
4128 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004129
Steven Rostedt2af15d62009-05-28 13:37:24 -04004130 set_ftrace_early_filters();
4131
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004132 return;
4133 failed:
4134 ftrace_disabled = 1;
4135}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004136
Steven Rostedt3d083392008-05-12 21:20:42 +02004137#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004138
Steven Rostedt2b499382011-05-03 22:49:52 -04004139static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004140 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004141 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004142};
4143
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004144static int __init ftrace_nodyn_init(void)
4145{
4146 ftrace_enabled = 1;
4147 return 0;
4148}
Steven Rostedt6f415672012-10-05 12:13:07 -04004149core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004150
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004151static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4152static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004153/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004154# define ftrace_startup(ops, command) \
4155 ({ \
4156 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4157 0; \
4158 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004159# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004160# define ftrace_startup_sysctl() do { } while (0)
4161# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004162
4163static inline int
4164ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4165{
4166 return 1;
4167}
4168
Steven Rostedt3d083392008-05-12 21:20:42 +02004169#endif /* CONFIG_DYNAMIC_FTRACE */
4170
Steven Rostedtb8489142011-05-04 09:27:52 -04004171static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004172ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004173 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004174{
Jiri Olsae2484912012-02-15 15:51:48 +01004175 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4176 return;
4177
4178 /*
4179 * Some of the ops may be dynamically allocated,
4180 * they must be freed after a synchronize_sched().
4181 */
4182 preempt_disable_notrace();
4183 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004184 do_for_each_ftrace_op(op, ftrace_control_list) {
Jiri Olsae2484912012-02-15 15:51:48 +01004185 if (!ftrace_function_local_disabled(op) &&
4186 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004187 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004188 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004189 trace_recursion_clear(TRACE_CONTROL_BIT);
4190 preempt_enable_notrace();
4191}
4192
4193static struct ftrace_ops control_ops = {
4194 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004195 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004196};
4197
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004198static inline void
4199__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004200 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004201{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004202 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004203 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004204
Steven Rostedtccf36722012-06-05 09:44:25 -04004205 if (function_trace_stop)
4206 return;
4207
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004208 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4209 if (bit < 0)
4210 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004211
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004212 /*
4213 * Some of the ops may be dynamically allocated,
4214 * they must be freed after a synchronize_sched().
4215 */
4216 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004217 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004218 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004219 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004220 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004221 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004222 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004223}
4224
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004225/*
4226 * Some archs only support passing ip and parent_ip. Even though
4227 * the list function ignores the op parameter, we do not want any
4228 * C side effects, where a function is called without the caller
4229 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004230 * Archs are to support both the regs and ftrace_ops at the same time.
4231 * If they support ftrace_ops, it is assumed they support regs.
4232 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004233 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4234 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004235 * An architecture can pass partial regs with ftrace_ops and still
4236 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004237 */
4238#if ARCH_SUPPORTS_FTRACE_OPS
4239static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004240 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004241{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004242 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004243}
4244#else
4245static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4246{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004247 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004248}
4249#endif
4250
Steven Rostedte32d8952008-12-04 00:26:41 -05004251static void clear_ftrace_swapper(void)
4252{
4253 struct task_struct *p;
4254 int cpu;
4255
4256 get_online_cpus();
4257 for_each_online_cpu(cpu) {
4258 p = idle_task(cpu);
4259 clear_tsk_trace_trace(p);
4260 }
4261 put_online_cpus();
4262}
4263
4264static void set_ftrace_swapper(void)
4265{
4266 struct task_struct *p;
4267 int cpu;
4268
4269 get_online_cpus();
4270 for_each_online_cpu(cpu) {
4271 p = idle_task(cpu);
4272 set_tsk_trace_trace(p);
4273 }
4274 put_online_cpus();
4275}
4276
4277static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004278{
4279 struct task_struct *p;
4280
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004281 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004282 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004283 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004284 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004285 rcu_read_unlock();
4286
Steven Rostedte32d8952008-12-04 00:26:41 -05004287 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004288}
4289
Steven Rostedte32d8952008-12-04 00:26:41 -05004290static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004291{
4292 struct task_struct *p;
4293
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004294 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004295 do_each_pid_task(pid, PIDTYPE_PID, p) {
4296 set_tsk_trace_trace(p);
4297 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004298 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004299}
4300
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004301static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004302{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004303 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004304 clear_ftrace_swapper();
4305 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004306 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004307}
4308
4309static void set_ftrace_pid_task(struct pid *pid)
4310{
4311 if (pid == ftrace_swapper_pid)
4312 set_ftrace_swapper();
4313 else
4314 set_ftrace_pid(pid);
4315}
4316
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004317static int ftrace_pid_add(int p)
4318{
4319 struct pid *pid;
4320 struct ftrace_pid *fpid;
4321 int ret = -EINVAL;
4322
4323 mutex_lock(&ftrace_lock);
4324
4325 if (!p)
4326 pid = ftrace_swapper_pid;
4327 else
4328 pid = find_get_pid(p);
4329
4330 if (!pid)
4331 goto out;
4332
4333 ret = 0;
4334
4335 list_for_each_entry(fpid, &ftrace_pids, list)
4336 if (fpid->pid == pid)
4337 goto out_put;
4338
4339 ret = -ENOMEM;
4340
4341 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4342 if (!fpid)
4343 goto out_put;
4344
4345 list_add(&fpid->list, &ftrace_pids);
4346 fpid->pid = pid;
4347
4348 set_ftrace_pid_task(pid);
4349
4350 ftrace_update_pid_func();
4351 ftrace_startup_enable(0);
4352
4353 mutex_unlock(&ftrace_lock);
4354 return 0;
4355
4356out_put:
4357 if (pid != ftrace_swapper_pid)
4358 put_pid(pid);
4359
4360out:
4361 mutex_unlock(&ftrace_lock);
4362 return ret;
4363}
4364
4365static void ftrace_pid_reset(void)
4366{
4367 struct ftrace_pid *fpid, *safe;
4368
4369 mutex_lock(&ftrace_lock);
4370 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4371 struct pid *pid = fpid->pid;
4372
4373 clear_ftrace_pid_task(pid);
4374
4375 list_del(&fpid->list);
4376 kfree(fpid);
4377 }
4378
4379 ftrace_update_pid_func();
4380 ftrace_startup_enable(0);
4381
4382 mutex_unlock(&ftrace_lock);
4383}
4384
4385static void *fpid_start(struct seq_file *m, loff_t *pos)
4386{
4387 mutex_lock(&ftrace_lock);
4388
4389 if (list_empty(&ftrace_pids) && (!*pos))
4390 return (void *) 1;
4391
4392 return seq_list_start(&ftrace_pids, *pos);
4393}
4394
4395static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4396{
4397 if (v == (void *)1)
4398 return NULL;
4399
4400 return seq_list_next(v, &ftrace_pids, pos);
4401}
4402
4403static void fpid_stop(struct seq_file *m, void *p)
4404{
4405 mutex_unlock(&ftrace_lock);
4406}
4407
4408static int fpid_show(struct seq_file *m, void *v)
4409{
4410 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4411
4412 if (v == (void *)1) {
4413 seq_printf(m, "no pid\n");
4414 return 0;
4415 }
4416
4417 if (fpid->pid == ftrace_swapper_pid)
4418 seq_printf(m, "swapper tasks\n");
4419 else
4420 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4421
4422 return 0;
4423}
4424
4425static const struct seq_operations ftrace_pid_sops = {
4426 .start = fpid_start,
4427 .next = fpid_next,
4428 .stop = fpid_stop,
4429 .show = fpid_show,
4430};
4431
4432static int
4433ftrace_pid_open(struct inode *inode, struct file *file)
4434{
4435 int ret = 0;
4436
4437 if ((file->f_mode & FMODE_WRITE) &&
4438 (file->f_flags & O_TRUNC))
4439 ftrace_pid_reset();
4440
4441 if (file->f_mode & FMODE_READ)
4442 ret = seq_open(file, &ftrace_pid_sops);
4443
4444 return ret;
4445}
4446
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004447static ssize_t
4448ftrace_pid_write(struct file *filp, const char __user *ubuf,
4449 size_t cnt, loff_t *ppos)
4450{
Ingo Molnar457dc922009-11-23 11:03:28 +01004451 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004452 long val;
4453 int ret;
4454
4455 if (cnt >= sizeof(buf))
4456 return -EINVAL;
4457
4458 if (copy_from_user(&buf, ubuf, cnt))
4459 return -EFAULT;
4460
4461 buf[cnt] = 0;
4462
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004463 /*
4464 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4465 * to clean the filter quietly.
4466 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004467 tmp = strstrip(buf);
4468 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004469 return 1;
4470
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004471 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004472 if (ret < 0)
4473 return ret;
4474
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004475 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004476
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004477 return ret ? ret : cnt;
4478}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004479
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004480static int
4481ftrace_pid_release(struct inode *inode, struct file *file)
4482{
4483 if (file->f_mode & FMODE_READ)
4484 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004485
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004486 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004487}
4488
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004489static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004490 .open = ftrace_pid_open,
4491 .write = ftrace_pid_write,
4492 .read = seq_read,
4493 .llseek = seq_lseek,
4494 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004495};
4496
4497static __init int ftrace_init_debugfs(void)
4498{
4499 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004500
4501 d_tracer = tracing_init_dentry();
4502 if (!d_tracer)
4503 return 0;
4504
4505 ftrace_init_dyn_debugfs(d_tracer);
4506
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004507 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4508 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004509
4510 ftrace_profile_debugfs(d_tracer);
4511
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004512 return 0;
4513}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004514fs_initcall(ftrace_init_debugfs);
4515
Steven Rostedt3d083392008-05-12 21:20:42 +02004516/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004517 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004518 *
4519 * This function should be used by panic code. It stops ftrace
4520 * but in a not so nice way. If you need to simply kill ftrace
4521 * from a non-atomic section, use ftrace_kill.
4522 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004523void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004524{
4525 ftrace_disabled = 1;
4526 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004527 clear_ftrace_function();
4528}
4529
4530/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004531 * Test if ftrace is dead or not.
4532 */
4533int ftrace_is_dead(void)
4534{
4535 return ftrace_disabled;
4536}
4537
4538/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004539 * register_ftrace_function - register a function for profiling
4540 * @ops - ops structure that holds the function for profiling.
4541 *
4542 * Register a function to be called by all functions in the
4543 * kernel.
4544 *
4545 * Note: @ops->func and all the functions it calls must be labeled
4546 * with "notrace", otherwise it will go into a
4547 * recursive loop.
4548 */
4549int register_ftrace_function(struct ftrace_ops *ops)
4550{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004551 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004552
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004553 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004554
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004555 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004556 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004557 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004558
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004559 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004560
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004561 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004562}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004563EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004564
4565/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004566 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004567 * @ops - ops structure that holds the function to unregister
4568 *
4569 * Unregister a function that was added to be called by ftrace profiling.
4570 */
4571int unregister_ftrace_function(struct ftrace_ops *ops)
4572{
4573 int ret;
4574
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004575 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004576 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004577 if (!ret)
4578 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004579 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004580
4581 return ret;
4582}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004583EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004584
Ingo Molnare309b412008-05-12 21:20:51 +02004585int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004586ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004587 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004588 loff_t *ppos)
4589{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004590 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004591
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004592 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004593
Steven Rostedt45a4a232011-04-21 23:16:46 -04004594 if (unlikely(ftrace_disabled))
4595 goto out;
4596
4597 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004598
Li Zefana32c7762009-06-26 16:55:51 +08004599 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004600 goto out;
4601
Li Zefana32c7762009-06-26 16:55:51 +08004602 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004603
4604 if (ftrace_enabled) {
4605
4606 ftrace_startup_sysctl();
4607
4608 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004609 if (ftrace_ops_list != &ftrace_list_end) {
4610 if (ftrace_ops_list->next == &ftrace_list_end)
4611 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004612 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004613 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004614 }
4615
4616 } else {
4617 /* stopping ftrace calls (just send to ftrace_stub) */
4618 ftrace_trace_function = ftrace_stub;
4619
4620 ftrace_shutdown_sysctl();
4621 }
4622
4623 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004624 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004625 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004626}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004627
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004628#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004629
Steven Rostedt597af812009-04-03 15:24:12 -04004630static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004631static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004632
Steven Rostedte49dc192008-12-02 23:50:05 -05004633int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4634{
4635 return 0;
4636}
4637
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004638/* The callbacks that hook a function */
4639trace_func_graph_ret_t ftrace_graph_return =
4640 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004641trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004642
4643/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4644static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4645{
4646 int i;
4647 int ret = 0;
4648 unsigned long flags;
4649 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4650 struct task_struct *g, *t;
4651
4652 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4653 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4654 * sizeof(struct ftrace_ret_stack),
4655 GFP_KERNEL);
4656 if (!ret_stack_list[i]) {
4657 start = 0;
4658 end = i;
4659 ret = -ENOMEM;
4660 goto free;
4661 }
4662 }
4663
4664 read_lock_irqsave(&tasklist_lock, flags);
4665 do_each_thread(g, t) {
4666 if (start == end) {
4667 ret = -EAGAIN;
4668 goto unlock;
4669 }
4670
4671 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004672 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004673 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004674 t->curr_ret_stack = -1;
4675 /* Make sure the tasks see the -1 first: */
4676 smp_wmb();
4677 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004678 }
4679 } while_each_thread(g, t);
4680
4681unlock:
4682 read_unlock_irqrestore(&tasklist_lock, flags);
4683free:
4684 for (i = start; i < end; i++)
4685 kfree(ret_stack_list[i]);
4686 return ret;
4687}
4688
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004689static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004690ftrace_graph_probe_sched_switch(void *ignore,
4691 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004692{
4693 unsigned long long timestamp;
4694 int index;
4695
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004696 /*
4697 * Does the user want to count the time a function was asleep.
4698 * If so, do not update the time stamps.
4699 */
4700 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4701 return;
4702
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004703 timestamp = trace_clock_local();
4704
4705 prev->ftrace_timestamp = timestamp;
4706
4707 /* only process tasks that we timestamped */
4708 if (!next->ftrace_timestamp)
4709 return;
4710
4711 /*
4712 * Update all the counters in next to make up for the
4713 * time next was sleeping.
4714 */
4715 timestamp -= next->ftrace_timestamp;
4716
4717 for (index = next->curr_ret_stack; index >= 0; index--)
4718 next->ret_stack[index].calltime += timestamp;
4719}
4720
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004721/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004722static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004723{
4724 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004725 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004726
4727 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4728 sizeof(struct ftrace_ret_stack *),
4729 GFP_KERNEL);
4730
4731 if (!ret_stack_list)
4732 return -ENOMEM;
4733
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004734 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004735 for_each_online_cpu(cpu) {
4736 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004737 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004738 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004739
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004740 do {
4741 ret = alloc_retstack_tasklist(ret_stack_list);
4742 } while (ret == -EAGAIN);
4743
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004744 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004745 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004746 if (ret)
4747 pr_info("ftrace_graph: Couldn't activate tracepoint"
4748 " probe to kernel_sched_switch\n");
4749 }
4750
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004751 kfree(ret_stack_list);
4752 return ret;
4753}
4754
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004755/*
4756 * Hibernation protection.
4757 * The state of the current task is too much unstable during
4758 * suspend/restore to disk. We want to protect against that.
4759 */
4760static int
4761ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4762 void *unused)
4763{
4764 switch (state) {
4765 case PM_HIBERNATION_PREPARE:
4766 pause_graph_tracing();
4767 break;
4768
4769 case PM_POST_HIBERNATION:
4770 unpause_graph_tracing();
4771 break;
4772 }
4773 return NOTIFY_DONE;
4774}
4775
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004776int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4777 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004778{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004779 int ret = 0;
4780
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004781 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004782
Steven Rostedt05ce5812009-03-24 00:18:31 -04004783 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004784 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004785 ret = -EBUSY;
4786 goto out;
4787 }
4788
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004789 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4790 register_pm_notifier(&ftrace_suspend_notifier);
4791
Steven Rostedt597af812009-04-03 15:24:12 -04004792 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004793 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004794 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004795 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004796 goto out;
4797 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004798
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004799 ftrace_graph_return = retfunc;
4800 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004801
Steven Rostedta1cd6172011-05-23 15:24:25 -04004802 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004803
4804out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004805 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004806 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004807}
4808
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004809void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004810{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004811 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004812
Steven Rostedt597af812009-04-03 15:24:12 -04004813 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004814 goto out;
4815
Steven Rostedt597af812009-04-03 15:24:12 -04004816 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004817 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004818 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004819 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004820 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004821 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004822
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004823 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004824 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004825}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004826
Steven Rostedt868baf02011-02-10 21:26:13 -05004827static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4828
4829static void
4830graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4831{
4832 atomic_set(&t->tracing_graph_pause, 0);
4833 atomic_set(&t->trace_overrun, 0);
4834 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004835 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004836 smp_wmb();
4837 t->ret_stack = ret_stack;
4838}
4839
4840/*
4841 * Allocate a return stack for the idle task. May be the first
4842 * time through, or it may be done by CPU hotplug online.
4843 */
4844void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4845{
4846 t->curr_ret_stack = -1;
4847 /*
4848 * The idle task has no parent, it either has its own
4849 * stack or no stack at all.
4850 */
4851 if (t->ret_stack)
4852 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4853
4854 if (ftrace_graph_active) {
4855 struct ftrace_ret_stack *ret_stack;
4856
4857 ret_stack = per_cpu(idle_ret_stack, cpu);
4858 if (!ret_stack) {
4859 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4860 * sizeof(struct ftrace_ret_stack),
4861 GFP_KERNEL);
4862 if (!ret_stack)
4863 return;
4864 per_cpu(idle_ret_stack, cpu) = ret_stack;
4865 }
4866 graph_init_task(t, ret_stack);
4867 }
4868}
4869
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004870/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004871void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004872{
Steven Rostedt84047e32009-06-02 16:51:55 -04004873 /* Make sure we do not use the parent ret_stack */
4874 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004875 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004876
Steven Rostedt597af812009-04-03 15:24:12 -04004877 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004878 struct ftrace_ret_stack *ret_stack;
4879
4880 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004881 * sizeof(struct ftrace_ret_stack),
4882 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004883 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004884 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004885 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004886 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004887}
4888
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004889void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004890{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004891 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4892
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004893 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004894 /* NULL must become visible to IRQs before we free it: */
4895 barrier();
4896
4897 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004898}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004899
4900void ftrace_graph_stop(void)
4901{
4902 ftrace_stop();
4903}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004904#endif