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/list.h>
30 #include <asm/ftrace.h>
34 #define FTRACE_WARN_ON(cond) \
40 #define FTRACE_WARN_ON_ONCE(cond) \
42 if (WARN_ON_ONCE(cond)) \
46 /* ftrace_enabled is a method to turn ftrace on or off */
47 int ftrace_enabled __read_mostly;
48 static int last_ftrace_enabled;
50 /* Quick disabling of function tracer. */
51 int function_trace_stop;
54 * ftrace_disabled is set when an anomaly is discovered.
55 * ftrace_disabled is much stronger than ftrace_enabled.
57 static int ftrace_disabled __read_mostly;
59 static DEFINE_SPINLOCK(ftrace_lock);
60 static DEFINE_MUTEX(ftrace_sysctl_lock);
62 static struct ftrace_ops ftrace_list_end __read_mostly =
67 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
68 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
69 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
71 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
73 struct ftrace_ops *op = ftrace_list;
75 /* in case someone actually ports this to alpha! */
76 read_barrier_depends();
78 while (op != &ftrace_list_end) {
80 read_barrier_depends();
81 op->func(ip, parent_ip);
87 * clear_ftrace_function - reset the ftrace function
89 * This NULLs the ftrace function and in essence stops
90 * tracing. There may be lag
92 void clear_ftrace_function(void)
94 ftrace_trace_function = ftrace_stub;
95 __ftrace_trace_function = ftrace_stub;
98 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
100 * For those archs that do not test ftrace_trace_stop in their
101 * mcount call site, we need to do it from C.
103 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
105 if (function_trace_stop)
108 __ftrace_trace_function(ip, parent_ip);
112 static int __register_ftrace_function(struct ftrace_ops *ops)
114 /* should not be called from interrupt context */
115 spin_lock(&ftrace_lock);
117 ops->next = ftrace_list;
119 * We are entering ops into the ftrace_list but another
120 * CPU might be walking that list. We need to make sure
121 * the ops->next pointer is valid before another CPU sees
122 * the ops pointer included into the ftrace_list.
127 if (ftrace_enabled) {
129 * For one func, simply call it directly.
130 * For more than one func, call the chain.
132 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
133 if (ops->next == &ftrace_list_end)
134 ftrace_trace_function = ops->func;
136 ftrace_trace_function = ftrace_list_func;
138 if (ops->next == &ftrace_list_end)
139 __ftrace_trace_function = ops->func;
141 __ftrace_trace_function = ftrace_list_func;
142 ftrace_trace_function = ftrace_test_stop_func;
146 spin_unlock(&ftrace_lock);
151 static int __unregister_ftrace_function(struct ftrace_ops *ops)
153 struct ftrace_ops **p;
156 /* should not be called from interrupt context */
157 spin_lock(&ftrace_lock);
160 * If we are removing the last function, then simply point
161 * to the ftrace_stub.
163 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
164 ftrace_trace_function = ftrace_stub;
165 ftrace_list = &ftrace_list_end;
169 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
180 if (ftrace_enabled) {
181 /* If we only have one func left, then call that directly */
182 if (ftrace_list == &ftrace_list_end ||
183 ftrace_list->next == &ftrace_list_end)
184 ftrace_trace_function = ftrace_list->func;
188 spin_unlock(&ftrace_lock);
193 #ifdef CONFIG_DYNAMIC_FTRACE
194 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
195 # error Dynamic ftrace depends on MCOUNT_RECORD
199 * Since MCOUNT_ADDR may point to mcount itself, we do not want
200 * to get it confused by reading a reference in the code as we
201 * are parsing on objcopy output of text. Use a variable for
204 static unsigned long mcount_addr = MCOUNT_ADDR;
207 FTRACE_ENABLE_CALLS = (1 << 0),
208 FTRACE_DISABLE_CALLS = (1 << 1),
209 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
210 FTRACE_ENABLE_MCOUNT = (1 << 3),
211 FTRACE_DISABLE_MCOUNT = (1 << 4),
214 static int ftrace_filtered;
215 static int tracing_on;
217 static LIST_HEAD(ftrace_new_addrs);
219 static DEFINE_MUTEX(ftrace_regex_lock);
222 struct ftrace_page *next;
224 struct dyn_ftrace records[];
227 #define ENTRIES_PER_PAGE \
228 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
230 /* estimate from running different kernels */
231 #define NR_TO_INIT 10000
233 static struct ftrace_page *ftrace_pages_start;
234 static struct ftrace_page *ftrace_pages;
236 static struct dyn_ftrace *ftrace_free_records;
239 #ifdef CONFIG_KPROBES
241 static int frozen_record_count;
243 static inline void freeze_record(struct dyn_ftrace *rec)
245 if (!(rec->flags & FTRACE_FL_FROZEN)) {
246 rec->flags |= FTRACE_FL_FROZEN;
247 frozen_record_count++;
251 static inline void unfreeze_record(struct dyn_ftrace *rec)
253 if (rec->flags & FTRACE_FL_FROZEN) {
254 rec->flags &= ~FTRACE_FL_FROZEN;
255 frozen_record_count--;
259 static inline int record_frozen(struct dyn_ftrace *rec)
261 return rec->flags & FTRACE_FL_FROZEN;
264 # define freeze_record(rec) ({ 0; })
265 # define unfreeze_record(rec) ({ 0; })
266 # define record_frozen(rec) ({ 0; })
267 #endif /* CONFIG_KPROBES */
269 static void ftrace_free_rec(struct dyn_ftrace *rec)
271 rec->ip = (unsigned long)ftrace_free_records;
272 ftrace_free_records = rec;
273 rec->flags |= FTRACE_FL_FREE;
276 void ftrace_release(void *start, unsigned long size)
278 struct dyn_ftrace *rec;
279 struct ftrace_page *pg;
280 unsigned long s = (unsigned long)start;
281 unsigned long e = s + size;
284 if (ftrace_disabled || !start)
287 /* should not be called from interrupt context */
288 spin_lock(&ftrace_lock);
290 for (pg = ftrace_pages_start; pg; pg = pg->next) {
291 for (i = 0; i < pg->index; i++) {
292 rec = &pg->records[i];
294 if ((rec->ip >= s) && (rec->ip < e))
295 ftrace_free_rec(rec);
298 spin_unlock(&ftrace_lock);
301 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
303 struct dyn_ftrace *rec;
305 /* First check for freed records */
306 if (ftrace_free_records) {
307 rec = ftrace_free_records;
309 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
310 FTRACE_WARN_ON_ONCE(1);
311 ftrace_free_records = NULL;
315 ftrace_free_records = (void *)rec->ip;
316 memset(rec, 0, sizeof(*rec));
320 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
321 if (!ftrace_pages->next) {
322 /* allocate another page */
324 (void *)get_zeroed_page(GFP_KERNEL);
325 if (!ftrace_pages->next)
328 ftrace_pages = ftrace_pages->next;
331 return &ftrace_pages->records[ftrace_pages->index++];
334 static struct dyn_ftrace *
335 ftrace_record_ip(unsigned long ip)
337 struct dyn_ftrace *rec;
339 if (!ftrace_enabled || ftrace_disabled)
342 rec = ftrace_alloc_dyn_node(ip);
348 list_add(&rec->list, &ftrace_new_addrs);
353 #define FTRACE_ADDR ((long)(ftrace_caller))
356 __ftrace_replace_code(struct dyn_ftrace *rec,
357 unsigned char *old, unsigned char *new, int enable)
359 unsigned long ip, fl;
363 if (ftrace_filtered && enable) {
365 * If filtering is on:
367 * If this record is set to be filtered and
368 * is enabled then do nothing.
370 * If this record is set to be filtered and
371 * it is not enabled, enable it.
373 * If this record is not set to be filtered
374 * and it is not enabled do nothing.
376 * If this record is set not to trace then
379 * If this record is set not to trace and
380 * it is enabled then disable it.
382 * If this record is not set to be filtered and
383 * it is enabled, disable it.
386 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
389 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
390 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
391 !fl || (fl == FTRACE_FL_NOTRACE))
395 * If it is enabled disable it,
396 * otherwise enable it!
398 if (fl & FTRACE_FL_ENABLED) {
399 /* swap new and old */
401 old = ftrace_call_replace(ip, FTRACE_ADDR);
402 rec->flags &= ~FTRACE_FL_ENABLED;
404 new = ftrace_call_replace(ip, FTRACE_ADDR);
405 rec->flags |= FTRACE_FL_ENABLED;
411 * If this record is set not to trace and is
412 * not enabled, do nothing.
414 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
415 if (fl == FTRACE_FL_NOTRACE)
418 new = ftrace_call_replace(ip, FTRACE_ADDR);
420 old = ftrace_call_replace(ip, FTRACE_ADDR);
423 if (rec->flags & FTRACE_FL_ENABLED)
425 rec->flags |= FTRACE_FL_ENABLED;
427 if (!(rec->flags & FTRACE_FL_ENABLED))
429 rec->flags &= ~FTRACE_FL_ENABLED;
433 return ftrace_modify_code(ip, old, new);
436 static void ftrace_replace_code(int enable)
439 unsigned char *new = NULL, *old = NULL;
440 struct dyn_ftrace *rec;
441 struct ftrace_page *pg;
444 old = ftrace_nop_replace();
446 new = ftrace_nop_replace();
448 for (pg = ftrace_pages_start; pg; pg = pg->next) {
449 for (i = 0; i < pg->index; i++) {
450 rec = &pg->records[i];
452 /* don't modify code that has already faulted */
453 if (rec->flags & FTRACE_FL_FAILED)
456 /* ignore updates to this record's mcount site */
457 if (get_kprobe((void *)rec->ip)) {
461 unfreeze_record(rec);
464 failed = __ftrace_replace_code(rec, old, new, enable);
465 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
466 rec->flags |= FTRACE_FL_FAILED;
467 if ((system_state == SYSTEM_BOOTING) ||
468 !core_kernel_text(rec->ip)) {
469 ftrace_free_rec(rec);
476 static void print_ip_ins(const char *fmt, unsigned char *p)
480 printk(KERN_CONT "%s", fmt);
482 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
483 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
487 ftrace_code_disable(struct dyn_ftrace *rec)
490 unsigned char *nop, *call;
495 nop = ftrace_nop_replace();
496 call = ftrace_call_replace(ip, mcount_addr);
498 ret = ftrace_modify_code(ip, call, nop);
502 FTRACE_WARN_ON_ONCE(1);
503 pr_info("ftrace faulted on modifying ");
507 FTRACE_WARN_ON_ONCE(1);
508 pr_info("ftrace failed to modify ");
510 print_ip_ins(" expected: ", call);
511 print_ip_ins(" actual: ", (unsigned char *)ip);
512 print_ip_ins(" replace: ", nop);
513 printk(KERN_CONT "\n");
516 FTRACE_WARN_ON_ONCE(1);
517 pr_info("ftrace faulted on writing ");
521 FTRACE_WARN_ON_ONCE(1);
522 pr_info("ftrace faulted on unknown error ");
526 rec->flags |= FTRACE_FL_FAILED;
532 static int __ftrace_modify_code(void *data)
536 if (*command & FTRACE_ENABLE_CALLS) {
537 ftrace_replace_code(1);
539 } else if (*command & FTRACE_DISABLE_CALLS) {
540 ftrace_replace_code(0);
544 if (*command & FTRACE_UPDATE_TRACE_FUNC)
545 ftrace_update_ftrace_func(ftrace_trace_function);
550 static void ftrace_run_update_code(int command)
552 stop_machine(__ftrace_modify_code, &command, NULL);
555 static ftrace_func_t saved_ftrace_func;
556 static int ftrace_start_up;
557 static DEFINE_MUTEX(ftrace_start_lock);
559 static void ftrace_startup(void)
563 if (unlikely(ftrace_disabled))
566 mutex_lock(&ftrace_start_lock);
568 if (ftrace_start_up == 1)
569 command |= FTRACE_ENABLE_CALLS;
571 if (saved_ftrace_func != ftrace_trace_function) {
572 saved_ftrace_func = ftrace_trace_function;
573 command |= FTRACE_UPDATE_TRACE_FUNC;
576 if (!command || !ftrace_enabled)
579 ftrace_run_update_code(command);
581 mutex_unlock(&ftrace_start_lock);
584 static void ftrace_shutdown(void)
588 if (unlikely(ftrace_disabled))
591 mutex_lock(&ftrace_start_lock);
593 if (!ftrace_start_up)
594 command |= FTRACE_DISABLE_CALLS;
596 if (saved_ftrace_func != ftrace_trace_function) {
597 saved_ftrace_func = ftrace_trace_function;
598 command |= FTRACE_UPDATE_TRACE_FUNC;
601 if (!command || !ftrace_enabled)
604 ftrace_run_update_code(command);
606 mutex_unlock(&ftrace_start_lock);
609 static void ftrace_startup_sysctl(void)
611 int command = FTRACE_ENABLE_MCOUNT;
613 if (unlikely(ftrace_disabled))
616 mutex_lock(&ftrace_start_lock);
617 /* Force update next time */
618 saved_ftrace_func = NULL;
619 /* ftrace_start_up is true if we want ftrace running */
621 command |= FTRACE_ENABLE_CALLS;
623 ftrace_run_update_code(command);
624 mutex_unlock(&ftrace_start_lock);
627 static void ftrace_shutdown_sysctl(void)
629 int command = FTRACE_DISABLE_MCOUNT;
631 if (unlikely(ftrace_disabled))
634 mutex_lock(&ftrace_start_lock);
635 /* ftrace_start_up is true if ftrace is running */
637 command |= FTRACE_DISABLE_CALLS;
639 ftrace_run_update_code(command);
640 mutex_unlock(&ftrace_start_lock);
643 static cycle_t ftrace_update_time;
644 static unsigned long ftrace_update_cnt;
645 unsigned long ftrace_update_tot_cnt;
647 static int ftrace_update_code(void)
649 struct dyn_ftrace *p, *t;
652 start = ftrace_now(raw_smp_processor_id());
653 ftrace_update_cnt = 0;
655 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
657 /* If something went wrong, bail without enabling anything */
658 if (unlikely(ftrace_disabled))
661 list_del_init(&p->list);
663 /* convert record (i.e, patch mcount-call with NOP) */
664 if (ftrace_code_disable(p)) {
665 p->flags |= FTRACE_FL_CONVERTED;
671 stop = ftrace_now(raw_smp_processor_id());
672 ftrace_update_time = stop - start;
673 ftrace_update_tot_cnt += ftrace_update_cnt;
678 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
680 struct ftrace_page *pg;
684 /* allocate a few pages */
685 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
686 if (!ftrace_pages_start)
690 * Allocate a few more pages.
692 * TODO: have some parser search vmlinux before
693 * final linking to find all calls to ftrace.
695 * a) know how many pages to allocate.
697 * b) set up the table then.
699 * The dynamic code is still necessary for
703 pg = ftrace_pages = ftrace_pages_start;
705 cnt = num_to_init / ENTRIES_PER_PAGE;
706 pr_info("ftrace: allocating %ld entries in %d pages\n",
709 for (i = 0; i < cnt; i++) {
710 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
712 /* If we fail, we'll try later anyway */
723 FTRACE_ITER_FILTER = (1 << 0),
724 FTRACE_ITER_CONT = (1 << 1),
725 FTRACE_ITER_NOTRACE = (1 << 2),
726 FTRACE_ITER_FAILURES = (1 << 3),
729 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
731 struct ftrace_iterator {
733 struct ftrace_page *pg;
736 unsigned char buffer[FTRACE_BUFF_MAX+1];
742 t_next(struct seq_file *m, void *v, loff_t *pos)
744 struct ftrace_iterator *iter = m->private;
745 struct dyn_ftrace *rec = NULL;
749 /* should not be called from interrupt context */
750 spin_lock(&ftrace_lock);
752 if (iter->idx >= iter->pg->index) {
753 if (iter->pg->next) {
754 iter->pg = iter->pg->next;
759 rec = &iter->pg->records[iter->idx++];
760 if ((rec->flags & FTRACE_FL_FREE) ||
762 (!(iter->flags & FTRACE_ITER_FAILURES) &&
763 (rec->flags & FTRACE_FL_FAILED)) ||
765 ((iter->flags & FTRACE_ITER_FAILURES) &&
766 !(rec->flags & FTRACE_FL_FAILED)) ||
768 ((iter->flags & FTRACE_ITER_FILTER) &&
769 !(rec->flags & FTRACE_FL_FILTER)) ||
771 ((iter->flags & FTRACE_ITER_NOTRACE) &&
772 !(rec->flags & FTRACE_FL_NOTRACE))) {
777 spin_unlock(&ftrace_lock);
784 static void *t_start(struct seq_file *m, loff_t *pos)
786 struct ftrace_iterator *iter = m->private;
790 if (*pos != iter->pos) {
791 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
795 p = t_next(m, p, &l);
801 static void t_stop(struct seq_file *m, void *p)
805 static int t_show(struct seq_file *m, void *v)
807 struct dyn_ftrace *rec = v;
808 char str[KSYM_SYMBOL_LEN];
813 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
815 seq_printf(m, "%s\n", str);
820 static struct seq_operations show_ftrace_seq_ops = {
828 ftrace_avail_open(struct inode *inode, struct file *file)
830 struct ftrace_iterator *iter;
833 if (unlikely(ftrace_disabled))
836 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
840 iter->pg = ftrace_pages_start;
843 ret = seq_open(file, &show_ftrace_seq_ops);
845 struct seq_file *m = file->private_data;
855 int ftrace_avail_release(struct inode *inode, struct file *file)
857 struct seq_file *m = (struct seq_file *)file->private_data;
858 struct ftrace_iterator *iter = m->private;
860 seq_release(inode, file);
867 ftrace_failures_open(struct inode *inode, struct file *file)
871 struct ftrace_iterator *iter;
873 ret = ftrace_avail_open(inode, file);
875 m = (struct seq_file *)file->private_data;
876 iter = (struct ftrace_iterator *)m->private;
877 iter->flags = FTRACE_ITER_FAILURES;
884 static void ftrace_filter_reset(int enable)
886 struct ftrace_page *pg;
887 struct dyn_ftrace *rec;
888 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
891 /* should not be called from interrupt context */
892 spin_lock(&ftrace_lock);
895 pg = ftrace_pages_start;
897 for (i = 0; i < pg->index; i++) {
898 rec = &pg->records[i];
899 if (rec->flags & FTRACE_FL_FAILED)
905 spin_unlock(&ftrace_lock);
909 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
911 struct ftrace_iterator *iter;
914 if (unlikely(ftrace_disabled))
917 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
921 mutex_lock(&ftrace_regex_lock);
922 if ((file->f_mode & FMODE_WRITE) &&
923 !(file->f_flags & O_APPEND))
924 ftrace_filter_reset(enable);
926 if (file->f_mode & FMODE_READ) {
927 iter->pg = ftrace_pages_start;
929 iter->flags = enable ? FTRACE_ITER_FILTER :
932 ret = seq_open(file, &show_ftrace_seq_ops);
934 struct seq_file *m = file->private_data;
939 file->private_data = iter;
940 mutex_unlock(&ftrace_regex_lock);
946 ftrace_filter_open(struct inode *inode, struct file *file)
948 return ftrace_regex_open(inode, file, 1);
952 ftrace_notrace_open(struct inode *inode, struct file *file)
954 return ftrace_regex_open(inode, file, 0);
958 ftrace_regex_read(struct file *file, char __user *ubuf,
959 size_t cnt, loff_t *ppos)
961 if (file->f_mode & FMODE_READ)
962 return seq_read(file, ubuf, cnt, ppos);
968 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
972 if (file->f_mode & FMODE_READ)
973 ret = seq_lseek(file, offset, origin);
975 file->f_pos = ret = 1;
988 ftrace_match(unsigned char *buff, int len, int enable)
990 char str[KSYM_SYMBOL_LEN];
992 struct ftrace_page *pg;
993 struct dyn_ftrace *rec;
994 int type = MATCH_FULL;
995 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
996 unsigned i, match = 0, search_len = 0;
998 for (i = 0; i < len; i++) {
999 if (buff[i] == '*') {
1001 search = buff + i + 1;
1002 type = MATCH_END_ONLY;
1003 search_len = len - (i + 1);
1005 if (type == MATCH_END_ONLY) {
1006 type = MATCH_MIDDLE_ONLY;
1009 type = MATCH_FRONT_ONLY;
1017 /* should not be called from interrupt context */
1018 spin_lock(&ftrace_lock);
1020 ftrace_filtered = 1;
1021 pg = ftrace_pages_start;
1023 for (i = 0; i < pg->index; i++) {
1027 rec = &pg->records[i];
1028 if (rec->flags & FTRACE_FL_FAILED)
1030 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1033 if (strcmp(str, buff) == 0)
1036 case MATCH_FRONT_ONLY:
1037 if (memcmp(str, buff, match) == 0)
1040 case MATCH_MIDDLE_ONLY:
1041 if (strstr(str, search))
1044 case MATCH_END_ONLY:
1045 ptr = strstr(str, search);
1046 if (ptr && (ptr[search_len] == 0))
1055 spin_unlock(&ftrace_lock);
1059 ftrace_regex_write(struct file *file, const char __user *ubuf,
1060 size_t cnt, loff_t *ppos, int enable)
1062 struct ftrace_iterator *iter;
1067 if (!cnt || cnt < 0)
1070 mutex_lock(&ftrace_regex_lock);
1072 if (file->f_mode & FMODE_READ) {
1073 struct seq_file *m = file->private_data;
1076 iter = file->private_data;
1079 iter->flags &= ~FTRACE_ITER_CONT;
1080 iter->buffer_idx = 0;
1083 ret = get_user(ch, ubuf++);
1089 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1090 /* skip white space */
1091 while (cnt && isspace(ch)) {
1092 ret = get_user(ch, ubuf++);
1100 file->f_pos += read;
1105 iter->buffer_idx = 0;
1108 while (cnt && !isspace(ch)) {
1109 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1110 iter->buffer[iter->buffer_idx++] = ch;
1115 ret = get_user(ch, ubuf++);
1124 iter->buffer[iter->buffer_idx] = 0;
1125 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1126 iter->buffer_idx = 0;
1128 iter->flags |= FTRACE_ITER_CONT;
1131 file->f_pos += read;
1135 mutex_unlock(&ftrace_regex_lock);
1141 ftrace_filter_write(struct file *file, const char __user *ubuf,
1142 size_t cnt, loff_t *ppos)
1144 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1148 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1149 size_t cnt, loff_t *ppos)
1151 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1155 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1157 if (unlikely(ftrace_disabled))
1160 mutex_lock(&ftrace_regex_lock);
1162 ftrace_filter_reset(enable);
1164 ftrace_match(buf, len, enable);
1165 mutex_unlock(&ftrace_regex_lock);
1169 * ftrace_set_filter - set a function to filter on in ftrace
1170 * @buf - the string that holds the function filter text.
1171 * @len - the length of the string.
1172 * @reset - non zero to reset all filters before applying this filter.
1174 * Filters denote which functions should be enabled when tracing is enabled.
1175 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1177 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1179 ftrace_set_regex(buf, len, reset, 1);
1183 * ftrace_set_notrace - set a function to not trace in ftrace
1184 * @buf - the string that holds the function notrace text.
1185 * @len - the length of the string.
1186 * @reset - non zero to reset all filters before applying this filter.
1188 * Notrace Filters denote which functions should not be enabled when tracing
1189 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1192 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1194 ftrace_set_regex(buf, len, reset, 0);
1198 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1200 struct seq_file *m = (struct seq_file *)file->private_data;
1201 struct ftrace_iterator *iter;
1203 mutex_lock(&ftrace_regex_lock);
1204 if (file->f_mode & FMODE_READ) {
1207 seq_release(inode, file);
1209 iter = file->private_data;
1211 if (iter->buffer_idx) {
1213 iter->buffer[iter->buffer_idx] = 0;
1214 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1217 mutex_lock(&ftrace_sysctl_lock);
1218 mutex_lock(&ftrace_start_lock);
1219 if (iter->filtered && ftrace_start_up && ftrace_enabled)
1220 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1221 mutex_unlock(&ftrace_start_lock);
1222 mutex_unlock(&ftrace_sysctl_lock);
1225 mutex_unlock(&ftrace_regex_lock);
1230 ftrace_filter_release(struct inode *inode, struct file *file)
1232 return ftrace_regex_release(inode, file, 1);
1236 ftrace_notrace_release(struct inode *inode, struct file *file)
1238 return ftrace_regex_release(inode, file, 0);
1241 static struct file_operations ftrace_avail_fops = {
1242 .open = ftrace_avail_open,
1244 .llseek = seq_lseek,
1245 .release = ftrace_avail_release,
1248 static struct file_operations ftrace_failures_fops = {
1249 .open = ftrace_failures_open,
1251 .llseek = seq_lseek,
1252 .release = ftrace_avail_release,
1255 static struct file_operations ftrace_filter_fops = {
1256 .open = ftrace_filter_open,
1257 .read = ftrace_regex_read,
1258 .write = ftrace_filter_write,
1259 .llseek = ftrace_regex_lseek,
1260 .release = ftrace_filter_release,
1263 static struct file_operations ftrace_notrace_fops = {
1264 .open = ftrace_notrace_open,
1265 .read = ftrace_regex_read,
1266 .write = ftrace_notrace_write,
1267 .llseek = ftrace_regex_lseek,
1268 .release = ftrace_notrace_release,
1271 static __init int ftrace_init_debugfs(void)
1273 struct dentry *d_tracer;
1274 struct dentry *entry;
1276 d_tracer = tracing_init_dentry();
1278 entry = debugfs_create_file("available_filter_functions", 0444,
1279 d_tracer, NULL, &ftrace_avail_fops);
1281 pr_warning("Could not create debugfs "
1282 "'available_filter_functions' entry\n");
1284 entry = debugfs_create_file("failures", 0444,
1285 d_tracer, NULL, &ftrace_failures_fops);
1287 pr_warning("Could not create debugfs 'failures' entry\n");
1289 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1290 NULL, &ftrace_filter_fops);
1292 pr_warning("Could not create debugfs "
1293 "'set_ftrace_filter' entry\n");
1295 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1296 NULL, &ftrace_notrace_fops);
1298 pr_warning("Could not create debugfs "
1299 "'set_ftrace_notrace' entry\n");
1304 fs_initcall(ftrace_init_debugfs);
1306 static int ftrace_convert_nops(unsigned long *start,
1311 unsigned long flags;
1313 mutex_lock(&ftrace_start_lock);
1316 addr = ftrace_call_adjust(*p++);
1317 ftrace_record_ip(addr);
1320 /* disable interrupts to prevent kstop machine */
1321 local_irq_save(flags);
1322 ftrace_update_code();
1323 local_irq_restore(flags);
1324 mutex_unlock(&ftrace_start_lock);
1329 void ftrace_init_module(unsigned long *start, unsigned long *end)
1331 if (ftrace_disabled || start == end)
1333 ftrace_convert_nops(start, end);
1336 extern unsigned long __start_mcount_loc[];
1337 extern unsigned long __stop_mcount_loc[];
1339 void __init ftrace_init(void)
1341 unsigned long count, addr, flags;
1344 /* Keep the ftrace pointer to the stub */
1345 addr = (unsigned long)ftrace_stub;
1347 local_irq_save(flags);
1348 ftrace_dyn_arch_init(&addr);
1349 local_irq_restore(flags);
1351 /* ftrace_dyn_arch_init places the return code in addr */
1355 count = __stop_mcount_loc - __start_mcount_loc;
1357 ret = ftrace_dyn_table_alloc(count);
1361 last_ftrace_enabled = ftrace_enabled = 1;
1363 ret = ftrace_convert_nops(__start_mcount_loc,
1368 ftrace_disabled = 1;
1373 static int __init ftrace_nodyn_init(void)
1378 device_initcall(ftrace_nodyn_init);
1380 # define ftrace_startup() do { } while (0)
1381 # define ftrace_shutdown() do { } while (0)
1382 # define ftrace_startup_sysctl() do { } while (0)
1383 # define ftrace_shutdown_sysctl() do { } while (0)
1384 #endif /* CONFIG_DYNAMIC_FTRACE */
1387 * ftrace_kill - kill ftrace
1389 * This function should be used by panic code. It stops ftrace
1390 * but in a not so nice way. If you need to simply kill ftrace
1391 * from a non-atomic section, use ftrace_kill.
1393 void ftrace_kill(void)
1395 ftrace_disabled = 1;
1397 clear_ftrace_function();
1401 * register_ftrace_function - register a function for profiling
1402 * @ops - ops structure that holds the function for profiling.
1404 * Register a function to be called by all functions in the
1407 * Note: @ops->func and all the functions it calls must be labeled
1408 * with "notrace", otherwise it will go into a
1411 int register_ftrace_function(struct ftrace_ops *ops)
1415 if (unlikely(ftrace_disabled))
1418 mutex_lock(&ftrace_sysctl_lock);
1419 ret = __register_ftrace_function(ops);
1421 mutex_unlock(&ftrace_sysctl_lock);
1427 * unregister_ftrace_function - unresgister a function for profiling.
1428 * @ops - ops structure that holds the function to unregister
1430 * Unregister a function that was added to be called by ftrace profiling.
1432 int unregister_ftrace_function(struct ftrace_ops *ops)
1436 mutex_lock(&ftrace_sysctl_lock);
1437 ret = __unregister_ftrace_function(ops);
1439 mutex_unlock(&ftrace_sysctl_lock);
1445 ftrace_enable_sysctl(struct ctl_table *table, int write,
1446 struct file *file, void __user *buffer, size_t *lenp,
1451 if (unlikely(ftrace_disabled))
1454 mutex_lock(&ftrace_sysctl_lock);
1456 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1458 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1461 last_ftrace_enabled = ftrace_enabled;
1463 if (ftrace_enabled) {
1465 ftrace_startup_sysctl();
1467 /* we are starting ftrace again */
1468 if (ftrace_list != &ftrace_list_end) {
1469 if (ftrace_list->next == &ftrace_list_end)
1470 ftrace_trace_function = ftrace_list->func;
1472 ftrace_trace_function = ftrace_list_func;
1476 /* stopping ftrace calls (just send to ftrace_stub) */
1477 ftrace_trace_function = ftrace_stub;
1479 ftrace_shutdown_sysctl();
1483 mutex_unlock(&ftrace_sysctl_lock);