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 /* ftrace_enabled is a method to turn ftrace on or off */
36 int ftrace_enabled __read_mostly;
37 static int last_ftrace_enabled;
40 * ftrace_disabled is set when an anomaly is discovered.
41 * ftrace_disabled is much stronger than ftrace_enabled.
43 static int ftrace_disabled __read_mostly;
45 static DEFINE_SPINLOCK(ftrace_lock);
46 static DEFINE_MUTEX(ftrace_sysctl_lock);
48 static struct ftrace_ops ftrace_list_end __read_mostly =
53 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
54 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
56 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
58 struct ftrace_ops *op = ftrace_list;
60 /* in case someone actually ports this to alpha! */
61 read_barrier_depends();
63 while (op != &ftrace_list_end) {
65 read_barrier_depends();
66 op->func(ip, parent_ip);
72 * clear_ftrace_function - reset the ftrace function
74 * This NULLs the ftrace function and in essence stops
75 * tracing. There may be lag
77 void clear_ftrace_function(void)
79 ftrace_trace_function = ftrace_stub;
82 static int __register_ftrace_function(struct ftrace_ops *ops)
84 /* Should never be called by interrupts */
85 spin_lock(&ftrace_lock);
87 ops->next = ftrace_list;
89 * We are entering ops into the ftrace_list but another
90 * CPU might be walking that list. We need to make sure
91 * the ops->next pointer is valid before another CPU sees
92 * the ops pointer included into the ftrace_list.
99 * For one func, simply call it directly.
100 * For more than one func, call the chain.
102 if (ops->next == &ftrace_list_end)
103 ftrace_trace_function = ops->func;
105 ftrace_trace_function = ftrace_list_func;
108 spin_unlock(&ftrace_lock);
113 static int __unregister_ftrace_function(struct ftrace_ops *ops)
115 struct ftrace_ops **p;
118 spin_lock(&ftrace_lock);
121 * If we are removing the last function, then simply point
122 * to the ftrace_stub.
124 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
125 ftrace_trace_function = ftrace_stub;
126 ftrace_list = &ftrace_list_end;
130 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
141 if (ftrace_enabled) {
142 /* If we only have one func left, then call that directly */
143 if (ftrace_list == &ftrace_list_end ||
144 ftrace_list->next == &ftrace_list_end)
145 ftrace_trace_function = ftrace_list->func;
149 spin_unlock(&ftrace_lock);
154 #ifdef CONFIG_DYNAMIC_FTRACE
156 static struct task_struct *ftraced_task;
159 FTRACE_ENABLE_CALLS = (1 << 0),
160 FTRACE_DISABLE_CALLS = (1 << 1),
161 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
162 FTRACE_ENABLE_MCOUNT = (1 << 3),
163 FTRACE_DISABLE_MCOUNT = (1 << 4),
166 static int ftrace_filtered;
167 static int tracing_on;
168 static int frozen_record_count;
170 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
172 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
174 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
175 static DEFINE_MUTEX(ftraced_lock);
176 static DEFINE_MUTEX(ftrace_regex_lock);
179 struct ftrace_page *next;
181 struct dyn_ftrace records[];
184 #define ENTRIES_PER_PAGE \
185 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
187 /* estimate from running different kernels */
188 #define NR_TO_INIT 10000
190 static struct ftrace_page *ftrace_pages_start;
191 static struct ftrace_page *ftrace_pages;
193 static int ftraced_trigger;
194 static int ftraced_suspend;
195 static int ftraced_stop;
197 static int ftrace_record_suspend;
199 static struct dyn_ftrace *ftrace_free_records;
202 #ifdef CONFIG_KPROBES
203 static inline void freeze_record(struct dyn_ftrace *rec)
205 if (!(rec->flags & FTRACE_FL_FROZEN)) {
206 rec->flags |= FTRACE_FL_FROZEN;
207 frozen_record_count++;
211 static inline void unfreeze_record(struct dyn_ftrace *rec)
213 if (rec->flags & FTRACE_FL_FROZEN) {
214 rec->flags &= ~FTRACE_FL_FROZEN;
215 frozen_record_count--;
219 static inline int record_frozen(struct dyn_ftrace *rec)
221 return rec->flags & FTRACE_FL_FROZEN;
224 # define freeze_record(rec) ({ 0; })
225 # define unfreeze_record(rec) ({ 0; })
226 # define record_frozen(rec) ({ 0; })
227 #endif /* CONFIG_KPROBES */
229 int skip_trace(unsigned long ip)
232 struct dyn_ftrace *rec;
233 struct hlist_node *t;
234 struct hlist_head *head;
236 if (frozen_record_count == 0)
239 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
240 hlist_for_each_entry_rcu(rec, t, head, node) {
242 if (record_frozen(rec)) {
243 if (rec->flags & FTRACE_FL_FAILED)
246 if (!(rec->flags & FTRACE_FL_CONVERTED))
249 if (!tracing_on || !ftrace_enabled)
252 if (ftrace_filtered) {
253 fl = rec->flags & (FTRACE_FL_FILTER |
255 if (!fl || (fl & FTRACE_FL_NOTRACE))
267 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
269 struct dyn_ftrace *p;
270 struct hlist_node *t;
273 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
284 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
286 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
289 /* called from kstop_machine */
290 static inline void ftrace_del_hash(struct dyn_ftrace *node)
292 hlist_del(&node->node);
295 static void ftrace_free_rec(struct dyn_ftrace *rec)
297 /* no locking, only called from kstop_machine */
299 rec->ip = (unsigned long)ftrace_free_records;
300 ftrace_free_records = rec;
301 rec->flags |= FTRACE_FL_FREE;
304 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
306 struct dyn_ftrace *rec;
308 /* First check for freed records */
309 if (ftrace_free_records) {
310 rec = ftrace_free_records;
312 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
314 ftrace_free_records = NULL;
320 ftrace_free_records = (void *)rec->ip;
321 memset(rec, 0, sizeof(*rec));
325 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
326 if (!ftrace_pages->next)
328 ftrace_pages = ftrace_pages->next;
331 return &ftrace_pages->records[ftrace_pages->index++];
335 ftrace_record_ip(unsigned long ip)
337 struct dyn_ftrace *node;
344 if (!ftrace_enabled || ftrace_disabled)
347 resched = need_resched();
348 preempt_disable_notrace();
351 * We simply need to protect against recursion.
352 * Use the the raw version of smp_processor_id and not
353 * __get_cpu_var which can call debug hooks that can
354 * cause a recursive crash here.
356 cpu = raw_smp_processor_id();
357 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
358 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
361 if (unlikely(ftrace_record_suspend))
364 key = hash_long(ip, FTRACE_HASHBITS);
366 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
368 if (ftrace_ip_in_hash(ip, key))
371 atomic = irqs_disabled();
373 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
375 /* This ip may have hit the hash before the lock */
376 if (ftrace_ip_in_hash(ip, key))
379 node = ftrace_alloc_dyn_node(ip);
385 ftrace_add_hash(node, key);
390 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
392 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
394 /* prevent recursion with scheduler */
396 preempt_enable_no_resched_notrace();
398 preempt_enable_notrace();
401 #define FTRACE_ADDR ((long)(ftrace_caller))
404 __ftrace_replace_code(struct dyn_ftrace *rec,
405 unsigned char *old, unsigned char *new, int enable)
407 unsigned long ip, fl;
411 if (ftrace_filtered && enable) {
413 * If filtering is on:
415 * If this record is set to be filtered and
416 * is enabled then do nothing.
418 * If this record is set to be filtered and
419 * it is not enabled, enable it.
421 * If this record is not set to be filtered
422 * and it is not enabled do nothing.
424 * If this record is set not to trace then
427 * If this record is set not to trace and
428 * it is enabled then disable it.
430 * If this record is not set to be filtered and
431 * it is enabled, disable it.
434 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
437 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
438 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
439 !fl || (fl == FTRACE_FL_NOTRACE))
443 * If it is enabled disable it,
444 * otherwise enable it!
446 if (fl & FTRACE_FL_ENABLED) {
447 /* swap new and old */
449 old = ftrace_call_replace(ip, FTRACE_ADDR);
450 rec->flags &= ~FTRACE_FL_ENABLED;
452 new = ftrace_call_replace(ip, FTRACE_ADDR);
453 rec->flags |= FTRACE_FL_ENABLED;
459 * If this record is set not to trace and is
460 * not enabled, do nothing.
462 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
463 if (fl == FTRACE_FL_NOTRACE)
466 new = ftrace_call_replace(ip, FTRACE_ADDR);
468 old = ftrace_call_replace(ip, FTRACE_ADDR);
471 if (rec->flags & FTRACE_FL_ENABLED)
473 rec->flags |= FTRACE_FL_ENABLED;
475 if (!(rec->flags & FTRACE_FL_ENABLED))
477 rec->flags &= ~FTRACE_FL_ENABLED;
481 return ftrace_modify_code(ip, old, new);
484 static void ftrace_replace_code(int enable)
487 unsigned char *new = NULL, *old = NULL;
488 struct dyn_ftrace *rec;
489 struct ftrace_page *pg;
492 old = ftrace_nop_replace();
494 new = ftrace_nop_replace();
496 for (pg = ftrace_pages_start; pg; pg = pg->next) {
497 for (i = 0; i < pg->index; i++) {
498 rec = &pg->records[i];
500 /* don't modify code that has already faulted */
501 if (rec->flags & FTRACE_FL_FAILED)
504 /* ignore updates to this record's mcount site */
505 if (get_kprobe((void *)rec->ip))
508 failed = __ftrace_replace_code(rec, old, new, enable);
509 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
510 rec->flags |= FTRACE_FL_FAILED;
511 if ((system_state == SYSTEM_BOOTING) ||
512 !core_kernel_text(rec->ip)) {
513 ftrace_del_hash(rec);
514 ftrace_free_rec(rec);
521 static void ftrace_shutdown_replenish(void)
523 if (ftrace_pages->next)
526 /* allocate another page */
527 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
531 ftrace_code_disable(struct dyn_ftrace *rec)
534 unsigned char *nop, *call;
539 nop = ftrace_nop_replace();
540 call = ftrace_call_replace(ip, MCOUNT_ADDR);
542 failed = ftrace_modify_code(ip, call, nop);
544 rec->flags |= FTRACE_FL_FAILED;
550 static int __ftrace_update_code(void *ignore);
552 static int __ftrace_modify_code(void *data)
557 if (*command & FTRACE_ENABLE_CALLS) {
559 * Update any recorded ips now that we have the
562 __ftrace_update_code(NULL);
563 ftrace_replace_code(1);
565 } else if (*command & FTRACE_DISABLE_CALLS) {
566 ftrace_replace_code(0);
570 if (*command & FTRACE_UPDATE_TRACE_FUNC)
571 ftrace_update_ftrace_func(ftrace_trace_function);
573 if (*command & FTRACE_ENABLE_MCOUNT) {
574 addr = (unsigned long)ftrace_record_ip;
575 ftrace_mcount_set(&addr);
576 } else if (*command & FTRACE_DISABLE_MCOUNT) {
577 addr = (unsigned long)ftrace_stub;
578 ftrace_mcount_set(&addr);
584 static void ftrace_run_update_code(int command)
586 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
589 void ftrace_disable_daemon(void)
591 /* Stop the daemon from calling kstop_machine */
592 mutex_lock(&ftraced_lock);
594 mutex_unlock(&ftraced_lock);
596 ftrace_force_update();
599 void ftrace_enable_daemon(void)
601 mutex_lock(&ftraced_lock);
603 mutex_unlock(&ftraced_lock);
605 ftrace_force_update();
608 static ftrace_func_t saved_ftrace_func;
610 static void ftrace_startup(void)
614 if (unlikely(ftrace_disabled))
617 mutex_lock(&ftraced_lock);
619 if (ftraced_suspend == 1)
620 command |= FTRACE_ENABLE_CALLS;
622 if (saved_ftrace_func != ftrace_trace_function) {
623 saved_ftrace_func = ftrace_trace_function;
624 command |= FTRACE_UPDATE_TRACE_FUNC;
627 if (!command || !ftrace_enabled)
630 ftrace_run_update_code(command);
632 mutex_unlock(&ftraced_lock);
635 static void ftrace_shutdown(void)
639 if (unlikely(ftrace_disabled))
642 mutex_lock(&ftraced_lock);
644 if (!ftraced_suspend)
645 command |= FTRACE_DISABLE_CALLS;
647 if (saved_ftrace_func != ftrace_trace_function) {
648 saved_ftrace_func = ftrace_trace_function;
649 command |= FTRACE_UPDATE_TRACE_FUNC;
652 if (!command || !ftrace_enabled)
655 ftrace_run_update_code(command);
657 mutex_unlock(&ftraced_lock);
660 static void ftrace_startup_sysctl(void)
662 int command = FTRACE_ENABLE_MCOUNT;
664 if (unlikely(ftrace_disabled))
667 mutex_lock(&ftraced_lock);
668 /* Force update next time */
669 saved_ftrace_func = NULL;
670 /* ftraced_suspend is true if we want ftrace running */
672 command |= FTRACE_ENABLE_CALLS;
674 ftrace_run_update_code(command);
675 mutex_unlock(&ftraced_lock);
678 static void ftrace_shutdown_sysctl(void)
680 int command = FTRACE_DISABLE_MCOUNT;
682 if (unlikely(ftrace_disabled))
685 mutex_lock(&ftraced_lock);
686 /* ftraced_suspend is true if ftrace is running */
688 command |= FTRACE_DISABLE_CALLS;
690 ftrace_run_update_code(command);
691 mutex_unlock(&ftraced_lock);
694 static cycle_t ftrace_update_time;
695 static unsigned long ftrace_update_cnt;
696 unsigned long ftrace_update_tot_cnt;
698 static int __ftrace_update_code(void *ignore)
700 int i, save_ftrace_enabled;
702 struct dyn_ftrace *p;
703 struct hlist_node *t, *n;
704 struct hlist_head *head, temp_list;
706 /* Don't be recording funcs now */
707 ftrace_record_suspend++;
708 save_ftrace_enabled = ftrace_enabled;
711 start = ftrace_now(raw_smp_processor_id());
712 ftrace_update_cnt = 0;
714 /* No locks needed, the machine is stopped! */
715 for (i = 0; i < FTRACE_HASHSIZE; i++) {
716 INIT_HLIST_HEAD(&temp_list);
717 head = &ftrace_hash[i];
719 /* all CPUS are stopped, we are safe to modify code */
720 hlist_for_each_entry_safe(p, t, n, head, node) {
721 /* Skip over failed records which have not been
723 if (p->flags & FTRACE_FL_FAILED)
726 /* Unconverted records are always at the head of the
727 * hash bucket. Once we encounter a converted record,
728 * simply skip over to the next bucket. Saves ftraced
729 * some processor cycles (ftrace does its bid for
730 * global warming :-p ). */
731 if (p->flags & (FTRACE_FL_CONVERTED))
734 /* Ignore updates to this record's mcount site.
735 * Reintroduce this record at the head of this
736 * bucket to attempt to "convert" it again if
737 * the kprobe on it is unregistered before the
739 if (get_kprobe((void *)p->ip)) {
741 INIT_HLIST_NODE(&p->node);
742 hlist_add_head(&p->node, &temp_list);
746 /* convert record (i.e, patch mcount-call with NOP) */
747 if (ftrace_code_disable(p)) {
748 p->flags |= FTRACE_FL_CONVERTED;
751 if ((system_state == SYSTEM_BOOTING) ||
752 !core_kernel_text(p->ip)) {
759 hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
761 INIT_HLIST_NODE(&p->node);
762 hlist_add_head(&p->node, head);
766 stop = ftrace_now(raw_smp_processor_id());
767 ftrace_update_time = stop - start;
768 ftrace_update_tot_cnt += ftrace_update_cnt;
771 ftrace_enabled = save_ftrace_enabled;
772 ftrace_record_suspend--;
777 static int ftrace_update_code(void)
779 if (unlikely(ftrace_disabled) ||
780 !ftrace_enabled || !ftraced_trigger)
783 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
788 static int ftraced(void *ignore)
792 while (!kthread_should_stop()) {
794 set_current_state(TASK_INTERRUPTIBLE);
796 /* check once a second */
797 schedule_timeout(HZ);
799 if (unlikely(ftrace_disabled))
802 mutex_lock(&ftrace_sysctl_lock);
803 mutex_lock(&ftraced_lock);
804 if (!ftraced_suspend && !ftraced_stop &&
805 ftrace_update_code()) {
806 usecs = nsecs_to_usecs(ftrace_update_time);
807 if (ftrace_update_tot_cnt > 100000) {
808 ftrace_update_tot_cnt = 0;
809 pr_info("hm, dftrace overflow: %lu change%s"
810 " (%lu total) in %lu usec%s\n",
812 ftrace_update_cnt != 1 ? "s" : "",
813 ftrace_update_tot_cnt,
814 usecs, usecs != 1 ? "s" : "");
819 mutex_unlock(&ftraced_lock);
820 mutex_unlock(&ftrace_sysctl_lock);
822 ftrace_shutdown_replenish();
824 __set_current_state(TASK_RUNNING);
828 static int __init ftrace_dyn_table_alloc(void)
830 struct ftrace_page *pg;
834 /* allocate a few pages */
835 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
836 if (!ftrace_pages_start)
840 * Allocate a few more pages.
842 * TODO: have some parser search vmlinux before
843 * final linking to find all calls to ftrace.
845 * a) know how many pages to allocate.
847 * b) set up the table then.
849 * The dynamic code is still necessary for
853 pg = ftrace_pages = ftrace_pages_start;
855 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
857 for (i = 0; i < cnt; i++) {
858 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
860 /* If we fail, we'll try later anyway */
871 FTRACE_ITER_FILTER = (1 << 0),
872 FTRACE_ITER_CONT = (1 << 1),
873 FTRACE_ITER_NOTRACE = (1 << 2),
874 FTRACE_ITER_FAILURES = (1 << 3),
877 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
879 struct ftrace_iterator {
881 struct ftrace_page *pg;
884 unsigned char buffer[FTRACE_BUFF_MAX+1];
890 t_next(struct seq_file *m, void *v, loff_t *pos)
892 struct ftrace_iterator *iter = m->private;
893 struct dyn_ftrace *rec = NULL;
898 if (iter->idx >= iter->pg->index) {
899 if (iter->pg->next) {
900 iter->pg = iter->pg->next;
905 rec = &iter->pg->records[iter->idx++];
906 if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
907 (rec->flags & FTRACE_FL_FAILED)) ||
909 ((iter->flags & FTRACE_ITER_FAILURES) &&
910 (!(rec->flags & FTRACE_FL_FAILED) ||
911 (rec->flags & FTRACE_FL_FREE))) ||
913 ((iter->flags & FTRACE_ITER_FILTER) &&
914 !(rec->flags & FTRACE_FL_FILTER)) ||
916 ((iter->flags & FTRACE_ITER_NOTRACE) &&
917 !(rec->flags & FTRACE_FL_NOTRACE))) {
928 static void *t_start(struct seq_file *m, loff_t *pos)
930 struct ftrace_iterator *iter = m->private;
934 if (*pos != iter->pos) {
935 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
939 p = t_next(m, p, &l);
945 static void t_stop(struct seq_file *m, void *p)
949 static int t_show(struct seq_file *m, void *v)
951 struct dyn_ftrace *rec = v;
952 char str[KSYM_SYMBOL_LEN];
957 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
959 seq_printf(m, "%s\n", str);
964 static struct seq_operations show_ftrace_seq_ops = {
972 ftrace_avail_open(struct inode *inode, struct file *file)
974 struct ftrace_iterator *iter;
977 if (unlikely(ftrace_disabled))
980 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
984 iter->pg = ftrace_pages_start;
987 ret = seq_open(file, &show_ftrace_seq_ops);
989 struct seq_file *m = file->private_data;
999 int ftrace_avail_release(struct inode *inode, struct file *file)
1001 struct seq_file *m = (struct seq_file *)file->private_data;
1002 struct ftrace_iterator *iter = m->private;
1004 seq_release(inode, file);
1011 ftrace_failures_open(struct inode *inode, struct file *file)
1015 struct ftrace_iterator *iter;
1017 ret = ftrace_avail_open(inode, file);
1019 m = (struct seq_file *)file->private_data;
1020 iter = (struct ftrace_iterator *)m->private;
1021 iter->flags = FTRACE_ITER_FAILURES;
1028 static void ftrace_filter_reset(int enable)
1030 struct ftrace_page *pg;
1031 struct dyn_ftrace *rec;
1032 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1035 /* keep kstop machine from running */
1038 ftrace_filtered = 0;
1039 pg = ftrace_pages_start;
1041 for (i = 0; i < pg->index; i++) {
1042 rec = &pg->records[i];
1043 if (rec->flags & FTRACE_FL_FAILED)
1045 rec->flags &= ~type;
1053 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1055 struct ftrace_iterator *iter;
1058 if (unlikely(ftrace_disabled))
1061 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1065 mutex_lock(&ftrace_regex_lock);
1066 if ((file->f_mode & FMODE_WRITE) &&
1067 !(file->f_flags & O_APPEND))
1068 ftrace_filter_reset(enable);
1070 if (file->f_mode & FMODE_READ) {
1071 iter->pg = ftrace_pages_start;
1073 iter->flags = enable ? FTRACE_ITER_FILTER :
1074 FTRACE_ITER_NOTRACE;
1076 ret = seq_open(file, &show_ftrace_seq_ops);
1078 struct seq_file *m = file->private_data;
1083 file->private_data = iter;
1084 mutex_unlock(&ftrace_regex_lock);
1090 ftrace_filter_open(struct inode *inode, struct file *file)
1092 return ftrace_regex_open(inode, file, 1);
1096 ftrace_notrace_open(struct inode *inode, struct file *file)
1098 return ftrace_regex_open(inode, file, 0);
1102 ftrace_regex_read(struct file *file, char __user *ubuf,
1103 size_t cnt, loff_t *ppos)
1105 if (file->f_mode & FMODE_READ)
1106 return seq_read(file, ubuf, cnt, ppos);
1112 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1116 if (file->f_mode & FMODE_READ)
1117 ret = seq_lseek(file, offset, origin);
1119 file->f_pos = ret = 1;
1132 ftrace_match(unsigned char *buff, int len, int enable)
1134 char str[KSYM_SYMBOL_LEN];
1135 char *search = NULL;
1136 struct ftrace_page *pg;
1137 struct dyn_ftrace *rec;
1138 int type = MATCH_FULL;
1139 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1140 unsigned i, match = 0, search_len = 0;
1142 for (i = 0; i < len; i++) {
1143 if (buff[i] == '*') {
1145 search = buff + i + 1;
1146 type = MATCH_END_ONLY;
1147 search_len = len - (i + 1);
1149 if (type == MATCH_END_ONLY) {
1150 type = MATCH_MIDDLE_ONLY;
1153 type = MATCH_FRONT_ONLY;
1161 /* keep kstop machine from running */
1164 ftrace_filtered = 1;
1165 pg = ftrace_pages_start;
1167 for (i = 0; i < pg->index; i++) {
1171 rec = &pg->records[i];
1172 if (rec->flags & FTRACE_FL_FAILED)
1174 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1177 if (strcmp(str, buff) == 0)
1180 case MATCH_FRONT_ONLY:
1181 if (memcmp(str, buff, match) == 0)
1184 case MATCH_MIDDLE_ONLY:
1185 if (strstr(str, search))
1188 case MATCH_END_ONLY:
1189 ptr = strstr(str, search);
1190 if (ptr && (ptr[search_len] == 0))
1203 ftrace_regex_write(struct file *file, const char __user *ubuf,
1204 size_t cnt, loff_t *ppos, int enable)
1206 struct ftrace_iterator *iter;
1211 if (!cnt || cnt < 0)
1214 mutex_lock(&ftrace_regex_lock);
1216 if (file->f_mode & FMODE_READ) {
1217 struct seq_file *m = file->private_data;
1220 iter = file->private_data;
1223 iter->flags &= ~FTRACE_ITER_CONT;
1224 iter->buffer_idx = 0;
1227 ret = get_user(ch, ubuf++);
1233 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1234 /* skip white space */
1235 while (cnt && isspace(ch)) {
1236 ret = get_user(ch, ubuf++);
1244 file->f_pos += read;
1249 iter->buffer_idx = 0;
1252 while (cnt && !isspace(ch)) {
1253 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1254 iter->buffer[iter->buffer_idx++] = ch;
1259 ret = get_user(ch, ubuf++);
1268 iter->buffer[iter->buffer_idx] = 0;
1269 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1270 iter->buffer_idx = 0;
1272 iter->flags |= FTRACE_ITER_CONT;
1275 file->f_pos += read;
1279 mutex_unlock(&ftrace_regex_lock);
1285 ftrace_filter_write(struct file *file, const char __user *ubuf,
1286 size_t cnt, loff_t *ppos)
1288 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1292 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1293 size_t cnt, loff_t *ppos)
1295 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1299 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1301 if (unlikely(ftrace_disabled))
1304 mutex_lock(&ftrace_regex_lock);
1306 ftrace_filter_reset(enable);
1308 ftrace_match(buf, len, enable);
1309 mutex_unlock(&ftrace_regex_lock);
1313 * ftrace_set_filter - set a function to filter on in ftrace
1314 * @buf - the string that holds the function filter text.
1315 * @len - the length of the string.
1316 * @reset - non zero to reset all filters before applying this filter.
1318 * Filters denote which functions should be enabled when tracing is enabled.
1319 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1321 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1323 ftrace_set_regex(buf, len, reset, 1);
1327 * ftrace_set_notrace - set a function to not trace in ftrace
1328 * @buf - the string that holds the function notrace text.
1329 * @len - the length of the string.
1330 * @reset - non zero to reset all filters before applying this filter.
1332 * Notrace Filters denote which functions should not be enabled when tracing
1333 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1336 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1338 ftrace_set_regex(buf, len, reset, 0);
1342 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1344 struct seq_file *m = (struct seq_file *)file->private_data;
1345 struct ftrace_iterator *iter;
1347 mutex_lock(&ftrace_regex_lock);
1348 if (file->f_mode & FMODE_READ) {
1351 seq_release(inode, file);
1353 iter = file->private_data;
1355 if (iter->buffer_idx) {
1357 iter->buffer[iter->buffer_idx] = 0;
1358 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1361 mutex_lock(&ftrace_sysctl_lock);
1362 mutex_lock(&ftraced_lock);
1363 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1364 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1365 mutex_unlock(&ftraced_lock);
1366 mutex_unlock(&ftrace_sysctl_lock);
1369 mutex_unlock(&ftrace_regex_lock);
1374 ftrace_filter_release(struct inode *inode, struct file *file)
1376 return ftrace_regex_release(inode, file, 1);
1380 ftrace_notrace_release(struct inode *inode, struct file *file)
1382 return ftrace_regex_release(inode, file, 0);
1386 ftraced_read(struct file *filp, char __user *ubuf,
1387 size_t cnt, loff_t *ppos)
1389 /* don't worry about races */
1390 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1391 int r = strlen(buf);
1393 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1397 ftraced_write(struct file *filp, const char __user *ubuf,
1398 size_t cnt, loff_t *ppos)
1404 if (cnt >= sizeof(buf))
1407 if (copy_from_user(&buf, ubuf, cnt))
1410 if (strncmp(buf, "enable", 6) == 0)
1412 else if (strncmp(buf, "disable", 7) == 0)
1417 ret = strict_strtoul(buf, 10, &val);
1425 ftrace_enable_daemon();
1427 ftrace_disable_daemon();
1434 static struct file_operations ftrace_avail_fops = {
1435 .open = ftrace_avail_open,
1437 .llseek = seq_lseek,
1438 .release = ftrace_avail_release,
1441 static struct file_operations ftrace_failures_fops = {
1442 .open = ftrace_failures_open,
1444 .llseek = seq_lseek,
1445 .release = ftrace_avail_release,
1448 static struct file_operations ftrace_filter_fops = {
1449 .open = ftrace_filter_open,
1450 .read = ftrace_regex_read,
1451 .write = ftrace_filter_write,
1452 .llseek = ftrace_regex_lseek,
1453 .release = ftrace_filter_release,
1456 static struct file_operations ftrace_notrace_fops = {
1457 .open = ftrace_notrace_open,
1458 .read = ftrace_regex_read,
1459 .write = ftrace_notrace_write,
1460 .llseek = ftrace_regex_lseek,
1461 .release = ftrace_notrace_release,
1464 static struct file_operations ftraced_fops = {
1465 .open = tracing_open_generic,
1466 .read = ftraced_read,
1467 .write = ftraced_write,
1471 * ftrace_force_update - force an update to all recording ftrace functions
1473 int ftrace_force_update(void)
1477 if (unlikely(ftrace_disabled))
1480 mutex_lock(&ftrace_sysctl_lock);
1481 mutex_lock(&ftraced_lock);
1484 * If ftraced_trigger is not set, then there is nothing
1487 if (ftraced_trigger && !ftrace_update_code())
1490 mutex_unlock(&ftraced_lock);
1491 mutex_unlock(&ftrace_sysctl_lock);
1496 static void ftrace_force_shutdown(void)
1498 struct task_struct *task;
1499 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1501 mutex_lock(&ftraced_lock);
1502 task = ftraced_task;
1503 ftraced_task = NULL;
1504 ftraced_suspend = -1;
1505 ftrace_run_update_code(command);
1506 mutex_unlock(&ftraced_lock);
1512 static __init int ftrace_init_debugfs(void)
1514 struct dentry *d_tracer;
1515 struct dentry *entry;
1517 d_tracer = tracing_init_dentry();
1519 entry = debugfs_create_file("available_filter_functions", 0444,
1520 d_tracer, NULL, &ftrace_avail_fops);
1522 pr_warning("Could not create debugfs "
1523 "'available_filter_functions' entry\n");
1525 entry = debugfs_create_file("failures", 0444,
1526 d_tracer, NULL, &ftrace_failures_fops);
1528 pr_warning("Could not create debugfs 'failures' entry\n");
1530 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1531 NULL, &ftrace_filter_fops);
1533 pr_warning("Could not create debugfs "
1534 "'set_ftrace_filter' entry\n");
1536 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1537 NULL, &ftrace_notrace_fops);
1539 pr_warning("Could not create debugfs "
1540 "'set_ftrace_notrace' entry\n");
1542 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1543 NULL, &ftraced_fops);
1545 pr_warning("Could not create debugfs "
1546 "'ftraced_enabled' entry\n");
1550 fs_initcall(ftrace_init_debugfs);
1552 static int __init ftrace_dynamic_init(void)
1554 struct task_struct *p;
1558 addr = (unsigned long)ftrace_record_ip;
1560 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1562 /* ftrace_dyn_arch_init places the return code in addr */
1568 ret = ftrace_dyn_table_alloc();
1572 p = kthread_run(ftraced, NULL, "ftraced");
1578 last_ftrace_enabled = ftrace_enabled = 1;
1584 ftrace_disabled = 1;
1588 core_initcall(ftrace_dynamic_init);
1590 # define ftrace_startup() do { } while (0)
1591 # define ftrace_shutdown() do { } while (0)
1592 # define ftrace_startup_sysctl() do { } while (0)
1593 # define ftrace_shutdown_sysctl() do { } while (0)
1594 # define ftrace_force_shutdown() do { } while (0)
1595 #endif /* CONFIG_DYNAMIC_FTRACE */
1598 * ftrace_kill - totally shutdown ftrace
1600 * This is a safety measure. If something was detected that seems
1601 * wrong, calling this function will keep ftrace from doing
1602 * any more modifications, and updates.
1603 * used when something went wrong.
1605 void ftrace_kill(void)
1607 mutex_lock(&ftrace_sysctl_lock);
1608 ftrace_disabled = 1;
1611 clear_ftrace_function();
1612 mutex_unlock(&ftrace_sysctl_lock);
1614 /* Try to totally disable ftrace */
1615 ftrace_force_shutdown();
1619 * register_ftrace_function - register a function for profiling
1620 * @ops - ops structure that holds the function for profiling.
1622 * Register a function to be called by all functions in the
1625 * Note: @ops->func and all the functions it calls must be labeled
1626 * with "notrace", otherwise it will go into a
1629 int register_ftrace_function(struct ftrace_ops *ops)
1633 if (unlikely(ftrace_disabled))
1636 mutex_lock(&ftrace_sysctl_lock);
1637 ret = __register_ftrace_function(ops);
1639 mutex_unlock(&ftrace_sysctl_lock);
1645 * unregister_ftrace_function - unresgister a function for profiling.
1646 * @ops - ops structure that holds the function to unregister
1648 * Unregister a function that was added to be called by ftrace profiling.
1650 int unregister_ftrace_function(struct ftrace_ops *ops)
1654 mutex_lock(&ftrace_sysctl_lock);
1655 ret = __unregister_ftrace_function(ops);
1657 mutex_unlock(&ftrace_sysctl_lock);
1663 ftrace_enable_sysctl(struct ctl_table *table, int write,
1664 struct file *file, void __user *buffer, size_t *lenp,
1669 if (unlikely(ftrace_disabled))
1672 mutex_lock(&ftrace_sysctl_lock);
1674 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1676 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1679 last_ftrace_enabled = ftrace_enabled;
1681 if (ftrace_enabled) {
1683 ftrace_startup_sysctl();
1685 /* we are starting ftrace again */
1686 if (ftrace_list != &ftrace_list_end) {
1687 if (ftrace_list->next == &ftrace_list_end)
1688 ftrace_trace_function = ftrace_list->func;
1690 ftrace_trace_function = ftrace_list_func;
1694 /* stopping ftrace calls (just send to ftrace_stub) */
1695 ftrace_trace_function = ftrace_stub;
1697 ftrace_shutdown_sysctl();
1701 mutex_unlock(&ftrace_sysctl_lock);