2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
46 #define FTRACE_WARN_ON_ONCE(cond) \
48 if (WARN_ON_ONCE(cond)) \
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly
;
58 static int last_ftrace_enabled
;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop
;
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
;
88 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
89 static int ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
);
92 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
94 struct ftrace_ops
*op
= ftrace_list
;
96 /* in case someone actually ports this to alpha! */
97 read_barrier_depends();
99 while (op
!= &ftrace_list_end
) {
101 read_barrier_depends();
102 op
->func(ip
, parent_ip
);
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.
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
;
270 struct ftrace_profile_page
{
271 struct ftrace_profile_page
*next
;
273 struct ftrace_profile records
[];
276 struct ftrace_profile_stat
{
278 struct hlist_head
*hash
;
279 struct ftrace_profile_page
*pages
;
280 struct ftrace_profile_page
*start
;
281 struct tracer_stat stat
;
284 #define PROFILE_RECORDS_SIZE \
285 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
287 #define PROFILES_PER_PAGE \
288 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
290 static int ftrace_profile_bits __read_mostly
;
291 static int ftrace_profile_enabled __read_mostly
;
293 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
294 static DEFINE_MUTEX(ftrace_profile_lock
);
296 static DEFINE_PER_CPU(struct ftrace_profile_stat
, ftrace_profile_stats
);
298 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
301 function_stat_next(void *v
, int idx
)
303 struct ftrace_profile
*rec
= v
;
304 struct ftrace_profile_page
*pg
;
306 pg
= (struct ftrace_profile_page
*)((unsigned long)rec
& PAGE_MASK
);
312 if ((void *)rec
>= (void *)&pg
->records
[pg
->index
]) {
316 rec
= &pg
->records
[0];
324 static void *function_stat_start(struct tracer_stat
*trace
)
326 struct ftrace_profile_stat
*stat
=
327 container_of(trace
, struct ftrace_profile_stat
, stat
);
329 if (!stat
|| !stat
->start
)
332 return function_stat_next(&stat
->start
->records
[0], 0);
335 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
336 /* function graph compares on total time */
337 static int function_stat_cmp(void *p1
, void *p2
)
339 struct ftrace_profile
*a
= p1
;
340 struct ftrace_profile
*b
= p2
;
342 if (a
->time
< b
->time
)
344 if (a
->time
> b
->time
)
350 /* not function graph compares against hits */
351 static int function_stat_cmp(void *p1
, void *p2
)
353 struct ftrace_profile
*a
= p1
;
354 struct ftrace_profile
*b
= p2
;
356 if (a
->counter
< b
->counter
)
358 if (a
->counter
> b
->counter
)
365 static int function_stat_headers(struct seq_file
*m
)
367 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
368 seq_printf(m
, " Function "
373 seq_printf(m
, " Function Hit\n"
379 static int function_stat_show(struct seq_file
*m
, void *v
)
381 struct ftrace_profile
*rec
= v
;
382 char str
[KSYM_SYMBOL_LEN
];
383 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
384 static DEFINE_MUTEX(mutex
);
385 static struct trace_seq s
;
386 unsigned long long avg
;
389 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
390 seq_printf(m
, " %-30.30s %10lu", str
, rec
->counter
);
392 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
395 do_div(avg
, rec
->counter
);
399 trace_print_graph_duration(rec
->time
, &s
);
400 trace_seq_puts(&s
, " ");
401 trace_print_graph_duration(avg
, &s
);
402 trace_print_seq(m
, &s
);
403 mutex_unlock(&mutex
);
410 static void ftrace_profile_reset(struct ftrace_profile_stat
*stat
)
412 struct ftrace_profile_page
*pg
;
414 pg
= stat
->pages
= stat
->start
;
417 memset(pg
->records
, 0, PROFILE_RECORDS_SIZE
);
422 memset(stat
->hash
, 0,
423 FTRACE_PROFILE_HASH_SIZE
* sizeof(struct hlist_head
));
426 int ftrace_profile_pages_init(struct ftrace_profile_stat
*stat
)
428 struct ftrace_profile_page
*pg
;
433 /* If we already allocated, do nothing */
437 stat
->pages
= (void *)get_zeroed_page(GFP_KERNEL
);
441 #ifdef CONFIG_DYNAMIC_FTRACE
442 functions
= ftrace_update_tot_cnt
;
445 * We do not know the number of functions that exist because
446 * dynamic tracing is what counts them. With past experience
447 * we have around 20K functions. That should be more than enough.
448 * It is highly unlikely we will execute every function in
454 pg
= stat
->start
= stat
->pages
;
456 pages
= DIV_ROUND_UP(functions
, PROFILES_PER_PAGE
);
458 for (i
= 0; i
< pages
; i
++) {
459 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
470 unsigned long tmp
= (unsigned long)pg
;
476 free_page((unsigned long)stat
->pages
);
483 static int ftrace_profile_init_cpu(int cpu
)
485 struct ftrace_profile_stat
*stat
;
488 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
491 /* If the profile is already created, simply reset it */
492 ftrace_profile_reset(stat
);
497 * We are profiling all functions, but usually only a few thousand
498 * functions are hit. We'll make a hash of 1024 items.
500 size
= FTRACE_PROFILE_HASH_SIZE
;
502 stat
->hash
= kzalloc(sizeof(struct hlist_head
) * size
, GFP_KERNEL
);
507 if (!ftrace_profile_bits
) {
510 for (; size
; size
>>= 1)
511 ftrace_profile_bits
++;
514 /* Preallocate the function profiling pages */
515 if (ftrace_profile_pages_init(stat
) < 0) {
524 static int ftrace_profile_init(void)
529 for_each_online_cpu(cpu
) {
530 ret
= ftrace_profile_init_cpu(cpu
);
538 /* interrupts must be disabled */
539 static struct ftrace_profile
*
540 ftrace_find_profiled_func(struct ftrace_profile_stat
*stat
, unsigned long ip
)
542 struct ftrace_profile
*rec
;
543 struct hlist_head
*hhd
;
544 struct hlist_node
*n
;
547 key
= hash_long(ip
, ftrace_profile_bits
);
548 hhd
= &stat
->hash
[key
];
550 if (hlist_empty(hhd
))
553 hlist_for_each_entry_rcu(rec
, n
, hhd
, node
) {
561 static void ftrace_add_profile(struct ftrace_profile_stat
*stat
,
562 struct ftrace_profile
*rec
)
566 key
= hash_long(rec
->ip
, ftrace_profile_bits
);
567 hlist_add_head_rcu(&rec
->node
, &stat
->hash
[key
]);
571 * The memory is already allocated, this simply finds a new record to use.
573 static struct ftrace_profile
*
574 ftrace_profile_alloc(struct ftrace_profile_stat
*stat
, unsigned long ip
)
576 struct ftrace_profile
*rec
= NULL
;
578 /* prevent recursion (from NMIs) */
579 if (atomic_inc_return(&stat
->disabled
) != 1)
583 * Try to find the function again since an NMI
584 * could have added it
586 rec
= ftrace_find_profiled_func(stat
, ip
);
590 if (stat
->pages
->index
== PROFILES_PER_PAGE
) {
591 if (!stat
->pages
->next
)
593 stat
->pages
= stat
->pages
->next
;
596 rec
= &stat
->pages
->records
[stat
->pages
->index
++];
598 ftrace_add_profile(stat
, rec
);
601 atomic_dec(&stat
->disabled
);
607 function_profile_call(unsigned long ip
, unsigned long parent_ip
)
609 struct ftrace_profile_stat
*stat
;
610 struct ftrace_profile
*rec
;
613 if (!ftrace_profile_enabled
)
616 local_irq_save(flags
);
618 stat
= &__get_cpu_var(ftrace_profile_stats
);
619 if (!stat
->hash
|| !ftrace_profile_enabled
)
622 rec
= ftrace_find_profiled_func(stat
, ip
);
624 rec
= ftrace_profile_alloc(stat
, ip
);
631 local_irq_restore(flags
);
634 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
635 static int profile_graph_entry(struct ftrace_graph_ent
*trace
)
637 function_profile_call(trace
->func
, 0);
641 static void profile_graph_return(struct ftrace_graph_ret
*trace
)
643 struct ftrace_profile_stat
*stat
;
644 unsigned long long calltime
;
645 struct ftrace_profile
*rec
;
648 local_irq_save(flags
);
649 stat
= &__get_cpu_var(ftrace_profile_stats
);
650 if (!stat
->hash
|| !ftrace_profile_enabled
)
653 calltime
= trace
->rettime
- trace
->calltime
;
655 if (!(trace_flags
& TRACE_ITER_GRAPH_TIME
)) {
658 index
= trace
->depth
;
660 /* Append this call time to the parent time to subtract */
662 current
->ret_stack
[index
- 1].subtime
+= calltime
;
664 if (current
->ret_stack
[index
].subtime
< calltime
)
665 calltime
-= current
->ret_stack
[index
].subtime
;
670 rec
= ftrace_find_profiled_func(stat
, trace
->func
);
672 rec
->time
+= calltime
;
675 local_irq_restore(flags
);
678 static int register_ftrace_profiler(void)
680 return register_ftrace_graph(&profile_graph_return
,
681 &profile_graph_entry
);
684 static void unregister_ftrace_profiler(void)
686 unregister_ftrace_graph();
689 static struct ftrace_ops ftrace_profile_ops __read_mostly
=
691 .func
= function_profile_call
,
694 static int register_ftrace_profiler(void)
696 return register_ftrace_function(&ftrace_profile_ops
);
699 static void unregister_ftrace_profiler(void)
701 unregister_ftrace_function(&ftrace_profile_ops
);
703 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
706 ftrace_profile_write(struct file
*filp
, const char __user
*ubuf
,
707 size_t cnt
, loff_t
*ppos
)
710 char buf
[64]; /* big enough to hold a number */
713 if (cnt
>= sizeof(buf
))
716 if (copy_from_user(&buf
, ubuf
, cnt
))
721 ret
= strict_strtoul(buf
, 10, &val
);
727 mutex_lock(&ftrace_profile_lock
);
728 if (ftrace_profile_enabled
^ val
) {
730 ret
= ftrace_profile_init();
736 ret
= register_ftrace_profiler();
741 ftrace_profile_enabled
= 1;
743 ftrace_profile_enabled
= 0;
745 * unregister_ftrace_profiler calls stop_machine
746 * so this acts like an synchronize_sched.
748 unregister_ftrace_profiler();
752 mutex_unlock(&ftrace_profile_lock
);
760 ftrace_profile_read(struct file
*filp
, char __user
*ubuf
,
761 size_t cnt
, loff_t
*ppos
)
763 char buf
[64]; /* big enough to hold a number */
766 r
= sprintf(buf
, "%u\n", ftrace_profile_enabled
);
767 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
770 static const struct file_operations ftrace_profile_fops
= {
771 .open
= tracing_open_generic
,
772 .read
= ftrace_profile_read
,
773 .write
= ftrace_profile_write
,
776 /* used to initialize the real stat files */
777 static struct tracer_stat function_stats __initdata
= {
779 .stat_start
= function_stat_start
,
780 .stat_next
= function_stat_next
,
781 .stat_cmp
= function_stat_cmp
,
782 .stat_headers
= function_stat_headers
,
783 .stat_show
= function_stat_show
786 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
788 struct ftrace_profile_stat
*stat
;
789 struct dentry
*entry
;
794 for_each_possible_cpu(cpu
) {
795 stat
= &per_cpu(ftrace_profile_stats
, cpu
);
797 /* allocate enough for function name + cpu number */
798 name
= kmalloc(32, GFP_KERNEL
);
801 * The files created are permanent, if something happens
802 * we still do not free memory.
805 "Could not allocate stat file for cpu %d\n",
809 stat
->stat
= function_stats
;
810 snprintf(name
, 32, "function%d", cpu
);
811 stat
->stat
.name
= name
;
812 ret
= register_stat_tracer(&stat
->stat
);
815 "Could not register function stat for cpu %d\n",
822 entry
= debugfs_create_file("function_profile_enabled", 0644,
823 d_tracer
, NULL
, &ftrace_profile_fops
);
825 pr_warning("Could not create debugfs "
826 "'function_profile_enabled' entry\n");
829 #else /* CONFIG_FUNCTION_PROFILER */
830 static __init
void ftrace_profile_debugfs(struct dentry
*d_tracer
)
833 #endif /* CONFIG_FUNCTION_PROFILER */
835 static struct pid
* const ftrace_swapper_pid
= &init_struct_pid
;
837 #ifdef CONFIG_DYNAMIC_FTRACE
839 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
840 # error Dynamic ftrace depends on MCOUNT_RECORD
843 static struct hlist_head ftrace_func_hash
[FTRACE_FUNC_HASHSIZE
] __read_mostly
;
845 struct ftrace_func_probe
{
846 struct hlist_node node
;
847 struct ftrace_probe_ops
*ops
;
855 FTRACE_ENABLE_CALLS
= (1 << 0),
856 FTRACE_DISABLE_CALLS
= (1 << 1),
857 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
858 FTRACE_ENABLE_MCOUNT
= (1 << 3),
859 FTRACE_DISABLE_MCOUNT
= (1 << 4),
860 FTRACE_START_FUNC_RET
= (1 << 5),
861 FTRACE_STOP_FUNC_RET
= (1 << 6),
864 static int ftrace_filtered
;
866 static struct dyn_ftrace
*ftrace_new_addrs
;
868 static DEFINE_MUTEX(ftrace_regex_lock
);
871 struct ftrace_page
*next
;
873 struct dyn_ftrace records
[];
876 #define ENTRIES_PER_PAGE \
877 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
879 /* estimate from running different kernels */
880 #define NR_TO_INIT 10000
882 static struct ftrace_page
*ftrace_pages_start
;
883 static struct ftrace_page
*ftrace_pages
;
885 static struct dyn_ftrace
*ftrace_free_records
;
888 * This is a double for. Do not use 'break' to break out of the loop,
889 * you must use a goto.
891 #define do_for_each_ftrace_rec(pg, rec) \
892 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
894 for (_____i = 0; _____i < pg->index; _____i++) { \
895 rec = &pg->records[_____i];
897 #define while_for_each_ftrace_rec() \
901 #ifdef CONFIG_KPROBES
903 static int frozen_record_count
;
905 static inline void freeze_record(struct dyn_ftrace
*rec
)
907 if (!(rec
->flags
& FTRACE_FL_FROZEN
)) {
908 rec
->flags
|= FTRACE_FL_FROZEN
;
909 frozen_record_count
++;
913 static inline void unfreeze_record(struct dyn_ftrace
*rec
)
915 if (rec
->flags
& FTRACE_FL_FROZEN
) {
916 rec
->flags
&= ~FTRACE_FL_FROZEN
;
917 frozen_record_count
--;
921 static inline int record_frozen(struct dyn_ftrace
*rec
)
923 return rec
->flags
& FTRACE_FL_FROZEN
;
926 # define freeze_record(rec) ({ 0; })
927 # define unfreeze_record(rec) ({ 0; })
928 # define record_frozen(rec) ({ 0; })
929 #endif /* CONFIG_KPROBES */
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 ");
1029 __ftrace_replace_code(struct dyn_ftrace
*rec
, int enable
)
1031 unsigned long ftrace_addr
;
1032 unsigned long flag
= 0UL;
1034 ftrace_addr
= (unsigned long)FTRACE_ADDR
;
1037 * If this record is not to be traced or we want to disable it,
1040 * If we want to enable it and filtering is off, then enable it.
1042 * If we want to enable it and filtering is on, enable it only if
1045 if (enable
&& !(rec
->flags
& FTRACE_FL_NOTRACE
)) {
1046 if (!ftrace_filtered
|| (rec
->flags
& FTRACE_FL_FILTER
))
1047 flag
= FTRACE_FL_ENABLED
;
1050 /* If the state of this record hasn't changed, then do nothing */
1051 if ((rec
->flags
& FTRACE_FL_ENABLED
) == flag
)
1055 rec
->flags
|= FTRACE_FL_ENABLED
;
1056 return ftrace_make_call(rec
, ftrace_addr
);
1059 rec
->flags
&= ~FTRACE_FL_ENABLED
;
1060 return ftrace_make_nop(NULL
, rec
, ftrace_addr
);
1063 static void ftrace_replace_code(int enable
)
1065 struct dyn_ftrace
*rec
;
1066 struct ftrace_page
*pg
;
1069 do_for_each_ftrace_rec(pg
, rec
) {
1071 * Skip over free records, records that have
1072 * failed and not converted.
1074 if (rec
->flags
& FTRACE_FL_FREE
||
1075 rec
->flags
& FTRACE_FL_FAILED
||
1076 !(rec
->flags
& FTRACE_FL_CONVERTED
))
1079 /* ignore updates to this record's mcount site */
1080 if (get_kprobe((void *)rec
->ip
)) {
1084 unfreeze_record(rec
);
1087 failed
= __ftrace_replace_code(rec
, enable
);
1089 rec
->flags
|= FTRACE_FL_FAILED
;
1090 ftrace_bug(failed
, rec
->ip
);
1091 /* Stop processing */
1094 } while_for_each_ftrace_rec();
1098 ftrace_code_disable(struct module
*mod
, struct dyn_ftrace
*rec
)
1105 ret
= ftrace_make_nop(mod
, rec
, MCOUNT_ADDR
);
1107 ftrace_bug(ret
, ip
);
1108 rec
->flags
|= FTRACE_FL_FAILED
;
1115 * archs can override this function if they must do something
1116 * before the modifying code is performed.
1118 int __weak
ftrace_arch_code_modify_prepare(void)
1124 * archs can override this function if they must do something
1125 * after the modifying code is performed.
1127 int __weak
ftrace_arch_code_modify_post_process(void)
1132 static int __ftrace_modify_code(void *data
)
1134 int *command
= data
;
1136 if (*command
& FTRACE_ENABLE_CALLS
)
1137 ftrace_replace_code(1);
1138 else if (*command
& FTRACE_DISABLE_CALLS
)
1139 ftrace_replace_code(0);
1141 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
1142 ftrace_update_ftrace_func(ftrace_trace_function
);
1144 if (*command
& FTRACE_START_FUNC_RET
)
1145 ftrace_enable_ftrace_graph_caller();
1146 else if (*command
& FTRACE_STOP_FUNC_RET
)
1147 ftrace_disable_ftrace_graph_caller();
1152 static void ftrace_run_update_code(int command
)
1156 ret
= ftrace_arch_code_modify_prepare();
1157 FTRACE_WARN_ON(ret
);
1161 stop_machine(__ftrace_modify_code
, &command
, NULL
);
1163 ret
= ftrace_arch_code_modify_post_process();
1164 FTRACE_WARN_ON(ret
);
1167 static ftrace_func_t saved_ftrace_func
;
1168 static int ftrace_start_up
;
1170 static void ftrace_startup_enable(int command
)
1172 if (saved_ftrace_func
!= ftrace_trace_function
) {
1173 saved_ftrace_func
= ftrace_trace_function
;
1174 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1177 if (!command
|| !ftrace_enabled
)
1180 ftrace_run_update_code(command
);
1183 static void ftrace_startup(int command
)
1185 if (unlikely(ftrace_disabled
))
1189 command
|= FTRACE_ENABLE_CALLS
;
1191 ftrace_startup_enable(command
);
1194 static void ftrace_shutdown(int command
)
1196 if (unlikely(ftrace_disabled
))
1201 * Just warn in case of unbalance, no need to kill ftrace, it's not
1202 * critical but the ftrace_call callers may be never nopped again after
1203 * further ftrace uses.
1205 WARN_ON_ONCE(ftrace_start_up
< 0);
1207 if (!ftrace_start_up
)
1208 command
|= FTRACE_DISABLE_CALLS
;
1210 if (saved_ftrace_func
!= ftrace_trace_function
) {
1211 saved_ftrace_func
= ftrace_trace_function
;
1212 command
|= FTRACE_UPDATE_TRACE_FUNC
;
1215 if (!command
|| !ftrace_enabled
)
1218 ftrace_run_update_code(command
);
1221 static void ftrace_startup_sysctl(void)
1223 int command
= FTRACE_ENABLE_MCOUNT
;
1225 if (unlikely(ftrace_disabled
))
1228 /* Force update next time */
1229 saved_ftrace_func
= NULL
;
1230 /* ftrace_start_up is true if we want ftrace running */
1231 if (ftrace_start_up
)
1232 command
|= FTRACE_ENABLE_CALLS
;
1234 ftrace_run_update_code(command
);
1237 static void ftrace_shutdown_sysctl(void)
1239 int command
= FTRACE_DISABLE_MCOUNT
;
1241 if (unlikely(ftrace_disabled
))
1244 /* ftrace_start_up is true if ftrace is running */
1245 if (ftrace_start_up
)
1246 command
|= FTRACE_DISABLE_CALLS
;
1248 ftrace_run_update_code(command
);
1251 static cycle_t ftrace_update_time
;
1252 static unsigned long ftrace_update_cnt
;
1253 unsigned long ftrace_update_tot_cnt
;
1255 static int ftrace_update_code(struct module
*mod
)
1257 struct dyn_ftrace
*p
;
1258 cycle_t start
, stop
;
1260 start
= ftrace_now(raw_smp_processor_id());
1261 ftrace_update_cnt
= 0;
1263 while (ftrace_new_addrs
) {
1265 /* If something went wrong, bail without enabling anything */
1266 if (unlikely(ftrace_disabled
))
1269 p
= ftrace_new_addrs
;
1270 ftrace_new_addrs
= p
->newlist
;
1274 * Do the initial record convertion from mcount jump
1275 * to the NOP instructions.
1277 if (!ftrace_code_disable(mod
, p
)) {
1282 p
->flags
|= FTRACE_FL_CONVERTED
;
1283 ftrace_update_cnt
++;
1286 * If the tracing is enabled, go ahead and enable the record.
1288 * The reason not to enable the record immediatelly is the
1289 * inherent check of ftrace_make_nop/ftrace_make_call for
1290 * correct previous instructions. Making first the NOP
1291 * conversion puts the module to the correct state, thus
1292 * passing the ftrace_make_call check.
1294 if (ftrace_start_up
) {
1295 int failed
= __ftrace_replace_code(p
, 1);
1297 ftrace_bug(failed
, p
->ip
);
1303 stop
= ftrace_now(raw_smp_processor_id());
1304 ftrace_update_time
= stop
- start
;
1305 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
1310 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
1312 struct ftrace_page
*pg
;
1316 /* allocate a few pages */
1317 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
1318 if (!ftrace_pages_start
)
1322 * Allocate a few more pages.
1324 * TODO: have some parser search vmlinux before
1325 * final linking to find all calls to ftrace.
1327 * a) know how many pages to allocate.
1329 * b) set up the table then.
1331 * The dynamic code is still necessary for
1335 pg
= ftrace_pages
= ftrace_pages_start
;
1337 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
1338 pr_info("ftrace: allocating %ld entries in %d pages\n",
1339 num_to_init
, cnt
+ 1);
1341 for (i
= 0; i
< cnt
; i
++) {
1342 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
1344 /* If we fail, we'll try later anyway */
1355 FTRACE_ITER_FILTER
= (1 << 0),
1356 FTRACE_ITER_NOTRACE
= (1 << 1),
1357 FTRACE_ITER_FAILURES
= (1 << 2),
1358 FTRACE_ITER_PRINTALL
= (1 << 3),
1359 FTRACE_ITER_HASH
= (1 << 4),
1362 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1364 struct ftrace_iterator
{
1365 struct ftrace_page
*pg
;
1369 struct trace_parser parser
;
1373 t_hash_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1375 struct ftrace_iterator
*iter
= m
->private;
1376 struct hlist_node
*hnd
= v
;
1377 struct hlist_head
*hhd
;
1379 WARN_ON(!(iter
->flags
& FTRACE_ITER_HASH
));
1384 if (iter
->hidx
>= FTRACE_FUNC_HASHSIZE
)
1387 hhd
= &ftrace_func_hash
[iter
->hidx
];
1389 if (hlist_empty(hhd
)) {
1408 static void *t_hash_start(struct seq_file
*m
, loff_t
*pos
)
1410 struct ftrace_iterator
*iter
= m
->private;
1414 if (!(iter
->flags
& FTRACE_ITER_HASH
))
1417 iter
->flags
|= FTRACE_ITER_HASH
;
1420 for (l
= 0; l
<= *pos
; ) {
1421 p
= t_hash_next(m
, p
, &l
);
1428 static int t_hash_show(struct seq_file
*m
, void *v
)
1430 struct ftrace_func_probe
*rec
;
1431 struct hlist_node
*hnd
= v
;
1433 rec
= hlist_entry(hnd
, struct ftrace_func_probe
, node
);
1435 if (rec
->ops
->print
)
1436 return rec
->ops
->print(m
, rec
->ip
, rec
->ops
, rec
->data
);
1438 seq_printf(m
, "%ps:%ps", (void *)rec
->ip
, (void *)rec
->ops
->func
);
1441 seq_printf(m
, ":%p", rec
->data
);
1448 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1450 struct ftrace_iterator
*iter
= m
->private;
1451 struct dyn_ftrace
*rec
= NULL
;
1453 if (iter
->flags
& FTRACE_ITER_HASH
)
1454 return t_hash_next(m
, v
, pos
);
1458 if (iter
->flags
& FTRACE_ITER_PRINTALL
)
1462 if (iter
->idx
>= iter
->pg
->index
) {
1463 if (iter
->pg
->next
) {
1464 iter
->pg
= iter
->pg
->next
;
1469 rec
= &iter
->pg
->records
[iter
->idx
++];
1470 if ((rec
->flags
& FTRACE_FL_FREE
) ||
1472 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
1473 (rec
->flags
& FTRACE_FL_FAILED
)) ||
1475 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
1476 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
1478 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
1479 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
1481 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
1482 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
1491 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1493 struct ftrace_iterator
*iter
= m
->private;
1497 mutex_lock(&ftrace_lock
);
1499 * For set_ftrace_filter reading, if we have the filter
1500 * off, we can short cut and just print out that all
1501 * functions are enabled.
1503 if (iter
->flags
& FTRACE_ITER_FILTER
&& !ftrace_filtered
) {
1505 return t_hash_start(m
, pos
);
1506 iter
->flags
|= FTRACE_ITER_PRINTALL
;
1510 if (iter
->flags
& FTRACE_ITER_HASH
)
1511 return t_hash_start(m
, pos
);
1513 iter
->pg
= ftrace_pages_start
;
1515 for (l
= 0; l
<= *pos
; ) {
1516 p
= t_next(m
, p
, &l
);
1521 if (!p
&& iter
->flags
& FTRACE_ITER_FILTER
)
1522 return t_hash_start(m
, pos
);
1527 static void t_stop(struct seq_file
*m
, void *p
)
1529 mutex_unlock(&ftrace_lock
);
1532 static int t_show(struct seq_file
*m
, void *v
)
1534 struct ftrace_iterator
*iter
= m
->private;
1535 struct dyn_ftrace
*rec
= v
;
1537 if (iter
->flags
& FTRACE_ITER_HASH
)
1538 return t_hash_show(m
, v
);
1540 if (iter
->flags
& FTRACE_ITER_PRINTALL
) {
1541 seq_printf(m
, "#### all functions enabled ####\n");
1548 seq_printf(m
, "%ps\n", (void *)rec
->ip
);
1553 static const struct seq_operations show_ftrace_seq_ops
= {
1561 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
1563 struct ftrace_iterator
*iter
;
1566 if (unlikely(ftrace_disabled
))
1569 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1573 iter
->pg
= ftrace_pages_start
;
1575 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1577 struct seq_file
*m
= file
->private_data
;
1588 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
1592 struct ftrace_iterator
*iter
;
1594 ret
= ftrace_avail_open(inode
, file
);
1596 m
= (struct seq_file
*)file
->private_data
;
1597 iter
= (struct ftrace_iterator
*)m
->private;
1598 iter
->flags
= FTRACE_ITER_FAILURES
;
1605 static void ftrace_filter_reset(int enable
)
1607 struct ftrace_page
*pg
;
1608 struct dyn_ftrace
*rec
;
1609 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1611 mutex_lock(&ftrace_lock
);
1613 ftrace_filtered
= 0;
1614 do_for_each_ftrace_rec(pg
, rec
) {
1615 if (rec
->flags
& FTRACE_FL_FAILED
)
1617 rec
->flags
&= ~type
;
1618 } while_for_each_ftrace_rec();
1619 mutex_unlock(&ftrace_lock
);
1623 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
1625 struct ftrace_iterator
*iter
;
1628 if (unlikely(ftrace_disabled
))
1631 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1635 if (trace_parser_get_init(&iter
->parser
, FTRACE_BUFF_MAX
)) {
1640 mutex_lock(&ftrace_regex_lock
);
1641 if ((file
->f_mode
& FMODE_WRITE
) &&
1642 (file
->f_flags
& O_TRUNC
))
1643 ftrace_filter_reset(enable
);
1645 if (file
->f_mode
& FMODE_READ
) {
1646 iter
->pg
= ftrace_pages_start
;
1647 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
1648 FTRACE_ITER_NOTRACE
;
1650 ret
= seq_open(file
, &show_ftrace_seq_ops
);
1652 struct seq_file
*m
= file
->private_data
;
1655 trace_parser_put(&iter
->parser
);
1659 file
->private_data
= iter
;
1660 mutex_unlock(&ftrace_regex_lock
);
1666 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
1668 return ftrace_regex_open(inode
, file
, 1);
1672 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
1674 return ftrace_regex_open(inode
, file
, 0);
1678 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
1682 if (file
->f_mode
& FMODE_READ
)
1683 ret
= seq_lseek(file
, offset
, origin
);
1685 file
->f_pos
= ret
= 1;
1690 static int ftrace_match(char *str
, char *regex
, int len
, int type
)
1697 if (strcmp(str
, regex
) == 0)
1700 case MATCH_FRONT_ONLY
:
1701 if (strncmp(str
, regex
, len
) == 0)
1704 case MATCH_MIDDLE_ONLY
:
1705 if (strstr(str
, regex
))
1708 case MATCH_END_ONLY
:
1710 if (slen
>= len
&& memcmp(str
+ slen
- len
, regex
, len
) == 0)
1719 ftrace_match_record(struct dyn_ftrace
*rec
, char *regex
, int len
, int type
)
1721 char str
[KSYM_SYMBOL_LEN
];
1723 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1724 return ftrace_match(str
, regex
, len
, type
);
1727 static int ftrace_match_records(char *buff
, int len
, int enable
)
1729 unsigned int search_len
;
1730 struct ftrace_page
*pg
;
1731 struct dyn_ftrace
*rec
;
1738 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1739 type
= filter_parse_regex(buff
, len
, &search
, ¬);
1741 search_len
= strlen(search
);
1743 mutex_lock(&ftrace_lock
);
1744 do_for_each_ftrace_rec(pg
, rec
) {
1746 if (rec
->flags
& FTRACE_FL_FAILED
)
1749 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
1751 rec
->flags
&= ~flag
;
1757 * Only enable filtering if we have a function that
1760 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1761 ftrace_filtered
= 1;
1762 } while_for_each_ftrace_rec();
1763 mutex_unlock(&ftrace_lock
);
1769 ftrace_match_module_record(struct dyn_ftrace
*rec
, char *mod
,
1770 char *regex
, int len
, int type
)
1772 char str
[KSYM_SYMBOL_LEN
];
1775 kallsyms_lookup(rec
->ip
, NULL
, NULL
, &modname
, str
);
1777 if (!modname
|| strcmp(modname
, mod
))
1780 /* blank search means to match all funcs in the mod */
1782 return ftrace_match(str
, regex
, len
, type
);
1787 static int ftrace_match_module_records(char *buff
, char *mod
, int enable
)
1789 unsigned search_len
= 0;
1790 struct ftrace_page
*pg
;
1791 struct dyn_ftrace
*rec
;
1792 int type
= MATCH_FULL
;
1793 char *search
= buff
;
1798 flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1800 /* blank or '*' mean the same */
1801 if (strcmp(buff
, "*") == 0)
1804 /* handle the case of 'dont filter this module' */
1805 if (strcmp(buff
, "!") == 0 || strcmp(buff
, "!*") == 0) {
1811 type
= filter_parse_regex(buff
, strlen(buff
), &search
, ¬);
1812 search_len
= strlen(search
);
1815 mutex_lock(&ftrace_lock
);
1816 do_for_each_ftrace_rec(pg
, rec
) {
1818 if (rec
->flags
& FTRACE_FL_FAILED
)
1821 if (ftrace_match_module_record(rec
, mod
,
1822 search
, search_len
, type
)) {
1824 rec
->flags
&= ~flag
;
1829 if (enable
&& (rec
->flags
& FTRACE_FL_FILTER
))
1830 ftrace_filtered
= 1;
1832 } while_for_each_ftrace_rec();
1833 mutex_unlock(&ftrace_lock
);
1839 * We register the module command as a template to show others how
1840 * to register the a command as well.
1844 ftrace_mod_callback(char *func
, char *cmd
, char *param
, int enable
)
1849 * cmd == 'mod' because we only registered this func
1850 * for the 'mod' ftrace_func_command.
1851 * But if you register one func with multiple commands,
1852 * you can tell which command was used by the cmd
1856 /* we must have a module name */
1860 mod
= strsep(¶m
, ":");
1864 if (ftrace_match_module_records(func
, mod
, enable
))
1869 static struct ftrace_func_command ftrace_mod_cmd
= {
1871 .func
= ftrace_mod_callback
,
1874 static int __init
ftrace_mod_cmd_init(void)
1876 return register_ftrace_command(&ftrace_mod_cmd
);
1878 device_initcall(ftrace_mod_cmd_init
);
1881 function_trace_probe_call(unsigned long ip
, unsigned long parent_ip
)
1883 struct ftrace_func_probe
*entry
;
1884 struct hlist_head
*hhd
;
1885 struct hlist_node
*n
;
1889 key
= hash_long(ip
, FTRACE_HASH_BITS
);
1891 hhd
= &ftrace_func_hash
[key
];
1893 if (hlist_empty(hhd
))
1897 * Disable preemption for these calls to prevent a RCU grace
1898 * period. This syncs the hash iteration and freeing of items
1899 * on the hash. rcu_read_lock is too dangerous here.
1901 resched
= ftrace_preempt_disable();
1902 hlist_for_each_entry_rcu(entry
, n
, hhd
, node
) {
1903 if (entry
->ip
== ip
)
1904 entry
->ops
->func(ip
, parent_ip
, &entry
->data
);
1906 ftrace_preempt_enable(resched
);
1909 static struct ftrace_ops trace_probe_ops __read_mostly
=
1911 .func
= function_trace_probe_call
,
1914 static int ftrace_probe_registered
;
1916 static void __enable_ftrace_function_probe(void)
1920 if (ftrace_probe_registered
)
1923 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1924 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1928 /* Nothing registered? */
1929 if (i
== FTRACE_FUNC_HASHSIZE
)
1932 __register_ftrace_function(&trace_probe_ops
);
1934 ftrace_probe_registered
= 1;
1937 static void __disable_ftrace_function_probe(void)
1941 if (!ftrace_probe_registered
)
1944 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
1945 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
1950 /* no more funcs left */
1951 __unregister_ftrace_function(&trace_probe_ops
);
1953 ftrace_probe_registered
= 0;
1957 static void ftrace_free_entry_rcu(struct rcu_head
*rhp
)
1959 struct ftrace_func_probe
*entry
=
1960 container_of(rhp
, struct ftrace_func_probe
, rcu
);
1962 if (entry
->ops
->free
)
1963 entry
->ops
->free(&entry
->data
);
1969 register_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
1972 struct ftrace_func_probe
*entry
;
1973 struct ftrace_page
*pg
;
1974 struct dyn_ftrace
*rec
;
1980 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
1981 len
= strlen(search
);
1983 /* we do not support '!' for function probes */
1987 mutex_lock(&ftrace_lock
);
1988 do_for_each_ftrace_rec(pg
, rec
) {
1990 if (rec
->flags
& FTRACE_FL_FAILED
)
1993 if (!ftrace_match_record(rec
, search
, len
, type
))
1996 entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
);
1998 /* If we did not process any, then return error */
2009 * The caller might want to do something special
2010 * for each function we find. We call the callback
2011 * to give the caller an opportunity to do so.
2013 if (ops
->callback
) {
2014 if (ops
->callback(rec
->ip
, &entry
->data
) < 0) {
2015 /* caller does not like this func */
2022 entry
->ip
= rec
->ip
;
2024 key
= hash_long(entry
->ip
, FTRACE_HASH_BITS
);
2025 hlist_add_head_rcu(&entry
->node
, &ftrace_func_hash
[key
]);
2027 } while_for_each_ftrace_rec();
2028 __enable_ftrace_function_probe();
2031 mutex_unlock(&ftrace_lock
);
2037 PROBE_TEST_FUNC
= 1,
2042 __unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2043 void *data
, int flags
)
2045 struct ftrace_func_probe
*entry
;
2046 struct hlist_node
*n
, *tmp
;
2047 char str
[KSYM_SYMBOL_LEN
];
2048 int type
= MATCH_FULL
;
2052 if (glob
&& (strcmp(glob
, "*") == 0 || !strlen(glob
)))
2057 type
= filter_parse_regex(glob
, strlen(glob
), &search
, ¬);
2058 len
= strlen(search
);
2060 /* we do not support '!' for function probes */
2065 mutex_lock(&ftrace_lock
);
2066 for (i
= 0; i
< FTRACE_FUNC_HASHSIZE
; i
++) {
2067 struct hlist_head
*hhd
= &ftrace_func_hash
[i
];
2069 hlist_for_each_entry_safe(entry
, n
, tmp
, hhd
, node
) {
2071 /* break up if statements for readability */
2072 if ((flags
& PROBE_TEST_FUNC
) && entry
->ops
!= ops
)
2075 if ((flags
& PROBE_TEST_DATA
) && entry
->data
!= data
)
2078 /* do this last, since it is the most expensive */
2080 kallsyms_lookup(entry
->ip
, NULL
, NULL
,
2082 if (!ftrace_match(str
, glob
, len
, type
))
2086 hlist_del(&entry
->node
);
2087 call_rcu(&entry
->rcu
, ftrace_free_entry_rcu
);
2090 __disable_ftrace_function_probe();
2091 mutex_unlock(&ftrace_lock
);
2095 unregister_ftrace_function_probe(char *glob
, struct ftrace_probe_ops
*ops
,
2098 __unregister_ftrace_function_probe(glob
, ops
, data
,
2099 PROBE_TEST_FUNC
| PROBE_TEST_DATA
);
2103 unregister_ftrace_function_probe_func(char *glob
, struct ftrace_probe_ops
*ops
)
2105 __unregister_ftrace_function_probe(glob
, ops
, NULL
, PROBE_TEST_FUNC
);
2108 void unregister_ftrace_function_probe_all(char *glob
)
2110 __unregister_ftrace_function_probe(glob
, NULL
, NULL
, 0);
2113 static LIST_HEAD(ftrace_commands
);
2114 static DEFINE_MUTEX(ftrace_cmd_mutex
);
2116 int register_ftrace_command(struct ftrace_func_command
*cmd
)
2118 struct ftrace_func_command
*p
;
2121 mutex_lock(&ftrace_cmd_mutex
);
2122 list_for_each_entry(p
, &ftrace_commands
, list
) {
2123 if (strcmp(cmd
->name
, p
->name
) == 0) {
2128 list_add(&cmd
->list
, &ftrace_commands
);
2130 mutex_unlock(&ftrace_cmd_mutex
);
2135 int unregister_ftrace_command(struct ftrace_func_command
*cmd
)
2137 struct ftrace_func_command
*p
, *n
;
2140 mutex_lock(&ftrace_cmd_mutex
);
2141 list_for_each_entry_safe(p
, n
, &ftrace_commands
, list
) {
2142 if (strcmp(cmd
->name
, p
->name
) == 0) {
2144 list_del_init(&p
->list
);
2149 mutex_unlock(&ftrace_cmd_mutex
);
2154 static int ftrace_process_regex(char *buff
, int len
, int enable
)
2156 char *func
, *command
, *next
= buff
;
2157 struct ftrace_func_command
*p
;
2160 func
= strsep(&next
, ":");
2163 if (ftrace_match_records(func
, len
, enable
))
2170 command
= strsep(&next
, ":");
2172 mutex_lock(&ftrace_cmd_mutex
);
2173 list_for_each_entry(p
, &ftrace_commands
, list
) {
2174 if (strcmp(p
->name
, command
) == 0) {
2175 ret
= p
->func(func
, command
, next
, enable
);
2180 mutex_unlock(&ftrace_cmd_mutex
);
2186 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
2187 size_t cnt
, loff_t
*ppos
, int enable
)
2189 struct ftrace_iterator
*iter
;
2190 struct trace_parser
*parser
;
2196 mutex_lock(&ftrace_regex_lock
);
2198 if (file
->f_mode
& FMODE_READ
) {
2199 struct seq_file
*m
= file
->private_data
;
2202 iter
= file
->private_data
;
2204 parser
= &iter
->parser
;
2205 read
= trace_get_user(parser
, ubuf
, cnt
, ppos
);
2207 if (read
>= 0 && trace_parser_loaded(parser
) &&
2208 !trace_parser_cont(parser
)) {
2209 ret
= ftrace_process_regex(parser
->buffer
,
2210 parser
->idx
, enable
);
2211 trace_parser_clear(parser
);
2218 mutex_unlock(&ftrace_regex_lock
);
2224 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
2225 size_t cnt
, loff_t
*ppos
)
2227 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
2231 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
2232 size_t cnt
, loff_t
*ppos
)
2234 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
2238 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
2240 if (unlikely(ftrace_disabled
))
2243 mutex_lock(&ftrace_regex_lock
);
2245 ftrace_filter_reset(enable
);
2247 ftrace_match_records(buf
, len
, enable
);
2248 mutex_unlock(&ftrace_regex_lock
);
2252 * ftrace_set_filter - set a function to filter on in ftrace
2253 * @buf - the string that holds the function filter text.
2254 * @len - the length of the string.
2255 * @reset - non zero to reset all filters before applying this filter.
2257 * Filters denote which functions should be enabled when tracing is enabled.
2258 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2260 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
2262 ftrace_set_regex(buf
, len
, reset
, 1);
2266 * ftrace_set_notrace - set a function to not trace in ftrace
2267 * @buf - the string that holds the function notrace text.
2268 * @len - the length of the string.
2269 * @reset - non zero to reset all filters before applying this filter.
2271 * Notrace Filters denote which functions should not be enabled when tracing
2272 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2275 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
2277 ftrace_set_regex(buf
, len
, reset
, 0);
2281 * command line interface to allow users to set filters on boot up.
2283 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2284 static char ftrace_notrace_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2285 static char ftrace_filter_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2287 static int __init
set_ftrace_notrace(char *str
)
2289 strncpy(ftrace_notrace_buf
, str
, FTRACE_FILTER_SIZE
);
2292 __setup("ftrace_notrace=", set_ftrace_notrace
);
2294 static int __init
set_ftrace_filter(char *str
)
2296 strncpy(ftrace_filter_buf
, str
, FTRACE_FILTER_SIZE
);
2299 __setup("ftrace_filter=", set_ftrace_filter
);
2301 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2302 static char ftrace_graph_buf
[FTRACE_FILTER_SIZE
] __initdata
;
2303 static int __init
set_graph_function(char *str
)
2305 strlcpy(ftrace_graph_buf
, str
, FTRACE_FILTER_SIZE
);
2308 __setup("ftrace_graph_filter=", set_graph_function
);
2310 static void __init
set_ftrace_early_graph(char *buf
)
2316 func
= strsep(&buf
, ",");
2317 /* we allow only one expression at a time */
2318 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2321 printk(KERN_DEBUG
"ftrace: function %s not "
2322 "traceable\n", func
);
2325 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2327 static void __init
set_ftrace_early_filter(char *buf
, int enable
)
2332 func
= strsep(&buf
, ",");
2333 ftrace_set_regex(func
, strlen(func
), 0, enable
);
2337 static void __init
set_ftrace_early_filters(void)
2339 if (ftrace_filter_buf
[0])
2340 set_ftrace_early_filter(ftrace_filter_buf
, 1);
2341 if (ftrace_notrace_buf
[0])
2342 set_ftrace_early_filter(ftrace_notrace_buf
, 0);
2343 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2344 if (ftrace_graph_buf
[0])
2345 set_ftrace_early_graph(ftrace_graph_buf
);
2346 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2350 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
2352 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2353 struct ftrace_iterator
*iter
;
2354 struct trace_parser
*parser
;
2356 mutex_lock(&ftrace_regex_lock
);
2357 if (file
->f_mode
& FMODE_READ
) {
2360 seq_release(inode
, file
);
2362 iter
= file
->private_data
;
2364 parser
= &iter
->parser
;
2365 if (trace_parser_loaded(parser
)) {
2366 parser
->buffer
[parser
->idx
] = 0;
2367 ftrace_match_records(parser
->buffer
, parser
->idx
, enable
);
2370 mutex_lock(&ftrace_lock
);
2371 if (ftrace_start_up
&& ftrace_enabled
)
2372 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
2373 mutex_unlock(&ftrace_lock
);
2375 trace_parser_put(parser
);
2378 mutex_unlock(&ftrace_regex_lock
);
2383 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
2385 return ftrace_regex_release(inode
, file
, 1);
2389 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
2391 return ftrace_regex_release(inode
, file
, 0);
2394 static const struct file_operations ftrace_avail_fops
= {
2395 .open
= ftrace_avail_open
,
2397 .llseek
= seq_lseek
,
2398 .release
= seq_release_private
,
2401 static const struct file_operations ftrace_failures_fops
= {
2402 .open
= ftrace_failures_open
,
2404 .llseek
= seq_lseek
,
2405 .release
= seq_release_private
,
2408 static const struct file_operations ftrace_filter_fops
= {
2409 .open
= ftrace_filter_open
,
2411 .write
= ftrace_filter_write
,
2412 .llseek
= ftrace_regex_lseek
,
2413 .release
= ftrace_filter_release
,
2416 static const struct file_operations ftrace_notrace_fops
= {
2417 .open
= ftrace_notrace_open
,
2419 .write
= ftrace_notrace_write
,
2420 .llseek
= ftrace_regex_lseek
,
2421 .release
= ftrace_notrace_release
,
2424 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2426 static DEFINE_MUTEX(graph_lock
);
2428 int ftrace_graph_count
;
2429 unsigned long ftrace_graph_funcs
[FTRACE_GRAPH_MAX_FUNCS
] __read_mostly
;
2432 __g_next(struct seq_file
*m
, loff_t
*pos
)
2434 if (*pos
>= ftrace_graph_count
)
2436 return &ftrace_graph_funcs
[*pos
];
2440 g_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2443 return __g_next(m
, pos
);
2446 static void *g_start(struct seq_file
*m
, loff_t
*pos
)
2448 mutex_lock(&graph_lock
);
2450 /* Nothing, tell g_show to print all functions are enabled */
2451 if (!ftrace_graph_count
&& !*pos
)
2454 return __g_next(m
, pos
);
2457 static void g_stop(struct seq_file
*m
, void *p
)
2459 mutex_unlock(&graph_lock
);
2462 static int g_show(struct seq_file
*m
, void *v
)
2464 unsigned long *ptr
= v
;
2469 if (ptr
== (unsigned long *)1) {
2470 seq_printf(m
, "#### all functions enabled ####\n");
2474 seq_printf(m
, "%ps\n", (void *)*ptr
);
2479 static const struct seq_operations ftrace_graph_seq_ops
= {
2487 ftrace_graph_open(struct inode
*inode
, struct file
*file
)
2491 if (unlikely(ftrace_disabled
))
2494 mutex_lock(&graph_lock
);
2495 if ((file
->f_mode
& FMODE_WRITE
) &&
2496 (file
->f_flags
& O_TRUNC
)) {
2497 ftrace_graph_count
= 0;
2498 memset(ftrace_graph_funcs
, 0, sizeof(ftrace_graph_funcs
));
2500 mutex_unlock(&graph_lock
);
2502 if (file
->f_mode
& FMODE_READ
)
2503 ret
= seq_open(file
, &ftrace_graph_seq_ops
);
2509 ftrace_graph_release(struct inode
*inode
, struct file
*file
)
2511 if (file
->f_mode
& FMODE_READ
)
2512 seq_release(inode
, file
);
2517 ftrace_set_func(unsigned long *array
, int *idx
, char *buffer
)
2519 struct dyn_ftrace
*rec
;
2520 struct ftrace_page
*pg
;
2528 if (ftrace_disabled
)
2532 type
= filter_parse_regex(buffer
, strlen(buffer
), &search
, ¬);
2536 search_len
= strlen(search
);
2538 mutex_lock(&ftrace_lock
);
2539 do_for_each_ftrace_rec(pg
, rec
) {
2541 if (*idx
>= FTRACE_GRAPH_MAX_FUNCS
)
2544 if (rec
->flags
& (FTRACE_FL_FAILED
| FTRACE_FL_FREE
))
2547 if (ftrace_match_record(rec
, search
, search_len
, type
)) {
2548 /* ensure it is not already in the array */
2550 for (i
= 0; i
< *idx
; i
++)
2551 if (array
[i
] == rec
->ip
) {
2556 array
[(*idx
)++] = rec
->ip
;
2559 } while_for_each_ftrace_rec();
2561 mutex_unlock(&ftrace_lock
);
2563 return found
? 0 : -EINVAL
;
2567 ftrace_graph_write(struct file
*file
, const char __user
*ubuf
,
2568 size_t cnt
, loff_t
*ppos
)
2570 struct trace_parser parser
;
2573 if (!cnt
|| cnt
< 0)
2576 mutex_lock(&graph_lock
);
2578 if (ftrace_graph_count
>= FTRACE_GRAPH_MAX_FUNCS
) {
2583 if (trace_parser_get_init(&parser
, FTRACE_BUFF_MAX
)) {
2588 read
= trace_get_user(&parser
, ubuf
, cnt
, ppos
);
2590 if (read
>= 0 && trace_parser_loaded((&parser
))) {
2591 parser
.buffer
[parser
.idx
] = 0;
2593 /* we allow only one expression at a time */
2594 ret
= ftrace_set_func(ftrace_graph_funcs
, &ftrace_graph_count
,
2603 trace_parser_put(&parser
);
2605 mutex_unlock(&graph_lock
);
2610 static const struct file_operations ftrace_graph_fops
= {
2611 .open
= ftrace_graph_open
,
2613 .write
= ftrace_graph_write
,
2614 .release
= ftrace_graph_release
,
2616 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2618 static __init
int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
)
2621 trace_create_file("available_filter_functions", 0444,
2622 d_tracer
, NULL
, &ftrace_avail_fops
);
2624 trace_create_file("failures", 0444,
2625 d_tracer
, NULL
, &ftrace_failures_fops
);
2627 trace_create_file("set_ftrace_filter", 0644, d_tracer
,
2628 NULL
, &ftrace_filter_fops
);
2630 trace_create_file("set_ftrace_notrace", 0644, d_tracer
,
2631 NULL
, &ftrace_notrace_fops
);
2633 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2634 trace_create_file("set_graph_function", 0444, d_tracer
,
2636 &ftrace_graph_fops
);
2637 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2642 static int ftrace_process_locs(struct module
*mod
,
2643 unsigned long *start
,
2648 unsigned long flags
;
2650 mutex_lock(&ftrace_lock
);
2653 addr
= ftrace_call_adjust(*p
++);
2655 * Some architecture linkers will pad between
2656 * the different mcount_loc sections of different
2657 * object files to satisfy alignments.
2658 * Skip any NULL pointers.
2662 ftrace_record_ip(addr
);
2665 /* disable interrupts to prevent kstop machine */
2666 local_irq_save(flags
);
2667 ftrace_update_code(mod
);
2668 local_irq_restore(flags
);
2669 mutex_unlock(&ftrace_lock
);
2674 #ifdef CONFIG_MODULES
2675 void ftrace_release_mod(struct module
*mod
)
2677 struct dyn_ftrace
*rec
;
2678 struct ftrace_page
*pg
;
2680 if (ftrace_disabled
)
2683 mutex_lock(&ftrace_lock
);
2684 do_for_each_ftrace_rec(pg
, rec
) {
2685 if (within_module_core(rec
->ip
, mod
)) {
2687 * rec->ip is changed in ftrace_free_rec()
2688 * It should not between s and e if record was freed.
2690 FTRACE_WARN_ON(rec
->flags
& FTRACE_FL_FREE
);
2691 ftrace_free_rec(rec
);
2693 } while_for_each_ftrace_rec();
2694 mutex_unlock(&ftrace_lock
);
2697 static void ftrace_init_module(struct module
*mod
,
2698 unsigned long *start
, unsigned long *end
)
2700 if (ftrace_disabled
|| start
== end
)
2702 ftrace_process_locs(mod
, start
, end
);
2705 static int ftrace_module_notify(struct notifier_block
*self
,
2706 unsigned long val
, void *data
)
2708 struct module
*mod
= data
;
2711 case MODULE_STATE_COMING
:
2712 ftrace_init_module(mod
, mod
->ftrace_callsites
,
2713 mod
->ftrace_callsites
+
2714 mod
->num_ftrace_callsites
);
2716 case MODULE_STATE_GOING
:
2717 ftrace_release_mod(mod
);
2724 static int ftrace_module_notify(struct notifier_block
*self
,
2725 unsigned long val
, void *data
)
2729 #endif /* CONFIG_MODULES */
2731 struct notifier_block ftrace_module_nb
= {
2732 .notifier_call
= ftrace_module_notify
,
2736 extern unsigned long __start_mcount_loc
[];
2737 extern unsigned long __stop_mcount_loc
[];
2739 void __init
ftrace_init(void)
2741 unsigned long count
, addr
, flags
;
2744 /* Keep the ftrace pointer to the stub */
2745 addr
= (unsigned long)ftrace_stub
;
2747 local_irq_save(flags
);
2748 ftrace_dyn_arch_init(&addr
);
2749 local_irq_restore(flags
);
2751 /* ftrace_dyn_arch_init places the return code in addr */
2755 count
= __stop_mcount_loc
- __start_mcount_loc
;
2757 ret
= ftrace_dyn_table_alloc(count
);
2761 last_ftrace_enabled
= ftrace_enabled
= 1;
2763 ret
= ftrace_process_locs(NULL
,
2767 ret
= register_module_notifier(&ftrace_module_nb
);
2769 pr_warning("Failed to register trace ftrace module notifier\n");
2771 set_ftrace_early_filters();
2775 ftrace_disabled
= 1;
2780 static int __init
ftrace_nodyn_init(void)
2785 device_initcall(ftrace_nodyn_init
);
2787 static inline int ftrace_init_dyn_debugfs(struct dentry
*d_tracer
) { return 0; }
2788 static inline void ftrace_startup_enable(int command
) { }
2789 /* Keep as macros so we do not need to define the commands */
2790 # define ftrace_startup(command) do { } while (0)
2791 # define ftrace_shutdown(command) do { } while (0)
2792 # define ftrace_startup_sysctl() do { } while (0)
2793 # define ftrace_shutdown_sysctl() do { } while (0)
2794 #endif /* CONFIG_DYNAMIC_FTRACE */
2796 static void clear_ftrace_swapper(void)
2798 struct task_struct
*p
;
2802 for_each_online_cpu(cpu
) {
2804 clear_tsk_trace_trace(p
);
2809 static void set_ftrace_swapper(void)
2811 struct task_struct
*p
;
2815 for_each_online_cpu(cpu
) {
2817 set_tsk_trace_trace(p
);
2822 static void clear_ftrace_pid(struct pid
*pid
)
2824 struct task_struct
*p
;
2827 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2828 clear_tsk_trace_trace(p
);
2829 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2835 static void set_ftrace_pid(struct pid
*pid
)
2837 struct task_struct
*p
;
2840 do_each_pid_task(pid
, PIDTYPE_PID
, p
) {
2841 set_tsk_trace_trace(p
);
2842 } while_each_pid_task(pid
, PIDTYPE_PID
, p
);
2846 static void clear_ftrace_pid_task(struct pid
*pid
)
2848 if (pid
== ftrace_swapper_pid
)
2849 clear_ftrace_swapper();
2851 clear_ftrace_pid(pid
);
2854 static void set_ftrace_pid_task(struct pid
*pid
)
2856 if (pid
== ftrace_swapper_pid
)
2857 set_ftrace_swapper();
2859 set_ftrace_pid(pid
);
2862 static int ftrace_pid_add(int p
)
2865 struct ftrace_pid
*fpid
;
2868 mutex_lock(&ftrace_lock
);
2871 pid
= ftrace_swapper_pid
;
2873 pid
= find_get_pid(p
);
2880 list_for_each_entry(fpid
, &ftrace_pids
, list
)
2881 if (fpid
->pid
== pid
)
2886 fpid
= kmalloc(sizeof(*fpid
), GFP_KERNEL
);
2890 list_add(&fpid
->list
, &ftrace_pids
);
2893 set_ftrace_pid_task(pid
);
2895 ftrace_update_pid_func();
2896 ftrace_startup_enable(0);
2898 mutex_unlock(&ftrace_lock
);
2902 if (pid
!= ftrace_swapper_pid
)
2906 mutex_unlock(&ftrace_lock
);
2910 static void ftrace_pid_reset(void)
2912 struct ftrace_pid
*fpid
, *safe
;
2914 mutex_lock(&ftrace_lock
);
2915 list_for_each_entry_safe(fpid
, safe
, &ftrace_pids
, list
) {
2916 struct pid
*pid
= fpid
->pid
;
2918 clear_ftrace_pid_task(pid
);
2920 list_del(&fpid
->list
);
2924 ftrace_update_pid_func();
2925 ftrace_startup_enable(0);
2927 mutex_unlock(&ftrace_lock
);
2930 static void *fpid_start(struct seq_file
*m
, loff_t
*pos
)
2932 mutex_lock(&ftrace_lock
);
2934 if (list_empty(&ftrace_pids
) && (!*pos
))
2937 return seq_list_start(&ftrace_pids
, *pos
);
2940 static void *fpid_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2945 return seq_list_next(v
, &ftrace_pids
, pos
);
2948 static void fpid_stop(struct seq_file
*m
, void *p
)
2950 mutex_unlock(&ftrace_lock
);
2953 static int fpid_show(struct seq_file
*m
, void *v
)
2955 const struct ftrace_pid
*fpid
= list_entry(v
, struct ftrace_pid
, list
);
2957 if (v
== (void *)1) {
2958 seq_printf(m
, "no pid\n");
2962 if (fpid
->pid
== ftrace_swapper_pid
)
2963 seq_printf(m
, "swapper tasks\n");
2965 seq_printf(m
, "%u\n", pid_vnr(fpid
->pid
));
2970 static const struct seq_operations ftrace_pid_sops
= {
2971 .start
= fpid_start
,
2978 ftrace_pid_open(struct inode
*inode
, struct file
*file
)
2982 if ((file
->f_mode
& FMODE_WRITE
) &&
2983 (file
->f_flags
& O_TRUNC
))
2986 if (file
->f_mode
& FMODE_READ
)
2987 ret
= seq_open(file
, &ftrace_pid_sops
);
2993 ftrace_pid_write(struct file
*filp
, const char __user
*ubuf
,
2994 size_t cnt
, loff_t
*ppos
)
3000 if (cnt
>= sizeof(buf
))
3003 if (copy_from_user(&buf
, ubuf
, cnt
))
3009 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3010 * to clean the filter quietly.
3012 tmp
= strstrip(buf
);
3013 if (strlen(tmp
) == 0)
3016 ret
= strict_strtol(tmp
, 10, &val
);
3020 ret
= ftrace_pid_add(val
);
3022 return ret
? ret
: cnt
;
3026 ftrace_pid_release(struct inode
*inode
, struct file
*file
)
3028 if (file
->f_mode
& FMODE_READ
)
3029 seq_release(inode
, file
);
3034 static const struct file_operations ftrace_pid_fops
= {
3035 .open
= ftrace_pid_open
,
3036 .write
= ftrace_pid_write
,
3038 .llseek
= seq_lseek
,
3039 .release
= ftrace_pid_release
,
3042 static __init
int ftrace_init_debugfs(void)
3044 struct dentry
*d_tracer
;
3046 d_tracer
= tracing_init_dentry();
3050 ftrace_init_dyn_debugfs(d_tracer
);
3052 trace_create_file("set_ftrace_pid", 0644, d_tracer
,
3053 NULL
, &ftrace_pid_fops
);
3055 ftrace_profile_debugfs(d_tracer
);
3059 fs_initcall(ftrace_init_debugfs
);
3062 * ftrace_kill - kill ftrace
3064 * This function should be used by panic code. It stops ftrace
3065 * but in a not so nice way. If you need to simply kill ftrace
3066 * from a non-atomic section, use ftrace_kill.
3068 void ftrace_kill(void)
3070 ftrace_disabled
= 1;
3072 clear_ftrace_function();
3076 * register_ftrace_function - register a function for profiling
3077 * @ops - ops structure that holds the function for profiling.
3079 * Register a function to be called by all functions in the
3082 * Note: @ops->func and all the functions it calls must be labeled
3083 * with "notrace", otherwise it will go into a
3086 int register_ftrace_function(struct ftrace_ops
*ops
)
3090 if (unlikely(ftrace_disabled
))
3093 mutex_lock(&ftrace_lock
);
3095 ret
= __register_ftrace_function(ops
);
3098 mutex_unlock(&ftrace_lock
);
3103 * unregister_ftrace_function - unregister a function for profiling.
3104 * @ops - ops structure that holds the function to unregister
3106 * Unregister a function that was added to be called by ftrace profiling.
3108 int unregister_ftrace_function(struct ftrace_ops
*ops
)
3112 mutex_lock(&ftrace_lock
);
3113 ret
= __unregister_ftrace_function(ops
);
3115 mutex_unlock(&ftrace_lock
);
3121 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
3122 void __user
*buffer
, size_t *lenp
,
3127 if (unlikely(ftrace_disabled
))
3130 mutex_lock(&ftrace_lock
);
3132 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3134 if (ret
|| !write
|| (last_ftrace_enabled
== !!ftrace_enabled
))
3137 last_ftrace_enabled
= !!ftrace_enabled
;
3139 if (ftrace_enabled
) {
3141 ftrace_startup_sysctl();
3143 /* we are starting ftrace again */
3144 if (ftrace_list
!= &ftrace_list_end
) {
3145 if (ftrace_list
->next
== &ftrace_list_end
)
3146 ftrace_trace_function
= ftrace_list
->func
;
3148 ftrace_trace_function
= ftrace_list_func
;
3152 /* stopping ftrace calls (just send to ftrace_stub) */
3153 ftrace_trace_function
= ftrace_stub
;
3155 ftrace_shutdown_sysctl();
3159 mutex_unlock(&ftrace_lock
);
3163 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3165 static int ftrace_graph_active
;
3166 static struct notifier_block ftrace_suspend_notifier
;
3168 int ftrace_graph_entry_stub(struct ftrace_graph_ent
*trace
)
3173 /* The callbacks that hook a function */
3174 trace_func_graph_ret_t ftrace_graph_return
=
3175 (trace_func_graph_ret_t
)ftrace_stub
;
3176 trace_func_graph_ent_t ftrace_graph_entry
= ftrace_graph_entry_stub
;
3178 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3179 static int alloc_retstack_tasklist(struct ftrace_ret_stack
**ret_stack_list
)
3183 unsigned long flags
;
3184 int start
= 0, end
= FTRACE_RETSTACK_ALLOC_SIZE
;
3185 struct task_struct
*g
, *t
;
3187 for (i
= 0; i
< FTRACE_RETSTACK_ALLOC_SIZE
; i
++) {
3188 ret_stack_list
[i
] = kmalloc(FTRACE_RETFUNC_DEPTH
3189 * sizeof(struct ftrace_ret_stack
),
3191 if (!ret_stack_list
[i
]) {
3199 read_lock_irqsave(&tasklist_lock
, flags
);
3200 do_each_thread(g
, t
) {
3206 if (t
->ret_stack
== NULL
) {
3207 atomic_set(&t
->tracing_graph_pause
, 0);
3208 atomic_set(&t
->trace_overrun
, 0);
3209 t
->curr_ret_stack
= -1;
3210 /* Make sure the tasks see the -1 first: */
3212 t
->ret_stack
= ret_stack_list
[start
++];
3214 } while_each_thread(g
, t
);
3217 read_unlock_irqrestore(&tasklist_lock
, flags
);
3219 for (i
= start
; i
< end
; i
++)
3220 kfree(ret_stack_list
[i
]);
3225 ftrace_graph_probe_sched_switch(struct rq
*__rq
, struct task_struct
*prev
,
3226 struct task_struct
*next
)
3228 unsigned long long timestamp
;
3232 * Does the user want to count the time a function was asleep.
3233 * If so, do not update the time stamps.
3235 if (trace_flags
& TRACE_ITER_SLEEP_TIME
)
3238 timestamp
= trace_clock_local();
3240 prev
->ftrace_timestamp
= timestamp
;
3242 /* only process tasks that we timestamped */
3243 if (!next
->ftrace_timestamp
)
3247 * Update all the counters in next to make up for the
3248 * time next was sleeping.
3250 timestamp
-= next
->ftrace_timestamp
;
3252 for (index
= next
->curr_ret_stack
; index
>= 0; index
--)
3253 next
->ret_stack
[index
].calltime
+= timestamp
;
3256 /* Allocate a return stack for each task */
3257 static int start_graph_tracing(void)
3259 struct ftrace_ret_stack
**ret_stack_list
;
3262 ret_stack_list
= kmalloc(FTRACE_RETSTACK_ALLOC_SIZE
*
3263 sizeof(struct ftrace_ret_stack
*),
3266 if (!ret_stack_list
)
3269 /* The cpu_boot init_task->ret_stack will never be freed */
3270 for_each_online_cpu(cpu
) {
3271 if (!idle_task(cpu
)->ret_stack
)
3272 ftrace_graph_init_idle_task(idle_task(cpu
), cpu
);
3276 ret
= alloc_retstack_tasklist(ret_stack_list
);
3277 } while (ret
== -EAGAIN
);
3280 ret
= register_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3282 pr_info("ftrace_graph: Couldn't activate tracepoint"
3283 " probe to kernel_sched_switch\n");
3286 kfree(ret_stack_list
);
3291 * Hibernation protection.
3292 * The state of the current task is too much unstable during
3293 * suspend/restore to disk. We want to protect against that.
3296 ftrace_suspend_notifier_call(struct notifier_block
*bl
, unsigned long state
,
3300 case PM_HIBERNATION_PREPARE
:
3301 pause_graph_tracing();
3304 case PM_POST_HIBERNATION
:
3305 unpause_graph_tracing();
3311 int register_ftrace_graph(trace_func_graph_ret_t retfunc
,
3312 trace_func_graph_ent_t entryfunc
)
3316 mutex_lock(&ftrace_lock
);
3318 /* we currently allow only one tracer registered at a time */
3319 if (ftrace_graph_active
) {
3324 ftrace_suspend_notifier
.notifier_call
= ftrace_suspend_notifier_call
;
3325 register_pm_notifier(&ftrace_suspend_notifier
);
3327 ftrace_graph_active
++;
3328 ret
= start_graph_tracing();
3330 ftrace_graph_active
--;
3334 ftrace_graph_return
= retfunc
;
3335 ftrace_graph_entry
= entryfunc
;
3337 ftrace_startup(FTRACE_START_FUNC_RET
);
3340 mutex_unlock(&ftrace_lock
);
3344 void unregister_ftrace_graph(void)
3346 mutex_lock(&ftrace_lock
);
3348 if (unlikely(!ftrace_graph_active
))
3351 ftrace_graph_active
--;
3352 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch
);
3353 ftrace_graph_return
= (trace_func_graph_ret_t
)ftrace_stub
;
3354 ftrace_graph_entry
= ftrace_graph_entry_stub
;
3355 ftrace_shutdown(FTRACE_STOP_FUNC_RET
);
3356 unregister_pm_notifier(&ftrace_suspend_notifier
);
3359 mutex_unlock(&ftrace_lock
);
3362 static DEFINE_PER_CPU(struct ftrace_ret_stack
*, idle_ret_stack
);
3365 graph_init_task(struct task_struct
*t
, struct ftrace_ret_stack
*ret_stack
)
3367 atomic_set(&t
->tracing_graph_pause
, 0);
3368 atomic_set(&t
->trace_overrun
, 0);
3369 t
->ftrace_timestamp
= 0;
3370 /* make curr_ret_stack visable before we add the ret_stack */
3372 t
->ret_stack
= ret_stack
;
3376 * Allocate a return stack for the idle task. May be the first
3377 * time through, or it may be done by CPU hotplug online.
3379 void ftrace_graph_init_idle_task(struct task_struct
*t
, int cpu
)
3381 t
->curr_ret_stack
= -1;
3383 * The idle task has no parent, it either has its own
3384 * stack or no stack at all.
3387 WARN_ON(t
->ret_stack
!= per_cpu(idle_ret_stack
, cpu
));
3389 if (ftrace_graph_active
) {
3390 struct ftrace_ret_stack
*ret_stack
;
3392 ret_stack
= per_cpu(idle_ret_stack
, cpu
);
3394 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3395 * sizeof(struct ftrace_ret_stack
),
3399 per_cpu(idle_ret_stack
, cpu
) = ret_stack
;
3401 graph_init_task(t
, ret_stack
);
3405 /* Allocate a return stack for newly created task */
3406 void ftrace_graph_init_task(struct task_struct
*t
)
3408 /* Make sure we do not use the parent ret_stack */
3409 t
->ret_stack
= NULL
;
3410 t
->curr_ret_stack
= -1;
3412 if (ftrace_graph_active
) {
3413 struct ftrace_ret_stack
*ret_stack
;
3415 ret_stack
= kmalloc(FTRACE_RETFUNC_DEPTH
3416 * sizeof(struct ftrace_ret_stack
),
3420 graph_init_task(t
, ret_stack
);
3424 void ftrace_graph_exit_task(struct task_struct
*t
)
3426 struct ftrace_ret_stack
*ret_stack
= t
->ret_stack
;
3428 t
->ret_stack
= NULL
;
3429 /* NULL must become visible to IRQs before we free it: */
3435 void ftrace_graph_stop(void)