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;
51 * ftrace_disabled is set when an anomaly is discovered.
52 * ftrace_disabled is much stronger than ftrace_enabled.
54 static int ftrace_disabled __read_mostly;
56 static DEFINE_SPINLOCK(ftrace_lock);
57 static DEFINE_MUTEX(ftrace_sysctl_lock);
59 static struct ftrace_ops ftrace_list_end __read_mostly =
64 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
65 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
67 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
69 struct ftrace_ops *op = ftrace_list;
71 /* in case someone actually ports this to alpha! */
72 read_barrier_depends();
74 while (op != &ftrace_list_end) {
76 read_barrier_depends();
77 op->func(ip, parent_ip);
83 * clear_ftrace_function - reset the ftrace function
85 * This NULLs the ftrace function and in essence stops
86 * tracing. There may be lag
88 void clear_ftrace_function(void)
90 ftrace_trace_function = ftrace_stub;
93 static int __register_ftrace_function(struct ftrace_ops *ops)
95 /* should not be called from interrupt context */
96 spin_lock(&ftrace_lock);
98 ops->next = ftrace_list;
100 * We are entering ops into the ftrace_list but another
101 * CPU might be walking that list. We need to make sure
102 * the ops->next pointer is valid before another CPU sees
103 * the ops pointer included into the ftrace_list.
108 if (ftrace_enabled) {
110 * For one func, simply call it directly.
111 * For more than one func, call the chain.
113 if (ops->next == &ftrace_list_end)
114 ftrace_trace_function = ops->func;
116 ftrace_trace_function = ftrace_list_func;
119 spin_unlock(&ftrace_lock);
124 static int __unregister_ftrace_function(struct ftrace_ops *ops)
126 struct ftrace_ops **p;
129 /* should not be called from interrupt context */
130 spin_lock(&ftrace_lock);
133 * If we are removing the last function, then simply point
134 * to the ftrace_stub.
136 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
137 ftrace_trace_function = ftrace_stub;
138 ftrace_list = &ftrace_list_end;
142 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
153 if (ftrace_enabled) {
154 /* If we only have one func left, then call that directly */
155 if (ftrace_list == &ftrace_list_end ||
156 ftrace_list->next == &ftrace_list_end)
157 ftrace_trace_function = ftrace_list->func;
161 spin_unlock(&ftrace_lock);
166 #ifdef CONFIG_DYNAMIC_FTRACE
167 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
168 # error Dynamic ftrace depends on MCOUNT_RECORD
172 * Since MCOUNT_ADDR may point to mcount itself, we do not want
173 * to get it confused by reading a reference in the code as we
174 * are parsing on objcopy output of text. Use a variable for
177 static unsigned long mcount_addr = MCOUNT_ADDR;
180 FTRACE_ENABLE_CALLS = (1 << 0),
181 FTRACE_DISABLE_CALLS = (1 << 1),
182 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
183 FTRACE_ENABLE_MCOUNT = (1 << 3),
184 FTRACE_DISABLE_MCOUNT = (1 << 4),
187 static int ftrace_filtered;
188 static int tracing_on;
190 static LIST_HEAD(ftrace_new_addrs);
192 static DEFINE_MUTEX(ftrace_regex_lock);
195 struct ftrace_page *next;
197 struct dyn_ftrace records[];
200 #define ENTRIES_PER_PAGE \
201 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
203 /* estimate from running different kernels */
204 #define NR_TO_INIT 10000
206 static struct ftrace_page *ftrace_pages_start;
207 static struct ftrace_page *ftrace_pages;
209 static struct dyn_ftrace *ftrace_free_records;
212 #ifdef CONFIG_KPROBES
214 static int frozen_record_count;
216 static inline void freeze_record(struct dyn_ftrace *rec)
218 if (!(rec->flags & FTRACE_FL_FROZEN)) {
219 rec->flags |= FTRACE_FL_FROZEN;
220 frozen_record_count++;
224 static inline void unfreeze_record(struct dyn_ftrace *rec)
226 if (rec->flags & FTRACE_FL_FROZEN) {
227 rec->flags &= ~FTRACE_FL_FROZEN;
228 frozen_record_count--;
232 static inline int record_frozen(struct dyn_ftrace *rec)
234 return rec->flags & FTRACE_FL_FROZEN;
237 # define freeze_record(rec) ({ 0; })
238 # define unfreeze_record(rec) ({ 0; })
239 # define record_frozen(rec) ({ 0; })
240 #endif /* CONFIG_KPROBES */
242 static void ftrace_free_rec(struct dyn_ftrace *rec)
244 rec->ip = (unsigned long)ftrace_free_records;
245 ftrace_free_records = rec;
246 rec->flags |= FTRACE_FL_FREE;
249 void ftrace_release(void *start, unsigned long size)
251 struct dyn_ftrace *rec;
252 struct ftrace_page *pg;
253 unsigned long s = (unsigned long)start;
254 unsigned long e = s + size;
257 if (ftrace_disabled || !start)
260 /* should not be called from interrupt context */
261 spin_lock(&ftrace_lock);
263 for (pg = ftrace_pages_start; pg; pg = pg->next) {
264 for (i = 0; i < pg->index; i++) {
265 rec = &pg->records[i];
267 if ((rec->ip >= s) && (rec->ip < e))
268 ftrace_free_rec(rec);
271 spin_unlock(&ftrace_lock);
274 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
276 struct dyn_ftrace *rec;
278 /* First check for freed records */
279 if (ftrace_free_records) {
280 rec = ftrace_free_records;
282 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
283 FTRACE_WARN_ON_ONCE(1);
284 ftrace_free_records = NULL;
288 ftrace_free_records = (void *)rec->ip;
289 memset(rec, 0, sizeof(*rec));
293 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
294 if (!ftrace_pages->next) {
295 /* allocate another page */
297 (void *)get_zeroed_page(GFP_KERNEL);
298 if (!ftrace_pages->next)
301 ftrace_pages = ftrace_pages->next;
304 return &ftrace_pages->records[ftrace_pages->index++];
307 static struct dyn_ftrace *
308 ftrace_record_ip(unsigned long ip)
310 struct dyn_ftrace *rec;
312 if (!ftrace_enabled || ftrace_disabled)
315 rec = ftrace_alloc_dyn_node(ip);
321 list_add(&rec->list, &ftrace_new_addrs);
326 #define FTRACE_ADDR ((long)(ftrace_caller))
329 __ftrace_replace_code(struct dyn_ftrace *rec,
330 unsigned char *nop, int enable)
332 unsigned long ip, fl;
333 unsigned char *call, *old, *new;
338 * If this record is not to be traced and
339 * it is not enabled then do nothing.
341 * If this record is not to be traced and
342 * it is enabled then disabled it.
345 if (rec->flags & FTRACE_FL_NOTRACE) {
346 if (rec->flags & FTRACE_FL_ENABLED)
347 rec->flags &= ~FTRACE_FL_ENABLED;
351 } else if (ftrace_filtered && enable) {
356 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
358 /* Record is filtered and enabled, do nothing */
359 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
362 /* Record is not filtered and is not enabled do nothing */
366 /* Record is not filtered but enabled, disable it */
367 if (fl == FTRACE_FL_ENABLED)
368 rec->flags &= ~FTRACE_FL_ENABLED;
370 /* Otherwise record is filtered but not enabled, enable it */
371 rec->flags |= FTRACE_FL_ENABLED;
373 /* Disable or not filtered */
376 /* if record is enabled, do nothing */
377 if (rec->flags & FTRACE_FL_ENABLED)
380 rec->flags |= FTRACE_FL_ENABLED;
384 /* if record is not enabled do nothing */
385 if (!(rec->flags & FTRACE_FL_ENABLED))
388 rec->flags &= ~FTRACE_FL_ENABLED;
392 call = ftrace_call_replace(ip, FTRACE_ADDR);
394 if (rec->flags & FTRACE_FL_ENABLED) {
402 return ftrace_modify_code(ip, old, new);
405 static void ftrace_replace_code(int enable)
408 unsigned char *nop = NULL;
409 struct dyn_ftrace *rec;
410 struct ftrace_page *pg;
412 nop = ftrace_nop_replace();
414 for (pg = ftrace_pages_start; pg; pg = pg->next) {
415 for (i = 0; i < pg->index; i++) {
416 rec = &pg->records[i];
418 /* don't modify code that has already faulted */
419 if (rec->flags & FTRACE_FL_FAILED)
422 /* ignore updates to this record's mcount site */
423 if (get_kprobe((void *)rec->ip)) {
427 unfreeze_record(rec);
430 failed = __ftrace_replace_code(rec, nop, enable);
431 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
432 rec->flags |= FTRACE_FL_FAILED;
433 if ((system_state == SYSTEM_BOOTING) ||
434 !core_kernel_text(rec->ip)) {
435 ftrace_free_rec(rec);
442 static void print_ip_ins(const char *fmt, unsigned char *p)
446 printk(KERN_CONT "%s", fmt);
448 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
449 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
453 ftrace_code_disable(struct dyn_ftrace *rec)
456 unsigned char *nop, *call;
461 nop = ftrace_nop_replace();
462 call = ftrace_call_replace(ip, mcount_addr);
464 ret = ftrace_modify_code(ip, call, nop);
468 FTRACE_WARN_ON_ONCE(1);
469 pr_info("ftrace faulted on modifying ");
473 FTRACE_WARN_ON_ONCE(1);
474 pr_info("ftrace failed to modify ");
476 print_ip_ins(" expected: ", call);
477 print_ip_ins(" actual: ", (unsigned char *)ip);
478 print_ip_ins(" replace: ", nop);
479 printk(KERN_CONT "\n");
482 FTRACE_WARN_ON_ONCE(1);
483 pr_info("ftrace faulted on writing ");
487 FTRACE_WARN_ON_ONCE(1);
488 pr_info("ftrace faulted on unknown error ");
492 rec->flags |= FTRACE_FL_FAILED;
498 static int __ftrace_modify_code(void *data)
502 if (*command & FTRACE_ENABLE_CALLS) {
503 ftrace_replace_code(1);
505 } else if (*command & FTRACE_DISABLE_CALLS) {
506 ftrace_replace_code(0);
510 if (*command & FTRACE_UPDATE_TRACE_FUNC)
511 ftrace_update_ftrace_func(ftrace_trace_function);
516 static void ftrace_run_update_code(int command)
518 stop_machine(__ftrace_modify_code, &command, NULL);
521 static ftrace_func_t saved_ftrace_func;
522 static int ftrace_start;
523 static DEFINE_MUTEX(ftrace_start_lock);
525 static void ftrace_startup(void)
529 if (unlikely(ftrace_disabled))
532 mutex_lock(&ftrace_start_lock);
534 command |= FTRACE_ENABLE_CALLS;
536 if (saved_ftrace_func != ftrace_trace_function) {
537 saved_ftrace_func = ftrace_trace_function;
538 command |= FTRACE_UPDATE_TRACE_FUNC;
541 if (!command || !ftrace_enabled)
544 ftrace_run_update_code(command);
546 mutex_unlock(&ftrace_start_lock);
549 static void ftrace_shutdown(void)
553 if (unlikely(ftrace_disabled))
556 mutex_lock(&ftrace_start_lock);
559 command |= FTRACE_DISABLE_CALLS;
561 if (saved_ftrace_func != ftrace_trace_function) {
562 saved_ftrace_func = ftrace_trace_function;
563 command |= FTRACE_UPDATE_TRACE_FUNC;
566 if (!command || !ftrace_enabled)
569 ftrace_run_update_code(command);
571 mutex_unlock(&ftrace_start_lock);
574 static void ftrace_startup_sysctl(void)
576 int command = FTRACE_ENABLE_MCOUNT;
578 if (unlikely(ftrace_disabled))
581 mutex_lock(&ftrace_start_lock);
582 /* Force update next time */
583 saved_ftrace_func = NULL;
584 /* ftrace_start is true if we want ftrace running */
586 command |= FTRACE_ENABLE_CALLS;
588 ftrace_run_update_code(command);
589 mutex_unlock(&ftrace_start_lock);
592 static void ftrace_shutdown_sysctl(void)
594 int command = FTRACE_DISABLE_MCOUNT;
596 if (unlikely(ftrace_disabled))
599 mutex_lock(&ftrace_start_lock);
600 /* ftrace_start is true if ftrace is running */
602 command |= FTRACE_DISABLE_CALLS;
604 ftrace_run_update_code(command);
605 mutex_unlock(&ftrace_start_lock);
608 static cycle_t ftrace_update_time;
609 static unsigned long ftrace_update_cnt;
610 unsigned long ftrace_update_tot_cnt;
612 static int ftrace_update_code(void)
614 struct dyn_ftrace *p, *t;
617 start = ftrace_now(raw_smp_processor_id());
618 ftrace_update_cnt = 0;
620 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
622 /* If something went wrong, bail without enabling anything */
623 if (unlikely(ftrace_disabled))
626 list_del_init(&p->list);
628 /* convert record (i.e, patch mcount-call with NOP) */
629 if (ftrace_code_disable(p)) {
630 p->flags |= FTRACE_FL_CONVERTED;
636 stop = ftrace_now(raw_smp_processor_id());
637 ftrace_update_time = stop - start;
638 ftrace_update_tot_cnt += ftrace_update_cnt;
643 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
645 struct ftrace_page *pg;
649 /* allocate a few pages */
650 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
651 if (!ftrace_pages_start)
655 * Allocate a few more pages.
657 * TODO: have some parser search vmlinux before
658 * final linking to find all calls to ftrace.
660 * a) know how many pages to allocate.
662 * b) set up the table then.
664 * The dynamic code is still necessary for
668 pg = ftrace_pages = ftrace_pages_start;
670 cnt = num_to_init / ENTRIES_PER_PAGE;
671 pr_info("ftrace: allocating %ld entries in %d pages\n",
674 for (i = 0; i < cnt; i++) {
675 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
677 /* If we fail, we'll try later anyway */
688 FTRACE_ITER_FILTER = (1 << 0),
689 FTRACE_ITER_CONT = (1 << 1),
690 FTRACE_ITER_NOTRACE = (1 << 2),
691 FTRACE_ITER_FAILURES = (1 << 3),
694 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
696 struct ftrace_iterator {
698 struct ftrace_page *pg;
701 unsigned char buffer[FTRACE_BUFF_MAX+1];
707 t_next(struct seq_file *m, void *v, loff_t *pos)
709 struct ftrace_iterator *iter = m->private;
710 struct dyn_ftrace *rec = NULL;
714 /* should not be called from interrupt context */
715 spin_lock(&ftrace_lock);
717 if (iter->idx >= iter->pg->index) {
718 if (iter->pg->next) {
719 iter->pg = iter->pg->next;
724 rec = &iter->pg->records[iter->idx++];
725 if ((rec->flags & FTRACE_FL_FREE) ||
727 (!(iter->flags & FTRACE_ITER_FAILURES) &&
728 (rec->flags & FTRACE_FL_FAILED)) ||
730 ((iter->flags & FTRACE_ITER_FAILURES) &&
731 !(rec->flags & FTRACE_FL_FAILED)) ||
733 ((iter->flags & FTRACE_ITER_FILTER) &&
734 !(rec->flags & FTRACE_FL_FILTER)) ||
736 ((iter->flags & FTRACE_ITER_NOTRACE) &&
737 !(rec->flags & FTRACE_FL_NOTRACE))) {
742 spin_unlock(&ftrace_lock);
749 static void *t_start(struct seq_file *m, loff_t *pos)
751 struct ftrace_iterator *iter = m->private;
755 if (*pos != iter->pos) {
756 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
760 p = t_next(m, p, &l);
766 static void t_stop(struct seq_file *m, void *p)
770 static int t_show(struct seq_file *m, void *v)
772 struct dyn_ftrace *rec = v;
773 char str[KSYM_SYMBOL_LEN];
778 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
780 seq_printf(m, "%s\n", str);
785 static struct seq_operations show_ftrace_seq_ops = {
793 ftrace_avail_open(struct inode *inode, struct file *file)
795 struct ftrace_iterator *iter;
798 if (unlikely(ftrace_disabled))
801 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
805 iter->pg = ftrace_pages_start;
808 ret = seq_open(file, &show_ftrace_seq_ops);
810 struct seq_file *m = file->private_data;
820 int ftrace_avail_release(struct inode *inode, struct file *file)
822 struct seq_file *m = (struct seq_file *)file->private_data;
823 struct ftrace_iterator *iter = m->private;
825 seq_release(inode, file);
832 ftrace_failures_open(struct inode *inode, struct file *file)
836 struct ftrace_iterator *iter;
838 ret = ftrace_avail_open(inode, file);
840 m = (struct seq_file *)file->private_data;
841 iter = (struct ftrace_iterator *)m->private;
842 iter->flags = FTRACE_ITER_FAILURES;
849 static void ftrace_filter_reset(int enable)
851 struct ftrace_page *pg;
852 struct dyn_ftrace *rec;
853 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
856 /* should not be called from interrupt context */
857 spin_lock(&ftrace_lock);
860 pg = ftrace_pages_start;
862 for (i = 0; i < pg->index; i++) {
863 rec = &pg->records[i];
864 if (rec->flags & FTRACE_FL_FAILED)
870 spin_unlock(&ftrace_lock);
874 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
876 struct ftrace_iterator *iter;
879 if (unlikely(ftrace_disabled))
882 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
886 mutex_lock(&ftrace_regex_lock);
887 if ((file->f_mode & FMODE_WRITE) &&
888 !(file->f_flags & O_APPEND))
889 ftrace_filter_reset(enable);
891 if (file->f_mode & FMODE_READ) {
892 iter->pg = ftrace_pages_start;
894 iter->flags = enable ? FTRACE_ITER_FILTER :
897 ret = seq_open(file, &show_ftrace_seq_ops);
899 struct seq_file *m = file->private_data;
904 file->private_data = iter;
905 mutex_unlock(&ftrace_regex_lock);
911 ftrace_filter_open(struct inode *inode, struct file *file)
913 return ftrace_regex_open(inode, file, 1);
917 ftrace_notrace_open(struct inode *inode, struct file *file)
919 return ftrace_regex_open(inode, file, 0);
923 ftrace_regex_read(struct file *file, char __user *ubuf,
924 size_t cnt, loff_t *ppos)
926 if (file->f_mode & FMODE_READ)
927 return seq_read(file, ubuf, cnt, ppos);
933 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
937 if (file->f_mode & FMODE_READ)
938 ret = seq_lseek(file, offset, origin);
940 file->f_pos = ret = 1;
953 ftrace_match(unsigned char *buff, int len, int enable)
955 char str[KSYM_SYMBOL_LEN];
957 struct ftrace_page *pg;
958 struct dyn_ftrace *rec;
959 int type = MATCH_FULL;
960 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
961 unsigned i, match = 0, search_len = 0;
963 for (i = 0; i < len; i++) {
964 if (buff[i] == '*') {
966 search = buff + i + 1;
967 type = MATCH_END_ONLY;
968 search_len = len - (i + 1);
970 if (type == MATCH_END_ONLY) {
971 type = MATCH_MIDDLE_ONLY;
974 type = MATCH_FRONT_ONLY;
982 /* should not be called from interrupt context */
983 spin_lock(&ftrace_lock);
986 pg = ftrace_pages_start;
988 for (i = 0; i < pg->index; i++) {
992 rec = &pg->records[i];
993 if (rec->flags & FTRACE_FL_FAILED)
995 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
998 if (strcmp(str, buff) == 0)
1001 case MATCH_FRONT_ONLY:
1002 if (memcmp(str, buff, match) == 0)
1005 case MATCH_MIDDLE_ONLY:
1006 if (strstr(str, search))
1009 case MATCH_END_ONLY:
1010 ptr = strstr(str, search);
1011 if (ptr && (ptr[search_len] == 0))
1020 spin_unlock(&ftrace_lock);
1024 ftrace_regex_write(struct file *file, const char __user *ubuf,
1025 size_t cnt, loff_t *ppos, int enable)
1027 struct ftrace_iterator *iter;
1032 if (!cnt || cnt < 0)
1035 mutex_lock(&ftrace_regex_lock);
1037 if (file->f_mode & FMODE_READ) {
1038 struct seq_file *m = file->private_data;
1041 iter = file->private_data;
1044 iter->flags &= ~FTRACE_ITER_CONT;
1045 iter->buffer_idx = 0;
1048 ret = get_user(ch, ubuf++);
1054 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1055 /* skip white space */
1056 while (cnt && isspace(ch)) {
1057 ret = get_user(ch, ubuf++);
1065 file->f_pos += read;
1070 iter->buffer_idx = 0;
1073 while (cnt && !isspace(ch)) {
1074 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1075 iter->buffer[iter->buffer_idx++] = ch;
1080 ret = get_user(ch, ubuf++);
1089 iter->buffer[iter->buffer_idx] = 0;
1090 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1091 iter->buffer_idx = 0;
1093 iter->flags |= FTRACE_ITER_CONT;
1096 file->f_pos += read;
1100 mutex_unlock(&ftrace_regex_lock);
1106 ftrace_filter_write(struct file *file, const char __user *ubuf,
1107 size_t cnt, loff_t *ppos)
1109 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1113 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1114 size_t cnt, loff_t *ppos)
1116 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1120 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1122 if (unlikely(ftrace_disabled))
1125 mutex_lock(&ftrace_regex_lock);
1127 ftrace_filter_reset(enable);
1129 ftrace_match(buf, len, enable);
1130 mutex_unlock(&ftrace_regex_lock);
1134 * ftrace_set_filter - set a function to filter on in ftrace
1135 * @buf - the string that holds the function filter text.
1136 * @len - the length of the string.
1137 * @reset - non zero to reset all filters before applying this filter.
1139 * Filters denote which functions should be enabled when tracing is enabled.
1140 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1142 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1144 ftrace_set_regex(buf, len, reset, 1);
1148 * ftrace_set_notrace - set a function to not trace in ftrace
1149 * @buf - the string that holds the function notrace text.
1150 * @len - the length of the string.
1151 * @reset - non zero to reset all filters before applying this filter.
1153 * Notrace Filters denote which functions should not be enabled when tracing
1154 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1157 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1159 ftrace_set_regex(buf, len, reset, 0);
1163 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1165 struct seq_file *m = (struct seq_file *)file->private_data;
1166 struct ftrace_iterator *iter;
1168 mutex_lock(&ftrace_regex_lock);
1169 if (file->f_mode & FMODE_READ) {
1172 seq_release(inode, file);
1174 iter = file->private_data;
1176 if (iter->buffer_idx) {
1178 iter->buffer[iter->buffer_idx] = 0;
1179 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1182 mutex_lock(&ftrace_sysctl_lock);
1183 mutex_lock(&ftrace_start_lock);
1184 if (ftrace_start && ftrace_enabled)
1185 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1186 mutex_unlock(&ftrace_start_lock);
1187 mutex_unlock(&ftrace_sysctl_lock);
1190 mutex_unlock(&ftrace_regex_lock);
1195 ftrace_filter_release(struct inode *inode, struct file *file)
1197 return ftrace_regex_release(inode, file, 1);
1201 ftrace_notrace_release(struct inode *inode, struct file *file)
1203 return ftrace_regex_release(inode, file, 0);
1206 static struct file_operations ftrace_avail_fops = {
1207 .open = ftrace_avail_open,
1209 .llseek = seq_lseek,
1210 .release = ftrace_avail_release,
1213 static struct file_operations ftrace_failures_fops = {
1214 .open = ftrace_failures_open,
1216 .llseek = seq_lseek,
1217 .release = ftrace_avail_release,
1220 static struct file_operations ftrace_filter_fops = {
1221 .open = ftrace_filter_open,
1222 .read = ftrace_regex_read,
1223 .write = ftrace_filter_write,
1224 .llseek = ftrace_regex_lseek,
1225 .release = ftrace_filter_release,
1228 static struct file_operations ftrace_notrace_fops = {
1229 .open = ftrace_notrace_open,
1230 .read = ftrace_regex_read,
1231 .write = ftrace_notrace_write,
1232 .llseek = ftrace_regex_lseek,
1233 .release = ftrace_notrace_release,
1236 static __init int ftrace_init_debugfs(void)
1238 struct dentry *d_tracer;
1239 struct dentry *entry;
1241 d_tracer = tracing_init_dentry();
1243 entry = debugfs_create_file("available_filter_functions", 0444,
1244 d_tracer, NULL, &ftrace_avail_fops);
1246 pr_warning("Could not create debugfs "
1247 "'available_filter_functions' entry\n");
1249 entry = debugfs_create_file("failures", 0444,
1250 d_tracer, NULL, &ftrace_failures_fops);
1252 pr_warning("Could not create debugfs 'failures' entry\n");
1254 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1255 NULL, &ftrace_filter_fops);
1257 pr_warning("Could not create debugfs "
1258 "'set_ftrace_filter' entry\n");
1260 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1261 NULL, &ftrace_notrace_fops);
1263 pr_warning("Could not create debugfs "
1264 "'set_ftrace_notrace' entry\n");
1269 fs_initcall(ftrace_init_debugfs);
1271 static int ftrace_convert_nops(unsigned long *start,
1276 unsigned long flags;
1278 mutex_lock(&ftrace_start_lock);
1281 addr = ftrace_call_adjust(*p++);
1282 ftrace_record_ip(addr);
1285 /* disable interrupts to prevent kstop machine */
1286 local_irq_save(flags);
1287 ftrace_update_code();
1288 local_irq_restore(flags);
1289 mutex_unlock(&ftrace_start_lock);
1294 void ftrace_init_module(unsigned long *start, unsigned long *end)
1296 if (ftrace_disabled || start == end)
1298 ftrace_convert_nops(start, end);
1301 extern unsigned long __start_mcount_loc[];
1302 extern unsigned long __stop_mcount_loc[];
1304 void __init ftrace_init(void)
1306 unsigned long count, addr, flags;
1309 /* Keep the ftrace pointer to the stub */
1310 addr = (unsigned long)ftrace_stub;
1312 local_irq_save(flags);
1313 ftrace_dyn_arch_init(&addr);
1314 local_irq_restore(flags);
1316 /* ftrace_dyn_arch_init places the return code in addr */
1320 count = __stop_mcount_loc - __start_mcount_loc;
1322 ret = ftrace_dyn_table_alloc(count);
1326 last_ftrace_enabled = ftrace_enabled = 1;
1328 ret = ftrace_convert_nops(__start_mcount_loc,
1333 ftrace_disabled = 1;
1338 static int __init ftrace_nodyn_init(void)
1343 device_initcall(ftrace_nodyn_init);
1345 # define ftrace_startup() do { } while (0)
1346 # define ftrace_shutdown() do { } while (0)
1347 # define ftrace_startup_sysctl() do { } while (0)
1348 # define ftrace_shutdown_sysctl() do { } while (0)
1349 #endif /* CONFIG_DYNAMIC_FTRACE */
1352 * ftrace_kill - kill ftrace
1354 * This function should be used by panic code. It stops ftrace
1355 * but in a not so nice way. If you need to simply kill ftrace
1356 * from a non-atomic section, use ftrace_kill.
1358 void ftrace_kill(void)
1360 ftrace_disabled = 1;
1362 clear_ftrace_function();
1366 * register_ftrace_function - register a function for profiling
1367 * @ops - ops structure that holds the function for profiling.
1369 * Register a function to be called by all functions in the
1372 * Note: @ops->func and all the functions it calls must be labeled
1373 * with "notrace", otherwise it will go into a
1376 int register_ftrace_function(struct ftrace_ops *ops)
1380 if (unlikely(ftrace_disabled))
1383 mutex_lock(&ftrace_sysctl_lock);
1384 ret = __register_ftrace_function(ops);
1386 mutex_unlock(&ftrace_sysctl_lock);
1392 * unregister_ftrace_function - unresgister a function for profiling.
1393 * @ops - ops structure that holds the function to unregister
1395 * Unregister a function that was added to be called by ftrace profiling.
1397 int unregister_ftrace_function(struct ftrace_ops *ops)
1401 mutex_lock(&ftrace_sysctl_lock);
1402 ret = __unregister_ftrace_function(ops);
1404 mutex_unlock(&ftrace_sysctl_lock);
1410 ftrace_enable_sysctl(struct ctl_table *table, int write,
1411 struct file *file, void __user *buffer, size_t *lenp,
1416 if (unlikely(ftrace_disabled))
1419 mutex_lock(&ftrace_sysctl_lock);
1421 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1423 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1426 last_ftrace_enabled = ftrace_enabled;
1428 if (ftrace_enabled) {
1430 ftrace_startup_sysctl();
1432 /* we are starting ftrace again */
1433 if (ftrace_list != &ftrace_list_end) {
1434 if (ftrace_list->next == &ftrace_list_end)
1435 ftrace_trace_function = ftrace_list->func;
1437 ftrace_trace_function = ftrace_list_func;
1441 /* stopping ftrace calls (just send to ftrace_stub) */
1442 ftrace_trace_function = ftrace_stub;
1444 ftrace_shutdown_sysctl();
1448 mutex_unlock(&ftrace_sysctl_lock);