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/sched.h>
34 #include <asm/ftrace.h>
36 #include "trace_output.h"
37 #include "trace_stat.h"
39 #define FTRACE_WARN_ON(cond) \
45 #define FTRACE_WARN_ON_ONCE(cond) \
47 if (WARN_ON_ONCE(cond)) \
51 /* hash bits for specific function selection */
52 #define FTRACE_HASH_BITS 7
53 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55 /* ftrace_enabled is a method to turn ftrace on or off */
56 int ftrace_enabled __read_mostly
;
57 static int last_ftrace_enabled
;
59 /* Quick disabling of function tracer. */
60 int function_trace_stop
;
63 * ftrace_disabled is set when an anomaly is discovered.
64 * ftrace_disabled is much stronger than ftrace_enabled.
66 static int ftrace_disabled __read_mostly
;
68 static DEFINE_MUTEX(ftrace_lock
);
70 static struct ftrace_ops ftrace_list_end __read_mostly
=
75 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
76 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
77 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
78 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
80 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
82 struct ftrace_ops
*op
= ftrace_list
;
84 /* in case someone actually ports this to alpha! */
85 read_barrier_depends();
87 while (op
!= &ftrace_list_end
) {
89 read_barrier_depends();
90 op
->func(ip
, parent_ip
);
95 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
97 if (!test_tsk_trace_trace(current
))
100 ftrace_pid_function(ip
, parent_ip
);
103 static void set_ftrace_pid_function(ftrace_func_t func
)
105 /* do not set ftrace_pid_function to itself! */
106 if (func
!= ftrace_pid_func
)
107 ftrace_pid_function
= func
;
111 * clear_ftrace_function - reset the ftrace function
113 * This NULLs the ftrace function and in essence stops
114 * tracing. There may be lag
116 void clear_ftrace_function(void)
118 ftrace_trace_function
= ftrace_stub
;
119 __ftrace_trace_function
= ftrace_stub
;
120 ftrace_pid_function
= ftrace_stub
;
123 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
125 * For those archs that do not test ftrace_trace_stop in their
126 * mcount call site, we need to do it from C.
128 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
130 if (function_trace_stop
)
133 __ftrace_trace_function(ip
, parent_ip
);
137 static int __register_ftrace_function(struct ftrace_ops
*ops
)
139 ops
->next
= ftrace_list
;
141 * We are entering ops into the ftrace_list but another
142 * CPU might be walking that list. We need to make sure
143 * the ops->next pointer is valid before another CPU sees
144 * the ops pointer included into the ftrace_list.
149 if (ftrace_enabled
) {
152 if (ops
->next
== &ftrace_list_end
)
155 func
= ftrace_list_func
;
157 if (ftrace_pid_trace
) {
158 set_ftrace_pid_function(func
);
159 func
= ftrace_pid_func
;
163 * For one func, simply call it directly.
164 * For more than one func, call the chain.
166 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
167 ftrace_trace_function
= func
;
169 __ftrace_trace_function
= func
;
170 ftrace_trace_function
= ftrace_test_stop_func
;
177 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
179 struct ftrace_ops
**p
;
182 * If we are removing the last function, then simply point
183 * to the ftrace_stub.
185 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
186 ftrace_trace_function
= ftrace_stub
;
187 ftrace_list
= &ftrace_list_end
;
191 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
200 if (ftrace_enabled
) {
201 /* If we only have one func left, then call that directly */
202 if (ftrace_list
->next
== &ftrace_list_end
) {
203 ftrace_func_t func
= ftrace_list
->func
;
205 if (ftrace_pid_trace
) {
206 set_ftrace_pid_function(func
);
207 func
= ftrace_pid_func
;
209 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
210 ftrace_trace_function
= func
;
212 __ftrace_trace_function
= func
;
220 static void ftrace_update_pid_func(void)
224 if (ftrace_trace_function
== ftrace_stub
)
227 func
= ftrace_trace_function
;
229 if (ftrace_pid_trace
) {
230 set_ftrace_pid_function(func
);
231 func
= ftrace_pid_func
;
233 if (func
== ftrace_pid_func
)
234 func
= ftrace_pid_function
;
237 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
238 ftrace_trace_function
= func
;
240 __ftrace_trace_function
= func
;
244 #ifdef CONFIG_FUNCTION_PROFILER
245 struct ftrace_profile
{
246 struct hlist_node node
;
248 unsigned long counter
;
249 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
250 unsigned long long time
;
254 struct ftrace_profile_page
{
255 struct ftrace_profile_page
*next
;
257 struct ftrace_profile records
[];
260 struct ftrace_profile_stat
{
262 struct hlist_head
*hash
;
263 struct ftrace_profile_page
*pages
;
264 struct ftrace_profile_page
*start
;
265 struct tracer_stat stat
;
268 #define PROFILE_RECORDS_SIZE \
269 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
271 #define PROFILES_PER_PAGE \
272 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
274 static int ftrace_profile_bits __read_mostly
;
275 static int ftrace_profile_enabled __read_mostly
;
277 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
278 static DEFINE_MUTEX(ftrace_profile_lock
);
280 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
282 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
285 function_stat_next(void *v
, int idx
)
287 struct ftrace_profile
*rec
= v
;
288 struct ftrace_profile_page
*pg
;
290 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
294 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
298 rec
= &pg
->records
[0];
306 static void *function_stat_start(struct tracer_stat
*trace
)
308 struct ftrace_profile_stat
*stat
=
309 container_of(trace
, struct ftrace_profile_stat
, stat
);
311 if (!stat
|| !stat
->start
)
314 return function_stat_next(&stat
->start
->records
[0], 0);
317 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
318 /* function graph compares on total time */
319 static int function_stat_cmp(void *p1
, void *p2
)
321 struct ftrace_profile
*a
= p1
;
322 struct ftrace_profile
*b
= p2
;
324 if (a
->time
< b
->time
)
326 if (a
->time
> b
->time
)
332 /* not function graph compares against hits */
333 static int function_stat_cmp(void *p1
, void *p2
)
335 struct ftrace_profile
*a
= p1
;
336 struct ftrace_profile
*b
= p2
;
338 if (a
->counter
< b
->counter
)
340 if (a
->counter
> b
->counter
)
347 static int function_stat_headers(struct seq_file
*m
)
349 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
350 seq_printf(m
, " Function Hit Time\n"
351 " -------- --- ----\n");
353 seq_printf(m
, " Function Hit\n"
359 static int function_stat_show(struct seq_file
*m
, void *v
)
361 struct ftrace_profile
*rec
= v
;
362 char str
[KSYM_SYMBOL_LEN
];
363 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
364 static struct trace_seq s
;
365 static DEFINE_MUTEX(mutex
);
369 trace_print_graph_duration(rec
->time
, &s
);
372 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
373 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
375 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
377 trace_print_seq(m
, &s
);
378 mutex_unlock(&mutex
);
385 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
387 struct ftrace_profile_page
*pg
;
389 pg
= stat
->pages
= stat
->start
;
392 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
397 memset(stat
->hash
, 0,
398 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
401 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
403 struct ftrace_profile_page
*pg
;
406 /* If we already allocated, do nothing */
410 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
414 pg
= stat
->start
= stat
->pages
;
416 /* allocate 10 more pages to start */
417 for (i
= 0; i
< 10; i
++) {
418 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
420 * We only care about allocating profile_pages, if
421 * we failed to allocate here, hopefully we will allocate
432 static int ftrace_profile_init_cpu(int cpu
)
434 struct ftrace_profile_stat
*stat
;
437 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
440 /* If the profile is already created, simply reset it */
441 ftrace_profile_reset(stat
);
446 * We are profiling all functions, but usually only a few thousand
447 * functions are hit. We'll make a hash of 1024 items.
449 size
= FTRACE_PROFILE_HASH_SIZE
;
451 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
456 if (!ftrace_profile_bits
) {
459 for (; size
; size
>>= 1)
460 ftrace_profile_bits
++;
463 /* Preallocate a few pages */
464 if (ftrace_profile_pages_init(stat
) < 0) {
473 static int ftrace_profile_init(void)
478 for_each_online_cpu(cpu
) {
479 ret
= ftrace_profile_init_cpu(cpu
);
487 /* interrupts must be disabled */
488 static struct ftrace_profile
*
489 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
491 struct ftrace_profile
*rec
;
492 struct hlist_head
*hhd
;
493 struct hlist_node
*n
;
496 key
= hash_long(ip
, ftrace_profile_bits
);
497 hhd
= &stat
->hash
[key
];
499 if (hlist_empty(hhd
))
502 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
510 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
511 struct ftrace_profile
*rec
)
515 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
516 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
519 /* Interrupts must be disabled calling this */
520 static struct ftrace_profile
*
521 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
,
522 unsigned long ip
, bool alloc_safe
)
524 struct ftrace_profile
*rec
= NULL
;
526 /* prevent recursion */
527 if (atomic_inc_return(&stat
->disabled
) != 1)
530 /* Try to always keep another page available */
531 if (!stat
->pages
->next
&& alloc_safe
)
532 stat
->pages
->next
= (void *)get_zeroed_page(GFP_ATOMIC
);
535 * Try to find the function again since another
536 * task on another CPU could have added it
538 rec
= ftrace_find_profiled_func(stat
, ip
);
542 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
543 if (!stat
->pages
->next
)
545 stat
->pages
= stat
->pages
->next
;
548 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
550 ftrace_add_profile(stat
, rec
);
553 atomic_dec(&stat
->disabled
);
559 * If we are not in an interrupt, or softirq and
560 * and interrupts are disabled and preemption is not enabled
561 * (not in a spinlock) then it should be safe to allocate memory.
563 static bool ftrace_safe_to_allocate(void)
565 return !in_interrupt() && irqs_disabled() && !preempt_count();
569 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
571 struct ftrace_profile_stat
*stat
;
572 struct ftrace_profile
*rec
;
576 if (!ftrace_profile_enabled
)
579 alloc_safe
= ftrace_safe_to_allocate();
581 local_irq_save(flags
);
583 stat
= &__get_cpu_var(ftrace_profile_stats
);
587 rec
= ftrace_find_profiled_func(stat
, ip
);
589 rec
= ftrace_profile_alloc(stat
, ip
, alloc_safe
);
596 local_irq_restore(flags
);
599 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
600 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
602 function_profile_call(trace
->func
, 0);
606 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
608 struct ftrace_profile_stat
*stat
;
609 unsigned long long calltime
;
610 struct ftrace_profile
*rec
;
613 local_irq_save(flags
);
614 stat
= &__get_cpu_var(ftrace_profile_stats
);
618 calltime
= trace
->rettime
- trace
->calltime
;
620 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
623 index
= trace
->depth
;
625 /* Append this call time to the parent time to subtract */
627 current
->ret_stack
[index
- 1].subtime
+= calltime
;
629 if (current
->ret_stack
[index
].subtime
< calltime
)
630 calltime
-= current
->ret_stack
[index
].subtime
;
635 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
637 rec
->time
+= calltime
;
640 local_irq_restore(flags
);
643 static int register_ftrace_profiler(void)
645 return register_ftrace_graph(&profile_graph_return
,
646 &profile_graph_entry
);
649 static void unregister_ftrace_profiler(void)
651 unregister_ftrace_graph();
654 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
656 .func
= function_profile_call
,
659 static int register_ftrace_profiler(void)
661 return register_ftrace_function(&ftrace_profile_ops
);
664 static void unregister_ftrace_profiler(void)
666 unregister_ftrace_function(&ftrace_profile_ops
);
668 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
671 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
672 size_t cnt
, loff_t
*ppos
)
675 char buf
[64]; /* big enough to hold a number */
678 if (cnt
>= sizeof(buf
))
681 if (copy_from_user(&buf
, ubuf
, cnt
))
686 ret
= strict_strtoul(buf
, 10, &val
);
692 mutex_lock(&ftrace_profile_lock
);
693 if (ftrace_profile_enabled
^ val
) {
695 ret
= ftrace_profile_init();
701 ret
= register_ftrace_profiler();
706 ftrace_profile_enabled
= 1;
708 ftrace_profile_enabled
= 0;
709 unregister_ftrace_profiler();
713 mutex_unlock(&ftrace_profile_lock
);
721 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
722 size_t cnt
, loff_t
*ppos
)
724 char buf
[64]; /* big enough to hold a number */
727 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
728 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
731 static const struct file_operations ftrace_profile_fops
= {
732 .open
= tracing_open_generic
,
733 .read
= ftrace_profile_read
,
734 .write
= ftrace_profile_write
,
737 /* used to initialize the real stat files */
738 static struct tracer_stat function_stats __initdata
= {
740 .stat_start
= function_stat_start
,
741 .stat_next
= function_stat_next
,
742 .stat_cmp
= function_stat_cmp
,
743 .stat_headers
= function_stat_headers
,
744 .stat_show
= function_stat_show
747 static void ftrace_profile_debugfs(struct dentry
*d_tracer
)
749 struct ftrace_profile_stat
*stat
;
750 struct dentry
*entry
;
755 for_each_possible_cpu(cpu
) {
756 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
758 /* allocate enough for function name + cpu number */
759 name
= kmalloc(32, GFP_KERNEL
);
762 * The files created are permanent, if something happens
763 * we still do not free memory.
767 "Could not allocate stat file for cpu %d\n",
771 stat
->stat
= function_stats
;
772 snprintf(name
, 32, "function%d", cpu
);
773 stat
->stat
.name
= name
;
774 ret
= register_stat_tracer(&stat
->stat
);
777 "Could not register function stat for cpu %d\n",
784 entry
= debugfs_create_file("function_profile_enabled", 0644,
785 d_tracer
, NULL
, &ftrace_profile_fops
);
787 pr_warning("Could not create debugfs "
788 "'function_profile_enabled' entry\n");
791 #else /* CONFIG_FUNCTION_PROFILER */
792 static void ftrace_profile_debugfs(struct dentry
*d_tracer
)
795 #endif /* CONFIG_FUNCTION_PROFILER */
797 /* set when tracing only a pid */
798 struct pid
*ftrace_pid_trace
;
799 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
801 #ifdef CONFIG_DYNAMIC_FTRACE
803 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
804 # error Dynamic ftrace depends on MCOUNT_RECORD
807 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
809 struct ftrace_func_probe
{
810 struct hlist_node node
;
811 struct ftrace_probe_ops
*ops
;
819 FTRACE_ENABLE_CALLS
= (1 << 0),
820 FTRACE_DISABLE_CALLS
= (1 << 1),
821 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
822 FTRACE_ENABLE_MCOUNT
= (1 << 3),
823 FTRACE_DISABLE_MCOUNT
= (1 << 4),
824 FTRACE_START_FUNC_RET
= (1 << 5),
825 FTRACE_STOP_FUNC_RET
= (1 << 6),
828 static int ftrace_filtered
;
830 static struct dyn_ftrace
*ftrace_new_addrs
;
832 static DEFINE_MUTEX(ftrace_regex_lock
);
835 struct ftrace_page
*next
;
837 struct dyn_ftrace records
[];
840 #define ENTRIES_PER_PAGE \
841 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
843 /* estimate from running different kernels */
844 #define NR_TO_INIT 10000
846 static struct ftrace_page
*ftrace_pages_start
;
847 static struct ftrace_page
*ftrace_pages
;
849 static struct dyn_ftrace
*ftrace_free_records
;
852 * This is a double for. Do not use 'break' to break out of the loop,
853 * you must use a goto.
855 #define do_for_each_ftrace_rec(pg, rec) \
856 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
858 for (_____i = 0; _____i < pg->index; _____i++) { \
859 rec = &pg->records[_____i];
861 #define while_for_each_ftrace_rec() \
865 #ifdef CONFIG_KPROBES
867 static int frozen_record_count
;
869 static inline void freeze_record(struct dyn_ftrace
*rec
)
871 if (!(rec
->flags
& FTRACE_FL_FROZEN
)) {
872 rec
->flags
|= FTRACE_FL_FROZEN
;
873 frozen_record_count
++;
877 static inline void unfreeze_record(struct dyn_ftrace
*rec
)
879 if (rec
->flags
& FTRACE_FL_FROZEN
) {
880 rec
->flags
&= ~FTRACE_FL_FROZEN
;
881 frozen_record_count
--;
885 static inline int record_frozen(struct dyn_ftrace
*rec
)
887 return rec
->flags
& FTRACE_FL_FROZEN
;
890 # define freeze_record(rec) ({ 0; })
891 # define unfreeze_record(rec) ({ 0; })
892 # define record_frozen(rec) ({ 0; })
893 #endif /* CONFIG_KPROBES */
895 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
897 rec
->freelist
= ftrace_free_records
;
898 ftrace_free_records
= rec
;
899 rec
->flags
|= FTRACE_FL_FREE
;
902 void ftrace_release(void *start
, unsigned long size
)
904 struct dyn_ftrace
*rec
;
905 struct ftrace_page
*pg
;
906 unsigned long s
= (unsigned long)start
;
907 unsigned long e
= s
+ size
;
909 if (ftrace_disabled
|| !start
)
912 mutex_lock(&ftrace_lock
);
913 do_for_each_ftrace_rec(pg
, rec
) {
914 if ((rec
->ip
>= s
) && (rec
->ip
< e
) &&
915 !(rec
->flags
& FTRACE_FL_FREE
))
916 ftrace_free_rec(rec
);
917 } while_for_each_ftrace_rec();
918 mutex_unlock(&ftrace_lock
);
921 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
923 struct dyn_ftrace
*rec
;
925 /* First check for freed records */
926 if (ftrace_free_records
) {
927 rec
= ftrace_free_records
;
929 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
930 FTRACE_WARN_ON_ONCE(1);
931 ftrace_free_records
= NULL
;
935 ftrace_free_records
= rec
->freelist
;
936 memset(rec
, 0, sizeof(*rec
));
940 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
941 if (!ftrace_pages
->next
) {
942 /* allocate another page */
944 (void *)get_zeroed_page(GFP_KERNEL
);
945 if (!ftrace_pages
->next
)
948 ftrace_pages
= ftrace_pages
->next
;
951 return &ftrace_pages
->records
[ftrace_pages
->index
++];
954 static struct dyn_ftrace
*
955 ftrace_record_ip(unsigned long ip
)
957 struct dyn_ftrace
*rec
;
962 rec
= ftrace_alloc_dyn_node(ip
);
967 rec
->newlist
= ftrace_new_addrs
;
968 ftrace_new_addrs
= rec
;
973 static void print_ip_ins(const char *fmt
, unsigned char *p
)
977 printk(KERN_CONT
"%s", fmt
);
979 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
980 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
983 static void ftrace_bug(int failed
, unsigned long ip
)
987 FTRACE_WARN_ON_ONCE(1);
988 pr_info("ftrace faulted on modifying ");
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace failed to modify ");
995 print_ip_ins(" actual: ", (unsigned char *)ip
);
996 printk(KERN_CONT
"\n");
999 FTRACE_WARN_ON_ONCE(1);
1000 pr_info("ftrace faulted on writing ");
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on unknown error ");
1012 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1014 unsigned long ftrace_addr
;
1015 unsigned long ip
, fl
;
1017 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1022 * If this record is not to be traced and
1023 * it is not enabled then do nothing.
1025 * If this record is not to be traced and
1026 * it is enabled then disable it.
1029 if (rec
->flags
& FTRACE_FL_NOTRACE
) {
1030 if (rec
->flags
& FTRACE_FL_ENABLED
)
1031 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1035 } else if (ftrace_filtered
&& enable
) {
1040 fl
= rec
->flags
& (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
);
1042 /* Record is filtered and enabled, do nothing */
1043 if (fl
== (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
))
1046 /* Record is not filtered or enabled, do nothing */
1050 /* Record is not filtered but enabled, disable it */
1051 if (fl
== FTRACE_FL_ENABLED
)
1052 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1054 /* Otherwise record is filtered but not enabled, enable it */
1055 rec
->flags
|= FTRACE_FL_ENABLED
;
1057 /* Disable or not filtered */
1060 /* if record is enabled, do nothing */
1061 if (rec
->flags
& FTRACE_FL_ENABLED
)
1064 rec
->flags
|= FTRACE_FL_ENABLED
;
1068 /* if record is not enabled, do nothing */
1069 if (!(rec
->flags
& FTRACE_FL_ENABLED
))
1072 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1076 if (rec
->flags
& FTRACE_FL_ENABLED
)
1077 return ftrace_make_call(rec
, ftrace_addr
);
1079 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1082 static void ftrace_replace_code(int enable
)
1084 struct dyn_ftrace
*rec
;
1085 struct ftrace_page
*pg
;
1088 do_for_each_ftrace_rec(pg
, rec
) {
1090 * Skip over free records, records that have
1091 * failed and not converted.
1093 if (rec
->flags
& FTRACE_FL_FREE
||
1094 rec
->flags
& FTRACE_FL_FAILED
||
1095 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1098 /* ignore updates to this record's mcount site */
1099 if (get_kprobe((void *)rec
->ip
)) {
1103 unfreeze_record(rec
);
1106 failed
= __ftrace_replace_code(rec
, enable
);
1108 rec
->flags
|= FTRACE_FL_FAILED
;
1109 if ((system_state
== SYSTEM_BOOTING
) ||
1110 !core_kernel_text(rec
->ip
)) {
1111 ftrace_free_rec(rec
);
1113 ftrace_bug(failed
, rec
->ip
);
1114 /* Stop processing */
1118 } while_for_each_ftrace_rec();
1122 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1129 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1131 ftrace_bug(ret
, ip
);
1132 rec
->flags
|= FTRACE_FL_FAILED
;
1139 * archs can override this function if they must do something
1140 * before the modifying code is performed.
1142 int __weak
ftrace_arch_code_modify_prepare(void)
1148 * archs can override this function if they must do something
1149 * after the modifying code is performed.
1151 int __weak
ftrace_arch_code_modify_post_process(void)
1156 static int __ftrace_modify_code(void *data
)
1158 int *command
= data
;
1160 if (*command
& FTRACE_ENABLE_CALLS
)
1161 ftrace_replace_code(1);
1162 else if (*command
& FTRACE_DISABLE_CALLS
)
1163 ftrace_replace_code(0);
1165 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1166 ftrace_update_ftrace_func(ftrace_trace_function
);
1168 if (*command
& FTRACE_START_FUNC_RET
)
1169 ftrace_enable_ftrace_graph_caller();
1170 else if (*command
& FTRACE_STOP_FUNC_RET
)
1171 ftrace_disable_ftrace_graph_caller();
1176 static void ftrace_run_update_code(int command
)
1180 ret
= ftrace_arch_code_modify_prepare();
1181 FTRACE_WARN_ON(ret
);
1185 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1187 ret
= ftrace_arch_code_modify_post_process();
1188 FTRACE_WARN_ON(ret
);
1191 static ftrace_func_t saved_ftrace_func
;
1192 static int ftrace_start_up
;
1194 static void ftrace_startup_enable(int command
)
1196 if (saved_ftrace_func
!= ftrace_trace_function
) {
1197 saved_ftrace_func
= ftrace_trace_function
;
1198 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1201 if (!command
|| !ftrace_enabled
)
1204 ftrace_run_update_code(command
);
1207 static void ftrace_startup(int command
)
1209 if (unlikely(ftrace_disabled
))
1213 command
|= FTRACE_ENABLE_CALLS
;
1215 ftrace_startup_enable(command
);
1218 static void ftrace_shutdown(int command
)
1220 if (unlikely(ftrace_disabled
))
1224 if (!ftrace_start_up
)
1225 command
|= FTRACE_DISABLE_CALLS
;
1227 if (saved_ftrace_func
!= ftrace_trace_function
) {
1228 saved_ftrace_func
= ftrace_trace_function
;
1229 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1232 if (!command
|| !ftrace_enabled
)
1235 ftrace_run_update_code(command
);
1238 static void ftrace_startup_sysctl(void)
1240 int command
= FTRACE_ENABLE_MCOUNT
;
1242 if (unlikely(ftrace_disabled
))
1245 /* Force update next time */
1246 saved_ftrace_func
= NULL
;
1247 /* ftrace_start_up is true if we want ftrace running */
1248 if (ftrace_start_up
)
1249 command
|= FTRACE_ENABLE_CALLS
;
1251 ftrace_run_update_code(command
);
1254 static void ftrace_shutdown_sysctl(void)
1256 int command
= FTRACE_DISABLE_MCOUNT
;
1258 if (unlikely(ftrace_disabled
))
1261 /* ftrace_start_up is true if ftrace is running */
1262 if (ftrace_start_up
)
1263 command
|= FTRACE_DISABLE_CALLS
;
1265 ftrace_run_update_code(command
);
1268 static cycle_t ftrace_update_time
;
1269 static unsigned long ftrace_update_cnt
;
1270 unsigned long ftrace_update_tot_cnt
;
1272 static int ftrace_update_code(struct module
*mod
)
1274 struct dyn_ftrace
*p
;
1275 cycle_t start
, stop
;
1277 start
= ftrace_now(raw_smp_processor_id());
1278 ftrace_update_cnt
= 0;
1280 while (ftrace_new_addrs
) {
1282 /* If something went wrong, bail without enabling anything */
1283 if (unlikely(ftrace_disabled
))
1286 p
= ftrace_new_addrs
;
1287 ftrace_new_addrs
= p
->newlist
;
1290 /* convert record (i.e, patch mcount-call with NOP) */
1291 if (ftrace_code_disable(mod
, p
)) {
1292 p
->flags
|= FTRACE_FL_CONVERTED
;
1293 ftrace_update_cnt
++;
1298 stop
= ftrace_now(raw_smp_processor_id());
1299 ftrace_update_time
= stop
- start
;
1300 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1305 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1307 struct ftrace_page
*pg
;
1311 /* allocate a few pages */
1312 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1313 if (!ftrace_pages_start
)
1317 * Allocate a few more pages.
1319 * TODO: have some parser search vmlinux before
1320 * final linking to find all calls to ftrace.
1322 * a) know how many pages to allocate.
1324 * b) set up the table then.
1326 * The dynamic code is still necessary for
1330 pg
= ftrace_pages
= ftrace_pages_start
;
1332 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1333 pr_info("ftrace: allocating %ld entries in %d pages\n",
1334 num_to_init
, cnt
+ 1);
1336 for (i
= 0; i
< cnt
; i
++) {
1337 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1339 /* If we fail, we'll try later anyway */
1350 FTRACE_ITER_FILTER
= (1 << 0),
1351 FTRACE_ITER_CONT
= (1 << 1),
1352 FTRACE_ITER_NOTRACE
= (1 << 2),
1353 FTRACE_ITER_FAILURES
= (1 << 3),
1354 FTRACE_ITER_PRINTALL
= (1 << 4),
1355 FTRACE_ITER_HASH
= (1 << 5),
1358 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1360 struct ftrace_iterator
{
1361 struct ftrace_page
*pg
;
1365 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
1366 unsigned buffer_idx
;
1371 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1373 struct ftrace_iterator
*iter
= m
->private;
1374 struct hlist_node
*hnd
= v
;
1375 struct hlist_head
*hhd
;
1377 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1382 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1385 hhd
= &ftrace_func_hash
[iter
->hidx
];
1387 if (hlist_empty(hhd
)) {
1406 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1408 struct ftrace_iterator
*iter
= m
->private;
1411 iter
->flags
|= FTRACE_ITER_HASH
;
1413 return t_hash_next(m
, p
, pos
);
1416 static int t_hash_show(struct seq_file
*m
, void *v
)
1418 struct ftrace_func_probe
*rec
;
1419 struct hlist_node
*hnd
= v
;
1420 char str
[KSYM_SYMBOL_LEN
];
1422 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1424 if (rec
->ops
->print
)
1425 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1427 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1428 seq_printf(m
, "%s:", str
);
1430 kallsyms_lookup((unsigned long)rec
->ops
->func
, NULL
, NULL
, NULL
, str
);
1431 seq_printf(m
, "%s", str
);
1434 seq_printf(m
, ":%p", rec
->data
);
1441 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1443 struct ftrace_iterator
*iter
= m
->private;
1444 struct dyn_ftrace
*rec
= NULL
;
1446 if (iter
->flags
& FTRACE_ITER_HASH
)
1447 return t_hash_next(m
, v
, pos
);
1451 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1455 if (iter
->idx
>= iter
->pg
->index
) {
1456 if (iter
->pg
->next
) {
1457 iter
->pg
= iter
->pg
->next
;
1464 rec
= &iter
->pg
->records
[iter
->idx
++];
1465 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1467 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1468 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1470 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1471 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1473 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1474 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1476 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1477 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1486 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1488 struct ftrace_iterator
*iter
= m
->private;
1491 mutex_lock(&ftrace_lock
);
1493 * For set_ftrace_filter reading, if we have the filter
1494 * off, we can short cut and just print out that all
1495 * functions are enabled.
1497 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1499 return t_hash_start(m
, pos
);
1500 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1505 if (iter
->flags
& FTRACE_ITER_HASH
)
1506 return t_hash_start(m
, pos
);
1515 p
= t_next(m
, p
, pos
);
1518 return t_hash_start(m
, pos
);
1523 static void t_stop(struct seq_file
*m
, void *p
)
1525 mutex_unlock(&ftrace_lock
);
1528 static int t_show(struct seq_file
*m
, void *v
)
1530 struct ftrace_iterator
*iter
= m
->private;
1531 struct dyn_ftrace
*rec
= v
;
1532 char str
[KSYM_SYMBOL_LEN
];
1534 if (iter
->flags
& FTRACE_ITER_HASH
)
1535 return t_hash_show(m
, v
);
1537 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1538 seq_printf(m
, "#### all functions enabled ####\n");
1545 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1547 seq_printf(m
, "%s\n", str
);
1552 static struct seq_operations show_ftrace_seq_ops
= {
1560 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1562 struct ftrace_iterator
*iter
;
1565 if (unlikely(ftrace_disabled
))
1568 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1572 iter
->pg
= ftrace_pages_start
;
1574 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1576 struct seq_file
*m
= file
->private_data
;
1586 int ftrace_avail_release(struct inode
*inode
, struct file
*file
)
1588 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1589 struct ftrace_iterator
*iter
= m
->private;
1591 seq_release(inode
, file
);
1598 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1602 struct ftrace_iterator
*iter
;
1604 ret
= ftrace_avail_open(inode
, file
);
1606 m
= (struct seq_file
*)file
->private_data
;
1607 iter
= (struct ftrace_iterator
*)m
->private;
1608 iter
->flags
= FTRACE_ITER_FAILURES
;
1615 static void ftrace_filter_reset(int enable
)
1617 struct ftrace_page
*pg
;
1618 struct dyn_ftrace
*rec
;
1619 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1621 mutex_lock(&ftrace_lock
);
1623 ftrace_filtered
= 0;
1624 do_for_each_ftrace_rec(pg
, rec
) {
1625 if (rec
->flags
& FTRACE_FL_FAILED
)
1627 rec
->flags
&= ~type
;
1628 } while_for_each_ftrace_rec();
1629 mutex_unlock(&ftrace_lock
);
1633 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1635 struct ftrace_iterator
*iter
;
1638 if (unlikely(ftrace_disabled
))
1641 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1645 mutex_lock(&ftrace_regex_lock
);
1646 if ((file
->f_mode
& FMODE_WRITE
) &&
1647 !(file
->f_flags
& O_APPEND
))
1648 ftrace_filter_reset(enable
);
1650 if (file
->f_mode
& FMODE_READ
) {
1651 iter
->pg
= ftrace_pages_start
;
1652 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1653 FTRACE_ITER_NOTRACE
;
1655 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1657 struct seq_file
*m
= file
->private_data
;
1662 file
->private_data
= iter
;
1663 mutex_unlock(&ftrace_regex_lock
);
1669 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1671 return ftrace_regex_open(inode
, file
, 1);
1675 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1677 return ftrace_regex_open(inode
, file
, 0);
1681 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1685 if (file
->f_mode
& FMODE_READ
)
1686 ret
= seq_lseek(file
, offset
, origin
);
1688 file
->f_pos
= ret
= 1;
1701 * (static function - no need for kernel doc)
1703 * Pass in a buffer containing a glob and this function will
1704 * set search to point to the search part of the buffer and
1705 * return the type of search it is (see enum above).
1706 * This does modify buff.
1708 * Returns enum type.
1709 * search returns the pointer to use for comparison.
1710 * not returns 1 if buff started with a '!'
1714 ftrace_setup_glob(char *buff
, int len
, char **search
, int *not)
1716 int type
= MATCH_FULL
;
1719 if (buff
[0] == '!') {
1728 for (i
= 0; i
< len
; i
++) {
1729 if (buff
[i
] == '*') {
1732 type
= MATCH_END_ONLY
;
1734 if (type
== MATCH_END_ONLY
)
1735 type
= MATCH_MIDDLE_ONLY
;
1737 type
= MATCH_FRONT_ONLY
;
1747 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1754 if (strcmp(str
, regex
) == 0)
1757 case MATCH_FRONT_ONLY
:
1758 if (strncmp(str
, regex
, len
) == 0)
1761 case MATCH_MIDDLE_ONLY
:
1762 if (strstr(str
, regex
))
1765 case MATCH_END_ONLY
:
1766 ptr
= strstr(str
, regex
);
1767 if (ptr
&& (ptr
[len
] == 0))
1776 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1778 char str
[KSYM_SYMBOL_LEN
];
1780 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1781 return ftrace_match(str
, regex
, len
, type
);
1784 static void ftrace_match_records(char *buff
, int len
, int enable
)
1786 unsigned int search_len
;
1787 struct ftrace_page
*pg
;
1788 struct dyn_ftrace
*rec
;
1794 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1795 type
= ftrace_setup_glob(buff
, len
, &search
, ¬);
1797 search_len
= strlen(search
);
1799 mutex_lock(&ftrace_lock
);
1800 do_for_each_ftrace_rec(pg
, rec
) {
1802 if (rec
->flags
& FTRACE_FL_FAILED
)
1805 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1807 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
);
1822 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1823 char *regex
, int len
, int type
)
1825 char str
[KSYM_SYMBOL_LEN
];
1828 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1830 if (!modname
|| strcmp(modname
, mod
))
1833 /* blank search means to match all funcs in the mod */
1835 return ftrace_match(str
, regex
, len
, type
);
1840 static void ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1842 unsigned search_len
= 0;
1843 struct ftrace_page
*pg
;
1844 struct dyn_ftrace
*rec
;
1845 int type
= MATCH_FULL
;
1846 char *search
= buff
;
1850 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1852 /* blank or '*' mean the same */
1853 if (strcmp(buff
, "*") == 0)
1856 /* handle the case of 'dont filter this module' */
1857 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1863 type
= ftrace_setup_glob(buff
, strlen(buff
), &search
, ¬);
1864 search_len
= strlen(search
);
1867 mutex_lock(&ftrace_lock
);
1868 do_for_each_ftrace_rec(pg
, rec
) {
1870 if (rec
->flags
& FTRACE_FL_FAILED
)
1873 if (ftrace_match_module_record(rec
, mod
,
1874 search
, search_len
, type
)) {
1876 rec
->flags
&= ~flag
;
1880 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1881 ftrace_filtered
= 1;
1883 } while_for_each_ftrace_rec();
1884 mutex_unlock(&ftrace_lock
);
1888 * We register the module command as a template to show others how
1889 * to register the a command as well.
1893 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1898 * cmd == 'mod' because we only registered this func
1899 * for the 'mod' ftrace_func_command.
1900 * But if you register one func with multiple commands,
1901 * you can tell which command was used by the cmd
1905 /* we must have a module name */
1909 mod
= strsep(¶m
, ":");
1913 ftrace_match_module_records(func
, mod
, enable
);
1917 static struct ftrace_func_command ftrace_mod_cmd
= {
1919 .func
= ftrace_mod_callback
,
1922 static int __init
ftrace_mod_cmd_init(void)
1924 return register_ftrace_command(&ftrace_mod_cmd
);
1926 device_initcall(ftrace_mod_cmd_init
);
1929 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1931 struct ftrace_func_probe
*entry
;
1932 struct hlist_head
*hhd
;
1933 struct hlist_node
*n
;
1937 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1939 hhd
= &ftrace_func_hash
[key
];
1941 if (hlist_empty(hhd
))
1945 * Disable preemption for these calls to prevent a RCU grace
1946 * period. This syncs the hash iteration and freeing of items
1947 * on the hash. rcu_read_lock is too dangerous here.
1949 resched
= ftrace_preempt_disable();
1950 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1951 if (entry
->ip
== ip
)
1952 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1954 ftrace_preempt_enable(resched
);
1957 static struct ftrace_ops trace_probe_ops __read_mostly
=
1959 .func
= function_trace_probe_call
,
1962 static int ftrace_probe_registered
;
1964 static void __enable_ftrace_function_probe(void)
1968 if (ftrace_probe_registered
)
1971 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1972 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1976 /* Nothing registered? */
1977 if (i
== FTRACE_FUNC_HASHSIZE
)
1980 __register_ftrace_function(&trace_probe_ops
);
1982 ftrace_probe_registered
= 1;
1985 static void __disable_ftrace_function_probe(void)
1989 if (!ftrace_probe_registered
)
1992 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1993 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1998 /* no more funcs left */
1999 __unregister_ftrace_function(&trace_probe_ops
);
2001 ftrace_probe_registered
= 0;
2005 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
2007 struct ftrace_func_probe
*entry
=
2008 container_of(rhp
, struct ftrace_func_probe
, rcu
);
2010 if (entry
->ops
->free
)
2011 entry
->ops
->free(&entry
->data
);
2017 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2020 struct ftrace_func_probe
*entry
;
2021 struct ftrace_page
*pg
;
2022 struct dyn_ftrace
*rec
;
2028 type
= ftrace_setup_glob(glob
, strlen(glob
), &search
, ¬);
2029 len
= strlen(search
);
2031 /* we do not support '!' for function probes */
2035 mutex_lock(&ftrace_lock
);
2036 do_for_each_ftrace_rec(pg
, rec
) {
2038 if (rec
->flags
& FTRACE_FL_FAILED
)
2041 if (!ftrace_match_record(rec
, search
, len
, type
))
2044 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2046 /* If we did not process any, then return error */
2057 * The caller might want to do something special
2058 * for each function we find. We call the callback
2059 * to give the caller an opportunity to do so.
2061 if (ops
->callback
) {
2062 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2063 /* caller does not like this func */
2070 entry
->ip
= rec
->ip
;
2072 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2073 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2075 } while_for_each_ftrace_rec();
2076 __enable_ftrace_function_probe();
2079 mutex_unlock(&ftrace_lock
);
2085 PROBE_TEST_FUNC
= 1,
2090 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2091 void *data
, int flags
)
2093 struct ftrace_func_probe
*entry
;
2094 struct hlist_node
*n
, *tmp
;
2095 char str
[KSYM_SYMBOL_LEN
];
2096 int type
= MATCH_FULL
;
2100 if (glob
&& (strcmp(glob
, "*") || !strlen(glob
)))
2105 type
= ftrace_setup_glob(glob
, strlen(glob
), &search
, ¬);
2106 len
= strlen(search
);
2108 /* we do not support '!' for function probes */
2113 mutex_lock(&ftrace_lock
);
2114 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2115 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2117 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2119 /* break up if statements for readability */
2120 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2123 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2126 /* do this last, since it is the most expensive */
2128 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2130 if (!ftrace_match(str
, glob
, len
, type
))
2134 hlist_del(&entry
->node
);
2135 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2138 __disable_ftrace_function_probe();
2139 mutex_unlock(&ftrace_lock
);
2143 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2146 __unregister_ftrace_function_probe(glob
, ops
, data
,
2147 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2151 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2153 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2156 void unregister_ftrace_function_probe_all(char *glob
)
2158 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2161 static LIST_HEAD(ftrace_commands
);
2162 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2164 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2166 struct ftrace_func_command
*p
;
2169 mutex_lock(&ftrace_cmd_mutex
);
2170 list_for_each_entry(p
, &ftrace_commands
, list
) {
2171 if (strcmp(cmd
->name
, p
->name
) == 0) {
2176 list_add(&cmd
->list
, &ftrace_commands
);
2178 mutex_unlock(&ftrace_cmd_mutex
);
2183 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2185 struct ftrace_func_command
*p
, *n
;
2188 mutex_lock(&ftrace_cmd_mutex
);
2189 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2190 if (strcmp(cmd
->name
, p
->name
) == 0) {
2192 list_del_init(&p
->list
);
2197 mutex_unlock(&ftrace_cmd_mutex
);
2202 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2204 char *func
, *command
, *next
= buff
;
2205 struct ftrace_func_command
*p
;
2208 func
= strsep(&next
, ":");
2211 ftrace_match_records(func
, len
, enable
);
2217 command
= strsep(&next
, ":");
2219 mutex_lock(&ftrace_cmd_mutex
);
2220 list_for_each_entry(p
, &ftrace_commands
, list
) {
2221 if (strcmp(p
->name
, command
) == 0) {
2222 ret
= p
->func(func
, command
, next
, enable
);
2227 mutex_unlock(&ftrace_cmd_mutex
);
2233 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2234 size_t cnt
, loff_t
*ppos
, int enable
)
2236 struct ftrace_iterator
*iter
;
2241 if (!cnt
|| cnt
< 0)
2244 mutex_lock(&ftrace_regex_lock
);
2246 if (file
->f_mode
& FMODE_READ
) {
2247 struct seq_file
*m
= file
->private_data
;
2250 iter
= file
->private_data
;
2253 iter
->flags
&= ~FTRACE_ITER_CONT
;
2254 iter
->buffer_idx
= 0;
2257 ret
= get_user(ch
, ubuf
++);
2263 if (!(iter
->flags
& ~FTRACE_ITER_CONT
)) {
2264 /* skip white space */
2265 while (cnt
&& isspace(ch
)) {
2266 ret
= get_user(ch
, ubuf
++);
2274 file
->f_pos
+= read
;
2279 iter
->buffer_idx
= 0;
2282 while (cnt
&& !isspace(ch
)) {
2283 if (iter
->buffer_idx
< FTRACE_BUFF_MAX
)
2284 iter
->buffer
[iter
->buffer_idx
++] = ch
;
2289 ret
= get_user(ch
, ubuf
++);
2298 iter
->buffer
[iter
->buffer_idx
] = 0;
2299 ret
= ftrace_process_regex(iter
->buffer
,
2300 iter
->buffer_idx
, enable
);
2303 iter
->buffer_idx
= 0;
2305 iter
->flags
|= FTRACE_ITER_CONT
;
2308 file
->f_pos
+= read
;
2312 mutex_unlock(&ftrace_regex_lock
);
2318 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2319 size_t cnt
, loff_t
*ppos
)
2321 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2325 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2326 size_t cnt
, loff_t
*ppos
)
2328 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2332 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2334 if (unlikely(ftrace_disabled
))
2337 mutex_lock(&ftrace_regex_lock
);
2339 ftrace_filter_reset(enable
);
2341 ftrace_match_records(buf
, len
, enable
);
2342 mutex_unlock(&ftrace_regex_lock
);
2346 * ftrace_set_filter - set a function to filter on in ftrace
2347 * @buf - the string that holds the function filter text.
2348 * @len - the length of the string.
2349 * @reset - non zero to reset all filters before applying this filter.
2351 * Filters denote which functions should be enabled when tracing is enabled.
2352 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2354 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2356 ftrace_set_regex(buf
, len
, reset
, 1);
2360 * ftrace_set_notrace - set a function to not trace in ftrace
2361 * @buf - the string that holds the function notrace text.
2362 * @len - the length of the string.
2363 * @reset - non zero to reset all filters before applying this filter.
2365 * Notrace Filters denote which functions should not be enabled when tracing
2366 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2369 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2371 ftrace_set_regex(buf
, len
, reset
, 0);
2375 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2377 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2378 struct ftrace_iterator
*iter
;
2380 mutex_lock(&ftrace_regex_lock
);
2381 if (file
->f_mode
& FMODE_READ
) {
2384 seq_release(inode
, file
);
2386 iter
= file
->private_data
;
2388 if (iter
->buffer_idx
) {
2390 iter
->buffer
[iter
->buffer_idx
] = 0;
2391 ftrace_match_records(iter
->buffer
, iter
->buffer_idx
, enable
);
2394 mutex_lock(&ftrace_lock
);
2395 if (ftrace_start_up
&& ftrace_enabled
)
2396 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2397 mutex_unlock(&ftrace_lock
);
2400 mutex_unlock(&ftrace_regex_lock
);
2405 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2407 return ftrace_regex_release(inode
, file
, 1);
2411 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2413 return ftrace_regex_release(inode
, file
, 0);
2416 static const struct file_operations ftrace_avail_fops
= {
2417 .open
= ftrace_avail_open
,
2419 .llseek
= seq_lseek
,
2420 .release
= ftrace_avail_release
,
2423 static const struct file_operations ftrace_failures_fops
= {
2424 .open
= ftrace_failures_open
,
2426 .llseek
= seq_lseek
,
2427 .release
= ftrace_avail_release
,
2430 static const struct file_operations ftrace_filter_fops
= {
2431 .open
= ftrace_filter_open
,
2433 .write
= ftrace_filter_write
,
2434 .llseek
= ftrace_regex_lseek
,
2435 .release
= ftrace_filter_release
,
2438 static const struct file_operations ftrace_notrace_fops
= {
2439 .open
= ftrace_notrace_open
,
2441 .write
= ftrace_notrace_write
,
2442 .llseek
= ftrace_regex_lseek
,
2443 .release
= ftrace_notrace_release
,
2446 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2448 static DEFINE_MUTEX(graph_lock
);
2450 int ftrace_graph_count
;
2451 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2454 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2456 unsigned long *array
= m
->private;
2461 if (index
>= ftrace_graph_count
)
2464 return &array
[index
];
2467 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2471 mutex_lock(&graph_lock
);
2473 /* Nothing, tell g_show to print all functions are enabled */
2474 if (!ftrace_graph_count
&& !*pos
)
2477 p
= g_next(m
, p
, pos
);
2482 static void g_stop(struct seq_file
*m
, void *p
)
2484 mutex_unlock(&graph_lock
);
2487 static int g_show(struct seq_file
*m
, void *v
)
2489 unsigned long *ptr
= v
;
2490 char str
[KSYM_SYMBOL_LEN
];
2495 if (ptr
== (unsigned long *)1) {
2496 seq_printf(m
, "#### all functions enabled ####\n");
2500 kallsyms_lookup(*ptr
, NULL
, NULL
, NULL
, str
);
2502 seq_printf(m
, "%s\n", str
);
2507 static struct seq_operations ftrace_graph_seq_ops
= {
2515 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2519 if (unlikely(ftrace_disabled
))
2522 mutex_lock(&graph_lock
);
2523 if ((file
->f_mode
& FMODE_WRITE
) &&
2524 !(file
->f_flags
& O_APPEND
)) {
2525 ftrace_graph_count
= 0;
2526 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2529 if (file
->f_mode
& FMODE_READ
) {
2530 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2532 struct seq_file
*m
= file
->private_data
;
2533 m
->private = ftrace_graph_funcs
;
2536 file
->private_data
= ftrace_graph_funcs
;
2537 mutex_unlock(&graph_lock
);
2543 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2545 struct dyn_ftrace
*rec
;
2546 struct ftrace_page
*pg
;
2554 if (ftrace_disabled
)
2558 type
= ftrace_setup_glob(buffer
, strlen(buffer
), &search
, ¬);
2562 search_len
= strlen(search
);
2564 mutex_lock(&ftrace_lock
);
2565 do_for_each_ftrace_rec(pg
, rec
) {
2567 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2570 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2573 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2574 /* ensure it is not already in the array */
2576 for (i
= 0; i
< *idx
; i
++)
2577 if (array
[i
] == rec
->ip
) {
2582 array
[(*idx
)++] = rec
->ip
;
2586 } while_for_each_ftrace_rec();
2588 mutex_unlock(&ftrace_lock
);
2590 return found
? 0 : -EINVAL
;
2594 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2595 size_t cnt
, loff_t
*ppos
)
2597 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
2598 unsigned long *array
;
2604 if (!cnt
|| cnt
< 0)
2607 mutex_lock(&graph_lock
);
2609 if (ftrace_graph_count
>= FTRACE_GRAPH_MAX_FUNCS
) {
2614 if (file
->f_mode
& FMODE_READ
) {
2615 struct seq_file
*m
= file
->private_data
;
2618 array
= file
->private_data
;
2620 ret
= get_user(ch
, ubuf
++);
2626 /* skip white space */
2627 while (cnt
&& isspace(ch
)) {
2628 ret
= get_user(ch
, ubuf
++);
2641 while (cnt
&& !isspace(ch
)) {
2642 if (index
< FTRACE_BUFF_MAX
)
2643 buffer
[index
++] = ch
;
2648 ret
= get_user(ch
, ubuf
++);
2656 /* we allow only one expression at a time */
2657 ret
= ftrace_set_func(array
, &ftrace_graph_count
, buffer
);
2661 file
->f_pos
+= read
;
2665 mutex_unlock(&graph_lock
);
2670 static const struct file_operations ftrace_graph_fops
= {
2671 .open
= ftrace_graph_open
,
2673 .write
= ftrace_graph_write
,
2675 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2677 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2679 struct dentry
*entry
;
2681 entry
= debugfs_create_file("available_filter_functions", 0444,
2682 d_tracer
, NULL
, &ftrace_avail_fops
);
2684 pr_warning("Could not create debugfs "
2685 "'available_filter_functions' entry\n");
2687 entry
= debugfs_create_file("failures", 0444,
2688 d_tracer
, NULL
, &ftrace_failures_fops
);
2690 pr_warning("Could not create debugfs 'failures' entry\n");
2692 entry
= debugfs_create_file("set_ftrace_filter", 0644, d_tracer
,
2693 NULL
, &ftrace_filter_fops
);
2695 pr_warning("Could not create debugfs "
2696 "'set_ftrace_filter' entry\n");
2698 entry
= debugfs_create_file("set_ftrace_notrace", 0644, d_tracer
,
2699 NULL
, &ftrace_notrace_fops
);
2701 pr_warning("Could not create debugfs "
2702 "'set_ftrace_notrace' entry\n");
2704 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2705 entry
= debugfs_create_file("set_graph_function", 0444, d_tracer
,
2707 &ftrace_graph_fops
);
2709 pr_warning("Could not create debugfs "
2710 "'set_graph_function' entry\n");
2711 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2716 static int ftrace_convert_nops(struct module
*mod
,
2717 unsigned long *start
,
2722 unsigned long flags
;
2724 mutex_lock(&ftrace_lock
);
2727 addr
= ftrace_call_adjust(*p
++);
2729 * Some architecture linkers will pad between
2730 * the different mcount_loc sections of different
2731 * object files to satisfy alignments.
2732 * Skip any NULL pointers.
2736 ftrace_record_ip(addr
);
2739 /* disable interrupts to prevent kstop machine */
2740 local_irq_save(flags
);
2741 ftrace_update_code(mod
);
2742 local_irq_restore(flags
);
2743 mutex_unlock(&ftrace_lock
);
2748 void ftrace_init_module(struct module
*mod
,
2749 unsigned long *start
, unsigned long *end
)
2751 if (ftrace_disabled
|| start
== end
)
2753 ftrace_convert_nops(mod
, start
, end
);
2756 extern unsigned long __start_mcount_loc
[];
2757 extern unsigned long __stop_mcount_loc
[];
2759 void __init
ftrace_init(void)
2761 unsigned long count
, addr
, flags
;
2764 /* Keep the ftrace pointer to the stub */
2765 addr
= (unsigned long)ftrace_stub
;
2767 local_irq_save(flags
);
2768 ftrace_dyn_arch_init(&addr
);
2769 local_irq_restore(flags
);
2771 /* ftrace_dyn_arch_init places the return code in addr */
2775 count
= __stop_mcount_loc
- __start_mcount_loc
;
2777 ret
= ftrace_dyn_table_alloc(count
);
2781 last_ftrace_enabled
= ftrace_enabled
= 1;
2783 ret
= ftrace_convert_nops(NULL
,
2789 ftrace_disabled
= 1;
2794 static int __init
ftrace_nodyn_init(void)
2799 device_initcall(ftrace_nodyn_init
);
2801 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2802 static inline void ftrace_startup_enable(int command
) { }
2803 /* Keep as macros so we do not need to define the commands */
2804 # define ftrace_startup(command) do { } while (0)
2805 # define ftrace_shutdown(command) do { } while (0)
2806 # define ftrace_startup_sysctl() do { } while (0)
2807 # define ftrace_shutdown_sysctl() do { } while (0)
2808 #endif /* CONFIG_DYNAMIC_FTRACE */
2811 ftrace_pid_read(struct file
*file
, char __user
*ubuf
,
2812 size_t cnt
, loff_t
*ppos
)
2817 if (ftrace_pid_trace
== ftrace_swapper_pid
)
2818 r
= sprintf(buf
, "swapper tasks\n");
2819 else if (ftrace_pid_trace
)
2820 r
= sprintf(buf
, "%u\n", pid_vnr(ftrace_pid_trace
));
2822 r
= sprintf(buf
, "no pid\n");
2824 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2827 static void clear_ftrace_swapper(void)
2829 struct task_struct
*p
;
2833 for_each_online_cpu(cpu
) {
2835 clear_tsk_trace_trace(p
);
2840 static void set_ftrace_swapper(void)
2842 struct task_struct
*p
;
2846 for_each_online_cpu(cpu
) {
2848 set_tsk_trace_trace(p
);
2853 static void clear_ftrace_pid(struct pid
*pid
)
2855 struct task_struct
*p
;
2858 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2859 clear_tsk_trace_trace(p
);
2860 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2866 static void set_ftrace_pid(struct pid
*pid
)
2868 struct task_struct
*p
;
2871 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2872 set_tsk_trace_trace(p
);
2873 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2877 static void clear_ftrace_pid_task(struct pid
**pid
)
2879 if (*pid
== ftrace_swapper_pid
)
2880 clear_ftrace_swapper();
2882 clear_ftrace_pid(*pid
);
2887 static void set_ftrace_pid_task(struct pid
*pid
)
2889 if (pid
== ftrace_swapper_pid
)
2890 set_ftrace_swapper();
2892 set_ftrace_pid(pid
);
2896 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
2897 size_t cnt
, loff_t
*ppos
)
2904 if (cnt
>= sizeof(buf
))
2907 if (copy_from_user(&buf
, ubuf
, cnt
))
2912 ret
= strict_strtol(buf
, 10, &val
);
2916 mutex_lock(&ftrace_lock
);
2918 /* disable pid tracing */
2919 if (!ftrace_pid_trace
)
2922 clear_ftrace_pid_task(&ftrace_pid_trace
);
2925 /* swapper task is special */
2927 pid
= ftrace_swapper_pid
;
2928 if (pid
== ftrace_pid_trace
)
2931 pid
= find_get_pid(val
);
2933 if (pid
== ftrace_pid_trace
) {
2939 if (ftrace_pid_trace
)
2940 clear_ftrace_pid_task(&ftrace_pid_trace
);
2945 ftrace_pid_trace
= pid
;
2947 set_ftrace_pid_task(ftrace_pid_trace
);
2950 /* update the function call */
2951 ftrace_update_pid_func();
2952 ftrace_startup_enable(0);
2955 mutex_unlock(&ftrace_lock
);
2960 static const struct file_operations ftrace_pid_fops
= {
2961 .read
= ftrace_pid_read
,
2962 .write
= ftrace_pid_write
,
2965 static __init
int ftrace_init_debugfs(void)
2967 struct dentry
*d_tracer
;
2968 struct dentry
*entry
;
2970 d_tracer
= tracing_init_dentry();
2974 ftrace_init_dyn_debugfs(d_tracer
);
2976 entry
= debugfs_create_file("set_ftrace_pid", 0644, d_tracer
,
2977 NULL
, &ftrace_pid_fops
);
2979 pr_warning("Could not create debugfs "
2980 "'set_ftrace_pid' entry\n");
2982 ftrace_profile_debugfs(d_tracer
);
2986 fs_initcall(ftrace_init_debugfs
);
2989 * ftrace_kill - kill ftrace
2991 * This function should be used by panic code. It stops ftrace
2992 * but in a not so nice way. If you need to simply kill ftrace
2993 * from a non-atomic section, use ftrace_kill.
2995 void ftrace_kill(void)
2997 ftrace_disabled
= 1;
2999 clear_ftrace_function();
3003 * register_ftrace_function - register a function for profiling
3004 * @ops - ops structure that holds the function for profiling.
3006 * Register a function to be called by all functions in the
3009 * Note: @ops->func and all the functions it calls must be labeled
3010 * with "notrace", otherwise it will go into a
3013 int register_ftrace_function(struct ftrace_ops
*ops
)
3017 if (unlikely(ftrace_disabled
))
3020 mutex_lock(&ftrace_lock
);
3022 ret
= __register_ftrace_function(ops
);
3025 mutex_unlock(&ftrace_lock
);
3030 * unregister_ftrace_function - unregister a function for profiling.
3031 * @ops - ops structure that holds the function to unregister
3033 * Unregister a function that was added to be called by ftrace profiling.
3035 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3039 mutex_lock(&ftrace_lock
);
3040 ret
= __unregister_ftrace_function(ops
);
3042 mutex_unlock(&ftrace_lock
);
3048 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3049 struct file
*file
, void __user
*buffer
, size_t *lenp
,
3054 if (unlikely(ftrace_disabled
))
3057 mutex_lock(&ftrace_lock
);
3059 ret
= proc_dointvec(table
, write
, file
, buffer
, lenp
, ppos
);
3061 if (ret
|| !write
|| (last_ftrace_enabled
== ftrace_enabled
))
3064 last_ftrace_enabled
= ftrace_enabled
;
3066 if (ftrace_enabled
) {
3068 ftrace_startup_sysctl();
3070 /* we are starting ftrace again */
3071 if (ftrace_list
!= &ftrace_list_end
) {
3072 if (ftrace_list
->next
== &ftrace_list_end
)
3073 ftrace_trace_function
= ftrace_list
->func
;
3075 ftrace_trace_function
= ftrace_list_func
;
3079 /* stopping ftrace calls (just send to ftrace_stub) */
3080 ftrace_trace_function
= ftrace_stub
;
3082 ftrace_shutdown_sysctl();
3086 mutex_unlock(&ftrace_lock
);
3090 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3092 static atomic_t ftrace_graph_active
;
3093 static struct notifier_block ftrace_suspend_notifier
;
3095 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3100 /* The callbacks that hook a function */
3101 trace_func_graph_ret_t ftrace_graph_return
=
3102 (trace_func_graph_ret_t
)ftrace_stub
;
3103 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3105 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3106 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3110 unsigned long flags
;
3111 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3112 struct task_struct
*g
, *t
;
3114 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3115 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3116 * sizeof(struct ftrace_ret_stack
),
3118 if (!ret_stack_list
[i
]) {
3126 read_lock_irqsave(&tasklist_lock
, flags
);
3127 do_each_thread(g
, t
) {
3133 if (t
->ret_stack
== NULL
) {
3134 t
->curr_ret_stack
= -1;
3135 /* Make sure IRQs see the -1 first: */
3137 t
->ret_stack
= ret_stack_list
[start
++];
3138 atomic_set(&t
->tracing_graph_pause
, 0);
3139 atomic_set(&t
->trace_overrun
, 0);
3141 } while_each_thread(g
, t
);
3144 read_unlock_irqrestore(&tasklist_lock
, flags
);
3146 for (i
= start
; i
< end
; i
++)
3147 kfree(ret_stack_list
[i
]);
3152 ftrace_graph_probe_sched_switch(struct rq
*__rq
, struct task_struct
*prev
,
3153 struct task_struct
*next
)
3155 unsigned long long timestamp
;
3159 * Does the user want to count the time a function was asleep.
3160 * If so, do not update the time stamps.
3162 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3165 timestamp
= trace_clock_local();
3167 prev
->ftrace_timestamp
= timestamp
;
3169 /* only process tasks that we timestamped */
3170 if (!next
->ftrace_timestamp
)
3174 * Update all the counters in next to make up for the
3175 * time next was sleeping.
3177 timestamp
-= next
->ftrace_timestamp
;
3179 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3180 next
->ret_stack
[index
].calltime
+= timestamp
;
3183 /* Allocate a return stack for each task */
3184 static int start_graph_tracing(void)
3186 struct ftrace_ret_stack
**ret_stack_list
;
3189 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3190 sizeof(struct ftrace_ret_stack
*),
3193 if (!ret_stack_list
)
3196 /* The cpu_boot init_task->ret_stack will never be freed */
3197 for_each_online_cpu(cpu
)
3198 ftrace_graph_init_task(idle_task(cpu
));
3201 ret
= alloc_retstack_tasklist(ret_stack_list
);
3202 } while (ret
== -EAGAIN
);
3205 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3207 pr_info("ftrace_graph: Couldn't activate tracepoint"
3208 " probe to kernel_sched_switch\n");
3211 kfree(ret_stack_list
);
3216 * Hibernation protection.
3217 * The state of the current task is too much unstable during
3218 * suspend/restore to disk. We want to protect against that.
3221 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3225 case PM_HIBERNATION_PREPARE
:
3226 pause_graph_tracing();
3229 case PM_POST_HIBERNATION
:
3230 unpause_graph_tracing();
3236 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3237 trace_func_graph_ent_t entryfunc
)
3241 mutex_lock(&ftrace_lock
);
3243 /* we currently allow only one tracer registered at a time */
3244 if (atomic_read(&ftrace_graph_active
)) {
3249 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3250 register_pm_notifier(&ftrace_suspend_notifier
);
3252 atomic_inc(&ftrace_graph_active
);
3253 ret
= start_graph_tracing();
3255 atomic_dec(&ftrace_graph_active
);
3259 ftrace_graph_return
= retfunc
;
3260 ftrace_graph_entry
= entryfunc
;
3262 ftrace_startup(FTRACE_START_FUNC_RET
);
3265 mutex_unlock(&ftrace_lock
);
3269 void unregister_ftrace_graph(void)
3271 mutex_lock(&ftrace_lock
);
3273 atomic_dec(&ftrace_graph_active
);
3274 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3275 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3276 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3277 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3278 unregister_pm_notifier(&ftrace_suspend_notifier
);
3280 mutex_unlock(&ftrace_lock
);
3283 /* Allocate a return stack for newly created task */
3284 void ftrace_graph_init_task(struct task_struct
*t
)
3286 if (atomic_read(&ftrace_graph_active
)) {
3287 t
->ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3288 * sizeof(struct ftrace_ret_stack
),
3292 t
->curr_ret_stack
= -1;
3293 atomic_set(&t
->tracing_graph_pause
, 0);
3294 atomic_set(&t
->trace_overrun
, 0);
3295 t
->ftrace_timestamp
= 0;
3297 t
->ret_stack
= NULL
;
3300 void ftrace_graph_exit_task(struct task_struct
*t
)
3302 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3304 t
->ret_stack
= NULL
;
3305 /* NULL must become visible to IRQs before we free it: */
3311 void ftrace_graph_stop(void)