2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/ctype.h>
28 #include <linux/list.h>
29 #include <linux/hash.h>
30 #include <linux/rcupdate.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
;
63 /* List for set_ftrace_pid's pids. */
64 LIST_HEAD(ftrace_pids
);
66 struct list_head list
;
71 * ftrace_disabled is set when an anomaly is discovered.
72 * ftrace_disabled is much stronger than ftrace_enabled.
74 static int ftrace_disabled __read_mostly
;
76 static DEFINE_MUTEX(ftrace_lock
);
78 static struct ftrace_ops ftrace_list_end __read_mostly
=
83 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
84 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
85 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
86 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
89 * Traverse the ftrace_list, invoking all entries. The reason that we
90 * can use rcu_dereference_raw() is that elements removed from this list
91 * are simply leaked, so there is no need to interact with a grace-period
92 * mechanism. The rcu_dereference_raw() calls are needed to handle
93 * concurrent insertions into the ftrace_list.
95 * Silly Alpha and silly pointer-speculation compiler optimizations!
97 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
99 struct ftrace_ops
*op
= rcu_dereference_raw(ftrace_list
); /*see above*/
101 while (op
!= &ftrace_list_end
) {
102 op
->func(ip
, parent_ip
);
103 op
= rcu_dereference_raw(op
->next
); /*see above*/
107 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
109 if (!test_tsk_trace_trace(current
))
112 ftrace_pid_function(ip
, parent_ip
);
115 static void set_ftrace_pid_function(ftrace_func_t func
)
117 /* do not set ftrace_pid_function to itself! */
118 if (func
!= ftrace_pid_func
)
119 ftrace_pid_function
= func
;
123 * clear_ftrace_function - reset the ftrace function
125 * This NULLs the ftrace function and in essence stops
126 * tracing. There may be lag
128 void clear_ftrace_function(void)
130 ftrace_trace_function
= ftrace_stub
;
131 __ftrace_trace_function
= ftrace_stub
;
132 ftrace_pid_function
= ftrace_stub
;
135 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
137 * For those archs that do not test ftrace_trace_stop in their
138 * mcount call site, we need to do it from C.
140 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
142 if (function_trace_stop
)
145 __ftrace_trace_function(ip
, parent_ip
);
149 static int __register_ftrace_function(struct ftrace_ops
*ops
)
151 ops
->next
= ftrace_list
;
153 * We are entering ops into the ftrace_list but another
154 * CPU might be walking that list. We need to make sure
155 * the ops->next pointer is valid before another CPU sees
156 * the ops pointer included into the ftrace_list.
158 rcu_assign_pointer(ftrace_list
, ops
);
160 if (ftrace_enabled
) {
163 if (ops
->next
== &ftrace_list_end
)
166 func
= ftrace_list_func
;
168 if (!list_empty(&ftrace_pids
)) {
169 set_ftrace_pid_function(func
);
170 func
= ftrace_pid_func
;
174 * For one func, simply call it directly.
175 * For more than one func, call the chain.
177 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
178 ftrace_trace_function
= func
;
180 __ftrace_trace_function
= func
;
181 ftrace_trace_function
= ftrace_test_stop_func
;
188 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
190 struct ftrace_ops
**p
;
193 * If we are removing the last function, then simply point
194 * to the ftrace_stub.
196 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
197 ftrace_trace_function
= ftrace_stub
;
198 ftrace_list
= &ftrace_list_end
;
202 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
211 if (ftrace_enabled
) {
212 /* If we only have one func left, then call that directly */
213 if (ftrace_list
->next
== &ftrace_list_end
) {
214 ftrace_func_t func
= ftrace_list
->func
;
216 if (!list_empty(&ftrace_pids
)) {
217 set_ftrace_pid_function(func
);
218 func
= ftrace_pid_func
;
220 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
221 ftrace_trace_function
= func
;
223 __ftrace_trace_function
= func
;
231 static void ftrace_update_pid_func(void)
235 if (ftrace_trace_function
== ftrace_stub
)
238 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 func
= ftrace_trace_function
;
241 func
= __ftrace_trace_function
;
244 if (!list_empty(&ftrace_pids
)) {
245 set_ftrace_pid_function(func
);
246 func
= ftrace_pid_func
;
248 if (func
== ftrace_pid_func
)
249 func
= ftrace_pid_function
;
252 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
253 ftrace_trace_function
= func
;
255 __ftrace_trace_function
= func
;
259 #ifdef CONFIG_FUNCTION_PROFILER
260 struct ftrace_profile
{
261 struct hlist_node node
;
263 unsigned long counter
;
264 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
265 unsigned long long time
;
269 struct ftrace_profile_page
{
270 struct ftrace_profile_page
*next
;
272 struct ftrace_profile records
[];
275 struct ftrace_profile_stat
{
277 struct hlist_head
*hash
;
278 struct ftrace_profile_page
*pages
;
279 struct ftrace_profile_page
*start
;
280 struct tracer_stat stat
;
283 #define PROFILE_RECORDS_SIZE \
284 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
286 #define PROFILES_PER_PAGE \
287 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
289 static int ftrace_profile_bits __read_mostly
;
290 static int ftrace_profile_enabled __read_mostly
;
292 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
293 static DEFINE_MUTEX(ftrace_profile_lock
);
295 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
297 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
300 function_stat_next(void *v
, int idx
)
302 struct ftrace_profile
*rec
= v
;
303 struct ftrace_profile_page
*pg
;
305 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
311 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
315 rec
= &pg
->records
[0];
323 static void *function_stat_start(struct tracer_stat
*trace
)
325 struct ftrace_profile_stat
*stat
=
326 container_of(trace
, struct ftrace_profile_stat
, stat
);
328 if (!stat
|| !stat
->start
)
331 return function_stat_next(&stat
->start
->records
[0], 0);
334 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
335 /* function graph compares on total time */
336 static int function_stat_cmp(void *p1
, void *p2
)
338 struct ftrace_profile
*a
= p1
;
339 struct ftrace_profile
*b
= p2
;
341 if (a
->time
< b
->time
)
343 if (a
->time
> b
->time
)
349 /* not function graph compares against hits */
350 static int function_stat_cmp(void *p1
, void *p2
)
352 struct ftrace_profile
*a
= p1
;
353 struct ftrace_profile
*b
= p2
;
355 if (a
->counter
< b
->counter
)
357 if (a
->counter
> b
->counter
)
364 static int function_stat_headers(struct seq_file
*m
)
366 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
367 seq_printf(m
, " Function "
372 seq_printf(m
, " Function Hit\n"
378 static int function_stat_show(struct seq_file
*m
, void *v
)
380 struct ftrace_profile
*rec
= v
;
381 char str
[KSYM_SYMBOL_LEN
];
382 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
383 static DEFINE_MUTEX(mutex
);
384 static struct trace_seq s
;
385 unsigned long long avg
;
388 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
389 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
391 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
394 do_div(avg
, rec
->counter
);
398 trace_print_graph_duration(rec
->time
, &s
);
399 trace_seq_puts(&s
, " ");
400 trace_print_graph_duration(avg
, &s
);
401 trace_print_seq(m
, &s
);
402 mutex_unlock(&mutex
);
409 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
411 struct ftrace_profile_page
*pg
;
413 pg
= stat
->pages
= stat
->start
;
416 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
421 memset(stat
->hash
, 0,
422 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
425 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
427 struct ftrace_profile_page
*pg
;
432 /* If we already allocated, do nothing */
436 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
440 #ifdef CONFIG_DYNAMIC_FTRACE
441 functions
= ftrace_update_tot_cnt
;
444 * We do not know the number of functions that exist because
445 * dynamic tracing is what counts them. With past experience
446 * we have around 20K functions. That should be more than enough.
447 * It is highly unlikely we will execute every function in
453 pg
= stat
->start
= stat
->pages
;
455 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
457 for (i
= 0; i
< pages
; i
++) {
458 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
469 unsigned long tmp
= (unsigned long)pg
;
475 free_page((unsigned long)stat
->pages
);
482 static int ftrace_profile_init_cpu(int cpu
)
484 struct ftrace_profile_stat
*stat
;
487 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
490 /* If the profile is already created, simply reset it */
491 ftrace_profile_reset(stat
);
496 * We are profiling all functions, but usually only a few thousand
497 * functions are hit. We'll make a hash of 1024 items.
499 size
= FTRACE_PROFILE_HASH_SIZE
;
501 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
506 if (!ftrace_profile_bits
) {
509 for (; size
; size
>>= 1)
510 ftrace_profile_bits
++;
513 /* Preallocate the function profiling pages */
514 if (ftrace_profile_pages_init(stat
) < 0) {
523 static int ftrace_profile_init(void)
528 for_each_online_cpu(cpu
) {
529 ret
= ftrace_profile_init_cpu(cpu
);
537 /* interrupts must be disabled */
538 static struct ftrace_profile
*
539 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
541 struct ftrace_profile
*rec
;
542 struct hlist_head
*hhd
;
543 struct hlist_node
*n
;
546 key
= hash_long(ip
, ftrace_profile_bits
);
547 hhd
= &stat
->hash
[key
];
549 if (hlist_empty(hhd
))
552 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
560 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
561 struct ftrace_profile
*rec
)
565 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
566 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
570 * The memory is already allocated, this simply finds a new record to use.
572 static struct ftrace_profile
*
573 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
575 struct ftrace_profile
*rec
= NULL
;
577 /* prevent recursion (from NMIs) */
578 if (atomic_inc_return(&stat
->disabled
) != 1)
582 * Try to find the function again since an NMI
583 * could have added it
585 rec
= ftrace_find_profiled_func(stat
, ip
);
589 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
590 if (!stat
->pages
->next
)
592 stat
->pages
= stat
->pages
->next
;
595 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
597 ftrace_add_profile(stat
, rec
);
600 atomic_dec(&stat
->disabled
);
606 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
608 struct ftrace_profile_stat
*stat
;
609 struct ftrace_profile
*rec
;
612 if (!ftrace_profile_enabled
)
615 local_irq_save(flags
);
617 stat
= &__get_cpu_var(ftrace_profile_stats
);
618 if (!stat
->hash
|| !ftrace_profile_enabled
)
621 rec
= ftrace_find_profiled_func(stat
, ip
);
623 rec
= ftrace_profile_alloc(stat
, ip
);
630 local_irq_restore(flags
);
633 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
634 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
636 function_profile_call(trace
->func
, 0);
640 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
642 struct ftrace_profile_stat
*stat
;
643 unsigned long long calltime
;
644 struct ftrace_profile
*rec
;
647 local_irq_save(flags
);
648 stat
= &__get_cpu_var(ftrace_profile_stats
);
649 if (!stat
->hash
|| !ftrace_profile_enabled
)
652 calltime
= trace
->rettime
- trace
->calltime
;
654 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
657 index
= trace
->depth
;
659 /* Append this call time to the parent time to subtract */
661 current
->ret_stack
[index
- 1].subtime
+= calltime
;
663 if (current
->ret_stack
[index
].subtime
< calltime
)
664 calltime
-= current
->ret_stack
[index
].subtime
;
669 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
671 rec
->time
+= calltime
;
674 local_irq_restore(flags
);
677 static int register_ftrace_profiler(void)
679 return register_ftrace_graph(&profile_graph_return
,
680 &profile_graph_entry
);
683 static void unregister_ftrace_profiler(void)
685 unregister_ftrace_graph();
688 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
690 .func
= function_profile_call
,
693 static int register_ftrace_profiler(void)
695 return register_ftrace_function(&ftrace_profile_ops
);
698 static void unregister_ftrace_profiler(void)
700 unregister_ftrace_function(&ftrace_profile_ops
);
702 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
705 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
706 size_t cnt
, loff_t
*ppos
)
709 char buf
[64]; /* big enough to hold a number */
712 if (cnt
>= sizeof(buf
))
715 if (copy_from_user(&buf
, ubuf
, cnt
))
720 ret
= strict_strtoul(buf
, 10, &val
);
726 mutex_lock(&ftrace_profile_lock
);
727 if (ftrace_profile_enabled
^ val
) {
729 ret
= ftrace_profile_init();
735 ret
= register_ftrace_profiler();
740 ftrace_profile_enabled
= 1;
742 ftrace_profile_enabled
= 0;
744 * unregister_ftrace_profiler calls stop_machine
745 * so this acts like an synchronize_sched.
747 unregister_ftrace_profiler();
751 mutex_unlock(&ftrace_profile_lock
);
759 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
760 size_t cnt
, loff_t
*ppos
)
762 char buf
[64]; /* big enough to hold a number */
765 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
766 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
769 static const struct file_operations ftrace_profile_fops
= {
770 .open
= tracing_open_generic
,
771 .read
= ftrace_profile_read
,
772 .write
= ftrace_profile_write
,
775 /* used to initialize the real stat files */
776 static struct tracer_stat function_stats __initdata
= {
778 .stat_start
= function_stat_start
,
779 .stat_next
= function_stat_next
,
780 .stat_cmp
= function_stat_cmp
,
781 .stat_headers
= function_stat_headers
,
782 .stat_show
= function_stat_show
785 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
787 struct ftrace_profile_stat
*stat
;
788 struct dentry
*entry
;
793 for_each_possible_cpu(cpu
) {
794 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
796 /* allocate enough for function name + cpu number */
797 name
= kmalloc(32, GFP_KERNEL
);
800 * The files created are permanent, if something happens
801 * we still do not free memory.
804 "Could not allocate stat file for cpu %d\n",
808 stat
->stat
= function_stats
;
809 snprintf(name
, 32, "function%d", cpu
);
810 stat
->stat
.name
= name
;
811 ret
= register_stat_tracer(&stat
->stat
);
814 "Could not register function stat for cpu %d\n",
821 entry
= debugfs_create_file("function_profile_enabled", 0644,
822 d_tracer
, NULL
, &ftrace_profile_fops
);
824 pr_warning("Could not create debugfs "
825 "'function_profile_enabled' entry\n");
828 #else /* CONFIG_FUNCTION_PROFILER */
829 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
832 #endif /* CONFIG_FUNCTION_PROFILER */
834 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
836 #ifdef CONFIG_DYNAMIC_FTRACE
838 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
839 # error Dynamic ftrace depends on MCOUNT_RECORD
842 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
844 struct ftrace_func_probe
{
845 struct hlist_node node
;
846 struct ftrace_probe_ops
*ops
;
854 FTRACE_ENABLE_CALLS
= (1 << 0),
855 FTRACE_DISABLE_CALLS
= (1 << 1),
856 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
857 FTRACE_ENABLE_MCOUNT
= (1 << 3),
858 FTRACE_DISABLE_MCOUNT
= (1 << 4),
859 FTRACE_START_FUNC_RET
= (1 << 5),
860 FTRACE_STOP_FUNC_RET
= (1 << 6),
863 static int ftrace_filtered
;
865 static struct dyn_ftrace
*ftrace_new_addrs
;
867 static DEFINE_MUTEX(ftrace_regex_lock
);
870 struct ftrace_page
*next
;
872 struct dyn_ftrace records
[];
875 #define ENTRIES_PER_PAGE \
876 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
878 /* estimate from running different kernels */
879 #define NR_TO_INIT 10000
881 static struct ftrace_page
*ftrace_pages_start
;
882 static struct ftrace_page
*ftrace_pages
;
884 static struct dyn_ftrace
*ftrace_free_records
;
887 * This is a double for. Do not use 'break' to break out of the loop,
888 * you must use a goto.
890 #define do_for_each_ftrace_rec(pg, rec) \
891 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
893 for (_____i = 0; _____i < pg->index; _____i++) { \
894 rec = &pg->records[_____i];
896 #define while_for_each_ftrace_rec() \
900 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
902 rec
->freelist
= ftrace_free_records
;
903 ftrace_free_records
= rec
;
904 rec
->flags
|= FTRACE_FL_FREE
;
907 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
909 struct dyn_ftrace
*rec
;
911 /* First check for freed records */
912 if (ftrace_free_records
) {
913 rec
= ftrace_free_records
;
915 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
916 FTRACE_WARN_ON_ONCE(1);
917 ftrace_free_records
= NULL
;
921 ftrace_free_records
= rec
->freelist
;
922 memset(rec
, 0, sizeof(*rec
));
926 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
927 if (!ftrace_pages
->next
) {
928 /* allocate another page */
930 (void *)get_zeroed_page(GFP_KERNEL
);
931 if (!ftrace_pages
->next
)
934 ftrace_pages
= ftrace_pages
->next
;
937 return &ftrace_pages
->records
[ftrace_pages
->index
++];
940 static struct dyn_ftrace
*
941 ftrace_record_ip(unsigned long ip
)
943 struct dyn_ftrace
*rec
;
948 rec
= ftrace_alloc_dyn_node(ip
);
953 rec
->newlist
= ftrace_new_addrs
;
954 ftrace_new_addrs
= rec
;
959 static void print_ip_ins(const char *fmt
, unsigned char *p
)
963 printk(KERN_CONT
"%s", fmt
);
965 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
966 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
969 static void ftrace_bug(int failed
, unsigned long ip
)
973 FTRACE_WARN_ON_ONCE(1);
974 pr_info("ftrace faulted on modifying ");
978 FTRACE_WARN_ON_ONCE(1);
979 pr_info("ftrace failed to modify ");
981 print_ip_ins(" actual: ", (unsigned char *)ip
);
982 printk(KERN_CONT
"\n");
985 FTRACE_WARN_ON_ONCE(1);
986 pr_info("ftrace faulted on writing ");
990 FTRACE_WARN_ON_ONCE(1);
991 pr_info("ftrace faulted on unknown error ");
997 /* Return 1 if the address range is reserved for ftrace */
998 int ftrace_text_reserved(void *start
, void *end
)
1000 struct dyn_ftrace
*rec
;
1001 struct ftrace_page
*pg
;
1003 do_for_each_ftrace_rec(pg
, rec
) {
1004 if (rec
->ip
<= (unsigned long)end
&&
1005 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1007 } while_for_each_ftrace_rec();
1013 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1015 unsigned long ftrace_addr
;
1016 unsigned long flag
= 0UL;
1018 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1021 * If this record is not to be traced or we want to disable it,
1024 * If we want to enable it and filtering is off, then enable it.
1026 * If we want to enable it and filtering is on, enable it only if
1029 if (enable
&& !(rec
->flags
& FTRACE_FL_NOTRACE
)) {
1030 if (!ftrace_filtered
|| (rec
->flags
& FTRACE_FL_FILTER
))
1031 flag
= FTRACE_FL_ENABLED
;
1034 /* If the state of this record hasn't changed, then do nothing */
1035 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1039 rec
->flags
|= FTRACE_FL_ENABLED
;
1040 return ftrace_make_call(rec
, ftrace_addr
);
1043 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1044 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1047 static void ftrace_replace_code(int enable
)
1049 struct dyn_ftrace
*rec
;
1050 struct ftrace_page
*pg
;
1053 do_for_each_ftrace_rec(pg
, rec
) {
1055 * Skip over free records, records that have
1056 * failed and not converted.
1058 if (rec
->flags
& FTRACE_FL_FREE
||
1059 rec
->flags
& FTRACE_FL_FAILED
||
1060 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1063 failed
= __ftrace_replace_code(rec
, enable
);
1065 rec
->flags
|= FTRACE_FL_FAILED
;
1066 ftrace_bug(failed
, rec
->ip
);
1067 /* Stop processing */
1070 } while_for_each_ftrace_rec();
1074 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1081 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1083 ftrace_bug(ret
, ip
);
1084 rec
->flags
|= FTRACE_FL_FAILED
;
1091 * archs can override this function if they must do something
1092 * before the modifying code is performed.
1094 int __weak
ftrace_arch_code_modify_prepare(void)
1100 * archs can override this function if they must do something
1101 * after the modifying code is performed.
1103 int __weak
ftrace_arch_code_modify_post_process(void)
1108 static int __ftrace_modify_code(void *data
)
1110 int *command
= data
;
1112 if (*command
& FTRACE_ENABLE_CALLS
)
1113 ftrace_replace_code(1);
1114 else if (*command
& FTRACE_DISABLE_CALLS
)
1115 ftrace_replace_code(0);
1117 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1118 ftrace_update_ftrace_func(ftrace_trace_function
);
1120 if (*command
& FTRACE_START_FUNC_RET
)
1121 ftrace_enable_ftrace_graph_caller();
1122 else if (*command
& FTRACE_STOP_FUNC_RET
)
1123 ftrace_disable_ftrace_graph_caller();
1128 static void ftrace_run_update_code(int command
)
1132 ret
= ftrace_arch_code_modify_prepare();
1133 FTRACE_WARN_ON(ret
);
1137 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1139 ret
= ftrace_arch_code_modify_post_process();
1140 FTRACE_WARN_ON(ret
);
1143 static ftrace_func_t saved_ftrace_func
;
1144 static int ftrace_start_up
;
1146 static void ftrace_startup_enable(int command
)
1148 if (saved_ftrace_func
!= ftrace_trace_function
) {
1149 saved_ftrace_func
= ftrace_trace_function
;
1150 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1153 if (!command
|| !ftrace_enabled
)
1156 ftrace_run_update_code(command
);
1159 static void ftrace_startup(int command
)
1161 if (unlikely(ftrace_disabled
))
1165 command
|= FTRACE_ENABLE_CALLS
;
1167 ftrace_startup_enable(command
);
1170 static void ftrace_shutdown(int command
)
1172 if (unlikely(ftrace_disabled
))
1177 * Just warn in case of unbalance, no need to kill ftrace, it's not
1178 * critical but the ftrace_call callers may be never nopped again after
1179 * further ftrace uses.
1181 WARN_ON_ONCE(ftrace_start_up
< 0);
1183 if (!ftrace_start_up
)
1184 command
|= FTRACE_DISABLE_CALLS
;
1186 if (saved_ftrace_func
!= ftrace_trace_function
) {
1187 saved_ftrace_func
= ftrace_trace_function
;
1188 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1191 if (!command
|| !ftrace_enabled
)
1194 ftrace_run_update_code(command
);
1197 static void ftrace_startup_sysctl(void)
1199 int command
= FTRACE_ENABLE_MCOUNT
;
1201 if (unlikely(ftrace_disabled
))
1204 /* Force update next time */
1205 saved_ftrace_func
= NULL
;
1206 /* ftrace_start_up is true if we want ftrace running */
1207 if (ftrace_start_up
)
1208 command
|= FTRACE_ENABLE_CALLS
;
1210 ftrace_run_update_code(command
);
1213 static void ftrace_shutdown_sysctl(void)
1215 int command
= FTRACE_DISABLE_MCOUNT
;
1217 if (unlikely(ftrace_disabled
))
1220 /* ftrace_start_up is true if ftrace is running */
1221 if (ftrace_start_up
)
1222 command
|= FTRACE_DISABLE_CALLS
;
1224 ftrace_run_update_code(command
);
1227 static cycle_t ftrace_update_time
;
1228 static unsigned long ftrace_update_cnt
;
1229 unsigned long ftrace_update_tot_cnt
;
1231 static int ftrace_update_code(struct module
*mod
)
1233 struct dyn_ftrace
*p
;
1234 cycle_t start
, stop
;
1236 start
= ftrace_now(raw_smp_processor_id());
1237 ftrace_update_cnt
= 0;
1239 while (ftrace_new_addrs
) {
1241 /* If something went wrong, bail without enabling anything */
1242 if (unlikely(ftrace_disabled
))
1245 p
= ftrace_new_addrs
;
1246 ftrace_new_addrs
= p
->newlist
;
1250 * Do the initial record convertion from mcount jump
1251 * to the NOP instructions.
1253 if (!ftrace_code_disable(mod
, p
)) {
1258 p
->flags
|= FTRACE_FL_CONVERTED
;
1259 ftrace_update_cnt
++;
1262 * If the tracing is enabled, go ahead and enable the record.
1264 * The reason not to enable the record immediatelly is the
1265 * inherent check of ftrace_make_nop/ftrace_make_call for
1266 * correct previous instructions. Making first the NOP
1267 * conversion puts the module to the correct state, thus
1268 * passing the ftrace_make_call check.
1270 if (ftrace_start_up
) {
1271 int failed
= __ftrace_replace_code(p
, 1);
1273 ftrace_bug(failed
, p
->ip
);
1279 stop
= ftrace_now(raw_smp_processor_id());
1280 ftrace_update_time
= stop
- start
;
1281 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1286 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1288 struct ftrace_page
*pg
;
1292 /* allocate a few pages */
1293 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1294 if (!ftrace_pages_start
)
1298 * Allocate a few more pages.
1300 * TODO: have some parser search vmlinux before
1301 * final linking to find all calls to ftrace.
1303 * a) know how many pages to allocate.
1305 * b) set up the table then.
1307 * The dynamic code is still necessary for
1311 pg
= ftrace_pages
= ftrace_pages_start
;
1313 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1314 pr_info("ftrace: allocating %ld entries in %d pages\n",
1315 num_to_init
, cnt
+ 1);
1317 for (i
= 0; i
< cnt
; i
++) {
1318 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1320 /* If we fail, we'll try later anyway */
1331 FTRACE_ITER_FILTER
= (1 << 0),
1332 FTRACE_ITER_NOTRACE
= (1 << 1),
1333 FTRACE_ITER_FAILURES
= (1 << 2),
1334 FTRACE_ITER_PRINTALL
= (1 << 3),
1335 FTRACE_ITER_HASH
= (1 << 4),
1338 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1340 struct ftrace_iterator
{
1341 struct ftrace_page
*pg
;
1345 struct trace_parser parser
;
1349 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1351 struct ftrace_iterator
*iter
= m
->private;
1352 struct hlist_node
*hnd
= v
;
1353 struct hlist_head
*hhd
;
1355 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1360 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1363 hhd
= &ftrace_func_hash
[iter
->hidx
];
1365 if (hlist_empty(hhd
)) {
1384 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1386 struct ftrace_iterator
*iter
= m
->private;
1390 if (!(iter
->flags
& FTRACE_ITER_HASH
))
1393 iter
->flags
|= FTRACE_ITER_HASH
;
1396 for (l
= 0; l
<= *pos
; ) {
1397 p
= t_hash_next(m
, p
, &l
);
1404 static int t_hash_show(struct seq_file
*m
, void *v
)
1406 struct ftrace_func_probe
*rec
;
1407 struct hlist_node
*hnd
= v
;
1409 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1411 if (rec
->ops
->print
)
1412 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1414 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1417 seq_printf(m
, ":%p", rec
->data
);
1424 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1426 struct ftrace_iterator
*iter
= m
->private;
1427 struct dyn_ftrace
*rec
= NULL
;
1429 if (iter
->flags
& FTRACE_ITER_HASH
)
1430 return t_hash_next(m
, v
, pos
);
1434 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1438 if (iter
->idx
>= iter
->pg
->index
) {
1439 if (iter
->pg
->next
) {
1440 iter
->pg
= iter
->pg
->next
;
1445 rec
= &iter
->pg
->records
[iter
->idx
++];
1446 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1448 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1449 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1451 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1452 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1454 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1455 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1457 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1458 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1467 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1469 struct ftrace_iterator
*iter
= m
->private;
1473 mutex_lock(&ftrace_lock
);
1475 * For set_ftrace_filter reading, if we have the filter
1476 * off, we can short cut and just print out that all
1477 * functions are enabled.
1479 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1481 return t_hash_start(m
, pos
);
1482 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1486 if (iter
->flags
& FTRACE_ITER_HASH
)
1487 return t_hash_start(m
, pos
);
1489 iter
->pg
= ftrace_pages_start
;
1491 for (l
= 0; l
<= *pos
; ) {
1492 p
= t_next(m
, p
, &l
);
1497 if (!p
&& iter
->flags
& FTRACE_ITER_FILTER
)
1498 return t_hash_start(m
, pos
);
1503 static void t_stop(struct seq_file
*m
, void *p
)
1505 mutex_unlock(&ftrace_lock
);
1508 static int t_show(struct seq_file
*m
, void *v
)
1510 struct ftrace_iterator
*iter
= m
->private;
1511 struct dyn_ftrace
*rec
= v
;
1513 if (iter
->flags
& FTRACE_ITER_HASH
)
1514 return t_hash_show(m
, v
);
1516 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1517 seq_printf(m
, "#### all functions enabled ####\n");
1524 seq_printf(m
, "%ps\n", (void *)rec
->ip
);
1529 static const struct seq_operations show_ftrace_seq_ops
= {
1537 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1539 struct ftrace_iterator
*iter
;
1542 if (unlikely(ftrace_disabled
))
1545 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1549 iter
->pg
= ftrace_pages_start
;
1551 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1553 struct seq_file
*m
= file
->private_data
;
1564 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1568 struct ftrace_iterator
*iter
;
1570 ret
= ftrace_avail_open(inode
, file
);
1572 m
= (struct seq_file
*)file
->private_data
;
1573 iter
= (struct ftrace_iterator
*)m
->private;
1574 iter
->flags
= FTRACE_ITER_FAILURES
;
1581 static void ftrace_filter_reset(int enable
)
1583 struct ftrace_page
*pg
;
1584 struct dyn_ftrace
*rec
;
1585 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1587 mutex_lock(&ftrace_lock
);
1589 ftrace_filtered
= 0;
1590 do_for_each_ftrace_rec(pg
, rec
) {
1591 if (rec
->flags
& FTRACE_FL_FAILED
)
1593 rec
->flags
&= ~type
;
1594 } while_for_each_ftrace_rec();
1595 mutex_unlock(&ftrace_lock
);
1599 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1601 struct ftrace_iterator
*iter
;
1604 if (unlikely(ftrace_disabled
))
1607 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1611 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
1616 mutex_lock(&ftrace_regex_lock
);
1617 if ((file
->f_mode
& FMODE_WRITE
) &&
1618 (file
->f_flags
& O_TRUNC
))
1619 ftrace_filter_reset(enable
);
1621 if (file
->f_mode
& FMODE_READ
) {
1622 iter
->pg
= ftrace_pages_start
;
1623 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1624 FTRACE_ITER_NOTRACE
;
1626 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1628 struct seq_file
*m
= file
->private_data
;
1631 trace_parser_put(&iter
->parser
);
1635 file
->private_data
= iter
;
1636 mutex_unlock(&ftrace_regex_lock
);
1642 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1644 return ftrace_regex_open(inode
, file
, 1);
1648 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1650 return ftrace_regex_open(inode
, file
, 0);
1654 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1658 if (file
->f_mode
& FMODE_READ
)
1659 ret
= seq_lseek(file
, offset
, origin
);
1661 file
->f_pos
= ret
= 1;
1666 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1673 if (strcmp(str
, regex
) == 0)
1676 case MATCH_FRONT_ONLY
:
1677 if (strncmp(str
, regex
, len
) == 0)
1680 case MATCH_MIDDLE_ONLY
:
1681 if (strstr(str
, regex
))
1684 case MATCH_END_ONLY
:
1686 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
1695 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1697 char str
[KSYM_SYMBOL_LEN
];
1699 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1700 return ftrace_match(str
, regex
, len
, type
);
1703 static int ftrace_match_records(char *buff
, int len
, int enable
)
1705 unsigned int search_len
;
1706 struct ftrace_page
*pg
;
1707 struct dyn_ftrace
*rec
;
1714 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1715 type
= filter_parse_regex(buff
, len
, &search
, ¬);
1717 search_len
= strlen(search
);
1719 mutex_lock(&ftrace_lock
);
1720 do_for_each_ftrace_rec(pg
, rec
) {
1722 if (rec
->flags
& FTRACE_FL_FAILED
)
1725 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1727 rec
->flags
&= ~flag
;
1733 * Only enable filtering if we have a function that
1736 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1737 ftrace_filtered
= 1;
1738 } while_for_each_ftrace_rec();
1739 mutex_unlock(&ftrace_lock
);
1745 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1746 char *regex
, int len
, int type
)
1748 char str
[KSYM_SYMBOL_LEN
];
1751 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1753 if (!modname
|| strcmp(modname
, mod
))
1756 /* blank search means to match all funcs in the mod */
1758 return ftrace_match(str
, regex
, len
, type
);
1763 static int ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1765 unsigned search_len
= 0;
1766 struct ftrace_page
*pg
;
1767 struct dyn_ftrace
*rec
;
1768 int type
= MATCH_FULL
;
1769 char *search
= buff
;
1774 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1776 /* blank or '*' mean the same */
1777 if (strcmp(buff
, "*") == 0)
1780 /* handle the case of 'dont filter this module' */
1781 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1787 type
= filter_parse_regex(buff
, strlen(buff
), &search
, ¬);
1788 search_len
= strlen(search
);
1791 mutex_lock(&ftrace_lock
);
1792 do_for_each_ftrace_rec(pg
, rec
) {
1794 if (rec
->flags
& FTRACE_FL_FAILED
)
1797 if (ftrace_match_module_record(rec
, mod
,
1798 search
, search_len
, type
)) {
1800 rec
->flags
&= ~flag
;
1805 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1806 ftrace_filtered
= 1;
1808 } while_for_each_ftrace_rec();
1809 mutex_unlock(&ftrace_lock
);
1815 * We register the module command as a template to show others how
1816 * to register the a command as well.
1820 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1825 * cmd == 'mod' because we only registered this func
1826 * for the 'mod' ftrace_func_command.
1827 * But if you register one func with multiple commands,
1828 * you can tell which command was used by the cmd
1832 /* we must have a module name */
1836 mod
= strsep(¶m
, ":");
1840 if (ftrace_match_module_records(func
, mod
, enable
))
1845 static struct ftrace_func_command ftrace_mod_cmd
= {
1847 .func
= ftrace_mod_callback
,
1850 static int __init
ftrace_mod_cmd_init(void)
1852 return register_ftrace_command(&ftrace_mod_cmd
);
1854 device_initcall(ftrace_mod_cmd_init
);
1857 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1859 struct ftrace_func_probe
*entry
;
1860 struct hlist_head
*hhd
;
1861 struct hlist_node
*n
;
1865 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1867 hhd
= &ftrace_func_hash
[key
];
1869 if (hlist_empty(hhd
))
1873 * Disable preemption for these calls to prevent a RCU grace
1874 * period. This syncs the hash iteration and freeing of items
1875 * on the hash. rcu_read_lock is too dangerous here.
1877 resched
= ftrace_preempt_disable();
1878 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1879 if (entry
->ip
== ip
)
1880 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1882 ftrace_preempt_enable(resched
);
1885 static struct ftrace_ops trace_probe_ops __read_mostly
=
1887 .func
= function_trace_probe_call
,
1890 static int ftrace_probe_registered
;
1892 static void __enable_ftrace_function_probe(void)
1896 if (ftrace_probe_registered
)
1899 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1900 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1904 /* Nothing registered? */
1905 if (i
== FTRACE_FUNC_HASHSIZE
)
1908 __register_ftrace_function(&trace_probe_ops
);
1910 ftrace_probe_registered
= 1;
1913 static void __disable_ftrace_function_probe(void)
1917 if (!ftrace_probe_registered
)
1920 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1921 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1926 /* no more funcs left */
1927 __unregister_ftrace_function(&trace_probe_ops
);
1929 ftrace_probe_registered
= 0;
1933 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
1935 struct ftrace_func_probe
*entry
=
1936 container_of(rhp
, struct ftrace_func_probe
, rcu
);
1938 if (entry
->ops
->free
)
1939 entry
->ops
->free(&entry
->data
);
1945 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
1948 struct ftrace_func_probe
*entry
;
1949 struct ftrace_page
*pg
;
1950 struct dyn_ftrace
*rec
;
1956 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
1957 len
= strlen(search
);
1959 /* we do not support '!' for function probes */
1963 mutex_lock(&ftrace_lock
);
1964 do_for_each_ftrace_rec(pg
, rec
) {
1966 if (rec
->flags
& FTRACE_FL_FAILED
)
1969 if (!ftrace_match_record(rec
, search
, len
, type
))
1972 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1974 /* If we did not process any, then return error */
1985 * The caller might want to do something special
1986 * for each function we find. We call the callback
1987 * to give the caller an opportunity to do so.
1989 if (ops
->callback
) {
1990 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
1991 /* caller does not like this func */
1998 entry
->ip
= rec
->ip
;
2000 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2001 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2003 } while_for_each_ftrace_rec();
2004 __enable_ftrace_function_probe();
2007 mutex_unlock(&ftrace_lock
);
2013 PROBE_TEST_FUNC
= 1,
2018 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2019 void *data
, int flags
)
2021 struct ftrace_func_probe
*entry
;
2022 struct hlist_node
*n
, *tmp
;
2023 char str
[KSYM_SYMBOL_LEN
];
2024 int type
= MATCH_FULL
;
2028 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2033 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2034 len
= strlen(search
);
2036 /* we do not support '!' for function probes */
2041 mutex_lock(&ftrace_lock
);
2042 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2043 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2045 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2047 /* break up if statements for readability */
2048 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2051 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2054 /* do this last, since it is the most expensive */
2056 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2058 if (!ftrace_match(str
, glob
, len
, type
))
2062 hlist_del(&entry
->node
);
2063 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2066 __disable_ftrace_function_probe();
2067 mutex_unlock(&ftrace_lock
);
2071 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2074 __unregister_ftrace_function_probe(glob
, ops
, data
,
2075 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2079 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2081 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2084 void unregister_ftrace_function_probe_all(char *glob
)
2086 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2089 static LIST_HEAD(ftrace_commands
);
2090 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2092 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2094 struct ftrace_func_command
*p
;
2097 mutex_lock(&ftrace_cmd_mutex
);
2098 list_for_each_entry(p
, &ftrace_commands
, list
) {
2099 if (strcmp(cmd
->name
, p
->name
) == 0) {
2104 list_add(&cmd
->list
, &ftrace_commands
);
2106 mutex_unlock(&ftrace_cmd_mutex
);
2111 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2113 struct ftrace_func_command
*p
, *n
;
2116 mutex_lock(&ftrace_cmd_mutex
);
2117 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2118 if (strcmp(cmd
->name
, p
->name
) == 0) {
2120 list_del_init(&p
->list
);
2125 mutex_unlock(&ftrace_cmd_mutex
);
2130 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2132 char *func
, *command
, *next
= buff
;
2133 struct ftrace_func_command
*p
;
2136 func
= strsep(&next
, ":");
2139 if (ftrace_match_records(func
, len
, enable
))
2146 command
= strsep(&next
, ":");
2148 mutex_lock(&ftrace_cmd_mutex
);
2149 list_for_each_entry(p
, &ftrace_commands
, list
) {
2150 if (strcmp(p
->name
, command
) == 0) {
2151 ret
= p
->func(func
, command
, next
, enable
);
2156 mutex_unlock(&ftrace_cmd_mutex
);
2162 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2163 size_t cnt
, loff_t
*ppos
, int enable
)
2165 struct ftrace_iterator
*iter
;
2166 struct trace_parser
*parser
;
2172 mutex_lock(&ftrace_regex_lock
);
2174 if (file
->f_mode
& FMODE_READ
) {
2175 struct seq_file
*m
= file
->private_data
;
2178 iter
= file
->private_data
;
2180 parser
= &iter
->parser
;
2181 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2183 if (read
>= 0 && trace_parser_loaded(parser
) &&
2184 !trace_parser_cont(parser
)) {
2185 ret
= ftrace_process_regex(parser
->buffer
,
2186 parser
->idx
, enable
);
2187 trace_parser_clear(parser
);
2194 mutex_unlock(&ftrace_regex_lock
);
2200 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2201 size_t cnt
, loff_t
*ppos
)
2203 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2207 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2208 size_t cnt
, loff_t
*ppos
)
2210 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2214 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2216 if (unlikely(ftrace_disabled
))
2219 mutex_lock(&ftrace_regex_lock
);
2221 ftrace_filter_reset(enable
);
2223 ftrace_match_records(buf
, len
, enable
);
2224 mutex_unlock(&ftrace_regex_lock
);
2228 * ftrace_set_filter - set a function to filter on in ftrace
2229 * @buf - the string that holds the function filter text.
2230 * @len - the length of the string.
2231 * @reset - non zero to reset all filters before applying this filter.
2233 * Filters denote which functions should be enabled when tracing is enabled.
2234 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2236 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2238 ftrace_set_regex(buf
, len
, reset
, 1);
2242 * ftrace_set_notrace - set a function to not trace in ftrace
2243 * @buf - the string that holds the function notrace text.
2244 * @len - the length of the string.
2245 * @reset - non zero to reset all filters before applying this filter.
2247 * Notrace Filters denote which functions should not be enabled when tracing
2248 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2251 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2253 ftrace_set_regex(buf
, len
, reset
, 0);
2257 * command line interface to allow users to set filters on boot up.
2259 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2260 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2261 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2263 static int __init
set_ftrace_notrace(char *str
)
2265 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2268 __setup("ftrace_notrace=", set_ftrace_notrace
);
2270 static int __init
set_ftrace_filter(char *str
)
2272 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2275 __setup("ftrace_filter=", set_ftrace_filter
);
2277 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2278 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2279 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
2281 static int __init
set_graph_function(char *str
)
2283 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2286 __setup("ftrace_graph_filter=", set_graph_function
);
2288 static void __init
set_ftrace_early_graph(char *buf
)
2294 func
= strsep(&buf
, ",");
2295 /* we allow only one expression at a time */
2296 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2299 printk(KERN_DEBUG
"ftrace: function %s not "
2300 "traceable\n", func
);
2303 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2305 static void __init
set_ftrace_early_filter(char *buf
, int enable
)
2310 func
= strsep(&buf
, ",");
2311 ftrace_set_regex(func
, strlen(func
), 0, enable
);
2315 static void __init
set_ftrace_early_filters(void)
2317 if (ftrace_filter_buf
[0])
2318 set_ftrace_early_filter(ftrace_filter_buf
, 1);
2319 if (ftrace_notrace_buf
[0])
2320 set_ftrace_early_filter(ftrace_notrace_buf
, 0);
2321 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2322 if (ftrace_graph_buf
[0])
2323 set_ftrace_early_graph(ftrace_graph_buf
);
2324 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2328 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2330 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2331 struct ftrace_iterator
*iter
;
2332 struct trace_parser
*parser
;
2334 mutex_lock(&ftrace_regex_lock
);
2335 if (file
->f_mode
& FMODE_READ
) {
2338 seq_release(inode
, file
);
2340 iter
= file
->private_data
;
2342 parser
= &iter
->parser
;
2343 if (trace_parser_loaded(parser
)) {
2344 parser
->buffer
[parser
->idx
] = 0;
2345 ftrace_match_records(parser
->buffer
, parser
->idx
, enable
);
2348 mutex_lock(&ftrace_lock
);
2349 if (ftrace_start_up
&& ftrace_enabled
)
2350 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2351 mutex_unlock(&ftrace_lock
);
2353 trace_parser_put(parser
);
2356 mutex_unlock(&ftrace_regex_lock
);
2361 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2363 return ftrace_regex_release(inode
, file
, 1);
2367 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2369 return ftrace_regex_release(inode
, file
, 0);
2372 static const struct file_operations ftrace_avail_fops
= {
2373 .open
= ftrace_avail_open
,
2375 .llseek
= seq_lseek
,
2376 .release
= seq_release_private
,
2379 static const struct file_operations ftrace_failures_fops
= {
2380 .open
= ftrace_failures_open
,
2382 .llseek
= seq_lseek
,
2383 .release
= seq_release_private
,
2386 static const struct file_operations ftrace_filter_fops
= {
2387 .open
= ftrace_filter_open
,
2389 .write
= ftrace_filter_write
,
2390 .llseek
= ftrace_regex_lseek
,
2391 .release
= ftrace_filter_release
,
2394 static const struct file_operations ftrace_notrace_fops
= {
2395 .open
= ftrace_notrace_open
,
2397 .write
= ftrace_notrace_write
,
2398 .llseek
= ftrace_regex_lseek
,
2399 .release
= ftrace_notrace_release
,
2402 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2404 static DEFINE_MUTEX(graph_lock
);
2406 int ftrace_graph_count
;
2407 int ftrace_graph_filter_enabled
;
2408 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2411 __g_next(struct seq_file
*m
, loff_t
*pos
)
2413 if (*pos
>= ftrace_graph_count
)
2415 return &ftrace_graph_funcs
[*pos
];
2419 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2422 return __g_next(m
, pos
);
2425 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2427 mutex_lock(&graph_lock
);
2429 /* Nothing, tell g_show to print all functions are enabled */
2430 if (!ftrace_graph_filter_enabled
&& !*pos
)
2433 return __g_next(m
, pos
);
2436 static void g_stop(struct seq_file
*m
, void *p
)
2438 mutex_unlock(&graph_lock
);
2441 static int g_show(struct seq_file
*m
, void *v
)
2443 unsigned long *ptr
= v
;
2448 if (ptr
== (unsigned long *)1) {
2449 seq_printf(m
, "#### all functions enabled ####\n");
2453 seq_printf(m
, "%ps\n", (void *)*ptr
);
2458 static const struct seq_operations ftrace_graph_seq_ops
= {
2466 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2470 if (unlikely(ftrace_disabled
))
2473 mutex_lock(&graph_lock
);
2474 if ((file
->f_mode
& FMODE_WRITE
) &&
2475 (file
->f_flags
& O_TRUNC
)) {
2476 ftrace_graph_filter_enabled
= 0;
2477 ftrace_graph_count
= 0;
2478 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2480 mutex_unlock(&graph_lock
);
2482 if (file
->f_mode
& FMODE_READ
)
2483 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2489 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
2491 if (file
->f_mode
& FMODE_READ
)
2492 seq_release(inode
, file
);
2497 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2499 struct dyn_ftrace
*rec
;
2500 struct ftrace_page
*pg
;
2508 if (ftrace_disabled
)
2512 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
2513 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2516 search_len
= strlen(search
);
2518 mutex_lock(&ftrace_lock
);
2519 do_for_each_ftrace_rec(pg
, rec
) {
2521 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2524 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2525 /* if it is in the array */
2527 for (i
= 0; i
< *idx
; i
++) {
2528 if (array
[i
] == rec
->ip
) {
2537 array
[(*idx
)++] = rec
->ip
;
2538 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2543 array
[i
] = array
[--(*idx
)];
2549 } while_for_each_ftrace_rec();
2551 mutex_unlock(&ftrace_lock
);
2556 ftrace_graph_filter_enabled
= 1;
2561 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2562 size_t cnt
, loff_t
*ppos
)
2564 struct trace_parser parser
;
2570 mutex_lock(&graph_lock
);
2572 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
2577 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
2579 if (read
>= 0 && trace_parser_loaded((&parser
))) {
2580 parser
.buffer
[parser
.idx
] = 0;
2582 /* we allow only one expression at a time */
2583 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2592 trace_parser_put(&parser
);
2594 mutex_unlock(&graph_lock
);
2599 static const struct file_operations ftrace_graph_fops
= {
2600 .open
= ftrace_graph_open
,
2602 .write
= ftrace_graph_write
,
2603 .release
= ftrace_graph_release
,
2605 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2607 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2610 trace_create_file("available_filter_functions", 0444,
2611 d_tracer
, NULL
, &ftrace_avail_fops
);
2613 trace_create_file("failures", 0444,
2614 d_tracer
, NULL
, &ftrace_failures_fops
);
2616 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
2617 NULL
, &ftrace_filter_fops
);
2619 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
2620 NULL
, &ftrace_notrace_fops
);
2622 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2623 trace_create_file("set_graph_function", 0444, d_tracer
,
2625 &ftrace_graph_fops
);
2626 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2631 static int ftrace_process_locs(struct module
*mod
,
2632 unsigned long *start
,
2637 unsigned long flags
;
2639 mutex_lock(&ftrace_lock
);
2642 addr
= ftrace_call_adjust(*p
++);
2644 * Some architecture linkers will pad between
2645 * the different mcount_loc sections of different
2646 * object files to satisfy alignments.
2647 * Skip any NULL pointers.
2651 ftrace_record_ip(addr
);
2654 /* disable interrupts to prevent kstop machine */
2655 local_irq_save(flags
);
2656 ftrace_update_code(mod
);
2657 local_irq_restore(flags
);
2658 mutex_unlock(&ftrace_lock
);
2663 #ifdef CONFIG_MODULES
2664 void ftrace_release_mod(struct module
*mod
)
2666 struct dyn_ftrace
*rec
;
2667 struct ftrace_page
*pg
;
2669 if (ftrace_disabled
)
2672 mutex_lock(&ftrace_lock
);
2673 do_for_each_ftrace_rec(pg
, rec
) {
2674 if (within_module_core(rec
->ip
, mod
)) {
2676 * rec->ip is changed in ftrace_free_rec()
2677 * It should not between s and e if record was freed.
2679 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
2680 ftrace_free_rec(rec
);
2682 } while_for_each_ftrace_rec();
2683 mutex_unlock(&ftrace_lock
);
2686 static void ftrace_init_module(struct module
*mod
,
2687 unsigned long *start
, unsigned long *end
)
2689 if (ftrace_disabled
|| start
== end
)
2691 ftrace_process_locs(mod
, start
, end
);
2694 static int ftrace_module_notify(struct notifier_block
*self
,
2695 unsigned long val
, void *data
)
2697 struct module
*mod
= data
;
2700 case MODULE_STATE_COMING
:
2701 ftrace_init_module(mod
, mod
->ftrace_callsites
,
2702 mod
->ftrace_callsites
+
2703 mod
->num_ftrace_callsites
);
2705 case MODULE_STATE_GOING
:
2706 ftrace_release_mod(mod
);
2713 static int ftrace_module_notify(struct notifier_block
*self
,
2714 unsigned long val
, void *data
)
2718 #endif /* CONFIG_MODULES */
2720 struct notifier_block ftrace_module_nb
= {
2721 .notifier_call
= ftrace_module_notify
,
2725 extern unsigned long __start_mcount_loc
[];
2726 extern unsigned long __stop_mcount_loc
[];
2728 void __init
ftrace_init(void)
2730 unsigned long count
, addr
, flags
;
2733 /* Keep the ftrace pointer to the stub */
2734 addr
= (unsigned long)ftrace_stub
;
2736 local_irq_save(flags
);
2737 ftrace_dyn_arch_init(&addr
);
2738 local_irq_restore(flags
);
2740 /* ftrace_dyn_arch_init places the return code in addr */
2744 count
= __stop_mcount_loc
- __start_mcount_loc
;
2746 ret
= ftrace_dyn_table_alloc(count
);
2750 last_ftrace_enabled
= ftrace_enabled
= 1;
2752 ret
= ftrace_process_locs(NULL
,
2756 ret
= register_module_notifier(&ftrace_module_nb
);
2758 pr_warning("Failed to register trace ftrace module notifier\n");
2760 set_ftrace_early_filters();
2764 ftrace_disabled
= 1;
2769 static int __init
ftrace_nodyn_init(void)
2774 device_initcall(ftrace_nodyn_init
);
2776 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2777 static inline void ftrace_startup_enable(int command
) { }
2778 /* Keep as macros so we do not need to define the commands */
2779 # define ftrace_startup(command) do { } while (0)
2780 # define ftrace_shutdown(command) do { } while (0)
2781 # define ftrace_startup_sysctl() do { } while (0)
2782 # define ftrace_shutdown_sysctl() do { } while (0)
2783 #endif /* CONFIG_DYNAMIC_FTRACE */
2785 static void clear_ftrace_swapper(void)
2787 struct task_struct
*p
;
2791 for_each_online_cpu(cpu
) {
2793 clear_tsk_trace_trace(p
);
2798 static void set_ftrace_swapper(void)
2800 struct task_struct
*p
;
2804 for_each_online_cpu(cpu
) {
2806 set_tsk_trace_trace(p
);
2811 static void clear_ftrace_pid(struct pid
*pid
)
2813 struct task_struct
*p
;
2816 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2817 clear_tsk_trace_trace(p
);
2818 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2824 static void set_ftrace_pid(struct pid
*pid
)
2826 struct task_struct
*p
;
2829 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2830 set_tsk_trace_trace(p
);
2831 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2835 static void clear_ftrace_pid_task(struct pid
*pid
)
2837 if (pid
== ftrace_swapper_pid
)
2838 clear_ftrace_swapper();
2840 clear_ftrace_pid(pid
);
2843 static void set_ftrace_pid_task(struct pid
*pid
)
2845 if (pid
== ftrace_swapper_pid
)
2846 set_ftrace_swapper();
2848 set_ftrace_pid(pid
);
2851 static int ftrace_pid_add(int p
)
2854 struct ftrace_pid
*fpid
;
2857 mutex_lock(&ftrace_lock
);
2860 pid
= ftrace_swapper_pid
;
2862 pid
= find_get_pid(p
);
2869 list_for_each_entry(fpid
, &ftrace_pids
, list
)
2870 if (fpid
->pid
== pid
)
2875 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
2879 list_add(&fpid
->list
, &ftrace_pids
);
2882 set_ftrace_pid_task(pid
);
2884 ftrace_update_pid_func();
2885 ftrace_startup_enable(0);
2887 mutex_unlock(&ftrace_lock
);
2891 if (pid
!= ftrace_swapper_pid
)
2895 mutex_unlock(&ftrace_lock
);
2899 static void ftrace_pid_reset(void)
2901 struct ftrace_pid
*fpid
, *safe
;
2903 mutex_lock(&ftrace_lock
);
2904 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
2905 struct pid
*pid
= fpid
->pid
;
2907 clear_ftrace_pid_task(pid
);
2909 list_del(&fpid
->list
);
2913 ftrace_update_pid_func();
2914 ftrace_startup_enable(0);
2916 mutex_unlock(&ftrace_lock
);
2919 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
2921 mutex_lock(&ftrace_lock
);
2923 if (list_empty(&ftrace_pids
) && (!*pos
))
2926 return seq_list_start(&ftrace_pids
, *pos
);
2929 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2934 return seq_list_next(v
, &ftrace_pids
, pos
);
2937 static void fpid_stop(struct seq_file
*m
, void *p
)
2939 mutex_unlock(&ftrace_lock
);
2942 static int fpid_show(struct seq_file
*m
, void *v
)
2944 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
2946 if (v
== (void *)1) {
2947 seq_printf(m
, "no pid\n");
2951 if (fpid
->pid
== ftrace_swapper_pid
)
2952 seq_printf(m
, "swapper tasks\n");
2954 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
2959 static const struct seq_operations ftrace_pid_sops
= {
2960 .start
= fpid_start
,
2967 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
2971 if ((file
->f_mode
& FMODE_WRITE
) &&
2972 (file
->f_flags
& O_TRUNC
))
2975 if (file
->f_mode
& FMODE_READ
)
2976 ret
= seq_open(file
, &ftrace_pid_sops
);
2982 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
2983 size_t cnt
, loff_t
*ppos
)
2989 if (cnt
>= sizeof(buf
))
2992 if (copy_from_user(&buf
, ubuf
, cnt
))
2998 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
2999 * to clean the filter quietly.
3001 tmp
= strstrip(buf
);
3002 if (strlen(tmp
) == 0)
3005 ret
= strict_strtol(tmp
, 10, &val
);
3009 ret
= ftrace_pid_add(val
);
3011 return ret
? ret
: cnt
;
3015 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3017 if (file
->f_mode
& FMODE_READ
)
3018 seq_release(inode
, file
);
3023 static const struct file_operations ftrace_pid_fops
= {
3024 .open
= ftrace_pid_open
,
3025 .write
= ftrace_pid_write
,
3027 .llseek
= seq_lseek
,
3028 .release
= ftrace_pid_release
,
3031 static __init
int ftrace_init_debugfs(void)
3033 struct dentry
*d_tracer
;
3035 d_tracer
= tracing_init_dentry();
3039 ftrace_init_dyn_debugfs(d_tracer
);
3041 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3042 NULL
, &ftrace_pid_fops
);
3044 ftrace_profile_debugfs(d_tracer
);
3048 fs_initcall(ftrace_init_debugfs
);
3051 * ftrace_kill - kill ftrace
3053 * This function should be used by panic code. It stops ftrace
3054 * but in a not so nice way. If you need to simply kill ftrace
3055 * from a non-atomic section, use ftrace_kill.
3057 void ftrace_kill(void)
3059 ftrace_disabled
= 1;
3061 clear_ftrace_function();
3065 * register_ftrace_function - register a function for profiling
3066 * @ops - ops structure that holds the function for profiling.
3068 * Register a function to be called by all functions in the
3071 * Note: @ops->func and all the functions it calls must be labeled
3072 * with "notrace", otherwise it will go into a
3075 int register_ftrace_function(struct ftrace_ops
*ops
)
3079 if (unlikely(ftrace_disabled
))
3082 mutex_lock(&ftrace_lock
);
3084 ret
= __register_ftrace_function(ops
);
3087 mutex_unlock(&ftrace_lock
);
3092 * unregister_ftrace_function - unregister a function for profiling.
3093 * @ops - ops structure that holds the function to unregister
3095 * Unregister a function that was added to be called by ftrace profiling.
3097 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3101 mutex_lock(&ftrace_lock
);
3102 ret
= __unregister_ftrace_function(ops
);
3104 mutex_unlock(&ftrace_lock
);
3110 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3111 void __user
*buffer
, size_t *lenp
,
3116 if (unlikely(ftrace_disabled
))
3119 mutex_lock(&ftrace_lock
);
3121 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3123 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3126 last_ftrace_enabled
= !!ftrace_enabled
;
3128 if (ftrace_enabled
) {
3130 ftrace_startup_sysctl();
3132 /* we are starting ftrace again */
3133 if (ftrace_list
!= &ftrace_list_end
) {
3134 if (ftrace_list
->next
== &ftrace_list_end
)
3135 ftrace_trace_function
= ftrace_list
->func
;
3137 ftrace_trace_function
= ftrace_list_func
;
3141 /* stopping ftrace calls (just send to ftrace_stub) */
3142 ftrace_trace_function
= ftrace_stub
;
3144 ftrace_shutdown_sysctl();
3148 mutex_unlock(&ftrace_lock
);
3152 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3154 static int ftrace_graph_active
;
3155 static struct notifier_block ftrace_suspend_notifier
;
3157 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3162 /* The callbacks that hook a function */
3163 trace_func_graph_ret_t ftrace_graph_return
=
3164 (trace_func_graph_ret_t
)ftrace_stub
;
3165 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3167 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3168 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3172 unsigned long flags
;
3173 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3174 struct task_struct
*g
, *t
;
3176 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3177 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3178 * sizeof(struct ftrace_ret_stack
),
3180 if (!ret_stack_list
[i
]) {
3188 read_lock_irqsave(&tasklist_lock
, flags
);
3189 do_each_thread(g
, t
) {
3195 if (t
->ret_stack
== NULL
) {
3196 atomic_set(&t
->tracing_graph_pause
, 0);
3197 atomic_set(&t
->trace_overrun
, 0);
3198 t
->curr_ret_stack
= -1;
3199 /* Make sure the tasks see the -1 first: */
3201 t
->ret_stack
= ret_stack_list
[start
++];
3203 } while_each_thread(g
, t
);
3206 read_unlock_irqrestore(&tasklist_lock
, flags
);
3208 for (i
= start
; i
< end
; i
++)
3209 kfree(ret_stack_list
[i
]);
3214 ftrace_graph_probe_sched_switch(struct rq
*__rq
, struct task_struct
*prev
,
3215 struct task_struct
*next
)
3217 unsigned long long timestamp
;
3221 * Does the user want to count the time a function was asleep.
3222 * If so, do not update the time stamps.
3224 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3227 timestamp
= trace_clock_local();
3229 prev
->ftrace_timestamp
= timestamp
;
3231 /* only process tasks that we timestamped */
3232 if (!next
->ftrace_timestamp
)
3236 * Update all the counters in next to make up for the
3237 * time next was sleeping.
3239 timestamp
-= next
->ftrace_timestamp
;
3241 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3242 next
->ret_stack
[index
].calltime
+= timestamp
;
3245 /* Allocate a return stack for each task */
3246 static int start_graph_tracing(void)
3248 struct ftrace_ret_stack
**ret_stack_list
;
3251 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3252 sizeof(struct ftrace_ret_stack
*),
3255 if (!ret_stack_list
)
3258 /* The cpu_boot init_task->ret_stack will never be freed */
3259 for_each_online_cpu(cpu
) {
3260 if (!idle_task(cpu
)->ret_stack
)
3261 ftrace_graph_init_task(idle_task(cpu
));
3265 ret
= alloc_retstack_tasklist(ret_stack_list
);
3266 } while (ret
== -EAGAIN
);
3269 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3271 pr_info("ftrace_graph: Couldn't activate tracepoint"
3272 " probe to kernel_sched_switch\n");
3275 kfree(ret_stack_list
);
3280 * Hibernation protection.
3281 * The state of the current task is too much unstable during
3282 * suspend/restore to disk. We want to protect against that.
3285 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3289 case PM_HIBERNATION_PREPARE
:
3290 pause_graph_tracing();
3293 case PM_POST_HIBERNATION
:
3294 unpause_graph_tracing();
3300 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3301 trace_func_graph_ent_t entryfunc
)
3305 mutex_lock(&ftrace_lock
);
3307 /* we currently allow only one tracer registered at a time */
3308 if (ftrace_graph_active
) {
3313 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3314 register_pm_notifier(&ftrace_suspend_notifier
);
3316 ftrace_graph_active
++;
3317 ret
= start_graph_tracing();
3319 ftrace_graph_active
--;
3323 ftrace_graph_return
= retfunc
;
3324 ftrace_graph_entry
= entryfunc
;
3326 ftrace_startup(FTRACE_START_FUNC_RET
);
3329 mutex_unlock(&ftrace_lock
);
3333 void unregister_ftrace_graph(void)
3335 mutex_lock(&ftrace_lock
);
3337 if (unlikely(!ftrace_graph_active
))
3340 ftrace_graph_active
--;
3341 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3342 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3343 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3344 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3345 unregister_pm_notifier(&ftrace_suspend_notifier
);
3348 mutex_unlock(&ftrace_lock
);
3351 /* Allocate a return stack for newly created task */
3352 void ftrace_graph_init_task(struct task_struct
*t
)
3354 /* Make sure we do not use the parent ret_stack */
3355 t
->ret_stack
= NULL
;
3356 t
->curr_ret_stack
= -1;
3358 if (ftrace_graph_active
) {
3359 struct ftrace_ret_stack
*ret_stack
;
3361 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3362 * sizeof(struct ftrace_ret_stack
),
3366 atomic_set(&t
->tracing_graph_pause
, 0);
3367 atomic_set(&t
->trace_overrun
, 0);
3368 t
->ftrace_timestamp
= 0;
3369 /* make curr_ret_stack visable before we add the ret_stack */
3371 t
->ret_stack
= ret_stack
;
3375 void ftrace_graph_exit_task(struct task_struct
*t
)
3377 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3379 t
->ret_stack
= NULL
;
3380 /* NULL must become visible to IRQs before we free it: */
3386 void ftrace_graph_stop(void)