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/debugfs.h>
21 #include <linux/hardirq.h>
22 #include <linux/kthread.h>
23 #include <linux/uaccess.h>
24 #include <linux/kprobes.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/ctype.h>
28 #include <linux/list.h>
30 #include <asm/ftrace.h>
34 #define FTRACE_WARN_ON(cond) \
40 #define FTRACE_WARN_ON_ONCE(cond) \
42 if (WARN_ON_ONCE(cond)) \
46 /* ftrace_enabled is a method to turn ftrace on or off */
47 int ftrace_enabled __read_mostly
;
48 static int last_ftrace_enabled
;
50 /* Quick disabling of function tracer. */
51 int function_trace_stop
;
54 * ftrace_disabled is set when an anomaly is discovered.
55 * ftrace_disabled is much stronger than ftrace_enabled.
57 static int ftrace_disabled __read_mostly
;
59 static DEFINE_SPINLOCK(ftrace_lock
);
60 static DEFINE_MUTEX(ftrace_sysctl_lock
);
62 static struct ftrace_ops ftrace_list_end __read_mostly
=
67 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
68 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
69 ftrace_func_t __ftrace_trace_function __read_mostly
= ftrace_stub
;
71 static void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
73 struct ftrace_ops
*op
= ftrace_list
;
75 /* in case someone actually ports this to alpha! */
76 read_barrier_depends();
78 while (op
!= &ftrace_list_end
) {
80 read_barrier_depends();
81 op
->func(ip
, parent_ip
);
87 * clear_ftrace_function - reset the ftrace function
89 * This NULLs the ftrace function and in essence stops
90 * tracing. There may be lag
92 void clear_ftrace_function(void)
94 ftrace_trace_function
= ftrace_stub
;
95 __ftrace_trace_function
= ftrace_stub
;
98 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
100 * For those archs that do not test ftrace_trace_stop in their
101 * mcount call site, we need to do it from C.
103 static void ftrace_test_stop_func(unsigned long ip
, unsigned long parent_ip
)
105 if (function_trace_stop
)
108 __ftrace_trace_function(ip
, parent_ip
);
112 static int __register_ftrace_function(struct ftrace_ops
*ops
)
114 /* should not be called from interrupt context */
115 spin_lock(&ftrace_lock
);
117 ops
->next
= ftrace_list
;
119 * We are entering ops into the ftrace_list but another
120 * CPU might be walking that list. We need to make sure
121 * the ops->next pointer is valid before another CPU sees
122 * the ops pointer included into the ftrace_list.
127 if (ftrace_enabled
) {
129 * For one func, simply call it directly.
130 * For more than one func, call the chain.
132 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
133 if (ops
->next
== &ftrace_list_end
)
134 ftrace_trace_function
= ops
->func
;
136 ftrace_trace_function
= ftrace_list_func
;
138 if (ops
->next
== &ftrace_list_end
)
139 __ftrace_trace_function
= ops
->func
;
141 __ftrace_trace_function
= ftrace_list_func
;
142 ftrace_trace_function
= ftrace_test_stop_func
;
146 spin_unlock(&ftrace_lock
);
151 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
153 struct ftrace_ops
**p
;
156 /* should not be called from interrupt context */
157 spin_lock(&ftrace_lock
);
160 * If we are removing the last function, then simply point
161 * to the ftrace_stub.
163 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
164 ftrace_trace_function
= ftrace_stub
;
165 ftrace_list
= &ftrace_list_end
;
169 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
180 if (ftrace_enabled
) {
181 /* If we only have one func left, then call that directly */
182 if (ftrace_list
== &ftrace_list_end
||
183 ftrace_list
->next
== &ftrace_list_end
)
184 ftrace_trace_function
= ftrace_list
->func
;
188 spin_unlock(&ftrace_lock
);
193 #ifdef CONFIG_DYNAMIC_FTRACE
194 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
195 # error Dynamic ftrace depends on MCOUNT_RECORD
199 * Since MCOUNT_ADDR may point to mcount itself, we do not want
200 * to get it confused by reading a reference in the code as we
201 * are parsing on objcopy output of text. Use a variable for
204 static unsigned long mcount_addr
= MCOUNT_ADDR
;
207 FTRACE_ENABLE_CALLS
= (1 << 0),
208 FTRACE_DISABLE_CALLS
= (1 << 1),
209 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
210 FTRACE_ENABLE_MCOUNT
= (1 << 3),
211 FTRACE_DISABLE_MCOUNT
= (1 << 4),
214 static int ftrace_filtered
;
215 static int tracing_on
;
217 static LIST_HEAD(ftrace_new_addrs
);
219 static DEFINE_MUTEX(ftrace_regex_lock
);
222 struct ftrace_page
*next
;
224 struct dyn_ftrace records
[];
227 #define ENTRIES_PER_PAGE \
228 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
230 /* estimate from running different kernels */
231 #define NR_TO_INIT 10000
233 static struct ftrace_page
*ftrace_pages_start
;
234 static struct ftrace_page
*ftrace_pages
;
236 static struct dyn_ftrace
*ftrace_free_records
;
239 #ifdef CONFIG_KPROBES
241 static int frozen_record_count
;
243 static inline void freeze_record(struct dyn_ftrace
*rec
)
245 if (!(rec
->flags
& FTRACE_FL_FROZEN
)) {
246 rec
->flags
|= FTRACE_FL_FROZEN
;
247 frozen_record_count
++;
251 static inline void unfreeze_record(struct dyn_ftrace
*rec
)
253 if (rec
->flags
& FTRACE_FL_FROZEN
) {
254 rec
->flags
&= ~FTRACE_FL_FROZEN
;
255 frozen_record_count
--;
259 static inline int record_frozen(struct dyn_ftrace
*rec
)
261 return rec
->flags
& FTRACE_FL_FROZEN
;
264 # define freeze_record(rec) ({ 0; })
265 # define unfreeze_record(rec) ({ 0; })
266 # define record_frozen(rec) ({ 0; })
267 #endif /* CONFIG_KPROBES */
269 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
271 rec
->ip
= (unsigned long)ftrace_free_records
;
272 ftrace_free_records
= rec
;
273 rec
->flags
|= FTRACE_FL_FREE
;
276 void ftrace_release(void *start
, unsigned long size
)
278 struct dyn_ftrace
*rec
;
279 struct ftrace_page
*pg
;
280 unsigned long s
= (unsigned long)start
;
281 unsigned long e
= s
+ size
;
284 if (ftrace_disabled
|| !start
)
287 /* should not be called from interrupt context */
288 spin_lock(&ftrace_lock
);
290 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
291 for (i
= 0; i
< pg
->index
; i
++) {
292 rec
= &pg
->records
[i
];
294 if ((rec
->ip
>= s
) && (rec
->ip
< e
))
295 ftrace_free_rec(rec
);
298 spin_unlock(&ftrace_lock
);
301 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
303 struct dyn_ftrace
*rec
;
305 /* First check for freed records */
306 if (ftrace_free_records
) {
307 rec
= ftrace_free_records
;
309 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
310 FTRACE_WARN_ON_ONCE(1);
311 ftrace_free_records
= NULL
;
315 ftrace_free_records
= (void *)rec
->ip
;
316 memset(rec
, 0, sizeof(*rec
));
320 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
321 if (!ftrace_pages
->next
) {
322 /* allocate another page */
324 (void *)get_zeroed_page(GFP_KERNEL
);
325 if (!ftrace_pages
->next
)
328 ftrace_pages
= ftrace_pages
->next
;
331 return &ftrace_pages
->records
[ftrace_pages
->index
++];
334 static struct dyn_ftrace
*
335 ftrace_record_ip(unsigned long ip
)
337 struct dyn_ftrace
*rec
;
339 if (!ftrace_enabled
|| ftrace_disabled
)
342 rec
= ftrace_alloc_dyn_node(ip
);
348 list_add(&rec
->list
, &ftrace_new_addrs
);
353 #define FTRACE_ADDR ((long)(ftrace_caller))
356 __ftrace_replace_code(struct dyn_ftrace
*rec
,
357 unsigned char *old
, unsigned char *new, int enable
)
359 unsigned long ip
, fl
;
363 if (ftrace_filtered
&& enable
) {
365 * If filtering is on:
367 * If this record is set to be filtered and
368 * is enabled then do nothing.
370 * If this record is set to be filtered and
371 * it is not enabled, enable it.
373 * If this record is not set to be filtered
374 * and it is not enabled do nothing.
376 * If this record is set not to trace then
379 * If this record is set not to trace and
380 * it is enabled then disable it.
382 * If this record is not set to be filtered and
383 * it is enabled, disable it.
386 fl
= rec
->flags
& (FTRACE_FL_FILTER
| FTRACE_FL_NOTRACE
|
389 if ((fl
== (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
)) ||
390 (fl
== (FTRACE_FL_FILTER
| FTRACE_FL_NOTRACE
)) ||
391 !fl
|| (fl
== FTRACE_FL_NOTRACE
))
395 * If it is enabled disable it,
396 * otherwise enable it!
398 if (fl
& FTRACE_FL_ENABLED
) {
399 /* swap new and old */
401 old
= ftrace_call_replace(ip
, FTRACE_ADDR
);
402 rec
->flags
&= ~FTRACE_FL_ENABLED
;
404 new = ftrace_call_replace(ip
, FTRACE_ADDR
);
405 rec
->flags
|= FTRACE_FL_ENABLED
;
411 * If this record is set not to trace and is
412 * not enabled, do nothing.
414 fl
= rec
->flags
& (FTRACE_FL_NOTRACE
| FTRACE_FL_ENABLED
);
415 if (fl
== FTRACE_FL_NOTRACE
)
418 new = ftrace_call_replace(ip
, FTRACE_ADDR
);
420 old
= ftrace_call_replace(ip
, FTRACE_ADDR
);
423 if (rec
->flags
& FTRACE_FL_ENABLED
)
425 rec
->flags
|= FTRACE_FL_ENABLED
;
427 if (!(rec
->flags
& FTRACE_FL_ENABLED
))
429 rec
->flags
&= ~FTRACE_FL_ENABLED
;
433 return ftrace_modify_code(ip
, old
, new);
436 static void ftrace_replace_code(int enable
)
439 unsigned char *new = NULL
, *old
= NULL
;
440 struct dyn_ftrace
*rec
;
441 struct ftrace_page
*pg
;
444 old
= ftrace_nop_replace();
446 new = ftrace_nop_replace();
448 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
449 for (i
= 0; i
< pg
->index
; i
++) {
450 rec
= &pg
->records
[i
];
452 /* don't modify code that has already faulted */
453 if (rec
->flags
& FTRACE_FL_FAILED
)
456 /* ignore updates to this record's mcount site */
457 if (get_kprobe((void *)rec
->ip
)) {
461 unfreeze_record(rec
);
464 failed
= __ftrace_replace_code(rec
, old
, new, enable
);
465 if (failed
&& (rec
->flags
& FTRACE_FL_CONVERTED
)) {
466 rec
->flags
|= FTRACE_FL_FAILED
;
467 if ((system_state
== SYSTEM_BOOTING
) ||
468 !core_kernel_text(rec
->ip
)) {
469 ftrace_free_rec(rec
);
476 static void print_ip_ins(const char *fmt
, unsigned char *p
)
480 printk(KERN_CONT
"%s", fmt
);
482 for (i
= 0; i
< MCOUNT_INSN_SIZE
; i
++)
483 printk(KERN_CONT
"%s%02x", i
? ":" : "", p
[i
]);
487 ftrace_code_disable(struct dyn_ftrace
*rec
)
490 unsigned char *nop
, *call
;
495 nop
= ftrace_nop_replace();
496 call
= ftrace_call_replace(ip
, mcount_addr
);
498 ret
= ftrace_modify_code(ip
, call
, nop
);
502 FTRACE_WARN_ON_ONCE(1);
503 pr_info("ftrace faulted on modifying ");
507 FTRACE_WARN_ON_ONCE(1);
508 pr_info("ftrace failed to modify ");
510 print_ip_ins(" expected: ", call
);
511 print_ip_ins(" actual: ", (unsigned char *)ip
);
512 print_ip_ins(" replace: ", nop
);
513 printk(KERN_CONT
"\n");
516 FTRACE_WARN_ON_ONCE(1);
517 pr_info("ftrace faulted on writing ");
521 FTRACE_WARN_ON_ONCE(1);
522 pr_info("ftrace faulted on unknown error ");
526 rec
->flags
|= FTRACE_FL_FAILED
;
532 static int __ftrace_modify_code(void *data
)
536 if (*command
& FTRACE_ENABLE_CALLS
) {
537 ftrace_replace_code(1);
539 } else if (*command
& FTRACE_DISABLE_CALLS
) {
540 ftrace_replace_code(0);
544 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
545 ftrace_update_ftrace_func(ftrace_trace_function
);
550 static void ftrace_run_update_code(int command
)
552 stop_machine(__ftrace_modify_code
, &command
, NULL
);
555 static ftrace_func_t saved_ftrace_func
;
556 static int ftrace_start_up
;
557 static DEFINE_MUTEX(ftrace_start_lock
);
559 static void ftrace_startup(void)
563 if (unlikely(ftrace_disabled
))
566 mutex_lock(&ftrace_start_lock
);
568 if (ftrace_start_up
== 1)
569 command
|= FTRACE_ENABLE_CALLS
;
571 if (saved_ftrace_func
!= ftrace_trace_function
) {
572 saved_ftrace_func
= ftrace_trace_function
;
573 command
|= FTRACE_UPDATE_TRACE_FUNC
;
576 if (!command
|| !ftrace_enabled
)
579 ftrace_run_update_code(command
);
581 mutex_unlock(&ftrace_start_lock
);
584 static void ftrace_shutdown(void)
588 if (unlikely(ftrace_disabled
))
591 mutex_lock(&ftrace_start_lock
);
593 if (!ftrace_start_up
)
594 command
|= FTRACE_DISABLE_CALLS
;
596 if (saved_ftrace_func
!= ftrace_trace_function
) {
597 saved_ftrace_func
= ftrace_trace_function
;
598 command
|= FTRACE_UPDATE_TRACE_FUNC
;
601 if (!command
|| !ftrace_enabled
)
604 ftrace_run_update_code(command
);
606 mutex_unlock(&ftrace_start_lock
);
609 static void ftrace_startup_sysctl(void)
611 int command
= FTRACE_ENABLE_MCOUNT
;
613 if (unlikely(ftrace_disabled
))
616 mutex_lock(&ftrace_start_lock
);
617 /* Force update next time */
618 saved_ftrace_func
= NULL
;
619 /* ftrace_start_up is true if we want ftrace running */
621 command
|= FTRACE_ENABLE_CALLS
;
623 ftrace_run_update_code(command
);
624 mutex_unlock(&ftrace_start_lock
);
627 static void ftrace_shutdown_sysctl(void)
629 int command
= FTRACE_DISABLE_MCOUNT
;
631 if (unlikely(ftrace_disabled
))
634 mutex_lock(&ftrace_start_lock
);
635 /* ftrace_start_up is true if ftrace is running */
637 command
|= FTRACE_DISABLE_CALLS
;
639 ftrace_run_update_code(command
);
640 mutex_unlock(&ftrace_start_lock
);
643 static cycle_t ftrace_update_time
;
644 static unsigned long ftrace_update_cnt
;
645 unsigned long ftrace_update_tot_cnt
;
647 static int ftrace_update_code(void)
649 struct dyn_ftrace
*p
, *t
;
652 start
= ftrace_now(raw_smp_processor_id());
653 ftrace_update_cnt
= 0;
655 list_for_each_entry_safe(p
, t
, &ftrace_new_addrs
, list
) {
657 /* If something went wrong, bail without enabling anything */
658 if (unlikely(ftrace_disabled
))
661 list_del_init(&p
->list
);
663 /* convert record (i.e, patch mcount-call with NOP) */
664 if (ftrace_code_disable(p
)) {
665 p
->flags
|= FTRACE_FL_CONVERTED
;
671 stop
= ftrace_now(raw_smp_processor_id());
672 ftrace_update_time
= stop
- start
;
673 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
678 static int __init
ftrace_dyn_table_alloc(unsigned long num_to_init
)
680 struct ftrace_page
*pg
;
684 /* allocate a few pages */
685 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
686 if (!ftrace_pages_start
)
690 * Allocate a few more pages.
692 * TODO: have some parser search vmlinux before
693 * final linking to find all calls to ftrace.
695 * a) know how many pages to allocate.
697 * b) set up the table then.
699 * The dynamic code is still necessary for
703 pg
= ftrace_pages
= ftrace_pages_start
;
705 cnt
= num_to_init
/ ENTRIES_PER_PAGE
;
706 pr_info("ftrace: allocating %ld entries in %d pages\n",
709 for (i
= 0; i
< cnt
; i
++) {
710 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
712 /* If we fail, we'll try later anyway */
723 FTRACE_ITER_FILTER
= (1 << 0),
724 FTRACE_ITER_CONT
= (1 << 1),
725 FTRACE_ITER_NOTRACE
= (1 << 2),
726 FTRACE_ITER_FAILURES
= (1 << 3),
729 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
731 struct ftrace_iterator
{
733 struct ftrace_page
*pg
;
736 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
742 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
744 struct ftrace_iterator
*iter
= m
->private;
745 struct dyn_ftrace
*rec
= NULL
;
749 /* should not be called from interrupt context */
750 spin_lock(&ftrace_lock
);
752 if (iter
->idx
>= iter
->pg
->index
) {
753 if (iter
->pg
->next
) {
754 iter
->pg
= iter
->pg
->next
;
759 rec
= &iter
->pg
->records
[iter
->idx
++];
760 if ((rec
->flags
& FTRACE_FL_FREE
) ||
762 (!(iter
->flags
& FTRACE_ITER_FAILURES
) &&
763 (rec
->flags
& FTRACE_FL_FAILED
)) ||
765 ((iter
->flags
& FTRACE_ITER_FAILURES
) &&
766 !(rec
->flags
& FTRACE_FL_FAILED
)) ||
768 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
769 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
774 spin_unlock(&ftrace_lock
);
781 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
783 struct ftrace_iterator
*iter
= m
->private;
787 if (*pos
!= iter
->pos
) {
788 for (p
= t_next(m
, p
, &l
); p
&& l
< *pos
; p
= t_next(m
, p
, &l
))
792 p
= t_next(m
, p
, &l
);
798 static void t_stop(struct seq_file
*m
, void *p
)
802 static int t_show(struct seq_file
*m
, void *v
)
804 struct dyn_ftrace
*rec
= v
;
805 char str
[KSYM_SYMBOL_LEN
];
810 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
812 seq_printf(m
, "%s\n", str
);
817 static struct seq_operations show_ftrace_seq_ops
= {
825 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
827 struct ftrace_iterator
*iter
;
830 if (unlikely(ftrace_disabled
))
833 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
837 iter
->pg
= ftrace_pages_start
;
840 ret
= seq_open(file
, &show_ftrace_seq_ops
);
842 struct seq_file
*m
= file
->private_data
;
852 int ftrace_avail_release(struct inode
*inode
, struct file
*file
)
854 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
855 struct ftrace_iterator
*iter
= m
->private;
857 seq_release(inode
, file
);
864 ftrace_failures_open(struct inode
*inode
, struct file
*file
)
868 struct ftrace_iterator
*iter
;
870 ret
= ftrace_avail_open(inode
, file
);
872 m
= (struct seq_file
*)file
->private_data
;
873 iter
= (struct ftrace_iterator
*)m
->private;
874 iter
->flags
= FTRACE_ITER_FAILURES
;
881 static void ftrace_filter_reset(int enable
)
883 struct ftrace_page
*pg
;
884 struct dyn_ftrace
*rec
;
885 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
888 /* should not be called from interrupt context */
889 spin_lock(&ftrace_lock
);
892 pg
= ftrace_pages_start
;
894 for (i
= 0; i
< pg
->index
; i
++) {
895 rec
= &pg
->records
[i
];
896 if (rec
->flags
& FTRACE_FL_FAILED
)
902 spin_unlock(&ftrace_lock
);
906 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
908 struct ftrace_iterator
*iter
;
911 if (unlikely(ftrace_disabled
))
914 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
918 mutex_lock(&ftrace_regex_lock
);
919 if ((file
->f_mode
& FMODE_WRITE
) &&
920 !(file
->f_flags
& O_APPEND
))
921 ftrace_filter_reset(enable
);
923 if (file
->f_mode
& FMODE_READ
) {
924 iter
->pg
= ftrace_pages_start
;
926 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
929 ret
= seq_open(file
, &show_ftrace_seq_ops
);
931 struct seq_file
*m
= file
->private_data
;
936 file
->private_data
= iter
;
937 mutex_unlock(&ftrace_regex_lock
);
943 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
945 return ftrace_regex_open(inode
, file
, 1);
949 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
951 return ftrace_regex_open(inode
, file
, 0);
955 ftrace_regex_read(struct file
*file
, char __user
*ubuf
,
956 size_t cnt
, loff_t
*ppos
)
958 if (file
->f_mode
& FMODE_READ
)
959 return seq_read(file
, ubuf
, cnt
, ppos
);
965 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
969 if (file
->f_mode
& FMODE_READ
)
970 ret
= seq_lseek(file
, offset
, origin
);
972 file
->f_pos
= ret
= 1;
985 ftrace_match(unsigned char *buff
, int len
, int enable
)
987 char str
[KSYM_SYMBOL_LEN
];
989 struct ftrace_page
*pg
;
990 struct dyn_ftrace
*rec
;
991 int type
= MATCH_FULL
;
992 unsigned long flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
993 unsigned i
, match
= 0, search_len
= 0;
995 for (i
= 0; i
< len
; i
++) {
996 if (buff
[i
] == '*') {
998 search
= buff
+ i
+ 1;
999 type
= MATCH_END_ONLY
;
1000 search_len
= len
- (i
+ 1);
1002 if (type
== MATCH_END_ONLY
) {
1003 type
= MATCH_MIDDLE_ONLY
;
1006 type
= MATCH_FRONT_ONLY
;
1014 /* should not be called from interrupt context */
1015 spin_lock(&ftrace_lock
);
1017 ftrace_filtered
= 1;
1018 pg
= ftrace_pages_start
;
1020 for (i
= 0; i
< pg
->index
; i
++) {
1024 rec
= &pg
->records
[i
];
1025 if (rec
->flags
& FTRACE_FL_FAILED
)
1027 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1030 if (strcmp(str
, buff
) == 0)
1033 case MATCH_FRONT_ONLY
:
1034 if (memcmp(str
, buff
, match
) == 0)
1037 case MATCH_MIDDLE_ONLY
:
1038 if (strstr(str
, search
))
1041 case MATCH_END_ONLY
:
1042 ptr
= strstr(str
, search
);
1043 if (ptr
&& (ptr
[search_len
] == 0))
1052 spin_unlock(&ftrace_lock
);
1056 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
1057 size_t cnt
, loff_t
*ppos
, int enable
)
1059 struct ftrace_iterator
*iter
;
1064 if (!cnt
|| cnt
< 0)
1067 mutex_lock(&ftrace_regex_lock
);
1069 if (file
->f_mode
& FMODE_READ
) {
1070 struct seq_file
*m
= file
->private_data
;
1073 iter
= file
->private_data
;
1076 iter
->flags
&= ~FTRACE_ITER_CONT
;
1077 iter
->buffer_idx
= 0;
1080 ret
= get_user(ch
, ubuf
++);
1086 if (!(iter
->flags
& ~FTRACE_ITER_CONT
)) {
1087 /* skip white space */
1088 while (cnt
&& isspace(ch
)) {
1089 ret
= get_user(ch
, ubuf
++);
1097 file
->f_pos
+= read
;
1102 iter
->buffer_idx
= 0;
1105 while (cnt
&& !isspace(ch
)) {
1106 if (iter
->buffer_idx
< FTRACE_BUFF_MAX
)
1107 iter
->buffer
[iter
->buffer_idx
++] = ch
;
1112 ret
= get_user(ch
, ubuf
++);
1121 iter
->buffer
[iter
->buffer_idx
] = 0;
1122 ftrace_match(iter
->buffer
, iter
->buffer_idx
, enable
);
1123 iter
->buffer_idx
= 0;
1125 iter
->flags
|= FTRACE_ITER_CONT
;
1128 file
->f_pos
+= read
;
1132 mutex_unlock(&ftrace_regex_lock
);
1138 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
1139 size_t cnt
, loff_t
*ppos
)
1141 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
1145 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
1146 size_t cnt
, loff_t
*ppos
)
1148 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
1152 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
1154 if (unlikely(ftrace_disabled
))
1157 mutex_lock(&ftrace_regex_lock
);
1159 ftrace_filter_reset(enable
);
1161 ftrace_match(buf
, len
, enable
);
1162 mutex_unlock(&ftrace_regex_lock
);
1166 * ftrace_set_filter - set a function to filter on in ftrace
1167 * @buf - the string that holds the function filter text.
1168 * @len - the length of the string.
1169 * @reset - non zero to reset all filters before applying this filter.
1171 * Filters denote which functions should be enabled when tracing is enabled.
1172 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1174 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
1176 ftrace_set_regex(buf
, len
, reset
, 1);
1180 * ftrace_set_notrace - set a function to not trace in ftrace
1181 * @buf - the string that holds the function notrace text.
1182 * @len - the length of the string.
1183 * @reset - non zero to reset all filters before applying this filter.
1185 * Notrace Filters denote which functions should not be enabled when tracing
1186 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1189 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
1191 ftrace_set_regex(buf
, len
, reset
, 0);
1195 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
1197 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1198 struct ftrace_iterator
*iter
;
1200 mutex_lock(&ftrace_regex_lock
);
1201 if (file
->f_mode
& FMODE_READ
) {
1204 seq_release(inode
, file
);
1206 iter
= file
->private_data
;
1208 if (iter
->buffer_idx
) {
1210 iter
->buffer
[iter
->buffer_idx
] = 0;
1211 ftrace_match(iter
->buffer
, iter
->buffer_idx
, enable
);
1214 mutex_lock(&ftrace_sysctl_lock
);
1215 mutex_lock(&ftrace_start_lock
);
1216 if (iter
->filtered
&& ftrace_start_up
&& ftrace_enabled
)
1217 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
1218 mutex_unlock(&ftrace_start_lock
);
1219 mutex_unlock(&ftrace_sysctl_lock
);
1222 mutex_unlock(&ftrace_regex_lock
);
1227 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
1229 return ftrace_regex_release(inode
, file
, 1);
1233 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
1235 return ftrace_regex_release(inode
, file
, 0);
1238 static struct file_operations ftrace_avail_fops
= {
1239 .open
= ftrace_avail_open
,
1241 .llseek
= seq_lseek
,
1242 .release
= ftrace_avail_release
,
1245 static struct file_operations ftrace_failures_fops
= {
1246 .open
= ftrace_failures_open
,
1248 .llseek
= seq_lseek
,
1249 .release
= ftrace_avail_release
,
1252 static struct file_operations ftrace_filter_fops
= {
1253 .open
= ftrace_filter_open
,
1254 .read
= ftrace_regex_read
,
1255 .write
= ftrace_filter_write
,
1256 .llseek
= ftrace_regex_lseek
,
1257 .release
= ftrace_filter_release
,
1260 static struct file_operations ftrace_notrace_fops
= {
1261 .open
= ftrace_notrace_open
,
1262 .read
= ftrace_regex_read
,
1263 .write
= ftrace_notrace_write
,
1264 .llseek
= ftrace_regex_lseek
,
1265 .release
= ftrace_notrace_release
,
1268 static __init
int ftrace_init_debugfs(void)
1270 struct dentry
*d_tracer
;
1271 struct dentry
*entry
;
1273 d_tracer
= tracing_init_dentry();
1275 entry
= debugfs_create_file("available_filter_functions", 0444,
1276 d_tracer
, NULL
, &ftrace_avail_fops
);
1278 pr_warning("Could not create debugfs "
1279 "'available_filter_functions' entry\n");
1281 entry
= debugfs_create_file("failures", 0444,
1282 d_tracer
, NULL
, &ftrace_failures_fops
);
1284 pr_warning("Could not create debugfs 'failures' entry\n");
1286 entry
= debugfs_create_file("set_ftrace_filter", 0644, d_tracer
,
1287 NULL
, &ftrace_filter_fops
);
1289 pr_warning("Could not create debugfs "
1290 "'set_ftrace_filter' entry\n");
1292 entry
= debugfs_create_file("set_ftrace_notrace", 0644, d_tracer
,
1293 NULL
, &ftrace_notrace_fops
);
1295 pr_warning("Could not create debugfs "
1296 "'set_ftrace_notrace' entry\n");
1301 fs_initcall(ftrace_init_debugfs
);
1303 static int ftrace_convert_nops(unsigned long *start
,
1308 unsigned long flags
;
1310 mutex_lock(&ftrace_start_lock
);
1313 addr
= ftrace_call_adjust(*p
++);
1314 ftrace_record_ip(addr
);
1317 /* disable interrupts to prevent kstop machine */
1318 local_irq_save(flags
);
1319 ftrace_update_code();
1320 local_irq_restore(flags
);
1321 mutex_unlock(&ftrace_start_lock
);
1326 void ftrace_init_module(unsigned long *start
, unsigned long *end
)
1328 if (ftrace_disabled
|| start
== end
)
1330 ftrace_convert_nops(start
, end
);
1333 extern unsigned long __start_mcount_loc
[];
1334 extern unsigned long __stop_mcount_loc
[];
1336 void __init
ftrace_init(void)
1338 unsigned long count
, addr
, flags
;
1341 /* Keep the ftrace pointer to the stub */
1342 addr
= (unsigned long)ftrace_stub
;
1344 local_irq_save(flags
);
1345 ftrace_dyn_arch_init(&addr
);
1346 local_irq_restore(flags
);
1348 /* ftrace_dyn_arch_init places the return code in addr */
1352 count
= __stop_mcount_loc
- __start_mcount_loc
;
1354 ret
= ftrace_dyn_table_alloc(count
);
1358 last_ftrace_enabled
= ftrace_enabled
= 1;
1360 ret
= ftrace_convert_nops(__start_mcount_loc
,
1365 ftrace_disabled
= 1;
1370 static int __init
ftrace_nodyn_init(void)
1375 device_initcall(ftrace_nodyn_init
);
1377 # define ftrace_startup() do { } while (0)
1378 # define ftrace_shutdown() do { } while (0)
1379 # define ftrace_startup_sysctl() do { } while (0)
1380 # define ftrace_shutdown_sysctl() do { } while (0)
1381 #endif /* CONFIG_DYNAMIC_FTRACE */
1384 * ftrace_kill - kill ftrace
1386 * This function should be used by panic code. It stops ftrace
1387 * but in a not so nice way. If you need to simply kill ftrace
1388 * from a non-atomic section, use ftrace_kill.
1390 void ftrace_kill(void)
1392 ftrace_disabled
= 1;
1394 clear_ftrace_function();
1398 * register_ftrace_function - register a function for profiling
1399 * @ops - ops structure that holds the function for profiling.
1401 * Register a function to be called by all functions in the
1404 * Note: @ops->func and all the functions it calls must be labeled
1405 * with "notrace", otherwise it will go into a
1408 int register_ftrace_function(struct ftrace_ops
*ops
)
1412 if (unlikely(ftrace_disabled
))
1415 mutex_lock(&ftrace_sysctl_lock
);
1416 ret
= __register_ftrace_function(ops
);
1418 mutex_unlock(&ftrace_sysctl_lock
);
1424 * unregister_ftrace_function - unresgister a function for profiling.
1425 * @ops - ops structure that holds the function to unregister
1427 * Unregister a function that was added to be called by ftrace profiling.
1429 int unregister_ftrace_function(struct ftrace_ops
*ops
)
1433 mutex_lock(&ftrace_sysctl_lock
);
1434 ret
= __unregister_ftrace_function(ops
);
1436 mutex_unlock(&ftrace_sysctl_lock
);
1442 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
1443 struct file
*file
, void __user
*buffer
, size_t *lenp
,
1448 if (unlikely(ftrace_disabled
))
1451 mutex_lock(&ftrace_sysctl_lock
);
1453 ret
= proc_dointvec(table
, write
, file
, buffer
, lenp
, ppos
);
1455 if (ret
|| !write
|| (last_ftrace_enabled
== ftrace_enabled
))
1458 last_ftrace_enabled
= ftrace_enabled
;
1460 if (ftrace_enabled
) {
1462 ftrace_startup_sysctl();
1464 /* we are starting ftrace again */
1465 if (ftrace_list
!= &ftrace_list_end
) {
1466 if (ftrace_list
->next
== &ftrace_list_end
)
1467 ftrace_trace_function
= ftrace_list
->func
;
1469 ftrace_trace_function
= ftrace_list_func
;
1473 /* stopping ftrace calls (just send to ftrace_stub) */
1474 ftrace_trace_function
= ftrace_stub
;
1476 ftrace_shutdown_sysctl();
1480 mutex_unlock(&ftrace_sysctl_lock
);