2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
47 #define FTRACE_WARN_ON_ONCE(cond) \
49 if (WARN_ON_ONCE(cond)) \
53 /* hash bits for specific function selection */
54 #define FTRACE_HASH_BITS 7
55 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
57 /* ftrace_enabled is a method to turn ftrace on or off */
58 int ftrace_enabled __read_mostly
;
59 static int last_ftrace_enabled
;
61 /* Quick disabling of function tracer. */
62 int function_trace_stop
;
64 /* List for set_ftrace_pid's pids. */
65 LIST_HEAD(ftrace_pids
);
67 struct list_head list
;
72 * ftrace_disabled is set when an anomaly is discovered.
73 * ftrace_disabled is much stronger than ftrace_enabled.
75 static int ftrace_disabled __read_mostly
;
77 static DEFINE_MUTEX(ftrace_lock
);
79 static struct ftrace_ops ftrace_list_end __read_mostly
=
84 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
85 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
86 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
87 ftrace_func_t ftrace_pid_function __read_mostly
= ftrace_stub
;
90 * Traverse the ftrace_list, invoking all entries. The reason that we
91 * can use rcu_dereference_raw() is that elements removed from this list
92 * are simply leaked, so there is no need to interact with a grace-period
93 * mechanism. The rcu_dereference_raw() calls are needed to handle
94 * concurrent insertions into the ftrace_list.
96 * Silly Alpha and silly pointer-speculation compiler optimizations!
98 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
100 struct ftrace_ops
*op
= rcu_dereference_raw(ftrace_list
); /*see above*/
102 while (op
!= &ftrace_list_end
) {
103 op
->func(ip
, parent_ip
);
104 op
= rcu_dereference_raw(op
->next
); /*see above*/
108 static void ftrace_pid_func(unsigned long ip
, unsigned long parent_ip
)
110 if (!test_tsk_trace_trace(current
))
113 ftrace_pid_function(ip
, parent_ip
);
116 static void set_ftrace_pid_function(ftrace_func_t func
)
118 /* do not set ftrace_pid_function to itself! */
119 if (func
!= ftrace_pid_func
)
120 ftrace_pid_function
= func
;
124 * clear_ftrace_function - reset the ftrace function
126 * This NULLs the ftrace function and in essence stops
127 * tracing. There may be lag
129 void clear_ftrace_function(void)
131 ftrace_trace_function
= ftrace_stub
;
132 __ftrace_trace_function
= ftrace_stub
;
133 ftrace_pid_function
= ftrace_stub
;
136 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
138 * For those archs that do not test ftrace_trace_stop in their
139 * mcount call site, we need to do it from C.
141 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
143 if (function_trace_stop
)
146 __ftrace_trace_function(ip
, parent_ip
);
150 static int __register_ftrace_function(struct ftrace_ops
*ops
)
152 ops
->next
= ftrace_list
;
154 * We are entering ops into the ftrace_list but another
155 * CPU might be walking that list. We need to make sure
156 * the ops->next pointer is valid before another CPU sees
157 * the ops pointer included into the ftrace_list.
159 rcu_assign_pointer(ftrace_list
, ops
);
161 if (ftrace_enabled
) {
164 if (ops
->next
== &ftrace_list_end
)
167 func
= ftrace_list_func
;
169 if (!list_empty(&ftrace_pids
)) {
170 set_ftrace_pid_function(func
);
171 func
= ftrace_pid_func
;
175 * For one func, simply call it directly.
176 * For more than one func, call the chain.
178 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
179 ftrace_trace_function
= func
;
181 __ftrace_trace_function
= func
;
182 ftrace_trace_function
= ftrace_test_stop_func
;
189 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
191 struct ftrace_ops
**p
;
194 * If we are removing the last function, then simply point
195 * to the ftrace_stub.
197 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
198 ftrace_trace_function
= ftrace_stub
;
199 ftrace_list
= &ftrace_list_end
;
203 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
212 if (ftrace_enabled
) {
213 /* If we only have one func left, then call that directly */
214 if (ftrace_list
->next
== &ftrace_list_end
) {
215 ftrace_func_t func
= ftrace_list
->func
;
217 if (!list_empty(&ftrace_pids
)) {
218 set_ftrace_pid_function(func
);
219 func
= ftrace_pid_func
;
221 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
222 ftrace_trace_function
= func
;
224 __ftrace_trace_function
= func
;
232 static void ftrace_update_pid_func(void)
236 if (ftrace_trace_function
== ftrace_stub
)
239 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
240 func
= ftrace_trace_function
;
242 func
= __ftrace_trace_function
;
245 if (!list_empty(&ftrace_pids
)) {
246 set_ftrace_pid_function(func
);
247 func
= ftrace_pid_func
;
249 if (func
== ftrace_pid_func
)
250 func
= ftrace_pid_function
;
253 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
254 ftrace_trace_function
= func
;
256 __ftrace_trace_function
= func
;
260 #ifdef CONFIG_FUNCTION_PROFILER
261 struct ftrace_profile
{
262 struct hlist_node node
;
264 unsigned long counter
;
265 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
266 unsigned long long time
;
267 unsigned long long time_squared
;
271 struct ftrace_profile_page
{
272 struct ftrace_profile_page
*next
;
274 struct ftrace_profile records
[];
277 struct ftrace_profile_stat
{
279 struct hlist_head
*hash
;
280 struct ftrace_profile_page
*pages
;
281 struct ftrace_profile_page
*start
;
282 struct tracer_stat stat
;
285 #define PROFILE_RECORDS_SIZE \
286 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
288 #define PROFILES_PER_PAGE \
289 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
291 static int ftrace_profile_bits __read_mostly
;
292 static int ftrace_profile_enabled __read_mostly
;
294 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
295 static DEFINE_MUTEX(ftrace_profile_lock
);
297 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
299 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
302 function_stat_next(void *v
, int idx
)
304 struct ftrace_profile
*rec
= v
;
305 struct ftrace_profile_page
*pg
;
307 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
313 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
317 rec
= &pg
->records
[0];
325 static void *function_stat_start(struct tracer_stat
*trace
)
327 struct ftrace_profile_stat
*stat
=
328 container_of(trace
, struct ftrace_profile_stat
, stat
);
330 if (!stat
|| !stat
->start
)
333 return function_stat_next(&stat
->start
->records
[0], 0);
336 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
337 /* function graph compares on total time */
338 static int function_stat_cmp(void *p1
, void *p2
)
340 struct ftrace_profile
*a
= p1
;
341 struct ftrace_profile
*b
= p2
;
343 if (a
->time
< b
->time
)
345 if (a
->time
> b
->time
)
351 /* not function graph compares against hits */
352 static int function_stat_cmp(void *p1
, void *p2
)
354 struct ftrace_profile
*a
= p1
;
355 struct ftrace_profile
*b
= p2
;
357 if (a
->counter
< b
->counter
)
359 if (a
->counter
> b
->counter
)
366 static int function_stat_headers(struct seq_file
*m
)
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 seq_printf(m
, " Function "
372 "--- ---- --- ---\n");
374 seq_printf(m
, " Function Hit\n"
380 static int function_stat_show(struct seq_file
*m
, void *v
)
382 struct ftrace_profile
*rec
= v
;
383 char str
[KSYM_SYMBOL_LEN
];
385 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
386 static struct trace_seq s
;
387 unsigned long long avg
;
388 unsigned long long stddev
;
390 mutex_lock(&ftrace_profile_lock
);
392 /* we raced with function_profile_reset() */
393 if (unlikely(rec
->counter
== 0)) {
398 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
399 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
401 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
404 do_div(avg
, rec
->counter
);
406 /* Sample standard deviation (s^2) */
407 if (rec
->counter
<= 1)
410 stddev
= rec
->time_squared
- rec
->counter
* avg
* avg
;
412 * Divide only 1000 for ns^2 -> us^2 conversion.
413 * trace_print_graph_duration will divide 1000 again.
415 do_div(stddev
, (rec
->counter
- 1) * 1000);
419 trace_print_graph_duration(rec
->time
, &s
);
420 trace_seq_puts(&s
, " ");
421 trace_print_graph_duration(avg
, &s
);
422 trace_seq_puts(&s
, " ");
423 trace_print_graph_duration(stddev
, &s
);
424 trace_print_seq(m
, &s
);
428 mutex_unlock(&ftrace_profile_lock
);
433 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
435 struct ftrace_profile_page
*pg
;
437 pg
= stat
->pages
= stat
->start
;
440 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
445 memset(stat
->hash
, 0,
446 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
449 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
451 struct ftrace_profile_page
*pg
;
456 /* If we already allocated, do nothing */
460 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
464 #ifdef CONFIG_DYNAMIC_FTRACE
465 functions
= ftrace_update_tot_cnt
;
468 * We do not know the number of functions that exist because
469 * dynamic tracing is what counts them. With past experience
470 * we have around 20K functions. That should be more than enough.
471 * It is highly unlikely we will execute every function in
477 pg
= stat
->start
= stat
->pages
;
479 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
481 for (i
= 0; i
< pages
; i
++) {
482 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
493 unsigned long tmp
= (unsigned long)pg
;
499 free_page((unsigned long)stat
->pages
);
506 static int ftrace_profile_init_cpu(int cpu
)
508 struct ftrace_profile_stat
*stat
;
511 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
514 /* If the profile is already created, simply reset it */
515 ftrace_profile_reset(stat
);
520 * We are profiling all functions, but usually only a few thousand
521 * functions are hit. We'll make a hash of 1024 items.
523 size
= FTRACE_PROFILE_HASH_SIZE
;
525 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
530 if (!ftrace_profile_bits
) {
533 for (; size
; size
>>= 1)
534 ftrace_profile_bits
++;
537 /* Preallocate the function profiling pages */
538 if (ftrace_profile_pages_init(stat
) < 0) {
547 static int ftrace_profile_init(void)
552 for_each_online_cpu(cpu
) {
553 ret
= ftrace_profile_init_cpu(cpu
);
561 /* interrupts must be disabled */
562 static struct ftrace_profile
*
563 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
565 struct ftrace_profile
*rec
;
566 struct hlist_head
*hhd
;
567 struct hlist_node
*n
;
570 key
= hash_long(ip
, ftrace_profile_bits
);
571 hhd
= &stat
->hash
[key
];
573 if (hlist_empty(hhd
))
576 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
584 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
585 struct ftrace_profile
*rec
)
589 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
590 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
594 * The memory is already allocated, this simply finds a new record to use.
596 static struct ftrace_profile
*
597 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
599 struct ftrace_profile
*rec
= NULL
;
601 /* prevent recursion (from NMIs) */
602 if (atomic_inc_return(&stat
->disabled
) != 1)
606 * Try to find the function again since an NMI
607 * could have added it
609 rec
= ftrace_find_profiled_func(stat
, ip
);
613 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
614 if (!stat
->pages
->next
)
616 stat
->pages
= stat
->pages
->next
;
619 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
621 ftrace_add_profile(stat
, rec
);
624 atomic_dec(&stat
->disabled
);
630 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
632 struct ftrace_profile_stat
*stat
;
633 struct ftrace_profile
*rec
;
636 if (!ftrace_profile_enabled
)
639 local_irq_save(flags
);
641 stat
= &__get_cpu_var(ftrace_profile_stats
);
642 if (!stat
->hash
|| !ftrace_profile_enabled
)
645 rec
= ftrace_find_profiled_func(stat
, ip
);
647 rec
= ftrace_profile_alloc(stat
, ip
);
654 local_irq_restore(flags
);
657 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
658 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
660 function_profile_call(trace
->func
, 0);
664 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
666 struct ftrace_profile_stat
*stat
;
667 unsigned long long calltime
;
668 struct ftrace_profile
*rec
;
671 local_irq_save(flags
);
672 stat
= &__get_cpu_var(ftrace_profile_stats
);
673 if (!stat
->hash
|| !ftrace_profile_enabled
)
676 /* If the calltime was zero'd ignore it */
677 if (!trace
->calltime
)
680 calltime
= trace
->rettime
- trace
->calltime
;
682 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
685 index
= trace
->depth
;
687 /* Append this call time to the parent time to subtract */
689 current
->ret_stack
[index
- 1].subtime
+= calltime
;
691 if (current
->ret_stack
[index
].subtime
< calltime
)
692 calltime
-= current
->ret_stack
[index
].subtime
;
697 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
699 rec
->time
+= calltime
;
700 rec
->time_squared
+= calltime
* calltime
;
704 local_irq_restore(flags
);
707 static int register_ftrace_profiler(void)
709 return register_ftrace_graph(&profile_graph_return
,
710 &profile_graph_entry
);
713 static void unregister_ftrace_profiler(void)
715 unregister_ftrace_graph();
718 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
720 .func
= function_profile_call
,
723 static int register_ftrace_profiler(void)
725 return register_ftrace_function(&ftrace_profile_ops
);
728 static void unregister_ftrace_profiler(void)
730 unregister_ftrace_function(&ftrace_profile_ops
);
732 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
735 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
736 size_t cnt
, loff_t
*ppos
)
739 char buf
[64]; /* big enough to hold a number */
742 if (cnt
>= sizeof(buf
))
745 if (copy_from_user(&buf
, ubuf
, cnt
))
750 ret
= strict_strtoul(buf
, 10, &val
);
756 mutex_lock(&ftrace_profile_lock
);
757 if (ftrace_profile_enabled
^ val
) {
759 ret
= ftrace_profile_init();
765 ret
= register_ftrace_profiler();
770 ftrace_profile_enabled
= 1;
772 ftrace_profile_enabled
= 0;
774 * unregister_ftrace_profiler calls stop_machine
775 * so this acts like an synchronize_sched.
777 unregister_ftrace_profiler();
781 mutex_unlock(&ftrace_profile_lock
);
789 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
790 size_t cnt
, loff_t
*ppos
)
792 char buf
[64]; /* big enough to hold a number */
795 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
796 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
799 static const struct file_operations ftrace_profile_fops
= {
800 .open
= tracing_open_generic
,
801 .read
= ftrace_profile_read
,
802 .write
= ftrace_profile_write
,
803 .llseek
= default_llseek
,
806 /* used to initialize the real stat files */
807 static struct tracer_stat function_stats __initdata
= {
809 .stat_start
= function_stat_start
,
810 .stat_next
= function_stat_next
,
811 .stat_cmp
= function_stat_cmp
,
812 .stat_headers
= function_stat_headers
,
813 .stat_show
= function_stat_show
816 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
818 struct ftrace_profile_stat
*stat
;
819 struct dentry
*entry
;
824 for_each_possible_cpu(cpu
) {
825 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
827 /* allocate enough for function name + cpu number */
828 name
= kmalloc(32, GFP_KERNEL
);
831 * The files created are permanent, if something happens
832 * we still do not free memory.
835 "Could not allocate stat file for cpu %d\n",
839 stat
->stat
= function_stats
;
840 snprintf(name
, 32, "function%d", cpu
);
841 stat
->stat
.name
= name
;
842 ret
= register_stat_tracer(&stat
->stat
);
845 "Could not register function stat for cpu %d\n",
852 entry
= debugfs_create_file("function_profile_enabled", 0644,
853 d_tracer
, NULL
, &ftrace_profile_fops
);
855 pr_warning("Could not create debugfs "
856 "'function_profile_enabled' entry\n");
859 #else /* CONFIG_FUNCTION_PROFILER */
860 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
863 #endif /* CONFIG_FUNCTION_PROFILER */
865 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
867 #ifdef CONFIG_DYNAMIC_FTRACE
869 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
870 # error Dynamic ftrace depends on MCOUNT_RECORD
873 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
875 struct ftrace_func_probe
{
876 struct hlist_node node
;
877 struct ftrace_probe_ops
*ops
;
885 FTRACE_ENABLE_CALLS
= (1 << 0),
886 FTRACE_DISABLE_CALLS
= (1 << 1),
887 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
888 FTRACE_ENABLE_MCOUNT
= (1 << 3),
889 FTRACE_DISABLE_MCOUNT
= (1 << 4),
890 FTRACE_START_FUNC_RET
= (1 << 5),
891 FTRACE_STOP_FUNC_RET
= (1 << 6),
894 static int ftrace_filtered
;
896 static struct dyn_ftrace
*ftrace_new_addrs
;
898 static DEFINE_MUTEX(ftrace_regex_lock
);
901 struct ftrace_page
*next
;
903 struct dyn_ftrace records
[];
906 #define ENTRIES_PER_PAGE \
907 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
909 /* estimate from running different kernels */
910 #define NR_TO_INIT 10000
912 static struct ftrace_page
*ftrace_pages_start
;
913 static struct ftrace_page
*ftrace_pages
;
915 static struct dyn_ftrace
*ftrace_free_records
;
918 * This is a double for. Do not use 'break' to break out of the loop,
919 * you must use a goto.
921 #define do_for_each_ftrace_rec(pg, rec) \
922 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
924 for (_____i = 0; _____i < pg->index; _____i++) { \
925 rec = &pg->records[_____i];
927 #define while_for_each_ftrace_rec() \
931 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
933 rec
->freelist
= ftrace_free_records
;
934 ftrace_free_records
= rec
;
935 rec
->flags
|= FTRACE_FL_FREE
;
938 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
940 struct dyn_ftrace
*rec
;
942 /* First check for freed records */
943 if (ftrace_free_records
) {
944 rec
= ftrace_free_records
;
946 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
947 FTRACE_WARN_ON_ONCE(1);
948 ftrace_free_records
= NULL
;
952 ftrace_free_records
= rec
->freelist
;
953 memset(rec
, 0, sizeof(*rec
));
957 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
958 if (!ftrace_pages
->next
) {
959 /* allocate another page */
961 (void *)get_zeroed_page(GFP_KERNEL
);
962 if (!ftrace_pages
->next
)
965 ftrace_pages
= ftrace_pages
->next
;
968 return &ftrace_pages
->records
[ftrace_pages
->index
++];
971 static struct dyn_ftrace
*
972 ftrace_record_ip(unsigned long ip
)
974 struct dyn_ftrace
*rec
;
979 rec
= ftrace_alloc_dyn_node(ip
);
984 rec
->newlist
= ftrace_new_addrs
;
985 ftrace_new_addrs
= rec
;
990 static void print_ip_ins(const char *fmt
, unsigned char *p
)
994 printk(KERN_CONT
"%s", fmt
);
996 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
997 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
1000 static void ftrace_bug(int failed
, unsigned long ip
)
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on modifying ");
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace failed to modify ");
1012 print_ip_ins(" actual: ", (unsigned char *)ip
);
1013 printk(KERN_CONT
"\n");
1016 FTRACE_WARN_ON_ONCE(1);
1017 pr_info("ftrace faulted on writing ");
1021 FTRACE_WARN_ON_ONCE(1);
1022 pr_info("ftrace faulted on unknown error ");
1028 /* Return 1 if the address range is reserved for ftrace */
1029 int ftrace_text_reserved(void *start
, void *end
)
1031 struct dyn_ftrace
*rec
;
1032 struct ftrace_page
*pg
;
1034 do_for_each_ftrace_rec(pg
, rec
) {
1035 if (rec
->ip
<= (unsigned long)end
&&
1036 rec
->ip
+ MCOUNT_INSN_SIZE
> (unsigned long)start
)
1038 } while_for_each_ftrace_rec();
1044 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1046 unsigned long ftrace_addr
;
1047 unsigned long flag
= 0UL;
1049 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1052 * If this record is not to be traced or we want to disable it,
1055 * If we want to enable it and filtering is off, then enable it.
1057 * If we want to enable it and filtering is on, enable it only if
1060 if (enable
&& !(rec
->flags
& FTRACE_FL_NOTRACE
)) {
1061 if (!ftrace_filtered
|| (rec
->flags
& FTRACE_FL_FILTER
))
1062 flag
= FTRACE_FL_ENABLED
;
1065 /* If the state of this record hasn't changed, then do nothing */
1066 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1070 rec
->flags
|= FTRACE_FL_ENABLED
;
1071 return ftrace_make_call(rec
, ftrace_addr
);
1074 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1075 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1078 static void ftrace_replace_code(int enable
)
1080 struct dyn_ftrace
*rec
;
1081 struct ftrace_page
*pg
;
1084 do_for_each_ftrace_rec(pg
, rec
) {
1086 * Skip over free records, records that have
1087 * failed and not converted.
1089 if (rec
->flags
& FTRACE_FL_FREE
||
1090 rec
->flags
& FTRACE_FL_FAILED
||
1091 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1094 failed
= __ftrace_replace_code(rec
, enable
);
1096 rec
->flags
|= FTRACE_FL_FAILED
;
1097 ftrace_bug(failed
, rec
->ip
);
1098 /* Stop processing */
1101 } while_for_each_ftrace_rec();
1105 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1112 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1114 ftrace_bug(ret
, ip
);
1115 rec
->flags
|= FTRACE_FL_FAILED
;
1122 * archs can override this function if they must do something
1123 * before the modifying code is performed.
1125 int __weak
ftrace_arch_code_modify_prepare(void)
1131 * archs can override this function if they must do something
1132 * after the modifying code is performed.
1134 int __weak
ftrace_arch_code_modify_post_process(void)
1139 static int __ftrace_modify_code(void *data
)
1141 int *command
= data
;
1143 if (*command
& FTRACE_ENABLE_CALLS
)
1144 ftrace_replace_code(1);
1145 else if (*command
& FTRACE_DISABLE_CALLS
)
1146 ftrace_replace_code(0);
1148 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1149 ftrace_update_ftrace_func(ftrace_trace_function
);
1151 if (*command
& FTRACE_START_FUNC_RET
)
1152 ftrace_enable_ftrace_graph_caller();
1153 else if (*command
& FTRACE_STOP_FUNC_RET
)
1154 ftrace_disable_ftrace_graph_caller();
1159 static void ftrace_run_update_code(int command
)
1163 ret
= ftrace_arch_code_modify_prepare();
1164 FTRACE_WARN_ON(ret
);
1168 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1170 ret
= ftrace_arch_code_modify_post_process();
1171 FTRACE_WARN_ON(ret
);
1174 static ftrace_func_t saved_ftrace_func
;
1175 static int ftrace_start_up
;
1177 static void ftrace_startup_enable(int command
)
1179 if (saved_ftrace_func
!= ftrace_trace_function
) {
1180 saved_ftrace_func
= ftrace_trace_function
;
1181 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1184 if (!command
|| !ftrace_enabled
)
1187 ftrace_run_update_code(command
);
1190 static void ftrace_startup(int command
)
1192 if (unlikely(ftrace_disabled
))
1196 command
|= FTRACE_ENABLE_CALLS
;
1198 ftrace_startup_enable(command
);
1201 static void ftrace_shutdown(int command
)
1203 if (unlikely(ftrace_disabled
))
1208 * Just warn in case of unbalance, no need to kill ftrace, it's not
1209 * critical but the ftrace_call callers may be never nopped again after
1210 * further ftrace uses.
1212 WARN_ON_ONCE(ftrace_start_up
< 0);
1214 if (!ftrace_start_up
)
1215 command
|= FTRACE_DISABLE_CALLS
;
1217 if (saved_ftrace_func
!= ftrace_trace_function
) {
1218 saved_ftrace_func
= ftrace_trace_function
;
1219 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1222 if (!command
|| !ftrace_enabled
)
1225 ftrace_run_update_code(command
);
1228 static void ftrace_startup_sysctl(void)
1230 int command
= FTRACE_ENABLE_MCOUNT
;
1232 if (unlikely(ftrace_disabled
))
1235 /* Force update next time */
1236 saved_ftrace_func
= NULL
;
1237 /* ftrace_start_up is true if we want ftrace running */
1238 if (ftrace_start_up
)
1239 command
|= FTRACE_ENABLE_CALLS
;
1241 ftrace_run_update_code(command
);
1244 static void ftrace_shutdown_sysctl(void)
1246 int command
= FTRACE_DISABLE_MCOUNT
;
1248 if (unlikely(ftrace_disabled
))
1251 /* ftrace_start_up is true if ftrace is running */
1252 if (ftrace_start_up
)
1253 command
|= FTRACE_DISABLE_CALLS
;
1255 ftrace_run_update_code(command
);
1258 static cycle_t ftrace_update_time
;
1259 static unsigned long ftrace_update_cnt
;
1260 unsigned long ftrace_update_tot_cnt
;
1262 static int ftrace_update_code(struct module
*mod
)
1264 struct dyn_ftrace
*p
;
1265 cycle_t start
, stop
;
1267 start
= ftrace_now(raw_smp_processor_id());
1268 ftrace_update_cnt
= 0;
1270 while (ftrace_new_addrs
) {
1272 /* If something went wrong, bail without enabling anything */
1273 if (unlikely(ftrace_disabled
))
1276 p
= ftrace_new_addrs
;
1277 ftrace_new_addrs
= p
->newlist
;
1281 * Do the initial record convertion from mcount jump
1282 * to the NOP instructions.
1284 if (!ftrace_code_disable(mod
, p
)) {
1289 p
->flags
|= FTRACE_FL_CONVERTED
;
1290 ftrace_update_cnt
++;
1293 * If the tracing is enabled, go ahead and enable the record.
1295 * The reason not to enable the record immediatelly is the
1296 * inherent check of ftrace_make_nop/ftrace_make_call for
1297 * correct previous instructions. Making first the NOP
1298 * conversion puts the module to the correct state, thus
1299 * passing the ftrace_make_call check.
1301 if (ftrace_start_up
) {
1302 int failed
= __ftrace_replace_code(p
, 1);
1304 ftrace_bug(failed
, p
->ip
);
1310 stop
= ftrace_now(raw_smp_processor_id());
1311 ftrace_update_time
= stop
- start
;
1312 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1317 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1319 struct ftrace_page
*pg
;
1323 /* allocate a few pages */
1324 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1325 if (!ftrace_pages_start
)
1329 * Allocate a few more pages.
1331 * TODO: have some parser search vmlinux before
1332 * final linking to find all calls to ftrace.
1334 * a) know how many pages to allocate.
1336 * b) set up the table then.
1338 * The dynamic code is still necessary for
1342 pg
= ftrace_pages
= ftrace_pages_start
;
1344 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1345 pr_info("ftrace: allocating %ld entries in %d pages\n",
1346 num_to_init
, cnt
+ 1);
1348 for (i
= 0; i
< cnt
; i
++) {
1349 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1351 /* If we fail, we'll try later anyway */
1362 FTRACE_ITER_FILTER
= (1 << 0),
1363 FTRACE_ITER_NOTRACE
= (1 << 1),
1364 FTRACE_ITER_FAILURES
= (1 << 2),
1365 FTRACE_ITER_PRINTALL
= (1 << 3),
1366 FTRACE_ITER_HASH
= (1 << 4),
1369 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1371 struct ftrace_iterator
{
1372 struct ftrace_page
*pg
;
1376 struct trace_parser parser
;
1380 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1382 struct ftrace_iterator
*iter
= m
->private;
1383 struct hlist_node
*hnd
= v
;
1384 struct hlist_head
*hhd
;
1386 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1391 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1394 hhd
= &ftrace_func_hash
[iter
->hidx
];
1396 if (hlist_empty(hhd
)) {
1415 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1417 struct ftrace_iterator
*iter
= m
->private;
1421 if (!(iter
->flags
& FTRACE_ITER_HASH
))
1424 iter
->flags
|= FTRACE_ITER_HASH
;
1427 for (l
= 0; l
<= *pos
; ) {
1428 p
= t_hash_next(m
, p
, &l
);
1435 static int t_hash_show(struct seq_file
*m
, void *v
)
1437 struct ftrace_func_probe
*rec
;
1438 struct hlist_node
*hnd
= v
;
1440 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1442 if (rec
->ops
->print
)
1443 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1445 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1448 seq_printf(m
, ":%p", rec
->data
);
1455 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1457 struct ftrace_iterator
*iter
= m
->private;
1458 struct dyn_ftrace
*rec
= NULL
;
1460 if (iter
->flags
& FTRACE_ITER_HASH
)
1461 return t_hash_next(m
, v
, pos
);
1465 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1469 if (iter
->idx
>= iter
->pg
->index
) {
1470 if (iter
->pg
->next
) {
1471 iter
->pg
= iter
->pg
->next
;
1476 rec
= &iter
->pg
->records
[iter
->idx
++];
1477 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1479 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1480 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1482 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1483 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1485 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1486 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1488 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1489 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1498 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1500 struct ftrace_iterator
*iter
= m
->private;
1504 mutex_lock(&ftrace_lock
);
1506 * For set_ftrace_filter reading, if we have the filter
1507 * off, we can short cut and just print out that all
1508 * functions are enabled.
1510 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1512 return t_hash_start(m
, pos
);
1513 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1514 /* reset in case of seek/pread */
1515 iter
->flags
&= ~FTRACE_ITER_HASH
;
1519 if (iter
->flags
& FTRACE_ITER_HASH
)
1520 return t_hash_start(m
, pos
);
1522 iter
->pg
= ftrace_pages_start
;
1524 for (l
= 0; l
<= *pos
; ) {
1525 p
= t_next(m
, p
, &l
);
1530 if (!p
&& iter
->flags
& FTRACE_ITER_FILTER
)
1531 return t_hash_start(m
, pos
);
1536 static void t_stop(struct seq_file
*m
, void *p
)
1538 mutex_unlock(&ftrace_lock
);
1541 static int t_show(struct seq_file
*m
, void *v
)
1543 struct ftrace_iterator
*iter
= m
->private;
1544 struct dyn_ftrace
*rec
= v
;
1546 if (iter
->flags
& FTRACE_ITER_HASH
)
1547 return t_hash_show(m
, v
);
1549 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1550 seq_printf(m
, "#### all functions enabled ####\n");
1557 seq_printf(m
, "%ps\n", (void *)rec
->ip
);
1562 static const struct seq_operations show_ftrace_seq_ops
= {
1570 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1572 struct ftrace_iterator
*iter
;
1575 if (unlikely(ftrace_disabled
))
1578 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1582 iter
->pg
= ftrace_pages_start
;
1584 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1586 struct seq_file
*m
= file
->private_data
;
1597 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1601 struct ftrace_iterator
*iter
;
1603 ret
= ftrace_avail_open(inode
, file
);
1605 m
= (struct seq_file
*)file
->private_data
;
1606 iter
= (struct ftrace_iterator
*)m
->private;
1607 iter
->flags
= FTRACE_ITER_FAILURES
;
1614 static void ftrace_filter_reset(int enable
)
1616 struct ftrace_page
*pg
;
1617 struct dyn_ftrace
*rec
;
1618 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1620 mutex_lock(&ftrace_lock
);
1622 ftrace_filtered
= 0;
1623 do_for_each_ftrace_rec(pg
, rec
) {
1624 if (rec
->flags
& FTRACE_FL_FAILED
)
1626 rec
->flags
&= ~type
;
1627 } while_for_each_ftrace_rec();
1628 mutex_unlock(&ftrace_lock
);
1632 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1634 struct ftrace_iterator
*iter
;
1637 if (unlikely(ftrace_disabled
))
1640 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1644 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
1649 mutex_lock(&ftrace_regex_lock
);
1650 if ((file
->f_mode
& FMODE_WRITE
) &&
1651 (file
->f_flags
& O_TRUNC
))
1652 ftrace_filter_reset(enable
);
1654 if (file
->f_mode
& FMODE_READ
) {
1655 iter
->pg
= ftrace_pages_start
;
1656 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1657 FTRACE_ITER_NOTRACE
;
1659 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1661 struct seq_file
*m
= file
->private_data
;
1664 trace_parser_put(&iter
->parser
);
1668 file
->private_data
= iter
;
1669 mutex_unlock(&ftrace_regex_lock
);
1675 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1677 return ftrace_regex_open(inode
, file
, 1);
1681 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1683 return ftrace_regex_open(inode
, file
, 0);
1687 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1691 if (file
->f_mode
& FMODE_READ
)
1692 ret
= seq_lseek(file
, offset
, origin
);
1694 file
->f_pos
= ret
= 1;
1699 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1706 if (strcmp(str
, regex
) == 0)
1709 case MATCH_FRONT_ONLY
:
1710 if (strncmp(str
, regex
, len
) == 0)
1713 case MATCH_MIDDLE_ONLY
:
1714 if (strstr(str
, regex
))
1717 case MATCH_END_ONLY
:
1719 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
1728 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1730 char str
[KSYM_SYMBOL_LEN
];
1732 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1733 return ftrace_match(str
, regex
, len
, type
);
1736 static int ftrace_match_records(char *buff
, int len
, int enable
)
1738 unsigned int search_len
;
1739 struct ftrace_page
*pg
;
1740 struct dyn_ftrace
*rec
;
1747 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1748 type
= filter_parse_regex(buff
, len
, &search
, ¬);
1750 search_len
= strlen(search
);
1752 mutex_lock(&ftrace_lock
);
1753 do_for_each_ftrace_rec(pg
, rec
) {
1755 if (rec
->flags
& FTRACE_FL_FAILED
)
1758 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1760 rec
->flags
&= ~flag
;
1766 * Only enable filtering if we have a function that
1769 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1770 ftrace_filtered
= 1;
1771 } while_for_each_ftrace_rec();
1772 mutex_unlock(&ftrace_lock
);
1778 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1779 char *regex
, int len
, int type
)
1781 char str
[KSYM_SYMBOL_LEN
];
1784 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1786 if (!modname
|| strcmp(modname
, mod
))
1789 /* blank search means to match all funcs in the mod */
1791 return ftrace_match(str
, regex
, len
, type
);
1796 static int ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1798 unsigned search_len
= 0;
1799 struct ftrace_page
*pg
;
1800 struct dyn_ftrace
*rec
;
1801 int type
= MATCH_FULL
;
1802 char *search
= buff
;
1807 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1809 /* blank or '*' mean the same */
1810 if (strcmp(buff
, "*") == 0)
1813 /* handle the case of 'dont filter this module' */
1814 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1820 type
= filter_parse_regex(buff
, strlen(buff
), &search
, ¬);
1821 search_len
= strlen(search
);
1824 mutex_lock(&ftrace_lock
);
1825 do_for_each_ftrace_rec(pg
, rec
) {
1827 if (rec
->flags
& FTRACE_FL_FAILED
)
1830 if (ftrace_match_module_record(rec
, mod
,
1831 search
, search_len
, type
)) {
1833 rec
->flags
&= ~flag
;
1838 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1839 ftrace_filtered
= 1;
1841 } while_for_each_ftrace_rec();
1842 mutex_unlock(&ftrace_lock
);
1848 * We register the module command as a template to show others how
1849 * to register the a command as well.
1853 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1858 * cmd == 'mod' because we only registered this func
1859 * for the 'mod' ftrace_func_command.
1860 * But if you register one func with multiple commands,
1861 * you can tell which command was used by the cmd
1865 /* we must have a module name */
1869 mod
= strsep(¶m
, ":");
1873 if (ftrace_match_module_records(func
, mod
, enable
))
1878 static struct ftrace_func_command ftrace_mod_cmd
= {
1880 .func
= ftrace_mod_callback
,
1883 static int __init
ftrace_mod_cmd_init(void)
1885 return register_ftrace_command(&ftrace_mod_cmd
);
1887 device_initcall(ftrace_mod_cmd_init
);
1890 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1892 struct ftrace_func_probe
*entry
;
1893 struct hlist_head
*hhd
;
1894 struct hlist_node
*n
;
1897 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1899 hhd
= &ftrace_func_hash
[key
];
1901 if (hlist_empty(hhd
))
1905 * Disable preemption for these calls to prevent a RCU grace
1906 * period. This syncs the hash iteration and freeing of items
1907 * on the hash. rcu_read_lock is too dangerous here.
1909 preempt_disable_notrace();
1910 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1911 if (entry
->ip
== ip
)
1912 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1914 preempt_enable_notrace();
1917 static struct ftrace_ops trace_probe_ops __read_mostly
=
1919 .func
= function_trace_probe_call
,
1922 static int ftrace_probe_registered
;
1924 static void __enable_ftrace_function_probe(void)
1928 if (ftrace_probe_registered
)
1931 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1932 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1936 /* Nothing registered? */
1937 if (i
== FTRACE_FUNC_HASHSIZE
)
1940 __register_ftrace_function(&trace_probe_ops
);
1942 ftrace_probe_registered
= 1;
1945 static void __disable_ftrace_function_probe(void)
1949 if (!ftrace_probe_registered
)
1952 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1953 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1958 /* no more funcs left */
1959 __unregister_ftrace_function(&trace_probe_ops
);
1961 ftrace_probe_registered
= 0;
1965 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
1967 struct ftrace_func_probe
*entry
=
1968 container_of(rhp
, struct ftrace_func_probe
, rcu
);
1970 if (entry
->ops
->free
)
1971 entry
->ops
->free(&entry
->data
);
1977 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
1980 struct ftrace_func_probe
*entry
;
1981 struct ftrace_page
*pg
;
1982 struct dyn_ftrace
*rec
;
1988 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
1989 len
= strlen(search
);
1991 /* we do not support '!' for function probes */
1995 mutex_lock(&ftrace_lock
);
1996 do_for_each_ftrace_rec(pg
, rec
) {
1998 if (rec
->flags
& FTRACE_FL_FAILED
)
2001 if (!ftrace_match_record(rec
, search
, len
, type
))
2004 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
2006 /* If we did not process any, then return error */
2017 * The caller might want to do something special
2018 * for each function we find. We call the callback
2019 * to give the caller an opportunity to do so.
2021 if (ops
->callback
) {
2022 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2023 /* caller does not like this func */
2030 entry
->ip
= rec
->ip
;
2032 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2033 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2035 } while_for_each_ftrace_rec();
2036 __enable_ftrace_function_probe();
2039 mutex_unlock(&ftrace_lock
);
2045 PROBE_TEST_FUNC
= 1,
2050 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2051 void *data
, int flags
)
2053 struct ftrace_func_probe
*entry
;
2054 struct hlist_node
*n
, *tmp
;
2055 char str
[KSYM_SYMBOL_LEN
];
2056 int type
= MATCH_FULL
;
2060 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2065 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2066 len
= strlen(search
);
2068 /* we do not support '!' for function probes */
2073 mutex_lock(&ftrace_lock
);
2074 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2075 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2077 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2079 /* break up if statements for readability */
2080 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2083 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2086 /* do this last, since it is the most expensive */
2088 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2090 if (!ftrace_match(str
, glob
, len
, type
))
2094 hlist_del(&entry
->node
);
2095 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2098 __disable_ftrace_function_probe();
2099 mutex_unlock(&ftrace_lock
);
2103 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2106 __unregister_ftrace_function_probe(glob
, ops
, data
,
2107 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2111 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2113 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2116 void unregister_ftrace_function_probe_all(char *glob
)
2118 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2121 static LIST_HEAD(ftrace_commands
);
2122 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2124 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2126 struct ftrace_func_command
*p
;
2129 mutex_lock(&ftrace_cmd_mutex
);
2130 list_for_each_entry(p
, &ftrace_commands
, list
) {
2131 if (strcmp(cmd
->name
, p
->name
) == 0) {
2136 list_add(&cmd
->list
, &ftrace_commands
);
2138 mutex_unlock(&ftrace_cmd_mutex
);
2143 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2145 struct ftrace_func_command
*p
, *n
;
2148 mutex_lock(&ftrace_cmd_mutex
);
2149 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2150 if (strcmp(cmd
->name
, p
->name
) == 0) {
2152 list_del_init(&p
->list
);
2157 mutex_unlock(&ftrace_cmd_mutex
);
2162 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2164 char *func
, *command
, *next
= buff
;
2165 struct ftrace_func_command
*p
;
2168 func
= strsep(&next
, ":");
2171 if (ftrace_match_records(func
, len
, enable
))
2178 command
= strsep(&next
, ":");
2180 mutex_lock(&ftrace_cmd_mutex
);
2181 list_for_each_entry(p
, &ftrace_commands
, list
) {
2182 if (strcmp(p
->name
, command
) == 0) {
2183 ret
= p
->func(func
, command
, next
, enable
);
2188 mutex_unlock(&ftrace_cmd_mutex
);
2194 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2195 size_t cnt
, loff_t
*ppos
, int enable
)
2197 struct ftrace_iterator
*iter
;
2198 struct trace_parser
*parser
;
2204 mutex_lock(&ftrace_regex_lock
);
2206 if (file
->f_mode
& FMODE_READ
) {
2207 struct seq_file
*m
= file
->private_data
;
2210 iter
= file
->private_data
;
2212 parser
= &iter
->parser
;
2213 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2215 if (read
>= 0 && trace_parser_loaded(parser
) &&
2216 !trace_parser_cont(parser
)) {
2217 ret
= ftrace_process_regex(parser
->buffer
,
2218 parser
->idx
, enable
);
2219 trace_parser_clear(parser
);
2226 mutex_unlock(&ftrace_regex_lock
);
2232 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2233 size_t cnt
, loff_t
*ppos
)
2235 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2239 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2240 size_t cnt
, loff_t
*ppos
)
2242 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2246 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2248 if (unlikely(ftrace_disabled
))
2251 mutex_lock(&ftrace_regex_lock
);
2253 ftrace_filter_reset(enable
);
2255 ftrace_match_records(buf
, len
, enable
);
2256 mutex_unlock(&ftrace_regex_lock
);
2260 * ftrace_set_filter - set a function to filter on in ftrace
2261 * @buf - the string that holds the function filter text.
2262 * @len - the length of the string.
2263 * @reset - non zero to reset all filters before applying this filter.
2265 * Filters denote which functions should be enabled when tracing is enabled.
2266 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2268 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2270 ftrace_set_regex(buf
, len
, reset
, 1);
2274 * ftrace_set_notrace - set a function to not trace in ftrace
2275 * @buf - the string that holds the function notrace text.
2276 * @len - the length of the string.
2277 * @reset - non zero to reset all filters before applying this filter.
2279 * Notrace Filters denote which functions should not be enabled when tracing
2280 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2283 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2285 ftrace_set_regex(buf
, len
, reset
, 0);
2289 * command line interface to allow users to set filters on boot up.
2291 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2292 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2293 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2295 static int __init
set_ftrace_notrace(char *str
)
2297 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2300 __setup("ftrace_notrace=", set_ftrace_notrace
);
2302 static int __init
set_ftrace_filter(char *str
)
2304 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2307 __setup("ftrace_filter=", set_ftrace_filter
);
2309 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2310 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2311 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
2313 static int __init
set_graph_function(char *str
)
2315 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2318 __setup("ftrace_graph_filter=", set_graph_function
);
2320 static void __init
set_ftrace_early_graph(char *buf
)
2326 func
= strsep(&buf
, ",");
2327 /* we allow only one expression at a time */
2328 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2331 printk(KERN_DEBUG
"ftrace: function %s not "
2332 "traceable\n", func
);
2335 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2337 static void __init
set_ftrace_early_filter(char *buf
, int enable
)
2342 func
= strsep(&buf
, ",");
2343 ftrace_set_regex(func
, strlen(func
), 0, enable
);
2347 static void __init
set_ftrace_early_filters(void)
2349 if (ftrace_filter_buf
[0])
2350 set_ftrace_early_filter(ftrace_filter_buf
, 1);
2351 if (ftrace_notrace_buf
[0])
2352 set_ftrace_early_filter(ftrace_notrace_buf
, 0);
2353 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2354 if (ftrace_graph_buf
[0])
2355 set_ftrace_early_graph(ftrace_graph_buf
);
2356 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2360 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2362 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2363 struct ftrace_iterator
*iter
;
2364 struct trace_parser
*parser
;
2366 mutex_lock(&ftrace_regex_lock
);
2367 if (file
->f_mode
& FMODE_READ
) {
2370 seq_release(inode
, file
);
2372 iter
= file
->private_data
;
2374 parser
= &iter
->parser
;
2375 if (trace_parser_loaded(parser
)) {
2376 parser
->buffer
[parser
->idx
] = 0;
2377 ftrace_match_records(parser
->buffer
, parser
->idx
, enable
);
2380 mutex_lock(&ftrace_lock
);
2381 if (ftrace_start_up
&& ftrace_enabled
)
2382 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2383 mutex_unlock(&ftrace_lock
);
2385 trace_parser_put(parser
);
2388 mutex_unlock(&ftrace_regex_lock
);
2393 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2395 return ftrace_regex_release(inode
, file
, 1);
2399 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2401 return ftrace_regex_release(inode
, file
, 0);
2404 static const struct file_operations ftrace_avail_fops
= {
2405 .open
= ftrace_avail_open
,
2407 .llseek
= seq_lseek
,
2408 .release
= seq_release_private
,
2411 static const struct file_operations ftrace_failures_fops
= {
2412 .open
= ftrace_failures_open
,
2414 .llseek
= seq_lseek
,
2415 .release
= seq_release_private
,
2418 static const struct file_operations ftrace_filter_fops
= {
2419 .open
= ftrace_filter_open
,
2421 .write
= ftrace_filter_write
,
2422 .llseek
= no_llseek
,
2423 .release
= ftrace_filter_release
,
2426 static const struct file_operations ftrace_notrace_fops
= {
2427 .open
= ftrace_notrace_open
,
2429 .write
= ftrace_notrace_write
,
2430 .llseek
= ftrace_regex_lseek
,
2431 .release
= ftrace_notrace_release
,
2434 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2436 static DEFINE_MUTEX(graph_lock
);
2438 int ftrace_graph_count
;
2439 int ftrace_graph_filter_enabled
;
2440 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2443 __g_next(struct seq_file
*m
, loff_t
*pos
)
2445 if (*pos
>= ftrace_graph_count
)
2447 return &ftrace_graph_funcs
[*pos
];
2451 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2454 return __g_next(m
, pos
);
2457 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2459 mutex_lock(&graph_lock
);
2461 /* Nothing, tell g_show to print all functions are enabled */
2462 if (!ftrace_graph_filter_enabled
&& !*pos
)
2465 return __g_next(m
, pos
);
2468 static void g_stop(struct seq_file
*m
, void *p
)
2470 mutex_unlock(&graph_lock
);
2473 static int g_show(struct seq_file
*m
, void *v
)
2475 unsigned long *ptr
= v
;
2480 if (ptr
== (unsigned long *)1) {
2481 seq_printf(m
, "#### all functions enabled ####\n");
2485 seq_printf(m
, "%ps\n", (void *)*ptr
);
2490 static const struct seq_operations ftrace_graph_seq_ops
= {
2498 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2502 if (unlikely(ftrace_disabled
))
2505 mutex_lock(&graph_lock
);
2506 if ((file
->f_mode
& FMODE_WRITE
) &&
2507 (file
->f_flags
& O_TRUNC
)) {
2508 ftrace_graph_filter_enabled
= 0;
2509 ftrace_graph_count
= 0;
2510 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2512 mutex_unlock(&graph_lock
);
2514 if (file
->f_mode
& FMODE_READ
)
2515 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2521 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
2523 if (file
->f_mode
& FMODE_READ
)
2524 seq_release(inode
, file
);
2529 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2531 struct dyn_ftrace
*rec
;
2532 struct ftrace_page
*pg
;
2540 if (ftrace_disabled
)
2544 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
2545 if (!not && *idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2548 search_len
= strlen(search
);
2550 mutex_lock(&ftrace_lock
);
2551 do_for_each_ftrace_rec(pg
, rec
) {
2553 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2556 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2557 /* if it is in the array */
2559 for (i
= 0; i
< *idx
; i
++) {
2560 if (array
[i
] == rec
->ip
) {
2569 array
[(*idx
)++] = rec
->ip
;
2570 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2575 array
[i
] = array
[--(*idx
)];
2581 } while_for_each_ftrace_rec();
2583 mutex_unlock(&ftrace_lock
);
2588 ftrace_graph_filter_enabled
= 1;
2593 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2594 size_t cnt
, loff_t
*ppos
)
2596 struct trace_parser parser
;
2602 mutex_lock(&graph_lock
);
2604 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
2609 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
2611 if (read
>= 0 && trace_parser_loaded((&parser
))) {
2612 parser
.buffer
[parser
.idx
] = 0;
2614 /* we allow only one expression at a time */
2615 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2624 trace_parser_put(&parser
);
2626 mutex_unlock(&graph_lock
);
2631 static const struct file_operations ftrace_graph_fops
= {
2632 .open
= ftrace_graph_open
,
2634 .write
= ftrace_graph_write
,
2635 .release
= ftrace_graph_release
,
2636 .llseek
= seq_lseek
,
2638 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2640 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2643 trace_create_file("available_filter_functions", 0444,
2644 d_tracer
, NULL
, &ftrace_avail_fops
);
2646 trace_create_file("failures", 0444,
2647 d_tracer
, NULL
, &ftrace_failures_fops
);
2649 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
2650 NULL
, &ftrace_filter_fops
);
2652 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
2653 NULL
, &ftrace_notrace_fops
);
2655 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2656 trace_create_file("set_graph_function", 0444, d_tracer
,
2658 &ftrace_graph_fops
);
2659 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2664 static int ftrace_process_locs(struct module
*mod
,
2665 unsigned long *start
,
2670 unsigned long flags
;
2672 mutex_lock(&ftrace_lock
);
2675 addr
= ftrace_call_adjust(*p
++);
2677 * Some architecture linkers will pad between
2678 * the different mcount_loc sections of different
2679 * object files to satisfy alignments.
2680 * Skip any NULL pointers.
2684 ftrace_record_ip(addr
);
2687 /* disable interrupts to prevent kstop machine */
2688 local_irq_save(flags
);
2689 ftrace_update_code(mod
);
2690 local_irq_restore(flags
);
2691 mutex_unlock(&ftrace_lock
);
2696 #ifdef CONFIG_MODULES
2697 void ftrace_release_mod(struct module
*mod
)
2699 struct dyn_ftrace
*rec
;
2700 struct ftrace_page
*pg
;
2702 if (ftrace_disabled
)
2705 mutex_lock(&ftrace_lock
);
2706 do_for_each_ftrace_rec(pg
, rec
) {
2707 if (within_module_core(rec
->ip
, mod
)) {
2709 * rec->ip is changed in ftrace_free_rec()
2710 * It should not between s and e if record was freed.
2712 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
2713 ftrace_free_rec(rec
);
2715 } while_for_each_ftrace_rec();
2716 mutex_unlock(&ftrace_lock
);
2719 static void ftrace_init_module(struct module
*mod
,
2720 unsigned long *start
, unsigned long *end
)
2722 if (ftrace_disabled
|| start
== end
)
2724 ftrace_process_locs(mod
, start
, end
);
2727 static int ftrace_module_notify(struct notifier_block
*self
,
2728 unsigned long val
, void *data
)
2730 struct module
*mod
= data
;
2733 case MODULE_STATE_COMING
:
2734 ftrace_init_module(mod
, mod
->ftrace_callsites
,
2735 mod
->ftrace_callsites
+
2736 mod
->num_ftrace_callsites
);
2738 case MODULE_STATE_GOING
:
2739 ftrace_release_mod(mod
);
2746 static int ftrace_module_notify(struct notifier_block
*self
,
2747 unsigned long val
, void *data
)
2751 #endif /* CONFIG_MODULES */
2753 struct notifier_block ftrace_module_nb
= {
2754 .notifier_call
= ftrace_module_notify
,
2758 extern unsigned long __start_mcount_loc
[];
2759 extern unsigned long __stop_mcount_loc
[];
2761 void __init
ftrace_init(void)
2763 unsigned long count
, addr
, flags
;
2766 /* Keep the ftrace pointer to the stub */
2767 addr
= (unsigned long)ftrace_stub
;
2769 local_irq_save(flags
);
2770 ftrace_dyn_arch_init(&addr
);
2771 local_irq_restore(flags
);
2773 /* ftrace_dyn_arch_init places the return code in addr */
2777 count
= __stop_mcount_loc
- __start_mcount_loc
;
2779 ret
= ftrace_dyn_table_alloc(count
);
2783 last_ftrace_enabled
= ftrace_enabled
= 1;
2785 ret
= ftrace_process_locs(NULL
,
2789 ret
= register_module_notifier(&ftrace_module_nb
);
2791 pr_warning("Failed to register trace ftrace module notifier\n");
2793 set_ftrace_early_filters();
2797 ftrace_disabled
= 1;
2802 static int __init
ftrace_nodyn_init(void)
2807 device_initcall(ftrace_nodyn_init
);
2809 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2810 static inline void ftrace_startup_enable(int command
) { }
2811 /* Keep as macros so we do not need to define the commands */
2812 # define ftrace_startup(command) do { } while (0)
2813 # define ftrace_shutdown(command) do { } while (0)
2814 # define ftrace_startup_sysctl() do { } while (0)
2815 # define ftrace_shutdown_sysctl() do { } while (0)
2816 #endif /* CONFIG_DYNAMIC_FTRACE */
2818 static void clear_ftrace_swapper(void)
2820 struct task_struct
*p
;
2824 for_each_online_cpu(cpu
) {
2826 clear_tsk_trace_trace(p
);
2831 static void set_ftrace_swapper(void)
2833 struct task_struct
*p
;
2837 for_each_online_cpu(cpu
) {
2839 set_tsk_trace_trace(p
);
2844 static void clear_ftrace_pid(struct pid
*pid
)
2846 struct task_struct
*p
;
2849 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2850 clear_tsk_trace_trace(p
);
2851 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2857 static void set_ftrace_pid(struct pid
*pid
)
2859 struct task_struct
*p
;
2862 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2863 set_tsk_trace_trace(p
);
2864 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2868 static void clear_ftrace_pid_task(struct pid
*pid
)
2870 if (pid
== ftrace_swapper_pid
)
2871 clear_ftrace_swapper();
2873 clear_ftrace_pid(pid
);
2876 static void set_ftrace_pid_task(struct pid
*pid
)
2878 if (pid
== ftrace_swapper_pid
)
2879 set_ftrace_swapper();
2881 set_ftrace_pid(pid
);
2884 static int ftrace_pid_add(int p
)
2887 struct ftrace_pid
*fpid
;
2890 mutex_lock(&ftrace_lock
);
2893 pid
= ftrace_swapper_pid
;
2895 pid
= find_get_pid(p
);
2902 list_for_each_entry(fpid
, &ftrace_pids
, list
)
2903 if (fpid
->pid
== pid
)
2908 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
2912 list_add(&fpid
->list
, &ftrace_pids
);
2915 set_ftrace_pid_task(pid
);
2917 ftrace_update_pid_func();
2918 ftrace_startup_enable(0);
2920 mutex_unlock(&ftrace_lock
);
2924 if (pid
!= ftrace_swapper_pid
)
2928 mutex_unlock(&ftrace_lock
);
2932 static void ftrace_pid_reset(void)
2934 struct ftrace_pid
*fpid
, *safe
;
2936 mutex_lock(&ftrace_lock
);
2937 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
2938 struct pid
*pid
= fpid
->pid
;
2940 clear_ftrace_pid_task(pid
);
2942 list_del(&fpid
->list
);
2946 ftrace_update_pid_func();
2947 ftrace_startup_enable(0);
2949 mutex_unlock(&ftrace_lock
);
2952 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
2954 mutex_lock(&ftrace_lock
);
2956 if (list_empty(&ftrace_pids
) && (!*pos
))
2959 return seq_list_start(&ftrace_pids
, *pos
);
2962 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2967 return seq_list_next(v
, &ftrace_pids
, pos
);
2970 static void fpid_stop(struct seq_file
*m
, void *p
)
2972 mutex_unlock(&ftrace_lock
);
2975 static int fpid_show(struct seq_file
*m
, void *v
)
2977 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
2979 if (v
== (void *)1) {
2980 seq_printf(m
, "no pid\n");
2984 if (fpid
->pid
== ftrace_swapper_pid
)
2985 seq_printf(m
, "swapper tasks\n");
2987 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
2992 static const struct seq_operations ftrace_pid_sops
= {
2993 .start
= fpid_start
,
3000 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
3004 if ((file
->f_mode
& FMODE_WRITE
) &&
3005 (file
->f_flags
& O_TRUNC
))
3008 if (file
->f_mode
& FMODE_READ
)
3009 ret
= seq_open(file
, &ftrace_pid_sops
);
3015 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
3016 size_t cnt
, loff_t
*ppos
)
3022 if (cnt
>= sizeof(buf
))
3025 if (copy_from_user(&buf
, ubuf
, cnt
))
3031 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3032 * to clean the filter quietly.
3034 tmp
= strstrip(buf
);
3035 if (strlen(tmp
) == 0)
3038 ret
= strict_strtol(tmp
, 10, &val
);
3042 ret
= ftrace_pid_add(val
);
3044 return ret
? ret
: cnt
;
3048 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3050 if (file
->f_mode
& FMODE_READ
)
3051 seq_release(inode
, file
);
3056 static const struct file_operations ftrace_pid_fops
= {
3057 .open
= ftrace_pid_open
,
3058 .write
= ftrace_pid_write
,
3060 .llseek
= seq_lseek
,
3061 .release
= ftrace_pid_release
,
3064 static __init
int ftrace_init_debugfs(void)
3066 struct dentry
*d_tracer
;
3068 d_tracer
= tracing_init_dentry();
3072 ftrace_init_dyn_debugfs(d_tracer
);
3074 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3075 NULL
, &ftrace_pid_fops
);
3077 ftrace_profile_debugfs(d_tracer
);
3081 fs_initcall(ftrace_init_debugfs
);
3084 * ftrace_kill - kill ftrace
3086 * This function should be used by panic code. It stops ftrace
3087 * but in a not so nice way. If you need to simply kill ftrace
3088 * from a non-atomic section, use ftrace_kill.
3090 void ftrace_kill(void)
3092 ftrace_disabled
= 1;
3094 clear_ftrace_function();
3098 * register_ftrace_function - register a function for profiling
3099 * @ops - ops structure that holds the function for profiling.
3101 * Register a function to be called by all functions in the
3104 * Note: @ops->func and all the functions it calls must be labeled
3105 * with "notrace", otherwise it will go into a
3108 int register_ftrace_function(struct ftrace_ops
*ops
)
3112 if (unlikely(ftrace_disabled
))
3115 mutex_lock(&ftrace_lock
);
3117 ret
= __register_ftrace_function(ops
);
3120 mutex_unlock(&ftrace_lock
);
3125 * unregister_ftrace_function - unregister a function for profiling.
3126 * @ops - ops structure that holds the function to unregister
3128 * Unregister a function that was added to be called by ftrace profiling.
3130 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3134 mutex_lock(&ftrace_lock
);
3135 ret
= __unregister_ftrace_function(ops
);
3137 mutex_unlock(&ftrace_lock
);
3143 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3144 void __user
*buffer
, size_t *lenp
,
3149 if (unlikely(ftrace_disabled
))
3152 mutex_lock(&ftrace_lock
);
3154 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3156 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3159 last_ftrace_enabled
= !!ftrace_enabled
;
3161 if (ftrace_enabled
) {
3163 ftrace_startup_sysctl();
3165 /* we are starting ftrace again */
3166 if (ftrace_list
!= &ftrace_list_end
) {
3167 if (ftrace_list
->next
== &ftrace_list_end
)
3168 ftrace_trace_function
= ftrace_list
->func
;
3170 ftrace_trace_function
= ftrace_list_func
;
3174 /* stopping ftrace calls (just send to ftrace_stub) */
3175 ftrace_trace_function
= ftrace_stub
;
3177 ftrace_shutdown_sysctl();
3181 mutex_unlock(&ftrace_lock
);
3185 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3187 static int ftrace_graph_active
;
3188 static struct notifier_block ftrace_suspend_notifier
;
3190 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3195 /* The callbacks that hook a function */
3196 trace_func_graph_ret_t ftrace_graph_return
=
3197 (trace_func_graph_ret_t
)ftrace_stub
;
3198 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3200 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3201 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3205 unsigned long flags
;
3206 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3207 struct task_struct
*g
, *t
;
3209 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3210 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3211 * sizeof(struct ftrace_ret_stack
),
3213 if (!ret_stack_list
[i
]) {
3221 read_lock_irqsave(&tasklist_lock
, flags
);
3222 do_each_thread(g
, t
) {
3228 if (t
->ret_stack
== NULL
) {
3229 atomic_set(&t
->tracing_graph_pause
, 0);
3230 atomic_set(&t
->trace_overrun
, 0);
3231 t
->curr_ret_stack
= -1;
3232 /* Make sure the tasks see the -1 first: */
3234 t
->ret_stack
= ret_stack_list
[start
++];
3236 } while_each_thread(g
, t
);
3239 read_unlock_irqrestore(&tasklist_lock
, flags
);
3241 for (i
= start
; i
< end
; i
++)
3242 kfree(ret_stack_list
[i
]);
3247 ftrace_graph_probe_sched_switch(void *ignore
,
3248 struct task_struct
*prev
, struct task_struct
*next
)
3250 unsigned long long timestamp
;
3254 * Does the user want to count the time a function was asleep.
3255 * If so, do not update the time stamps.
3257 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3260 timestamp
= trace_clock_local();
3262 prev
->ftrace_timestamp
= timestamp
;
3264 /* only process tasks that we timestamped */
3265 if (!next
->ftrace_timestamp
)
3269 * Update all the counters in next to make up for the
3270 * time next was sleeping.
3272 timestamp
-= next
->ftrace_timestamp
;
3274 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3275 next
->ret_stack
[index
].calltime
+= timestamp
;
3278 /* Allocate a return stack for each task */
3279 static int start_graph_tracing(void)
3281 struct ftrace_ret_stack
**ret_stack_list
;
3284 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3285 sizeof(struct ftrace_ret_stack
*),
3288 if (!ret_stack_list
)
3291 /* The cpu_boot init_task->ret_stack will never be freed */
3292 for_each_online_cpu(cpu
) {
3293 if (!idle_task(cpu
)->ret_stack
)
3294 ftrace_graph_init_task(idle_task(cpu
));
3298 ret
= alloc_retstack_tasklist(ret_stack_list
);
3299 } while (ret
== -EAGAIN
);
3302 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
3304 pr_info("ftrace_graph: Couldn't activate tracepoint"
3305 " probe to kernel_sched_switch\n");
3308 kfree(ret_stack_list
);
3313 * Hibernation protection.
3314 * The state of the current task is too much unstable during
3315 * suspend/restore to disk. We want to protect against that.
3318 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3322 case PM_HIBERNATION_PREPARE
:
3323 pause_graph_tracing();
3326 case PM_POST_HIBERNATION
:
3327 unpause_graph_tracing();
3333 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3334 trace_func_graph_ent_t entryfunc
)
3338 mutex_lock(&ftrace_lock
);
3340 /* we currently allow only one tracer registered at a time */
3341 if (ftrace_graph_active
) {
3346 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3347 register_pm_notifier(&ftrace_suspend_notifier
);
3349 ftrace_graph_active
++;
3350 ret
= start_graph_tracing();
3352 ftrace_graph_active
--;
3356 ftrace_graph_return
= retfunc
;
3357 ftrace_graph_entry
= entryfunc
;
3359 ftrace_startup(FTRACE_START_FUNC_RET
);
3362 mutex_unlock(&ftrace_lock
);
3366 void unregister_ftrace_graph(void)
3368 mutex_lock(&ftrace_lock
);
3370 if (unlikely(!ftrace_graph_active
))
3373 ftrace_graph_active
--;
3374 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3375 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3376 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3377 unregister_pm_notifier(&ftrace_suspend_notifier
);
3378 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
, NULL
);
3381 mutex_unlock(&ftrace_lock
);
3384 /* Allocate a return stack for newly created task */
3385 void ftrace_graph_init_task(struct task_struct
*t
)
3387 /* Make sure we do not use the parent ret_stack */
3388 t
->ret_stack
= NULL
;
3389 t
->curr_ret_stack
= -1;
3391 if (ftrace_graph_active
) {
3392 struct ftrace_ret_stack
*ret_stack
;
3394 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3395 * sizeof(struct ftrace_ret_stack
),
3399 atomic_set(&t
->tracing_graph_pause
, 0);
3400 atomic_set(&t
->trace_overrun
, 0);
3401 t
->ftrace_timestamp
= 0;
3402 /* make curr_ret_stack visable before we add the ret_stack */
3404 t
->ret_stack
= ret_stack
;
3408 void ftrace_graph_exit_task(struct task_struct
*t
)
3410 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3412 t
->ret_stack
= NULL
;
3413 /* NULL must become visible to IRQs before we free it: */
3419 void ftrace_graph_stop(void)