2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
47 #define FTRACE_WARN_ON_ONCE(cond) \
49 if (WARN_ON_ONCE(cond)) \
53 /* hash bits for specific function selection */
54 #define FTRACE_HASH_BITS 7
55 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
57 /* ftrace_enabled is a method to turn ftrace on or off */
58 int ftrace_enabled __read_mostly
;
59 static int last_ftrace_enabled
;
61 /* Quick disabling of function tracer. */
62 int function_trace_stop
;
64 /* List for set_ftrace_pid's pids. */
65 LIST_HEAD(ftrace_pids
);
67 struct list_head list
;
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
75 static int ftrace_disabled __read_mostly
;
77 static DEFINE_MUTEX(ftrace_lock
);
79 static struct ftrace_ops ftrace_list_end __read_mostly
=
84 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
85 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
86 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
87 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
98 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
100 struct ftrace_ops
*op
= rcu_dereference_raw(ftrace_list
); /*see above*/
102 while (op
!= &ftrace_list_end
) {
103 op
->func(ip
, parent_ip
);
104 op
= rcu_dereference_raw(op
->next
); /*see above*/
108 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
110 if (!test_tsk_trace_trace(current
))
113 ftrace_pid_function(ip
, parent_ip
);
116 static void set_ftrace_pid_function(ftrace_func_t func
)
118 /* do not set ftrace_pid_function to itself! */
119 if (func
!= ftrace_pid_func
)
120 ftrace_pid_function
= func
;
124 * clear_ftrace_function - reset the ftrace function
126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
129 void clear_ftrace_function(void)
131 ftrace_trace_function
= ftrace_stub
;
132 __ftrace_trace_function
= ftrace_stub
;
133 ftrace_pid_function
= ftrace_stub
;
136 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
141 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
143 if (function_trace_stop
)
146 __ftrace_trace_function(ip
, parent_ip
);
150 static int __register_ftrace_function(struct ftrace_ops
*ops
)
152 ops
->next
= ftrace_list
;
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
159 rcu_assign_pointer(ftrace_list
, ops
);
161 if (ftrace_enabled
) {
164 if (ops
->next
== &ftrace_list_end
)
167 func
= ftrace_list_func
;
169 if (!list_empty(&ftrace_pids
)) {
170 set_ftrace_pid_function(func
);
171 func
= ftrace_pid_func
;
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
178 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
179 ftrace_trace_function
= func
;
181 __ftrace_trace_function
= func
;
182 ftrace_trace_function
= ftrace_test_stop_func
;
189 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
191 struct ftrace_ops
**p
;
194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
197 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
198 ftrace_trace_function
= ftrace_stub
;
199 ftrace_list
= &ftrace_list_end
;
203 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
212 if (ftrace_enabled
) {
213 /* If we only have one func left, then call that directly */
214 if (ftrace_list
->next
== &ftrace_list_end
) {
215 ftrace_func_t func
= ftrace_list
->func
;
217 if (!list_empty(&ftrace_pids
)) {
218 set_ftrace_pid_function(func
);
219 func
= ftrace_pid_func
;
221 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function
= func
;
224 __ftrace_trace_function
= func
;
232 static void ftrace_update_pid_func(void)
236 if (ftrace_trace_function
== ftrace_stub
)
239 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
240 func
= ftrace_trace_function
;
242 func
= __ftrace_trace_function
;
245 if (!list_empty(&ftrace_pids
)) {
246 set_ftrace_pid_function(func
);
247 func
= ftrace_pid_func
;
249 if (func
== ftrace_pid_func
)
250 func
= ftrace_pid_function
;
253 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function
= func
;
256 __ftrace_trace_function
= func
;
260 #ifdef CONFIG_FUNCTION_PROFILER
261 struct ftrace_profile
{
262 struct hlist_node node
;
264 unsigned long counter
;
265 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time
;
267 unsigned long long time_squared
;
271 struct ftrace_profile_page
{
272 struct ftrace_profile_page
*next
;
274 struct ftrace_profile records
[];
277 struct ftrace_profile_stat
{
279 struct hlist_head
*hash
;
280 struct ftrace_profile_page
*pages
;
281 struct ftrace_profile_page
*start
;
282 struct tracer_stat stat
;
285 #define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
288 #define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
291 static int ftrace_profile_bits __read_mostly
;
292 static int ftrace_profile_enabled __read_mostly
;
294 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
295 static DEFINE_MUTEX(ftrace_profile_lock
);
297 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
299 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
302 function_stat_next(void *v
, int idx
)
304 struct ftrace_profile
*rec
= v
;
305 struct ftrace_profile_page
*pg
;
307 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
313 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
317 rec
= &pg
->records
[0];
325 static void *function_stat_start(struct tracer_stat
*trace
)
327 struct ftrace_profile_stat
*stat
=
328 container_of(trace
, struct ftrace_profile_stat
, stat
);
330 if (!stat
|| !stat
->start
)
333 return function_stat_next(&stat
->start
->records
[0], 0);
336 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
337 /* function graph compares on total time */
338 static int function_stat_cmp(void *p1
, void *p2
)
340 struct ftrace_profile
*a
= p1
;
341 struct ftrace_profile
*b
= p2
;
343 if (a
->time
< b
->time
)
345 if (a
->time
> b
->time
)
351 /* not function graph compares against hits */
352 static int function_stat_cmp(void *p1
, void *p2
)
354 struct ftrace_profile
*a
= p1
;
355 struct ftrace_profile
*b
= p2
;
357 if (a
->counter
< b
->counter
)
359 if (a
->counter
> b
->counter
)
366 static int function_stat_headers(struct seq_file
*m
)
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 seq_printf(m
, " Function "
372 "--- ---- --- ---\n");
374 seq_printf(m
, " Function Hit\n"
380 static int function_stat_show(struct seq_file
*m
, void *v
)
382 struct ftrace_profile
*rec
= v
;
383 char str
[KSYM_SYMBOL_LEN
];
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
386 static struct trace_seq s
;
387 unsigned long long avg
;
388 unsigned long long stddev
;
390 mutex_lock(&ftrace_profile_lock
);
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec
->counter
== 0)) {
398 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
399 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
404 do_div(avg
, rec
->counter
);
406 /* Sample standard deviation (s^2) */
407 if (rec
->counter
<= 1)
410 stddev
= rec
->time_squared
- rec
->counter
* avg
* avg
;
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
415 do_div(stddev
, (rec
->counter
- 1) * 1000);
419 trace_print_graph_duration(rec
->time
, &s
);
420 trace_seq_puts(&s
, " ");
421 trace_print_graph_duration(avg
, &s
);
422 trace_seq_puts(&s
, " ");
423 trace_print_graph_duration(stddev
, &s
);
424 trace_print_seq(m
, &s
);
428 mutex_unlock(&ftrace_profile_lock
);
433 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
435 struct ftrace_profile_page
*pg
;
437 pg
= stat
->pages
= stat
->start
;
440 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
445 memset(stat
->hash
, 0,
446 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
449 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
451 struct ftrace_profile_page
*pg
;
456 /* If we already allocated, do nothing */
460 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
464 #ifdef CONFIG_DYNAMIC_FTRACE
465 functions
= ftrace_update_tot_cnt
;
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
477 pg
= stat
->start
= stat
->pages
;
479 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
481 for (i
= 0; i
< pages
; i
++) {
482 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
493 unsigned long tmp
= (unsigned long)pg
;
499 free_page((unsigned long)stat
->pages
);
506 static int ftrace_profile_init_cpu(int cpu
)
508 struct ftrace_profile_stat
*stat
;
511 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
514 /* If the profile is already created, simply reset it */
515 ftrace_profile_reset(stat
);
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
523 size
= FTRACE_PROFILE_HASH_SIZE
;
525 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
530 if (!ftrace_profile_bits
) {
533 for (; size
; size
>>= 1)
534 ftrace_profile_bits
++;
537 /* Preallocate the function profiling pages */
538 if (ftrace_profile_pages_init(stat
) < 0) {
547 static int ftrace_profile_init(void)
552 for_each_online_cpu(cpu
) {
553 ret
= ftrace_profile_init_cpu(cpu
);
561 /* interrupts must be disabled */
562 static struct ftrace_profile
*
563 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
565 struct ftrace_profile
*rec
;
566 struct hlist_head
*hhd
;
567 struct hlist_node
*n
;
570 key
= hash_long(ip
, ftrace_profile_bits
);
571 hhd
= &stat
->hash
[key
];
573 if (hlist_empty(hhd
))
576 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
584 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
585 struct ftrace_profile
*rec
)
589 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
590 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
594 * The memory is already allocated, this simply finds a new record to use.
596 static struct ftrace_profile
*
597 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
599 struct ftrace_profile
*rec
= NULL
;
601 /* prevent recursion (from NMIs) */
602 if (atomic_inc_return(&stat
->disabled
) != 1)
606 * Try to find the function again since an NMI
607 * could have added it
609 rec
= ftrace_find_profiled_func(stat
, ip
);
613 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
614 if (!stat
->pages
->next
)
616 stat
->pages
= stat
->pages
->next
;
619 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
621 ftrace_add_profile(stat
, rec
);
624 atomic_dec(&stat
->disabled
);
630 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
632 struct ftrace_profile_stat
*stat
;
633 struct ftrace_profile
*rec
;
636 if (!ftrace_profile_enabled
)
639 local_irq_save(flags
);
641 stat
= &__get_cpu_var(ftrace_profile_stats
);
642 if (!stat
->hash
|| !ftrace_profile_enabled
)
645 rec
= ftrace_find_profiled_func(stat
, ip
);
647 rec
= ftrace_profile_alloc(stat
, ip
);
654 local_irq_restore(flags
);
657 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
658 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
660 function_profile_call(trace
->func
, 0);
664 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
666 struct ftrace_profile_stat
*stat
;
667 unsigned long long calltime
;
668 struct ftrace_profile
*rec
;
671 local_irq_save(flags
);
672 stat
= &__get_cpu_var(ftrace_profile_stats
);
673 if (!stat
->hash
|| !ftrace_profile_enabled
)
676 /* If the calltime was zero'd ignore it */
677 if (!trace
->calltime
)
680 calltime
= trace
->rettime
- trace
->calltime
;
682 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
685 index
= trace
->depth
;
687 /* Append this call time to the parent time to subtract */
689 current
->ret_stack
[index
- 1].subtime
+= calltime
;
691 if (current
->ret_stack
[index
].subtime
< calltime
)
692 calltime
-= current
->ret_stack
[index
].subtime
;
697 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
699 rec
->time
+= calltime
;
700 rec
->time_squared
+= calltime
* calltime
;
704 local_irq_restore(flags
);
707 static int register_ftrace_profiler(void)
709 return register_ftrace_graph(&profile_graph_return
,
710 &profile_graph_entry
);
713 static void unregister_ftrace_profiler(void)
715 unregister_ftrace_graph();
718 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
720 .func
= function_profile_call
,
723 static int register_ftrace_profiler(void)
725 return register_ftrace_function(&ftrace_profile_ops
);
728 static void unregister_ftrace_profiler(void)
730 unregister_ftrace_function(&ftrace_profile_ops
);
732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
735 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
736 size_t cnt
, loff_t
*ppos
)
739 char buf
[64]; /* big enough to hold a number */
742 if (cnt
>= sizeof(buf
))
745 if (copy_from_user(&buf
, ubuf
, cnt
))
750 ret
= strict_strtoul(buf
, 10, &val
);
756 mutex_lock(&ftrace_profile_lock
);
757 if (ftrace_profile_enabled
^ val
) {
759 ret
= ftrace_profile_init();
765 ret
= register_ftrace_profiler();
770 ftrace_profile_enabled
= 1;
772 ftrace_profile_enabled
= 0;
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
777 unregister_ftrace_profiler();
781 mutex_unlock(&ftrace_profile_lock
);
789 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
790 size_t cnt
, loff_t
*ppos
)
792 char buf
[64]; /* big enough to hold a number */
795 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
796 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
799 static const struct file_operations ftrace_profile_fops
= {
800 .open
= tracing_open_generic
,
801 .read
= ftrace_profile_read
,
802 .write
= ftrace_profile_write
,
805 /* used to initialize the real stat files */
806 static struct tracer_stat function_stats __initdata
= {
808 .stat_start
= function_stat_start
,
809 .stat_next
= function_stat_next
,
810 .stat_cmp
= function_stat_cmp
,
811 .stat_headers
= function_stat_headers
,
812 .stat_show
= function_stat_show
815 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
817 struct ftrace_profile_stat
*stat
;
818 struct dentry
*entry
;
823 for_each_possible_cpu(cpu
) {
824 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
826 /* allocate enough for function name + cpu number */
827 name
= kmalloc(32, GFP_KERNEL
);
830 * The files created are permanent, if something happens
831 * we still do not free memory.
834 "Could not allocate stat file for cpu %d\n",
838 stat
->stat
= function_stats
;
839 snprintf(name
, 32, "function%d", cpu
);
840 stat
->stat
.name
= name
;
841 ret
= register_stat_tracer(&stat
->stat
);
844 "Could not register function stat for cpu %d\n",
851 entry
= debugfs_create_file("function_profile_enabled", 0644,
852 d_tracer
, NULL
, &ftrace_profile_fops
);
854 pr_warning("Could not create debugfs "
855 "'function_profile_enabled' entry\n");
858 #else /* CONFIG_FUNCTION_PROFILER */
859 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
862 #endif /* CONFIG_FUNCTION_PROFILER */
864 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
866 #ifdef CONFIG_DYNAMIC_FTRACE
868 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
869 # error Dynamic ftrace depends on MCOUNT_RECORD
872 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
874 struct ftrace_func_probe
{
875 struct hlist_node node
;
876 struct ftrace_probe_ops
*ops
;
884 FTRACE_ENABLE_CALLS
= (1 << 0),
885 FTRACE_DISABLE_CALLS
= (1 << 1),
886 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
887 FTRACE_ENABLE_MCOUNT
= (1 << 3),
888 FTRACE_DISABLE_MCOUNT
= (1 << 4),
889 FTRACE_START_FUNC_RET
= (1 << 5),
890 FTRACE_STOP_FUNC_RET
= (1 << 6),
893 static int ftrace_filtered
;
895 static struct dyn_ftrace
*ftrace_new_addrs
;
897 static DEFINE_MUTEX(ftrace_regex_lock
);
900 struct ftrace_page
*next
;
902 struct dyn_ftrace records
[];
905 #define ENTRIES_PER_PAGE \
906 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
908 /* estimate from running different kernels */
909 #define NR_TO_INIT 10000
911 static struct ftrace_page
*ftrace_pages_start
;
912 static struct ftrace_page
*ftrace_pages
;
914 static struct dyn_ftrace
*ftrace_free_records
;
917 * This is a double for. Do not use 'break' to break out of the loop,
918 * you must use a goto.
920 #define do_for_each_ftrace_rec(pg, rec) \
921 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
923 for (_____i = 0; _____i < pg->index; _____i++) { \
924 rec = &pg->records[_____i];
926 #define while_for_each_ftrace_rec() \
930 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
932 rec
->freelist
= ftrace_free_records
;
933 ftrace_free_records
= rec
;
934 rec
->flags
|= FTRACE_FL_FREE
;
937 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
939 struct dyn_ftrace
*rec
;
941 /* First check for freed records */
942 if (ftrace_free_records
) {
943 rec
= ftrace_free_records
;
945 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
946 FTRACE_WARN_ON_ONCE(1);
947 ftrace_free_records
= NULL
;
951 ftrace_free_records
= rec
->freelist
;
952 memset(rec
, 0, sizeof(*rec
));
956 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
957 if (!ftrace_pages
->next
) {
958 /* allocate another page */
960 (void *)get_zeroed_page(GFP_KERNEL
);
961 if (!ftrace_pages
->next
)
964 ftrace_pages
= ftrace_pages
->next
;
967 return &ftrace_pages
->records
[ftrace_pages
->index
++];
970 static struct dyn_ftrace
*
971 ftrace_record_ip(unsigned long ip
)
973 struct dyn_ftrace
*rec
;
978 rec
= ftrace_alloc_dyn_node(ip
);
983 rec
->newlist
= ftrace_new_addrs
;
984 ftrace_new_addrs
= rec
;
989 static void print_ip_ins(const char *fmt
, unsigned char *p
)
993 printk(KERN_CONT
"%s", fmt
);
995 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
996 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
999 static void ftrace_bug(int failed
, unsigned long ip
)
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on modifying ");
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace failed to modify ");
1011 print_ip_ins(" actual: ", (unsigned char *)ip
);
1012 printk(KERN_CONT
"\n");
1015 FTRACE_WARN_ON_ONCE(1);
1016 pr_info("ftrace faulted on writing ");
1020 FTRACE_WARN_ON_ONCE(1);
1021 pr_info("ftrace faulted on unknown error ");
1027 /* Return 1 if the address range is reserved for ftrace */
1028 int ftrace_text_reserved(void *start
, void *end
)
1030 struct dyn_ftrace
*rec
;
1031 struct ftrace_page
*pg
;
1033 do_for_each_ftrace_rec(pg
, rec
) {
1034 if (rec
->ip
<= (unsigned long)end
&&
1035 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1037 } while_for_each_ftrace_rec();
1043 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1045 unsigned long ftrace_addr
;
1046 unsigned long flag
= 0UL;
1048 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1051 * If this record is not to be traced or we want to disable it,
1054 * If we want to enable it and filtering is off, then enable it.
1056 * If we want to enable it and filtering is on, enable it only if
1059 if (enable
&& !(rec
->flags
& FTRACE_FL_NOTRACE
)) {
1060 if (!ftrace_filtered
|| (rec
->flags
& FTRACE_FL_FILTER
))
1061 flag
= FTRACE_FL_ENABLED
;
1064 /* If the state of this record hasn't changed, then do nothing */
1065 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1069 rec
->flags
|= FTRACE_FL_ENABLED
;
1070 return ftrace_make_call(rec
, ftrace_addr
);
1073 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1074 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1077 static void ftrace_replace_code(int enable
)
1079 struct dyn_ftrace
*rec
;
1080 struct ftrace_page
*pg
;
1083 do_for_each_ftrace_rec(pg
, rec
) {
1085 * Skip over free records, records that have
1086 * failed and not converted.
1088 if (rec
->flags
& FTRACE_FL_FREE
||
1089 rec
->flags
& FTRACE_FL_FAILED
||
1090 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1093 failed
= __ftrace_replace_code(rec
, enable
);
1095 rec
->flags
|= FTRACE_FL_FAILED
;
1096 ftrace_bug(failed
, rec
->ip
);
1097 /* Stop processing */
1100 } while_for_each_ftrace_rec();
1104 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1111 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1113 ftrace_bug(ret
, ip
);
1114 rec
->flags
|= FTRACE_FL_FAILED
;
1121 * archs can override this function if they must do something
1122 * before the modifying code is performed.
1124 int __weak
ftrace_arch_code_modify_prepare(void)
1130 * archs can override this function if they must do something
1131 * after the modifying code is performed.
1133 int __weak
ftrace_arch_code_modify_post_process(void)
1138 static int __ftrace_modify_code(void *data
)
1140 int *command
= data
;
1142 if (*command
& FTRACE_ENABLE_CALLS
)
1143 ftrace_replace_code(1);
1144 else if (*command
& FTRACE_DISABLE_CALLS
)
1145 ftrace_replace_code(0);
1147 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1148 ftrace_update_ftrace_func(ftrace_trace_function
);
1150 if (*command
& FTRACE_START_FUNC_RET
)
1151 ftrace_enable_ftrace_graph_caller();
1152 else if (*command
& FTRACE_STOP_FUNC_RET
)
1153 ftrace_disable_ftrace_graph_caller();
1158 static void ftrace_run_update_code(int command
)
1162 ret
= ftrace_arch_code_modify_prepare();
1163 FTRACE_WARN_ON(ret
);
1167 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1169 ret
= ftrace_arch_code_modify_post_process();
1170 FTRACE_WARN_ON(ret
);
1173 static ftrace_func_t saved_ftrace_func
;
1174 static int ftrace_start_up
;
1176 static void ftrace_startup_enable(int command
)
1178 if (saved_ftrace_func
!= ftrace_trace_function
) {
1179 saved_ftrace_func
= ftrace_trace_function
;
1180 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1183 if (!command
|| !ftrace_enabled
)
1186 ftrace_run_update_code(command
);
1189 static void ftrace_startup(int command
)
1191 if (unlikely(ftrace_disabled
))
1195 command
|= FTRACE_ENABLE_CALLS
;
1197 ftrace_startup_enable(command
);
1200 static void ftrace_shutdown(int command
)
1202 if (unlikely(ftrace_disabled
))
1207 * Just warn in case of unbalance, no need to kill ftrace, it's not
1208 * critical but the ftrace_call callers may be never nopped again after
1209 * further ftrace uses.
1211 WARN_ON_ONCE(ftrace_start_up
< 0);
1213 if (!ftrace_start_up
)
1214 command
|= FTRACE_DISABLE_CALLS
;
1216 if (saved_ftrace_func
!= ftrace_trace_function
) {
1217 saved_ftrace_func
= ftrace_trace_function
;
1218 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1221 if (!command
|| !ftrace_enabled
)
1224 ftrace_run_update_code(command
);
1227 static void ftrace_startup_sysctl(void)
1229 int command
= FTRACE_ENABLE_MCOUNT
;
1231 if (unlikely(ftrace_disabled
))
1234 /* Force update next time */
1235 saved_ftrace_func
= NULL
;
1236 /* ftrace_start_up is true if we want ftrace running */
1237 if (ftrace_start_up
)
1238 command
|= FTRACE_ENABLE_CALLS
;
1240 ftrace_run_update_code(command
);
1243 static void ftrace_shutdown_sysctl(void)
1245 int command
= FTRACE_DISABLE_MCOUNT
;
1247 if (unlikely(ftrace_disabled
))
1250 /* ftrace_start_up is true if ftrace is running */
1251 if (ftrace_start_up
)
1252 command
|= FTRACE_DISABLE_CALLS
;
1254 ftrace_run_update_code(command
);
1257 static cycle_t ftrace_update_time
;
1258 static unsigned long ftrace_update_cnt
;
1259 unsigned long ftrace_update_tot_cnt
;
1261 static int ftrace_update_code(struct module
*mod
)
1263 struct dyn_ftrace
*p
;
1264 cycle_t start
, stop
;
1266 start
= ftrace_now(raw_smp_processor_id());
1267 ftrace_update_cnt
= 0;
1269 while (ftrace_new_addrs
) {
1271 /* If something went wrong, bail without enabling anything */
1272 if (unlikely(ftrace_disabled
))
1275 p
= ftrace_new_addrs
;
1276 ftrace_new_addrs
= p
->newlist
;
1280 * Do the initial record convertion from mcount jump
1281 * to the NOP instructions.
1283 if (!ftrace_code_disable(mod
, p
)) {
1288 p
->flags
|= FTRACE_FL_CONVERTED
;
1289 ftrace_update_cnt
++;
1292 * If the tracing is enabled, go ahead and enable the record.
1294 * The reason not to enable the record immediatelly is the
1295 * inherent check of ftrace_make_nop/ftrace_make_call for
1296 * correct previous instructions. Making first the NOP
1297 * conversion puts the module to the correct state, thus
1298 * passing the ftrace_make_call check.
1300 if (ftrace_start_up
) {
1301 int failed
= __ftrace_replace_code(p
, 1);
1303 ftrace_bug(failed
, p
->ip
);
1309 stop
= ftrace_now(raw_smp_processor_id());
1310 ftrace_update_time
= stop
- start
;
1311 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1316 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1318 struct ftrace_page
*pg
;
1322 /* allocate a few pages */
1323 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1324 if (!ftrace_pages_start
)
1328 * Allocate a few more pages.
1330 * TODO: have some parser search vmlinux before
1331 * final linking to find all calls to ftrace.
1333 * a) know how many pages to allocate.
1335 * b) set up the table then.
1337 * The dynamic code is still necessary for
1341 pg
= ftrace_pages
= ftrace_pages_start
;
1343 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1344 pr_info("ftrace: allocating %ld entries in %d pages\n",
1345 num_to_init
, cnt
+ 1);
1347 for (i
= 0; i
< cnt
; i
++) {
1348 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1350 /* If we fail, we'll try later anyway */
1361 FTRACE_ITER_FILTER
= (1 << 0),
1362 FTRACE_ITER_NOTRACE
= (1 << 1),
1363 FTRACE_ITER_FAILURES
= (1 << 2),
1364 FTRACE_ITER_PRINTALL
= (1 << 3),
1365 FTRACE_ITER_HASH
= (1 << 4),
1368 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370 struct ftrace_iterator
{
1373 struct ftrace_page
*pg
;
1374 struct dyn_ftrace
*func
;
1375 struct ftrace_func_probe
*probe
;
1376 struct trace_parser parser
;
1383 t_hash_next(struct seq_file
*m
, loff_t
*pos
)
1385 struct ftrace_iterator
*iter
= m
->private;
1386 struct hlist_node
*hnd
= NULL
;
1387 struct hlist_head
*hhd
;
1393 hnd
= &iter
->probe
->node
;
1395 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1398 hhd
= &ftrace_func_hash
[iter
->hidx
];
1400 if (hlist_empty(hhd
)) {
1416 if (WARN_ON_ONCE(!hnd
))
1419 iter
->probe
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1424 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1426 struct ftrace_iterator
*iter
= m
->private;
1430 if (iter
->func_pos
> *pos
)
1434 for (l
= 0; l
<= (*pos
- iter
->func_pos
); ) {
1435 p
= t_hash_next(m
, &l
);
1442 /* Only set this if we have an item */
1443 iter
->flags
|= FTRACE_ITER_HASH
;
1449 t_hash_show(struct seq_file
*m
, struct ftrace_iterator
*iter
)
1451 struct ftrace_func_probe
*rec
;
1454 if (WARN_ON_ONCE(!rec
))
1457 if (rec
->ops
->print
)
1458 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1460 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1463 seq_printf(m
, ":%p", rec
->data
);
1470 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1472 struct ftrace_iterator
*iter
= m
->private;
1473 struct dyn_ftrace
*rec
= NULL
;
1475 if (iter
->flags
& FTRACE_ITER_HASH
)
1476 return t_hash_next(m
, pos
);
1481 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1482 return t_hash_start(m
, pos
);
1485 if (iter
->idx
>= iter
->pg
->index
) {
1486 if (iter
->pg
->next
) {
1487 iter
->pg
= iter
->pg
->next
;
1492 rec
= &iter
->pg
->records
[iter
->idx
++];
1493 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1495 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1496 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1498 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1499 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1501 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1502 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1504 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1505 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1512 return t_hash_start(m
, pos
);
1514 iter
->func_pos
= *pos
;
1520 static void reset_iter_read(struct ftrace_iterator
*iter
)
1524 iter
->flags
&= ~(FTRACE_ITER_PRINTALL
& FTRACE_ITER_HASH
);
1527 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1529 struct ftrace_iterator
*iter
= m
->private;
1533 mutex_lock(&ftrace_lock
);
1535 * If an lseek was done, then reset and start from beginning.
1537 if (*pos
< iter
->pos
)
1538 reset_iter_read(iter
);
1541 * For set_ftrace_filter reading, if we have the filter
1542 * off, we can short cut and just print out that all
1543 * functions are enabled.
1545 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1547 return t_hash_start(m
, pos
);
1548 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1549 /* reset in case of seek/pread */
1550 iter
->flags
&= ~FTRACE_ITER_HASH
;
1554 if (iter
->flags
& FTRACE_ITER_HASH
)
1555 return t_hash_start(m
, pos
);
1558 * Unfortunately, we need to restart at ftrace_pages_start
1559 * every time we let go of the ftrace_mutex. This is because
1560 * those pointers can change without the lock.
1562 iter
->pg
= ftrace_pages_start
;
1564 for (l
= 0; l
<= *pos
; ) {
1565 p
= t_next(m
, p
, &l
);
1571 if (iter
->flags
& FTRACE_ITER_FILTER
)
1572 return t_hash_start(m
, pos
);
1580 static void t_stop(struct seq_file
*m
, void *p
)
1582 mutex_unlock(&ftrace_lock
);
1585 static int t_show(struct seq_file
*m
, void *v
)
1587 struct ftrace_iterator
*iter
= m
->private;
1588 struct dyn_ftrace
*rec
;
1590 if (iter
->flags
& FTRACE_ITER_HASH
)
1591 return t_hash_show(m
, iter
);
1593 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1594 seq_printf(m
, "#### all functions enabled ####\n");
1603 seq_printf(m
, "%ps\n", (void *)rec
->ip
);
1608 static const struct seq_operations show_ftrace_seq_ops
= {
1616 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1618 struct ftrace_iterator
*iter
;
1621 if (unlikely(ftrace_disabled
))
1624 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1628 iter
->pg
= ftrace_pages_start
;
1630 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1632 struct seq_file
*m
= file
->private_data
;
1643 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1647 struct ftrace_iterator
*iter
;
1649 ret
= ftrace_avail_open(inode
, file
);
1651 m
= (struct seq_file
*)file
->private_data
;
1652 iter
= (struct ftrace_iterator
*)m
->private;
1653 iter
->flags
= FTRACE_ITER_FAILURES
;
1660 static void ftrace_filter_reset(int enable
)
1662 struct ftrace_page
*pg
;
1663 struct dyn_ftrace
*rec
;
1664 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1666 mutex_lock(&ftrace_lock
);
1668 ftrace_filtered
= 0;
1669 do_for_each_ftrace_rec(pg
, rec
) {
1670 if (rec
->flags
& FTRACE_FL_FAILED
)
1672 rec
->flags
&= ~type
;
1673 } while_for_each_ftrace_rec();
1674 mutex_unlock(&ftrace_lock
);
1678 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1680 struct ftrace_iterator
*iter
;
1683 if (unlikely(ftrace_disabled
))
1686 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1690 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
1695 mutex_lock(&ftrace_regex_lock
);
1696 if ((file
->f_mode
& FMODE_WRITE
) &&
1697 (file
->f_flags
& O_TRUNC
))
1698 ftrace_filter_reset(enable
);
1700 if (file
->f_mode
& FMODE_READ
) {
1701 iter
->pg
= ftrace_pages_start
;
1702 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1703 FTRACE_ITER_NOTRACE
;
1705 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1707 struct seq_file
*m
= file
->private_data
;
1710 trace_parser_put(&iter
->parser
);
1714 file
->private_data
= iter
;
1715 mutex_unlock(&ftrace_regex_lock
);
1721 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1723 return ftrace_regex_open(inode
, file
, 1);
1727 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1729 return ftrace_regex_open(inode
, file
, 0);
1733 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1737 if (file
->f_mode
& FMODE_READ
)
1738 ret
= seq_lseek(file
, offset
, origin
);
1740 file
->f_pos
= ret
= 1;
1745 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1752 if (strcmp(str
, regex
) == 0)
1755 case MATCH_FRONT_ONLY
:
1756 if (strncmp(str
, regex
, len
) == 0)
1759 case MATCH_MIDDLE_ONLY
:
1760 if (strstr(str
, regex
))
1763 case MATCH_END_ONLY
:
1765 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
1774 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1776 char str
[KSYM_SYMBOL_LEN
];
1778 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1779 return ftrace_match(str
, regex
, len
, type
);
1782 static int ftrace_match_records(char *buff
, int len
, int enable
)
1784 unsigned int search_len
;
1785 struct ftrace_page
*pg
;
1786 struct dyn_ftrace
*rec
;
1793 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1794 type
= filter_parse_regex(buff
, len
, &search
, ¬);
1796 search_len
= strlen(search
);
1798 mutex_lock(&ftrace_lock
);
1799 do_for_each_ftrace_rec(pg
, rec
) {
1801 if (rec
->flags
& FTRACE_FL_FAILED
)
1804 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1806 rec
->flags
&= ~flag
;
1812 * Only enable filtering if we have a function that
1815 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1816 ftrace_filtered
= 1;
1817 } while_for_each_ftrace_rec();
1818 mutex_unlock(&ftrace_lock
);
1824 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1825 char *regex
, int len
, int type
)
1827 char str
[KSYM_SYMBOL_LEN
];
1830 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1832 if (!modname
|| strcmp(modname
, mod
))
1835 /* blank search means to match all funcs in the mod */
1837 return ftrace_match(str
, regex
, len
, type
);
1842 static int ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1844 unsigned search_len
= 0;
1845 struct ftrace_page
*pg
;
1846 struct dyn_ftrace
*rec
;
1847 int type
= MATCH_FULL
;
1848 char *search
= buff
;
1853 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1855 /* blank or '*' mean the same */
1856 if (strcmp(buff
, "*") == 0)
1859 /* handle the case of 'dont filter this module' */
1860 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1866 type
= filter_parse_regex(buff
, strlen(buff
), &search
, ¬);
1867 search_len
= strlen(search
);
1870 mutex_lock(&ftrace_lock
);
1871 do_for_each_ftrace_rec(pg
, rec
) {
1873 if (rec
->flags
& FTRACE_FL_FAILED
)
1876 if (ftrace_match_module_record(rec
, mod
,
1877 search
, search_len
, type
)) {
1879 rec
->flags
&= ~flag
;
1884 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1885 ftrace_filtered
= 1;
1887 } while_for_each_ftrace_rec();
1888 mutex_unlock(&ftrace_lock
);
1894 * We register the module command as a template to show others how
1895 * to register the a command as well.
1899 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1904 * cmd == 'mod' because we only registered this func
1905 * for the 'mod' ftrace_func_command.
1906 * But if you register one func with multiple commands,
1907 * you can tell which command was used by the cmd
1911 /* we must have a module name */
1915 mod
= strsep(¶m
, ":");
1919 if (ftrace_match_module_records(func
, mod
, enable
))
1924 static struct ftrace_func_command ftrace_mod_cmd
= {
1926 .func
= ftrace_mod_callback
,
1929 static int __init
ftrace_mod_cmd_init(void)
1931 return register_ftrace_command(&ftrace_mod_cmd
);
1933 device_initcall(ftrace_mod_cmd_init
);
1936 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1938 struct ftrace_func_probe
*entry
;
1939 struct hlist_head
*hhd
;
1940 struct hlist_node
*n
;
1943 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1945 hhd
= &ftrace_func_hash
[key
];
1947 if (hlist_empty(hhd
))
1951 * Disable preemption for these calls to prevent a RCU grace
1952 * period. This syncs the hash iteration and freeing of items
1953 * on the hash. rcu_read_lock is too dangerous here.
1955 preempt_disable_notrace();
1956 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1957 if (entry
->ip
== ip
)
1958 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1960 preempt_enable_notrace();
1963 static struct ftrace_ops trace_probe_ops __read_mostly
=
1965 .func
= function_trace_probe_call
,
1968 static int ftrace_probe_registered
;
1970 static void __enable_ftrace_function_probe(void)
1974 if (ftrace_probe_registered
)
1977 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1978 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1982 /* Nothing registered? */
1983 if (i
== FTRACE_FUNC_HASHSIZE
)
1986 __register_ftrace_function(&trace_probe_ops
);
1988 ftrace_probe_registered
= 1;
1991 static void __disable_ftrace_function_probe(void)
1995 if (!ftrace_probe_registered
)
1998 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1999 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2004 /* no more funcs left */
2005 __unregister_ftrace_function(&trace_probe_ops
);
2007 ftrace_probe_registered
= 0;
2011 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2013 struct ftrace_func_probe
*entry
=
2014 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2016 if (entry
->ops
->free
)
2017 entry
->ops
->free(&entry
->data
);
2023 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2026 struct ftrace_func_probe
*entry
;
2027 struct ftrace_page
*pg
;
2028 struct dyn_ftrace
*rec
;
2034 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2035 len
= strlen(search
);
2037 /* we do not support '!' for function probes */
2041 mutex_lock(&ftrace_lock
);
2042 do_for_each_ftrace_rec(pg
, rec
) {
2044 if (rec
->flags
& FTRACE_FL_FAILED
)
2047 if (!ftrace_match_record(rec
, search
, len
, type
))
2050 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2052 /* If we did not process any, then return error */
2063 * The caller might want to do something special
2064 * for each function we find. We call the callback
2065 * to give the caller an opportunity to do so.
2067 if (ops
->callback
) {
2068 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2069 /* caller does not like this func */
2076 entry
->ip
= rec
->ip
;
2078 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2079 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2081 } while_for_each_ftrace_rec();
2082 __enable_ftrace_function_probe();
2085 mutex_unlock(&ftrace_lock
);
2091 PROBE_TEST_FUNC
= 1,
2096 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2097 void *data
, int flags
)
2099 struct ftrace_func_probe
*entry
;
2100 struct hlist_node
*n
, *tmp
;
2101 char str
[KSYM_SYMBOL_LEN
];
2102 int type
= MATCH_FULL
;
2106 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2111 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2112 len
= strlen(search
);
2114 /* we do not support '!' for function probes */
2119 mutex_lock(&ftrace_lock
);
2120 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2121 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2123 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2125 /* break up if statements for readability */
2126 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2129 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2132 /* do this last, since it is the most expensive */
2134 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2136 if (!ftrace_match(str
, glob
, len
, type
))
2140 hlist_del(&entry
->node
);
2141 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2144 __disable_ftrace_function_probe();
2145 mutex_unlock(&ftrace_lock
);
2149 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2152 __unregister_ftrace_function_probe(glob
, ops
, data
,
2153 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2157 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2159 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2162 void unregister_ftrace_function_probe_all(char *glob
)
2164 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2167 static LIST_HEAD(ftrace_commands
);
2168 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2170 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2172 struct ftrace_func_command
*p
;
2175 mutex_lock(&ftrace_cmd_mutex
);
2176 list_for_each_entry(p
, &ftrace_commands
, list
) {
2177 if (strcmp(cmd
->name
, p
->name
) == 0) {
2182 list_add(&cmd
->list
, &ftrace_commands
);
2184 mutex_unlock(&ftrace_cmd_mutex
);
2189 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2191 struct ftrace_func_command
*p
, *n
;
2194 mutex_lock(&ftrace_cmd_mutex
);
2195 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2196 if (strcmp(cmd
->name
, p
->name
) == 0) {
2198 list_del_init(&p
->list
);
2203 mutex_unlock(&ftrace_cmd_mutex
);
2208 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2210 char *func
, *command
, *next
= buff
;
2211 struct ftrace_func_command
*p
;
2214 func
= strsep(&next
, ":");
2217 if (ftrace_match_records(func
, len
, enable
))
2224 command
= strsep(&next
, ":");
2226 mutex_lock(&ftrace_cmd_mutex
);
2227 list_for_each_entry(p
, &ftrace_commands
, list
) {
2228 if (strcmp(p
->name
, command
) == 0) {
2229 ret
= p
->func(func
, command
, next
, enable
);
2234 mutex_unlock(&ftrace_cmd_mutex
);
2240 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2241 size_t cnt
, loff_t
*ppos
, int enable
)
2243 struct ftrace_iterator
*iter
;
2244 struct trace_parser
*parser
;
2250 mutex_lock(&ftrace_regex_lock
);
2252 if (file
->f_mode
& FMODE_READ
) {
2253 struct seq_file
*m
= file
->private_data
;
2256 iter
= file
->private_data
;
2258 parser
= &iter
->parser
;
2259 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2261 if (read
>= 0 && trace_parser_loaded(parser
) &&
2262 !trace_parser_cont(parser
)) {
2263 ret
= ftrace_process_regex(parser
->buffer
,
2264 parser
->idx
, enable
);
2265 trace_parser_clear(parser
);
2272 mutex_unlock(&ftrace_regex_lock
);
2278 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2279 size_t cnt
, loff_t
*ppos
)
2281 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2285 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2286 size_t cnt
, loff_t
*ppos
)
2288 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2292 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2294 if (unlikely(ftrace_disabled
))
2297 mutex_lock(&ftrace_regex_lock
);
2299 ftrace_filter_reset(enable
);
2301 ftrace_match_records(buf
, len
, enable
);
2302 mutex_unlock(&ftrace_regex_lock
);
2306 * ftrace_set_filter - set a function to filter on in ftrace
2307 * @buf - the string that holds the function filter text.
2308 * @len - the length of the string.
2309 * @reset - non zero to reset all filters before applying this filter.
2311 * Filters denote which functions should be enabled when tracing is enabled.
2312 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2314 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2316 ftrace_set_regex(buf
, len
, reset
, 1);
2320 * ftrace_set_notrace - set a function to not trace in ftrace
2321 * @buf - the string that holds the function notrace text.
2322 * @len - the length of the string.
2323 * @reset - non zero to reset all filters before applying this filter.
2325 * Notrace Filters denote which functions should not be enabled when tracing
2326 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2329 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2331 ftrace_set_regex(buf
, len
, reset
, 0);
2335 * command line interface to allow users to set filters on boot up.
2337 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2338 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2339 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2341 static int __init
set_ftrace_notrace(char *str
)
2343 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2346 __setup("ftrace_notrace=", set_ftrace_notrace
);
2348 static int __init
set_ftrace_filter(char *str
)
2350 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2353 __setup("ftrace_filter=", set_ftrace_filter
);
2355 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2356 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2357 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
2359 static int __init
set_graph_function(char *str
)
2361 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2364 __setup("ftrace_graph_filter=", set_graph_function
);
2366 static void __init
set_ftrace_early_graph(char *buf
)
2372 func
= strsep(&buf
, ",");
2373 /* we allow only one expression at a time */
2374 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2377 printk(KERN_DEBUG
"ftrace: function %s not "
2378 "traceable\n", func
);
2381 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2383 static void __init
set_ftrace_early_filter(char *buf
, int enable
)
2388 func
= strsep(&buf
, ",");
2389 ftrace_set_regex(func
, strlen(func
), 0, enable
);
2393 static void __init
set_ftrace_early_filters(void)
2395 if (ftrace_filter_buf
[0])
2396 set_ftrace_early_filter(ftrace_filter_buf
, 1);
2397 if (ftrace_notrace_buf
[0])
2398 set_ftrace_early_filter(ftrace_notrace_buf
, 0);
2399 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2400 if (ftrace_graph_buf
[0])
2401 set_ftrace_early_graph(ftrace_graph_buf
);
2402 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2406 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2408 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2409 struct ftrace_iterator
*iter
;
2410 struct trace_parser
*parser
;
2412 mutex_lock(&ftrace_regex_lock
);
2413 if (file
->f_mode
& FMODE_READ
) {
2416 seq_release(inode
, file
);
2418 iter
= file
->private_data
;
2420 parser
= &iter
->parser
;
2421 if (trace_parser_loaded(parser
)) {
2422 parser
->buffer
[parser
->idx
] = 0;
2423 ftrace_match_records(parser
->buffer
, parser
->idx
, enable
);
2426 mutex_lock(&ftrace_lock
);
2427 if (ftrace_start_up
&& ftrace_enabled
)
2428 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2429 mutex_unlock(&ftrace_lock
);
2431 trace_parser_put(parser
);
2434 mutex_unlock(&ftrace_regex_lock
);
2439 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2441 return ftrace_regex_release(inode
, file
, 1);
2445 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2447 return ftrace_regex_release(inode
, file
, 0);
2450 static const struct file_operations ftrace_avail_fops
= {
2451 .open
= ftrace_avail_open
,
2453 .llseek
= seq_lseek
,
2454 .release
= seq_release_private
,
2457 static const struct file_operations ftrace_failures_fops
= {
2458 .open
= ftrace_failures_open
,
2460 .llseek
= seq_lseek
,
2461 .release
= seq_release_private
,
2464 static const struct file_operations ftrace_filter_fops
= {
2465 .open
= ftrace_filter_open
,
2467 .write
= ftrace_filter_write
,
2468 .llseek
= ftrace_regex_lseek
,
2469 .release
= ftrace_filter_release
,
2472 static const struct file_operations ftrace_notrace_fops
= {
2473 .open
= ftrace_notrace_open
,
2475 .write
= ftrace_notrace_write
,
2476 .llseek
= ftrace_regex_lseek
,
2477 .release
= ftrace_notrace_release
,
2480 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2482 static DEFINE_MUTEX(graph_lock
);
2484 int ftrace_graph_count
;
2485 int ftrace_graph_filter_enabled
;
2486 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2489 __g_next(struct seq_file
*m
, loff_t
*pos
)
2491 if (*pos
>= ftrace_graph_count
)
2493 return &ftrace_graph_funcs
[*pos
];
2497 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2500 return __g_next(m
, pos
);
2503 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2505 mutex_lock(&graph_lock
);
2507 /* Nothing, tell g_show to print all functions are enabled */
2508 if (!ftrace_graph_filter_enabled
&& !*pos
)
2511 return __g_next(m
, pos
);
2514 static void g_stop(struct seq_file
*m
, void *p
)
2516 mutex_unlock(&graph_lock
);
2519 static int g_show(struct seq_file
*m
, void *v
)
2521 unsigned long *ptr
= v
;
2526 if (ptr
== (unsigned long *)1) {
2527 seq_printf(m
, "#### all functions enabled ####\n");
2531 seq_printf(m
, "%ps\n", (void *)*ptr
);
2536 static const struct seq_operations ftrace_graph_seq_ops
= {
2544 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2548 if (unlikely(ftrace_disabled
))
2551 mutex_lock(&graph_lock
);
2552 if ((file
->f_mode
& FMODE_WRITE
) &&
2553 (file
->f_flags
& O_TRUNC
)) {
2554 ftrace_graph_filter_enabled
= 0;
2555 ftrace_graph_count
= 0;
2556 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2558 mutex_unlock(&graph_lock
);
2560 if (file
->f_mode
& FMODE_READ
)
2561 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2567 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
2569 if (file
->f_mode
& FMODE_READ
)
2570 seq_release(inode
, file
);
2575 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2577 struct dyn_ftrace
*rec
;
2578 struct ftrace_page
*pg
;
2586 if (ftrace_disabled
)
2590 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
2591 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2594 search_len
= strlen(search
);
2596 mutex_lock(&ftrace_lock
);
2597 do_for_each_ftrace_rec(pg
, rec
) {
2599 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2602 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2603 /* if it is in the array */
2605 for (i
= 0; i
< *idx
; i
++) {
2606 if (array
[i
] == rec
->ip
) {
2615 array
[(*idx
)++] = rec
->ip
;
2616 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2621 array
[i
] = array
[--(*idx
)];
2627 } while_for_each_ftrace_rec();
2629 mutex_unlock(&ftrace_lock
);
2634 ftrace_graph_filter_enabled
= 1;
2639 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2640 size_t cnt
, loff_t
*ppos
)
2642 struct trace_parser parser
;
2648 mutex_lock(&graph_lock
);
2650 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
2655 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
2657 if (read
>= 0 && trace_parser_loaded((&parser
))) {
2658 parser
.buffer
[parser
.idx
] = 0;
2660 /* we allow only one expression at a time */
2661 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2670 trace_parser_put(&parser
);
2672 mutex_unlock(&graph_lock
);
2677 static const struct file_operations ftrace_graph_fops
= {
2678 .open
= ftrace_graph_open
,
2680 .write
= ftrace_graph_write
,
2681 .release
= ftrace_graph_release
,
2683 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2685 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2688 trace_create_file("available_filter_functions", 0444,
2689 d_tracer
, NULL
, &ftrace_avail_fops
);
2691 trace_create_file("failures", 0444,
2692 d_tracer
, NULL
, &ftrace_failures_fops
);
2694 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
2695 NULL
, &ftrace_filter_fops
);
2697 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
2698 NULL
, &ftrace_notrace_fops
);
2700 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2701 trace_create_file("set_graph_function", 0444, d_tracer
,
2703 &ftrace_graph_fops
);
2704 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2709 static int ftrace_process_locs(struct module
*mod
,
2710 unsigned long *start
,
2715 unsigned long flags
;
2717 mutex_lock(&ftrace_lock
);
2720 addr
= ftrace_call_adjust(*p
++);
2722 * Some architecture linkers will pad between
2723 * the different mcount_loc sections of different
2724 * object files to satisfy alignments.
2725 * Skip any NULL pointers.
2729 ftrace_record_ip(addr
);
2732 /* disable interrupts to prevent kstop machine */
2733 local_irq_save(flags
);
2734 ftrace_update_code(mod
);
2735 local_irq_restore(flags
);
2736 mutex_unlock(&ftrace_lock
);
2741 #ifdef CONFIG_MODULES
2742 void ftrace_release_mod(struct module
*mod
)
2744 struct dyn_ftrace
*rec
;
2745 struct ftrace_page
*pg
;
2747 if (ftrace_disabled
)
2750 mutex_lock(&ftrace_lock
);
2751 do_for_each_ftrace_rec(pg
, rec
) {
2752 if (within_module_core(rec
->ip
, mod
)) {
2754 * rec->ip is changed in ftrace_free_rec()
2755 * It should not between s and e if record was freed.
2757 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
2758 ftrace_free_rec(rec
);
2760 } while_for_each_ftrace_rec();
2761 mutex_unlock(&ftrace_lock
);
2764 static void ftrace_init_module(struct module
*mod
,
2765 unsigned long *start
, unsigned long *end
)
2767 if (ftrace_disabled
|| start
== end
)
2769 ftrace_process_locs(mod
, start
, end
);
2772 static int ftrace_module_notify(struct notifier_block
*self
,
2773 unsigned long val
, void *data
)
2775 struct module
*mod
= data
;
2778 case MODULE_STATE_COMING
:
2779 ftrace_init_module(mod
, mod
->ftrace_callsites
,
2780 mod
->ftrace_callsites
+
2781 mod
->num_ftrace_callsites
);
2783 case MODULE_STATE_GOING
:
2784 ftrace_release_mod(mod
);
2791 static int ftrace_module_notify(struct notifier_block
*self
,
2792 unsigned long val
, void *data
)
2796 #endif /* CONFIG_MODULES */
2798 struct notifier_block ftrace_module_nb
= {
2799 .notifier_call
= ftrace_module_notify
,
2803 extern unsigned long __start_mcount_loc
[];
2804 extern unsigned long __stop_mcount_loc
[];
2806 void __init
ftrace_init(void)
2808 unsigned long count
, addr
, flags
;
2811 /* Keep the ftrace pointer to the stub */
2812 addr
= (unsigned long)ftrace_stub
;
2814 local_irq_save(flags
);
2815 ftrace_dyn_arch_init(&addr
);
2816 local_irq_restore(flags
);
2818 /* ftrace_dyn_arch_init places the return code in addr */
2822 count
= __stop_mcount_loc
- __start_mcount_loc
;
2824 ret
= ftrace_dyn_table_alloc(count
);
2828 last_ftrace_enabled
= ftrace_enabled
= 1;
2830 ret
= ftrace_process_locs(NULL
,
2834 ret
= register_module_notifier(&ftrace_module_nb
);
2836 pr_warning("Failed to register trace ftrace module notifier\n");
2838 set_ftrace_early_filters();
2842 ftrace_disabled
= 1;
2847 static int __init
ftrace_nodyn_init(void)
2852 device_initcall(ftrace_nodyn_init
);
2854 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2855 static inline void ftrace_startup_enable(int command
) { }
2856 /* Keep as macros so we do not need to define the commands */
2857 # define ftrace_startup(command) do { } while (0)
2858 # define ftrace_shutdown(command) do { } while (0)
2859 # define ftrace_startup_sysctl() do { } while (0)
2860 # define ftrace_shutdown_sysctl() do { } while (0)
2861 #endif /* CONFIG_DYNAMIC_FTRACE */
2863 static void clear_ftrace_swapper(void)
2865 struct task_struct
*p
;
2869 for_each_online_cpu(cpu
) {
2871 clear_tsk_trace_trace(p
);
2876 static void set_ftrace_swapper(void)
2878 struct task_struct
*p
;
2882 for_each_online_cpu(cpu
) {
2884 set_tsk_trace_trace(p
);
2889 static void clear_ftrace_pid(struct pid
*pid
)
2891 struct task_struct
*p
;
2894 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2895 clear_tsk_trace_trace(p
);
2896 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2902 static void set_ftrace_pid(struct pid
*pid
)
2904 struct task_struct
*p
;
2907 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2908 set_tsk_trace_trace(p
);
2909 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2913 static void clear_ftrace_pid_task(struct pid
*pid
)
2915 if (pid
== ftrace_swapper_pid
)
2916 clear_ftrace_swapper();
2918 clear_ftrace_pid(pid
);
2921 static void set_ftrace_pid_task(struct pid
*pid
)
2923 if (pid
== ftrace_swapper_pid
)
2924 set_ftrace_swapper();
2926 set_ftrace_pid(pid
);
2929 static int ftrace_pid_add(int p
)
2932 struct ftrace_pid
*fpid
;
2935 mutex_lock(&ftrace_lock
);
2938 pid
= ftrace_swapper_pid
;
2940 pid
= find_get_pid(p
);
2947 list_for_each_entry(fpid
, &ftrace_pids
, list
)
2948 if (fpid
->pid
== pid
)
2953 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
2957 list_add(&fpid
->list
, &ftrace_pids
);
2960 set_ftrace_pid_task(pid
);
2962 ftrace_update_pid_func();
2963 ftrace_startup_enable(0);
2965 mutex_unlock(&ftrace_lock
);
2969 if (pid
!= ftrace_swapper_pid
)
2973 mutex_unlock(&ftrace_lock
);
2977 static void ftrace_pid_reset(void)
2979 struct ftrace_pid
*fpid
, *safe
;
2981 mutex_lock(&ftrace_lock
);
2982 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
2983 struct pid
*pid
= fpid
->pid
;
2985 clear_ftrace_pid_task(pid
);
2987 list_del(&fpid
->list
);
2991 ftrace_update_pid_func();
2992 ftrace_startup_enable(0);
2994 mutex_unlock(&ftrace_lock
);
2997 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
2999 mutex_lock(&ftrace_lock
);
3001 if (list_empty(&ftrace_pids
) && (!*pos
))
3004 return seq_list_start(&ftrace_pids
, *pos
);
3007 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
3012 return seq_list_next(v
, &ftrace_pids
, pos
);
3015 static void fpid_stop(struct seq_file
*m
, void *p
)
3017 mutex_unlock(&ftrace_lock
);
3020 static int fpid_show(struct seq_file
*m
, void *v
)
3022 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
3024 if (v
== (void *)1) {
3025 seq_printf(m
, "no pid\n");
3029 if (fpid
->pid
== ftrace_swapper_pid
)
3030 seq_printf(m
, "swapper tasks\n");
3032 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
3037 static const struct seq_operations ftrace_pid_sops
= {
3038 .start
= fpid_start
,
3045 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
3049 if ((file
->f_mode
& FMODE_WRITE
) &&
3050 (file
->f_flags
& O_TRUNC
))
3053 if (file
->f_mode
& FMODE_READ
)
3054 ret
= seq_open(file
, &ftrace_pid_sops
);
3060 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
3061 size_t cnt
, loff_t
*ppos
)
3067 if (cnt
>= sizeof(buf
))
3070 if (copy_from_user(&buf
, ubuf
, cnt
))
3076 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3077 * to clean the filter quietly.
3079 tmp
= strstrip(buf
);
3080 if (strlen(tmp
) == 0)
3083 ret
= strict_strtol(tmp
, 10, &val
);
3087 ret
= ftrace_pid_add(val
);
3089 return ret
? ret
: cnt
;
3093 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3095 if (file
->f_mode
& FMODE_READ
)
3096 seq_release(inode
, file
);
3101 static const struct file_operations ftrace_pid_fops
= {
3102 .open
= ftrace_pid_open
,
3103 .write
= ftrace_pid_write
,
3105 .llseek
= seq_lseek
,
3106 .release
= ftrace_pid_release
,
3109 static __init
int ftrace_init_debugfs(void)
3111 struct dentry
*d_tracer
;
3113 d_tracer
= tracing_init_dentry();
3117 ftrace_init_dyn_debugfs(d_tracer
);
3119 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3120 NULL
, &ftrace_pid_fops
);
3122 ftrace_profile_debugfs(d_tracer
);
3126 fs_initcall(ftrace_init_debugfs
);
3129 * ftrace_kill - kill ftrace
3131 * This function should be used by panic code. It stops ftrace
3132 * but in a not so nice way. If you need to simply kill ftrace
3133 * from a non-atomic section, use ftrace_kill.
3135 void ftrace_kill(void)
3137 ftrace_disabled
= 1;
3139 clear_ftrace_function();
3143 * register_ftrace_function - register a function for profiling
3144 * @ops - ops structure that holds the function for profiling.
3146 * Register a function to be called by all functions in the
3149 * Note: @ops->func and all the functions it calls must be labeled
3150 * with "notrace", otherwise it will go into a
3153 int register_ftrace_function(struct ftrace_ops
*ops
)
3157 if (unlikely(ftrace_disabled
))
3160 mutex_lock(&ftrace_lock
);
3162 ret
= __register_ftrace_function(ops
);
3165 mutex_unlock(&ftrace_lock
);
3170 * unregister_ftrace_function - unregister a function for profiling.
3171 * @ops - ops structure that holds the function to unregister
3173 * Unregister a function that was added to be called by ftrace profiling.
3175 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3179 mutex_lock(&ftrace_lock
);
3180 ret
= __unregister_ftrace_function(ops
);
3182 mutex_unlock(&ftrace_lock
);
3188 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3189 void __user
*buffer
, size_t *lenp
,
3194 if (unlikely(ftrace_disabled
))
3197 mutex_lock(&ftrace_lock
);
3199 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3201 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3204 last_ftrace_enabled
= !!ftrace_enabled
;
3206 if (ftrace_enabled
) {
3208 ftrace_startup_sysctl();
3210 /* we are starting ftrace again */
3211 if (ftrace_list
!= &ftrace_list_end
) {
3212 if (ftrace_list
->next
== &ftrace_list_end
)
3213 ftrace_trace_function
= ftrace_list
->func
;
3215 ftrace_trace_function
= ftrace_list_func
;
3219 /* stopping ftrace calls (just send to ftrace_stub) */
3220 ftrace_trace_function
= ftrace_stub
;
3222 ftrace_shutdown_sysctl();
3226 mutex_unlock(&ftrace_lock
);
3230 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3232 static int ftrace_graph_active
;
3233 static struct notifier_block ftrace_suspend_notifier
;
3235 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3240 /* The callbacks that hook a function */
3241 trace_func_graph_ret_t ftrace_graph_return
=
3242 (trace_func_graph_ret_t
)ftrace_stub
;
3243 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3245 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3246 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3250 unsigned long flags
;
3251 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3252 struct task_struct
*g
, *t
;
3254 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3255 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3256 * sizeof(struct ftrace_ret_stack
),
3258 if (!ret_stack_list
[i
]) {
3266 read_lock_irqsave(&tasklist_lock
, flags
);
3267 do_each_thread(g
, t
) {
3273 if (t
->ret_stack
== NULL
) {
3274 atomic_set(&t
->tracing_graph_pause
, 0);
3275 atomic_set(&t
->trace_overrun
, 0);
3276 t
->curr_ret_stack
= -1;
3277 /* Make sure the tasks see the -1 first: */
3279 t
->ret_stack
= ret_stack_list
[start
++];
3281 } while_each_thread(g
, t
);
3284 read_unlock_irqrestore(&tasklist_lock
, flags
);
3286 for (i
= start
; i
< end
; i
++)
3287 kfree(ret_stack_list
[i
]);
3292 ftrace_graph_probe_sched_switch(void *ignore
,
3293 struct task_struct
*prev
, struct task_struct
*next
)
3295 unsigned long long timestamp
;
3299 * Does the user want to count the time a function was asleep.
3300 * If so, do not update the time stamps.
3302 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3305 timestamp
= trace_clock_local();
3307 prev
->ftrace_timestamp
= timestamp
;
3309 /* only process tasks that we timestamped */
3310 if (!next
->ftrace_timestamp
)
3314 * Update all the counters in next to make up for the
3315 * time next was sleeping.
3317 timestamp
-= next
->ftrace_timestamp
;
3319 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3320 next
->ret_stack
[index
].calltime
+= timestamp
;
3323 /* Allocate a return stack for each task */
3324 static int start_graph_tracing(void)
3326 struct ftrace_ret_stack
**ret_stack_list
;
3329 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3330 sizeof(struct ftrace_ret_stack
*),
3333 if (!ret_stack_list
)
3336 /* The cpu_boot init_task->ret_stack will never be freed */
3337 for_each_online_cpu(cpu
) {
3338 if (!idle_task(cpu
)->ret_stack
)
3339 ftrace_graph_init_task(idle_task(cpu
));
3343 ret
= alloc_retstack_tasklist(ret_stack_list
);
3344 } while (ret
== -EAGAIN
);
3347 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
3349 pr_info("ftrace_graph: Couldn't activate tracepoint"
3350 " probe to kernel_sched_switch\n");
3353 kfree(ret_stack_list
);
3358 * Hibernation protection.
3359 * The state of the current task is too much unstable during
3360 * suspend/restore to disk. We want to protect against that.
3363 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3367 case PM_HIBERNATION_PREPARE
:
3368 pause_graph_tracing();
3371 case PM_POST_HIBERNATION
:
3372 unpause_graph_tracing();
3378 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3379 trace_func_graph_ent_t entryfunc
)
3383 mutex_lock(&ftrace_lock
);
3385 /* we currently allow only one tracer registered at a time */
3386 if (ftrace_graph_active
) {
3391 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3392 register_pm_notifier(&ftrace_suspend_notifier
);
3394 ftrace_graph_active
++;
3395 ret
= start_graph_tracing();
3397 ftrace_graph_active
--;
3401 ftrace_graph_return
= retfunc
;
3402 ftrace_graph_entry
= entryfunc
;
3404 ftrace_startup(FTRACE_START_FUNC_RET
);
3407 mutex_unlock(&ftrace_lock
);
3411 void unregister_ftrace_graph(void)
3413 mutex_lock(&ftrace_lock
);
3415 if (unlikely(!ftrace_graph_active
))
3418 ftrace_graph_active
--;
3419 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3420 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3421 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3422 unregister_pm_notifier(&ftrace_suspend_notifier
);
3423 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
3426 mutex_unlock(&ftrace_lock
);
3429 /* Allocate a return stack for newly created task */
3430 void ftrace_graph_init_task(struct task_struct
*t
)
3432 /* Make sure we do not use the parent ret_stack */
3433 t
->ret_stack
= NULL
;
3434 t
->curr_ret_stack
= -1;
3436 if (ftrace_graph_active
) {
3437 struct ftrace_ret_stack
*ret_stack
;
3439 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3440 * sizeof(struct ftrace_ret_stack
),
3444 atomic_set(&t
->tracing_graph_pause
, 0);
3445 atomic_set(&t
->trace_overrun
, 0);
3446 t
->ftrace_timestamp
= 0;
3447 /* make curr_ret_stack visable before we add the ret_stack */
3449 t
->ret_stack
= ret_stack
;
3453 void ftrace_graph_exit_task(struct task_struct
*t
)
3455 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3457 t
->ret_stack
= NULL
;
3458 /* NULL must become visible to IRQs before we free it: */
3464 void ftrace_graph_stop(void)