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/ftrace.h>
25 #include <linux/sysctl.h>
26 #include <linux/ctype.h>
27 #include <linux/hash.h>
28 #include <linux/list.h>
32 /* ftrace_enabled is a method to turn ftrace on or off */
33 int ftrace_enabled __read_mostly
;
34 static int last_ftrace_enabled
;
37 * ftrace_disabled is set when an anomaly is discovered.
38 * ftrace_disabled is much stronger than ftrace_enabled.
40 static int ftrace_disabled __read_mostly
;
42 static DEFINE_SPINLOCK(ftrace_lock
);
43 static DEFINE_MUTEX(ftrace_sysctl_lock
);
45 static struct ftrace_ops ftrace_list_end __read_mostly
=
50 static struct ftrace_ops
*ftrace_list __read_mostly
= &ftrace_list_end
;
51 ftrace_func_t ftrace_trace_function __read_mostly
= ftrace_stub
;
53 void ftrace_list_func(unsigned long ip
, unsigned long parent_ip
)
55 struct ftrace_ops
*op
= ftrace_list
;
57 /* in case someone actually ports this to alpha! */
58 read_barrier_depends();
60 while (op
!= &ftrace_list_end
) {
62 read_barrier_depends();
63 op
->func(ip
, parent_ip
);
69 * clear_ftrace_function - reset the ftrace function
71 * This NULLs the ftrace function and in essence stops
72 * tracing. There may be lag
74 void clear_ftrace_function(void)
76 ftrace_trace_function
= ftrace_stub
;
79 static int __register_ftrace_function(struct ftrace_ops
*ops
)
81 /* Should never be called by interrupts */
82 spin_lock(&ftrace_lock
);
84 ops
->next
= ftrace_list
;
86 * We are entering ops into the ftrace_list but another
87 * CPU might be walking that list. We need to make sure
88 * the ops->next pointer is valid before another CPU sees
89 * the ops pointer included into the ftrace_list.
96 * For one func, simply call it directly.
97 * For more than one func, call the chain.
99 if (ops
->next
== &ftrace_list_end
)
100 ftrace_trace_function
= ops
->func
;
102 ftrace_trace_function
= ftrace_list_func
;
105 spin_unlock(&ftrace_lock
);
110 static int __unregister_ftrace_function(struct ftrace_ops
*ops
)
112 struct ftrace_ops
**p
;
115 spin_lock(&ftrace_lock
);
118 * If we are removing the last function, then simply point
119 * to the ftrace_stub.
121 if (ftrace_list
== ops
&& ops
->next
== &ftrace_list_end
) {
122 ftrace_trace_function
= ftrace_stub
;
123 ftrace_list
= &ftrace_list_end
;
127 for (p
= &ftrace_list
; *p
!= &ftrace_list_end
; p
= &(*p
)->next
)
138 if (ftrace_enabled
) {
139 /* If we only have one func left, then call that directly */
140 if (ftrace_list
== &ftrace_list_end
||
141 ftrace_list
->next
== &ftrace_list_end
)
142 ftrace_trace_function
= ftrace_list
->func
;
146 spin_unlock(&ftrace_lock
);
151 #ifdef CONFIG_DYNAMIC_FTRACE
153 static struct task_struct
*ftraced_task
;
156 FTRACE_ENABLE_CALLS
= (1 << 0),
157 FTRACE_DISABLE_CALLS
= (1 << 1),
158 FTRACE_UPDATE_TRACE_FUNC
= (1 << 2),
159 FTRACE_ENABLE_MCOUNT
= (1 << 3),
160 FTRACE_DISABLE_MCOUNT
= (1 << 4),
163 static int ftrace_filtered
;
165 static struct hlist_head ftrace_hash
[FTRACE_HASHSIZE
];
167 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu
);
169 static DEFINE_SPINLOCK(ftrace_shutdown_lock
);
170 static DEFINE_MUTEX(ftraced_lock
);
171 static DEFINE_MUTEX(ftrace_regex_lock
);
174 struct ftrace_page
*next
;
176 struct dyn_ftrace records
[];
179 #define ENTRIES_PER_PAGE \
180 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
182 /* estimate from running different kernels */
183 #define NR_TO_INIT 10000
185 static struct ftrace_page
*ftrace_pages_start
;
186 static struct ftrace_page
*ftrace_pages
;
188 static int ftraced_trigger
;
189 static int ftraced_suspend
;
190 static int ftraced_stop
;
192 static int ftrace_record_suspend
;
194 static struct dyn_ftrace
*ftrace_free_records
;
197 ftrace_ip_in_hash(unsigned long ip
, unsigned long key
)
199 struct dyn_ftrace
*p
;
200 struct hlist_node
*t
;
203 hlist_for_each_entry_rcu(p
, t
, &ftrace_hash
[key
], node
) {
214 ftrace_add_hash(struct dyn_ftrace
*node
, unsigned long key
)
216 hlist_add_head_rcu(&node
->node
, &ftrace_hash
[key
]);
219 static void ftrace_free_rec(struct dyn_ftrace
*rec
)
221 /* no locking, only called from kstop_machine */
223 rec
->ip
= (unsigned long)ftrace_free_records
;
224 ftrace_free_records
= rec
;
225 rec
->flags
|= FTRACE_FL_FREE
;
228 static struct dyn_ftrace
*ftrace_alloc_dyn_node(unsigned long ip
)
230 struct dyn_ftrace
*rec
;
232 /* First check for freed records */
233 if (ftrace_free_records
) {
234 rec
= ftrace_free_records
;
236 if (unlikely(!(rec
->flags
& FTRACE_FL_FREE
))) {
238 ftrace_free_records
= NULL
;
244 ftrace_free_records
= (void *)rec
->ip
;
245 memset(rec
, 0, sizeof(*rec
));
249 if (ftrace_pages
->index
== ENTRIES_PER_PAGE
) {
250 if (!ftrace_pages
->next
)
252 ftrace_pages
= ftrace_pages
->next
;
255 return &ftrace_pages
->records
[ftrace_pages
->index
++];
259 ftrace_record_ip(unsigned long ip
)
261 struct dyn_ftrace
*node
;
268 if (!ftrace_enabled
|| ftrace_disabled
)
271 resched
= need_resched();
272 preempt_disable_notrace();
275 * We simply need to protect against recursion.
276 * Use the the raw version of smp_processor_id and not
277 * __get_cpu_var which can call debug hooks that can
278 * cause a recursive crash here.
280 cpu
= raw_smp_processor_id();
281 per_cpu(ftrace_shutdown_disable_cpu
, cpu
)++;
282 if (per_cpu(ftrace_shutdown_disable_cpu
, cpu
) != 1)
285 if (unlikely(ftrace_record_suspend
))
288 key
= hash_long(ip
, FTRACE_HASHBITS
);
290 WARN_ON_ONCE(key
>= FTRACE_HASHSIZE
);
292 if (ftrace_ip_in_hash(ip
, key
))
295 atomic
= irqs_disabled();
297 spin_lock_irqsave(&ftrace_shutdown_lock
, flags
);
299 /* This ip may have hit the hash before the lock */
300 if (ftrace_ip_in_hash(ip
, key
))
304 * There's a slight race that the ftraced will update the
305 * hash and reset here. If it is already converted, skip it.
307 if (ftrace_ip_converted(ip
))
310 node
= ftrace_alloc_dyn_node(ip
);
316 ftrace_add_hash(node
, key
);
321 spin_unlock_irqrestore(&ftrace_shutdown_lock
, flags
);
323 per_cpu(ftrace_shutdown_disable_cpu
, cpu
)--;
325 /* prevent recursion with scheduler */
327 preempt_enable_no_resched_notrace();
329 preempt_enable_notrace();
332 #define FTRACE_ADDR ((long)(ftrace_caller))
333 #define MCOUNT_ADDR ((long)(mcount))
336 __ftrace_replace_code(struct dyn_ftrace
*rec
,
337 unsigned char *old
, unsigned char *new, int enable
)
339 unsigned long ip
, fl
;
344 if (ftrace_filtered
&& enable
) {
346 * If filtering is on:
348 * If this record is set to be filtered and
349 * is enabled then do nothing.
351 * If this record is set to be filtered and
352 * it is not enabled, enable it.
354 * If this record is not set to be filtered
355 * and it is not enabled do nothing.
357 * If this record is set not to trace then
360 * If this record is not set to be filtered and
361 * it is enabled, disable it.
363 fl
= rec
->flags
& (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
);
365 if ((fl
== (FTRACE_FL_FILTER
| FTRACE_FL_ENABLED
)) ||
366 (fl
== 0) || (rec
->flags
& FTRACE_FL_NOTRACE
))
370 * If it is enabled disable it,
371 * otherwise enable it!
373 if (fl
== FTRACE_FL_ENABLED
) {
374 /* swap new and old */
376 old
= ftrace_call_replace(ip
, FTRACE_ADDR
);
377 rec
->flags
&= ~FTRACE_FL_ENABLED
;
379 new = ftrace_call_replace(ip
, FTRACE_ADDR
);
380 rec
->flags
|= FTRACE_FL_ENABLED
;
386 * If this record is set not to trace and is
387 * not enabled, do nothing.
389 fl
= rec
->flags
& (FTRACE_FL_NOTRACE
| FTRACE_FL_ENABLED
);
390 if (fl
== FTRACE_FL_NOTRACE
)
393 new = ftrace_call_replace(ip
, FTRACE_ADDR
);
395 old
= ftrace_call_replace(ip
, FTRACE_ADDR
);
398 if (rec
->flags
& FTRACE_FL_ENABLED
)
400 rec
->flags
|= FTRACE_FL_ENABLED
;
402 if (!(rec
->flags
& FTRACE_FL_ENABLED
))
404 rec
->flags
&= ~FTRACE_FL_ENABLED
;
408 failed
= ftrace_modify_code(ip
, old
, new);
411 /* It is possible that the function hasn't been converted yet */
412 key
= hash_long(ip
, FTRACE_HASHBITS
);
413 if (!ftrace_ip_in_hash(ip
, key
)) {
414 rec
->flags
|= FTRACE_FL_FAILED
;
415 ftrace_free_rec(rec
);
421 static void ftrace_replace_code(int enable
)
423 unsigned char *new = NULL
, *old
= NULL
;
424 struct dyn_ftrace
*rec
;
425 struct ftrace_page
*pg
;
429 old
= ftrace_nop_replace();
431 new = ftrace_nop_replace();
433 for (pg
= ftrace_pages_start
; pg
; pg
= pg
->next
) {
434 for (i
= 0; i
< pg
->index
; i
++) {
435 rec
= &pg
->records
[i
];
437 /* don't modify code that has already faulted */
438 if (rec
->flags
& FTRACE_FL_FAILED
)
441 __ftrace_replace_code(rec
, old
, new, enable
);
446 static void ftrace_shutdown_replenish(void)
448 if (ftrace_pages
->next
)
451 /* allocate another page */
452 ftrace_pages
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
456 ftrace_code_disable(struct dyn_ftrace
*rec
)
459 unsigned char *nop
, *call
;
464 nop
= ftrace_nop_replace();
465 call
= ftrace_call_replace(ip
, MCOUNT_ADDR
);
467 failed
= ftrace_modify_code(ip
, call
, nop
);
469 rec
->flags
|= FTRACE_FL_FAILED
;
470 ftrace_free_rec(rec
);
476 static int __ftrace_update_code(void *ignore
);
478 static int __ftrace_modify_code(void *data
)
483 if (*command
& FTRACE_ENABLE_CALLS
) {
485 * Update any recorded ips now that we have the
488 __ftrace_update_code(NULL
);
489 ftrace_replace_code(1);
490 } else if (*command
& FTRACE_DISABLE_CALLS
)
491 ftrace_replace_code(0);
493 if (*command
& FTRACE_UPDATE_TRACE_FUNC
)
494 ftrace_update_ftrace_func(ftrace_trace_function
);
496 if (*command
& FTRACE_ENABLE_MCOUNT
) {
497 addr
= (unsigned long)ftrace_record_ip
;
498 ftrace_mcount_set(&addr
);
499 } else if (*command
& FTRACE_DISABLE_MCOUNT
) {
500 addr
= (unsigned long)ftrace_stub
;
501 ftrace_mcount_set(&addr
);
507 static void ftrace_run_update_code(int command
)
509 stop_machine_run(__ftrace_modify_code
, &command
, NR_CPUS
);
512 void ftrace_disable_daemon(void)
514 /* Stop the daemon from calling kstop_machine */
515 mutex_lock(&ftraced_lock
);
517 mutex_unlock(&ftraced_lock
);
519 ftrace_force_update();
522 void ftrace_enable_daemon(void)
524 mutex_lock(&ftraced_lock
);
526 mutex_unlock(&ftraced_lock
);
528 ftrace_force_update();
531 static ftrace_func_t saved_ftrace_func
;
533 static void ftrace_startup(void)
537 if (unlikely(ftrace_disabled
))
540 mutex_lock(&ftraced_lock
);
542 if (ftraced_suspend
== 1)
543 command
|= FTRACE_ENABLE_CALLS
;
545 if (saved_ftrace_func
!= ftrace_trace_function
) {
546 saved_ftrace_func
= ftrace_trace_function
;
547 command
|= FTRACE_UPDATE_TRACE_FUNC
;
550 if (!command
|| !ftrace_enabled
)
553 ftrace_run_update_code(command
);
555 mutex_unlock(&ftraced_lock
);
558 static void ftrace_shutdown(void)
562 if (unlikely(ftrace_disabled
))
565 mutex_lock(&ftraced_lock
);
567 if (!ftraced_suspend
)
568 command
|= FTRACE_DISABLE_CALLS
;
570 if (saved_ftrace_func
!= ftrace_trace_function
) {
571 saved_ftrace_func
= ftrace_trace_function
;
572 command
|= FTRACE_UPDATE_TRACE_FUNC
;
575 if (!command
|| !ftrace_enabled
)
578 ftrace_run_update_code(command
);
580 mutex_unlock(&ftraced_lock
);
583 static void ftrace_startup_sysctl(void)
585 int command
= FTRACE_ENABLE_MCOUNT
;
587 if (unlikely(ftrace_disabled
))
590 mutex_lock(&ftraced_lock
);
591 /* Force update next time */
592 saved_ftrace_func
= NULL
;
593 /* ftraced_suspend is true if we want ftrace running */
595 command
|= FTRACE_ENABLE_CALLS
;
597 ftrace_run_update_code(command
);
598 mutex_unlock(&ftraced_lock
);
601 static void ftrace_shutdown_sysctl(void)
603 int command
= FTRACE_DISABLE_MCOUNT
;
605 if (unlikely(ftrace_disabled
))
608 mutex_lock(&ftraced_lock
);
609 /* ftraced_suspend is true if ftrace is running */
611 command
|= FTRACE_DISABLE_CALLS
;
613 ftrace_run_update_code(command
);
614 mutex_unlock(&ftraced_lock
);
617 static cycle_t ftrace_update_time
;
618 static unsigned long ftrace_update_cnt
;
619 unsigned long ftrace_update_tot_cnt
;
621 static int __ftrace_update_code(void *ignore
)
623 struct dyn_ftrace
*p
;
624 struct hlist_head head
;
625 struct hlist_node
*t
;
626 int save_ftrace_enabled
;
630 /* Don't be recording funcs now */
631 ftrace_record_suspend
++;
632 save_ftrace_enabled
= ftrace_enabled
;
635 start
= ftrace_now(raw_smp_processor_id());
636 ftrace_update_cnt
= 0;
638 /* No locks needed, the machine is stopped! */
639 for (i
= 0; i
< FTRACE_HASHSIZE
; i
++) {
640 if (hlist_empty(&ftrace_hash
[i
]))
643 head
= ftrace_hash
[i
];
644 INIT_HLIST_HEAD(&ftrace_hash
[i
]);
646 /* all CPUS are stopped, we are safe to modify code */
647 hlist_for_each_entry(p
, t
, &head
, node
) {
648 if (ftrace_code_disable(p
))
654 stop
= ftrace_now(raw_smp_processor_id());
655 ftrace_update_time
= stop
- start
;
656 ftrace_update_tot_cnt
+= ftrace_update_cnt
;
659 ftrace_enabled
= save_ftrace_enabled
;
660 ftrace_record_suspend
--;
665 static int ftrace_update_code(void)
667 if (unlikely(ftrace_disabled
) ||
668 !ftrace_enabled
|| !ftraced_trigger
)
671 stop_machine_run(__ftrace_update_code
, NULL
, NR_CPUS
);
676 static int ftraced(void *ignore
)
680 while (!kthread_should_stop()) {
682 set_current_state(TASK_INTERRUPTIBLE
);
684 /* check once a second */
685 schedule_timeout(HZ
);
687 if (unlikely(ftrace_disabled
))
690 mutex_lock(&ftrace_sysctl_lock
);
691 mutex_lock(&ftraced_lock
);
692 if (!ftraced_suspend
&& !ftraced_stop
&&
693 ftrace_update_code()) {
694 usecs
= nsecs_to_usecs(ftrace_update_time
);
695 if (ftrace_update_tot_cnt
> 100000) {
696 ftrace_update_tot_cnt
= 0;
697 pr_info("hm, dftrace overflow: %lu change%s"
698 " (%lu total) in %lu usec%s\n",
700 ftrace_update_cnt
!= 1 ? "s" : "",
701 ftrace_update_tot_cnt
,
702 usecs
, usecs
!= 1 ? "s" : "");
707 mutex_unlock(&ftraced_lock
);
708 mutex_unlock(&ftrace_sysctl_lock
);
710 ftrace_shutdown_replenish();
712 __set_current_state(TASK_RUNNING
);
716 static int __init
ftrace_dyn_table_alloc(void)
718 struct ftrace_page
*pg
;
722 /* allocate a few pages */
723 ftrace_pages_start
= (void *)get_zeroed_page(GFP_KERNEL
);
724 if (!ftrace_pages_start
)
728 * Allocate a few more pages.
730 * TODO: have some parser search vmlinux before
731 * final linking to find all calls to ftrace.
733 * a) know how many pages to allocate.
735 * b) set up the table then.
737 * The dynamic code is still necessary for
741 pg
= ftrace_pages
= ftrace_pages_start
;
743 cnt
= NR_TO_INIT
/ ENTRIES_PER_PAGE
;
745 for (i
= 0; i
< cnt
; i
++) {
746 pg
->next
= (void *)get_zeroed_page(GFP_KERNEL
);
748 /* If we fail, we'll try later anyway */
759 FTRACE_ITER_FILTER
= (1 << 0),
760 FTRACE_ITER_CONT
= (1 << 1),
761 FTRACE_ITER_NOTRACE
= (1 << 2),
764 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
766 struct ftrace_iterator
{
768 struct ftrace_page
*pg
;
771 unsigned char buffer
[FTRACE_BUFF_MAX
+1];
777 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
779 struct ftrace_iterator
*iter
= m
->private;
780 struct dyn_ftrace
*rec
= NULL
;
785 if (iter
->idx
>= iter
->pg
->index
) {
786 if (iter
->pg
->next
) {
787 iter
->pg
= iter
->pg
->next
;
792 rec
= &iter
->pg
->records
[iter
->idx
++];
793 if ((rec
->flags
& FTRACE_FL_FAILED
) ||
794 ((iter
->flags
& FTRACE_ITER_FILTER
) &&
795 !(rec
->flags
& FTRACE_FL_FILTER
)) ||
796 ((iter
->flags
& FTRACE_ITER_NOTRACE
) &&
797 !(rec
->flags
& FTRACE_FL_NOTRACE
))) {
808 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
810 struct ftrace_iterator
*iter
= m
->private;
814 if (*pos
!= iter
->pos
) {
815 for (p
= t_next(m
, p
, &l
); p
&& l
< *pos
; p
= t_next(m
, p
, &l
))
819 p
= t_next(m
, p
, &l
);
825 static void t_stop(struct seq_file
*m
, void *p
)
829 static int t_show(struct seq_file
*m
, void *v
)
831 struct dyn_ftrace
*rec
= v
;
832 char str
[KSYM_SYMBOL_LEN
];
837 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
839 seq_printf(m
, "%s\n", str
);
844 static struct seq_operations show_ftrace_seq_ops
= {
852 ftrace_avail_open(struct inode
*inode
, struct file
*file
)
854 struct ftrace_iterator
*iter
;
857 if (unlikely(ftrace_disabled
))
860 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
864 iter
->pg
= ftrace_pages_start
;
867 ret
= seq_open(file
, &show_ftrace_seq_ops
);
869 struct seq_file
*m
= file
->private_data
;
879 int ftrace_avail_release(struct inode
*inode
, struct file
*file
)
881 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
882 struct ftrace_iterator
*iter
= m
->private;
884 seq_release(inode
, file
);
890 static void ftrace_filter_reset(int enable
)
892 struct ftrace_page
*pg
;
893 struct dyn_ftrace
*rec
;
894 unsigned long type
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
897 /* keep kstop machine from running */
901 pg
= ftrace_pages_start
;
903 for (i
= 0; i
< pg
->index
; i
++) {
904 rec
= &pg
->records
[i
];
905 if (rec
->flags
& FTRACE_FL_FAILED
)
915 ftrace_regex_open(struct inode
*inode
, struct file
*file
, int enable
)
917 struct ftrace_iterator
*iter
;
920 if (unlikely(ftrace_disabled
))
923 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
927 mutex_lock(&ftrace_regex_lock
);
928 if ((file
->f_mode
& FMODE_WRITE
) &&
929 !(file
->f_flags
& O_APPEND
))
930 ftrace_filter_reset(enable
);
932 if (file
->f_mode
& FMODE_READ
) {
933 iter
->pg
= ftrace_pages_start
;
935 iter
->flags
= enable
? FTRACE_ITER_FILTER
:
938 ret
= seq_open(file
, &show_ftrace_seq_ops
);
940 struct seq_file
*m
= file
->private_data
;
945 file
->private_data
= iter
;
946 mutex_unlock(&ftrace_regex_lock
);
952 ftrace_filter_open(struct inode
*inode
, struct file
*file
)
954 return ftrace_regex_open(inode
, file
, 1);
958 ftrace_notrace_open(struct inode
*inode
, struct file
*file
)
960 return ftrace_regex_open(inode
, file
, 0);
964 ftrace_regex_read(struct file
*file
, char __user
*ubuf
,
965 size_t cnt
, loff_t
*ppos
)
967 if (file
->f_mode
& FMODE_READ
)
968 return seq_read(file
, ubuf
, cnt
, ppos
);
974 ftrace_regex_lseek(struct file
*file
, loff_t offset
, int origin
)
978 if (file
->f_mode
& FMODE_READ
)
979 ret
= seq_lseek(file
, offset
, origin
);
981 file
->f_pos
= ret
= 1;
994 ftrace_match(unsigned char *buff
, int len
, int enable
)
996 char str
[KSYM_SYMBOL_LEN
];
998 struct ftrace_page
*pg
;
999 struct dyn_ftrace
*rec
;
1000 int type
= MATCH_FULL
;
1001 unsigned long flag
= enable
? FTRACE_FL_FILTER
: FTRACE_FL_NOTRACE
;
1002 unsigned i
, match
= 0, search_len
= 0;
1004 for (i
= 0; i
< len
; i
++) {
1005 if (buff
[i
] == '*') {
1007 search
= buff
+ i
+ 1;
1008 type
= MATCH_END_ONLY
;
1009 search_len
= len
- (i
+ 1);
1011 if (type
== MATCH_END_ONLY
) {
1012 type
= MATCH_MIDDLE_ONLY
;
1015 type
= MATCH_FRONT_ONLY
;
1023 /* keep kstop machine from running */
1026 ftrace_filtered
= 1;
1027 pg
= ftrace_pages_start
;
1029 for (i
= 0; i
< pg
->index
; i
++) {
1033 rec
= &pg
->records
[i
];
1034 if (rec
->flags
& FTRACE_FL_FAILED
)
1036 kallsyms_lookup(rec
->ip
, NULL
, NULL
, NULL
, str
);
1039 if (strcmp(str
, buff
) == 0)
1042 case MATCH_FRONT_ONLY
:
1043 if (memcmp(str
, buff
, match
) == 0)
1046 case MATCH_MIDDLE_ONLY
:
1047 if (strstr(str
, search
))
1050 case MATCH_END_ONLY
:
1051 ptr
= strstr(str
, search
);
1052 if (ptr
&& (ptr
[search_len
] == 0))
1065 ftrace_regex_write(struct file
*file
, const char __user
*ubuf
,
1066 size_t cnt
, loff_t
*ppos
, int enable
)
1068 struct ftrace_iterator
*iter
;
1073 if (!cnt
|| cnt
< 0)
1076 mutex_lock(&ftrace_regex_lock
);
1078 if (file
->f_mode
& FMODE_READ
) {
1079 struct seq_file
*m
= file
->private_data
;
1082 iter
= file
->private_data
;
1085 iter
->flags
&= ~FTRACE_ITER_CONT
;
1086 iter
->buffer_idx
= 0;
1089 ret
= get_user(ch
, ubuf
++);
1095 if (!(iter
->flags
& ~FTRACE_ITER_CONT
)) {
1096 /* skip white space */
1097 while (cnt
&& isspace(ch
)) {
1098 ret
= get_user(ch
, ubuf
++);
1106 file
->f_pos
+= read
;
1111 iter
->buffer_idx
= 0;
1114 while (cnt
&& !isspace(ch
)) {
1115 if (iter
->buffer_idx
< FTRACE_BUFF_MAX
)
1116 iter
->buffer
[iter
->buffer_idx
++] = ch
;
1121 ret
= get_user(ch
, ubuf
++);
1130 iter
->buffer
[iter
->buffer_idx
] = 0;
1131 ftrace_match(iter
->buffer
, iter
->buffer_idx
, enable
);
1132 iter
->buffer_idx
= 0;
1134 iter
->flags
|= FTRACE_ITER_CONT
;
1137 file
->f_pos
+= read
;
1141 mutex_unlock(&ftrace_regex_lock
);
1147 ftrace_filter_write(struct file
*file
, const char __user
*ubuf
,
1148 size_t cnt
, loff_t
*ppos
)
1150 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 1);
1154 ftrace_notrace_write(struct file
*file
, const char __user
*ubuf
,
1155 size_t cnt
, loff_t
*ppos
)
1157 return ftrace_regex_write(file
, ubuf
, cnt
, ppos
, 0);
1161 ftrace_set_regex(unsigned char *buf
, int len
, int reset
, int enable
)
1163 if (unlikely(ftrace_disabled
))
1166 mutex_lock(&ftrace_regex_lock
);
1168 ftrace_filter_reset(enable
);
1170 ftrace_match(buf
, len
, enable
);
1171 mutex_unlock(&ftrace_regex_lock
);
1175 * ftrace_set_filter - set a function to filter on in ftrace
1176 * @buf - the string that holds the function filter text.
1177 * @len - the length of the string.
1178 * @reset - non zero to reset all filters before applying this filter.
1180 * Filters denote which functions should be enabled when tracing is enabled.
1181 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1183 void ftrace_set_filter(unsigned char *buf
, int len
, int reset
)
1185 ftrace_set_regex(buf
, len
, reset
, 1);
1189 * ftrace_set_notrace - set a function to not trace in ftrace
1190 * @buf - the string that holds the function notrace text.
1191 * @len - the length of the string.
1192 * @reset - non zero to reset all filters before applying this filter.
1194 * Notrace Filters denote which functions should not be enabled when tracing
1195 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1198 void ftrace_set_notrace(unsigned char *buf
, int len
, int reset
)
1200 ftrace_set_regex(buf
, len
, reset
, 0);
1204 ftrace_regex_release(struct inode
*inode
, struct file
*file
, int enable
)
1206 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1207 struct ftrace_iterator
*iter
;
1209 mutex_lock(&ftrace_regex_lock
);
1210 if (file
->f_mode
& FMODE_READ
) {
1213 seq_release(inode
, file
);
1215 iter
= file
->private_data
;
1217 if (iter
->buffer_idx
) {
1219 iter
->buffer
[iter
->buffer_idx
] = 0;
1220 ftrace_match(iter
->buffer
, iter
->buffer_idx
, enable
);
1223 mutex_lock(&ftrace_sysctl_lock
);
1224 mutex_lock(&ftraced_lock
);
1225 if (iter
->filtered
&& ftraced_suspend
&& ftrace_enabled
)
1226 ftrace_run_update_code(FTRACE_ENABLE_CALLS
);
1227 mutex_unlock(&ftraced_lock
);
1228 mutex_unlock(&ftrace_sysctl_lock
);
1231 mutex_unlock(&ftrace_regex_lock
);
1236 ftrace_filter_release(struct inode
*inode
, struct file
*file
)
1238 return ftrace_regex_release(inode
, file
, 1);
1242 ftrace_notrace_release(struct inode
*inode
, struct file
*file
)
1244 return ftrace_regex_release(inode
, file
, 0);
1248 ftraced_read(struct file
*filp
, char __user
*ubuf
,
1249 size_t cnt
, loff_t
*ppos
)
1251 /* don't worry about races */
1252 char *buf
= ftraced_stop
? "disabled\n" : "enabled\n";
1253 int r
= strlen(buf
);
1255 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
1259 ftraced_write(struct file
*filp
, const char __user
*ubuf
,
1260 size_t cnt
, loff_t
*ppos
)
1266 if (cnt
>= sizeof(buf
))
1269 if (copy_from_user(&buf
, ubuf
, cnt
))
1272 if (strncmp(buf
, "enable", 6) == 0)
1274 else if (strncmp(buf
, "disable", 7) == 0)
1279 ret
= strict_strtoul(buf
, 10, &val
);
1287 ftrace_enable_daemon();
1289 ftrace_disable_daemon();
1296 static struct file_operations ftrace_avail_fops
= {
1297 .open
= ftrace_avail_open
,
1299 .llseek
= seq_lseek
,
1300 .release
= ftrace_avail_release
,
1303 static struct file_operations ftrace_filter_fops
= {
1304 .open
= ftrace_filter_open
,
1305 .read
= ftrace_regex_read
,
1306 .write
= ftrace_filter_write
,
1307 .llseek
= ftrace_regex_lseek
,
1308 .release
= ftrace_filter_release
,
1311 static struct file_operations ftrace_notrace_fops
= {
1312 .open
= ftrace_notrace_open
,
1313 .read
= ftrace_regex_read
,
1314 .write
= ftrace_notrace_write
,
1315 .llseek
= ftrace_regex_lseek
,
1316 .release
= ftrace_notrace_release
,
1319 static struct file_operations ftraced_fops
= {
1320 .open
= tracing_open_generic
,
1321 .read
= ftraced_read
,
1322 .write
= ftraced_write
,
1326 * ftrace_force_update - force an update to all recording ftrace functions
1328 int ftrace_force_update(void)
1332 if (unlikely(ftrace_disabled
))
1335 mutex_lock(&ftrace_sysctl_lock
);
1336 mutex_lock(&ftraced_lock
);
1339 * If ftraced_trigger is not set, then there is nothing
1342 if (ftraced_trigger
&& !ftrace_update_code())
1345 mutex_unlock(&ftraced_lock
);
1346 mutex_unlock(&ftrace_sysctl_lock
);
1351 static void ftrace_force_shutdown(void)
1353 struct task_struct
*task
;
1354 int command
= FTRACE_DISABLE_CALLS
| FTRACE_UPDATE_TRACE_FUNC
;
1356 mutex_lock(&ftraced_lock
);
1357 task
= ftraced_task
;
1358 ftraced_task
= NULL
;
1359 ftraced_suspend
= -1;
1360 ftrace_run_update_code(command
);
1361 mutex_unlock(&ftraced_lock
);
1367 static __init
int ftrace_init_debugfs(void)
1369 struct dentry
*d_tracer
;
1370 struct dentry
*entry
;
1372 d_tracer
= tracing_init_dentry();
1374 entry
= debugfs_create_file("available_filter_functions", 0444,
1375 d_tracer
, NULL
, &ftrace_avail_fops
);
1377 pr_warning("Could not create debugfs "
1378 "'available_filter_functions' entry\n");
1380 entry
= debugfs_create_file("set_ftrace_filter", 0644, d_tracer
,
1381 NULL
, &ftrace_filter_fops
);
1383 pr_warning("Could not create debugfs "
1384 "'set_ftrace_filter' entry\n");
1386 entry
= debugfs_create_file("set_ftrace_notrace", 0644, d_tracer
,
1387 NULL
, &ftrace_notrace_fops
);
1389 pr_warning("Could not create debugfs "
1390 "'set_ftrace_notrace' entry\n");
1392 entry
= debugfs_create_file("ftraced_enabled", 0644, d_tracer
,
1393 NULL
, &ftraced_fops
);
1395 pr_warning("Could not create debugfs "
1396 "'ftraced_enabled' entry\n");
1400 fs_initcall(ftrace_init_debugfs
);
1402 static int __init
ftrace_dynamic_init(void)
1404 struct task_struct
*p
;
1408 addr
= (unsigned long)ftrace_record_ip
;
1410 stop_machine_run(ftrace_dyn_arch_init
, &addr
, NR_CPUS
);
1412 /* ftrace_dyn_arch_init places the return code in addr */
1418 ret
= ftrace_dyn_table_alloc();
1422 p
= kthread_run(ftraced
, NULL
, "ftraced");
1428 last_ftrace_enabled
= ftrace_enabled
= 1;
1434 ftrace_disabled
= 1;
1438 core_initcall(ftrace_dynamic_init
);
1440 # define ftrace_startup() do { } while (0)
1441 # define ftrace_shutdown() do { } while (0)
1442 # define ftrace_startup_sysctl() do { } while (0)
1443 # define ftrace_shutdown_sysctl() do { } while (0)
1444 # define ftrace_force_shutdown() do { } while (0)
1445 #endif /* CONFIG_DYNAMIC_FTRACE */
1448 * ftrace_kill - totally shutdown ftrace
1450 * This is a safety measure. If something was detected that seems
1451 * wrong, calling this function will keep ftrace from doing
1452 * any more modifications, and updates.
1453 * used when something went wrong.
1455 void ftrace_kill(void)
1457 mutex_lock(&ftrace_sysctl_lock
);
1458 ftrace_disabled
= 1;
1461 clear_ftrace_function();
1462 mutex_unlock(&ftrace_sysctl_lock
);
1464 /* Try to totally disable ftrace */
1465 ftrace_force_shutdown();
1469 * register_ftrace_function - register a function for profiling
1470 * @ops - ops structure that holds the function for profiling.
1472 * Register a function to be called by all functions in the
1475 * Note: @ops->func and all the functions it calls must be labeled
1476 * with "notrace", otherwise it will go into a
1479 int register_ftrace_function(struct ftrace_ops
*ops
)
1483 if (unlikely(ftrace_disabled
))
1486 mutex_lock(&ftrace_sysctl_lock
);
1487 ret
= __register_ftrace_function(ops
);
1489 mutex_unlock(&ftrace_sysctl_lock
);
1495 * unregister_ftrace_function - unresgister a function for profiling.
1496 * @ops - ops structure that holds the function to unregister
1498 * Unregister a function that was added to be called by ftrace profiling.
1500 int unregister_ftrace_function(struct ftrace_ops
*ops
)
1504 mutex_lock(&ftrace_sysctl_lock
);
1505 ret
= __unregister_ftrace_function(ops
);
1507 mutex_unlock(&ftrace_sysctl_lock
);
1513 ftrace_enable_sysctl(struct ctl_table
*table
, int write
,
1514 struct file
*file
, void __user
*buffer
, size_t *lenp
,
1519 if (unlikely(ftrace_disabled
))
1522 mutex_lock(&ftrace_sysctl_lock
);
1524 ret
= proc_dointvec(table
, write
, file
, buffer
, lenp
, ppos
);
1526 if (ret
|| !write
|| (last_ftrace_enabled
== ftrace_enabled
))
1529 last_ftrace_enabled
= ftrace_enabled
;
1531 if (ftrace_enabled
) {
1533 ftrace_startup_sysctl();
1535 /* we are starting ftrace again */
1536 if (ftrace_list
!= &ftrace_list_end
) {
1537 if (ftrace_list
->next
== &ftrace_list_end
)
1538 ftrace_trace_function
= ftrace_list
->func
;
1540 ftrace_trace_function
= ftrace_list_func
;
1544 /* stopping ftrace calls (just send to ftrace_stub) */
1545 ftrace_trace_function
= ftrace_stub
;
1547 ftrace_shutdown_sysctl();
1551 mutex_unlock(&ftrace_sysctl_lock
);