blob: 9b44abb2c5a0bf585ff73de22c07031af35b76da [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 Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-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_enabled __read_mostly;
490
491/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400492static DEFINE_MUTEX(ftrace_profile_lock);
493
Steven Rostedtcafb1682009-03-24 20:50:39 -0400494static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400495
Namhyung Kim20079eb2013-04-10 08:55:50 +0900496#define FTRACE_PROFILE_HASH_BITS 10
497#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400498
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 Rostedt318e0a72009-03-25 20:06:34 -0400728 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729 if (ftrace_profile_pages_init(stat) < 0) {
730 kfree(stat->hash);
731 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 return -ENOMEM;
733 }
734
735 return 0;
736}
737
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738static int ftrace_profile_init(void)
739{
740 int cpu;
741 int ret = 0;
742
743 for_each_online_cpu(cpu) {
744 ret = ftrace_profile_init_cpu(cpu);
745 if (ret)
746 break;
747 }
748
749 return ret;
750}
751
Steven Rostedt493762f2009-03-23 17:12:36 -0400752/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753static struct ftrace_profile *
754ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400755{
756 struct ftrace_profile *rec;
757 struct hlist_head *hhd;
758 struct hlist_node *n;
759 unsigned long key;
760
Namhyung Kim20079eb2013-04-10 08:55:50 +0900761 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400763
764 if (hlist_empty(hhd))
765 return NULL;
766
767 hlist_for_each_entry_rcu(rec, n, hhd, node) {
768 if (rec->ip == ip)
769 return rec;
770 }
771
772 return NULL;
773}
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775static void ftrace_add_profile(struct ftrace_profile_stat *stat,
776 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400777{
778 unsigned long key;
779
Namhyung Kim20079eb2013-04-10 08:55:50 +0900780 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400782}
783
Steven Rostedt318e0a72009-03-25 20:06:34 -0400784/*
785 * The memory is already allocated, this simply finds a new record to use.
786 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400787static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400788ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400789{
790 struct ftrace_profile *rec = NULL;
791
Steven Rostedt318e0a72009-03-25 20:06:34 -0400792 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400794 goto out;
795
Steven Rostedt493762f2009-03-23 17:12:36 -0400796 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797 * Try to find the function again since an NMI
798 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400803
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 if (stat->pages->index == PROFILES_PER_PAGE) {
805 if (!stat->pages->next)
806 goto out;
807 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 }
809
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400811 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400813
Steven Rostedt493762f2009-03-23 17:12:36 -0400814 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400816
817 return rec;
818}
819
Steven Rostedt493762f2009-03-23 17:12:36 -0400820static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400821function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400822 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400823{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 struct ftrace_profile *rec;
826 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400827
828 if (!ftrace_profile_enabled)
829 return;
830
Steven Rostedt493762f2009-03-23 17:12:36 -0400831 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400832
833 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400834 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400835 goto out;
836
837 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400839 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400840 if (!rec)
841 goto out;
842 }
843
844 rec->counter++;
845 out:
846 local_irq_restore(flags);
847}
848
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849#ifdef CONFIG_FUNCTION_GRAPH_TRACER
850static int profile_graph_entry(struct ftrace_graph_ent *trace)
851{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400852 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400853 return 1;
854}
855
856static void profile_graph_return(struct ftrace_graph_ret *trace)
857{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400859 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400861 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400862
863 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400865 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866 goto out;
867
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400868 /* If the calltime was zero'd ignore it */
869 if (!trace->calltime)
870 goto out;
871
Steven Rostedta2a16d62009-03-24 23:17:58 -0400872 calltime = trace->rettime - trace->calltime;
873
874 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
875 int index;
876
877 index = trace->depth;
878
879 /* Append this call time to the parent time to subtract */
880 if (index)
881 current->ret_stack[index - 1].subtime += calltime;
882
883 if (current->ret_stack[index].subtime < calltime)
884 calltime -= current->ret_stack[index].subtime;
885 else
886 calltime = 0;
887 }
888
Steven Rostedtcafb1682009-03-24 20:50:39 -0400889 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400890 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400891 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400892 rec->time_squared += calltime * calltime;
893 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400894
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400896 local_irq_restore(flags);
897}
898
899static int register_ftrace_profiler(void)
900{
901 return register_ftrace_graph(&profile_graph_return,
902 &profile_graph_entry);
903}
904
905static void unregister_ftrace_profiler(void)
906{
907 unregister_ftrace_graph();
908}
909#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100910static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400911 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400912 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400913};
914
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400915static int register_ftrace_profiler(void)
916{
917 return register_ftrace_function(&ftrace_profile_ops);
918}
919
920static void unregister_ftrace_profiler(void)
921{
922 unregister_ftrace_function(&ftrace_profile_ops);
923}
924#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
925
Steven Rostedt493762f2009-03-23 17:12:36 -0400926static ssize_t
927ftrace_profile_write(struct file *filp, const char __user *ubuf,
928 size_t cnt, loff_t *ppos)
929{
930 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400931 int ret;
932
Peter Huewe22fe9b52011-06-07 21:58:27 +0200933 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
934 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400935 return ret;
936
937 val = !!val;
938
939 mutex_lock(&ftrace_profile_lock);
940 if (ftrace_profile_enabled ^ val) {
941 if (val) {
942 ret = ftrace_profile_init();
943 if (ret < 0) {
944 cnt = ret;
945 goto out;
946 }
947
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400948 ret = register_ftrace_profiler();
949 if (ret < 0) {
950 cnt = ret;
951 goto out;
952 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400953 ftrace_profile_enabled = 1;
954 } else {
955 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400956 /*
957 * unregister_ftrace_profiler calls stop_machine
958 * so this acts like an synchronize_sched.
959 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400960 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400961 }
962 }
963 out:
964 mutex_unlock(&ftrace_profile_lock);
965
Jiri Olsacf8517c2009-10-23 19:36:16 -0400966 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400967
968 return cnt;
969}
970
971static ssize_t
972ftrace_profile_read(struct file *filp, char __user *ubuf,
973 size_t cnt, loff_t *ppos)
974{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400975 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400976 int r;
977
978 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
979 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
980}
981
982static const struct file_operations ftrace_profile_fops = {
983 .open = tracing_open_generic,
984 .read = ftrace_profile_read,
985 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200986 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400987};
988
Steven Rostedtcafb1682009-03-24 20:50:39 -0400989/* used to initialize the real stat files */
990static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400991 .name = "functions",
992 .stat_start = function_stat_start,
993 .stat_next = function_stat_next,
994 .stat_cmp = function_stat_cmp,
995 .stat_headers = function_stat_headers,
996 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400997};
998
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400999static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001000{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001001 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001003 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001004 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001005 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001006
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007 for_each_possible_cpu(cpu) {
1008 stat = &per_cpu(ftrace_profile_stats, cpu);
1009
1010 /* allocate enough for function name + cpu number */
1011 name = kmalloc(32, GFP_KERNEL);
1012 if (!name) {
1013 /*
1014 * The files created are permanent, if something happens
1015 * we still do not free memory.
1016 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001017 WARN(1,
1018 "Could not allocate stat file for cpu %d\n",
1019 cpu);
1020 return;
1021 }
1022 stat->stat = function_stats;
1023 snprintf(name, 32, "function%d", cpu);
1024 stat->stat.name = name;
1025 ret = register_stat_tracer(&stat->stat);
1026 if (ret) {
1027 WARN(1,
1028 "Could not register function stat for cpu %d\n",
1029 cpu);
1030 kfree(name);
1031 return;
1032 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001033 }
1034
1035 entry = debugfs_create_file("function_profile_enabled", 0644,
1036 d_tracer, NULL, &ftrace_profile_fops);
1037 if (!entry)
1038 pr_warning("Could not create debugfs "
1039 "'function_profile_enabled' entry\n");
1040}
1041
1042#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001043static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001044{
1045}
1046#endif /* CONFIG_FUNCTION_PROFILER */
1047
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001048static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1049
Steven Rostedt3d083392008-05-12 21:20:42 +02001050#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001051
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001052#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001053# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001054#endif
1055
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001056static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1057
Steven Rostedtb6887d72009-02-17 12:32:04 -05001058struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001059 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001060 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001061 unsigned long flags;
1062 unsigned long ip;
1063 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001064 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001065};
1066
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001067struct ftrace_func_entry {
1068 struct hlist_node hlist;
1069 unsigned long ip;
1070};
1071
1072struct ftrace_hash {
1073 unsigned long size_bits;
1074 struct hlist_head *buckets;
1075 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001076 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001077};
1078
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001079/*
1080 * We make these constant because no one should touch them,
1081 * but they are used as the default "empty hash", to avoid allocating
1082 * it all the time. These are in a read only section such that if
1083 * anyone does try to modify it, it will cause an exception.
1084 */
1085static const struct hlist_head empty_buckets[1];
1086static const struct ftrace_hash empty_hash = {
1087 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001088};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001089#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001090
Steven Rostedt2b499382011-05-03 22:49:52 -04001091static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001092 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001093 .notrace_hash = EMPTY_HASH,
1094 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001095 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001096};
1097
Steven Rostedt41c52c02008-05-22 11:46:33 -04001098static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001099
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001100struct ftrace_page {
1101 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001102 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001103 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001104 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001105};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001106
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001107static struct ftrace_page *ftrace_new_pgs;
1108
Steven Rostedta7900872011-12-16 16:23:44 -05001109#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1110#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001111
1112/* estimate from running different kernels */
1113#define NR_TO_INIT 10000
1114
1115static struct ftrace_page *ftrace_pages_start;
1116static struct ftrace_page *ftrace_pages;
1117
Steven Rostedt06a51d92011-12-19 19:07:36 -05001118static bool ftrace_hash_empty(struct ftrace_hash *hash)
1119{
1120 return !hash || !hash->count;
1121}
1122
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123static struct ftrace_func_entry *
1124ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1125{
1126 unsigned long key;
1127 struct ftrace_func_entry *entry;
1128 struct hlist_head *hhd;
1129 struct hlist_node *n;
1130
Steven Rostedt06a51d92011-12-19 19:07:36 -05001131 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001132 return NULL;
1133
1134 if (hash->size_bits > 0)
1135 key = hash_long(ip, hash->size_bits);
1136 else
1137 key = 0;
1138
1139 hhd = &hash->buckets[key];
1140
1141 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1142 if (entry->ip == ip)
1143 return entry;
1144 }
1145 return NULL;
1146}
1147
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001148static void __add_hash_entry(struct ftrace_hash *hash,
1149 struct ftrace_func_entry *entry)
1150{
1151 struct hlist_head *hhd;
1152 unsigned long key;
1153
1154 if (hash->size_bits)
1155 key = hash_long(entry->ip, hash->size_bits);
1156 else
1157 key = 0;
1158
1159 hhd = &hash->buckets[key];
1160 hlist_add_head(&entry->hlist, hhd);
1161 hash->count++;
1162}
1163
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1165{
1166 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001167
1168 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1169 if (!entry)
1170 return -ENOMEM;
1171
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001173 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001174
1175 return 0;
1176}
1177
1178static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001179free_hash_entry(struct ftrace_hash *hash,
1180 struct ftrace_func_entry *entry)
1181{
1182 hlist_del(&entry->hlist);
1183 kfree(entry);
1184 hash->count--;
1185}
1186
1187static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188remove_hash_entry(struct ftrace_hash *hash,
1189 struct ftrace_func_entry *entry)
1190{
1191 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001192 hash->count--;
1193}
1194
1195static void ftrace_hash_clear(struct ftrace_hash *hash)
1196{
1197 struct hlist_head *hhd;
1198 struct hlist_node *tp, *tn;
1199 struct ftrace_func_entry *entry;
1200 int size = 1 << hash->size_bits;
1201 int i;
1202
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001203 if (!hash->count)
1204 return;
1205
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001206 for (i = 0; i < size; i++) {
1207 hhd = &hash->buckets[i];
1208 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001209 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 }
1211 FTRACE_WARN_ON(hash->count);
1212}
1213
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214static void free_ftrace_hash(struct ftrace_hash *hash)
1215{
1216 if (!hash || hash == EMPTY_HASH)
1217 return;
1218 ftrace_hash_clear(hash);
1219 kfree(hash->buckets);
1220 kfree(hash);
1221}
1222
Steven Rostedt07fd5512011-05-05 18:03:47 -04001223static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1224{
1225 struct ftrace_hash *hash;
1226
1227 hash = container_of(rcu, struct ftrace_hash, rcu);
1228 free_ftrace_hash(hash);
1229}
1230
1231static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1232{
1233 if (!hash || hash == EMPTY_HASH)
1234 return;
1235 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1236}
1237
Jiri Olsa5500fa52012-02-15 15:51:54 +01001238void ftrace_free_filter(struct ftrace_ops *ops)
1239{
1240 free_ftrace_hash(ops->filter_hash);
1241 free_ftrace_hash(ops->notrace_hash);
1242}
1243
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001244static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1245{
1246 struct ftrace_hash *hash;
1247 int size;
1248
1249 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1250 if (!hash)
1251 return NULL;
1252
1253 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001254 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255
1256 if (!hash->buckets) {
1257 kfree(hash);
1258 return NULL;
1259 }
1260
1261 hash->size_bits = size_bits;
1262
1263 return hash;
1264}
1265
1266static struct ftrace_hash *
1267alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1268{
1269 struct ftrace_func_entry *entry;
1270 struct ftrace_hash *new_hash;
1271 struct hlist_node *tp;
1272 int size;
1273 int ret;
1274 int i;
1275
1276 new_hash = alloc_ftrace_hash(size_bits);
1277 if (!new_hash)
1278 return NULL;
1279
1280 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001281 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001282 return new_hash;
1283
1284 size = 1 << hash->size_bits;
1285 for (i = 0; i < size; i++) {
1286 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1287 ret = add_hash_entry(new_hash, entry->ip);
1288 if (ret < 0)
1289 goto free_hash;
1290 }
1291 }
1292
1293 FTRACE_WARN_ON(new_hash->count != hash->count);
1294
1295 return new_hash;
1296
1297 free_hash:
1298 free_ftrace_hash(new_hash);
1299 return NULL;
1300}
1301
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001302static void
1303ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1304static void
1305ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1306
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001307static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001308ftrace_hash_move(struct ftrace_ops *ops, int enable,
1309 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310{
1311 struct ftrace_func_entry *entry;
1312 struct hlist_node *tp, *tn;
1313 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001314 struct ftrace_hash *old_hash;
1315 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001316 int size = src->count;
1317 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001318 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319 int i;
1320
1321 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001322 * Remove the current set, update the hash and add
1323 * them back.
1324 */
1325 ftrace_hash_rec_disable(ops, enable);
1326
1327 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 * If the new source is empty, just free dst and assign it
1329 * the empty_hash.
1330 */
1331 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001332 free_ftrace_hash_rcu(*dst);
1333 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b982011-11-04 20:32:39 -04001334 /* still need to update the function records */
1335 ret = 0;
1336 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 }
1338
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339 /*
1340 * Make the hash size about 1/2 the # found
1341 */
1342 for (size /= 2; size; size >>= 1)
1343 bits++;
1344
1345 /* Don't allocate too much */
1346 if (bits > FTRACE_HASH_MAX_BITS)
1347 bits = FTRACE_HASH_MAX_BITS;
1348
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001349 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001350 new_hash = alloc_ftrace_hash(bits);
1351 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001352 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001353
1354 size = 1 << src->size_bits;
1355 for (i = 0; i < size; i++) {
1356 hhd = &src->buckets[i];
1357 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001358 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001359 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360 }
1361 }
1362
Steven Rostedt07fd5512011-05-05 18:03:47 -04001363 old_hash = *dst;
1364 rcu_assign_pointer(*dst, new_hash);
1365 free_ftrace_hash_rcu(old_hash);
1366
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001367 ret = 0;
1368 out:
1369 /*
1370 * Enable regardless of ret:
1371 * On success, we enable the new hash.
1372 * On failure, we re-enable the original hash.
1373 */
1374 ftrace_hash_rec_enable(ops, enable);
1375
1376 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001377}
1378
Steven Rostedt265c8312009-02-13 12:43:56 -05001379/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001380 * Test the hashes for this ops to see if we want to call
1381 * the ops->func or not.
1382 *
1383 * It's a match if the ip is in the ops->filter_hash or
1384 * the filter_hash does not exist or is empty,
1385 * AND
1386 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001387 *
1388 * This needs to be called with preemption disabled as
1389 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001390 */
1391static int
1392ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1393{
1394 struct ftrace_hash *filter_hash;
1395 struct ftrace_hash *notrace_hash;
1396 int ret;
1397
Steven Rostedtb8489142011-05-04 09:27:52 -04001398 filter_hash = rcu_dereference_raw(ops->filter_hash);
1399 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1400
Steven Rostedt06a51d92011-12-19 19:07:36 -05001401 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001402 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001403 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001404 !ftrace_lookup_ip(notrace_hash, ip)))
1405 ret = 1;
1406 else
1407 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001408
1409 return ret;
1410}
1411
1412/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001413 * This is a double for. Do not use 'break' to break out of the loop,
1414 * you must use a goto.
1415 */
1416#define do_for_each_ftrace_rec(pg, rec) \
1417 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1418 int _____i; \
1419 for (_____i = 0; _____i < pg->index; _____i++) { \
1420 rec = &pg->records[_____i];
1421
1422#define while_for_each_ftrace_rec() \
1423 } \
1424 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301425
Steven Rostedt5855fea2011-12-16 19:27:42 -05001426
1427static int ftrace_cmp_recs(const void *a, const void *b)
1428{
Steven Rostedta650e022012-04-25 13:48:13 -04001429 const struct dyn_ftrace *key = a;
1430 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001431
Steven Rostedta650e022012-04-25 13:48:13 -04001432 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001433 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001434 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1435 return 1;
1436 return 0;
1437}
1438
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001439static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001440{
1441 struct ftrace_page *pg;
1442 struct dyn_ftrace *rec;
1443 struct dyn_ftrace key;
1444
1445 key.ip = start;
1446 key.flags = end; /* overload flags, as it is unsigned long */
1447
1448 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1449 if (end < pg->records[0].ip ||
1450 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1451 continue;
1452 rec = bsearch(&key, pg->records, pg->index,
1453 sizeof(struct dyn_ftrace),
1454 ftrace_cmp_recs);
1455 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001456 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001457 }
1458
Steven Rostedt5855fea2011-12-16 19:27:42 -05001459 return 0;
1460}
1461
Steven Rostedtc88fd862011-08-16 09:53:39 -04001462/**
1463 * ftrace_location - return true if the ip giving is a traced location
1464 * @ip: the instruction pointer to check
1465 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001466 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001467 * That is, the instruction that is either a NOP or call to
1468 * the function tracer. It checks the ftrace internal tables to
1469 * determine if the address belongs or not.
1470 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001471unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001472{
Steven Rostedta650e022012-04-25 13:48:13 -04001473 return ftrace_location_range(ip, ip);
1474}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001475
Steven Rostedta650e022012-04-25 13:48:13 -04001476/**
1477 * ftrace_text_reserved - return true if range contains an ftrace location
1478 * @start: start of range to search
1479 * @end: end of range to search (inclusive). @end points to the last byte to check.
1480 *
1481 * Returns 1 if @start and @end contains a ftrace location.
1482 * That is, the instruction that is either a NOP or call to
1483 * the function tracer. It checks the ftrace internal tables to
1484 * determine if the address belongs or not.
1485 */
1486int ftrace_text_reserved(void *start, void *end)
1487{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001488 unsigned long ret;
1489
1490 ret = ftrace_location_range((unsigned long)start,
1491 (unsigned long)end);
1492
1493 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001494}
1495
Steven Rostedted926f92011-05-03 13:25:24 -04001496static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1497 int filter_hash,
1498 bool inc)
1499{
1500 struct ftrace_hash *hash;
1501 struct ftrace_hash *other_hash;
1502 struct ftrace_page *pg;
1503 struct dyn_ftrace *rec;
1504 int count = 0;
1505 int all = 0;
1506
1507 /* Only update if the ops has been registered */
1508 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1509 return;
1510
1511 /*
1512 * In the filter_hash case:
1513 * If the count is zero, we update all records.
1514 * Otherwise we just update the items in the hash.
1515 *
1516 * In the notrace_hash case:
1517 * We enable the update in the hash.
1518 * As disabling notrace means enabling the tracing,
1519 * and enabling notrace means disabling, the inc variable
1520 * gets inversed.
1521 */
1522 if (filter_hash) {
1523 hash = ops->filter_hash;
1524 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001525 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001526 all = 1;
1527 } else {
1528 inc = !inc;
1529 hash = ops->notrace_hash;
1530 other_hash = ops->filter_hash;
1531 /*
1532 * If the notrace hash has no items,
1533 * then there's nothing to do.
1534 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001535 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001536 return;
1537 }
1538
1539 do_for_each_ftrace_rec(pg, rec) {
1540 int in_other_hash = 0;
1541 int in_hash = 0;
1542 int match = 0;
1543
1544 if (all) {
1545 /*
1546 * Only the filter_hash affects all records.
1547 * Update if the record is not in the notrace hash.
1548 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001549 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001550 match = 1;
1551 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001552 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1553 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001554
1555 /*
1556 *
1557 */
1558 if (filter_hash && in_hash && !in_other_hash)
1559 match = 1;
1560 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001561 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001562 match = 1;
1563 }
1564 if (!match)
1565 continue;
1566
1567 if (inc) {
1568 rec->flags++;
1569 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1570 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001571 /*
1572 * If any ops wants regs saved for this function
1573 * then all ops will get saved regs.
1574 */
1575 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1576 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001577 } else {
1578 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1579 return;
1580 rec->flags--;
1581 }
1582 count++;
1583 /* Shortcut, if we handled all records, we are done. */
1584 if (!all && count == hash->count)
1585 return;
1586 } while_for_each_ftrace_rec();
1587}
1588
1589static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1590 int filter_hash)
1591{
1592 __ftrace_hash_rec_update(ops, filter_hash, 0);
1593}
1594
1595static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1596 int filter_hash)
1597{
1598 __ftrace_hash_rec_update(ops, filter_hash, 1);
1599}
1600
Steven Rostedt05736a42008-09-22 14:55:47 -07001601static void print_ip_ins(const char *fmt, unsigned char *p)
1602{
1603 int i;
1604
1605 printk(KERN_CONT "%s", fmt);
1606
1607 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1608 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1609}
1610
Steven Rostedtc88fd862011-08-16 09:53:39 -04001611/**
1612 * ftrace_bug - report and shutdown function tracer
1613 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1614 * @ip: The address that failed
1615 *
1616 * The arch code that enables or disables the function tracing
1617 * can call ftrace_bug() when it has detected a problem in
1618 * modifying the code. @failed should be one of either:
1619 * EFAULT - if the problem happens on reading the @ip address
1620 * EINVAL - if what is read at @ip is not what was expected
1621 * EPERM - if the problem happens on writting to the @ip address
1622 */
1623void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001624{
1625 switch (failed) {
1626 case -EFAULT:
1627 FTRACE_WARN_ON_ONCE(1);
1628 pr_info("ftrace faulted on modifying ");
1629 print_ip_sym(ip);
1630 break;
1631 case -EINVAL:
1632 FTRACE_WARN_ON_ONCE(1);
1633 pr_info("ftrace failed to modify ");
1634 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001635 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001636 printk(KERN_CONT "\n");
1637 break;
1638 case -EPERM:
1639 FTRACE_WARN_ON_ONCE(1);
1640 pr_info("ftrace faulted on writing ");
1641 print_ip_sym(ip);
1642 break;
1643 default:
1644 FTRACE_WARN_ON_ONCE(1);
1645 pr_info("ftrace faulted on unknown error ");
1646 print_ip_sym(ip);
1647 }
1648}
1649
Steven Rostedtc88fd862011-08-16 09:53:39 -04001650static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001651{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001652 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001653
Steven Rostedt982c3502008-11-15 16:31:41 -05001654 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001655 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001656 *
Steven Rostedted926f92011-05-03 13:25:24 -04001657 * If the record has a ref count, then we need to enable it
1658 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001659 *
Steven Rostedted926f92011-05-03 13:25:24 -04001660 * Otherwise we make sure its disabled.
1661 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001662 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001663 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001664 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001665 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001666 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001667
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001668 /*
1669 * If enabling and the REGS flag does not match the REGS_EN, then
1670 * do not ignore this record. Set flags to fail the compare against
1671 * ENABLED.
1672 */
1673 if (flag &&
1674 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1675 flag |= FTRACE_FL_REGS;
1676
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001677 /* If the state of this record hasn't changed, then do nothing */
1678 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001679 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001680
1681 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001682 /* Save off if rec is being enabled (for return value) */
1683 flag ^= rec->flags & FTRACE_FL_ENABLED;
1684
1685 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001686 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001687 if (flag & FTRACE_FL_REGS) {
1688 if (rec->flags & FTRACE_FL_REGS)
1689 rec->flags |= FTRACE_FL_REGS_EN;
1690 else
1691 rec->flags &= ~FTRACE_FL_REGS_EN;
1692 }
1693 }
1694
1695 /*
1696 * If this record is being updated from a nop, then
1697 * return UPDATE_MAKE_CALL.
1698 * Otherwise, if the EN flag is set, then return
1699 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1700 * from the non-save regs, to a save regs function.
1701 * Otherwise,
1702 * return UPDATE_MODIFY_CALL to tell the caller to convert
1703 * from the save regs, to a non-save regs function.
1704 */
1705 if (flag & FTRACE_FL_ENABLED)
1706 return FTRACE_UPDATE_MAKE_CALL;
1707 else if (rec->flags & FTRACE_FL_REGS_EN)
1708 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1709 else
1710 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001711 }
1712
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001713 if (update) {
1714 /* If there's no more users, clear all flags */
1715 if (!(rec->flags & ~FTRACE_FL_MASK))
1716 rec->flags = 0;
1717 else
1718 /* Just disable the record (keep REGS state) */
1719 rec->flags &= ~FTRACE_FL_ENABLED;
1720 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001721
1722 return FTRACE_UPDATE_MAKE_NOP;
1723}
1724
1725/**
1726 * ftrace_update_record, set a record that now is tracing or not
1727 * @rec: the record to update
1728 * @enable: set to 1 if the record is tracing, zero to force disable
1729 *
1730 * The records that represent all functions that can be traced need
1731 * to be updated when tracing has been enabled.
1732 */
1733int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1734{
1735 return ftrace_check_record(rec, enable, 1);
1736}
1737
1738/**
1739 * ftrace_test_record, check if the record has been enabled or not
1740 * @rec: the record to test
1741 * @enable: set to 1 to check if enabled, 0 if it is disabled
1742 *
1743 * The arch code may need to test if a record is already set to
1744 * tracing to determine how to modify the function code that it
1745 * represents.
1746 */
1747int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1748{
1749 return ftrace_check_record(rec, enable, 0);
1750}
1751
1752static int
1753__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1754{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001755 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001756 unsigned long ftrace_addr;
1757 int ret;
1758
Steven Rostedtc88fd862011-08-16 09:53:39 -04001759 ret = ftrace_update_record(rec, enable);
1760
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001761 if (rec->flags & FTRACE_FL_REGS)
1762 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1763 else
1764 ftrace_addr = (unsigned long)FTRACE_ADDR;
1765
Steven Rostedtc88fd862011-08-16 09:53:39 -04001766 switch (ret) {
1767 case FTRACE_UPDATE_IGNORE:
1768 return 0;
1769
1770 case FTRACE_UPDATE_MAKE_CALL:
1771 return ftrace_make_call(rec, ftrace_addr);
1772
1773 case FTRACE_UPDATE_MAKE_NOP:
1774 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001775
1776 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1777 case FTRACE_UPDATE_MODIFY_CALL:
1778 if (rec->flags & FTRACE_FL_REGS)
1779 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1780 else
1781 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1782
1783 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001784 }
1785
1786 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001787}
1788
Steven Rostedte4f5d542012-04-27 09:13:18 -04001789void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001790{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001791 struct dyn_ftrace *rec;
1792 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001793 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001794
Steven Rostedt45a4a232011-04-21 23:16:46 -04001795 if (unlikely(ftrace_disabled))
1796 return;
1797
Steven Rostedt265c8312009-02-13 12:43:56 -05001798 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001799 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001800 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001801 ftrace_bug(failed, rec->ip);
1802 /* Stop processing */
1803 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001804 }
1805 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001806}
1807
Steven Rostedtc88fd862011-08-16 09:53:39 -04001808struct ftrace_rec_iter {
1809 struct ftrace_page *pg;
1810 int index;
1811};
1812
1813/**
1814 * ftrace_rec_iter_start, start up iterating over traced functions
1815 *
1816 * Returns an iterator handle that is used to iterate over all
1817 * the records that represent address locations where functions
1818 * are traced.
1819 *
1820 * May return NULL if no records are available.
1821 */
1822struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1823{
1824 /*
1825 * We only use a single iterator.
1826 * Protected by the ftrace_lock mutex.
1827 */
1828 static struct ftrace_rec_iter ftrace_rec_iter;
1829 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1830
1831 iter->pg = ftrace_pages_start;
1832 iter->index = 0;
1833
1834 /* Could have empty pages */
1835 while (iter->pg && !iter->pg->index)
1836 iter->pg = iter->pg->next;
1837
1838 if (!iter->pg)
1839 return NULL;
1840
1841 return iter;
1842}
1843
1844/**
1845 * ftrace_rec_iter_next, get the next record to process.
1846 * @iter: The handle to the iterator.
1847 *
1848 * Returns the next iterator after the given iterator @iter.
1849 */
1850struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1851{
1852 iter->index++;
1853
1854 if (iter->index >= iter->pg->index) {
1855 iter->pg = iter->pg->next;
1856 iter->index = 0;
1857
1858 /* Could have empty pages */
1859 while (iter->pg && !iter->pg->index)
1860 iter->pg = iter->pg->next;
1861 }
1862
1863 if (!iter->pg)
1864 return NULL;
1865
1866 return iter;
1867}
1868
1869/**
1870 * ftrace_rec_iter_record, get the record at the iterator location
1871 * @iter: The current iterator location
1872 *
1873 * Returns the record that the current @iter is at.
1874 */
1875struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1876{
1877 return &iter->pg->records[iter->index];
1878}
1879
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301880static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001881ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001882{
1883 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001884 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001885
1886 ip = rec->ip;
1887
Steven Rostedt45a4a232011-04-21 23:16:46 -04001888 if (unlikely(ftrace_disabled))
1889 return 0;
1890
Shaohua Li25aac9d2009-01-09 11:29:40 +08001891 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001892 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001893 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301894 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001895 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301896 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001897}
1898
Steven Rostedt000ab692009-02-17 13:35:06 -05001899/*
1900 * archs can override this function if they must do something
1901 * before the modifying code is performed.
1902 */
1903int __weak ftrace_arch_code_modify_prepare(void)
1904{
1905 return 0;
1906}
1907
1908/*
1909 * archs can override this function if they must do something
1910 * after the modifying code is performed.
1911 */
1912int __weak ftrace_arch_code_modify_post_process(void)
1913{
1914 return 0;
1915}
1916
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001917void ftrace_modify_all_code(int command)
1918{
1919 if (command & FTRACE_UPDATE_CALLS)
1920 ftrace_replace_code(1);
1921 else if (command & FTRACE_DISABLE_CALLS)
1922 ftrace_replace_code(0);
1923
1924 if (command & FTRACE_UPDATE_TRACE_FUNC)
1925 ftrace_update_ftrace_func(ftrace_trace_function);
1926
1927 if (command & FTRACE_START_FUNC_RET)
1928 ftrace_enable_ftrace_graph_caller();
1929 else if (command & FTRACE_STOP_FUNC_RET)
1930 ftrace_disable_ftrace_graph_caller();
1931}
1932
Ingo Molnare309b412008-05-12 21:20:51 +02001933static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001934{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001935 int *command = data;
1936
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001937 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001938
Steven Rostedtc88fd862011-08-16 09:53:39 -04001939 return 0;
1940}
1941
1942/**
1943 * ftrace_run_stop_machine, go back to the stop machine method
1944 * @command: The command to tell ftrace what to do
1945 *
1946 * If an arch needs to fall back to the stop machine method, the
1947 * it can call this function.
1948 */
1949void ftrace_run_stop_machine(int command)
1950{
1951 stop_machine(__ftrace_modify_code, &command, NULL);
1952}
1953
1954/**
1955 * arch_ftrace_update_code, modify the code to trace or not trace
1956 * @command: The command that needs to be done
1957 *
1958 * Archs can override this function if it does not need to
1959 * run stop_machine() to modify code.
1960 */
1961void __weak arch_ftrace_update_code(int command)
1962{
1963 ftrace_run_stop_machine(command);
1964}
1965
1966static void ftrace_run_update_code(int command)
1967{
1968 int ret;
1969
1970 ret = ftrace_arch_code_modify_prepare();
1971 FTRACE_WARN_ON(ret);
1972 if (ret)
1973 return;
1974 /*
1975 * Do not call function tracer while we update the code.
1976 * We are in stop machine.
1977 */
1978 function_trace_stop++;
1979
1980 /*
1981 * By default we use stop_machine() to modify the code.
1982 * But archs can do what ever they want as long as it
1983 * is safe. The stop_machine() is the safest, but also
1984 * produces the most overhead.
1985 */
1986 arch_ftrace_update_code(command);
1987
Steven Rostedt6331c282011-07-13 15:11:02 -04001988 function_trace_stop--;
1989
Steven Rostedt000ab692009-02-17 13:35:06 -05001990 ret = ftrace_arch_code_modify_post_process();
1991 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001992}
1993
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001994static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001995static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001996static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001997
1998static void ftrace_startup_enable(int command)
1999{
2000 if (saved_ftrace_func != ftrace_trace_function) {
2001 saved_ftrace_func = ftrace_trace_function;
2002 command |= FTRACE_UPDATE_TRACE_FUNC;
2003 }
2004
2005 if (!command || !ftrace_enabled)
2006 return;
2007
2008 ftrace_run_update_code(command);
2009}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002010
Steven Rostedta1cd6172011-05-23 15:24:25 -04002011static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002012{
Steven Rostedtb8489142011-05-04 09:27:52 -04002013 bool hash_enable = true;
2014
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002015 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002016 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002017
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002018 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002019 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002020
Steven Rostedtb8489142011-05-04 09:27:52 -04002021 /* ops marked global share the filter hashes */
2022 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2023 ops = &global_ops;
2024 /* Don't update hash if global is already set */
2025 if (global_start_up)
2026 hash_enable = false;
2027 global_start_up++;
2028 }
2029
Steven Rostedted926f92011-05-03 13:25:24 -04002030 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002031 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002032 ftrace_hash_rec_enable(ops, 1);
2033
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002034 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002035
2036 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002037}
2038
Steven Rostedtbd69c302011-05-03 21:55:54 -04002039static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002040{
Steven Rostedtb8489142011-05-04 09:27:52 -04002041 bool hash_disable = true;
2042
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002043 if (unlikely(ftrace_disabled))
2044 return;
2045
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002046 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002047 /*
2048 * Just warn in case of unbalance, no need to kill ftrace, it's not
2049 * critical but the ftrace_call callers may be never nopped again after
2050 * further ftrace uses.
2051 */
2052 WARN_ON_ONCE(ftrace_start_up < 0);
2053
Steven Rostedtb8489142011-05-04 09:27:52 -04002054 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2055 ops = &global_ops;
2056 global_start_up--;
2057 WARN_ON_ONCE(global_start_up < 0);
2058 /* Don't update hash if global still has users */
2059 if (global_start_up) {
2060 WARN_ON_ONCE(!ftrace_start_up);
2061 hash_disable = false;
2062 }
2063 }
2064
2065 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002066 ftrace_hash_rec_disable(ops, 1);
2067
Steven Rostedtb8489142011-05-04 09:27:52 -04002068 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002069 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002070
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002071 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002072
2073 if (saved_ftrace_func != ftrace_trace_function) {
2074 saved_ftrace_func = ftrace_trace_function;
2075 command |= FTRACE_UPDATE_TRACE_FUNC;
2076 }
2077
2078 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002079 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002080
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002081 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002082}
2083
Ingo Molnare309b412008-05-12 21:20:51 +02002084static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002085{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002086 if (unlikely(ftrace_disabled))
2087 return;
2088
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002089 /* Force update next time */
2090 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002091 /* ftrace_start_up is true if we want ftrace running */
2092 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002093 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002094}
2095
Ingo Molnare309b412008-05-12 21:20:51 +02002096static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002097{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002098 if (unlikely(ftrace_disabled))
2099 return;
2100
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002101 /* ftrace_start_up is true if ftrace is running */
2102 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002103 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002104}
2105
Steven Rostedt3d083392008-05-12 21:20:42 +02002106static cycle_t ftrace_update_time;
2107static unsigned long ftrace_update_cnt;
2108unsigned long ftrace_update_tot_cnt;
2109
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002110static int ops_traces_mod(struct ftrace_ops *ops)
2111{
2112 struct ftrace_hash *hash;
2113
2114 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002115 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002116}
2117
Steven Rostedt31e88902008-11-14 16:21:19 -08002118static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002119{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002120 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002121 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302122 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002123 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002124 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002125
2126 /*
2127 * When adding a module, we need to check if tracers are
2128 * currently enabled and if they are set to trace all functions.
2129 * If they are, we need to enable the module functions as well
2130 * as update the reference counts for those function records.
2131 */
2132 if (mod) {
2133 struct ftrace_ops *ops;
2134
2135 for (ops = ftrace_ops_list;
2136 ops != &ftrace_list_end; ops = ops->next) {
2137 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2138 ops_traces_mod(ops))
2139 ref++;
2140 }
2141 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002142
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002143 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002144 ftrace_update_cnt = 0;
2145
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002146 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302147
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002148 for (i = 0; i < pg->index; i++) {
2149 /* If something went wrong, bail without enabling anything */
2150 if (unlikely(ftrace_disabled))
2151 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002152
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002153 p = &pg->records[i];
2154 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302155
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002156 /*
2157 * Do the initial record conversion from mcount jump
2158 * to the NOP instructions.
2159 */
2160 if (!ftrace_code_disable(mod, p))
2161 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002162
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002163 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002164
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002165 /*
2166 * If the tracing is enabled, go ahead and enable the record.
2167 *
2168 * The reason not to enable the record immediatelly is the
2169 * inherent check of ftrace_make_nop/ftrace_make_call for
2170 * correct previous instructions. Making first the NOP
2171 * conversion puts the module to the correct state, thus
2172 * passing the ftrace_make_call check.
2173 */
2174 if (ftrace_start_up && ref) {
2175 int failed = __ftrace_replace_code(p, 1);
2176 if (failed)
2177 ftrace_bug(failed, p->ip);
2178 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002179 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002180 }
2181
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002182 ftrace_new_pgs = NULL;
2183
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002184 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002185 ftrace_update_time = stop - start;
2186 ftrace_update_tot_cnt += ftrace_update_cnt;
2187
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002188 return 0;
2189}
2190
Steven Rostedta7900872011-12-16 16:23:44 -05002191static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002192{
Steven Rostedta7900872011-12-16 16:23:44 -05002193 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002194 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002195
Steven Rostedta7900872011-12-16 16:23:44 -05002196 if (WARN_ON(!count))
2197 return -EINVAL;
2198
2199 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002200
2201 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002202 * We want to fill as much as possible. No more than a page
2203 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002204 */
Steven Rostedta7900872011-12-16 16:23:44 -05002205 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2206 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002207
Steven Rostedta7900872011-12-16 16:23:44 -05002208 again:
2209 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2210
2211 if (!pg->records) {
2212 /* if we can't allocate this size, try something smaller */
2213 if (!order)
2214 return -ENOMEM;
2215 order >>= 1;
2216 goto again;
2217 }
2218
2219 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2220 pg->size = cnt;
2221
2222 if (cnt > count)
2223 cnt = count;
2224
2225 return cnt;
2226}
2227
2228static struct ftrace_page *
2229ftrace_allocate_pages(unsigned long num_to_init)
2230{
2231 struct ftrace_page *start_pg;
2232 struct ftrace_page *pg;
2233 int order;
2234 int cnt;
2235
2236 if (!num_to_init)
2237 return 0;
2238
2239 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2240 if (!pg)
2241 return NULL;
2242
2243 /*
2244 * Try to allocate as much as possible in one continues
2245 * location that fills in all of the space. We want to
2246 * waste as little space as possible.
2247 */
2248 for (;;) {
2249 cnt = ftrace_allocate_records(pg, num_to_init);
2250 if (cnt < 0)
2251 goto free_pages;
2252
2253 num_to_init -= cnt;
2254 if (!num_to_init)
2255 break;
2256
2257 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2258 if (!pg->next)
2259 goto free_pages;
2260
2261 pg = pg->next;
2262 }
2263
2264 return start_pg;
2265
2266 free_pages:
2267 while (start_pg) {
2268 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2269 free_pages((unsigned long)pg->records, order);
2270 start_pg = pg->next;
2271 kfree(pg);
2272 pg = start_pg;
2273 }
2274 pr_info("ftrace: FAILED to allocate memory for functions\n");
2275 return NULL;
2276}
2277
2278static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2279{
2280 int cnt;
2281
2282 if (!num_to_init) {
2283 pr_info("ftrace: No functions to be traced?\n");
2284 return -1;
2285 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002286
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002287 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002288 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002289 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002290
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002291 return 0;
2292}
2293
Steven Rostedt5072c592008-05-12 21:20:43 +02002294#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2295
2296struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002297 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002298 loff_t func_pos;
2299 struct ftrace_page *pg;
2300 struct dyn_ftrace *func;
2301 struct ftrace_func_probe *probe;
2302 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002303 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002304 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002305 int hidx;
2306 int idx;
2307 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002308};
2309
Ingo Molnare309b412008-05-12 21:20:51 +02002310static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002311t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002312{
2313 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002314 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002315 struct hlist_head *hhd;
2316
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002317 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002318 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002319
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002320 if (iter->probe)
2321 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002322 retry:
2323 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2324 return NULL;
2325
2326 hhd = &ftrace_func_hash[iter->hidx];
2327
2328 if (hlist_empty(hhd)) {
2329 iter->hidx++;
2330 hnd = NULL;
2331 goto retry;
2332 }
2333
2334 if (!hnd)
2335 hnd = hhd->first;
2336 else {
2337 hnd = hnd->next;
2338 if (!hnd) {
2339 iter->hidx++;
2340 goto retry;
2341 }
2342 }
2343
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002344 if (WARN_ON_ONCE(!hnd))
2345 return NULL;
2346
2347 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2348
2349 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002350}
2351
2352static void *t_hash_start(struct seq_file *m, loff_t *pos)
2353{
2354 struct ftrace_iterator *iter = m->private;
2355 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002356 loff_t l;
2357
Steven Rostedt69a30832011-12-19 15:21:16 -05002358 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2359 return NULL;
2360
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002361 if (iter->func_pos > *pos)
2362 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002363
Li Zefand82d6242009-06-24 09:54:54 +08002364 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002365 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002366 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002367 if (!p)
2368 break;
2369 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002370 if (!p)
2371 return NULL;
2372
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002373 /* Only set this if we have an item */
2374 iter->flags |= FTRACE_ITER_HASH;
2375
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002377}
2378
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002379static int
2380t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002381{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002382 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002383
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002384 rec = iter->probe;
2385 if (WARN_ON_ONCE(!rec))
2386 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002387
Steven Rostedt809dcf22009-02-16 23:06:01 -05002388 if (rec->ops->print)
2389 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2390
Steven Rostedtb375a112009-09-17 00:05:58 -04002391 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002392
2393 if (rec->data)
2394 seq_printf(m, ":%p", rec->data);
2395 seq_putc(m, '\n');
2396
2397 return 0;
2398}
2399
2400static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002401t_next(struct seq_file *m, void *v, loff_t *pos)
2402{
2403 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002404 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002405 struct dyn_ftrace *rec = NULL;
2406
Steven Rostedt45a4a232011-04-21 23:16:46 -04002407 if (unlikely(ftrace_disabled))
2408 return NULL;
2409
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002410 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002411 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002412
Steven Rostedt5072c592008-05-12 21:20:43 +02002413 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002414 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002415
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002416 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002417 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002418
Steven Rostedt5072c592008-05-12 21:20:43 +02002419 retry:
2420 if (iter->idx >= iter->pg->index) {
2421 if (iter->pg->next) {
2422 iter->pg = iter->pg->next;
2423 iter->idx = 0;
2424 goto retry;
2425 }
2426 } else {
2427 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002428 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002429 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002430
Steven Rostedt41c52c02008-05-22 11:46:33 -04002431 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002432 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2433
2434 ((iter->flags & FTRACE_ITER_ENABLED) &&
2435 !(rec->flags & ~FTRACE_FL_MASK))) {
2436
Steven Rostedt5072c592008-05-12 21:20:43 +02002437 rec = NULL;
2438 goto retry;
2439 }
2440 }
2441
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002442 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002443 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002444
2445 iter->func = rec;
2446
2447 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002448}
2449
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002450static void reset_iter_read(struct ftrace_iterator *iter)
2451{
2452 iter->pos = 0;
2453 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002454 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002455}
2456
2457static void *t_start(struct seq_file *m, loff_t *pos)
2458{
2459 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002460 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002461 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002462 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002463
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002464 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002465
2466 if (unlikely(ftrace_disabled))
2467 return NULL;
2468
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002469 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002470 * If an lseek was done, then reset and start from beginning.
2471 */
2472 if (*pos < iter->pos)
2473 reset_iter_read(iter);
2474
2475 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002476 * For set_ftrace_filter reading, if we have the filter
2477 * off, we can short cut and just print out that all
2478 * functions are enabled.
2479 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002480 if (iter->flags & FTRACE_ITER_FILTER &&
2481 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002482 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002483 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002484 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002485 /* reset in case of seek/pread */
2486 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002487 return iter;
2488 }
2489
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002490 if (iter->flags & FTRACE_ITER_HASH)
2491 return t_hash_start(m, pos);
2492
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002493 /*
2494 * Unfortunately, we need to restart at ftrace_pages_start
2495 * every time we let go of the ftrace_mutex. This is because
2496 * those pointers can change without the lock.
2497 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002498 iter->pg = ftrace_pages_start;
2499 iter->idx = 0;
2500 for (l = 0; l <= *pos; ) {
2501 p = t_next(m, p, &l);
2502 if (!p)
2503 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002504 }
walimis5821e1b2008-11-15 15:19:06 +08002505
Steven Rostedt69a30832011-12-19 15:21:16 -05002506 if (!p)
2507 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002508
2509 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002510}
2511
2512static void t_stop(struct seq_file *m, void *p)
2513{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002514 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002515}
2516
2517static int t_show(struct seq_file *m, void *v)
2518{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002519 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002520 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002521
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002522 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002523 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002524
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002525 if (iter->flags & FTRACE_ITER_PRINTALL) {
2526 seq_printf(m, "#### all functions enabled ####\n");
2527 return 0;
2528 }
2529
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002530 rec = iter->func;
2531
Steven Rostedt5072c592008-05-12 21:20:43 +02002532 if (!rec)
2533 return 0;
2534
Steven Rostedt647bcd02011-05-03 14:39:21 -04002535 seq_printf(m, "%ps", (void *)rec->ip);
2536 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002537 seq_printf(m, " (%ld)%s",
2538 rec->flags & ~FTRACE_FL_MASK,
2539 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002540 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002541
2542 return 0;
2543}
2544
James Morris88e9d342009-09-22 16:43:43 -07002545static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002546 .start = t_start,
2547 .next = t_next,
2548 .stop = t_stop,
2549 .show = t_show,
2550};
2551
Ingo Molnare309b412008-05-12 21:20:51 +02002552static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002553ftrace_avail_open(struct inode *inode, struct file *file)
2554{
2555 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002556
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002557 if (unlikely(ftrace_disabled))
2558 return -ENODEV;
2559
Jiri Olsa50e18b92012-04-25 10:23:39 +02002560 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2561 if (iter) {
2562 iter->pg = ftrace_pages_start;
2563 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002564 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002565
Jiri Olsa50e18b92012-04-25 10:23:39 +02002566 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002567}
2568
Steven Rostedt647bcd02011-05-03 14:39:21 -04002569static int
2570ftrace_enabled_open(struct inode *inode, struct file *file)
2571{
2572 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002573
2574 if (unlikely(ftrace_disabled))
2575 return -ENODEV;
2576
Jiri Olsa50e18b92012-04-25 10:23:39 +02002577 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2578 if (iter) {
2579 iter->pg = ftrace_pages_start;
2580 iter->flags = FTRACE_ITER_ENABLED;
2581 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002582 }
2583
Jiri Olsa50e18b92012-04-25 10:23:39 +02002584 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002585}
2586
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002587static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002588{
Steven Rostedt52baf112009-02-14 01:15:39 -05002589 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002590 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002591 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002592}
2593
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002594/**
2595 * ftrace_regex_open - initialize function tracer filter files
2596 * @ops: The ftrace_ops that hold the hash filters
2597 * @flag: The type of filter to process
2598 * @inode: The inode, usually passed in to your open routine
2599 * @file: The file, usually passed in to your open routine
2600 *
2601 * ftrace_regex_open() initializes the filter files for the
2602 * @ops. Depending on @flag it may process the filter hash or
2603 * the notrace hash of @ops. With this called from the open
2604 * routine, you can use ftrace_filter_write() for the write
2605 * routine if @flag has FTRACE_ITER_FILTER set, or
2606 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2607 * ftrace_regex_lseek() should be used as the lseek routine, and
2608 * release must call ftrace_regex_release().
2609 */
2610int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002611ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002612 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002613{
2614 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002615 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002616 int ret = 0;
2617
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002618 if (unlikely(ftrace_disabled))
2619 return -ENODEV;
2620
Steven Rostedt5072c592008-05-12 21:20:43 +02002621 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2622 if (!iter)
2623 return -ENOMEM;
2624
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002625 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2626 kfree(iter);
2627 return -ENOMEM;
2628 }
2629
Steven Rostedtf45948e2011-05-02 12:29:25 -04002630 if (flag & FTRACE_ITER_NOTRACE)
2631 hash = ops->notrace_hash;
2632 else
2633 hash = ops->filter_hash;
2634
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002635 iter->ops = ops;
2636 iter->flags = flag;
2637
2638 if (file->f_mode & FMODE_WRITE) {
2639 mutex_lock(&ftrace_lock);
2640 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2641 mutex_unlock(&ftrace_lock);
2642
2643 if (!iter->hash) {
2644 trace_parser_put(&iter->parser);
2645 kfree(iter);
2646 return -ENOMEM;
2647 }
2648 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002649
Steven Rostedt41c52c02008-05-22 11:46:33 -04002650 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002651
Steven Rostedt5072c592008-05-12 21:20:43 +02002652 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002653 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002654 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002655
2656 if (file->f_mode & FMODE_READ) {
2657 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002658
2659 ret = seq_open(file, &show_ftrace_seq_ops);
2660 if (!ret) {
2661 struct seq_file *m = file->private_data;
2662 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002663 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002664 /* Failed */
2665 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002666 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002667 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002668 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002669 } else
2670 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002671 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002672
2673 return ret;
2674}
2675
Steven Rostedt41c52c02008-05-22 11:46:33 -04002676static int
2677ftrace_filter_open(struct inode *inode, struct file *file)
2678{
Steven Rostedt69a30832011-12-19 15:21:16 -05002679 return ftrace_regex_open(&global_ops,
2680 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2681 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002682}
2683
2684static int
2685ftrace_notrace_open(struct inode *inode, struct file *file)
2686{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002687 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002688 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002689}
2690
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002691loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -08002692ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
Steven Rostedt5072c592008-05-12 21:20:43 +02002693{
2694 loff_t ret;
2695
2696 if (file->f_mode & FMODE_READ)
Andrew Morton965c8e52012-12-17 15:59:39 -08002697 ret = seq_lseek(file, offset, whence);
Steven Rostedt5072c592008-05-12 21:20:43 +02002698 else
2699 file->f_pos = ret = 1;
2700
2701 return ret;
2702}
2703
Steven Rostedt64e7c442009-02-13 17:08:48 -05002704static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002705{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002706 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002707 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002708
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002709 switch (type) {
2710 case MATCH_FULL:
2711 if (strcmp(str, regex) == 0)
2712 matched = 1;
2713 break;
2714 case MATCH_FRONT_ONLY:
2715 if (strncmp(str, regex, len) == 0)
2716 matched = 1;
2717 break;
2718 case MATCH_MIDDLE_ONLY:
2719 if (strstr(str, regex))
2720 matched = 1;
2721 break;
2722 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002723 slen = strlen(str);
2724 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002725 matched = 1;
2726 break;
2727 }
2728
2729 return matched;
2730}
2731
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002732static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002733enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002734{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002735 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002736 int ret = 0;
2737
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002738 entry = ftrace_lookup_ip(hash, rec->ip);
2739 if (not) {
2740 /* Do nothing if it doesn't exist */
2741 if (!entry)
2742 return 0;
2743
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002744 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002745 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002746 /* Do nothing if it exists */
2747 if (entry)
2748 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002749
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002750 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002751 }
2752 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002753}
2754
Steven Rostedt64e7c442009-02-13 17:08:48 -05002755static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002756ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2757 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002758{
2759 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002760 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002761
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002762 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2763
2764 if (mod) {
2765 /* module lookup requires matching the module */
2766 if (!modname || strcmp(modname, mod))
2767 return 0;
2768
2769 /* blank search means to match all funcs in the mod */
2770 if (!len)
2771 return 1;
2772 }
2773
Steven Rostedt64e7c442009-02-13 17:08:48 -05002774 return ftrace_match(str, regex, len, type);
2775}
2776
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002777static int
2778match_records(struct ftrace_hash *hash, char *buff,
2779 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002780{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002781 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002782 struct ftrace_page *pg;
2783 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002784 int type = MATCH_FULL;
2785 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002786 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002787 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002788
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002789 if (len) {
2790 type = filter_parse_regex(buff, len, &search, &not);
2791 search_len = strlen(search);
2792 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002793
Steven Rostedt52baf112009-02-14 01:15:39 -05002794 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002795
2796 if (unlikely(ftrace_disabled))
2797 goto out_unlock;
2798
Steven Rostedt265c8312009-02-13 12:43:56 -05002799 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002800 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002801 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002802 if (ret < 0) {
2803 found = ret;
2804 goto out_unlock;
2805 }
Li Zefan311d16d2009-12-08 11:15:11 +08002806 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002807 }
2808 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002809 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002810 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002811
2812 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002813}
2814
Steven Rostedt64e7c442009-02-13 17:08:48 -05002815static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002816ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002817{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002818 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002819}
2820
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002821static int
2822ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002823{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002824 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002825
Steven Rostedt64e7c442009-02-13 17:08:48 -05002826 /* blank or '*' mean the same */
2827 if (strcmp(buff, "*") == 0)
2828 buff[0] = 0;
2829
2830 /* handle the case of 'dont filter this module' */
2831 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2832 buff[0] = 0;
2833 not = 1;
2834 }
2835
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002836 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002837}
2838
Steven Rostedtf6180772009-02-14 00:40:25 -05002839/*
2840 * We register the module command as a template to show others how
2841 * to register the a command as well.
2842 */
2843
2844static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002845ftrace_mod_callback(struct ftrace_hash *hash,
2846 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002847{
2848 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002849 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002850
2851 /*
2852 * cmd == 'mod' because we only registered this func
2853 * for the 'mod' ftrace_func_command.
2854 * But if you register one func with multiple commands,
2855 * you can tell which command was used by the cmd
2856 * parameter.
2857 */
2858
2859 /* we must have a module name */
2860 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002861 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002862
2863 mod = strsep(&param, ":");
2864 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002865 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002866
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002867 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002868 if (!ret)
2869 ret = -EINVAL;
2870 if (ret < 0)
2871 return ret;
2872
2873 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002874}
2875
2876static struct ftrace_func_command ftrace_mod_cmd = {
2877 .name = "mod",
2878 .func = ftrace_mod_callback,
2879};
2880
2881static int __init ftrace_mod_cmd_init(void)
2882{
2883 return register_ftrace_command(&ftrace_mod_cmd);
2884}
Steven Rostedt6f415672012-10-05 12:13:07 -04002885core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002886
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002887static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002888 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002890 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002891 struct hlist_head *hhd;
2892 struct hlist_node *n;
2893 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002894
2895 key = hash_long(ip, FTRACE_HASH_BITS);
2896
2897 hhd = &ftrace_func_hash[key];
2898
2899 if (hlist_empty(hhd))
2900 return;
2901
2902 /*
2903 * Disable preemption for these calls to prevent a RCU grace
2904 * period. This syncs the hash iteration and freeing of items
2905 * on the hash. rcu_read_lock is too dangerous here.
2906 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002907 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2909 if (entry->ip == ip)
2910 entry->ops->func(ip, parent_ip, &entry->data);
2911 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002912 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913}
2914
Steven Rostedtb6887d72009-02-17 12:32:04 -05002915static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002917 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002918};
2919
Steven Rostedtb6887d72009-02-17 12:32:04 -05002920static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002921
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923{
Steven Rostedtb8489142011-05-04 09:27:52 -04002924 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925 int i;
2926
Steven Rostedtb6887d72009-02-17 12:32:04 -05002927 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928 return;
2929
2930 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2931 struct hlist_head *hhd = &ftrace_func_hash[i];
2932 if (hhd->first)
2933 break;
2934 }
2935 /* Nothing registered? */
2936 if (i == FTRACE_FUNC_HASHSIZE)
2937 return;
2938
Steven Rostedtb8489142011-05-04 09:27:52 -04002939 ret = __register_ftrace_function(&trace_probe_ops);
2940 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002941 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002942
Steven Rostedtb6887d72009-02-17 12:32:04 -05002943 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002944}
2945
Steven Rostedtb6887d72009-02-17 12:32:04 -05002946static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947{
Steven Rostedtb8489142011-05-04 09:27:52 -04002948 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002949 int i;
2950
Steven Rostedtb6887d72009-02-17 12:32:04 -05002951 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952 return;
2953
2954 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2955 struct hlist_head *hhd = &ftrace_func_hash[i];
2956 if (hhd->first)
2957 return;
2958 }
2959
2960 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002961 ret = __unregister_ftrace_function(&trace_probe_ops);
2962 if (!ret)
2963 ftrace_shutdown(&trace_probe_ops, 0);
2964
Steven Rostedtb6887d72009-02-17 12:32:04 -05002965 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966}
2967
2968
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04002969static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002970{
Steven Rostedt59df055f2009-02-14 15:29:06 -05002971 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04002972 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973 kfree(entry);
2974}
2975
Steven Rostedt59df055f2009-02-14 15:29:06 -05002976int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002977register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002978 void *data)
2979{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002980 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002981 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
2982 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002983 struct ftrace_page *pg;
2984 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002985 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002986 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002987 int count = 0;
2988 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002989 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002990
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002991 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002992 len = strlen(search);
2993
Steven Rostedtb6887d72009-02-17 12:32:04 -05002994 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002995 if (WARN_ON(not))
2996 return -EINVAL;
2997
2998 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002999
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003000 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3001 if (!hash) {
3002 count = -ENOMEM;
Steven Rostedt45a4a232011-04-21 23:16:46 -04003003 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003004 }
3005
3006 if (unlikely(ftrace_disabled)) {
3007 count = -ENODEV;
3008 goto out_unlock;
3009 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003010
Steven Rostedt59df055f2009-02-14 15:29:06 -05003011 do_for_each_ftrace_rec(pg, rec) {
3012
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003013 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003014 continue;
3015
3016 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3017 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003018 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003019 if (!count)
3020 count = -ENOMEM;
3021 goto out_unlock;
3022 }
3023
3024 count++;
3025
3026 entry->data = data;
3027
3028 /*
3029 * The caller might want to do something special
3030 * for each function we find. We call the callback
3031 * to give the caller an opportunity to do so.
3032 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003033 if (ops->init) {
3034 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035 /* caller does not like this func */
3036 kfree(entry);
3037 continue;
3038 }
3039 }
3040
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003041 ret = enter_record(hash, rec, 0);
3042 if (ret < 0) {
3043 kfree(entry);
3044 count = ret;
3045 goto out_unlock;
3046 }
3047
Steven Rostedt59df055f2009-02-14 15:29:06 -05003048 entry->ops = ops;
3049 entry->ip = rec->ip;
3050
3051 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3052 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3053
3054 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003055
3056 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3057 if (ret < 0)
3058 count = ret;
3059
Steven Rostedtb6887d72009-02-17 12:32:04 -05003060 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061
3062 out_unlock:
3063 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003064 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065
3066 return count;
3067}
3068
3069enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003070 PROBE_TEST_FUNC = 1,
3071 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003072};
3073
3074static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003075__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003076 void *data, int flags)
3077{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003078 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003079 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003080 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003081 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003082 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003083 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003084 struct hlist_node *n, *tmp;
3085 char str[KSYM_SYMBOL_LEN];
3086 int type = MATCH_FULL;
3087 int i, len = 0;
3088 char *search;
3089
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003090 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003091 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003092 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093 int not;
3094
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003095 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096 len = strlen(search);
3097
Steven Rostedtb6887d72009-02-17 12:32:04 -05003098 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003099 if (WARN_ON(not))
3100 return;
3101 }
3102
3103 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003104
3105 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3106 if (!hash)
3107 /* Hmm, should report this somehow */
3108 goto out_unlock;
3109
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003110 INIT_LIST_HEAD(&free_list);
3111
Steven Rostedt59df055f2009-02-14 15:29:06 -05003112 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3113 struct hlist_head *hhd = &ftrace_func_hash[i];
3114
3115 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3116
3117 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003118 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003119 continue;
3120
Steven Rostedtb6887d72009-02-17 12:32:04 -05003121 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003122 continue;
3123
3124 /* do this last, since it is the most expensive */
3125 if (glob) {
3126 kallsyms_lookup(entry->ip, NULL, NULL,
3127 NULL, str);
3128 if (!ftrace_match(str, glob, len, type))
3129 continue;
3130 }
3131
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003132 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3133 /* It is possible more than one entry had this ip */
3134 if (rec_entry)
3135 free_hash_entry(hash, rec_entry);
3136
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003137 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003138 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003139 }
3140 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003141 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003142 /*
3143 * Remove after the disable is called. Otherwise, if the last
3144 * probe is removed, a null hash means *all enabled*.
3145 */
3146 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003147 synchronize_sched();
3148 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3149 list_del(&entry->free_list);
3150 ftrace_free_entry(entry);
3151 }
3152
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003153 out_unlock:
Steven Rostedt59df055f2009-02-14 15:29:06 -05003154 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003155 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003156}
3157
3158void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003159unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003160 void *data)
3161{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003162 __unregister_ftrace_function_probe(glob, ops, data,
3163 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003164}
3165
3166void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003167unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003168{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003169 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003170}
3171
Steven Rostedtb6887d72009-02-17 12:32:04 -05003172void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003174 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003175}
3176
Steven Rostedtf6180772009-02-14 00:40:25 -05003177static LIST_HEAD(ftrace_commands);
3178static DEFINE_MUTEX(ftrace_cmd_mutex);
3179
3180int register_ftrace_command(struct ftrace_func_command *cmd)
3181{
3182 struct ftrace_func_command *p;
3183 int ret = 0;
3184
3185 mutex_lock(&ftrace_cmd_mutex);
3186 list_for_each_entry(p, &ftrace_commands, list) {
3187 if (strcmp(cmd->name, p->name) == 0) {
3188 ret = -EBUSY;
3189 goto out_unlock;
3190 }
3191 }
3192 list_add(&cmd->list, &ftrace_commands);
3193 out_unlock:
3194 mutex_unlock(&ftrace_cmd_mutex);
3195
3196 return ret;
3197}
3198
3199int unregister_ftrace_command(struct ftrace_func_command *cmd)
3200{
3201 struct ftrace_func_command *p, *n;
3202 int ret = -ENODEV;
3203
3204 mutex_lock(&ftrace_cmd_mutex);
3205 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3206 if (strcmp(cmd->name, p->name) == 0) {
3207 ret = 0;
3208 list_del_init(&p->list);
3209 goto out_unlock;
3210 }
3211 }
3212 out_unlock:
3213 mutex_unlock(&ftrace_cmd_mutex);
3214
3215 return ret;
3216}
3217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003218static int ftrace_process_regex(struct ftrace_hash *hash,
3219 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003220{
Steven Rostedtf6180772009-02-14 00:40:25 -05003221 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003222 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003223 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003224
3225 func = strsep(&next, ":");
3226
3227 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003228 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003229 if (!ret)
3230 ret = -EINVAL;
3231 if (ret < 0)
3232 return ret;
3233 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003234 }
3235
Steven Rostedtf6180772009-02-14 00:40:25 -05003236 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003237
3238 command = strsep(&next, ":");
3239
Steven Rostedtf6180772009-02-14 00:40:25 -05003240 mutex_lock(&ftrace_cmd_mutex);
3241 list_for_each_entry(p, &ftrace_commands, list) {
3242 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003243 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003244 goto out_unlock;
3245 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003246 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003247 out_unlock:
3248 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003249
Steven Rostedtf6180772009-02-14 00:40:25 -05003250 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003251}
3252
Ingo Molnare309b412008-05-12 21:20:51 +02003253static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003254ftrace_regex_write(struct file *file, const char __user *ubuf,
3255 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003256{
3257 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003258 struct trace_parser *parser;
3259 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003260
Li Zefan4ba79782009-09-22 13:52:20 +08003261 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003262 return 0;
3263
Steven Rostedt41c52c02008-05-22 11:46:33 -04003264 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003265
Steven Rostedt45a4a232011-04-21 23:16:46 -04003266 ret = -ENODEV;
3267 if (unlikely(ftrace_disabled))
3268 goto out_unlock;
3269
Steven Rostedt5072c592008-05-12 21:20:43 +02003270 if (file->f_mode & FMODE_READ) {
3271 struct seq_file *m = file->private_data;
3272 iter = m->private;
3273 } else
3274 iter = file->private_data;
3275
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003276 parser = &iter->parser;
3277 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003278
Li Zefan4ba79782009-09-22 13:52:20 +08003279 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003280 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003281 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003282 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003283 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003284 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003285 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003286 }
3287
Steven Rostedt5072c592008-05-12 21:20:43 +02003288 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003289out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003290 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003291
Steven Rostedt5072c592008-05-12 21:20:43 +02003292 return ret;
3293}
3294
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003295ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003296ftrace_filter_write(struct file *file, const char __user *ubuf,
3297 size_t cnt, loff_t *ppos)
3298{
3299 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3300}
3301
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003302ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003303ftrace_notrace_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, 0);
3307}
3308
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003309static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003310ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3311{
3312 struct ftrace_func_entry *entry;
3313
3314 if (!ftrace_location(ip))
3315 return -EINVAL;
3316
3317 if (remove) {
3318 entry = ftrace_lookup_ip(hash, ip);
3319 if (!entry)
3320 return -ENOENT;
3321 free_hash_entry(hash, entry);
3322 return 0;
3323 }
3324
3325 return add_hash_entry(hash, ip);
3326}
3327
3328static int
3329ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3330 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003331{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003332 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003333 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003334 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003335
Steven Rostedt936e0742011-05-05 22:54:01 -04003336 /* All global ops uses the global ops filters */
3337 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3338 ops = &global_ops;
3339
Steven Rostedt41c52c02008-05-22 11:46:33 -04003340 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003341 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003342
Steven Rostedtf45948e2011-05-02 12:29:25 -04003343 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003344 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003345 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003346 orig_hash = &ops->notrace_hash;
3347
3348 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3349 if (!hash)
3350 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003351
Steven Rostedt41c52c02008-05-22 11:46:33 -04003352 mutex_lock(&ftrace_regex_lock);
3353 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003354 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003355 if (buf && !ftrace_match_records(hash, buf, len)) {
3356 ret = -EINVAL;
3357 goto out_regex_unlock;
3358 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003359 if (ip) {
3360 ret = ftrace_match_addr(hash, ip, remove);
3361 if (ret < 0)
3362 goto out_regex_unlock;
3363 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003364
3365 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003366 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003367 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3368 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003369 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003370
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003371 mutex_unlock(&ftrace_lock);
3372
Jiri Olsaac483c42012-01-02 10:04:14 +01003373 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003374 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003375
3376 free_ftrace_hash(hash);
3377 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003378}
3379
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003380static int
3381ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3382 int reset, int enable)
3383{
3384 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3385}
3386
3387/**
3388 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3389 * @ops - the ops to set the filter with
3390 * @ip - the address to add to or remove from the filter.
3391 * @remove - non zero to remove the ip from the filter
3392 * @reset - non zero to reset all filters before applying this filter.
3393 *
3394 * Filters denote which functions should be enabled when tracing is enabled
3395 * If @ip is NULL, it failes to update filter.
3396 */
3397int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3398 int remove, int reset)
3399{
3400 return ftrace_set_addr(ops, ip, remove, reset, 1);
3401}
3402EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3403
3404static int
3405ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3406 int reset, int enable)
3407{
3408 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3409}
3410
Steven Rostedt77a2b372008-05-12 21:20:45 +02003411/**
3412 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003413 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003414 * @buf - the string that holds the function filter text.
3415 * @len - the length of the string.
3416 * @reset - non zero to reset all filters before applying this filter.
3417 *
3418 * Filters denote which functions should be enabled when tracing is enabled.
3419 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3420 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003421int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003422 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003423{
Jiri Olsaac483c42012-01-02 10:04:14 +01003424 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003425}
Steven Rostedt936e0742011-05-05 22:54:01 -04003426EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003427
Steven Rostedt41c52c02008-05-22 11:46:33 -04003428/**
3429 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003430 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003431 * @buf - the string that holds the function notrace text.
3432 * @len - the length of the string.
3433 * @reset - non zero to reset all filters before applying this filter.
3434 *
3435 * Notrace Filters denote which functions should not be enabled when tracing
3436 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3437 * for tracing.
3438 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003439int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003440 int len, int reset)
3441{
Jiri Olsaac483c42012-01-02 10:04:14 +01003442 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003443}
3444EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3445/**
3446 * ftrace_set_filter - set a function to filter on in ftrace
3447 * @ops - the ops to set the filter with
3448 * @buf - the string that holds the function filter text.
3449 * @len - the length of the string.
3450 * @reset - non zero to reset all filters before applying this filter.
3451 *
3452 * Filters denote which functions should be enabled when tracing is enabled.
3453 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3454 */
3455void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3456{
3457 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3458}
3459EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3460
3461/**
3462 * ftrace_set_notrace - set a function to not trace in ftrace
3463 * @ops - the ops to set the notrace filter with
3464 * @buf - the string that holds the function notrace text.
3465 * @len - the length of the string.
3466 * @reset - non zero to reset all filters before applying this filter.
3467 *
3468 * Notrace Filters denote which functions should not be enabled when tracing
3469 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3470 * for tracing.
3471 */
3472void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003473{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003474 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003475}
Steven Rostedt936e0742011-05-05 22:54:01 -04003476EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003477
Steven Rostedt2af15d62009-05-28 13:37:24 -04003478/*
3479 * command line interface to allow users to set filters on boot up.
3480 */
3481#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3482static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3483static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3484
3485static int __init set_ftrace_notrace(char *str)
3486{
Chen Gang9607a862013-04-08 12:06:44 +08003487 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003488 return 1;
3489}
3490__setup("ftrace_notrace=", set_ftrace_notrace);
3491
3492static int __init set_ftrace_filter(char *str)
3493{
Chen Gang9607a862013-04-08 12:06:44 +08003494 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003495 return 1;
3496}
3497__setup("ftrace_filter=", set_ftrace_filter);
3498
Stefan Assmann369bc182009-10-12 22:17:21 +02003499#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003500static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003501static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3502
Stefan Assmann369bc182009-10-12 22:17:21 +02003503static int __init set_graph_function(char *str)
3504{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003505 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003506 return 1;
3507}
3508__setup("ftrace_graph_filter=", set_graph_function);
3509
3510static void __init set_ftrace_early_graph(char *buf)
3511{
3512 int ret;
3513 char *func;
3514
3515 while (buf) {
3516 func = strsep(&buf, ",");
3517 /* we allow only one expression at a time */
3518 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3519 func);
3520 if (ret)
3521 printk(KERN_DEBUG "ftrace: function %s not "
3522 "traceable\n", func);
3523 }
3524}
3525#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3526
Steven Rostedt2a85a372011-12-19 21:57:44 -05003527void __init
3528ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003529{
3530 char *func;
3531
3532 while (buf) {
3533 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003534 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003535 }
3536}
3537
3538static void __init set_ftrace_early_filters(void)
3539{
3540 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003541 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003542 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003543 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003544#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3545 if (ftrace_graph_buf[0])
3546 set_ftrace_early_graph(ftrace_graph_buf);
3547#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003548}
3549
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003550int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003551{
3552 struct seq_file *m = (struct seq_file *)file->private_data;
3553 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003554 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003555 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003556 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003557 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003558
Steven Rostedt41c52c02008-05-22 11:46:33 -04003559 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003560 if (file->f_mode & FMODE_READ) {
3561 iter = m->private;
3562
3563 seq_release(inode, file);
3564 } else
3565 iter = file->private_data;
3566
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003567 parser = &iter->parser;
3568 if (trace_parser_loaded(parser)) {
3569 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003570 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003571 }
3572
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003573 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003574
Steven Rostedt058e2972011-04-29 22:35:33 -04003575 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003576 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3577
3578 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003579 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003580 else
3581 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003582
Steven Rostedt058e2972011-04-29 22:35:33 -04003583 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003584 ret = ftrace_hash_move(iter->ops, filter_hash,
3585 orig_hash, iter->hash);
3586 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3587 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003588 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003589
Steven Rostedt058e2972011-04-29 22:35:33 -04003590 mutex_unlock(&ftrace_lock);
3591 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003592 free_ftrace_hash(iter->hash);
3593 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003594
Steven Rostedt41c52c02008-05-22 11:46:33 -04003595 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003596 return 0;
3597}
3598
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003599static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003600 .open = ftrace_avail_open,
3601 .read = seq_read,
3602 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003603 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003604};
3605
Steven Rostedt647bcd02011-05-03 14:39:21 -04003606static const struct file_operations ftrace_enabled_fops = {
3607 .open = ftrace_enabled_open,
3608 .read = seq_read,
3609 .llseek = seq_lseek,
3610 .release = seq_release_private,
3611};
3612
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003613static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003614 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003615 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003616 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003617 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003618 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003619};
3620
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003621static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003622 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003623 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003624 .write = ftrace_notrace_write,
3625 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003626 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003627};
3628
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003629#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3630
3631static DEFINE_MUTEX(graph_lock);
3632
3633int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003634int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003635unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3636
3637static void *
Li Zefan85951842009-06-24 09:54:00 +08003638__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003639{
Li Zefan85951842009-06-24 09:54:00 +08003640 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003641 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003642 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003643}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003644
Li Zefan85951842009-06-24 09:54:00 +08003645static void *
3646g_next(struct seq_file *m, void *v, loff_t *pos)
3647{
3648 (*pos)++;
3649 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003650}
3651
3652static void *g_start(struct seq_file *m, loff_t *pos)
3653{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003654 mutex_lock(&graph_lock);
3655
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003656 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003657 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003658 return (void *)1;
3659
Li Zefan85951842009-06-24 09:54:00 +08003660 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003661}
3662
3663static void g_stop(struct seq_file *m, void *p)
3664{
3665 mutex_unlock(&graph_lock);
3666}
3667
3668static int g_show(struct seq_file *m, void *v)
3669{
3670 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003671
3672 if (!ptr)
3673 return 0;
3674
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003675 if (ptr == (unsigned long *)1) {
3676 seq_printf(m, "#### all functions enabled ####\n");
3677 return 0;
3678 }
3679
Steven Rostedtb375a112009-09-17 00:05:58 -04003680 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003681
3682 return 0;
3683}
3684
James Morris88e9d342009-09-22 16:43:43 -07003685static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003686 .start = g_start,
3687 .next = g_next,
3688 .stop = g_stop,
3689 .show = g_show,
3690};
3691
3692static int
3693ftrace_graph_open(struct inode *inode, struct file *file)
3694{
3695 int ret = 0;
3696
3697 if (unlikely(ftrace_disabled))
3698 return -ENODEV;
3699
3700 mutex_lock(&graph_lock);
3701 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003702 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003703 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003704 ftrace_graph_count = 0;
3705 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3706 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003707 mutex_unlock(&graph_lock);
3708
Li Zefana4ec5e02009-09-18 14:06:28 +08003709 if (file->f_mode & FMODE_READ)
3710 ret = seq_open(file, &ftrace_graph_seq_ops);
3711
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003712 return ret;
3713}
3714
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003715static int
Li Zefan87827112009-07-23 11:29:11 +08003716ftrace_graph_release(struct inode *inode, struct file *file)
3717{
3718 if (file->f_mode & FMODE_READ)
3719 seq_release(inode, file);
3720 return 0;
3721}
3722
3723static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003724ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003725{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003726 struct dyn_ftrace *rec;
3727 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003728 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003729 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003730 int type, not;
3731 char *search;
3732 bool exists;
3733 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003734
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003735 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003736 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003737 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3738 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003739
3740 search_len = strlen(search);
3741
Steven Rostedt52baf112009-02-14 01:15:39 -05003742 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003743
3744 if (unlikely(ftrace_disabled)) {
3745 mutex_unlock(&ftrace_lock);
3746 return -ENODEV;
3747 }
3748
Steven Rostedt265c8312009-02-13 12:43:56 -05003749 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003750
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003751 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003752 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003753 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003754 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003755 if (array[i] == rec->ip) {
3756 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003757 break;
3758 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003759 }
3760
3761 if (!not) {
3762 fail = 0;
3763 if (!exists) {
3764 array[(*idx)++] = rec->ip;
3765 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3766 goto out;
3767 }
3768 } else {
3769 if (exists) {
3770 array[i] = array[--(*idx)];
3771 array[*idx] = 0;
3772 fail = 0;
3773 }
3774 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003775 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003776 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003777out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003778 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003779
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003780 if (fail)
3781 return -EINVAL;
3782
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003783 ftrace_graph_filter_enabled = !!(*idx);
3784
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003785 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003786}
3787
3788static ssize_t
3789ftrace_graph_write(struct file *file, const char __user *ubuf,
3790 size_t cnt, loff_t *ppos)
3791{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003792 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003793 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003794
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003795 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003796 return 0;
3797
3798 mutex_lock(&graph_lock);
3799
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003800 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3801 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003802 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003803 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003804
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003805 read = trace_get_user(&parser, ubuf, cnt, ppos);
3806
Li Zefan4ba79782009-09-22 13:52:20 +08003807 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003808 parser.buffer[parser.idx] = 0;
3809
3810 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003811 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003812 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003813 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003814 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003815 }
3816
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003817 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003818
3819out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003820 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003821out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003822 mutex_unlock(&graph_lock);
3823
3824 return ret;
3825}
3826
3827static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003828 .open = ftrace_graph_open,
3829 .read = seq_read,
3830 .write = ftrace_graph_write,
3831 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003832 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003833};
3834#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3835
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003836static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003837{
Steven Rostedt5072c592008-05-12 21:20:43 +02003838
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003839 trace_create_file("available_filter_functions", 0444,
3840 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003841
Steven Rostedt647bcd02011-05-03 14:39:21 -04003842 trace_create_file("enabled_functions", 0444,
3843 d_tracer, NULL, &ftrace_enabled_fops);
3844
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003845 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3846 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003847
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003848 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003849 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003850
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003851#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003852 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003853 NULL,
3854 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003855#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3856
Steven Rostedt5072c592008-05-12 21:20:43 +02003857 return 0;
3858}
3859
Steven Rostedt9fd49322012-04-24 22:32:06 -04003860static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003861{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003862 const unsigned long *ipa = a;
3863 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003864
Steven Rostedt9fd49322012-04-24 22:32:06 -04003865 if (*ipa > *ipb)
3866 return 1;
3867 if (*ipa < *ipb)
3868 return -1;
3869 return 0;
3870}
3871
3872static void ftrace_swap_ips(void *a, void *b, int size)
3873{
3874 unsigned long *ipa = a;
3875 unsigned long *ipb = b;
3876 unsigned long t;
3877
3878 t = *ipa;
3879 *ipa = *ipb;
3880 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003881}
3882
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003883static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003884 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003885 unsigned long *end)
3886{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003887 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003888 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003889 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003890 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003891 unsigned long *p;
3892 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003893 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003894 int ret = -ENOMEM;
3895
3896 count = end - start;
3897
3898 if (!count)
3899 return 0;
3900
Steven Rostedt9fd49322012-04-24 22:32:06 -04003901 sort(start, count, sizeof(*start),
3902 ftrace_cmp_ips, ftrace_swap_ips);
3903
Steven Rostedt706c81f2012-04-24 23:45:26 -04003904 start_pg = ftrace_allocate_pages(count);
3905 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003906 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003907
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003908 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003909
Steven Rostedt320823092011-12-16 14:42:37 -05003910 /*
3911 * Core and each module needs their own pages, as
3912 * modules will free them when they are removed.
3913 * Force a new page to be allocated for modules.
3914 */
Steven Rostedta7900872011-12-16 16:23:44 -05003915 if (!mod) {
3916 WARN_ON(ftrace_pages || ftrace_pages_start);
3917 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003918 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003919 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05003920 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003921 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05003922
Steven Rostedta7900872011-12-16 16:23:44 -05003923 if (WARN_ON(ftrace_pages->next)) {
3924 /* Hmm, we have free pages? */
3925 while (ftrace_pages->next)
3926 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05003927 }
Steven Rostedta7900872011-12-16 16:23:44 -05003928
Steven Rostedt706c81f2012-04-24 23:45:26 -04003929 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05003930 }
3931
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003932 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003933 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003934 while (p < end) {
3935 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003936 /*
3937 * Some architecture linkers will pad between
3938 * the different mcount_loc sections of different
3939 * object files to satisfy alignments.
3940 * Skip any NULL pointers.
3941 */
3942 if (!addr)
3943 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003944
3945 if (pg->index == pg->size) {
3946 /* We should have allocated enough */
3947 if (WARN_ON(!pg->next))
3948 break;
3949 pg = pg->next;
3950 }
3951
3952 rec = &pg->records[pg->index++];
3953 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003954 }
3955
Steven Rostedt706c81f2012-04-24 23:45:26 -04003956 /* We should have used all pages */
3957 WARN_ON(pg->next);
3958
3959 /* Assign the last page to ftrace_pages */
3960 ftrace_pages = pg;
3961
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003962 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003963 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003964
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003965 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003966 * We only need to disable interrupts on start up
3967 * because we are modifying code that an interrupt
3968 * may execute, and the modification is not atomic.
3969 * But for modules, nothing runs the code we modify
3970 * until we are finished with it, and there's no
3971 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003972 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003973 if (!mod)
3974 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003975 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003976 if (!mod)
3977 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003978 ret = 0;
3979 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003980 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003981
Steven Rostedta7900872011-12-16 16:23:44 -05003982 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003983}
3984
Steven Rostedt93eb6772009-04-15 13:24:06 -04003985#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05003986
3987#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3988
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003989void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003990{
3991 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05003992 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003993 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003994 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003995
Steven Rostedt93eb6772009-04-15 13:24:06 -04003996 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003997
3998 if (ftrace_disabled)
3999 goto out_unlock;
4000
Steven Rostedt320823092011-12-16 14:42:37 -05004001 /*
4002 * Each module has its own ftrace_pages, remove
4003 * them from the list.
4004 */
4005 last_pg = &ftrace_pages_start;
4006 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4007 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004008 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004009 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004010 * As core pages are first, the first
4011 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004012 */
Steven Rostedt320823092011-12-16 14:42:37 -05004013 if (WARN_ON(pg == ftrace_pages_start))
4014 goto out_unlock;
4015
4016 /* Check if we are deleting the last page */
4017 if (pg == ftrace_pages)
4018 ftrace_pages = next_to_ftrace_page(last_pg);
4019
4020 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004021 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4022 free_pages((unsigned long)pg->records, order);
4023 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004024 } else
4025 last_pg = &pg->next;
4026 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004027 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004028 mutex_unlock(&ftrace_lock);
4029}
4030
4031static void ftrace_init_module(struct module *mod,
4032 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004033{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004034 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004035 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004036 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004037}
4038
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004039static int ftrace_module_notify_enter(struct notifier_block *self,
4040 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004041{
4042 struct module *mod = data;
4043
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004044 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004045 ftrace_init_module(mod, mod->ftrace_callsites,
4046 mod->ftrace_callsites +
4047 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004048 return 0;
4049}
4050
4051static int ftrace_module_notify_exit(struct notifier_block *self,
4052 unsigned long val, void *data)
4053{
4054 struct module *mod = data;
4055
4056 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004057 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004058
4059 return 0;
4060}
4061#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004062static int ftrace_module_notify_enter(struct notifier_block *self,
4063 unsigned long val, void *data)
4064{
4065 return 0;
4066}
4067static int ftrace_module_notify_exit(struct notifier_block *self,
4068 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004069{
4070 return 0;
4071}
4072#endif /* CONFIG_MODULES */
4073
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004074struct notifier_block ftrace_module_enter_nb = {
4075 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004076 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004077};
4078
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004079struct notifier_block ftrace_module_exit_nb = {
4080 .notifier_call = ftrace_module_notify_exit,
4081 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4082};
4083
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004084extern unsigned long __start_mcount_loc[];
4085extern unsigned long __stop_mcount_loc[];
4086
4087void __init ftrace_init(void)
4088{
4089 unsigned long count, addr, flags;
4090 int ret;
4091
4092 /* Keep the ftrace pointer to the stub */
4093 addr = (unsigned long)ftrace_stub;
4094
4095 local_irq_save(flags);
4096 ftrace_dyn_arch_init(&addr);
4097 local_irq_restore(flags);
4098
4099 /* ftrace_dyn_arch_init places the return code in addr */
4100 if (addr)
4101 goto failed;
4102
4103 count = __stop_mcount_loc - __start_mcount_loc;
4104
4105 ret = ftrace_dyn_table_alloc(count);
4106 if (ret)
4107 goto failed;
4108
4109 last_ftrace_enabled = ftrace_enabled = 1;
4110
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004111 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004112 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004113 __stop_mcount_loc);
4114
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004115 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004116 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004117 pr_warning("Failed to register trace ftrace module enter notifier\n");
4118
4119 ret = register_module_notifier(&ftrace_module_exit_nb);
4120 if (ret)
4121 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004122
Steven Rostedt2af15d62009-05-28 13:37:24 -04004123 set_ftrace_early_filters();
4124
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004125 return;
4126 failed:
4127 ftrace_disabled = 1;
4128}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004129
Steven Rostedt3d083392008-05-12 21:20:42 +02004130#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004131
Steven Rostedt2b499382011-05-03 22:49:52 -04004132static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004133 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004134 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004135};
4136
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004137static int __init ftrace_nodyn_init(void)
4138{
4139 ftrace_enabled = 1;
4140 return 0;
4141}
Steven Rostedt6f415672012-10-05 12:13:07 -04004142core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004143
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004144static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4145static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004146/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004147# define ftrace_startup(ops, command) \
4148 ({ \
4149 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4150 0; \
4151 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004152# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004153# define ftrace_startup_sysctl() do { } while (0)
4154# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004155
4156static inline int
4157ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4158{
4159 return 1;
4160}
4161
Steven Rostedt3d083392008-05-12 21:20:42 +02004162#endif /* CONFIG_DYNAMIC_FTRACE */
4163
Steven Rostedtb8489142011-05-04 09:27:52 -04004164static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004165ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004166 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004167{
Jiri Olsae2484912012-02-15 15:51:48 +01004168 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4169 return;
4170
4171 /*
4172 * Some of the ops may be dynamically allocated,
4173 * they must be freed after a synchronize_sched().
4174 */
4175 preempt_disable_notrace();
4176 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004177 do_for_each_ftrace_op(op, ftrace_control_list) {
Jiri Olsae2484912012-02-15 15:51:48 +01004178 if (!ftrace_function_local_disabled(op) &&
4179 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004180 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004181 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004182 trace_recursion_clear(TRACE_CONTROL_BIT);
4183 preempt_enable_notrace();
4184}
4185
4186static struct ftrace_ops control_ops = {
4187 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004188 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004189};
4190
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004191static inline void
4192__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004193 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004194{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004195 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004196 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004197
Steven Rostedtccf36722012-06-05 09:44:25 -04004198 if (function_trace_stop)
4199 return;
4200
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004201 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4202 if (bit < 0)
4203 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004204
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004205 /*
4206 * Some of the ops may be dynamically allocated,
4207 * they must be freed after a synchronize_sched().
4208 */
4209 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004210 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004211 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004212 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004213 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004214 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004215 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004216}
4217
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004218/*
4219 * Some archs only support passing ip and parent_ip. Even though
4220 * the list function ignores the op parameter, we do not want any
4221 * C side effects, where a function is called without the caller
4222 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004223 * Archs are to support both the regs and ftrace_ops at the same time.
4224 * If they support ftrace_ops, it is assumed they support regs.
4225 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004226 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4227 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004228 * An architecture can pass partial regs with ftrace_ops and still
4229 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004230 */
4231#if ARCH_SUPPORTS_FTRACE_OPS
4232static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004233 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004234{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004235 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004236}
4237#else
4238static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4239{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004240 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004241}
4242#endif
4243
Steven Rostedte32d8952008-12-04 00:26:41 -05004244static void clear_ftrace_swapper(void)
4245{
4246 struct task_struct *p;
4247 int cpu;
4248
4249 get_online_cpus();
4250 for_each_online_cpu(cpu) {
4251 p = idle_task(cpu);
4252 clear_tsk_trace_trace(p);
4253 }
4254 put_online_cpus();
4255}
4256
4257static void set_ftrace_swapper(void)
4258{
4259 struct task_struct *p;
4260 int cpu;
4261
4262 get_online_cpus();
4263 for_each_online_cpu(cpu) {
4264 p = idle_task(cpu);
4265 set_tsk_trace_trace(p);
4266 }
4267 put_online_cpus();
4268}
4269
4270static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004271{
4272 struct task_struct *p;
4273
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004274 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004275 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004276 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004277 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004278 rcu_read_unlock();
4279
Steven Rostedte32d8952008-12-04 00:26:41 -05004280 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004281}
4282
Steven Rostedte32d8952008-12-04 00:26:41 -05004283static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004284{
4285 struct task_struct *p;
4286
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004287 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004288 do_each_pid_task(pid, PIDTYPE_PID, p) {
4289 set_tsk_trace_trace(p);
4290 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004291 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004292}
4293
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004294static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004295{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004296 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004297 clear_ftrace_swapper();
4298 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004299 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004300}
4301
4302static void set_ftrace_pid_task(struct pid *pid)
4303{
4304 if (pid == ftrace_swapper_pid)
4305 set_ftrace_swapper();
4306 else
4307 set_ftrace_pid(pid);
4308}
4309
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004310static int ftrace_pid_add(int p)
4311{
4312 struct pid *pid;
4313 struct ftrace_pid *fpid;
4314 int ret = -EINVAL;
4315
4316 mutex_lock(&ftrace_lock);
4317
4318 if (!p)
4319 pid = ftrace_swapper_pid;
4320 else
4321 pid = find_get_pid(p);
4322
4323 if (!pid)
4324 goto out;
4325
4326 ret = 0;
4327
4328 list_for_each_entry(fpid, &ftrace_pids, list)
4329 if (fpid->pid == pid)
4330 goto out_put;
4331
4332 ret = -ENOMEM;
4333
4334 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4335 if (!fpid)
4336 goto out_put;
4337
4338 list_add(&fpid->list, &ftrace_pids);
4339 fpid->pid = pid;
4340
4341 set_ftrace_pid_task(pid);
4342
4343 ftrace_update_pid_func();
4344 ftrace_startup_enable(0);
4345
4346 mutex_unlock(&ftrace_lock);
4347 return 0;
4348
4349out_put:
4350 if (pid != ftrace_swapper_pid)
4351 put_pid(pid);
4352
4353out:
4354 mutex_unlock(&ftrace_lock);
4355 return ret;
4356}
4357
4358static void ftrace_pid_reset(void)
4359{
4360 struct ftrace_pid *fpid, *safe;
4361
4362 mutex_lock(&ftrace_lock);
4363 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4364 struct pid *pid = fpid->pid;
4365
4366 clear_ftrace_pid_task(pid);
4367
4368 list_del(&fpid->list);
4369 kfree(fpid);
4370 }
4371
4372 ftrace_update_pid_func();
4373 ftrace_startup_enable(0);
4374
4375 mutex_unlock(&ftrace_lock);
4376}
4377
4378static void *fpid_start(struct seq_file *m, loff_t *pos)
4379{
4380 mutex_lock(&ftrace_lock);
4381
4382 if (list_empty(&ftrace_pids) && (!*pos))
4383 return (void *) 1;
4384
4385 return seq_list_start(&ftrace_pids, *pos);
4386}
4387
4388static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4389{
4390 if (v == (void *)1)
4391 return NULL;
4392
4393 return seq_list_next(v, &ftrace_pids, pos);
4394}
4395
4396static void fpid_stop(struct seq_file *m, void *p)
4397{
4398 mutex_unlock(&ftrace_lock);
4399}
4400
4401static int fpid_show(struct seq_file *m, void *v)
4402{
4403 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4404
4405 if (v == (void *)1) {
4406 seq_printf(m, "no pid\n");
4407 return 0;
4408 }
4409
4410 if (fpid->pid == ftrace_swapper_pid)
4411 seq_printf(m, "swapper tasks\n");
4412 else
4413 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4414
4415 return 0;
4416}
4417
4418static const struct seq_operations ftrace_pid_sops = {
4419 .start = fpid_start,
4420 .next = fpid_next,
4421 .stop = fpid_stop,
4422 .show = fpid_show,
4423};
4424
4425static int
4426ftrace_pid_open(struct inode *inode, struct file *file)
4427{
4428 int ret = 0;
4429
4430 if ((file->f_mode & FMODE_WRITE) &&
4431 (file->f_flags & O_TRUNC))
4432 ftrace_pid_reset();
4433
4434 if (file->f_mode & FMODE_READ)
4435 ret = seq_open(file, &ftrace_pid_sops);
4436
4437 return ret;
4438}
4439
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004440static ssize_t
4441ftrace_pid_write(struct file *filp, const char __user *ubuf,
4442 size_t cnt, loff_t *ppos)
4443{
Ingo Molnar457dc922009-11-23 11:03:28 +01004444 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004445 long val;
4446 int ret;
4447
4448 if (cnt >= sizeof(buf))
4449 return -EINVAL;
4450
4451 if (copy_from_user(&buf, ubuf, cnt))
4452 return -EFAULT;
4453
4454 buf[cnt] = 0;
4455
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004456 /*
4457 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4458 * to clean the filter quietly.
4459 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004460 tmp = strstrip(buf);
4461 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004462 return 1;
4463
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004464 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004465 if (ret < 0)
4466 return ret;
4467
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004468 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004469
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004470 return ret ? ret : cnt;
4471}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004472
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004473static int
4474ftrace_pid_release(struct inode *inode, struct file *file)
4475{
4476 if (file->f_mode & FMODE_READ)
4477 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004478
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004479 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004480}
4481
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004482static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004483 .open = ftrace_pid_open,
4484 .write = ftrace_pid_write,
4485 .read = seq_read,
4486 .llseek = seq_lseek,
4487 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004488};
4489
4490static __init int ftrace_init_debugfs(void)
4491{
4492 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004493
4494 d_tracer = tracing_init_dentry();
4495 if (!d_tracer)
4496 return 0;
4497
4498 ftrace_init_dyn_debugfs(d_tracer);
4499
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004500 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4501 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004502
4503 ftrace_profile_debugfs(d_tracer);
4504
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004505 return 0;
4506}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004507fs_initcall(ftrace_init_debugfs);
4508
Steven Rostedt3d083392008-05-12 21:20:42 +02004509/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004510 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004511 *
4512 * This function should be used by panic code. It stops ftrace
4513 * but in a not so nice way. If you need to simply kill ftrace
4514 * from a non-atomic section, use ftrace_kill.
4515 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004516void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004517{
4518 ftrace_disabled = 1;
4519 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004520 clear_ftrace_function();
4521}
4522
4523/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004524 * Test if ftrace is dead or not.
4525 */
4526int ftrace_is_dead(void)
4527{
4528 return ftrace_disabled;
4529}
4530
4531/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004532 * register_ftrace_function - register a function for profiling
4533 * @ops - ops structure that holds the function for profiling.
4534 *
4535 * Register a function to be called by all functions in the
4536 * kernel.
4537 *
4538 * Note: @ops->func and all the functions it calls must be labeled
4539 * with "notrace", otherwise it will go into a
4540 * recursive loop.
4541 */
4542int register_ftrace_function(struct ftrace_ops *ops)
4543{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004544 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004545
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004546 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004547
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004548 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004549 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004550 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004551
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004552 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004553
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004554 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004555}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004556EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004557
4558/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004559 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004560 * @ops - ops structure that holds the function to unregister
4561 *
4562 * Unregister a function that was added to be called by ftrace profiling.
4563 */
4564int unregister_ftrace_function(struct ftrace_ops *ops)
4565{
4566 int ret;
4567
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004568 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004569 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004570 if (!ret)
4571 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004572 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004573
4574 return ret;
4575}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004576EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004577
Ingo Molnare309b412008-05-12 21:20:51 +02004578int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004579ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004580 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004581 loff_t *ppos)
4582{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004583 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004584
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004585 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004586
Steven Rostedt45a4a232011-04-21 23:16:46 -04004587 if (unlikely(ftrace_disabled))
4588 goto out;
4589
4590 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004591
Li Zefana32c7762009-06-26 16:55:51 +08004592 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004593 goto out;
4594
Li Zefana32c7762009-06-26 16:55:51 +08004595 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004596
4597 if (ftrace_enabled) {
4598
4599 ftrace_startup_sysctl();
4600
4601 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004602 if (ftrace_ops_list != &ftrace_list_end) {
4603 if (ftrace_ops_list->next == &ftrace_list_end)
4604 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004605 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004606 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004607 }
4608
4609 } else {
4610 /* stopping ftrace calls (just send to ftrace_stub) */
4611 ftrace_trace_function = ftrace_stub;
4612
4613 ftrace_shutdown_sysctl();
4614 }
4615
4616 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004617 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004618 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004619}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004620
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004621#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004622
Steven Rostedt597af812009-04-03 15:24:12 -04004623static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004624static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004625
Steven Rostedte49dc192008-12-02 23:50:05 -05004626int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4627{
4628 return 0;
4629}
4630
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004631/* The callbacks that hook a function */
4632trace_func_graph_ret_t ftrace_graph_return =
4633 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004634trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004635
4636/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4637static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4638{
4639 int i;
4640 int ret = 0;
4641 unsigned long flags;
4642 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4643 struct task_struct *g, *t;
4644
4645 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4646 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4647 * sizeof(struct ftrace_ret_stack),
4648 GFP_KERNEL);
4649 if (!ret_stack_list[i]) {
4650 start = 0;
4651 end = i;
4652 ret = -ENOMEM;
4653 goto free;
4654 }
4655 }
4656
4657 read_lock_irqsave(&tasklist_lock, flags);
4658 do_each_thread(g, t) {
4659 if (start == end) {
4660 ret = -EAGAIN;
4661 goto unlock;
4662 }
4663
4664 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004665 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004666 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004667 t->curr_ret_stack = -1;
4668 /* Make sure the tasks see the -1 first: */
4669 smp_wmb();
4670 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004671 }
4672 } while_each_thread(g, t);
4673
4674unlock:
4675 read_unlock_irqrestore(&tasklist_lock, flags);
4676free:
4677 for (i = start; i < end; i++)
4678 kfree(ret_stack_list[i]);
4679 return ret;
4680}
4681
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004682static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004683ftrace_graph_probe_sched_switch(void *ignore,
4684 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004685{
4686 unsigned long long timestamp;
4687 int index;
4688
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004689 /*
4690 * Does the user want to count the time a function was asleep.
4691 * If so, do not update the time stamps.
4692 */
4693 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4694 return;
4695
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004696 timestamp = trace_clock_local();
4697
4698 prev->ftrace_timestamp = timestamp;
4699
4700 /* only process tasks that we timestamped */
4701 if (!next->ftrace_timestamp)
4702 return;
4703
4704 /*
4705 * Update all the counters in next to make up for the
4706 * time next was sleeping.
4707 */
4708 timestamp -= next->ftrace_timestamp;
4709
4710 for (index = next->curr_ret_stack; index >= 0; index--)
4711 next->ret_stack[index].calltime += timestamp;
4712}
4713
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004714/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004715static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004716{
4717 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004718 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004719
4720 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4721 sizeof(struct ftrace_ret_stack *),
4722 GFP_KERNEL);
4723
4724 if (!ret_stack_list)
4725 return -ENOMEM;
4726
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004727 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004728 for_each_online_cpu(cpu) {
4729 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004730 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004731 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004732
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004733 do {
4734 ret = alloc_retstack_tasklist(ret_stack_list);
4735 } while (ret == -EAGAIN);
4736
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004737 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004738 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004739 if (ret)
4740 pr_info("ftrace_graph: Couldn't activate tracepoint"
4741 " probe to kernel_sched_switch\n");
4742 }
4743
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004744 kfree(ret_stack_list);
4745 return ret;
4746}
4747
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004748/*
4749 * Hibernation protection.
4750 * The state of the current task is too much unstable during
4751 * suspend/restore to disk. We want to protect against that.
4752 */
4753static int
4754ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4755 void *unused)
4756{
4757 switch (state) {
4758 case PM_HIBERNATION_PREPARE:
4759 pause_graph_tracing();
4760 break;
4761
4762 case PM_POST_HIBERNATION:
4763 unpause_graph_tracing();
4764 break;
4765 }
4766 return NOTIFY_DONE;
4767}
4768
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004769int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4770 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004771{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004772 int ret = 0;
4773
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004774 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004775
Steven Rostedt05ce5812009-03-24 00:18:31 -04004776 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004777 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004778 ret = -EBUSY;
4779 goto out;
4780 }
4781
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004782 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4783 register_pm_notifier(&ftrace_suspend_notifier);
4784
Steven Rostedt597af812009-04-03 15:24:12 -04004785 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004786 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004787 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004788 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004789 goto out;
4790 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004791
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004792 ftrace_graph_return = retfunc;
4793 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004794
Steven Rostedta1cd6172011-05-23 15:24:25 -04004795 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004796
4797out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004798 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004799 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004800}
4801
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004802void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004803{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004804 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004805
Steven Rostedt597af812009-04-03 15:24:12 -04004806 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004807 goto out;
4808
Steven Rostedt597af812009-04-03 15:24:12 -04004809 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004810 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004811 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004812 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004813 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004814 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004815
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004816 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004817 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004818}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004819
Steven Rostedt868baf02011-02-10 21:26:13 -05004820static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4821
4822static void
4823graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4824{
4825 atomic_set(&t->tracing_graph_pause, 0);
4826 atomic_set(&t->trace_overrun, 0);
4827 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004828 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004829 smp_wmb();
4830 t->ret_stack = ret_stack;
4831}
4832
4833/*
4834 * Allocate a return stack for the idle task. May be the first
4835 * time through, or it may be done by CPU hotplug online.
4836 */
4837void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4838{
4839 t->curr_ret_stack = -1;
4840 /*
4841 * The idle task has no parent, it either has its own
4842 * stack or no stack at all.
4843 */
4844 if (t->ret_stack)
4845 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4846
4847 if (ftrace_graph_active) {
4848 struct ftrace_ret_stack *ret_stack;
4849
4850 ret_stack = per_cpu(idle_ret_stack, cpu);
4851 if (!ret_stack) {
4852 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4853 * sizeof(struct ftrace_ret_stack),
4854 GFP_KERNEL);
4855 if (!ret_stack)
4856 return;
4857 per_cpu(idle_ret_stack, cpu) = ret_stack;
4858 }
4859 graph_init_task(t, ret_stack);
4860 }
4861}
4862
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004863/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004864void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004865{
Steven Rostedt84047e32009-06-02 16:51:55 -04004866 /* Make sure we do not use the parent ret_stack */
4867 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004868 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004869
Steven Rostedt597af812009-04-03 15:24:12 -04004870 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004871 struct ftrace_ret_stack *ret_stack;
4872
4873 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004874 * sizeof(struct ftrace_ret_stack),
4875 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004876 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004877 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004878 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004879 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004880}
4881
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004882void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004883{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004884 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4885
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004886 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004887 /* NULL must become visible to IRQs before we free it: */
4888 barrier();
4889
4890 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004891}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004892
4893void ftrace_graph_stop(void)
4894{
4895 ftrace_stop();
4896}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004897#endif