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/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
46 #define FTRACE_WARN_ON_ONCE(cond) \
48 if (WARN_ON_ONCE(cond)) \
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly
;
58 static int last_ftrace_enabled
;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop
;
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
67 static int ftrace_disabled __read_mostly
;
69 static DEFINE_MUTEX(ftrace_lock
);
71 static struct ftrace_ops ftrace_list_end __read_mostly
=
76 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
77 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
78 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
79 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
81 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
82 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
85 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
87 struct ftrace_ops
*op
= ftrace_list
;
89 /* in case someone actually ports this to alpha! */
90 read_barrier_depends();
92 while (op
!= &ftrace_list_end
) {
94 read_barrier_depends();
95 op
->func(ip
, parent_ip
);
100 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
102 if (!test_tsk_trace_trace(current
))
105 ftrace_pid_function(ip
, parent_ip
);
108 static void set_ftrace_pid_function(ftrace_func_t func
)
110 /* do not set ftrace_pid_function to itself! */
111 if (func
!= ftrace_pid_func
)
112 ftrace_pid_function
= func
;
116 * clear_ftrace_function - reset the ftrace function
118 * This NULLs the ftrace function and in essence stops
119 * tracing. There may be lag
121 void clear_ftrace_function(void)
123 ftrace_trace_function
= ftrace_stub
;
124 __ftrace_trace_function
= ftrace_stub
;
125 ftrace_pid_function
= ftrace_stub
;
128 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
130 * For those archs that do not test ftrace_trace_stop in their
131 * mcount call site, we need to do it from C.
133 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
135 if (function_trace_stop
)
138 __ftrace_trace_function(ip
, parent_ip
);
142 static int __register_ftrace_function(struct ftrace_ops
*ops
)
144 ops
->next
= ftrace_list
;
146 * We are entering ops into the ftrace_list but another
147 * CPU might be walking that list. We need to make sure
148 * the ops->next pointer is valid before another CPU sees
149 * the ops pointer included into the ftrace_list.
154 if (ftrace_enabled
) {
157 if (ops
->next
== &ftrace_list_end
)
160 func
= ftrace_list_func
;
162 if (ftrace_pid_trace
) {
163 set_ftrace_pid_function(func
);
164 func
= ftrace_pid_func
;
168 * For one func, simply call it directly.
169 * For more than one func, call the chain.
171 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
172 ftrace_trace_function
= func
;
174 __ftrace_trace_function
= func
;
175 ftrace_trace_function
= ftrace_test_stop_func
;
182 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
184 struct ftrace_ops
**p
;
187 * If we are removing the last function, then simply point
188 * to the ftrace_stub.
190 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
191 ftrace_trace_function
= ftrace_stub
;
192 ftrace_list
= &ftrace_list_end
;
196 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
205 if (ftrace_enabled
) {
206 /* If we only have one func left, then call that directly */
207 if (ftrace_list
->next
== &ftrace_list_end
) {
208 ftrace_func_t func
= ftrace_list
->func
;
210 if (ftrace_pid_trace
) {
211 set_ftrace_pid_function(func
);
212 func
= ftrace_pid_func
;
214 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
215 ftrace_trace_function
= func
;
217 __ftrace_trace_function
= func
;
225 static void ftrace_update_pid_func(void)
229 if (ftrace_trace_function
== ftrace_stub
)
232 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
233 func
= ftrace_trace_function
;
235 func
= __ftrace_trace_function
;
238 if (ftrace_pid_trace
) {
239 set_ftrace_pid_function(func
);
240 func
= ftrace_pid_func
;
242 if (func
== ftrace_pid_func
)
243 func
= ftrace_pid_function
;
246 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
247 ftrace_trace_function
= func
;
249 __ftrace_trace_function
= func
;
253 #ifdef CONFIG_FUNCTION_PROFILER
254 struct ftrace_profile
{
255 struct hlist_node node
;
257 unsigned long counter
;
258 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
259 unsigned long long time
;
263 struct ftrace_profile_page
{
264 struct ftrace_profile_page
*next
;
266 struct ftrace_profile records
[];
269 struct ftrace_profile_stat
{
271 struct hlist_head
*hash
;
272 struct ftrace_profile_page
*pages
;
273 struct ftrace_profile_page
*start
;
274 struct tracer_stat stat
;
277 #define PROFILE_RECORDS_SIZE \
278 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
280 #define PROFILES_PER_PAGE \
281 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
283 static int ftrace_profile_bits __read_mostly
;
284 static int ftrace_profile_enabled __read_mostly
;
286 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
287 static DEFINE_MUTEX(ftrace_profile_lock
);
289 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
291 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
294 function_stat_next(void *v
, int idx
)
296 struct ftrace_profile
*rec
= v
;
297 struct ftrace_profile_page
*pg
;
299 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
305 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
309 rec
= &pg
->records
[0];
317 static void *function_stat_start(struct tracer_stat
*trace
)
319 struct ftrace_profile_stat
*stat
=
320 container_of(trace
, struct ftrace_profile_stat
, stat
);
322 if (!stat
|| !stat
->start
)
325 return function_stat_next(&stat
->start
->records
[0], 0);
328 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
329 /* function graph compares on total time */
330 static int function_stat_cmp(void *p1
, void *p2
)
332 struct ftrace_profile
*a
= p1
;
333 struct ftrace_profile
*b
= p2
;
335 if (a
->time
< b
->time
)
337 if (a
->time
> b
->time
)
343 /* not function graph compares against hits */
344 static int function_stat_cmp(void *p1
, void *p2
)
346 struct ftrace_profile
*a
= p1
;
347 struct ftrace_profile
*b
= p2
;
349 if (a
->counter
< b
->counter
)
351 if (a
->counter
> b
->counter
)
358 static int function_stat_headers(struct seq_file
*m
)
360 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
361 seq_printf(m
, " Function "
366 seq_printf(m
, " Function Hit\n"
372 static int function_stat_show(struct seq_file
*m
, void *v
)
374 struct ftrace_profile
*rec
= v
;
375 char str
[KSYM_SYMBOL_LEN
];
376 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
377 static DEFINE_MUTEX(mutex
);
378 static struct trace_seq s
;
379 unsigned long long avg
;
382 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
383 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
388 do_div(avg
, rec
->counter
);
392 trace_print_graph_duration(rec
->time
, &s
);
393 trace_seq_puts(&s
, " ");
394 trace_print_graph_duration(avg
, &s
);
395 trace_print_seq(m
, &s
);
396 mutex_unlock(&mutex
);
403 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
405 struct ftrace_profile_page
*pg
;
407 pg
= stat
->pages
= stat
->start
;
410 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
415 memset(stat
->hash
, 0,
416 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
419 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
421 struct ftrace_profile_page
*pg
;
426 /* If we already allocated, do nothing */
430 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
434 #ifdef CONFIG_DYNAMIC_FTRACE
435 functions
= ftrace_update_tot_cnt
;
438 * We do not know the number of functions that exist because
439 * dynamic tracing is what counts them. With past experience
440 * we have around 20K functions. That should be more than enough.
441 * It is highly unlikely we will execute every function in
447 pg
= stat
->start
= stat
->pages
;
449 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
451 for (i
= 0; i
< pages
; i
++) {
452 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
463 unsigned long tmp
= (unsigned long)pg
;
469 free_page((unsigned long)stat
->pages
);
476 static int ftrace_profile_init_cpu(int cpu
)
478 struct ftrace_profile_stat
*stat
;
481 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
484 /* If the profile is already created, simply reset it */
485 ftrace_profile_reset(stat
);
490 * We are profiling all functions, but usually only a few thousand
491 * functions are hit. We'll make a hash of 1024 items.
493 size
= FTRACE_PROFILE_HASH_SIZE
;
495 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
500 if (!ftrace_profile_bits
) {
503 for (; size
; size
>>= 1)
504 ftrace_profile_bits
++;
507 /* Preallocate the function profiling pages */
508 if (ftrace_profile_pages_init(stat
) < 0) {
517 static int ftrace_profile_init(void)
522 for_each_online_cpu(cpu
) {
523 ret
= ftrace_profile_init_cpu(cpu
);
531 /* interrupts must be disabled */
532 static struct ftrace_profile
*
533 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
535 struct ftrace_profile
*rec
;
536 struct hlist_head
*hhd
;
537 struct hlist_node
*n
;
540 key
= hash_long(ip
, ftrace_profile_bits
);
541 hhd
= &stat
->hash
[key
];
543 if (hlist_empty(hhd
))
546 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
554 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
555 struct ftrace_profile
*rec
)
559 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
560 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
564 * The memory is already allocated, this simply finds a new record to use.
566 static struct ftrace_profile
*
567 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
569 struct ftrace_profile
*rec
= NULL
;
571 /* prevent recursion (from NMIs) */
572 if (atomic_inc_return(&stat
->disabled
) != 1)
576 * Try to find the function again since an NMI
577 * could have added it
579 rec
= ftrace_find_profiled_func(stat
, ip
);
583 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
584 if (!stat
->pages
->next
)
586 stat
->pages
= stat
->pages
->next
;
589 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
591 ftrace_add_profile(stat
, rec
);
594 atomic_dec(&stat
->disabled
);
600 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
602 struct ftrace_profile_stat
*stat
;
603 struct ftrace_profile
*rec
;
606 if (!ftrace_profile_enabled
)
609 local_irq_save(flags
);
611 stat
= &__get_cpu_var(ftrace_profile_stats
);
612 if (!stat
->hash
|| !ftrace_profile_enabled
)
615 rec
= ftrace_find_profiled_func(stat
, ip
);
617 rec
= ftrace_profile_alloc(stat
, ip
);
624 local_irq_restore(flags
);
627 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
628 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
630 function_profile_call(trace
->func
, 0);
634 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
636 struct ftrace_profile_stat
*stat
;
637 unsigned long long calltime
;
638 struct ftrace_profile
*rec
;
641 local_irq_save(flags
);
642 stat
= &__get_cpu_var(ftrace_profile_stats
);
643 if (!stat
->hash
|| !ftrace_profile_enabled
)
646 calltime
= trace
->rettime
- trace
->calltime
;
648 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
651 index
= trace
->depth
;
653 /* Append this call time to the parent time to subtract */
655 current
->ret_stack
[index
- 1].subtime
+= calltime
;
657 if (current
->ret_stack
[index
].subtime
< calltime
)
658 calltime
-= current
->ret_stack
[index
].subtime
;
663 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
665 rec
->time
+= calltime
;
668 local_irq_restore(flags
);
671 static int register_ftrace_profiler(void)
673 return register_ftrace_graph(&profile_graph_return
,
674 &profile_graph_entry
);
677 static void unregister_ftrace_profiler(void)
679 unregister_ftrace_graph();
682 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
684 .func
= function_profile_call
,
687 static int register_ftrace_profiler(void)
689 return register_ftrace_function(&ftrace_profile_ops
);
692 static void unregister_ftrace_profiler(void)
694 unregister_ftrace_function(&ftrace_profile_ops
);
696 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
699 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
700 size_t cnt
, loff_t
*ppos
)
703 char buf
[64]; /* big enough to hold a number */
706 if (cnt
>= sizeof(buf
))
709 if (copy_from_user(&buf
, ubuf
, cnt
))
714 ret
= strict_strtoul(buf
, 10, &val
);
720 mutex_lock(&ftrace_profile_lock
);
721 if (ftrace_profile_enabled
^ val
) {
723 ret
= ftrace_profile_init();
729 ret
= register_ftrace_profiler();
734 ftrace_profile_enabled
= 1;
736 ftrace_profile_enabled
= 0;
738 * unregister_ftrace_profiler calls stop_machine
739 * so this acts like an synchronize_sched.
741 unregister_ftrace_profiler();
745 mutex_unlock(&ftrace_profile_lock
);
753 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
754 size_t cnt
, loff_t
*ppos
)
756 char buf
[64]; /* big enough to hold a number */
759 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
760 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
763 static const struct file_operations ftrace_profile_fops
= {
764 .open
= tracing_open_generic
,
765 .read
= ftrace_profile_read
,
766 .write
= ftrace_profile_write
,
769 /* used to initialize the real stat files */
770 static struct tracer_stat function_stats __initdata
= {
772 .stat_start
= function_stat_start
,
773 .stat_next
= function_stat_next
,
774 .stat_cmp
= function_stat_cmp
,
775 .stat_headers
= function_stat_headers
,
776 .stat_show
= function_stat_show
779 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
781 struct ftrace_profile_stat
*stat
;
782 struct dentry
*entry
;
787 for_each_possible_cpu(cpu
) {
788 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
790 /* allocate enough for function name + cpu number */
791 name
= kmalloc(32, GFP_KERNEL
);
794 * The files created are permanent, if something happens
795 * we still do not free memory.
798 "Could not allocate stat file for cpu %d\n",
802 stat
->stat
= function_stats
;
803 snprintf(name
, 32, "function%d", cpu
);
804 stat
->stat
.name
= name
;
805 ret
= register_stat_tracer(&stat
->stat
);
808 "Could not register function stat for cpu %d\n",
815 entry
= debugfs_create_file("function_profile_enabled", 0644,
816 d_tracer
, NULL
, &ftrace_profile_fops
);
818 pr_warning("Could not create debugfs "
819 "'function_profile_enabled' entry\n");
822 #else /* CONFIG_FUNCTION_PROFILER */
823 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
826 #endif /* CONFIG_FUNCTION_PROFILER */
828 /* set when tracing only a pid */
829 struct pid
*ftrace_pid_trace
;
830 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
832 #ifdef CONFIG_DYNAMIC_FTRACE
834 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
835 # error Dynamic ftrace depends on MCOUNT_RECORD
838 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
840 struct ftrace_func_probe
{
841 struct hlist_node node
;
842 struct ftrace_probe_ops
*ops
;
850 FTRACE_ENABLE_CALLS
= (1 << 0),
851 FTRACE_DISABLE_CALLS
= (1 << 1),
852 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
853 FTRACE_ENABLE_MCOUNT
= (1 << 3),
854 FTRACE_DISABLE_MCOUNT
= (1 << 4),
855 FTRACE_START_FUNC_RET
= (1 << 5),
856 FTRACE_STOP_FUNC_RET
= (1 << 6),
859 static int ftrace_filtered
;
861 static struct dyn_ftrace
*ftrace_new_addrs
;
863 static DEFINE_MUTEX(ftrace_regex_lock
);
866 struct ftrace_page
*next
;
868 struct dyn_ftrace records
[];
871 #define ENTRIES_PER_PAGE \
872 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
874 /* estimate from running different kernels */
875 #define NR_TO_INIT 10000
877 static struct ftrace_page
*ftrace_pages_start
;
878 static struct ftrace_page
*ftrace_pages
;
880 static struct dyn_ftrace
*ftrace_free_records
;
883 * This is a double for. Do not use 'break' to break out of the loop,
884 * you must use a goto.
886 #define do_for_each_ftrace_rec(pg, rec) \
887 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
889 for (_____i = 0; _____i < pg->index; _____i++) { \
890 rec = &pg->records[_____i];
892 #define while_for_each_ftrace_rec() \
896 #ifdef CONFIG_KPROBES
898 static int frozen_record_count
;
900 static inline void freeze_record(struct dyn_ftrace
*rec
)
902 if (!(rec
->flags
& FTRACE_FL_FROZEN
)) {
903 rec
->flags
|= FTRACE_FL_FROZEN
;
904 frozen_record_count
++;
908 static inline void unfreeze_record(struct dyn_ftrace
*rec
)
910 if (rec
->flags
& FTRACE_FL_FROZEN
) {
911 rec
->flags
&= ~FTRACE_FL_FROZEN
;
912 frozen_record_count
--;
916 static inline int record_frozen(struct dyn_ftrace
*rec
)
918 return rec
->flags
& FTRACE_FL_FROZEN
;
921 # define freeze_record(rec) ({ 0; })
922 # define unfreeze_record(rec) ({ 0; })
923 # define record_frozen(rec) ({ 0; })
924 #endif /* CONFIG_KPROBES */
926 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
928 rec
->freelist
= ftrace_free_records
;
929 ftrace_free_records
= rec
;
930 rec
->flags
|= FTRACE_FL_FREE
;
933 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
935 struct dyn_ftrace
*rec
;
937 /* First check for freed records */
938 if (ftrace_free_records
) {
939 rec
= ftrace_free_records
;
941 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
942 FTRACE_WARN_ON_ONCE(1);
943 ftrace_free_records
= NULL
;
947 ftrace_free_records
= rec
->freelist
;
948 memset(rec
, 0, sizeof(*rec
));
952 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
953 if (!ftrace_pages
->next
) {
954 /* allocate another page */
956 (void *)get_zeroed_page(GFP_KERNEL
);
957 if (!ftrace_pages
->next
)
960 ftrace_pages
= ftrace_pages
->next
;
963 return &ftrace_pages
->records
[ftrace_pages
->index
++];
966 static struct dyn_ftrace
*
967 ftrace_record_ip(unsigned long ip
)
969 struct dyn_ftrace
*rec
;
974 rec
= ftrace_alloc_dyn_node(ip
);
979 rec
->newlist
= ftrace_new_addrs
;
980 ftrace_new_addrs
= rec
;
985 static void print_ip_ins(const char *fmt
, unsigned char *p
)
989 printk(KERN_CONT
"%s", fmt
);
991 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
992 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
995 static void ftrace_bug(int failed
, unsigned long ip
)
999 FTRACE_WARN_ON_ONCE(1);
1000 pr_info("ftrace faulted on modifying ");
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace failed to modify ");
1007 print_ip_ins(" actual: ", (unsigned char *)ip
);
1008 printk(KERN_CONT
"\n");
1011 FTRACE_WARN_ON_ONCE(1);
1012 pr_info("ftrace faulted on writing ");
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on unknown error ");
1024 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1026 unsigned long ftrace_addr
;
1027 unsigned long flag
= 0UL;
1029 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1032 * If this record is not to be traced or we want to disable it,
1035 * If we want to enable it and filtering is off, then enable it.
1037 * If we want to enable it and filtering is on, enable it only if
1040 if (enable
&& !(rec
->flags
& FTRACE_FL_NOTRACE
)) {
1041 if (!ftrace_filtered
|| (rec
->flags
& FTRACE_FL_FILTER
))
1042 flag
= FTRACE_FL_ENABLED
;
1045 /* If the state of this record hasn't changed, then do nothing */
1046 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1050 rec
->flags
|= FTRACE_FL_ENABLED
;
1051 return ftrace_make_call(rec
, ftrace_addr
);
1054 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1055 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1058 static void ftrace_replace_code(int enable
)
1060 struct dyn_ftrace
*rec
;
1061 struct ftrace_page
*pg
;
1064 do_for_each_ftrace_rec(pg
, rec
) {
1066 * Skip over free records, records that have
1067 * failed and not converted.
1069 if (rec
->flags
& FTRACE_FL_FREE
||
1070 rec
->flags
& FTRACE_FL_FAILED
||
1071 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1074 /* ignore updates to this record's mcount site */
1075 if (get_kprobe((void *)rec
->ip
)) {
1079 unfreeze_record(rec
);
1082 failed
= __ftrace_replace_code(rec
, enable
);
1084 rec
->flags
|= FTRACE_FL_FAILED
;
1085 ftrace_bug(failed
, rec
->ip
);
1086 /* Stop processing */
1089 } while_for_each_ftrace_rec();
1093 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1100 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1102 ftrace_bug(ret
, ip
);
1103 rec
->flags
|= FTRACE_FL_FAILED
;
1110 * archs can override this function if they must do something
1111 * before the modifying code is performed.
1113 int __weak
ftrace_arch_code_modify_prepare(void)
1119 * archs can override this function if they must do something
1120 * after the modifying code is performed.
1122 int __weak
ftrace_arch_code_modify_post_process(void)
1127 static int __ftrace_modify_code(void *data
)
1129 int *command
= data
;
1131 if (*command
& FTRACE_ENABLE_CALLS
)
1132 ftrace_replace_code(1);
1133 else if (*command
& FTRACE_DISABLE_CALLS
)
1134 ftrace_replace_code(0);
1136 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1137 ftrace_update_ftrace_func(ftrace_trace_function
);
1139 if (*command
& FTRACE_START_FUNC_RET
)
1140 ftrace_enable_ftrace_graph_caller();
1141 else if (*command
& FTRACE_STOP_FUNC_RET
)
1142 ftrace_disable_ftrace_graph_caller();
1147 static void ftrace_run_update_code(int command
)
1151 ret
= ftrace_arch_code_modify_prepare();
1152 FTRACE_WARN_ON(ret
);
1156 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1158 ret
= ftrace_arch_code_modify_post_process();
1159 FTRACE_WARN_ON(ret
);
1162 static ftrace_func_t saved_ftrace_func
;
1163 static int ftrace_start_up
;
1165 static void ftrace_startup_enable(int command
)
1167 if (saved_ftrace_func
!= ftrace_trace_function
) {
1168 saved_ftrace_func
= ftrace_trace_function
;
1169 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1172 if (!command
|| !ftrace_enabled
)
1175 ftrace_run_update_code(command
);
1178 static void ftrace_startup(int command
)
1180 if (unlikely(ftrace_disabled
))
1184 command
|= FTRACE_ENABLE_CALLS
;
1186 ftrace_startup_enable(command
);
1189 static void ftrace_shutdown(int command
)
1191 if (unlikely(ftrace_disabled
))
1196 * Just warn in case of unbalance, no need to kill ftrace, it's not
1197 * critical but the ftrace_call callers may be never nopped again after
1198 * further ftrace uses.
1200 WARN_ON_ONCE(ftrace_start_up
< 0);
1202 if (!ftrace_start_up
)
1203 command
|= FTRACE_DISABLE_CALLS
;
1205 if (saved_ftrace_func
!= ftrace_trace_function
) {
1206 saved_ftrace_func
= ftrace_trace_function
;
1207 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1210 if (!command
|| !ftrace_enabled
)
1213 ftrace_run_update_code(command
);
1216 static void ftrace_startup_sysctl(void)
1218 int command
= FTRACE_ENABLE_MCOUNT
;
1220 if (unlikely(ftrace_disabled
))
1223 /* Force update next time */
1224 saved_ftrace_func
= NULL
;
1225 /* ftrace_start_up is true if we want ftrace running */
1226 if (ftrace_start_up
)
1227 command
|= FTRACE_ENABLE_CALLS
;
1229 ftrace_run_update_code(command
);
1232 static void ftrace_shutdown_sysctl(void)
1234 int command
= FTRACE_DISABLE_MCOUNT
;
1236 if (unlikely(ftrace_disabled
))
1239 /* ftrace_start_up is true if ftrace is running */
1240 if (ftrace_start_up
)
1241 command
|= FTRACE_DISABLE_CALLS
;
1243 ftrace_run_update_code(command
);
1246 static cycle_t ftrace_update_time
;
1247 static unsigned long ftrace_update_cnt
;
1248 unsigned long ftrace_update_tot_cnt
;
1250 static int ftrace_update_code(struct module
*mod
)
1252 struct dyn_ftrace
*p
;
1253 cycle_t start
, stop
;
1255 start
= ftrace_now(raw_smp_processor_id());
1256 ftrace_update_cnt
= 0;
1258 while (ftrace_new_addrs
) {
1260 /* If something went wrong, bail without enabling anything */
1261 if (unlikely(ftrace_disabled
))
1264 p
= ftrace_new_addrs
;
1265 ftrace_new_addrs
= p
->newlist
;
1268 /* convert record (i.e, patch mcount-call with NOP) */
1269 if (ftrace_code_disable(mod
, p
)) {
1270 p
->flags
|= FTRACE_FL_CONVERTED
;
1271 ftrace_update_cnt
++;
1276 stop
= ftrace_now(raw_smp_processor_id());
1277 ftrace_update_time
= stop
- start
;
1278 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1283 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1285 struct ftrace_page
*pg
;
1289 /* allocate a few pages */
1290 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1291 if (!ftrace_pages_start
)
1295 * Allocate a few more pages.
1297 * TODO: have some parser search vmlinux before
1298 * final linking to find all calls to ftrace.
1300 * a) know how many pages to allocate.
1302 * b) set up the table then.
1304 * The dynamic code is still necessary for
1308 pg
= ftrace_pages
= ftrace_pages_start
;
1310 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1311 pr_info("ftrace: allocating %ld entries in %d pages\n",
1312 num_to_init
, cnt
+ 1);
1314 for (i
= 0; i
< cnt
; i
++) {
1315 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1317 /* If we fail, we'll try later anyway */
1328 FTRACE_ITER_FILTER
= (1 << 0),
1329 FTRACE_ITER_NOTRACE
= (1 << 1),
1330 FTRACE_ITER_FAILURES
= (1 << 2),
1331 FTRACE_ITER_PRINTALL
= (1 << 3),
1332 FTRACE_ITER_HASH
= (1 << 4),
1335 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1337 struct ftrace_iterator
{
1338 struct ftrace_page
*pg
;
1342 struct trace_parser parser
;
1346 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1348 struct ftrace_iterator
*iter
= m
->private;
1349 struct hlist_node
*hnd
= v
;
1350 struct hlist_head
*hhd
;
1352 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1357 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1360 hhd
= &ftrace_func_hash
[iter
->hidx
];
1362 if (hlist_empty(hhd
)) {
1381 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1383 struct ftrace_iterator
*iter
= m
->private;
1387 if (!(iter
->flags
& FTRACE_ITER_HASH
))
1390 iter
->flags
|= FTRACE_ITER_HASH
;
1393 for (l
= 0; l
<= *pos
; ) {
1394 p
= t_hash_next(m
, p
, &l
);
1401 static int t_hash_show(struct seq_file
*m
, void *v
)
1403 struct ftrace_func_probe
*rec
;
1404 struct hlist_node
*hnd
= v
;
1406 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1408 if (rec
->ops
->print
)
1409 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1411 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1414 seq_printf(m
, ":%p", rec
->data
);
1421 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1423 struct ftrace_iterator
*iter
= m
->private;
1424 struct dyn_ftrace
*rec
= NULL
;
1426 if (iter
->flags
& FTRACE_ITER_HASH
)
1427 return t_hash_next(m
, v
, pos
);
1431 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1435 if (iter
->idx
>= iter
->pg
->index
) {
1436 if (iter
->pg
->next
) {
1437 iter
->pg
= iter
->pg
->next
;
1442 rec
= &iter
->pg
->records
[iter
->idx
++];
1443 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1445 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1446 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1448 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1449 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1451 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1452 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1454 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1455 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1464 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1466 struct ftrace_iterator
*iter
= m
->private;
1470 mutex_lock(&ftrace_lock
);
1472 * For set_ftrace_filter reading, if we have the filter
1473 * off, we can short cut and just print out that all
1474 * functions are enabled.
1476 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1478 return t_hash_start(m
, pos
);
1479 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1483 if (iter
->flags
& FTRACE_ITER_HASH
)
1484 return t_hash_start(m
, pos
);
1486 iter
->pg
= ftrace_pages_start
;
1488 for (l
= 0; l
<= *pos
; ) {
1489 p
= t_next(m
, p
, &l
);
1494 if (!p
&& iter
->flags
& FTRACE_ITER_FILTER
)
1495 return t_hash_start(m
, pos
);
1500 static void t_stop(struct seq_file
*m
, void *p
)
1502 mutex_unlock(&ftrace_lock
);
1505 static int t_show(struct seq_file
*m
, void *v
)
1507 struct ftrace_iterator
*iter
= m
->private;
1508 struct dyn_ftrace
*rec
= v
;
1510 if (iter
->flags
& FTRACE_ITER_HASH
)
1511 return t_hash_show(m
, v
);
1513 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1514 seq_printf(m
, "#### all functions enabled ####\n");
1521 seq_printf(m
, "%ps\n", (void *)rec
->ip
);
1526 static const struct seq_operations show_ftrace_seq_ops
= {
1534 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1536 struct ftrace_iterator
*iter
;
1539 if (unlikely(ftrace_disabled
))
1542 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1546 iter
->pg
= ftrace_pages_start
;
1548 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1550 struct seq_file
*m
= file
->private_data
;
1561 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1565 struct ftrace_iterator
*iter
;
1567 ret
= ftrace_avail_open(inode
, file
);
1569 m
= (struct seq_file
*)file
->private_data
;
1570 iter
= (struct ftrace_iterator
*)m
->private;
1571 iter
->flags
= FTRACE_ITER_FAILURES
;
1578 static void ftrace_filter_reset(int enable
)
1580 struct ftrace_page
*pg
;
1581 struct dyn_ftrace
*rec
;
1582 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1584 mutex_lock(&ftrace_lock
);
1586 ftrace_filtered
= 0;
1587 do_for_each_ftrace_rec(pg
, rec
) {
1588 if (rec
->flags
& FTRACE_FL_FAILED
)
1590 rec
->flags
&= ~type
;
1591 } while_for_each_ftrace_rec();
1592 mutex_unlock(&ftrace_lock
);
1596 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1598 struct ftrace_iterator
*iter
;
1601 if (unlikely(ftrace_disabled
))
1604 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1608 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
1613 mutex_lock(&ftrace_regex_lock
);
1614 if ((file
->f_mode
& FMODE_WRITE
) &&
1615 (file
->f_flags
& O_TRUNC
))
1616 ftrace_filter_reset(enable
);
1618 if (file
->f_mode
& FMODE_READ
) {
1619 iter
->pg
= ftrace_pages_start
;
1620 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1621 FTRACE_ITER_NOTRACE
;
1623 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1625 struct seq_file
*m
= file
->private_data
;
1628 trace_parser_put(&iter
->parser
);
1632 file
->private_data
= iter
;
1633 mutex_unlock(&ftrace_regex_lock
);
1639 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1641 return ftrace_regex_open(inode
, file
, 1);
1645 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1647 return ftrace_regex_open(inode
, file
, 0);
1651 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1655 if (file
->f_mode
& FMODE_READ
)
1656 ret
= seq_lseek(file
, offset
, origin
);
1658 file
->f_pos
= ret
= 1;
1663 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1670 if (strcmp(str
, regex
) == 0)
1673 case MATCH_FRONT_ONLY
:
1674 if (strncmp(str
, regex
, len
) == 0)
1677 case MATCH_MIDDLE_ONLY
:
1678 if (strstr(str
, regex
))
1681 case MATCH_END_ONLY
:
1682 ptr
= strstr(str
, regex
);
1683 if (ptr
&& (ptr
[len
] == 0))
1692 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1694 char str
[KSYM_SYMBOL_LEN
];
1696 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1697 return ftrace_match(str
, regex
, len
, type
);
1700 static void ftrace_match_records(char *buff
, int len
, int enable
)
1702 unsigned int search_len
;
1703 struct ftrace_page
*pg
;
1704 struct dyn_ftrace
*rec
;
1710 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1711 type
= filter_parse_regex(buff
, len
, &search
, ¬);
1713 search_len
= strlen(search
);
1715 mutex_lock(&ftrace_lock
);
1716 do_for_each_ftrace_rec(pg
, rec
) {
1718 if (rec
->flags
& FTRACE_FL_FAILED
)
1721 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1723 rec
->flags
&= ~flag
;
1728 * Only enable filtering if we have a function that
1731 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1732 ftrace_filtered
= 1;
1733 } while_for_each_ftrace_rec();
1734 mutex_unlock(&ftrace_lock
);
1738 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1739 char *regex
, int len
, int type
)
1741 char str
[KSYM_SYMBOL_LEN
];
1744 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1746 if (!modname
|| strcmp(modname
, mod
))
1749 /* blank search means to match all funcs in the mod */
1751 return ftrace_match(str
, regex
, len
, type
);
1756 static void ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1758 unsigned search_len
= 0;
1759 struct ftrace_page
*pg
;
1760 struct dyn_ftrace
*rec
;
1761 int type
= MATCH_FULL
;
1762 char *search
= buff
;
1766 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1768 /* blank or '*' mean the same */
1769 if (strcmp(buff
, "*") == 0)
1772 /* handle the case of 'dont filter this module' */
1773 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1779 type
= filter_parse_regex(buff
, strlen(buff
), &search
, ¬);
1780 search_len
= strlen(search
);
1783 mutex_lock(&ftrace_lock
);
1784 do_for_each_ftrace_rec(pg
, rec
) {
1786 if (rec
->flags
& FTRACE_FL_FAILED
)
1789 if (ftrace_match_module_record(rec
, mod
,
1790 search
, search_len
, type
)) {
1792 rec
->flags
&= ~flag
;
1796 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1797 ftrace_filtered
= 1;
1799 } while_for_each_ftrace_rec();
1800 mutex_unlock(&ftrace_lock
);
1804 * We register the module command as a template to show others how
1805 * to register the a command as well.
1809 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1814 * cmd == 'mod' because we only registered this func
1815 * for the 'mod' ftrace_func_command.
1816 * But if you register one func with multiple commands,
1817 * you can tell which command was used by the cmd
1821 /* we must have a module name */
1825 mod
= strsep(¶m
, ":");
1829 ftrace_match_module_records(func
, mod
, enable
);
1833 static struct ftrace_func_command ftrace_mod_cmd
= {
1835 .func
= ftrace_mod_callback
,
1838 static int __init
ftrace_mod_cmd_init(void)
1840 return register_ftrace_command(&ftrace_mod_cmd
);
1842 device_initcall(ftrace_mod_cmd_init
);
1845 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1847 struct ftrace_func_probe
*entry
;
1848 struct hlist_head
*hhd
;
1849 struct hlist_node
*n
;
1853 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1855 hhd
= &ftrace_func_hash
[key
];
1857 if (hlist_empty(hhd
))
1861 * Disable preemption for these calls to prevent a RCU grace
1862 * period. This syncs the hash iteration and freeing of items
1863 * on the hash. rcu_read_lock is too dangerous here.
1865 resched
= ftrace_preempt_disable();
1866 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1867 if (entry
->ip
== ip
)
1868 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1870 ftrace_preempt_enable(resched
);
1873 static struct ftrace_ops trace_probe_ops __read_mostly
=
1875 .func
= function_trace_probe_call
,
1878 static int ftrace_probe_registered
;
1880 static void __enable_ftrace_function_probe(void)
1884 if (ftrace_probe_registered
)
1887 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1888 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1892 /* Nothing registered? */
1893 if (i
== FTRACE_FUNC_HASHSIZE
)
1896 __register_ftrace_function(&trace_probe_ops
);
1898 ftrace_probe_registered
= 1;
1901 static void __disable_ftrace_function_probe(void)
1905 if (!ftrace_probe_registered
)
1908 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1909 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1914 /* no more funcs left */
1915 __unregister_ftrace_function(&trace_probe_ops
);
1917 ftrace_probe_registered
= 0;
1921 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
1923 struct ftrace_func_probe
*entry
=
1924 container_of(rhp
, struct ftrace_func_probe
, rcu
);
1926 if (entry
->ops
->free
)
1927 entry
->ops
->free(&entry
->data
);
1933 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
1936 struct ftrace_func_probe
*entry
;
1937 struct ftrace_page
*pg
;
1938 struct dyn_ftrace
*rec
;
1944 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
1945 len
= strlen(search
);
1947 /* we do not support '!' for function probes */
1951 mutex_lock(&ftrace_lock
);
1952 do_for_each_ftrace_rec(pg
, rec
) {
1954 if (rec
->flags
& FTRACE_FL_FAILED
)
1957 if (!ftrace_match_record(rec
, search
, len
, type
))
1960 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1962 /* If we did not process any, then return error */
1973 * The caller might want to do something special
1974 * for each function we find. We call the callback
1975 * to give the caller an opportunity to do so.
1977 if (ops
->callback
) {
1978 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
1979 /* caller does not like this func */
1986 entry
->ip
= rec
->ip
;
1988 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
1989 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
1991 } while_for_each_ftrace_rec();
1992 __enable_ftrace_function_probe();
1995 mutex_unlock(&ftrace_lock
);
2001 PROBE_TEST_FUNC
= 1,
2006 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2007 void *data
, int flags
)
2009 struct ftrace_func_probe
*entry
;
2010 struct hlist_node
*n
, *tmp
;
2011 char str
[KSYM_SYMBOL_LEN
];
2012 int type
= MATCH_FULL
;
2016 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2021 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2022 len
= strlen(search
);
2024 /* we do not support '!' for function probes */
2029 mutex_lock(&ftrace_lock
);
2030 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2031 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2033 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2035 /* break up if statements for readability */
2036 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2039 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2042 /* do this last, since it is the most expensive */
2044 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2046 if (!ftrace_match(str
, glob
, len
, type
))
2050 hlist_del(&entry
->node
);
2051 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2054 __disable_ftrace_function_probe();
2055 mutex_unlock(&ftrace_lock
);
2059 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2062 __unregister_ftrace_function_probe(glob
, ops
, data
,
2063 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2067 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2069 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2072 void unregister_ftrace_function_probe_all(char *glob
)
2074 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2077 static LIST_HEAD(ftrace_commands
);
2078 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2080 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2082 struct ftrace_func_command
*p
;
2085 mutex_lock(&ftrace_cmd_mutex
);
2086 list_for_each_entry(p
, &ftrace_commands
, list
) {
2087 if (strcmp(cmd
->name
, p
->name
) == 0) {
2092 list_add(&cmd
->list
, &ftrace_commands
);
2094 mutex_unlock(&ftrace_cmd_mutex
);
2099 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2101 struct ftrace_func_command
*p
, *n
;
2104 mutex_lock(&ftrace_cmd_mutex
);
2105 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2106 if (strcmp(cmd
->name
, p
->name
) == 0) {
2108 list_del_init(&p
->list
);
2113 mutex_unlock(&ftrace_cmd_mutex
);
2118 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2120 char *func
, *command
, *next
= buff
;
2121 struct ftrace_func_command
*p
;
2124 func
= strsep(&next
, ":");
2127 ftrace_match_records(func
, len
, enable
);
2133 command
= strsep(&next
, ":");
2135 mutex_lock(&ftrace_cmd_mutex
);
2136 list_for_each_entry(p
, &ftrace_commands
, list
) {
2137 if (strcmp(p
->name
, command
) == 0) {
2138 ret
= p
->func(func
, command
, next
, enable
);
2143 mutex_unlock(&ftrace_cmd_mutex
);
2149 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2150 size_t cnt
, loff_t
*ppos
, int enable
)
2152 struct ftrace_iterator
*iter
;
2153 struct trace_parser
*parser
;
2159 mutex_lock(&ftrace_regex_lock
);
2161 if (file
->f_mode
& FMODE_READ
) {
2162 struct seq_file
*m
= file
->private_data
;
2165 iter
= file
->private_data
;
2167 parser
= &iter
->parser
;
2168 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2170 if (read
>= 0 && trace_parser_loaded(parser
) &&
2171 !trace_parser_cont(parser
)) {
2172 ret
= ftrace_process_regex(parser
->buffer
,
2173 parser
->idx
, enable
);
2177 trace_parser_clear(parser
);
2182 mutex_unlock(&ftrace_regex_lock
);
2188 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2189 size_t cnt
, loff_t
*ppos
)
2191 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2195 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2196 size_t cnt
, loff_t
*ppos
)
2198 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2202 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2204 if (unlikely(ftrace_disabled
))
2207 mutex_lock(&ftrace_regex_lock
);
2209 ftrace_filter_reset(enable
);
2211 ftrace_match_records(buf
, len
, enable
);
2212 mutex_unlock(&ftrace_regex_lock
);
2216 * ftrace_set_filter - set a function to filter on in ftrace
2217 * @buf - the string that holds the function filter text.
2218 * @len - the length of the string.
2219 * @reset - non zero to reset all filters before applying this filter.
2221 * Filters denote which functions should be enabled when tracing is enabled.
2222 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2224 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2226 ftrace_set_regex(buf
, len
, reset
, 1);
2230 * ftrace_set_notrace - set a function to not trace in ftrace
2231 * @buf - the string that holds the function notrace text.
2232 * @len - the length of the string.
2233 * @reset - non zero to reset all filters before applying this filter.
2235 * Notrace Filters denote which functions should not be enabled when tracing
2236 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2239 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2241 ftrace_set_regex(buf
, len
, reset
, 0);
2245 * command line interface to allow users to set filters on boot up.
2247 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2248 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2249 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2250 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2252 static int __init
set_ftrace_notrace(char *str
)
2254 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2257 __setup("ftrace_notrace=", set_ftrace_notrace
);
2259 static int __init
set_ftrace_filter(char *str
)
2261 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2264 __setup("ftrace_filter=", set_ftrace_filter
);
2266 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2267 static int __init
set_graph_function(char *str
)
2269 strncpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2272 __setup("ftrace_graph_filter=", set_graph_function
);
2274 static void __init
set_ftrace_early_graph(char *buf
)
2280 func
= strsep(&buf
, ",");
2281 /* we allow only one expression at a time */
2282 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2285 printk(KERN_DEBUG
"ftrace: function %s not "
2286 "traceable\n", func
);
2289 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2291 static void __init
set_ftrace_early_filter(char *buf
, int enable
)
2296 func
= strsep(&buf
, ",");
2297 ftrace_set_regex(func
, strlen(func
), 0, enable
);
2301 static void __init
set_ftrace_early_filters(void)
2303 if (ftrace_filter_buf
[0])
2304 set_ftrace_early_filter(ftrace_filter_buf
, 1);
2305 if (ftrace_notrace_buf
[0])
2306 set_ftrace_early_filter(ftrace_notrace_buf
, 0);
2307 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2308 if (ftrace_graph_buf
[0])
2309 set_ftrace_early_graph(ftrace_graph_buf
);
2310 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2314 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2316 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2317 struct ftrace_iterator
*iter
;
2318 struct trace_parser
*parser
;
2320 mutex_lock(&ftrace_regex_lock
);
2321 if (file
->f_mode
& FMODE_READ
) {
2324 seq_release(inode
, file
);
2326 iter
= file
->private_data
;
2328 parser
= &iter
->parser
;
2329 if (trace_parser_loaded(parser
)) {
2330 parser
->buffer
[parser
->idx
] = 0;
2331 ftrace_match_records(parser
->buffer
, parser
->idx
, enable
);
2334 mutex_lock(&ftrace_lock
);
2335 if (ftrace_start_up
&& ftrace_enabled
)
2336 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2337 mutex_unlock(&ftrace_lock
);
2339 trace_parser_put(parser
);
2342 mutex_unlock(&ftrace_regex_lock
);
2347 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2349 return ftrace_regex_release(inode
, file
, 1);
2353 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2355 return ftrace_regex_release(inode
, file
, 0);
2358 static const struct file_operations ftrace_avail_fops
= {
2359 .open
= ftrace_avail_open
,
2361 .llseek
= seq_lseek
,
2362 .release
= seq_release_private
,
2365 static const struct file_operations ftrace_failures_fops
= {
2366 .open
= ftrace_failures_open
,
2368 .llseek
= seq_lseek
,
2369 .release
= seq_release_private
,
2372 static const struct file_operations ftrace_filter_fops
= {
2373 .open
= ftrace_filter_open
,
2375 .write
= ftrace_filter_write
,
2376 .llseek
= ftrace_regex_lseek
,
2377 .release
= ftrace_filter_release
,
2380 static const struct file_operations ftrace_notrace_fops
= {
2381 .open
= ftrace_notrace_open
,
2383 .write
= ftrace_notrace_write
,
2384 .llseek
= ftrace_regex_lseek
,
2385 .release
= ftrace_notrace_release
,
2388 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2390 static DEFINE_MUTEX(graph_lock
);
2392 int ftrace_graph_count
;
2393 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2396 __g_next(struct seq_file
*m
, loff_t
*pos
)
2398 if (*pos
>= ftrace_graph_count
)
2400 return &ftrace_graph_funcs
[*pos
];
2404 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2407 return __g_next(m
, pos
);
2410 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2412 mutex_lock(&graph_lock
);
2414 /* Nothing, tell g_show to print all functions are enabled */
2415 if (!ftrace_graph_count
&& !*pos
)
2418 return __g_next(m
, pos
);
2421 static void g_stop(struct seq_file
*m
, void *p
)
2423 mutex_unlock(&graph_lock
);
2426 static int g_show(struct seq_file
*m
, void *v
)
2428 unsigned long *ptr
= v
;
2433 if (ptr
== (unsigned long *)1) {
2434 seq_printf(m
, "#### all functions enabled ####\n");
2438 seq_printf(m
, "%ps\n", (void *)*ptr
);
2443 static const struct seq_operations ftrace_graph_seq_ops
= {
2451 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2455 if (unlikely(ftrace_disabled
))
2458 mutex_lock(&graph_lock
);
2459 if ((file
->f_mode
& FMODE_WRITE
) &&
2460 (file
->f_flags
& O_TRUNC
)) {
2461 ftrace_graph_count
= 0;
2462 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2464 mutex_unlock(&graph_lock
);
2466 if (file
->f_mode
& FMODE_READ
)
2467 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2473 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
2475 if (file
->f_mode
& FMODE_READ
)
2476 seq_release(inode
, file
);
2481 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2483 struct dyn_ftrace
*rec
;
2484 struct ftrace_page
*pg
;
2492 if (ftrace_disabled
)
2496 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
2500 search_len
= strlen(search
);
2502 mutex_lock(&ftrace_lock
);
2503 do_for_each_ftrace_rec(pg
, rec
) {
2505 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2508 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2511 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2512 /* ensure it is not already in the array */
2514 for (i
= 0; i
< *idx
; i
++)
2515 if (array
[i
] == rec
->ip
) {
2520 array
[(*idx
)++] = rec
->ip
;
2524 } while_for_each_ftrace_rec();
2526 mutex_unlock(&ftrace_lock
);
2528 return found
? 0 : -EINVAL
;
2532 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2533 size_t cnt
, loff_t
*ppos
)
2535 struct trace_parser parser
;
2538 if (!cnt
|| cnt
< 0)
2541 mutex_lock(&graph_lock
);
2543 if (ftrace_graph_count
>= FTRACE_GRAPH_MAX_FUNCS
) {
2548 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
2553 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
2555 if (read
>= 0 && trace_parser_loaded((&parser
))) {
2556 parser
.buffer
[parser
.idx
] = 0;
2558 /* we allow only one expression at a time */
2559 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2568 trace_parser_put(&parser
);
2570 mutex_unlock(&graph_lock
);
2575 static const struct file_operations ftrace_graph_fops
= {
2576 .open
= ftrace_graph_open
,
2578 .write
= ftrace_graph_write
,
2579 .release
= ftrace_graph_release
,
2581 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2583 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2586 trace_create_file("available_filter_functions", 0444,
2587 d_tracer
, NULL
, &ftrace_avail_fops
);
2589 trace_create_file("failures", 0444,
2590 d_tracer
, NULL
, &ftrace_failures_fops
);
2592 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
2593 NULL
, &ftrace_filter_fops
);
2595 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
2596 NULL
, &ftrace_notrace_fops
);
2598 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2599 trace_create_file("set_graph_function", 0444, d_tracer
,
2601 &ftrace_graph_fops
);
2602 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2607 static int ftrace_convert_nops(struct module
*mod
,
2608 unsigned long *start
,
2613 unsigned long flags
;
2615 mutex_lock(&ftrace_lock
);
2618 addr
= ftrace_call_adjust(*p
++);
2620 * Some architecture linkers will pad between
2621 * the different mcount_loc sections of different
2622 * object files to satisfy alignments.
2623 * Skip any NULL pointers.
2627 ftrace_record_ip(addr
);
2630 /* disable interrupts to prevent kstop machine */
2631 local_irq_save(flags
);
2632 ftrace_update_code(mod
);
2633 local_irq_restore(flags
);
2634 mutex_unlock(&ftrace_lock
);
2639 #ifdef CONFIG_MODULES
2640 void ftrace_release_mod(struct module
*mod
)
2642 struct dyn_ftrace
*rec
;
2643 struct ftrace_page
*pg
;
2645 if (ftrace_disabled
)
2648 mutex_lock(&ftrace_lock
);
2649 do_for_each_ftrace_rec(pg
, rec
) {
2650 if (within_module_core(rec
->ip
, mod
)) {
2652 * rec->ip is changed in ftrace_free_rec()
2653 * It should not between s and e if record was freed.
2655 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
2656 ftrace_free_rec(rec
);
2658 } while_for_each_ftrace_rec();
2659 mutex_unlock(&ftrace_lock
);
2662 static void ftrace_init_module(struct module
*mod
,
2663 unsigned long *start
, unsigned long *end
)
2665 if (ftrace_disabled
|| start
== end
)
2667 ftrace_convert_nops(mod
, start
, end
);
2670 static int ftrace_module_notify(struct notifier_block
*self
,
2671 unsigned long val
, void *data
)
2673 struct module
*mod
= data
;
2676 case MODULE_STATE_COMING
:
2677 ftrace_init_module(mod
, mod
->ftrace_callsites
,
2678 mod
->ftrace_callsites
+
2679 mod
->num_ftrace_callsites
);
2681 case MODULE_STATE_GOING
:
2682 ftrace_release_mod(mod
);
2689 static int ftrace_module_notify(struct notifier_block
*self
,
2690 unsigned long val
, void *data
)
2694 #endif /* CONFIG_MODULES */
2696 struct notifier_block ftrace_module_nb
= {
2697 .notifier_call
= ftrace_module_notify
,
2701 extern unsigned long __start_mcount_loc
[];
2702 extern unsigned long __stop_mcount_loc
[];
2704 void __init
ftrace_init(void)
2706 unsigned long count
, addr
, flags
;
2709 /* Keep the ftrace pointer to the stub */
2710 addr
= (unsigned long)ftrace_stub
;
2712 local_irq_save(flags
);
2713 ftrace_dyn_arch_init(&addr
);
2714 local_irq_restore(flags
);
2716 /* ftrace_dyn_arch_init places the return code in addr */
2720 count
= __stop_mcount_loc
- __start_mcount_loc
;
2722 ret
= ftrace_dyn_table_alloc(count
);
2726 last_ftrace_enabled
= ftrace_enabled
= 1;
2728 ret
= ftrace_convert_nops(NULL
,
2732 ret
= register_module_notifier(&ftrace_module_nb
);
2734 pr_warning("Failed to register trace ftrace module notifier\n");
2736 set_ftrace_early_filters();
2740 ftrace_disabled
= 1;
2745 static int __init
ftrace_nodyn_init(void)
2750 device_initcall(ftrace_nodyn_init
);
2752 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2753 static inline void ftrace_startup_enable(int command
) { }
2754 /* Keep as macros so we do not need to define the commands */
2755 # define ftrace_startup(command) do { } while (0)
2756 # define ftrace_shutdown(command) do { } while (0)
2757 # define ftrace_startup_sysctl() do { } while (0)
2758 # define ftrace_shutdown_sysctl() do { } while (0)
2759 #endif /* CONFIG_DYNAMIC_FTRACE */
2762 ftrace_pid_read(struct file
*file
, char __user
*ubuf
,
2763 size_t cnt
, loff_t
*ppos
)
2768 if (ftrace_pid_trace
== ftrace_swapper_pid
)
2769 r
= sprintf(buf
, "swapper tasks\n");
2770 else if (ftrace_pid_trace
)
2771 r
= sprintf(buf
, "%u\n", pid_vnr(ftrace_pid_trace
));
2773 r
= sprintf(buf
, "no pid\n");
2775 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2778 static void clear_ftrace_swapper(void)
2780 struct task_struct
*p
;
2784 for_each_online_cpu(cpu
) {
2786 clear_tsk_trace_trace(p
);
2791 static void set_ftrace_swapper(void)
2793 struct task_struct
*p
;
2797 for_each_online_cpu(cpu
) {
2799 set_tsk_trace_trace(p
);
2804 static void clear_ftrace_pid(struct pid
*pid
)
2806 struct task_struct
*p
;
2809 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2810 clear_tsk_trace_trace(p
);
2811 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2817 static void set_ftrace_pid(struct pid
*pid
)
2819 struct task_struct
*p
;
2822 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2823 set_tsk_trace_trace(p
);
2824 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2828 static void clear_ftrace_pid_task(struct pid
**pid
)
2830 if (*pid
== ftrace_swapper_pid
)
2831 clear_ftrace_swapper();
2833 clear_ftrace_pid(*pid
);
2838 static void set_ftrace_pid_task(struct pid
*pid
)
2840 if (pid
== ftrace_swapper_pid
)
2841 set_ftrace_swapper();
2843 set_ftrace_pid(pid
);
2847 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
2848 size_t cnt
, loff_t
*ppos
)
2855 if (cnt
>= sizeof(buf
))
2858 if (copy_from_user(&buf
, ubuf
, cnt
))
2863 ret
= strict_strtol(buf
, 10, &val
);
2867 mutex_lock(&ftrace_lock
);
2869 /* disable pid tracing */
2870 if (!ftrace_pid_trace
)
2873 clear_ftrace_pid_task(&ftrace_pid_trace
);
2876 /* swapper task is special */
2878 pid
= ftrace_swapper_pid
;
2879 if (pid
== ftrace_pid_trace
)
2882 pid
= find_get_pid(val
);
2884 if (pid
== ftrace_pid_trace
) {
2890 if (ftrace_pid_trace
)
2891 clear_ftrace_pid_task(&ftrace_pid_trace
);
2896 ftrace_pid_trace
= pid
;
2898 set_ftrace_pid_task(ftrace_pid_trace
);
2901 /* update the function call */
2902 ftrace_update_pid_func();
2903 ftrace_startup_enable(0);
2906 mutex_unlock(&ftrace_lock
);
2911 static const struct file_operations ftrace_pid_fops
= {
2912 .read
= ftrace_pid_read
,
2913 .write
= ftrace_pid_write
,
2916 static __init
int ftrace_init_debugfs(void)
2918 struct dentry
*d_tracer
;
2920 d_tracer
= tracing_init_dentry();
2924 ftrace_init_dyn_debugfs(d_tracer
);
2926 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
2927 NULL
, &ftrace_pid_fops
);
2929 ftrace_profile_debugfs(d_tracer
);
2933 fs_initcall(ftrace_init_debugfs
);
2936 * ftrace_kill - kill ftrace
2938 * This function should be used by panic code. It stops ftrace
2939 * but in a not so nice way. If you need to simply kill ftrace
2940 * from a non-atomic section, use ftrace_kill.
2942 void ftrace_kill(void)
2944 ftrace_disabled
= 1;
2946 clear_ftrace_function();
2950 * register_ftrace_function - register a function for profiling
2951 * @ops - ops structure that holds the function for profiling.
2953 * Register a function to be called by all functions in the
2956 * Note: @ops->func and all the functions it calls must be labeled
2957 * with "notrace", otherwise it will go into a
2960 int register_ftrace_function(struct ftrace_ops
*ops
)
2964 if (unlikely(ftrace_disabled
))
2967 mutex_lock(&ftrace_lock
);
2969 ret
= __register_ftrace_function(ops
);
2972 mutex_unlock(&ftrace_lock
);
2977 * unregister_ftrace_function - unregister a function for profiling.
2978 * @ops - ops structure that holds the function to unregister
2980 * Unregister a function that was added to be called by ftrace profiling.
2982 int unregister_ftrace_function(struct ftrace_ops
*ops
)
2986 mutex_lock(&ftrace_lock
);
2987 ret
= __unregister_ftrace_function(ops
);
2989 mutex_unlock(&ftrace_lock
);
2995 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
2996 void __user
*buffer
, size_t *lenp
,
3001 if (unlikely(ftrace_disabled
))
3004 mutex_lock(&ftrace_lock
);
3006 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3008 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3011 last_ftrace_enabled
= !!ftrace_enabled
;
3013 if (ftrace_enabled
) {
3015 ftrace_startup_sysctl();
3017 /* we are starting ftrace again */
3018 if (ftrace_list
!= &ftrace_list_end
) {
3019 if (ftrace_list
->next
== &ftrace_list_end
)
3020 ftrace_trace_function
= ftrace_list
->func
;
3022 ftrace_trace_function
= ftrace_list_func
;
3026 /* stopping ftrace calls (just send to ftrace_stub) */
3027 ftrace_trace_function
= ftrace_stub
;
3029 ftrace_shutdown_sysctl();
3033 mutex_unlock(&ftrace_lock
);
3037 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3039 static int ftrace_graph_active
;
3040 static struct notifier_block ftrace_suspend_notifier
;
3042 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3047 /* The callbacks that hook a function */
3048 trace_func_graph_ret_t ftrace_graph_return
=
3049 (trace_func_graph_ret_t
)ftrace_stub
;
3050 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3052 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3053 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3057 unsigned long flags
;
3058 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3059 struct task_struct
*g
, *t
;
3061 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3062 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3063 * sizeof(struct ftrace_ret_stack
),
3065 if (!ret_stack_list
[i
]) {
3073 read_lock_irqsave(&tasklist_lock
, flags
);
3074 do_each_thread(g
, t
) {
3080 if (t
->ret_stack
== NULL
) {
3081 atomic_set(&t
->tracing_graph_pause
, 0);
3082 atomic_set(&t
->trace_overrun
, 0);
3083 t
->curr_ret_stack
= -1;
3084 /* Make sure the tasks see the -1 first: */
3086 t
->ret_stack
= ret_stack_list
[start
++];
3088 } while_each_thread(g
, t
);
3091 read_unlock_irqrestore(&tasklist_lock
, flags
);
3093 for (i
= start
; i
< end
; i
++)
3094 kfree(ret_stack_list
[i
]);
3099 ftrace_graph_probe_sched_switch(struct rq
*__rq
, struct task_struct
*prev
,
3100 struct task_struct
*next
)
3102 unsigned long long timestamp
;
3106 * Does the user want to count the time a function was asleep.
3107 * If so, do not update the time stamps.
3109 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3112 timestamp
= trace_clock_local();
3114 prev
->ftrace_timestamp
= timestamp
;
3116 /* only process tasks that we timestamped */
3117 if (!next
->ftrace_timestamp
)
3121 * Update all the counters in next to make up for the
3122 * time next was sleeping.
3124 timestamp
-= next
->ftrace_timestamp
;
3126 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3127 next
->ret_stack
[index
].calltime
+= timestamp
;
3130 /* Allocate a return stack for each task */
3131 static int start_graph_tracing(void)
3133 struct ftrace_ret_stack
**ret_stack_list
;
3136 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3137 sizeof(struct ftrace_ret_stack
*),
3140 if (!ret_stack_list
)
3143 /* The cpu_boot init_task->ret_stack will never be freed */
3144 for_each_online_cpu(cpu
) {
3145 if (!idle_task(cpu
)->ret_stack
)
3146 ftrace_graph_init_task(idle_task(cpu
));
3150 ret
= alloc_retstack_tasklist(ret_stack_list
);
3151 } while (ret
== -EAGAIN
);
3154 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3156 pr_info("ftrace_graph: Couldn't activate tracepoint"
3157 " probe to kernel_sched_switch\n");
3160 kfree(ret_stack_list
);
3165 * Hibernation protection.
3166 * The state of the current task is too much unstable during
3167 * suspend/restore to disk. We want to protect against that.
3170 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3174 case PM_HIBERNATION_PREPARE
:
3175 pause_graph_tracing();
3178 case PM_POST_HIBERNATION
:
3179 unpause_graph_tracing();
3185 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3186 trace_func_graph_ent_t entryfunc
)
3190 mutex_lock(&ftrace_lock
);
3192 /* we currently allow only one tracer registered at a time */
3193 if (ftrace_graph_active
) {
3198 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3199 register_pm_notifier(&ftrace_suspend_notifier
);
3201 ftrace_graph_active
++;
3202 ret
= start_graph_tracing();
3204 ftrace_graph_active
--;
3208 ftrace_graph_return
= retfunc
;
3209 ftrace_graph_entry
= entryfunc
;
3211 ftrace_startup(FTRACE_START_FUNC_RET
);
3214 mutex_unlock(&ftrace_lock
);
3218 void unregister_ftrace_graph(void)
3220 mutex_lock(&ftrace_lock
);
3222 if (unlikely(!ftrace_graph_active
))
3225 ftrace_graph_active
--;
3226 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3227 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3228 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3229 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3230 unregister_pm_notifier(&ftrace_suspend_notifier
);
3233 mutex_unlock(&ftrace_lock
);
3236 /* Allocate a return stack for newly created task */
3237 void ftrace_graph_init_task(struct task_struct
*t
)
3239 /* Make sure we do not use the parent ret_stack */
3240 t
->ret_stack
= NULL
;
3242 if (ftrace_graph_active
) {
3243 struct ftrace_ret_stack
*ret_stack
;
3245 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3246 * sizeof(struct ftrace_ret_stack
),
3250 t
->curr_ret_stack
= -1;
3251 atomic_set(&t
->tracing_graph_pause
, 0);
3252 atomic_set(&t
->trace_overrun
, 0);
3253 t
->ftrace_timestamp
= 0;
3254 /* make curr_ret_stack visable before we add the ret_stack */
3256 t
->ret_stack
= ret_stack
;
3260 void ftrace_graph_exit_task(struct task_struct
*t
)
3262 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3264 t
->ret_stack
= NULL
;
3265 /* NULL must become visible to IRQs before we free it: */
3271 void ftrace_graph_stop(void)