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->next == &ftrace_list_end)
183 ftrace_trace_function = ftrace_list->func;
187 spin_unlock(&ftrace_lock);
192 #ifdef CONFIG_DYNAMIC_FTRACE
193 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
194 # error Dynamic ftrace depends on MCOUNT_RECORD
198 * Since MCOUNT_ADDR may point to mcount itself, we do not want
199 * to get it confused by reading a reference in the code as we
200 * are parsing on objcopy output of text. Use a variable for
203 static unsigned long mcount_addr = MCOUNT_ADDR;
206 FTRACE_ENABLE_CALLS = (1 << 0),
207 FTRACE_DISABLE_CALLS = (1 << 1),
208 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
209 FTRACE_ENABLE_MCOUNT = (1 << 3),
210 FTRACE_DISABLE_MCOUNT = (1 << 4),
213 static int ftrace_filtered;
215 static LIST_HEAD(ftrace_new_addrs);
217 static DEFINE_MUTEX(ftrace_regex_lock);
220 struct ftrace_page *next;
222 struct dyn_ftrace records[];
225 #define ENTRIES_PER_PAGE \
226 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
228 /* estimate from running different kernels */
229 #define NR_TO_INIT 10000
231 static struct ftrace_page *ftrace_pages_start;
232 static struct ftrace_page *ftrace_pages;
234 static struct dyn_ftrace *ftrace_free_records;
237 #ifdef CONFIG_KPROBES
239 static int frozen_record_count;
241 static inline void freeze_record(struct dyn_ftrace *rec)
243 if (!(rec->flags & FTRACE_FL_FROZEN)) {
244 rec->flags |= FTRACE_FL_FROZEN;
245 frozen_record_count++;
249 static inline void unfreeze_record(struct dyn_ftrace *rec)
251 if (rec->flags & FTRACE_FL_FROZEN) {
252 rec->flags &= ~FTRACE_FL_FROZEN;
253 frozen_record_count--;
257 static inline int record_frozen(struct dyn_ftrace *rec)
259 return rec->flags & FTRACE_FL_FROZEN;
262 # define freeze_record(rec) ({ 0; })
263 # define unfreeze_record(rec) ({ 0; })
264 # define record_frozen(rec) ({ 0; })
265 #endif /* CONFIG_KPROBES */
267 static void ftrace_free_rec(struct dyn_ftrace *rec)
269 rec->ip = (unsigned long)ftrace_free_records;
270 ftrace_free_records = rec;
271 rec->flags |= FTRACE_FL_FREE;
274 void ftrace_release(void *start, unsigned long size)
276 struct dyn_ftrace *rec;
277 struct ftrace_page *pg;
278 unsigned long s = (unsigned long)start;
279 unsigned long e = s + size;
282 if (ftrace_disabled || !start)
285 /* should not be called from interrupt context */
286 spin_lock(&ftrace_lock);
288 for (pg = ftrace_pages_start; pg; pg = pg->next) {
289 for (i = 0; i < pg->index; i++) {
290 rec = &pg->records[i];
292 if ((rec->ip >= s) && (rec->ip < e))
293 ftrace_free_rec(rec);
296 spin_unlock(&ftrace_lock);
299 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
301 struct dyn_ftrace *rec;
303 /* First check for freed records */
304 if (ftrace_free_records) {
305 rec = ftrace_free_records;
307 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
308 FTRACE_WARN_ON_ONCE(1);
309 ftrace_free_records = NULL;
313 ftrace_free_records = (void *)rec->ip;
314 memset(rec, 0, sizeof(*rec));
318 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
319 if (!ftrace_pages->next) {
320 /* allocate another page */
322 (void *)get_zeroed_page(GFP_KERNEL);
323 if (!ftrace_pages->next)
326 ftrace_pages = ftrace_pages->next;
329 return &ftrace_pages->records[ftrace_pages->index++];
332 static struct dyn_ftrace *
333 ftrace_record_ip(unsigned long ip)
335 struct dyn_ftrace *rec;
340 rec = ftrace_alloc_dyn_node(ip);
346 list_add(&rec->list, &ftrace_new_addrs);
351 static void print_ip_ins(const char *fmt, unsigned char *p)
355 printk(KERN_CONT "%s", fmt);
357 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
358 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
361 static void ftrace_bug(int failed, unsigned long ip,
362 unsigned char *expected,
363 unsigned char *replace)
367 FTRACE_WARN_ON_ONCE(1);
368 pr_info("ftrace faulted on modifying ");
372 FTRACE_WARN_ON_ONCE(1);
373 pr_info("ftrace failed to modify ");
375 print_ip_ins(" expected: ", expected);
376 print_ip_ins(" actual: ", (unsigned char *)ip);
377 print_ip_ins(" replace: ", replace);
378 printk(KERN_CONT "\n");
381 FTRACE_WARN_ON_ONCE(1);
382 pr_info("ftrace faulted on writing ");
386 FTRACE_WARN_ON_ONCE(1);
387 pr_info("ftrace faulted on unknown error ");
392 #define FTRACE_ADDR ((long)(ftrace_caller))
395 __ftrace_replace_code(struct dyn_ftrace *rec,
396 unsigned char *old, unsigned char *new, int enable)
398 unsigned long ip, fl;
402 if (ftrace_filtered && enable) {
404 * If filtering is on:
406 * If this record is set to be filtered and
407 * is enabled then do nothing.
409 * If this record is set to be filtered and
410 * it is not enabled, enable it.
412 * If this record is not set to be filtered
413 * and it is not enabled do nothing.
415 * If this record is set not to trace then
418 * If this record is set not to trace and
419 * it is enabled then disable it.
421 * If this record is not set to be filtered and
422 * it is enabled, disable it.
425 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
428 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
429 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
430 !fl || (fl == FTRACE_FL_NOTRACE))
434 * If it is enabled disable it,
435 * otherwise enable it!
437 if (fl & FTRACE_FL_ENABLED) {
438 /* swap new and old */
440 old = ftrace_call_replace(ip, FTRACE_ADDR);
441 rec->flags &= ~FTRACE_FL_ENABLED;
443 new = ftrace_call_replace(ip, FTRACE_ADDR);
444 rec->flags |= FTRACE_FL_ENABLED;
450 * If this record is set not to trace and is
451 * not enabled, do nothing.
453 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
454 if (fl == FTRACE_FL_NOTRACE)
457 new = ftrace_call_replace(ip, FTRACE_ADDR);
459 old = ftrace_call_replace(ip, FTRACE_ADDR);
462 if (rec->flags & FTRACE_FL_ENABLED)
464 rec->flags |= FTRACE_FL_ENABLED;
466 if (!(rec->flags & FTRACE_FL_ENABLED))
468 rec->flags &= ~FTRACE_FL_ENABLED;
472 return ftrace_modify_code(ip, old, new);
475 static void ftrace_replace_code(int enable)
478 unsigned char *new = NULL, *old = NULL;
479 struct dyn_ftrace *rec;
480 struct ftrace_page *pg;
483 old = ftrace_nop_replace();
485 new = ftrace_nop_replace();
487 for (pg = ftrace_pages_start; pg; pg = pg->next) {
488 for (i = 0; i < pg->index; i++) {
489 rec = &pg->records[i];
492 * Skip over free records and records that have
495 if (rec->flags & FTRACE_FL_FREE ||
496 rec->flags & FTRACE_FL_FAILED)
499 /* ignore updates to this record's mcount site */
500 if (get_kprobe((void *)rec->ip)) {
504 unfreeze_record(rec);
507 failed = __ftrace_replace_code(rec, old, new, enable);
508 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
509 rec->flags |= FTRACE_FL_FAILED;
510 if ((system_state == SYSTEM_BOOTING) ||
511 !core_kernel_text(rec->ip)) {
512 ftrace_free_rec(rec);
514 ftrace_bug(failed, rec->ip, old, new);
521 ftrace_code_disable(struct dyn_ftrace *rec)
524 unsigned char *nop, *call;
529 nop = ftrace_nop_replace();
530 call = ftrace_call_replace(ip, mcount_addr);
532 ret = ftrace_modify_code(ip, call, nop);
534 ftrace_bug(ret, ip, call, nop);
535 rec->flags |= FTRACE_FL_FAILED;
541 static int __ftrace_modify_code(void *data)
545 if (*command & FTRACE_ENABLE_CALLS)
546 ftrace_replace_code(1);
547 else if (*command & FTRACE_DISABLE_CALLS)
548 ftrace_replace_code(0);
550 if (*command & FTRACE_UPDATE_TRACE_FUNC)
551 ftrace_update_ftrace_func(ftrace_trace_function);
556 static void ftrace_run_update_code(int command)
558 stop_machine(__ftrace_modify_code, &command, NULL);
561 static ftrace_func_t saved_ftrace_func;
562 static int ftrace_start_up;
563 static DEFINE_MUTEX(ftrace_start_lock);
565 static void ftrace_startup(void)
569 if (unlikely(ftrace_disabled))
572 mutex_lock(&ftrace_start_lock);
574 if (ftrace_start_up == 1)
575 command |= FTRACE_ENABLE_CALLS;
577 if (saved_ftrace_func != ftrace_trace_function) {
578 saved_ftrace_func = ftrace_trace_function;
579 command |= FTRACE_UPDATE_TRACE_FUNC;
582 if (!command || !ftrace_enabled)
585 ftrace_run_update_code(command);
587 mutex_unlock(&ftrace_start_lock);
590 static void ftrace_shutdown(void)
594 if (unlikely(ftrace_disabled))
597 mutex_lock(&ftrace_start_lock);
599 if (!ftrace_start_up)
600 command |= FTRACE_DISABLE_CALLS;
602 if (saved_ftrace_func != ftrace_trace_function) {
603 saved_ftrace_func = ftrace_trace_function;
604 command |= FTRACE_UPDATE_TRACE_FUNC;
607 if (!command || !ftrace_enabled)
610 ftrace_run_update_code(command);
612 mutex_unlock(&ftrace_start_lock);
615 static void ftrace_startup_sysctl(void)
617 int command = FTRACE_ENABLE_MCOUNT;
619 if (unlikely(ftrace_disabled))
622 mutex_lock(&ftrace_start_lock);
623 /* Force update next time */
624 saved_ftrace_func = NULL;
625 /* ftrace_start_up is true if we want ftrace running */
627 command |= FTRACE_ENABLE_CALLS;
629 ftrace_run_update_code(command);
630 mutex_unlock(&ftrace_start_lock);
633 static void ftrace_shutdown_sysctl(void)
635 int command = FTRACE_DISABLE_MCOUNT;
637 if (unlikely(ftrace_disabled))
640 mutex_lock(&ftrace_start_lock);
641 /* ftrace_start_up is true if ftrace is running */
643 command |= FTRACE_DISABLE_CALLS;
645 ftrace_run_update_code(command);
646 mutex_unlock(&ftrace_start_lock);
649 static cycle_t ftrace_update_time;
650 static unsigned long ftrace_update_cnt;
651 unsigned long ftrace_update_tot_cnt;
653 static int ftrace_update_code(void)
655 struct dyn_ftrace *p, *t;
658 start = ftrace_now(raw_smp_processor_id());
659 ftrace_update_cnt = 0;
661 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
663 /* If something went wrong, bail without enabling anything */
664 if (unlikely(ftrace_disabled))
667 list_del_init(&p->list);
669 /* convert record (i.e, patch mcount-call with NOP) */
670 if (ftrace_code_disable(p)) {
671 p->flags |= FTRACE_FL_CONVERTED;
677 stop = ftrace_now(raw_smp_processor_id());
678 ftrace_update_time = stop - start;
679 ftrace_update_tot_cnt += ftrace_update_cnt;
684 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
686 struct ftrace_page *pg;
690 /* allocate a few pages */
691 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
692 if (!ftrace_pages_start)
696 * Allocate a few more pages.
698 * TODO: have some parser search vmlinux before
699 * final linking to find all calls to ftrace.
701 * a) know how many pages to allocate.
703 * b) set up the table then.
705 * The dynamic code is still necessary for
709 pg = ftrace_pages = ftrace_pages_start;
711 cnt = num_to_init / ENTRIES_PER_PAGE;
712 pr_info("ftrace: allocating %ld entries in %d pages\n",
715 for (i = 0; i < cnt; i++) {
716 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
718 /* If we fail, we'll try later anyway */
729 FTRACE_ITER_FILTER = (1 << 0),
730 FTRACE_ITER_CONT = (1 << 1),
731 FTRACE_ITER_NOTRACE = (1 << 2),
732 FTRACE_ITER_FAILURES = (1 << 3),
735 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
737 struct ftrace_iterator {
739 struct ftrace_page *pg;
742 unsigned char buffer[FTRACE_BUFF_MAX+1];
748 t_next(struct seq_file *m, void *v, loff_t *pos)
750 struct ftrace_iterator *iter = m->private;
751 struct dyn_ftrace *rec = NULL;
755 /* should not be called from interrupt context */
756 spin_lock(&ftrace_lock);
758 if (iter->idx >= iter->pg->index) {
759 if (iter->pg->next) {
760 iter->pg = iter->pg->next;
765 rec = &iter->pg->records[iter->idx++];
766 if ((rec->flags & FTRACE_FL_FREE) ||
768 (!(iter->flags & FTRACE_ITER_FAILURES) &&
769 (rec->flags & FTRACE_FL_FAILED)) ||
771 ((iter->flags & FTRACE_ITER_FAILURES) &&
772 !(rec->flags & FTRACE_FL_FAILED)) ||
774 ((iter->flags & FTRACE_ITER_FILTER) &&
775 !(rec->flags & FTRACE_FL_FILTER)) ||
777 ((iter->flags & FTRACE_ITER_NOTRACE) &&
778 !(rec->flags & FTRACE_FL_NOTRACE))) {
783 spin_unlock(&ftrace_lock);
790 static void *t_start(struct seq_file *m, loff_t *pos)
792 struct ftrace_iterator *iter = m->private;
796 if (*pos != iter->pos) {
797 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
801 p = t_next(m, p, &l);
807 static void t_stop(struct seq_file *m, void *p)
811 static int t_show(struct seq_file *m, void *v)
813 struct dyn_ftrace *rec = v;
814 char str[KSYM_SYMBOL_LEN];
819 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
821 seq_printf(m, "%s\n", str);
826 static struct seq_operations show_ftrace_seq_ops = {
834 ftrace_avail_open(struct inode *inode, struct file *file)
836 struct ftrace_iterator *iter;
839 if (unlikely(ftrace_disabled))
842 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
846 iter->pg = ftrace_pages_start;
849 ret = seq_open(file, &show_ftrace_seq_ops);
851 struct seq_file *m = file->private_data;
861 int ftrace_avail_release(struct inode *inode, struct file *file)
863 struct seq_file *m = (struct seq_file *)file->private_data;
864 struct ftrace_iterator *iter = m->private;
866 seq_release(inode, file);
873 ftrace_failures_open(struct inode *inode, struct file *file)
877 struct ftrace_iterator *iter;
879 ret = ftrace_avail_open(inode, file);
881 m = (struct seq_file *)file->private_data;
882 iter = (struct ftrace_iterator *)m->private;
883 iter->flags = FTRACE_ITER_FAILURES;
890 static void ftrace_filter_reset(int enable)
892 struct ftrace_page *pg;
893 struct dyn_ftrace *rec;
894 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
897 /* should not be called from interrupt context */
898 spin_lock(&ftrace_lock);
901 pg = ftrace_pages_start;
903 for (i = 0; i < pg->index; i++) {
904 rec = &pg->records[i];
905 if (rec->flags & FTRACE_FL_FAILED)
911 spin_unlock(&ftrace_lock);
915 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
917 struct ftrace_iterator *iter;
920 if (unlikely(ftrace_disabled))
923 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
927 mutex_lock(&ftrace_regex_lock);
928 if ((file->f_mode & FMODE_WRITE) &&
929 !(file->f_flags & O_APPEND))
930 ftrace_filter_reset(enable);
932 if (file->f_mode & FMODE_READ) {
933 iter->pg = ftrace_pages_start;
935 iter->flags = enable ? FTRACE_ITER_FILTER :
938 ret = seq_open(file, &show_ftrace_seq_ops);
940 struct seq_file *m = file->private_data;
945 file->private_data = iter;
946 mutex_unlock(&ftrace_regex_lock);
952 ftrace_filter_open(struct inode *inode, struct file *file)
954 return ftrace_regex_open(inode, file, 1);
958 ftrace_notrace_open(struct inode *inode, struct file *file)
960 return ftrace_regex_open(inode, file, 0);
964 ftrace_regex_read(struct file *file, char __user *ubuf,
965 size_t cnt, loff_t *ppos)
967 if (file->f_mode & FMODE_READ)
968 return seq_read(file, ubuf, cnt, ppos);
974 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
978 if (file->f_mode & FMODE_READ)
979 ret = seq_lseek(file, offset, origin);
981 file->f_pos = ret = 1;
994 ftrace_match(unsigned char *buff, int len, int enable)
996 char str[KSYM_SYMBOL_LEN];
998 struct ftrace_page *pg;
999 struct dyn_ftrace *rec;
1000 int type = MATCH_FULL;
1001 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1002 unsigned i, match = 0, search_len = 0;
1004 for (i = 0; i < len; i++) {
1005 if (buff[i] == '*') {
1007 search = buff + i + 1;
1008 type = MATCH_END_ONLY;
1009 search_len = len - (i + 1);
1011 if (type == MATCH_END_ONLY) {
1012 type = MATCH_MIDDLE_ONLY;
1015 type = MATCH_FRONT_ONLY;
1023 /* should not be called from interrupt context */
1024 spin_lock(&ftrace_lock);
1026 ftrace_filtered = 1;
1027 pg = ftrace_pages_start;
1029 for (i = 0; i < pg->index; i++) {
1033 rec = &pg->records[i];
1034 if (rec->flags & FTRACE_FL_FAILED)
1036 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1039 if (strcmp(str, buff) == 0)
1042 case MATCH_FRONT_ONLY:
1043 if (memcmp(str, buff, match) == 0)
1046 case MATCH_MIDDLE_ONLY:
1047 if (strstr(str, search))
1050 case MATCH_END_ONLY:
1051 ptr = strstr(str, search);
1052 if (ptr && (ptr[search_len] == 0))
1061 spin_unlock(&ftrace_lock);
1065 ftrace_regex_write(struct file *file, const char __user *ubuf,
1066 size_t cnt, loff_t *ppos, int enable)
1068 struct ftrace_iterator *iter;
1073 if (!cnt || cnt < 0)
1076 mutex_lock(&ftrace_regex_lock);
1078 if (file->f_mode & FMODE_READ) {
1079 struct seq_file *m = file->private_data;
1082 iter = file->private_data;
1085 iter->flags &= ~FTRACE_ITER_CONT;
1086 iter->buffer_idx = 0;
1089 ret = get_user(ch, ubuf++);
1095 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1096 /* skip white space */
1097 while (cnt && isspace(ch)) {
1098 ret = get_user(ch, ubuf++);
1106 file->f_pos += read;
1111 iter->buffer_idx = 0;
1114 while (cnt && !isspace(ch)) {
1115 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1116 iter->buffer[iter->buffer_idx++] = ch;
1121 ret = get_user(ch, ubuf++);
1130 iter->buffer[iter->buffer_idx] = 0;
1131 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1132 iter->buffer_idx = 0;
1134 iter->flags |= FTRACE_ITER_CONT;
1137 file->f_pos += read;
1141 mutex_unlock(&ftrace_regex_lock);
1147 ftrace_filter_write(struct file *file, const char __user *ubuf,
1148 size_t cnt, loff_t *ppos)
1150 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1154 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1155 size_t cnt, loff_t *ppos)
1157 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1161 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1163 if (unlikely(ftrace_disabled))
1166 mutex_lock(&ftrace_regex_lock);
1168 ftrace_filter_reset(enable);
1170 ftrace_match(buf, len, enable);
1171 mutex_unlock(&ftrace_regex_lock);
1175 * ftrace_set_filter - set a function to filter on in ftrace
1176 * @buf - the string that holds the function filter text.
1177 * @len - the length of the string.
1178 * @reset - non zero to reset all filters before applying this filter.
1180 * Filters denote which functions should be enabled when tracing is enabled.
1181 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1183 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1185 ftrace_set_regex(buf, len, reset, 1);
1189 * ftrace_set_notrace - set a function to not trace in ftrace
1190 * @buf - the string that holds the function notrace text.
1191 * @len - the length of the string.
1192 * @reset - non zero to reset all filters before applying this filter.
1194 * Notrace Filters denote which functions should not be enabled when tracing
1195 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1198 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1200 ftrace_set_regex(buf, len, reset, 0);
1204 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1206 struct seq_file *m = (struct seq_file *)file->private_data;
1207 struct ftrace_iterator *iter;
1209 mutex_lock(&ftrace_regex_lock);
1210 if (file->f_mode & FMODE_READ) {
1213 seq_release(inode, file);
1215 iter = file->private_data;
1217 if (iter->buffer_idx) {
1219 iter->buffer[iter->buffer_idx] = 0;
1220 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1223 mutex_lock(&ftrace_sysctl_lock);
1224 mutex_lock(&ftrace_start_lock);
1225 if (iter->filtered && ftrace_start_up && ftrace_enabled)
1226 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1227 mutex_unlock(&ftrace_start_lock);
1228 mutex_unlock(&ftrace_sysctl_lock);
1231 mutex_unlock(&ftrace_regex_lock);
1236 ftrace_filter_release(struct inode *inode, struct file *file)
1238 return ftrace_regex_release(inode, file, 1);
1242 ftrace_notrace_release(struct inode *inode, struct file *file)
1244 return ftrace_regex_release(inode, file, 0);
1247 static struct file_operations ftrace_avail_fops = {
1248 .open = ftrace_avail_open,
1250 .llseek = seq_lseek,
1251 .release = ftrace_avail_release,
1254 static struct file_operations ftrace_failures_fops = {
1255 .open = ftrace_failures_open,
1257 .llseek = seq_lseek,
1258 .release = ftrace_avail_release,
1261 static struct file_operations ftrace_filter_fops = {
1262 .open = ftrace_filter_open,
1263 .read = ftrace_regex_read,
1264 .write = ftrace_filter_write,
1265 .llseek = ftrace_regex_lseek,
1266 .release = ftrace_filter_release,
1269 static struct file_operations ftrace_notrace_fops = {
1270 .open = ftrace_notrace_open,
1271 .read = ftrace_regex_read,
1272 .write = ftrace_notrace_write,
1273 .llseek = ftrace_regex_lseek,
1274 .release = ftrace_notrace_release,
1277 static __init int ftrace_init_debugfs(void)
1279 struct dentry *d_tracer;
1280 struct dentry *entry;
1282 d_tracer = tracing_init_dentry();
1284 entry = debugfs_create_file("available_filter_functions", 0444,
1285 d_tracer, NULL, &ftrace_avail_fops);
1287 pr_warning("Could not create debugfs "
1288 "'available_filter_functions' entry\n");
1290 entry = debugfs_create_file("failures", 0444,
1291 d_tracer, NULL, &ftrace_failures_fops);
1293 pr_warning("Could not create debugfs 'failures' entry\n");
1295 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1296 NULL, &ftrace_filter_fops);
1298 pr_warning("Could not create debugfs "
1299 "'set_ftrace_filter' entry\n");
1301 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1302 NULL, &ftrace_notrace_fops);
1304 pr_warning("Could not create debugfs "
1305 "'set_ftrace_notrace' entry\n");
1310 fs_initcall(ftrace_init_debugfs);
1312 static int ftrace_convert_nops(unsigned long *start,
1317 unsigned long flags;
1319 mutex_lock(&ftrace_start_lock);
1322 addr = ftrace_call_adjust(*p++);
1323 ftrace_record_ip(addr);
1326 /* disable interrupts to prevent kstop machine */
1327 local_irq_save(flags);
1328 ftrace_update_code();
1329 local_irq_restore(flags);
1330 mutex_unlock(&ftrace_start_lock);
1335 void ftrace_init_module(unsigned long *start, unsigned long *end)
1337 if (ftrace_disabled || start == end)
1339 ftrace_convert_nops(start, end);
1342 extern unsigned long __start_mcount_loc[];
1343 extern unsigned long __stop_mcount_loc[];
1345 void __init ftrace_init(void)
1347 unsigned long count, addr, flags;
1350 /* Keep the ftrace pointer to the stub */
1351 addr = (unsigned long)ftrace_stub;
1353 local_irq_save(flags);
1354 ftrace_dyn_arch_init(&addr);
1355 local_irq_restore(flags);
1357 /* ftrace_dyn_arch_init places the return code in addr */
1361 count = __stop_mcount_loc - __start_mcount_loc;
1363 ret = ftrace_dyn_table_alloc(count);
1367 last_ftrace_enabled = ftrace_enabled = 1;
1369 ret = ftrace_convert_nops(__start_mcount_loc,
1374 ftrace_disabled = 1;
1379 static int __init ftrace_nodyn_init(void)
1384 device_initcall(ftrace_nodyn_init);
1386 # define ftrace_startup() do { } while (0)
1387 # define ftrace_shutdown() do { } while (0)
1388 # define ftrace_startup_sysctl() do { } while (0)
1389 # define ftrace_shutdown_sysctl() do { } while (0)
1390 #endif /* CONFIG_DYNAMIC_FTRACE */
1393 * ftrace_kill - kill ftrace
1395 * This function should be used by panic code. It stops ftrace
1396 * but in a not so nice way. If you need to simply kill ftrace
1397 * from a non-atomic section, use ftrace_kill.
1399 void ftrace_kill(void)
1401 ftrace_disabled = 1;
1403 clear_ftrace_function();
1407 * register_ftrace_function - register a function for profiling
1408 * @ops - ops structure that holds the function for profiling.
1410 * Register a function to be called by all functions in the
1413 * Note: @ops->func and all the functions it calls must be labeled
1414 * with "notrace", otherwise it will go into a
1417 int register_ftrace_function(struct ftrace_ops *ops)
1421 if (unlikely(ftrace_disabled))
1424 mutex_lock(&ftrace_sysctl_lock);
1425 ret = __register_ftrace_function(ops);
1427 mutex_unlock(&ftrace_sysctl_lock);
1433 * unregister_ftrace_function - unresgister a function for profiling.
1434 * @ops - ops structure that holds the function to unregister
1436 * Unregister a function that was added to be called by ftrace profiling.
1438 int unregister_ftrace_function(struct ftrace_ops *ops)
1442 mutex_lock(&ftrace_sysctl_lock);
1443 ret = __unregister_ftrace_function(ops);
1445 mutex_unlock(&ftrace_sysctl_lock);
1451 ftrace_enable_sysctl(struct ctl_table *table, int write,
1452 struct file *file, void __user *buffer, size_t *lenp,
1457 if (unlikely(ftrace_disabled))
1460 mutex_lock(&ftrace_sysctl_lock);
1462 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1464 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1467 last_ftrace_enabled = ftrace_enabled;
1469 if (ftrace_enabled) {
1471 ftrace_startup_sysctl();
1473 /* we are starting ftrace again */
1474 if (ftrace_list != &ftrace_list_end) {
1475 if (ftrace_list->next == &ftrace_list_end)
1476 ftrace_trace_function = ftrace_list->func;
1478 ftrace_trace_function = ftrace_list_func;
1482 /* stopping ftrace calls (just send to ftrace_stub) */
1483 ftrace_trace_function = ftrace_stub;
1485 ftrace_shutdown_sysctl();
1489 mutex_unlock(&ftrace_sysctl_lock);
1493 #ifdef CONFIG_FUNCTION_RET_TRACER
1494 trace_function_return_t ftrace_function_return =
1495 (trace_function_return_t)ftrace_stub;
1496 void register_ftrace_return(trace_function_return_t func)
1498 ftrace_function_return = func;
1501 void unregister_ftrace_return(void)
1503 ftrace_function_return = (trace_function_return_t)ftrace_stub;