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 not be called from interrupt context */
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 /* should not be called from interrupt context */
119 spin_lock(&ftrace_lock);
122 * If we are removing the last function, then simply point
123 * to the ftrace_stub.
125 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
126 ftrace_trace_function = ftrace_stub;
127 ftrace_list = &ftrace_list_end;
131 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
142 if (ftrace_enabled) {
143 /* If we only have one func left, then call that directly */
144 if (ftrace_list == &ftrace_list_end ||
145 ftrace_list->next == &ftrace_list_end)
146 ftrace_trace_function = ftrace_list->func;
150 spin_unlock(&ftrace_lock);
155 #ifdef CONFIG_DYNAMIC_FTRACE
157 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
159 * The hash lock is only needed when the recording of the mcount
160 * callers are dynamic. That is, by the caller themselves and
161 * not recorded via the compilation.
163 static DEFINE_SPINLOCK(ftrace_hash_lock);
164 #define ftrace_hash_lock(flags) spin_lock_irqsave(&ftrace_hash_lock, flags)
165 #define ftrace_hash_unlock(flags) \
166 spin_unlock_irqrestore(&ftrace_hash_lock, flags)
167 static void ftrace_release_hash(unsigned long start, unsigned long end);
169 /* This is protected via the ftrace_lock with MCOUNT_RECORD. */
170 #define ftrace_hash_lock(flags) do { (void)(flags); } while (0)
171 #define ftrace_hash_unlock(flags) do { } while(0)
172 static inline void ftrace_release_hash(unsigned long start, unsigned long end)
178 * Since MCOUNT_ADDR may point to mcount itself, we do not want
179 * to get it confused by reading a reference in the code as we
180 * are parsing on objcopy output of text. Use a variable for
183 static unsigned long mcount_addr = MCOUNT_ADDR;
185 static struct task_struct *ftraced_task;
188 FTRACE_ENABLE_CALLS = (1 << 0),
189 FTRACE_DISABLE_CALLS = (1 << 1),
190 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
191 FTRACE_ENABLE_MCOUNT = (1 << 3),
192 FTRACE_DISABLE_MCOUNT = (1 << 4),
195 static int ftrace_filtered;
196 static int tracing_on;
197 static int frozen_record_count;
199 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
201 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
203 static DEFINE_MUTEX(ftraced_lock);
204 static DEFINE_MUTEX(ftrace_regex_lock);
207 struct ftrace_page *next;
209 struct dyn_ftrace records[];
212 #define ENTRIES_PER_PAGE \
213 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
215 /* estimate from running different kernels */
216 #define NR_TO_INIT 10000
218 static struct ftrace_page *ftrace_pages_start;
219 static struct ftrace_page *ftrace_pages;
221 static int ftraced_trigger;
222 static int ftraced_suspend;
223 static int ftraced_stop;
225 static int ftrace_record_suspend;
227 static struct dyn_ftrace *ftrace_free_records;
230 #ifdef CONFIG_KPROBES
231 static inline void freeze_record(struct dyn_ftrace *rec)
233 if (!(rec->flags & FTRACE_FL_FROZEN)) {
234 rec->flags |= FTRACE_FL_FROZEN;
235 frozen_record_count++;
239 static inline void unfreeze_record(struct dyn_ftrace *rec)
241 if (rec->flags & FTRACE_FL_FROZEN) {
242 rec->flags &= ~FTRACE_FL_FROZEN;
243 frozen_record_count--;
247 static inline int record_frozen(struct dyn_ftrace *rec)
249 return rec->flags & FTRACE_FL_FROZEN;
252 # define freeze_record(rec) ({ 0; })
253 # define unfreeze_record(rec) ({ 0; })
254 # define record_frozen(rec) ({ 0; })
255 #endif /* CONFIG_KPROBES */
257 int skip_trace(unsigned long ip)
260 struct dyn_ftrace *rec;
261 struct hlist_node *t;
262 struct hlist_head *head;
264 if (frozen_record_count == 0)
267 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
268 hlist_for_each_entry_rcu(rec, t, head, node) {
270 if (record_frozen(rec)) {
271 if (rec->flags & FTRACE_FL_FAILED)
274 if (!(rec->flags & FTRACE_FL_CONVERTED))
277 if (!tracing_on || !ftrace_enabled)
280 if (ftrace_filtered) {
281 fl = rec->flags & (FTRACE_FL_FILTER |
283 if (!fl || (fl & FTRACE_FL_NOTRACE))
295 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
297 struct dyn_ftrace *p;
298 struct hlist_node *t;
301 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
312 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
314 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
317 /* called from kstop_machine */
318 static inline void ftrace_del_hash(struct dyn_ftrace *node)
320 hlist_del(&node->node);
323 static void ftrace_free_rec(struct dyn_ftrace *rec)
325 rec->ip = (unsigned long)ftrace_free_records;
326 ftrace_free_records = rec;
327 rec->flags |= FTRACE_FL_FREE;
330 void ftrace_release(void *start, unsigned long size)
332 struct dyn_ftrace *rec;
333 struct ftrace_page *pg;
334 unsigned long s = (unsigned long)start;
335 unsigned long e = s + size;
338 if (ftrace_disabled || !start)
341 /* should not be called from interrupt context */
342 spin_lock(&ftrace_lock);
344 for (pg = ftrace_pages_start; pg; pg = pg->next) {
345 for (i = 0; i < pg->index; i++) {
346 rec = &pg->records[i];
348 if ((rec->ip >= s) && (rec->ip < e))
349 ftrace_free_rec(rec);
352 spin_unlock(&ftrace_lock);
354 ftrace_release_hash(s, e);
357 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
359 struct dyn_ftrace *rec;
361 /* First check for freed records */
362 if (ftrace_free_records) {
363 rec = ftrace_free_records;
365 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
367 ftrace_free_records = NULL;
373 ftrace_free_records = (void *)rec->ip;
374 memset(rec, 0, sizeof(*rec));
378 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
379 if (!ftrace_pages->next)
381 ftrace_pages = ftrace_pages->next;
384 return &ftrace_pages->records[ftrace_pages->index++];
388 ftrace_record_ip(unsigned long ip)
390 struct dyn_ftrace *node;
396 if (!ftrace_enabled || ftrace_disabled)
399 resched = need_resched();
400 preempt_disable_notrace();
403 * We simply need to protect against recursion.
404 * Use the the raw version of smp_processor_id and not
405 * __get_cpu_var which can call debug hooks that can
406 * cause a recursive crash here.
408 cpu = raw_smp_processor_id();
409 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
410 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
413 if (unlikely(ftrace_record_suspend))
416 key = hash_long(ip, FTRACE_HASHBITS);
418 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
420 if (ftrace_ip_in_hash(ip, key))
423 ftrace_hash_lock(flags);
425 /* This ip may have hit the hash before the lock */
426 if (ftrace_ip_in_hash(ip, key))
429 node = ftrace_alloc_dyn_node(ip);
435 ftrace_add_hash(node, key);
440 ftrace_hash_unlock(flags);
442 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
444 /* prevent recursion with scheduler */
446 preempt_enable_no_resched_notrace();
448 preempt_enable_notrace();
451 #define FTRACE_ADDR ((long)(ftrace_caller))
454 __ftrace_replace_code(struct dyn_ftrace *rec,
455 unsigned char *old, unsigned char *new, int enable)
457 unsigned long ip, fl;
461 if (ftrace_filtered && enable) {
463 * If filtering is on:
465 * If this record is set to be filtered and
466 * is enabled then do nothing.
468 * If this record is set to be filtered and
469 * it is not enabled, enable it.
471 * If this record is not set to be filtered
472 * and it is not enabled do nothing.
474 * If this record is set not to trace then
477 * If this record is set not to trace and
478 * it is enabled then disable it.
480 * If this record is not set to be filtered and
481 * it is enabled, disable it.
484 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
487 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
488 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
489 !fl || (fl == FTRACE_FL_NOTRACE))
493 * If it is enabled disable it,
494 * otherwise enable it!
496 if (fl & FTRACE_FL_ENABLED) {
497 /* swap new and old */
499 old = ftrace_call_replace(ip, FTRACE_ADDR);
500 rec->flags &= ~FTRACE_FL_ENABLED;
502 new = ftrace_call_replace(ip, FTRACE_ADDR);
503 rec->flags |= FTRACE_FL_ENABLED;
509 * If this record is set not to trace and is
510 * not enabled, do nothing.
512 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
513 if (fl == FTRACE_FL_NOTRACE)
516 new = ftrace_call_replace(ip, FTRACE_ADDR);
518 old = ftrace_call_replace(ip, FTRACE_ADDR);
521 if (rec->flags & FTRACE_FL_ENABLED)
523 rec->flags |= FTRACE_FL_ENABLED;
525 if (!(rec->flags & FTRACE_FL_ENABLED))
527 rec->flags &= ~FTRACE_FL_ENABLED;
531 return ftrace_modify_code(ip, old, new);
534 static void ftrace_replace_code(int enable)
537 unsigned char *new = NULL, *old = NULL;
538 struct dyn_ftrace *rec;
539 struct ftrace_page *pg;
542 old = ftrace_nop_replace();
544 new = ftrace_nop_replace();
546 for (pg = ftrace_pages_start; pg; pg = pg->next) {
547 for (i = 0; i < pg->index; i++) {
548 rec = &pg->records[i];
550 /* don't modify code that has already faulted */
551 if (rec->flags & FTRACE_FL_FAILED)
554 /* ignore updates to this record's mcount site */
555 if (get_kprobe((void *)rec->ip)) {
559 unfreeze_record(rec);
562 failed = __ftrace_replace_code(rec, old, new, enable);
563 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
564 rec->flags |= FTRACE_FL_FAILED;
565 if ((system_state == SYSTEM_BOOTING) ||
566 !core_kernel_text(rec->ip)) {
567 ftrace_del_hash(rec);
568 ftrace_free_rec(rec);
575 static void ftrace_shutdown_replenish(void)
577 if (ftrace_pages->next)
580 /* allocate another page */
581 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
584 static void print_ip_ins(const char *fmt, unsigned char *p)
588 printk(KERN_CONT "%s", fmt);
590 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
591 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
595 ftrace_code_disable(struct dyn_ftrace *rec)
598 unsigned char *nop, *call;
603 nop = ftrace_nop_replace();
604 call = ftrace_call_replace(ip, mcount_addr);
606 failed = ftrace_modify_code(ip, call, nop);
611 pr_info("ftrace faulted on modifying ");
616 pr_info("ftrace failed to modify ");
618 print_ip_ins(" expected: ", call);
619 print_ip_ins(" actual: ", (unsigned char *)ip);
620 print_ip_ins(" replace: ", nop);
621 printk(KERN_CONT "\n");
625 rec->flags |= FTRACE_FL_FAILED;
631 static int __ftrace_update_code(void *ignore);
633 static int __ftrace_modify_code(void *data)
638 if (*command & FTRACE_ENABLE_CALLS) {
640 * Update any recorded ips now that we have the
643 __ftrace_update_code(NULL);
644 ftrace_replace_code(1);
646 } else if (*command & FTRACE_DISABLE_CALLS) {
647 ftrace_replace_code(0);
651 if (*command & FTRACE_UPDATE_TRACE_FUNC)
652 ftrace_update_ftrace_func(ftrace_trace_function);
654 if (*command & FTRACE_ENABLE_MCOUNT) {
655 addr = (unsigned long)ftrace_record_ip;
656 ftrace_mcount_set(&addr);
657 } else if (*command & FTRACE_DISABLE_MCOUNT) {
658 addr = (unsigned long)ftrace_stub;
659 ftrace_mcount_set(&addr);
665 static void ftrace_run_update_code(int command)
667 stop_machine(__ftrace_modify_code, &command, NULL);
670 void ftrace_disable_daemon(void)
672 /* Stop the daemon from calling kstop_machine */
673 mutex_lock(&ftraced_lock);
675 mutex_unlock(&ftraced_lock);
677 ftrace_force_update();
680 void ftrace_enable_daemon(void)
682 mutex_lock(&ftraced_lock);
684 mutex_unlock(&ftraced_lock);
686 ftrace_force_update();
689 static ftrace_func_t saved_ftrace_func;
691 static void ftrace_startup(void)
695 if (unlikely(ftrace_disabled))
698 mutex_lock(&ftraced_lock);
700 if (ftraced_suspend == 1)
701 command |= FTRACE_ENABLE_CALLS;
703 if (saved_ftrace_func != ftrace_trace_function) {
704 saved_ftrace_func = ftrace_trace_function;
705 command |= FTRACE_UPDATE_TRACE_FUNC;
708 if (!command || !ftrace_enabled)
711 ftrace_run_update_code(command);
713 mutex_unlock(&ftraced_lock);
716 static void ftrace_shutdown(void)
720 if (unlikely(ftrace_disabled))
723 mutex_lock(&ftraced_lock);
725 if (!ftraced_suspend)
726 command |= FTRACE_DISABLE_CALLS;
728 if (saved_ftrace_func != ftrace_trace_function) {
729 saved_ftrace_func = ftrace_trace_function;
730 command |= FTRACE_UPDATE_TRACE_FUNC;
733 if (!command || !ftrace_enabled)
736 ftrace_run_update_code(command);
738 mutex_unlock(&ftraced_lock);
741 static void ftrace_startup_sysctl(void)
743 int command = FTRACE_ENABLE_MCOUNT;
745 if (unlikely(ftrace_disabled))
748 mutex_lock(&ftraced_lock);
749 /* Force update next time */
750 saved_ftrace_func = NULL;
751 /* ftraced_suspend is true if we want ftrace running */
753 command |= FTRACE_ENABLE_CALLS;
755 ftrace_run_update_code(command);
756 mutex_unlock(&ftraced_lock);
759 static void ftrace_shutdown_sysctl(void)
761 int command = FTRACE_DISABLE_MCOUNT;
763 if (unlikely(ftrace_disabled))
766 mutex_lock(&ftraced_lock);
767 /* ftraced_suspend is true if ftrace is running */
769 command |= FTRACE_DISABLE_CALLS;
771 ftrace_run_update_code(command);
772 mutex_unlock(&ftraced_lock);
775 static cycle_t ftrace_update_time;
776 static unsigned long ftrace_update_cnt;
777 unsigned long ftrace_update_tot_cnt;
779 static int __ftrace_update_code(void *ignore)
781 int i, save_ftrace_enabled;
783 struct dyn_ftrace *p;
784 struct hlist_node *t, *n;
785 struct hlist_head *head, temp_list;
787 /* Don't be recording funcs now */
788 ftrace_record_suspend++;
789 save_ftrace_enabled = ftrace_enabled;
792 start = ftrace_now(raw_smp_processor_id());
793 ftrace_update_cnt = 0;
795 /* No locks needed, the machine is stopped! */
796 for (i = 0; i < FTRACE_HASHSIZE; i++) {
797 INIT_HLIST_HEAD(&temp_list);
798 head = &ftrace_hash[i];
800 /* all CPUS are stopped, we are safe to modify code */
801 hlist_for_each_entry_safe(p, t, n, head, node) {
802 /* Skip over failed records which have not been
804 if (p->flags & FTRACE_FL_FAILED)
807 /* Unconverted records are always at the head of the
808 * hash bucket. Once we encounter a converted record,
809 * simply skip over to the next bucket. Saves ftraced
810 * some processor cycles (ftrace does its bid for
811 * global warming :-p ). */
812 if (p->flags & (FTRACE_FL_CONVERTED))
815 /* Ignore updates to this record's mcount site.
816 * Reintroduce this record at the head of this
817 * bucket to attempt to "convert" it again if
818 * the kprobe on it is unregistered before the
820 if (get_kprobe((void *)p->ip)) {
822 INIT_HLIST_NODE(&p->node);
823 hlist_add_head(&p->node, &temp_list);
830 /* convert record (i.e, patch mcount-call with NOP) */
831 if (ftrace_code_disable(p)) {
832 p->flags |= FTRACE_FL_CONVERTED;
835 if ((system_state == SYSTEM_BOOTING) ||
836 !core_kernel_text(p->ip)) {
843 hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
845 INIT_HLIST_NODE(&p->node);
846 hlist_add_head(&p->node, head);
850 stop = ftrace_now(raw_smp_processor_id());
851 ftrace_update_time = stop - start;
852 ftrace_update_tot_cnt += ftrace_update_cnt;
855 ftrace_enabled = save_ftrace_enabled;
856 ftrace_record_suspend--;
861 static int ftrace_update_code(void)
863 if (unlikely(ftrace_disabled) ||
864 !ftrace_enabled || !ftraced_trigger)
867 stop_machine(__ftrace_update_code, NULL, NULL);
872 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
874 struct ftrace_page *pg;
878 /* allocate a few pages */
879 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
880 if (!ftrace_pages_start)
884 * Allocate a few more pages.
886 * TODO: have some parser search vmlinux before
887 * final linking to find all calls to ftrace.
889 * a) know how many pages to allocate.
891 * b) set up the table then.
893 * The dynamic code is still necessary for
897 pg = ftrace_pages = ftrace_pages_start;
899 cnt = num_to_init / ENTRIES_PER_PAGE;
900 pr_info("ftrace: allocating %ld hash entries in %d pages\n",
903 for (i = 0; i < cnt; i++) {
904 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
906 /* If we fail, we'll try later anyway */
917 FTRACE_ITER_FILTER = (1 << 0),
918 FTRACE_ITER_CONT = (1 << 1),
919 FTRACE_ITER_NOTRACE = (1 << 2),
920 FTRACE_ITER_FAILURES = (1 << 3),
923 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
925 struct ftrace_iterator {
927 struct ftrace_page *pg;
930 unsigned char buffer[FTRACE_BUFF_MAX+1];
936 t_next(struct seq_file *m, void *v, loff_t *pos)
938 struct ftrace_iterator *iter = m->private;
939 struct dyn_ftrace *rec = NULL;
943 /* should not be called from interrupt context */
944 spin_lock(&ftrace_lock);
946 if (iter->idx >= iter->pg->index) {
947 if (iter->pg->next) {
948 iter->pg = iter->pg->next;
953 rec = &iter->pg->records[iter->idx++];
954 if ((rec->flags & FTRACE_FL_FREE) ||
956 (!(iter->flags & FTRACE_ITER_FAILURES) &&
957 (rec->flags & FTRACE_FL_FAILED)) ||
959 ((iter->flags & FTRACE_ITER_FAILURES) &&
960 !(rec->flags & FTRACE_FL_FAILED)) ||
962 ((iter->flags & FTRACE_ITER_NOTRACE) &&
963 !(rec->flags & FTRACE_FL_NOTRACE))) {
968 spin_unlock(&ftrace_lock);
975 static void *t_start(struct seq_file *m, loff_t *pos)
977 struct ftrace_iterator *iter = m->private;
981 if (*pos != iter->pos) {
982 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
986 p = t_next(m, p, &l);
992 static void t_stop(struct seq_file *m, void *p)
996 static int t_show(struct seq_file *m, void *v)
998 struct dyn_ftrace *rec = v;
999 char str[KSYM_SYMBOL_LEN];
1004 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1006 seq_printf(m, "%s\n", str);
1011 static struct seq_operations show_ftrace_seq_ops = {
1019 ftrace_avail_open(struct inode *inode, struct file *file)
1021 struct ftrace_iterator *iter;
1024 if (unlikely(ftrace_disabled))
1027 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1031 iter->pg = ftrace_pages_start;
1034 ret = seq_open(file, &show_ftrace_seq_ops);
1036 struct seq_file *m = file->private_data;
1046 int ftrace_avail_release(struct inode *inode, struct file *file)
1048 struct seq_file *m = (struct seq_file *)file->private_data;
1049 struct ftrace_iterator *iter = m->private;
1051 seq_release(inode, file);
1058 ftrace_failures_open(struct inode *inode, struct file *file)
1062 struct ftrace_iterator *iter;
1064 ret = ftrace_avail_open(inode, file);
1066 m = (struct seq_file *)file->private_data;
1067 iter = (struct ftrace_iterator *)m->private;
1068 iter->flags = FTRACE_ITER_FAILURES;
1075 static void ftrace_filter_reset(int enable)
1077 struct ftrace_page *pg;
1078 struct dyn_ftrace *rec;
1079 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1082 /* should not be called from interrupt context */
1083 spin_lock(&ftrace_lock);
1085 ftrace_filtered = 0;
1086 pg = ftrace_pages_start;
1088 for (i = 0; i < pg->index; i++) {
1089 rec = &pg->records[i];
1090 if (rec->flags & FTRACE_FL_FAILED)
1092 rec->flags &= ~type;
1096 spin_unlock(&ftrace_lock);
1100 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1102 struct ftrace_iterator *iter;
1105 if (unlikely(ftrace_disabled))
1108 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1112 mutex_lock(&ftrace_regex_lock);
1113 if ((file->f_mode & FMODE_WRITE) &&
1114 !(file->f_flags & O_APPEND))
1115 ftrace_filter_reset(enable);
1117 if (file->f_mode & FMODE_READ) {
1118 iter->pg = ftrace_pages_start;
1120 iter->flags = enable ? FTRACE_ITER_FILTER :
1121 FTRACE_ITER_NOTRACE;
1123 ret = seq_open(file, &show_ftrace_seq_ops);
1125 struct seq_file *m = file->private_data;
1130 file->private_data = iter;
1131 mutex_unlock(&ftrace_regex_lock);
1137 ftrace_filter_open(struct inode *inode, struct file *file)
1139 return ftrace_regex_open(inode, file, 1);
1143 ftrace_notrace_open(struct inode *inode, struct file *file)
1145 return ftrace_regex_open(inode, file, 0);
1149 ftrace_regex_read(struct file *file, char __user *ubuf,
1150 size_t cnt, loff_t *ppos)
1152 if (file->f_mode & FMODE_READ)
1153 return seq_read(file, ubuf, cnt, ppos);
1159 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1163 if (file->f_mode & FMODE_READ)
1164 ret = seq_lseek(file, offset, origin);
1166 file->f_pos = ret = 1;
1179 ftrace_match(unsigned char *buff, int len, int enable)
1181 char str[KSYM_SYMBOL_LEN];
1182 char *search = NULL;
1183 struct ftrace_page *pg;
1184 struct dyn_ftrace *rec;
1185 int type = MATCH_FULL;
1186 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1187 unsigned i, match = 0, search_len = 0;
1189 for (i = 0; i < len; i++) {
1190 if (buff[i] == '*') {
1192 search = buff + i + 1;
1193 type = MATCH_END_ONLY;
1194 search_len = len - (i + 1);
1196 if (type == MATCH_END_ONLY) {
1197 type = MATCH_MIDDLE_ONLY;
1200 type = MATCH_FRONT_ONLY;
1208 /* should not be called from interrupt context */
1209 spin_lock(&ftrace_lock);
1211 ftrace_filtered = 1;
1212 pg = ftrace_pages_start;
1214 for (i = 0; i < pg->index; i++) {
1218 rec = &pg->records[i];
1219 if (rec->flags & FTRACE_FL_FAILED)
1221 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1224 if (strcmp(str, buff) == 0)
1227 case MATCH_FRONT_ONLY:
1228 if (memcmp(str, buff, match) == 0)
1231 case MATCH_MIDDLE_ONLY:
1232 if (strstr(str, search))
1235 case MATCH_END_ONLY:
1236 ptr = strstr(str, search);
1237 if (ptr && (ptr[search_len] == 0))
1246 spin_unlock(&ftrace_lock);
1250 ftrace_regex_write(struct file *file, const char __user *ubuf,
1251 size_t cnt, loff_t *ppos, int enable)
1253 struct ftrace_iterator *iter;
1258 if (!cnt || cnt < 0)
1261 mutex_lock(&ftrace_regex_lock);
1263 if (file->f_mode & FMODE_READ) {
1264 struct seq_file *m = file->private_data;
1267 iter = file->private_data;
1270 iter->flags &= ~FTRACE_ITER_CONT;
1271 iter->buffer_idx = 0;
1274 ret = get_user(ch, ubuf++);
1280 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1281 /* skip white space */
1282 while (cnt && isspace(ch)) {
1283 ret = get_user(ch, ubuf++);
1291 file->f_pos += read;
1296 iter->buffer_idx = 0;
1299 while (cnt && !isspace(ch)) {
1300 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1301 iter->buffer[iter->buffer_idx++] = ch;
1306 ret = get_user(ch, ubuf++);
1315 iter->buffer[iter->buffer_idx] = 0;
1316 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1317 iter->buffer_idx = 0;
1319 iter->flags |= FTRACE_ITER_CONT;
1322 file->f_pos += read;
1326 mutex_unlock(&ftrace_regex_lock);
1332 ftrace_filter_write(struct file *file, const char __user *ubuf,
1333 size_t cnt, loff_t *ppos)
1335 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1339 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1340 size_t cnt, loff_t *ppos)
1342 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1346 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1348 if (unlikely(ftrace_disabled))
1351 mutex_lock(&ftrace_regex_lock);
1353 ftrace_filter_reset(enable);
1355 ftrace_match(buf, len, enable);
1356 mutex_unlock(&ftrace_regex_lock);
1360 * ftrace_set_filter - set a function to filter on in ftrace
1361 * @buf - the string that holds the function filter text.
1362 * @len - the length of the string.
1363 * @reset - non zero to reset all filters before applying this filter.
1365 * Filters denote which functions should be enabled when tracing is enabled.
1366 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1368 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1370 ftrace_set_regex(buf, len, reset, 1);
1374 * ftrace_set_notrace - set a function to not trace in ftrace
1375 * @buf - the string that holds the function notrace text.
1376 * @len - the length of the string.
1377 * @reset - non zero to reset all filters before applying this filter.
1379 * Notrace Filters denote which functions should not be enabled when tracing
1380 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1383 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1385 ftrace_set_regex(buf, len, reset, 0);
1389 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1391 struct seq_file *m = (struct seq_file *)file->private_data;
1392 struct ftrace_iterator *iter;
1394 mutex_lock(&ftrace_regex_lock);
1395 if (file->f_mode & FMODE_READ) {
1398 seq_release(inode, file);
1400 iter = file->private_data;
1402 if (iter->buffer_idx) {
1404 iter->buffer[iter->buffer_idx] = 0;
1405 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1408 mutex_lock(&ftrace_sysctl_lock);
1409 mutex_lock(&ftraced_lock);
1410 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1411 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1412 mutex_unlock(&ftraced_lock);
1413 mutex_unlock(&ftrace_sysctl_lock);
1416 mutex_unlock(&ftrace_regex_lock);
1421 ftrace_filter_release(struct inode *inode, struct file *file)
1423 return ftrace_regex_release(inode, file, 1);
1427 ftrace_notrace_release(struct inode *inode, struct file *file)
1429 return ftrace_regex_release(inode, file, 0);
1433 ftraced_read(struct file *filp, char __user *ubuf,
1434 size_t cnt, loff_t *ppos)
1436 /* don't worry about races */
1437 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1438 int r = strlen(buf);
1440 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1444 ftraced_write(struct file *filp, const char __user *ubuf,
1445 size_t cnt, loff_t *ppos)
1451 if (cnt >= sizeof(buf))
1454 if (copy_from_user(&buf, ubuf, cnt))
1457 if (strncmp(buf, "enable", 6) == 0)
1459 else if (strncmp(buf, "disable", 7) == 0)
1464 ret = strict_strtoul(buf, 10, &val);
1472 ftrace_enable_daemon();
1474 ftrace_disable_daemon();
1481 static struct file_operations ftrace_avail_fops = {
1482 .open = ftrace_avail_open,
1484 .llseek = seq_lseek,
1485 .release = ftrace_avail_release,
1488 static struct file_operations ftrace_failures_fops = {
1489 .open = ftrace_failures_open,
1491 .llseek = seq_lseek,
1492 .release = ftrace_avail_release,
1495 static struct file_operations ftrace_filter_fops = {
1496 .open = ftrace_filter_open,
1497 .read = ftrace_regex_read,
1498 .write = ftrace_filter_write,
1499 .llseek = ftrace_regex_lseek,
1500 .release = ftrace_filter_release,
1503 static struct file_operations ftrace_notrace_fops = {
1504 .open = ftrace_notrace_open,
1505 .read = ftrace_regex_read,
1506 .write = ftrace_notrace_write,
1507 .llseek = ftrace_regex_lseek,
1508 .release = ftrace_notrace_release,
1511 static struct file_operations ftraced_fops = {
1512 .open = tracing_open_generic,
1513 .read = ftraced_read,
1514 .write = ftraced_write,
1518 * ftrace_force_update - force an update to all recording ftrace functions
1520 int ftrace_force_update(void)
1524 if (unlikely(ftrace_disabled))
1527 mutex_lock(&ftrace_sysctl_lock);
1528 mutex_lock(&ftraced_lock);
1531 * If ftraced_trigger is not set, then there is nothing
1534 if (ftraced_trigger && !ftrace_update_code())
1537 mutex_unlock(&ftraced_lock);
1538 mutex_unlock(&ftrace_sysctl_lock);
1543 static void ftrace_force_shutdown(void)
1545 struct task_struct *task;
1546 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1548 mutex_lock(&ftraced_lock);
1549 task = ftraced_task;
1550 ftraced_task = NULL;
1551 ftraced_suspend = -1;
1552 ftrace_run_update_code(command);
1553 mutex_unlock(&ftraced_lock);
1559 static __init int ftrace_init_debugfs(void)
1561 struct dentry *d_tracer;
1562 struct dentry *entry;
1564 d_tracer = tracing_init_dentry();
1566 entry = debugfs_create_file("available_filter_functions", 0444,
1567 d_tracer, NULL, &ftrace_avail_fops);
1569 pr_warning("Could not create debugfs "
1570 "'available_filter_functions' entry\n");
1572 entry = debugfs_create_file("failures", 0444,
1573 d_tracer, NULL, &ftrace_failures_fops);
1575 pr_warning("Could not create debugfs 'failures' entry\n");
1577 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1578 NULL, &ftrace_filter_fops);
1580 pr_warning("Could not create debugfs "
1581 "'set_ftrace_filter' entry\n");
1583 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1584 NULL, &ftrace_notrace_fops);
1586 pr_warning("Could not create debugfs "
1587 "'set_ftrace_notrace' entry\n");
1589 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1590 NULL, &ftraced_fops);
1592 pr_warning("Could not create debugfs "
1593 "'ftraced_enabled' entry\n");
1597 fs_initcall(ftrace_init_debugfs);
1599 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
1600 static int ftrace_convert_nops(unsigned long *start,
1605 unsigned long flags;
1609 addr = ftrace_call_adjust(*p++);
1610 /* should not be called from interrupt context */
1611 spin_lock(&ftrace_lock);
1612 ftrace_record_ip(addr);
1613 spin_unlock(&ftrace_lock);
1614 ftrace_shutdown_replenish();
1618 local_irq_save(flags);
1619 __ftrace_update_code(p);
1620 local_irq_restore(flags);
1625 void ftrace_init_module(unsigned long *start, unsigned long *end)
1627 if (ftrace_disabled || start == end)
1629 ftrace_convert_nops(start, end);
1632 extern unsigned long __start_mcount_loc[];
1633 extern unsigned long __stop_mcount_loc[];
1635 void __init ftrace_init(void)
1637 unsigned long count, addr, flags;
1640 /* Keep the ftrace pointer to the stub */
1641 addr = (unsigned long)ftrace_stub;
1643 local_irq_save(flags);
1644 ftrace_dyn_arch_init(&addr);
1645 local_irq_restore(flags);
1647 /* ftrace_dyn_arch_init places the return code in addr */
1651 count = __stop_mcount_loc - __start_mcount_loc;
1653 ret = ftrace_dyn_table_alloc(count);
1657 last_ftrace_enabled = ftrace_enabled = 1;
1659 ret = ftrace_convert_nops(__start_mcount_loc,
1664 ftrace_disabled = 1;
1666 #else /* CONFIG_FTRACE_MCOUNT_RECORD */
1668 static void ftrace_release_hash(unsigned long start, unsigned long end)
1670 struct dyn_ftrace *rec;
1671 struct hlist_node *t, *n;
1672 struct hlist_head *head, temp_list;
1673 unsigned long flags;
1676 preempt_disable_notrace();
1678 /* disable incase we call something that calls mcount */
1679 cpu = raw_smp_processor_id();
1680 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
1682 ftrace_hash_lock(flags);
1684 for (i = 0; i < FTRACE_HASHSIZE; i++) {
1685 INIT_HLIST_HEAD(&temp_list);
1686 head = &ftrace_hash[i];
1688 /* all CPUS are stopped, we are safe to modify code */
1689 hlist_for_each_entry_safe(rec, t, n, head, node) {
1690 if (rec->flags & FTRACE_FL_FREE)
1693 if ((rec->ip >= start) && (rec->ip < end))
1694 ftrace_free_rec(rec);
1698 ftrace_hash_unlock(flags);
1700 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
1701 preempt_enable_notrace();
1705 static int ftraced(void *ignore)
1707 unsigned long usecs;
1709 while (!kthread_should_stop()) {
1711 set_current_state(TASK_INTERRUPTIBLE);
1713 /* check once a second */
1714 schedule_timeout(HZ);
1716 if (unlikely(ftrace_disabled))
1719 mutex_lock(&ftrace_sysctl_lock);
1720 mutex_lock(&ftraced_lock);
1721 if (!ftraced_suspend && !ftraced_stop &&
1722 ftrace_update_code()) {
1723 usecs = nsecs_to_usecs(ftrace_update_time);
1724 if (ftrace_update_tot_cnt > 100000) {
1725 ftrace_update_tot_cnt = 0;
1726 pr_info("hm, dftrace overflow: %lu change%s"
1727 " (%lu total) in %lu usec%s\n",
1729 ftrace_update_cnt != 1 ? "s" : "",
1730 ftrace_update_tot_cnt,
1731 usecs, usecs != 1 ? "s" : "");
1732 ftrace_disabled = 1;
1736 mutex_unlock(&ftraced_lock);
1737 mutex_unlock(&ftrace_sysctl_lock);
1739 ftrace_shutdown_replenish();
1741 __set_current_state(TASK_RUNNING);
1745 static int __init ftrace_dynamic_init(void)
1747 struct task_struct *p;
1751 addr = (unsigned long)ftrace_record_ip;
1753 stop_machine(ftrace_dyn_arch_init, &addr, NULL);
1755 /* ftrace_dyn_arch_init places the return code in addr */
1761 ret = ftrace_dyn_table_alloc(NR_TO_INIT);
1765 p = kthread_run(ftraced, NULL, "ftraced");
1771 last_ftrace_enabled = ftrace_enabled = 1;
1777 ftrace_disabled = 1;
1781 core_initcall(ftrace_dynamic_init);
1782 #endif /* CONFIG_FTRACE_MCOUNT_RECORD */
1785 # define ftrace_startup() do { } while (0)
1786 # define ftrace_shutdown() do { } while (0)
1787 # define ftrace_startup_sysctl() do { } while (0)
1788 # define ftrace_shutdown_sysctl() do { } while (0)
1789 # define ftrace_force_shutdown() do { } while (0)
1790 #endif /* CONFIG_DYNAMIC_FTRACE */
1793 * ftrace_kill_atomic - kill ftrace from critical sections
1795 * This function should be used by panic code. It stops ftrace
1796 * but in a not so nice way. If you need to simply kill ftrace
1797 * from a non-atomic section, use ftrace_kill.
1799 void ftrace_kill_atomic(void)
1801 ftrace_disabled = 1;
1803 #ifdef CONFIG_DYNAMIC_FTRACE
1804 ftraced_suspend = -1;
1806 clear_ftrace_function();
1810 * ftrace_kill - totally shutdown ftrace
1812 * This is a safety measure. If something was detected that seems
1813 * wrong, calling this function will keep ftrace from doing
1814 * any more modifications, and updates.
1815 * used when something went wrong.
1817 void ftrace_kill(void)
1819 mutex_lock(&ftrace_sysctl_lock);
1820 ftrace_disabled = 1;
1823 clear_ftrace_function();
1824 mutex_unlock(&ftrace_sysctl_lock);
1826 /* Try to totally disable ftrace */
1827 ftrace_force_shutdown();
1831 * register_ftrace_function - register a function for profiling
1832 * @ops - ops structure that holds the function for profiling.
1834 * Register a function to be called by all functions in the
1837 * Note: @ops->func and all the functions it calls must be labeled
1838 * with "notrace", otherwise it will go into a
1841 int register_ftrace_function(struct ftrace_ops *ops)
1845 if (unlikely(ftrace_disabled))
1848 mutex_lock(&ftrace_sysctl_lock);
1849 ret = __register_ftrace_function(ops);
1851 mutex_unlock(&ftrace_sysctl_lock);
1857 * unregister_ftrace_function - unresgister a function for profiling.
1858 * @ops - ops structure that holds the function to unregister
1860 * Unregister a function that was added to be called by ftrace profiling.
1862 int unregister_ftrace_function(struct ftrace_ops *ops)
1866 mutex_lock(&ftrace_sysctl_lock);
1867 ret = __unregister_ftrace_function(ops);
1869 mutex_unlock(&ftrace_sysctl_lock);
1875 ftrace_enable_sysctl(struct ctl_table *table, int write,
1876 struct file *file, void __user *buffer, size_t *lenp,
1881 if (unlikely(ftrace_disabled))
1884 mutex_lock(&ftrace_sysctl_lock);
1886 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1888 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1891 last_ftrace_enabled = ftrace_enabled;
1893 if (ftrace_enabled) {
1895 ftrace_startup_sysctl();
1897 /* we are starting ftrace again */
1898 if (ftrace_list != &ftrace_list_end) {
1899 if (ftrace_list->next == &ftrace_list_end)
1900 ftrace_trace_function = ftrace_list->func;
1902 ftrace_trace_function = ftrace_list_func;
1906 /* stopping ftrace calls (just send to ftrace_stub) */
1907 ftrace_trace_function = ftrace_stub;
1909 ftrace_shutdown_sysctl();
1913 mutex_unlock(&ftrace_sysctl_lock);