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/hash.h>
29 #include <linux/list.h>
31 #include <asm/ftrace.h>
35 #define FTRACE_WARN_ON(cond) \
41 #define FTRACE_WARN_ON_ONCE(cond) \
43 if (WARN_ON_ONCE(cond)) \
47 /* ftrace_enabled is a method to turn ftrace on or off */
48 int ftrace_enabled __read_mostly;
49 static int last_ftrace_enabled;
52 * ftrace_disabled is set when an anomaly is discovered.
53 * ftrace_disabled is much stronger than ftrace_enabled.
55 static int ftrace_disabled __read_mostly;
57 static DEFINE_SPINLOCK(ftrace_lock);
58 static DEFINE_MUTEX(ftrace_sysctl_lock);
60 static struct ftrace_ops ftrace_list_end __read_mostly =
65 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
66 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
68 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
70 struct ftrace_ops *op = ftrace_list;
72 /* in case someone actually ports this to alpha! */
73 read_barrier_depends();
75 while (op != &ftrace_list_end) {
77 read_barrier_depends();
78 op->func(ip, parent_ip);
84 * clear_ftrace_function - reset the ftrace function
86 * This NULLs the ftrace function and in essence stops
87 * tracing. There may be lag
89 void clear_ftrace_function(void)
91 ftrace_trace_function = ftrace_stub;
94 static int __register_ftrace_function(struct ftrace_ops *ops)
96 /* should not be called from interrupt context */
97 spin_lock(&ftrace_lock);
99 ops->next = ftrace_list;
101 * We are entering ops into the ftrace_list but another
102 * CPU might be walking that list. We need to make sure
103 * the ops->next pointer is valid before another CPU sees
104 * the ops pointer included into the ftrace_list.
109 if (ftrace_enabled) {
111 * For one func, simply call it directly.
112 * For more than one func, call the chain.
114 if (ops->next == &ftrace_list_end)
115 ftrace_trace_function = ops->func;
117 ftrace_trace_function = ftrace_list_func;
120 spin_unlock(&ftrace_lock);
125 static int __unregister_ftrace_function(struct ftrace_ops *ops)
127 struct ftrace_ops **p;
130 /* should not be called from interrupt context */
131 spin_lock(&ftrace_lock);
134 * If we are removing the last function, then simply point
135 * to the ftrace_stub.
137 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
138 ftrace_trace_function = ftrace_stub;
139 ftrace_list = &ftrace_list_end;
143 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
154 if (ftrace_enabled) {
155 /* If we only have one func left, then call that directly */
156 if (ftrace_list == &ftrace_list_end ||
157 ftrace_list->next == &ftrace_list_end)
158 ftrace_trace_function = ftrace_list->func;
162 spin_unlock(&ftrace_lock);
167 #ifdef CONFIG_DYNAMIC_FTRACE
168 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
169 # error Dynamic ftrace depends on MCOUNT_RECORD
173 * Since MCOUNT_ADDR may point to mcount itself, we do not want
174 * to get it confused by reading a reference in the code as we
175 * are parsing on objcopy output of text. Use a variable for
178 static unsigned long mcount_addr = MCOUNT_ADDR;
181 FTRACE_ENABLE_CALLS = (1 << 0),
182 FTRACE_DISABLE_CALLS = (1 << 1),
183 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
184 FTRACE_ENABLE_MCOUNT = (1 << 3),
185 FTRACE_DISABLE_MCOUNT = (1 << 4),
188 static int ftrace_filtered;
189 static int tracing_on;
190 static int frozen_record_count;
192 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
194 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
196 static DEFINE_MUTEX(ftrace_regex_lock);
199 struct ftrace_page *next;
201 struct dyn_ftrace records[];
204 #define ENTRIES_PER_PAGE \
205 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
207 /* estimate from running different kernels */
208 #define NR_TO_INIT 10000
210 static struct ftrace_page *ftrace_pages_start;
211 static struct ftrace_page *ftrace_pages;
213 static int ftrace_record_suspend;
215 static struct dyn_ftrace *ftrace_free_records;
218 #ifdef CONFIG_KPROBES
219 static inline void freeze_record(struct dyn_ftrace *rec)
221 if (!(rec->flags & FTRACE_FL_FROZEN)) {
222 rec->flags |= FTRACE_FL_FROZEN;
223 frozen_record_count++;
227 static inline void unfreeze_record(struct dyn_ftrace *rec)
229 if (rec->flags & FTRACE_FL_FROZEN) {
230 rec->flags &= ~FTRACE_FL_FROZEN;
231 frozen_record_count--;
235 static inline int record_frozen(struct dyn_ftrace *rec)
237 return rec->flags & FTRACE_FL_FROZEN;
240 # define freeze_record(rec) ({ 0; })
241 # define unfreeze_record(rec) ({ 0; })
242 # define record_frozen(rec) ({ 0; })
243 #endif /* CONFIG_KPROBES */
245 int skip_trace(unsigned long ip)
248 struct dyn_ftrace *rec;
249 struct hlist_node *t;
250 struct hlist_head *head;
252 if (frozen_record_count == 0)
255 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
256 hlist_for_each_entry_rcu(rec, t, head, node) {
258 if (record_frozen(rec)) {
259 if (rec->flags & FTRACE_FL_FAILED)
262 if (!(rec->flags & FTRACE_FL_CONVERTED))
265 if (!tracing_on || !ftrace_enabled)
268 if (ftrace_filtered) {
269 fl = rec->flags & (FTRACE_FL_FILTER |
271 if (!fl || (fl & FTRACE_FL_NOTRACE))
283 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
285 struct dyn_ftrace *p;
286 struct hlist_node *t;
289 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
300 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
302 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
305 /* called from kstop_machine */
306 static inline void ftrace_del_hash(struct dyn_ftrace *node)
308 hlist_del(&node->node);
311 static void ftrace_free_rec(struct dyn_ftrace *rec)
313 rec->ip = (unsigned long)ftrace_free_records;
314 ftrace_free_records = rec;
315 rec->flags |= FTRACE_FL_FREE;
318 void ftrace_release(void *start, unsigned long size)
320 struct dyn_ftrace *rec;
321 struct ftrace_page *pg;
322 unsigned long s = (unsigned long)start;
323 unsigned long e = s + size;
326 if (ftrace_disabled || !start)
329 /* should not be called from interrupt context */
330 spin_lock(&ftrace_lock);
332 for (pg = ftrace_pages_start; pg; pg = pg->next) {
333 for (i = 0; i < pg->index; i++) {
334 rec = &pg->records[i];
336 if ((rec->ip >= s) && (rec->ip < e))
337 ftrace_free_rec(rec);
340 spin_unlock(&ftrace_lock);
342 ftrace_release_hash(s, e);
345 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
347 struct dyn_ftrace *rec;
349 /* First check for freed records */
350 if (ftrace_free_records) {
351 rec = ftrace_free_records;
353 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
354 FTRACE_WARN_ON_ONCE(1);
355 ftrace_free_records = NULL;
359 ftrace_free_records = (void *)rec->ip;
360 memset(rec, 0, sizeof(*rec));
364 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
365 if (!ftrace_pages->next)
367 ftrace_pages = ftrace_pages->next;
370 return &ftrace_pages->records[ftrace_pages->index++];
374 ftrace_record_ip(unsigned long ip)
376 struct dyn_ftrace *node;
381 if (!ftrace_enabled || ftrace_disabled)
384 resched = need_resched();
385 preempt_disable_notrace();
388 * We simply need to protect against recursion.
389 * Use the the raw version of smp_processor_id and not
390 * __get_cpu_var which can call debug hooks that can
391 * cause a recursive crash here.
393 cpu = raw_smp_processor_id();
394 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
395 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
398 if (unlikely(ftrace_record_suspend))
401 key = hash_long(ip, FTRACE_HASHBITS);
403 FTRACE_WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
405 if (ftrace_ip_in_hash(ip, key))
408 /* This ip may have hit the hash before the lock */
409 if (ftrace_ip_in_hash(ip, key))
412 node = ftrace_alloc_dyn_node(ip);
418 ftrace_add_hash(node, key);
421 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
423 /* prevent recursion with scheduler */
425 preempt_enable_no_resched_notrace();
427 preempt_enable_notrace();
430 #define FTRACE_ADDR ((long)(ftrace_caller))
433 __ftrace_replace_code(struct dyn_ftrace *rec,
434 unsigned char *old, unsigned char *new, int enable)
436 unsigned long ip, fl;
440 if (ftrace_filtered && enable) {
442 * If filtering is on:
444 * If this record is set to be filtered and
445 * is enabled then do nothing.
447 * If this record is set to be filtered and
448 * it is not enabled, enable it.
450 * If this record is not set to be filtered
451 * and it is not enabled do nothing.
453 * If this record is set not to trace then
456 * If this record is set not to trace and
457 * it is enabled then disable it.
459 * If this record is not set to be filtered and
460 * it is enabled, disable it.
463 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
466 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
467 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
468 !fl || (fl == FTRACE_FL_NOTRACE))
472 * If it is enabled disable it,
473 * otherwise enable it!
475 if (fl & FTRACE_FL_ENABLED) {
476 /* swap new and old */
478 old = ftrace_call_replace(ip, FTRACE_ADDR);
479 rec->flags &= ~FTRACE_FL_ENABLED;
481 new = ftrace_call_replace(ip, FTRACE_ADDR);
482 rec->flags |= FTRACE_FL_ENABLED;
488 * If this record is set not to trace and is
489 * not enabled, do nothing.
491 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
492 if (fl == FTRACE_FL_NOTRACE)
495 new = ftrace_call_replace(ip, FTRACE_ADDR);
497 old = ftrace_call_replace(ip, FTRACE_ADDR);
500 if (rec->flags & FTRACE_FL_ENABLED)
502 rec->flags |= FTRACE_FL_ENABLED;
504 if (!(rec->flags & FTRACE_FL_ENABLED))
506 rec->flags &= ~FTRACE_FL_ENABLED;
510 return ftrace_modify_code(ip, old, new);
513 static void ftrace_replace_code(int enable)
516 unsigned char *new = NULL, *old = NULL;
517 struct dyn_ftrace *rec;
518 struct ftrace_page *pg;
521 old = ftrace_nop_replace();
523 new = ftrace_nop_replace();
525 for (pg = ftrace_pages_start; pg; pg = pg->next) {
526 for (i = 0; i < pg->index; i++) {
527 rec = &pg->records[i];
529 /* don't modify code that has already faulted */
530 if (rec->flags & FTRACE_FL_FAILED)
533 /* ignore updates to this record's mcount site */
534 if (get_kprobe((void *)rec->ip)) {
538 unfreeze_record(rec);
541 failed = __ftrace_replace_code(rec, old, new, enable);
542 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
543 rec->flags |= FTRACE_FL_FAILED;
544 if ((system_state == SYSTEM_BOOTING) ||
545 !core_kernel_text(rec->ip)) {
546 ftrace_del_hash(rec);
547 ftrace_free_rec(rec);
554 static void ftrace_shutdown_replenish(void)
556 if (ftrace_pages->next)
559 /* allocate another page */
560 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
563 static void print_ip_ins(const char *fmt, unsigned char *p)
567 printk(KERN_CONT "%s", fmt);
569 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
570 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
574 ftrace_code_disable(struct dyn_ftrace *rec)
577 unsigned char *nop, *call;
582 nop = ftrace_nop_replace();
583 call = ftrace_call_replace(ip, mcount_addr);
585 ret = ftrace_modify_code(ip, call, nop);
589 FTRACE_WARN_ON_ONCE(1);
590 pr_info("ftrace faulted on modifying ");
594 FTRACE_WARN_ON_ONCE(1);
595 pr_info("ftrace failed to modify ");
597 print_ip_ins(" expected: ", call);
598 print_ip_ins(" actual: ", (unsigned char *)ip);
599 print_ip_ins(" replace: ", nop);
600 printk(KERN_CONT "\n");
603 FTRACE_WARN_ON_ONCE(1);
604 pr_info("ftrace faulted on writing ");
608 FTRACE_WARN_ON_ONCE(1);
609 pr_info("ftrace faulted on unknown error ");
613 rec->flags |= FTRACE_FL_FAILED;
619 static int ftrace_update_code(void *ignore);
621 static int __ftrace_modify_code(void *data)
626 if (*command & FTRACE_ENABLE_CALLS) {
628 * Update any recorded ips now that we have the
631 ftrace_update_code(NULL);
632 ftrace_replace_code(1);
634 } else if (*command & FTRACE_DISABLE_CALLS) {
635 ftrace_replace_code(0);
639 if (*command & FTRACE_UPDATE_TRACE_FUNC)
640 ftrace_update_ftrace_func(ftrace_trace_function);
642 if (*command & FTRACE_ENABLE_MCOUNT) {
643 addr = (unsigned long)ftrace_record_ip;
644 ftrace_mcount_set(&addr);
645 } else if (*command & FTRACE_DISABLE_MCOUNT) {
646 addr = (unsigned long)ftrace_stub;
647 ftrace_mcount_set(&addr);
653 static void ftrace_run_update_code(int command)
655 stop_machine(__ftrace_modify_code, &command, NULL);
658 static ftrace_func_t saved_ftrace_func;
659 static int ftrace_start;
660 static DEFINE_MUTEX(ftrace_start_lock);
662 static void ftrace_startup(void)
666 if (unlikely(ftrace_disabled))
669 mutex_lock(&ftrace_start_lock);
671 if (ftrace_start == 1)
672 command |= FTRACE_ENABLE_CALLS;
674 if (saved_ftrace_func != ftrace_trace_function) {
675 saved_ftrace_func = ftrace_trace_function;
676 command |= FTRACE_UPDATE_TRACE_FUNC;
679 if (!command || !ftrace_enabled)
682 ftrace_run_update_code(command);
684 mutex_unlock(&ftrace_start_lock);
687 static void ftrace_shutdown(void)
691 if (unlikely(ftrace_disabled))
694 mutex_lock(&ftrace_start_lock);
697 command |= FTRACE_DISABLE_CALLS;
699 if (saved_ftrace_func != ftrace_trace_function) {
700 saved_ftrace_func = ftrace_trace_function;
701 command |= FTRACE_UPDATE_TRACE_FUNC;
704 if (!command || !ftrace_enabled)
707 ftrace_run_update_code(command);
709 mutex_unlock(&ftrace_start_lock);
712 static void ftrace_startup_sysctl(void)
714 int command = FTRACE_ENABLE_MCOUNT;
716 if (unlikely(ftrace_disabled))
719 mutex_lock(&ftrace_start_lock);
720 /* Force update next time */
721 saved_ftrace_func = NULL;
722 /* ftrace_start is true if we want ftrace running */
724 command |= FTRACE_ENABLE_CALLS;
726 ftrace_run_update_code(command);
727 mutex_unlock(&ftrace_start_lock);
730 static void ftrace_shutdown_sysctl(void)
732 int command = FTRACE_DISABLE_MCOUNT;
734 if (unlikely(ftrace_disabled))
737 mutex_lock(&ftrace_start_lock);
738 /* ftrace_start is true if ftrace is running */
740 command |= FTRACE_DISABLE_CALLS;
742 ftrace_run_update_code(command);
743 mutex_unlock(&ftrace_start_lock);
746 static cycle_t ftrace_update_time;
747 static unsigned long ftrace_update_cnt;
748 unsigned long ftrace_update_tot_cnt;
750 static int ftrace_update_code(void *ignore)
752 int i, save_ftrace_enabled;
754 struct dyn_ftrace *p;
755 struct hlist_node *t, *n;
756 struct hlist_head *head, temp_list;
758 /* Don't be recording funcs now */
759 ftrace_record_suspend++;
760 save_ftrace_enabled = ftrace_enabled;
763 start = ftrace_now(raw_smp_processor_id());
764 ftrace_update_cnt = 0;
766 /* No locks needed, the machine is stopped! */
767 for (i = 0; i < FTRACE_HASHSIZE; i++) {
768 INIT_HLIST_HEAD(&temp_list);
769 head = &ftrace_hash[i];
771 /* all CPUS are stopped, we are safe to modify code */
772 hlist_for_each_entry_safe(p, t, n, head, node) {
773 /* Skip over failed records which have not been
775 if (p->flags & FTRACE_FL_FAILED)
778 /* Unconverted records are always at the head of the
779 * hash bucket. Once we encounter a converted record,
780 * simply skip over to the next bucket. Saves ftraced
781 * some processor cycles (ftrace does its bid for
782 * global warming :-p ). */
783 if (p->flags & (FTRACE_FL_CONVERTED))
786 /* Ignore updates to this record's mcount site.
787 * Reintroduce this record at the head of this
788 * bucket to attempt to "convert" it again if
789 * the kprobe on it is unregistered before the
791 if (get_kprobe((void *)p->ip)) {
793 INIT_HLIST_NODE(&p->node);
794 hlist_add_head(&p->node, &temp_list);
801 /* convert record (i.e, patch mcount-call with NOP) */
802 if (ftrace_code_disable(p)) {
803 p->flags |= FTRACE_FL_CONVERTED;
806 if ((system_state == SYSTEM_BOOTING) ||
807 !core_kernel_text(p->ip)) {
814 hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
816 INIT_HLIST_NODE(&p->node);
817 hlist_add_head(&p->node, head);
821 stop = ftrace_now(raw_smp_processor_id());
822 ftrace_update_time = stop - start;
823 ftrace_update_tot_cnt += ftrace_update_cnt;
825 ftrace_enabled = save_ftrace_enabled;
826 ftrace_record_suspend--;
831 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
833 struct ftrace_page *pg;
837 /* allocate a few pages */
838 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
839 if (!ftrace_pages_start)
843 * Allocate a few more pages.
845 * TODO: have some parser search vmlinux before
846 * final linking to find all calls to ftrace.
848 * a) know how many pages to allocate.
850 * b) set up the table then.
852 * The dynamic code is still necessary for
856 pg = ftrace_pages = ftrace_pages_start;
858 cnt = num_to_init / ENTRIES_PER_PAGE;
859 pr_info("ftrace: allocating %ld hash entries in %d pages\n",
862 for (i = 0; i < cnt; i++) {
863 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
865 /* If we fail, we'll try later anyway */
876 FTRACE_ITER_FILTER = (1 << 0),
877 FTRACE_ITER_CONT = (1 << 1),
878 FTRACE_ITER_NOTRACE = (1 << 2),
879 FTRACE_ITER_FAILURES = (1 << 3),
882 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
884 struct ftrace_iterator {
886 struct ftrace_page *pg;
889 unsigned char buffer[FTRACE_BUFF_MAX+1];
895 t_next(struct seq_file *m, void *v, loff_t *pos)
897 struct ftrace_iterator *iter = m->private;
898 struct dyn_ftrace *rec = NULL;
902 /* should not be called from interrupt context */
903 spin_lock(&ftrace_lock);
905 if (iter->idx >= iter->pg->index) {
906 if (iter->pg->next) {
907 iter->pg = iter->pg->next;
912 rec = &iter->pg->records[iter->idx++];
913 if ((rec->flags & FTRACE_FL_FREE) ||
915 (!(iter->flags & FTRACE_ITER_FAILURES) &&
916 (rec->flags & FTRACE_FL_FAILED)) ||
918 ((iter->flags & FTRACE_ITER_FAILURES) &&
919 !(rec->flags & FTRACE_FL_FAILED)) ||
921 ((iter->flags & FTRACE_ITER_NOTRACE) &&
922 !(rec->flags & FTRACE_FL_NOTRACE))) {
927 spin_unlock(&ftrace_lock);
934 static void *t_start(struct seq_file *m, loff_t *pos)
936 struct ftrace_iterator *iter = m->private;
940 if (*pos != iter->pos) {
941 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
945 p = t_next(m, p, &l);
951 static void t_stop(struct seq_file *m, void *p)
955 static int t_show(struct seq_file *m, void *v)
957 struct dyn_ftrace *rec = v;
958 char str[KSYM_SYMBOL_LEN];
963 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
965 seq_printf(m, "%s\n", str);
970 static struct seq_operations show_ftrace_seq_ops = {
978 ftrace_avail_open(struct inode *inode, struct file *file)
980 struct ftrace_iterator *iter;
983 if (unlikely(ftrace_disabled))
986 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
990 iter->pg = ftrace_pages_start;
993 ret = seq_open(file, &show_ftrace_seq_ops);
995 struct seq_file *m = file->private_data;
1005 int ftrace_avail_release(struct inode *inode, struct file *file)
1007 struct seq_file *m = (struct seq_file *)file->private_data;
1008 struct ftrace_iterator *iter = m->private;
1010 seq_release(inode, file);
1017 ftrace_failures_open(struct inode *inode, struct file *file)
1021 struct ftrace_iterator *iter;
1023 ret = ftrace_avail_open(inode, file);
1025 m = (struct seq_file *)file->private_data;
1026 iter = (struct ftrace_iterator *)m->private;
1027 iter->flags = FTRACE_ITER_FAILURES;
1034 static void ftrace_filter_reset(int enable)
1036 struct ftrace_page *pg;
1037 struct dyn_ftrace *rec;
1038 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1041 /* should not be called from interrupt context */
1042 spin_lock(&ftrace_lock);
1044 ftrace_filtered = 0;
1045 pg = ftrace_pages_start;
1047 for (i = 0; i < pg->index; i++) {
1048 rec = &pg->records[i];
1049 if (rec->flags & FTRACE_FL_FAILED)
1051 rec->flags &= ~type;
1055 spin_unlock(&ftrace_lock);
1059 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1061 struct ftrace_iterator *iter;
1064 if (unlikely(ftrace_disabled))
1067 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1071 mutex_lock(&ftrace_regex_lock);
1072 if ((file->f_mode & FMODE_WRITE) &&
1073 !(file->f_flags & O_APPEND))
1074 ftrace_filter_reset(enable);
1076 if (file->f_mode & FMODE_READ) {
1077 iter->pg = ftrace_pages_start;
1079 iter->flags = enable ? FTRACE_ITER_FILTER :
1080 FTRACE_ITER_NOTRACE;
1082 ret = seq_open(file, &show_ftrace_seq_ops);
1084 struct seq_file *m = file->private_data;
1089 file->private_data = iter;
1090 mutex_unlock(&ftrace_regex_lock);
1096 ftrace_filter_open(struct inode *inode, struct file *file)
1098 return ftrace_regex_open(inode, file, 1);
1102 ftrace_notrace_open(struct inode *inode, struct file *file)
1104 return ftrace_regex_open(inode, file, 0);
1108 ftrace_regex_read(struct file *file, char __user *ubuf,
1109 size_t cnt, loff_t *ppos)
1111 if (file->f_mode & FMODE_READ)
1112 return seq_read(file, ubuf, cnt, ppos);
1118 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1122 if (file->f_mode & FMODE_READ)
1123 ret = seq_lseek(file, offset, origin);
1125 file->f_pos = ret = 1;
1138 ftrace_match(unsigned char *buff, int len, int enable)
1140 char str[KSYM_SYMBOL_LEN];
1141 char *search = NULL;
1142 struct ftrace_page *pg;
1143 struct dyn_ftrace *rec;
1144 int type = MATCH_FULL;
1145 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1146 unsigned i, match = 0, search_len = 0;
1148 for (i = 0; i < len; i++) {
1149 if (buff[i] == '*') {
1151 search = buff + i + 1;
1152 type = MATCH_END_ONLY;
1153 search_len = len - (i + 1);
1155 if (type == MATCH_END_ONLY) {
1156 type = MATCH_MIDDLE_ONLY;
1159 type = MATCH_FRONT_ONLY;
1167 /* should not be called from interrupt context */
1168 spin_lock(&ftrace_lock);
1170 ftrace_filtered = 1;
1171 pg = ftrace_pages_start;
1173 for (i = 0; i < pg->index; i++) {
1177 rec = &pg->records[i];
1178 if (rec->flags & FTRACE_FL_FAILED)
1180 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1183 if (strcmp(str, buff) == 0)
1186 case MATCH_FRONT_ONLY:
1187 if (memcmp(str, buff, match) == 0)
1190 case MATCH_MIDDLE_ONLY:
1191 if (strstr(str, search))
1194 case MATCH_END_ONLY:
1195 ptr = strstr(str, search);
1196 if (ptr && (ptr[search_len] == 0))
1205 spin_unlock(&ftrace_lock);
1209 ftrace_regex_write(struct file *file, const char __user *ubuf,
1210 size_t cnt, loff_t *ppos, int enable)
1212 struct ftrace_iterator *iter;
1217 if (!cnt || cnt < 0)
1220 mutex_lock(&ftrace_regex_lock);
1222 if (file->f_mode & FMODE_READ) {
1223 struct seq_file *m = file->private_data;
1226 iter = file->private_data;
1229 iter->flags &= ~FTRACE_ITER_CONT;
1230 iter->buffer_idx = 0;
1233 ret = get_user(ch, ubuf++);
1239 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1240 /* skip white space */
1241 while (cnt && isspace(ch)) {
1242 ret = get_user(ch, ubuf++);
1250 file->f_pos += read;
1255 iter->buffer_idx = 0;
1258 while (cnt && !isspace(ch)) {
1259 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1260 iter->buffer[iter->buffer_idx++] = ch;
1265 ret = get_user(ch, ubuf++);
1274 iter->buffer[iter->buffer_idx] = 0;
1275 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1276 iter->buffer_idx = 0;
1278 iter->flags |= FTRACE_ITER_CONT;
1281 file->f_pos += read;
1285 mutex_unlock(&ftrace_regex_lock);
1291 ftrace_filter_write(struct file *file, const char __user *ubuf,
1292 size_t cnt, loff_t *ppos)
1294 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1298 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1299 size_t cnt, loff_t *ppos)
1301 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1305 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1307 if (unlikely(ftrace_disabled))
1310 mutex_lock(&ftrace_regex_lock);
1312 ftrace_filter_reset(enable);
1314 ftrace_match(buf, len, enable);
1315 mutex_unlock(&ftrace_regex_lock);
1319 * ftrace_set_filter - set a function to filter on in ftrace
1320 * @buf - the string that holds the function filter text.
1321 * @len - the length of the string.
1322 * @reset - non zero to reset all filters before applying this filter.
1324 * Filters denote which functions should be enabled when tracing is enabled.
1325 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1327 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1329 ftrace_set_regex(buf, len, reset, 1);
1333 * ftrace_set_notrace - set a function to not trace in ftrace
1334 * @buf - the string that holds the function notrace text.
1335 * @len - the length of the string.
1336 * @reset - non zero to reset all filters before applying this filter.
1338 * Notrace Filters denote which functions should not be enabled when tracing
1339 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1342 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1344 ftrace_set_regex(buf, len, reset, 0);
1348 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1350 struct seq_file *m = (struct seq_file *)file->private_data;
1351 struct ftrace_iterator *iter;
1353 mutex_lock(&ftrace_regex_lock);
1354 if (file->f_mode & FMODE_READ) {
1357 seq_release(inode, file);
1359 iter = file->private_data;
1361 if (iter->buffer_idx) {
1363 iter->buffer[iter->buffer_idx] = 0;
1364 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1367 mutex_lock(&ftrace_sysctl_lock);
1368 mutex_lock(&ftrace_start_lock);
1369 if (iter->filtered && ftrace_start && ftrace_enabled)
1370 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1371 mutex_unlock(&ftrace_start_lock);
1372 mutex_unlock(&ftrace_sysctl_lock);
1375 mutex_unlock(&ftrace_regex_lock);
1380 ftrace_filter_release(struct inode *inode, struct file *file)
1382 return ftrace_regex_release(inode, file, 1);
1386 ftrace_notrace_release(struct inode *inode, struct file *file)
1388 return ftrace_regex_release(inode, file, 0);
1391 static struct file_operations ftrace_avail_fops = {
1392 .open = ftrace_avail_open,
1394 .llseek = seq_lseek,
1395 .release = ftrace_avail_release,
1398 static struct file_operations ftrace_failures_fops = {
1399 .open = ftrace_failures_open,
1401 .llseek = seq_lseek,
1402 .release = ftrace_avail_release,
1405 static struct file_operations ftrace_filter_fops = {
1406 .open = ftrace_filter_open,
1407 .read = ftrace_regex_read,
1408 .write = ftrace_filter_write,
1409 .llseek = ftrace_regex_lseek,
1410 .release = ftrace_filter_release,
1413 static struct file_operations ftrace_notrace_fops = {
1414 .open = ftrace_notrace_open,
1415 .read = ftrace_regex_read,
1416 .write = ftrace_notrace_write,
1417 .llseek = ftrace_regex_lseek,
1418 .release = ftrace_notrace_release,
1421 static __init int ftrace_init_debugfs(void)
1423 struct dentry *d_tracer;
1424 struct dentry *entry;
1426 d_tracer = tracing_init_dentry();
1428 entry = debugfs_create_file("available_filter_functions", 0444,
1429 d_tracer, NULL, &ftrace_avail_fops);
1431 pr_warning("Could not create debugfs "
1432 "'available_filter_functions' entry\n");
1434 entry = debugfs_create_file("failures", 0444,
1435 d_tracer, NULL, &ftrace_failures_fops);
1437 pr_warning("Could not create debugfs 'failures' entry\n");
1439 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1440 NULL, &ftrace_filter_fops);
1442 pr_warning("Could not create debugfs "
1443 "'set_ftrace_filter' entry\n");
1445 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1446 NULL, &ftrace_notrace_fops);
1448 pr_warning("Could not create debugfs "
1449 "'set_ftrace_notrace' entry\n");
1454 fs_initcall(ftrace_init_debugfs);
1456 static int ftrace_convert_nops(unsigned long *start,
1461 unsigned long flags;
1465 addr = ftrace_call_adjust(*p++);
1466 /* should not be called from interrupt context */
1467 spin_lock(&ftrace_lock);
1468 ftrace_record_ip(addr);
1469 spin_unlock(&ftrace_lock);
1470 ftrace_shutdown_replenish();
1474 local_irq_save(flags);
1475 ftrace_update_code(p);
1476 local_irq_restore(flags);
1481 void ftrace_init_module(unsigned long *start, unsigned long *end)
1483 if (ftrace_disabled || start == end)
1485 ftrace_convert_nops(start, end);
1488 extern unsigned long __start_mcount_loc[];
1489 extern unsigned long __stop_mcount_loc[];
1491 void __init ftrace_init(void)
1493 unsigned long count, addr, flags;
1496 /* Keep the ftrace pointer to the stub */
1497 addr = (unsigned long)ftrace_stub;
1499 local_irq_save(flags);
1500 ftrace_dyn_arch_init(&addr);
1501 local_irq_restore(flags);
1503 /* ftrace_dyn_arch_init places the return code in addr */
1507 count = __stop_mcount_loc - __start_mcount_loc;
1509 ret = ftrace_dyn_table_alloc(count);
1513 last_ftrace_enabled = ftrace_enabled = 1;
1515 ret = ftrace_convert_nops(__start_mcount_loc,
1520 ftrace_disabled = 1;
1524 # define ftrace_startup() do { } while (0)
1525 # define ftrace_shutdown() do { } while (0)
1526 # define ftrace_startup_sysctl() do { } while (0)
1527 # define ftrace_shutdown_sysctl() do { } while (0)
1528 #endif /* CONFIG_DYNAMIC_FTRACE */
1531 * ftrace_kill - kill ftrace
1533 * This function should be used by panic code. It stops ftrace
1534 * but in a not so nice way. If you need to simply kill ftrace
1535 * from a non-atomic section, use ftrace_kill.
1537 void ftrace_kill(void)
1539 ftrace_disabled = 1;
1541 clear_ftrace_function();
1545 * register_ftrace_function - register a function for profiling
1546 * @ops - ops structure that holds the function for profiling.
1548 * Register a function to be called by all functions in the
1551 * Note: @ops->func and all the functions it calls must be labeled
1552 * with "notrace", otherwise it will go into a
1555 int register_ftrace_function(struct ftrace_ops *ops)
1559 if (unlikely(ftrace_disabled))
1562 mutex_lock(&ftrace_sysctl_lock);
1563 ret = __register_ftrace_function(ops);
1565 mutex_unlock(&ftrace_sysctl_lock);
1571 * unregister_ftrace_function - unresgister a function for profiling.
1572 * @ops - ops structure that holds the function to unregister
1574 * Unregister a function that was added to be called by ftrace profiling.
1576 int unregister_ftrace_function(struct ftrace_ops *ops)
1580 mutex_lock(&ftrace_sysctl_lock);
1581 ret = __unregister_ftrace_function(ops);
1583 mutex_unlock(&ftrace_sysctl_lock);
1589 ftrace_enable_sysctl(struct ctl_table *table, int write,
1590 struct file *file, void __user *buffer, size_t *lenp,
1595 if (unlikely(ftrace_disabled))
1598 mutex_lock(&ftrace_sysctl_lock);
1600 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1602 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1605 last_ftrace_enabled = ftrace_enabled;
1607 if (ftrace_enabled) {
1609 ftrace_startup_sysctl();
1611 /* we are starting ftrace again */
1612 if (ftrace_list != &ftrace_list_end) {
1613 if (ftrace_list->next == &ftrace_list_end)
1614 ftrace_trace_function = ftrace_list->func;
1616 ftrace_trace_function = ftrace_list_func;
1620 /* stopping ftrace calls (just send to ftrace_stub) */
1621 ftrace_trace_function = ftrace_stub;
1623 ftrace_shutdown_sysctl();
1627 mutex_unlock(&ftrace_sysctl_lock);