2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugfs.h>
21 #include <linux/hardirq.h>
22 #include <linux/kthread.h>
23 #include <linux/uaccess.h>
24 #include <linux/ftrace.h>
25 #include <linux/sysctl.h>
26 #include <linux/ctype.h>
27 #include <linux/hash.h>
28 #include <linux/list.h>
30 #include <asm/ftrace.h>
34 /* ftrace_enabled is a method to turn ftrace on or off */
35 int ftrace_enabled __read_mostly;
36 static int last_ftrace_enabled;
39 * ftrace_disabled is set when an anomaly is discovered.
40 * ftrace_disabled is much stronger than ftrace_enabled.
42 static int ftrace_disabled __read_mostly;
44 static DEFINE_SPINLOCK(ftrace_lock);
45 static DEFINE_MUTEX(ftrace_sysctl_lock);
47 static struct ftrace_ops ftrace_list_end __read_mostly =
52 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
53 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
55 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
57 struct ftrace_ops *op = ftrace_list;
59 /* in case someone actually ports this to alpha! */
60 read_barrier_depends();
62 while (op != &ftrace_list_end) {
64 read_barrier_depends();
65 op->func(ip, parent_ip);
71 * clear_ftrace_function - reset the ftrace function
73 * This NULLs the ftrace function and in essence stops
74 * tracing. There may be lag
76 void clear_ftrace_function(void)
78 ftrace_trace_function = ftrace_stub;
81 static int __register_ftrace_function(struct ftrace_ops *ops)
83 /* Should never be called by interrupts */
84 spin_lock(&ftrace_lock);
86 ops->next = ftrace_list;
88 * We are entering ops into the ftrace_list but another
89 * CPU might be walking that list. We need to make sure
90 * the ops->next pointer is valid before another CPU sees
91 * the ops pointer included into the ftrace_list.
98 * For one func, simply call it directly.
99 * For more than one func, call the chain.
101 if (ops->next == &ftrace_list_end)
102 ftrace_trace_function = ops->func;
104 ftrace_trace_function = ftrace_list_func;
107 spin_unlock(&ftrace_lock);
112 static int __unregister_ftrace_function(struct ftrace_ops *ops)
114 struct ftrace_ops **p;
117 spin_lock(&ftrace_lock);
120 * If we are removing the last function, then simply point
121 * to the ftrace_stub.
123 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
124 ftrace_trace_function = ftrace_stub;
125 ftrace_list = &ftrace_list_end;
129 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
140 if (ftrace_enabled) {
141 /* If we only have one func left, then call that directly */
142 if (ftrace_list == &ftrace_list_end ||
143 ftrace_list->next == &ftrace_list_end)
144 ftrace_trace_function = ftrace_list->func;
148 spin_unlock(&ftrace_lock);
153 #ifdef CONFIG_DYNAMIC_FTRACE
155 static struct task_struct *ftraced_task;
158 FTRACE_ENABLE_CALLS = (1 << 0),
159 FTRACE_DISABLE_CALLS = (1 << 1),
160 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
161 FTRACE_ENABLE_MCOUNT = (1 << 3),
162 FTRACE_DISABLE_MCOUNT = (1 << 4),
165 static int ftrace_filtered;
166 static int tracing_on;
167 static int frozen_record_count;
169 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
171 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
173 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
174 static DEFINE_MUTEX(ftraced_lock);
175 static DEFINE_MUTEX(ftrace_regex_lock);
178 struct ftrace_page *next;
180 struct dyn_ftrace records[];
183 #define ENTRIES_PER_PAGE \
184 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
186 /* estimate from running different kernels */
187 #define NR_TO_INIT 10000
189 static struct ftrace_page *ftrace_pages_start;
190 static struct ftrace_page *ftrace_pages;
192 static int ftraced_trigger;
193 static int ftraced_suspend;
194 static int ftraced_stop;
196 static int ftrace_record_suspend;
198 static struct dyn_ftrace *ftrace_free_records;
201 #ifdef CONFIG_KPROBES
202 static inline void freeze_record(struct dyn_ftrace *rec)
204 if (!(rec->flags & FTRACE_FL_FROZEN)) {
205 rec->flags |= FTRACE_FL_FROZEN;
206 frozen_record_count++;
210 static inline void unfreeze_record(struct dyn_ftrace *rec)
212 if (rec->flags & FTRACE_FL_FROZEN) {
213 rec->flags &= ~FTRACE_FL_FROZEN;
214 frozen_record_count--;
218 static inline int record_frozen(struct dyn_ftrace *rec)
220 return rec->flags & FTRACE_FL_FROZEN;
223 # define freeze_record(rec) ({ 0; })
224 # define unfreeze_record(rec) ({ 0; })
225 # define record_frozen(rec) ({ 0; })
226 #endif /* CONFIG_KPROBES */
228 int skip_trace(unsigned long ip)
231 struct dyn_ftrace *rec;
232 struct hlist_node *t;
233 struct hlist_head *head;
235 if (frozen_record_count == 0)
238 head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
239 hlist_for_each_entry_rcu(rec, t, head, node) {
241 if (record_frozen(rec)) {
242 if (rec->flags & FTRACE_FL_FAILED)
245 if (!(rec->flags & FTRACE_FL_CONVERTED))
248 if (!tracing_on || !ftrace_enabled)
251 if (ftrace_filtered) {
252 fl = rec->flags & (FTRACE_FL_FILTER |
254 if (!fl || (fl & FTRACE_FL_NOTRACE))
266 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
268 struct dyn_ftrace *p;
269 struct hlist_node *t;
272 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
283 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
285 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
288 /* called from kstop_machine */
289 static inline void ftrace_del_hash(struct dyn_ftrace *node)
291 hlist_del(&node->node);
294 static void ftrace_free_rec(struct dyn_ftrace *rec)
296 /* no locking, only called from kstop_machine */
298 rec->ip = (unsigned long)ftrace_free_records;
299 ftrace_free_records = rec;
300 rec->flags |= FTRACE_FL_FREE;
303 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
305 struct dyn_ftrace *rec;
307 /* First check for freed records */
308 if (ftrace_free_records) {
309 rec = ftrace_free_records;
311 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
313 ftrace_free_records = NULL;
319 ftrace_free_records = (void *)rec->ip;
320 memset(rec, 0, sizeof(*rec));
324 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
325 if (!ftrace_pages->next)
327 ftrace_pages = ftrace_pages->next;
330 return &ftrace_pages->records[ftrace_pages->index++];
334 ftrace_record_ip(unsigned long ip)
336 struct dyn_ftrace *node;
343 if (!ftrace_enabled || ftrace_disabled)
346 resched = need_resched();
347 preempt_disable_notrace();
350 * We simply need to protect against recursion.
351 * Use the the raw version of smp_processor_id and not
352 * __get_cpu_var which can call debug hooks that can
353 * cause a recursive crash here.
355 cpu = raw_smp_processor_id();
356 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
357 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
360 if (unlikely(ftrace_record_suspend))
363 key = hash_long(ip, FTRACE_HASHBITS);
365 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
367 if (ftrace_ip_in_hash(ip, key))
370 atomic = irqs_disabled();
372 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
374 /* This ip may have hit the hash before the lock */
375 if (ftrace_ip_in_hash(ip, key))
378 node = ftrace_alloc_dyn_node(ip);
384 ftrace_add_hash(node, key);
389 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
391 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
393 /* prevent recursion with scheduler */
395 preempt_enable_no_resched_notrace();
397 preempt_enable_notrace();
400 #define FTRACE_ADDR ((long)(ftrace_caller))
403 __ftrace_replace_code(struct dyn_ftrace *rec,
404 unsigned char *old, unsigned char *new, int enable)
406 unsigned long ip, fl;
410 if (ftrace_filtered && enable) {
412 * If filtering is on:
414 * If this record is set to be filtered and
415 * is enabled then do nothing.
417 * If this record is set to be filtered and
418 * it is not enabled, enable it.
420 * If this record is not set to be filtered
421 * and it is not enabled do nothing.
423 * If this record is set not to trace then
426 * If this record is set not to trace and
427 * it is enabled then disable it.
429 * If this record is not set to be filtered and
430 * it is enabled, disable it.
433 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
436 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
437 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
438 !fl || (fl == FTRACE_FL_NOTRACE))
442 * If it is enabled disable it,
443 * otherwise enable it!
445 if (fl & FTRACE_FL_ENABLED) {
446 /* swap new and old */
448 old = ftrace_call_replace(ip, FTRACE_ADDR);
449 rec->flags &= ~FTRACE_FL_ENABLED;
451 new = ftrace_call_replace(ip, FTRACE_ADDR);
452 rec->flags |= FTRACE_FL_ENABLED;
458 * If this record is set not to trace and is
459 * not enabled, do nothing.
461 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
462 if (fl == FTRACE_FL_NOTRACE)
465 new = ftrace_call_replace(ip, FTRACE_ADDR);
467 old = ftrace_call_replace(ip, FTRACE_ADDR);
470 if (rec->flags & FTRACE_FL_ENABLED)
472 rec->flags |= FTRACE_FL_ENABLED;
474 if (!(rec->flags & FTRACE_FL_ENABLED))
476 rec->flags &= ~FTRACE_FL_ENABLED;
480 return ftrace_modify_code(ip, old, new);
483 static void ftrace_replace_code(int enable)
486 unsigned char *new = NULL, *old = NULL;
487 struct dyn_ftrace *rec;
488 struct ftrace_page *pg;
491 old = ftrace_nop_replace();
493 new = ftrace_nop_replace();
495 for (pg = ftrace_pages_start; pg; pg = pg->next) {
496 for (i = 0; i < pg->index; i++) {
497 rec = &pg->records[i];
499 /* don't modify code that has already faulted */
500 if (rec->flags & FTRACE_FL_FAILED)
503 failed = __ftrace_replace_code(rec, old, new, enable);
504 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
505 rec->flags |= FTRACE_FL_FAILED;
506 if ((system_state == SYSTEM_BOOTING) ||
507 !core_kernel_text(rec->ip)) {
508 ftrace_del_hash(rec);
509 ftrace_free_rec(rec);
516 static void ftrace_shutdown_replenish(void)
518 if (ftrace_pages->next)
521 /* allocate another page */
522 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
526 ftrace_code_disable(struct dyn_ftrace *rec)
529 unsigned char *nop, *call;
534 nop = ftrace_nop_replace();
535 call = ftrace_call_replace(ip, MCOUNT_ADDR);
537 failed = ftrace_modify_code(ip, call, nop);
539 rec->flags |= FTRACE_FL_FAILED;
545 static int __ftrace_update_code(void *ignore);
547 static int __ftrace_modify_code(void *data)
552 if (*command & FTRACE_ENABLE_CALLS) {
554 * Update any recorded ips now that we have the
557 __ftrace_update_code(NULL);
558 ftrace_replace_code(1);
560 } else if (*command & FTRACE_DISABLE_CALLS) {
561 ftrace_replace_code(0);
565 if (*command & FTRACE_UPDATE_TRACE_FUNC)
566 ftrace_update_ftrace_func(ftrace_trace_function);
568 if (*command & FTRACE_ENABLE_MCOUNT) {
569 addr = (unsigned long)ftrace_record_ip;
570 ftrace_mcount_set(&addr);
571 } else if (*command & FTRACE_DISABLE_MCOUNT) {
572 addr = (unsigned long)ftrace_stub;
573 ftrace_mcount_set(&addr);
579 static void ftrace_run_update_code(int command)
581 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
584 void ftrace_disable_daemon(void)
586 /* Stop the daemon from calling kstop_machine */
587 mutex_lock(&ftraced_lock);
589 mutex_unlock(&ftraced_lock);
591 ftrace_force_update();
594 void ftrace_enable_daemon(void)
596 mutex_lock(&ftraced_lock);
598 mutex_unlock(&ftraced_lock);
600 ftrace_force_update();
603 static ftrace_func_t saved_ftrace_func;
605 static void ftrace_startup(void)
609 if (unlikely(ftrace_disabled))
612 mutex_lock(&ftraced_lock);
614 if (ftraced_suspend == 1)
615 command |= FTRACE_ENABLE_CALLS;
617 if (saved_ftrace_func != ftrace_trace_function) {
618 saved_ftrace_func = ftrace_trace_function;
619 command |= FTRACE_UPDATE_TRACE_FUNC;
622 if (!command || !ftrace_enabled)
625 ftrace_run_update_code(command);
627 mutex_unlock(&ftraced_lock);
630 static void ftrace_shutdown(void)
634 if (unlikely(ftrace_disabled))
637 mutex_lock(&ftraced_lock);
639 if (!ftraced_suspend)
640 command |= FTRACE_DISABLE_CALLS;
642 if (saved_ftrace_func != ftrace_trace_function) {
643 saved_ftrace_func = ftrace_trace_function;
644 command |= FTRACE_UPDATE_TRACE_FUNC;
647 if (!command || !ftrace_enabled)
650 ftrace_run_update_code(command);
652 mutex_unlock(&ftraced_lock);
655 static void ftrace_startup_sysctl(void)
657 int command = FTRACE_ENABLE_MCOUNT;
659 if (unlikely(ftrace_disabled))
662 mutex_lock(&ftraced_lock);
663 /* Force update next time */
664 saved_ftrace_func = NULL;
665 /* ftraced_suspend is true if we want ftrace running */
667 command |= FTRACE_ENABLE_CALLS;
669 ftrace_run_update_code(command);
670 mutex_unlock(&ftraced_lock);
673 static void ftrace_shutdown_sysctl(void)
675 int command = FTRACE_DISABLE_MCOUNT;
677 if (unlikely(ftrace_disabled))
680 mutex_lock(&ftraced_lock);
681 /* ftraced_suspend is true if ftrace is running */
683 command |= FTRACE_DISABLE_CALLS;
685 ftrace_run_update_code(command);
686 mutex_unlock(&ftraced_lock);
689 static cycle_t ftrace_update_time;
690 static unsigned long ftrace_update_cnt;
691 unsigned long ftrace_update_tot_cnt;
693 static int __ftrace_update_code(void *ignore)
695 struct dyn_ftrace *p;
696 struct hlist_node *t, *n;
697 int save_ftrace_enabled;
701 /* Don't be recording funcs now */
702 ftrace_record_suspend++;
703 save_ftrace_enabled = ftrace_enabled;
706 start = ftrace_now(raw_smp_processor_id());
707 ftrace_update_cnt = 0;
709 /* No locks needed, the machine is stopped! */
710 for (i = 0; i < FTRACE_HASHSIZE; i++) {
711 /* all CPUS are stopped, we are safe to modify code */
712 hlist_for_each_entry_safe(p, t, n, &ftrace_hash[i], node) {
713 /* Skip over failed records which have not been
715 if (p->flags & FTRACE_FL_FAILED)
718 /* Unconverted records are always at the head of the
719 * hash bucket. Once we encounter a converted record,
720 * simply skip over to the next bucket. Saves ftraced
721 * some processor cycles (ftrace does its bid for
722 * global warming :-p ). */
723 if (p->flags & (FTRACE_FL_CONVERTED))
726 if (ftrace_code_disable(p)) {
727 p->flags |= FTRACE_FL_CONVERTED;
730 if ((system_state == SYSTEM_BOOTING) ||
731 !core_kernel_text(p->ip)) {
739 stop = ftrace_now(raw_smp_processor_id());
740 ftrace_update_time = stop - start;
741 ftrace_update_tot_cnt += ftrace_update_cnt;
744 ftrace_enabled = save_ftrace_enabled;
745 ftrace_record_suspend--;
750 static int ftrace_update_code(void)
752 if (unlikely(ftrace_disabled) ||
753 !ftrace_enabled || !ftraced_trigger)
756 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
761 static int ftraced(void *ignore)
765 while (!kthread_should_stop()) {
767 set_current_state(TASK_INTERRUPTIBLE);
769 /* check once a second */
770 schedule_timeout(HZ);
772 if (unlikely(ftrace_disabled))
775 mutex_lock(&ftrace_sysctl_lock);
776 mutex_lock(&ftraced_lock);
777 if (!ftraced_suspend && !ftraced_stop &&
778 ftrace_update_code()) {
779 usecs = nsecs_to_usecs(ftrace_update_time);
780 if (ftrace_update_tot_cnt > 100000) {
781 ftrace_update_tot_cnt = 0;
782 pr_info("hm, dftrace overflow: %lu change%s"
783 " (%lu total) in %lu usec%s\n",
785 ftrace_update_cnt != 1 ? "s" : "",
786 ftrace_update_tot_cnt,
787 usecs, usecs != 1 ? "s" : "");
792 mutex_unlock(&ftraced_lock);
793 mutex_unlock(&ftrace_sysctl_lock);
795 ftrace_shutdown_replenish();
797 __set_current_state(TASK_RUNNING);
801 static int __init ftrace_dyn_table_alloc(void)
803 struct ftrace_page *pg;
807 /* allocate a few pages */
808 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
809 if (!ftrace_pages_start)
813 * Allocate a few more pages.
815 * TODO: have some parser search vmlinux before
816 * final linking to find all calls to ftrace.
818 * a) know how many pages to allocate.
820 * b) set up the table then.
822 * The dynamic code is still necessary for
826 pg = ftrace_pages = ftrace_pages_start;
828 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
830 for (i = 0; i < cnt; i++) {
831 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
833 /* If we fail, we'll try later anyway */
844 FTRACE_ITER_FILTER = (1 << 0),
845 FTRACE_ITER_CONT = (1 << 1),
846 FTRACE_ITER_NOTRACE = (1 << 2),
847 FTRACE_ITER_FAILURES = (1 << 3),
850 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
852 struct ftrace_iterator {
854 struct ftrace_page *pg;
857 unsigned char buffer[FTRACE_BUFF_MAX+1];
863 t_next(struct seq_file *m, void *v, loff_t *pos)
865 struct ftrace_iterator *iter = m->private;
866 struct dyn_ftrace *rec = NULL;
871 if (iter->idx >= iter->pg->index) {
872 if (iter->pg->next) {
873 iter->pg = iter->pg->next;
878 rec = &iter->pg->records[iter->idx++];
879 if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
880 (rec->flags & FTRACE_FL_FAILED)) ||
882 ((iter->flags & FTRACE_ITER_FAILURES) &&
883 (!(rec->flags & FTRACE_FL_FAILED) ||
884 (rec->flags & FTRACE_FL_FREE))) ||
886 ((iter->flags & FTRACE_ITER_FILTER) &&
887 !(rec->flags & FTRACE_FL_FILTER)) ||
889 ((iter->flags & FTRACE_ITER_NOTRACE) &&
890 !(rec->flags & FTRACE_FL_NOTRACE))) {
901 static void *t_start(struct seq_file *m, loff_t *pos)
903 struct ftrace_iterator *iter = m->private;
907 if (*pos != iter->pos) {
908 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
912 p = t_next(m, p, &l);
918 static void t_stop(struct seq_file *m, void *p)
922 static int t_show(struct seq_file *m, void *v)
924 struct dyn_ftrace *rec = v;
925 char str[KSYM_SYMBOL_LEN];
930 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
932 seq_printf(m, "%s\n", str);
937 static struct seq_operations show_ftrace_seq_ops = {
945 ftrace_avail_open(struct inode *inode, struct file *file)
947 struct ftrace_iterator *iter;
950 if (unlikely(ftrace_disabled))
953 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
957 iter->pg = ftrace_pages_start;
960 ret = seq_open(file, &show_ftrace_seq_ops);
962 struct seq_file *m = file->private_data;
972 int ftrace_avail_release(struct inode *inode, struct file *file)
974 struct seq_file *m = (struct seq_file *)file->private_data;
975 struct ftrace_iterator *iter = m->private;
977 seq_release(inode, file);
984 ftrace_failures_open(struct inode *inode, struct file *file)
988 struct ftrace_iterator *iter;
990 ret = ftrace_avail_open(inode, file);
992 m = (struct seq_file *)file->private_data;
993 iter = (struct ftrace_iterator *)m->private;
994 iter->flags = FTRACE_ITER_FAILURES;
1001 static void ftrace_filter_reset(int enable)
1003 struct ftrace_page *pg;
1004 struct dyn_ftrace *rec;
1005 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1008 /* keep kstop machine from running */
1011 ftrace_filtered = 0;
1012 pg = ftrace_pages_start;
1014 for (i = 0; i < pg->index; i++) {
1015 rec = &pg->records[i];
1016 if (rec->flags & FTRACE_FL_FAILED)
1018 rec->flags &= ~type;
1026 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1028 struct ftrace_iterator *iter;
1031 if (unlikely(ftrace_disabled))
1034 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1038 mutex_lock(&ftrace_regex_lock);
1039 if ((file->f_mode & FMODE_WRITE) &&
1040 !(file->f_flags & O_APPEND))
1041 ftrace_filter_reset(enable);
1043 if (file->f_mode & FMODE_READ) {
1044 iter->pg = ftrace_pages_start;
1046 iter->flags = enable ? FTRACE_ITER_FILTER :
1047 FTRACE_ITER_NOTRACE;
1049 ret = seq_open(file, &show_ftrace_seq_ops);
1051 struct seq_file *m = file->private_data;
1056 file->private_data = iter;
1057 mutex_unlock(&ftrace_regex_lock);
1063 ftrace_filter_open(struct inode *inode, struct file *file)
1065 return ftrace_regex_open(inode, file, 1);
1069 ftrace_notrace_open(struct inode *inode, struct file *file)
1071 return ftrace_regex_open(inode, file, 0);
1075 ftrace_regex_read(struct file *file, char __user *ubuf,
1076 size_t cnt, loff_t *ppos)
1078 if (file->f_mode & FMODE_READ)
1079 return seq_read(file, ubuf, cnt, ppos);
1085 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1089 if (file->f_mode & FMODE_READ)
1090 ret = seq_lseek(file, offset, origin);
1092 file->f_pos = ret = 1;
1105 ftrace_match(unsigned char *buff, int len, int enable)
1107 char str[KSYM_SYMBOL_LEN];
1108 char *search = NULL;
1109 struct ftrace_page *pg;
1110 struct dyn_ftrace *rec;
1111 int type = MATCH_FULL;
1112 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1113 unsigned i, match = 0, search_len = 0;
1115 for (i = 0; i < len; i++) {
1116 if (buff[i] == '*') {
1118 search = buff + i + 1;
1119 type = MATCH_END_ONLY;
1120 search_len = len - (i + 1);
1122 if (type == MATCH_END_ONLY) {
1123 type = MATCH_MIDDLE_ONLY;
1126 type = MATCH_FRONT_ONLY;
1134 /* keep kstop machine from running */
1137 ftrace_filtered = 1;
1138 pg = ftrace_pages_start;
1140 for (i = 0; i < pg->index; i++) {
1144 rec = &pg->records[i];
1145 if (rec->flags & FTRACE_FL_FAILED)
1147 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1150 if (strcmp(str, buff) == 0)
1153 case MATCH_FRONT_ONLY:
1154 if (memcmp(str, buff, match) == 0)
1157 case MATCH_MIDDLE_ONLY:
1158 if (strstr(str, search))
1161 case MATCH_END_ONLY:
1162 ptr = strstr(str, search);
1163 if (ptr && (ptr[search_len] == 0))
1176 ftrace_regex_write(struct file *file, const char __user *ubuf,
1177 size_t cnt, loff_t *ppos, int enable)
1179 struct ftrace_iterator *iter;
1184 if (!cnt || cnt < 0)
1187 mutex_lock(&ftrace_regex_lock);
1189 if (file->f_mode & FMODE_READ) {
1190 struct seq_file *m = file->private_data;
1193 iter = file->private_data;
1196 iter->flags &= ~FTRACE_ITER_CONT;
1197 iter->buffer_idx = 0;
1200 ret = get_user(ch, ubuf++);
1206 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1207 /* skip white space */
1208 while (cnt && isspace(ch)) {
1209 ret = get_user(ch, ubuf++);
1217 file->f_pos += read;
1222 iter->buffer_idx = 0;
1225 while (cnt && !isspace(ch)) {
1226 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1227 iter->buffer[iter->buffer_idx++] = ch;
1232 ret = get_user(ch, ubuf++);
1241 iter->buffer[iter->buffer_idx] = 0;
1242 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1243 iter->buffer_idx = 0;
1245 iter->flags |= FTRACE_ITER_CONT;
1248 file->f_pos += read;
1252 mutex_unlock(&ftrace_regex_lock);
1258 ftrace_filter_write(struct file *file, const char __user *ubuf,
1259 size_t cnt, loff_t *ppos)
1261 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1265 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1266 size_t cnt, loff_t *ppos)
1268 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1272 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1274 if (unlikely(ftrace_disabled))
1277 mutex_lock(&ftrace_regex_lock);
1279 ftrace_filter_reset(enable);
1281 ftrace_match(buf, len, enable);
1282 mutex_unlock(&ftrace_regex_lock);
1286 * ftrace_set_filter - set a function to filter on in ftrace
1287 * @buf - the string that holds the function filter text.
1288 * @len - the length of the string.
1289 * @reset - non zero to reset all filters before applying this filter.
1291 * Filters denote which functions should be enabled when tracing is enabled.
1292 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1294 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1296 ftrace_set_regex(buf, len, reset, 1);
1300 * ftrace_set_notrace - set a function to not trace in ftrace
1301 * @buf - the string that holds the function notrace text.
1302 * @len - the length of the string.
1303 * @reset - non zero to reset all filters before applying this filter.
1305 * Notrace Filters denote which functions should not be enabled when tracing
1306 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1309 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1311 ftrace_set_regex(buf, len, reset, 0);
1315 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1317 struct seq_file *m = (struct seq_file *)file->private_data;
1318 struct ftrace_iterator *iter;
1320 mutex_lock(&ftrace_regex_lock);
1321 if (file->f_mode & FMODE_READ) {
1324 seq_release(inode, file);
1326 iter = file->private_data;
1328 if (iter->buffer_idx) {
1330 iter->buffer[iter->buffer_idx] = 0;
1331 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1334 mutex_lock(&ftrace_sysctl_lock);
1335 mutex_lock(&ftraced_lock);
1336 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1337 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1338 mutex_unlock(&ftraced_lock);
1339 mutex_unlock(&ftrace_sysctl_lock);
1342 mutex_unlock(&ftrace_regex_lock);
1347 ftrace_filter_release(struct inode *inode, struct file *file)
1349 return ftrace_regex_release(inode, file, 1);
1353 ftrace_notrace_release(struct inode *inode, struct file *file)
1355 return ftrace_regex_release(inode, file, 0);
1359 ftraced_read(struct file *filp, char __user *ubuf,
1360 size_t cnt, loff_t *ppos)
1362 /* don't worry about races */
1363 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1364 int r = strlen(buf);
1366 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1370 ftraced_write(struct file *filp, const char __user *ubuf,
1371 size_t cnt, loff_t *ppos)
1377 if (cnt >= sizeof(buf))
1380 if (copy_from_user(&buf, ubuf, cnt))
1383 if (strncmp(buf, "enable", 6) == 0)
1385 else if (strncmp(buf, "disable", 7) == 0)
1390 ret = strict_strtoul(buf, 10, &val);
1398 ftrace_enable_daemon();
1400 ftrace_disable_daemon();
1407 static struct file_operations ftrace_avail_fops = {
1408 .open = ftrace_avail_open,
1410 .llseek = seq_lseek,
1411 .release = ftrace_avail_release,
1414 static struct file_operations ftrace_failures_fops = {
1415 .open = ftrace_failures_open,
1417 .llseek = seq_lseek,
1418 .release = ftrace_avail_release,
1421 static struct file_operations ftrace_filter_fops = {
1422 .open = ftrace_filter_open,
1423 .read = ftrace_regex_read,
1424 .write = ftrace_filter_write,
1425 .llseek = ftrace_regex_lseek,
1426 .release = ftrace_filter_release,
1429 static struct file_operations ftrace_notrace_fops = {
1430 .open = ftrace_notrace_open,
1431 .read = ftrace_regex_read,
1432 .write = ftrace_notrace_write,
1433 .llseek = ftrace_regex_lseek,
1434 .release = ftrace_notrace_release,
1437 static struct file_operations ftraced_fops = {
1438 .open = tracing_open_generic,
1439 .read = ftraced_read,
1440 .write = ftraced_write,
1444 * ftrace_force_update - force an update to all recording ftrace functions
1446 int ftrace_force_update(void)
1450 if (unlikely(ftrace_disabled))
1453 mutex_lock(&ftrace_sysctl_lock);
1454 mutex_lock(&ftraced_lock);
1457 * If ftraced_trigger is not set, then there is nothing
1460 if (ftraced_trigger && !ftrace_update_code())
1463 mutex_unlock(&ftraced_lock);
1464 mutex_unlock(&ftrace_sysctl_lock);
1469 static void ftrace_force_shutdown(void)
1471 struct task_struct *task;
1472 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1474 mutex_lock(&ftraced_lock);
1475 task = ftraced_task;
1476 ftraced_task = NULL;
1477 ftraced_suspend = -1;
1478 ftrace_run_update_code(command);
1479 mutex_unlock(&ftraced_lock);
1485 static __init int ftrace_init_debugfs(void)
1487 struct dentry *d_tracer;
1488 struct dentry *entry;
1490 d_tracer = tracing_init_dentry();
1492 entry = debugfs_create_file("available_filter_functions", 0444,
1493 d_tracer, NULL, &ftrace_avail_fops);
1495 pr_warning("Could not create debugfs "
1496 "'available_filter_functions' entry\n");
1498 entry = debugfs_create_file("failures", 0444,
1499 d_tracer, NULL, &ftrace_failures_fops);
1501 pr_warning("Could not create debugfs 'failures' entry\n");
1503 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1504 NULL, &ftrace_filter_fops);
1506 pr_warning("Could not create debugfs "
1507 "'set_ftrace_filter' entry\n");
1509 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1510 NULL, &ftrace_notrace_fops);
1512 pr_warning("Could not create debugfs "
1513 "'set_ftrace_notrace' entry\n");
1515 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1516 NULL, &ftraced_fops);
1518 pr_warning("Could not create debugfs "
1519 "'ftraced_enabled' entry\n");
1523 fs_initcall(ftrace_init_debugfs);
1525 static int __init ftrace_dynamic_init(void)
1527 struct task_struct *p;
1531 addr = (unsigned long)ftrace_record_ip;
1533 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1535 /* ftrace_dyn_arch_init places the return code in addr */
1541 ret = ftrace_dyn_table_alloc();
1545 p = kthread_run(ftraced, NULL, "ftraced");
1551 last_ftrace_enabled = ftrace_enabled = 1;
1557 ftrace_disabled = 1;
1561 core_initcall(ftrace_dynamic_init);
1563 # define ftrace_startup() do { } while (0)
1564 # define ftrace_shutdown() do { } while (0)
1565 # define ftrace_startup_sysctl() do { } while (0)
1566 # define ftrace_shutdown_sysctl() do { } while (0)
1567 # define ftrace_force_shutdown() do { } while (0)
1568 #endif /* CONFIG_DYNAMIC_FTRACE */
1571 * ftrace_kill - totally shutdown ftrace
1573 * This is a safety measure. If something was detected that seems
1574 * wrong, calling this function will keep ftrace from doing
1575 * any more modifications, and updates.
1576 * used when something went wrong.
1578 void ftrace_kill(void)
1580 mutex_lock(&ftrace_sysctl_lock);
1581 ftrace_disabled = 1;
1584 clear_ftrace_function();
1585 mutex_unlock(&ftrace_sysctl_lock);
1587 /* Try to totally disable ftrace */
1588 ftrace_force_shutdown();
1592 * register_ftrace_function - register a function for profiling
1593 * @ops - ops structure that holds the function for profiling.
1595 * Register a function to be called by all functions in the
1598 * Note: @ops->func and all the functions it calls must be labeled
1599 * with "notrace", otherwise it will go into a
1602 int register_ftrace_function(struct ftrace_ops *ops)
1606 if (unlikely(ftrace_disabled))
1609 mutex_lock(&ftrace_sysctl_lock);
1610 ret = __register_ftrace_function(ops);
1612 mutex_unlock(&ftrace_sysctl_lock);
1618 * unregister_ftrace_function - unresgister a function for profiling.
1619 * @ops - ops structure that holds the function to unregister
1621 * Unregister a function that was added to be called by ftrace profiling.
1623 int unregister_ftrace_function(struct ftrace_ops *ops)
1627 mutex_lock(&ftrace_sysctl_lock);
1628 ret = __unregister_ftrace_function(ops);
1630 mutex_unlock(&ftrace_sysctl_lock);
1636 ftrace_enable_sysctl(struct ctl_table *table, int write,
1637 struct file *file, void __user *buffer, size_t *lenp,
1642 if (unlikely(ftrace_disabled))
1645 mutex_lock(&ftrace_sysctl_lock);
1647 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1649 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1652 last_ftrace_enabled = ftrace_enabled;
1654 if (ftrace_enabled) {
1656 ftrace_startup_sysctl();
1658 /* we are starting ftrace again */
1659 if (ftrace_list != &ftrace_list_end) {
1660 if (ftrace_list->next == &ftrace_list_end)
1661 ftrace_trace_function = ftrace_list->func;
1663 ftrace_trace_function = ftrace_list_func;
1667 /* stopping ftrace calls (just send to ftrace_stub) */
1668 ftrace_trace_function = ftrace_stub;
1670 ftrace_shutdown_sysctl();
1674 mutex_unlock(&ftrace_sysctl_lock);