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;
167 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
169 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
171 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
172 static DEFINE_MUTEX(ftraced_lock);
173 static DEFINE_MUTEX(ftrace_regex_lock);
176 struct ftrace_page *next;
178 struct dyn_ftrace records[];
181 #define ENTRIES_PER_PAGE \
182 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
184 /* estimate from running different kernels */
185 #define NR_TO_INIT 10000
187 static struct ftrace_page *ftrace_pages_start;
188 static struct ftrace_page *ftrace_pages;
190 static int ftraced_trigger;
191 static int ftraced_suspend;
192 static int ftraced_stop;
194 static int ftrace_record_suspend;
196 static struct dyn_ftrace *ftrace_free_records;
199 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
201 struct dyn_ftrace *p;
202 struct hlist_node *t;
205 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
216 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
218 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
221 /* called from kstop_machine */
222 static inline void ftrace_del_hash(struct dyn_ftrace *node)
224 hlist_del(&node->node);
227 static void ftrace_free_rec(struct dyn_ftrace *rec)
229 /* no locking, only called from kstop_machine */
231 rec->ip = (unsigned long)ftrace_free_records;
232 ftrace_free_records = rec;
233 rec->flags |= FTRACE_FL_FREE;
236 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
238 struct dyn_ftrace *rec;
240 /* First check for freed records */
241 if (ftrace_free_records) {
242 rec = ftrace_free_records;
244 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
246 ftrace_free_records = NULL;
252 ftrace_free_records = (void *)rec->ip;
253 memset(rec, 0, sizeof(*rec));
257 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
258 if (!ftrace_pages->next)
260 ftrace_pages = ftrace_pages->next;
263 return &ftrace_pages->records[ftrace_pages->index++];
267 ftrace_record_ip(unsigned long ip)
269 struct dyn_ftrace *node;
276 if (!ftrace_enabled || ftrace_disabled)
279 resched = need_resched();
280 preempt_disable_notrace();
283 * We simply need to protect against recursion.
284 * Use the the raw version of smp_processor_id and not
285 * __get_cpu_var which can call debug hooks that can
286 * cause a recursive crash here.
288 cpu = raw_smp_processor_id();
289 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
290 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
293 if (unlikely(ftrace_record_suspend))
296 key = hash_long(ip, FTRACE_HASHBITS);
298 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
300 if (ftrace_ip_in_hash(ip, key))
303 atomic = irqs_disabled();
305 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
307 /* This ip may have hit the hash before the lock */
308 if (ftrace_ip_in_hash(ip, key))
311 node = ftrace_alloc_dyn_node(ip);
317 ftrace_add_hash(node, key);
322 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
324 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
326 /* prevent recursion with scheduler */
328 preempt_enable_no_resched_notrace();
330 preempt_enable_notrace();
333 #define FTRACE_ADDR ((long)(ftrace_caller))
336 __ftrace_replace_code(struct dyn_ftrace *rec,
337 unsigned char *old, unsigned char *new, int enable)
339 unsigned long ip, fl;
343 if (ftrace_filtered && enable) {
345 * If filtering is on:
347 * If this record is set to be filtered and
348 * is enabled then do nothing.
350 * If this record is set to be filtered and
351 * it is not enabled, enable it.
353 * If this record is not set to be filtered
354 * and it is not enabled do nothing.
356 * If this record is set not to trace then
359 * If this record is set not to trace and
360 * it is enabled then disable it.
362 * If this record is not set to be filtered and
363 * it is enabled, disable it.
366 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
369 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
370 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
371 !fl || (fl == FTRACE_FL_NOTRACE))
375 * If it is enabled disable it,
376 * otherwise enable it!
378 if (fl & FTRACE_FL_ENABLED) {
379 /* swap new and old */
381 old = ftrace_call_replace(ip, FTRACE_ADDR);
382 rec->flags &= ~FTRACE_FL_ENABLED;
384 new = ftrace_call_replace(ip, FTRACE_ADDR);
385 rec->flags |= FTRACE_FL_ENABLED;
391 * If this record is set not to trace and is
392 * not enabled, do nothing.
394 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
395 if (fl == FTRACE_FL_NOTRACE)
398 new = ftrace_call_replace(ip, FTRACE_ADDR);
400 old = ftrace_call_replace(ip, FTRACE_ADDR);
403 if (rec->flags & FTRACE_FL_ENABLED)
405 rec->flags |= FTRACE_FL_ENABLED;
407 if (!(rec->flags & FTRACE_FL_ENABLED))
409 rec->flags &= ~FTRACE_FL_ENABLED;
413 return ftrace_modify_code(ip, old, new);
416 static void ftrace_replace_code(int enable)
419 unsigned char *new = NULL, *old = NULL;
420 struct dyn_ftrace *rec;
421 struct ftrace_page *pg;
424 old = ftrace_nop_replace();
426 new = ftrace_nop_replace();
428 for (pg = ftrace_pages_start; pg; pg = pg->next) {
429 for (i = 0; i < pg->index; i++) {
430 rec = &pg->records[i];
432 /* don't modify code that has already faulted */
433 if (rec->flags & FTRACE_FL_FAILED)
436 failed = __ftrace_replace_code(rec, old, new, enable);
437 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
438 rec->flags |= FTRACE_FL_FAILED;
439 if ((system_state == SYSTEM_BOOTING) ||
440 !core_kernel_text(rec->ip)) {
441 ftrace_del_hash(rec);
442 ftrace_free_rec(rec);
449 static void ftrace_shutdown_replenish(void)
451 if (ftrace_pages->next)
454 /* allocate another page */
455 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
459 ftrace_code_disable(struct dyn_ftrace *rec)
462 unsigned char *nop, *call;
467 nop = ftrace_nop_replace();
468 call = ftrace_call_replace(ip, MCOUNT_ADDR);
470 failed = ftrace_modify_code(ip, call, nop);
472 rec->flags |= FTRACE_FL_FAILED;
478 static int __ftrace_update_code(void *ignore);
480 static int __ftrace_modify_code(void *data)
485 if (*command & FTRACE_ENABLE_CALLS) {
487 * Update any recorded ips now that we have the
490 __ftrace_update_code(NULL);
491 ftrace_replace_code(1);
492 } else if (*command & FTRACE_DISABLE_CALLS)
493 ftrace_replace_code(0);
495 if (*command & FTRACE_UPDATE_TRACE_FUNC)
496 ftrace_update_ftrace_func(ftrace_trace_function);
498 if (*command & FTRACE_ENABLE_MCOUNT) {
499 addr = (unsigned long)ftrace_record_ip;
500 ftrace_mcount_set(&addr);
501 } else if (*command & FTRACE_DISABLE_MCOUNT) {
502 addr = (unsigned long)ftrace_stub;
503 ftrace_mcount_set(&addr);
509 static void ftrace_run_update_code(int command)
511 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
514 void ftrace_disable_daemon(void)
516 /* Stop the daemon from calling kstop_machine */
517 mutex_lock(&ftraced_lock);
519 mutex_unlock(&ftraced_lock);
521 ftrace_force_update();
524 void ftrace_enable_daemon(void)
526 mutex_lock(&ftraced_lock);
528 mutex_unlock(&ftraced_lock);
530 ftrace_force_update();
533 static ftrace_func_t saved_ftrace_func;
535 static void ftrace_startup(void)
539 if (unlikely(ftrace_disabled))
542 mutex_lock(&ftraced_lock);
544 if (ftraced_suspend == 1)
545 command |= FTRACE_ENABLE_CALLS;
547 if (saved_ftrace_func != ftrace_trace_function) {
548 saved_ftrace_func = ftrace_trace_function;
549 command |= FTRACE_UPDATE_TRACE_FUNC;
552 if (!command || !ftrace_enabled)
555 ftrace_run_update_code(command);
557 mutex_unlock(&ftraced_lock);
560 static void ftrace_shutdown(void)
564 if (unlikely(ftrace_disabled))
567 mutex_lock(&ftraced_lock);
569 if (!ftraced_suspend)
570 command |= FTRACE_DISABLE_CALLS;
572 if (saved_ftrace_func != ftrace_trace_function) {
573 saved_ftrace_func = ftrace_trace_function;
574 command |= FTRACE_UPDATE_TRACE_FUNC;
577 if (!command || !ftrace_enabled)
580 ftrace_run_update_code(command);
582 mutex_unlock(&ftraced_lock);
585 static void ftrace_startup_sysctl(void)
587 int command = FTRACE_ENABLE_MCOUNT;
589 if (unlikely(ftrace_disabled))
592 mutex_lock(&ftraced_lock);
593 /* Force update next time */
594 saved_ftrace_func = NULL;
595 /* ftraced_suspend is true if we want ftrace running */
597 command |= FTRACE_ENABLE_CALLS;
599 ftrace_run_update_code(command);
600 mutex_unlock(&ftraced_lock);
603 static void ftrace_shutdown_sysctl(void)
605 int command = FTRACE_DISABLE_MCOUNT;
607 if (unlikely(ftrace_disabled))
610 mutex_lock(&ftraced_lock);
611 /* ftraced_suspend is true if ftrace is running */
613 command |= FTRACE_DISABLE_CALLS;
615 ftrace_run_update_code(command);
616 mutex_unlock(&ftraced_lock);
619 static cycle_t ftrace_update_time;
620 static unsigned long ftrace_update_cnt;
621 unsigned long ftrace_update_tot_cnt;
623 static int __ftrace_update_code(void *ignore)
625 struct dyn_ftrace *p;
626 struct hlist_node *t, *n;
627 int save_ftrace_enabled;
631 /* Don't be recording funcs now */
632 ftrace_record_suspend++;
633 save_ftrace_enabled = ftrace_enabled;
636 start = ftrace_now(raw_smp_processor_id());
637 ftrace_update_cnt = 0;
639 /* No locks needed, the machine is stopped! */
640 for (i = 0; i < FTRACE_HASHSIZE; i++) {
641 /* all CPUS are stopped, we are safe to modify code */
642 hlist_for_each_entry_safe(p, t, n, &ftrace_hash[i], node) {
643 /* Skip over failed records which have not been
645 if (p->flags & FTRACE_FL_FAILED)
648 /* Unconverted records are always at the head of the
649 * hash bucket. Once we encounter a converted record,
650 * simply skip over to the next bucket. Saves ftraced
651 * some processor cycles (ftrace does its bid for
652 * global warming :-p ). */
653 if (p->flags & (FTRACE_FL_CONVERTED))
656 if (ftrace_code_disable(p)) {
657 p->flags |= FTRACE_FL_CONVERTED;
660 if ((system_state == SYSTEM_BOOTING) ||
661 !core_kernel_text(p->ip)) {
669 stop = ftrace_now(raw_smp_processor_id());
670 ftrace_update_time = stop - start;
671 ftrace_update_tot_cnt += ftrace_update_cnt;
674 ftrace_enabled = save_ftrace_enabled;
675 ftrace_record_suspend--;
680 static int ftrace_update_code(void)
682 if (unlikely(ftrace_disabled) ||
683 !ftrace_enabled || !ftraced_trigger)
686 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
691 static int ftraced(void *ignore)
695 while (!kthread_should_stop()) {
697 set_current_state(TASK_INTERRUPTIBLE);
699 /* check once a second */
700 schedule_timeout(HZ);
702 if (unlikely(ftrace_disabled))
705 mutex_lock(&ftrace_sysctl_lock);
706 mutex_lock(&ftraced_lock);
707 if (!ftraced_suspend && !ftraced_stop &&
708 ftrace_update_code()) {
709 usecs = nsecs_to_usecs(ftrace_update_time);
710 if (ftrace_update_tot_cnt > 100000) {
711 ftrace_update_tot_cnt = 0;
712 pr_info("hm, dftrace overflow: %lu change%s"
713 " (%lu total) in %lu usec%s\n",
715 ftrace_update_cnt != 1 ? "s" : "",
716 ftrace_update_tot_cnt,
717 usecs, usecs != 1 ? "s" : "");
722 mutex_unlock(&ftraced_lock);
723 mutex_unlock(&ftrace_sysctl_lock);
725 ftrace_shutdown_replenish();
727 __set_current_state(TASK_RUNNING);
731 static int __init ftrace_dyn_table_alloc(void)
733 struct ftrace_page *pg;
737 /* allocate a few pages */
738 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
739 if (!ftrace_pages_start)
743 * Allocate a few more pages.
745 * TODO: have some parser search vmlinux before
746 * final linking to find all calls to ftrace.
748 * a) know how many pages to allocate.
750 * b) set up the table then.
752 * The dynamic code is still necessary for
756 pg = ftrace_pages = ftrace_pages_start;
758 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
760 for (i = 0; i < cnt; i++) {
761 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
763 /* If we fail, we'll try later anyway */
774 FTRACE_ITER_FILTER = (1 << 0),
775 FTRACE_ITER_CONT = (1 << 1),
776 FTRACE_ITER_NOTRACE = (1 << 2),
777 FTRACE_ITER_FAILURES = (1 << 3),
780 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
782 struct ftrace_iterator {
784 struct ftrace_page *pg;
787 unsigned char buffer[FTRACE_BUFF_MAX+1];
793 t_next(struct seq_file *m, void *v, loff_t *pos)
795 struct ftrace_iterator *iter = m->private;
796 struct dyn_ftrace *rec = NULL;
801 if (iter->idx >= iter->pg->index) {
802 if (iter->pg->next) {
803 iter->pg = iter->pg->next;
808 rec = &iter->pg->records[iter->idx++];
809 if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
810 (rec->flags & FTRACE_FL_FAILED)) ||
812 ((iter->flags & FTRACE_ITER_FAILURES) &&
813 (!(rec->flags & FTRACE_FL_FAILED) ||
814 (rec->flags & FTRACE_FL_FREE))) ||
816 ((iter->flags & FTRACE_ITER_FILTER) &&
817 !(rec->flags & FTRACE_FL_FILTER)) ||
819 ((iter->flags & FTRACE_ITER_NOTRACE) &&
820 !(rec->flags & FTRACE_FL_NOTRACE))) {
831 static void *t_start(struct seq_file *m, loff_t *pos)
833 struct ftrace_iterator *iter = m->private;
837 if (*pos != iter->pos) {
838 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
842 p = t_next(m, p, &l);
848 static void t_stop(struct seq_file *m, void *p)
852 static int t_show(struct seq_file *m, void *v)
854 struct dyn_ftrace *rec = v;
855 char str[KSYM_SYMBOL_LEN];
860 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
862 seq_printf(m, "%s\n", str);
867 static struct seq_operations show_ftrace_seq_ops = {
875 ftrace_avail_open(struct inode *inode, struct file *file)
877 struct ftrace_iterator *iter;
880 if (unlikely(ftrace_disabled))
883 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
887 iter->pg = ftrace_pages_start;
890 ret = seq_open(file, &show_ftrace_seq_ops);
892 struct seq_file *m = file->private_data;
902 int ftrace_avail_release(struct inode *inode, struct file *file)
904 struct seq_file *m = (struct seq_file *)file->private_data;
905 struct ftrace_iterator *iter = m->private;
907 seq_release(inode, file);
914 ftrace_failures_open(struct inode *inode, struct file *file)
918 struct ftrace_iterator *iter;
920 ret = ftrace_avail_open(inode, file);
922 m = (struct seq_file *)file->private_data;
923 iter = (struct ftrace_iterator *)m->private;
924 iter->flags = FTRACE_ITER_FAILURES;
931 static void ftrace_filter_reset(int enable)
933 struct ftrace_page *pg;
934 struct dyn_ftrace *rec;
935 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
938 /* keep kstop machine from running */
942 pg = ftrace_pages_start;
944 for (i = 0; i < pg->index; i++) {
945 rec = &pg->records[i];
946 if (rec->flags & FTRACE_FL_FAILED)
956 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
958 struct ftrace_iterator *iter;
961 if (unlikely(ftrace_disabled))
964 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
968 mutex_lock(&ftrace_regex_lock);
969 if ((file->f_mode & FMODE_WRITE) &&
970 !(file->f_flags & O_APPEND))
971 ftrace_filter_reset(enable);
973 if (file->f_mode & FMODE_READ) {
974 iter->pg = ftrace_pages_start;
976 iter->flags = enable ? FTRACE_ITER_FILTER :
979 ret = seq_open(file, &show_ftrace_seq_ops);
981 struct seq_file *m = file->private_data;
986 file->private_data = iter;
987 mutex_unlock(&ftrace_regex_lock);
993 ftrace_filter_open(struct inode *inode, struct file *file)
995 return ftrace_regex_open(inode, file, 1);
999 ftrace_notrace_open(struct inode *inode, struct file *file)
1001 return ftrace_regex_open(inode, file, 0);
1005 ftrace_regex_read(struct file *file, char __user *ubuf,
1006 size_t cnt, loff_t *ppos)
1008 if (file->f_mode & FMODE_READ)
1009 return seq_read(file, ubuf, cnt, ppos);
1015 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1019 if (file->f_mode & FMODE_READ)
1020 ret = seq_lseek(file, offset, origin);
1022 file->f_pos = ret = 1;
1035 ftrace_match(unsigned char *buff, int len, int enable)
1037 char str[KSYM_SYMBOL_LEN];
1038 char *search = NULL;
1039 struct ftrace_page *pg;
1040 struct dyn_ftrace *rec;
1041 int type = MATCH_FULL;
1042 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1043 unsigned i, match = 0, search_len = 0;
1045 for (i = 0; i < len; i++) {
1046 if (buff[i] == '*') {
1048 search = buff + i + 1;
1049 type = MATCH_END_ONLY;
1050 search_len = len - (i + 1);
1052 if (type == MATCH_END_ONLY) {
1053 type = MATCH_MIDDLE_ONLY;
1056 type = MATCH_FRONT_ONLY;
1064 /* keep kstop machine from running */
1067 ftrace_filtered = 1;
1068 pg = ftrace_pages_start;
1070 for (i = 0; i < pg->index; i++) {
1074 rec = &pg->records[i];
1075 if (rec->flags & FTRACE_FL_FAILED)
1077 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1080 if (strcmp(str, buff) == 0)
1083 case MATCH_FRONT_ONLY:
1084 if (memcmp(str, buff, match) == 0)
1087 case MATCH_MIDDLE_ONLY:
1088 if (strstr(str, search))
1091 case MATCH_END_ONLY:
1092 ptr = strstr(str, search);
1093 if (ptr && (ptr[search_len] == 0))
1106 ftrace_regex_write(struct file *file, const char __user *ubuf,
1107 size_t cnt, loff_t *ppos, int enable)
1109 struct ftrace_iterator *iter;
1114 if (!cnt || cnt < 0)
1117 mutex_lock(&ftrace_regex_lock);
1119 if (file->f_mode & FMODE_READ) {
1120 struct seq_file *m = file->private_data;
1123 iter = file->private_data;
1126 iter->flags &= ~FTRACE_ITER_CONT;
1127 iter->buffer_idx = 0;
1130 ret = get_user(ch, ubuf++);
1136 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1137 /* skip white space */
1138 while (cnt && isspace(ch)) {
1139 ret = get_user(ch, ubuf++);
1147 file->f_pos += read;
1152 iter->buffer_idx = 0;
1155 while (cnt && !isspace(ch)) {
1156 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1157 iter->buffer[iter->buffer_idx++] = ch;
1162 ret = get_user(ch, ubuf++);
1171 iter->buffer[iter->buffer_idx] = 0;
1172 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1173 iter->buffer_idx = 0;
1175 iter->flags |= FTRACE_ITER_CONT;
1178 file->f_pos += read;
1182 mutex_unlock(&ftrace_regex_lock);
1188 ftrace_filter_write(struct file *file, const char __user *ubuf,
1189 size_t cnt, loff_t *ppos)
1191 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1195 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1196 size_t cnt, loff_t *ppos)
1198 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1202 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1204 if (unlikely(ftrace_disabled))
1207 mutex_lock(&ftrace_regex_lock);
1209 ftrace_filter_reset(enable);
1211 ftrace_match(buf, len, enable);
1212 mutex_unlock(&ftrace_regex_lock);
1216 * ftrace_set_filter - set a function to filter on in ftrace
1217 * @buf - the string that holds the function filter text.
1218 * @len - the length of the string.
1219 * @reset - non zero to reset all filters before applying this filter.
1221 * Filters denote which functions should be enabled when tracing is enabled.
1222 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1224 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1226 ftrace_set_regex(buf, len, reset, 1);
1230 * ftrace_set_notrace - set a function to not trace in ftrace
1231 * @buf - the string that holds the function notrace text.
1232 * @len - the length of the string.
1233 * @reset - non zero to reset all filters before applying this filter.
1235 * Notrace Filters denote which functions should not be enabled when tracing
1236 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1239 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1241 ftrace_set_regex(buf, len, reset, 0);
1245 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1247 struct seq_file *m = (struct seq_file *)file->private_data;
1248 struct ftrace_iterator *iter;
1250 mutex_lock(&ftrace_regex_lock);
1251 if (file->f_mode & FMODE_READ) {
1254 seq_release(inode, file);
1256 iter = file->private_data;
1258 if (iter->buffer_idx) {
1260 iter->buffer[iter->buffer_idx] = 0;
1261 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1264 mutex_lock(&ftrace_sysctl_lock);
1265 mutex_lock(&ftraced_lock);
1266 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1267 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1268 mutex_unlock(&ftraced_lock);
1269 mutex_unlock(&ftrace_sysctl_lock);
1272 mutex_unlock(&ftrace_regex_lock);
1277 ftrace_filter_release(struct inode *inode, struct file *file)
1279 return ftrace_regex_release(inode, file, 1);
1283 ftrace_notrace_release(struct inode *inode, struct file *file)
1285 return ftrace_regex_release(inode, file, 0);
1289 ftraced_read(struct file *filp, char __user *ubuf,
1290 size_t cnt, loff_t *ppos)
1292 /* don't worry about races */
1293 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1294 int r = strlen(buf);
1296 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1300 ftraced_write(struct file *filp, const char __user *ubuf,
1301 size_t cnt, loff_t *ppos)
1307 if (cnt >= sizeof(buf))
1310 if (copy_from_user(&buf, ubuf, cnt))
1313 if (strncmp(buf, "enable", 6) == 0)
1315 else if (strncmp(buf, "disable", 7) == 0)
1320 ret = strict_strtoul(buf, 10, &val);
1328 ftrace_enable_daemon();
1330 ftrace_disable_daemon();
1337 static struct file_operations ftrace_avail_fops = {
1338 .open = ftrace_avail_open,
1340 .llseek = seq_lseek,
1341 .release = ftrace_avail_release,
1344 static struct file_operations ftrace_failures_fops = {
1345 .open = ftrace_failures_open,
1347 .llseek = seq_lseek,
1348 .release = ftrace_avail_release,
1351 static struct file_operations ftrace_filter_fops = {
1352 .open = ftrace_filter_open,
1353 .read = ftrace_regex_read,
1354 .write = ftrace_filter_write,
1355 .llseek = ftrace_regex_lseek,
1356 .release = ftrace_filter_release,
1359 static struct file_operations ftrace_notrace_fops = {
1360 .open = ftrace_notrace_open,
1361 .read = ftrace_regex_read,
1362 .write = ftrace_notrace_write,
1363 .llseek = ftrace_regex_lseek,
1364 .release = ftrace_notrace_release,
1367 static struct file_operations ftraced_fops = {
1368 .open = tracing_open_generic,
1369 .read = ftraced_read,
1370 .write = ftraced_write,
1374 * ftrace_force_update - force an update to all recording ftrace functions
1376 int ftrace_force_update(void)
1380 if (unlikely(ftrace_disabled))
1383 mutex_lock(&ftrace_sysctl_lock);
1384 mutex_lock(&ftraced_lock);
1387 * If ftraced_trigger is not set, then there is nothing
1390 if (ftraced_trigger && !ftrace_update_code())
1393 mutex_unlock(&ftraced_lock);
1394 mutex_unlock(&ftrace_sysctl_lock);
1399 static void ftrace_force_shutdown(void)
1401 struct task_struct *task;
1402 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1404 mutex_lock(&ftraced_lock);
1405 task = ftraced_task;
1406 ftraced_task = NULL;
1407 ftraced_suspend = -1;
1408 ftrace_run_update_code(command);
1409 mutex_unlock(&ftraced_lock);
1415 static __init int ftrace_init_debugfs(void)
1417 struct dentry *d_tracer;
1418 struct dentry *entry;
1420 d_tracer = tracing_init_dentry();
1422 entry = debugfs_create_file("available_filter_functions", 0444,
1423 d_tracer, NULL, &ftrace_avail_fops);
1425 pr_warning("Could not create debugfs "
1426 "'available_filter_functions' entry\n");
1428 entry = debugfs_create_file("failures", 0444,
1429 d_tracer, NULL, &ftrace_failures_fops);
1431 pr_warning("Could not create debugfs 'failures' entry\n");
1433 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1434 NULL, &ftrace_filter_fops);
1436 pr_warning("Could not create debugfs "
1437 "'set_ftrace_filter' entry\n");
1439 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1440 NULL, &ftrace_notrace_fops);
1442 pr_warning("Could not create debugfs "
1443 "'set_ftrace_notrace' entry\n");
1445 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1446 NULL, &ftraced_fops);
1448 pr_warning("Could not create debugfs "
1449 "'ftraced_enabled' entry\n");
1453 fs_initcall(ftrace_init_debugfs);
1455 static int __init ftrace_dynamic_init(void)
1457 struct task_struct *p;
1461 addr = (unsigned long)ftrace_record_ip;
1463 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1465 /* ftrace_dyn_arch_init places the return code in addr */
1471 ret = ftrace_dyn_table_alloc();
1475 p = kthread_run(ftraced, NULL, "ftraced");
1481 last_ftrace_enabled = ftrace_enabled = 1;
1487 ftrace_disabled = 1;
1491 core_initcall(ftrace_dynamic_init);
1493 # define ftrace_startup() do { } while (0)
1494 # define ftrace_shutdown() do { } while (0)
1495 # define ftrace_startup_sysctl() do { } while (0)
1496 # define ftrace_shutdown_sysctl() do { } while (0)
1497 # define ftrace_force_shutdown() do { } while (0)
1498 #endif /* CONFIG_DYNAMIC_FTRACE */
1501 * ftrace_kill - totally shutdown ftrace
1503 * This is a safety measure. If something was detected that seems
1504 * wrong, calling this function will keep ftrace from doing
1505 * any more modifications, and updates.
1506 * used when something went wrong.
1508 void ftrace_kill(void)
1510 mutex_lock(&ftrace_sysctl_lock);
1511 ftrace_disabled = 1;
1514 clear_ftrace_function();
1515 mutex_unlock(&ftrace_sysctl_lock);
1517 /* Try to totally disable ftrace */
1518 ftrace_force_shutdown();
1522 * register_ftrace_function - register a function for profiling
1523 * @ops - ops structure that holds the function for profiling.
1525 * Register a function to be called by all functions in the
1528 * Note: @ops->func and all the functions it calls must be labeled
1529 * with "notrace", otherwise it will go into a
1532 int register_ftrace_function(struct ftrace_ops *ops)
1536 if (unlikely(ftrace_disabled))
1539 mutex_lock(&ftrace_sysctl_lock);
1540 ret = __register_ftrace_function(ops);
1542 mutex_unlock(&ftrace_sysctl_lock);
1548 * unregister_ftrace_function - unresgister a function for profiling.
1549 * @ops - ops structure that holds the function to unregister
1551 * Unregister a function that was added to be called by ftrace profiling.
1553 int unregister_ftrace_function(struct ftrace_ops *ops)
1557 mutex_lock(&ftrace_sysctl_lock);
1558 ret = __unregister_ftrace_function(ops);
1560 mutex_unlock(&ftrace_sysctl_lock);
1566 ftrace_enable_sysctl(struct ctl_table *table, int write,
1567 struct file *file, void __user *buffer, size_t *lenp,
1572 if (unlikely(ftrace_disabled))
1575 mutex_lock(&ftrace_sysctl_lock);
1577 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1579 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1582 last_ftrace_enabled = ftrace_enabled;
1584 if (ftrace_enabled) {
1586 ftrace_startup_sysctl();
1588 /* we are starting ftrace again */
1589 if (ftrace_list != &ftrace_list_end) {
1590 if (ftrace_list->next == &ftrace_list_end)
1591 ftrace_trace_function = ftrace_list->func;
1593 ftrace_trace_function = ftrace_list_func;
1597 /* stopping ftrace calls (just send to ftrace_stub) */
1598 ftrace_trace_function = ftrace_stub;
1600 ftrace_shutdown_sysctl();
1604 mutex_unlock(&ftrace_sysctl_lock);