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;
189 static LIST_HEAD(ftrace_new_addrs);
191 static DEFINE_MUTEX(ftrace_regex_lock);
194 struct ftrace_page *next;
196 struct dyn_ftrace records[];
199 #define ENTRIES_PER_PAGE \
200 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
202 /* estimate from running different kernels */
203 #define NR_TO_INIT 10000
205 static struct ftrace_page *ftrace_pages_start;
206 static struct ftrace_page *ftrace_pages;
208 static struct dyn_ftrace *ftrace_free_records;
211 #ifdef CONFIG_KPROBES
213 static int frozen_record_count;
215 static inline void freeze_record(struct dyn_ftrace *rec)
217 if (!(rec->flags & FTRACE_FL_FROZEN)) {
218 rec->flags |= FTRACE_FL_FROZEN;
219 frozen_record_count++;
223 static inline void unfreeze_record(struct dyn_ftrace *rec)
225 if (rec->flags & FTRACE_FL_FROZEN) {
226 rec->flags &= ~FTRACE_FL_FROZEN;
227 frozen_record_count--;
231 static inline int record_frozen(struct dyn_ftrace *rec)
233 return rec->flags & FTRACE_FL_FROZEN;
236 # define freeze_record(rec) ({ 0; })
237 # define unfreeze_record(rec) ({ 0; })
238 # define record_frozen(rec) ({ 0; })
239 #endif /* CONFIG_KPROBES */
241 static void ftrace_free_rec(struct dyn_ftrace *rec)
243 rec->ip = (unsigned long)ftrace_free_records;
244 ftrace_free_records = rec;
245 rec->flags |= FTRACE_FL_FREE;
248 void ftrace_release(void *start, unsigned long size)
250 struct dyn_ftrace *rec;
251 struct ftrace_page *pg;
252 unsigned long s = (unsigned long)start;
253 unsigned long e = s + size;
256 if (ftrace_disabled || !start)
259 /* should not be called from interrupt context */
260 spin_lock(&ftrace_lock);
262 for (pg = ftrace_pages_start; pg; pg = pg->next) {
263 for (i = 0; i < pg->index; i++) {
264 rec = &pg->records[i];
266 if ((rec->ip >= s) && (rec->ip < e))
267 ftrace_free_rec(rec);
270 spin_unlock(&ftrace_lock);
273 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
275 struct dyn_ftrace *rec;
277 /* First check for freed records */
278 if (ftrace_free_records) {
279 rec = ftrace_free_records;
281 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
282 FTRACE_WARN_ON_ONCE(1);
283 ftrace_free_records = NULL;
287 ftrace_free_records = (void *)rec->ip;
288 memset(rec, 0, sizeof(*rec));
292 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
293 if (!ftrace_pages->next) {
294 /* allocate another page */
296 (void *)get_zeroed_page(GFP_KERNEL);
297 if (!ftrace_pages->next)
300 ftrace_pages = ftrace_pages->next;
303 return &ftrace_pages->records[ftrace_pages->index++];
306 static struct dyn_ftrace *
307 ftrace_record_ip(unsigned long ip)
309 struct dyn_ftrace *rec;
311 if (!ftrace_enabled || ftrace_disabled)
314 rec = ftrace_alloc_dyn_node(ip);
320 list_add(&rec->list, &ftrace_new_addrs);
325 #define FTRACE_ADDR ((long)(ftrace_caller))
328 __ftrace_replace_code(struct dyn_ftrace *rec,
329 unsigned char *old, unsigned char *new, int enable)
331 unsigned long ip, fl;
335 if (ftrace_filtered && enable) {
337 * If filtering is on:
339 * If this record is set to be filtered and
340 * is enabled then do nothing.
342 * If this record is set to be filtered and
343 * it is not enabled, enable it.
345 * If this record is not set to be filtered
346 * and it is not enabled do nothing.
348 * If this record is set not to trace then
351 * If this record is set not to trace and
352 * it is enabled then disable it.
354 * If this record is not set to be filtered and
355 * it is enabled, disable it.
358 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
361 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
362 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
363 !fl || (fl == FTRACE_FL_NOTRACE))
367 * If it is enabled disable it,
368 * otherwise enable it!
370 if (fl & FTRACE_FL_ENABLED) {
371 /* swap new and old */
373 old = ftrace_call_replace(ip, FTRACE_ADDR);
374 rec->flags &= ~FTRACE_FL_ENABLED;
376 new = ftrace_call_replace(ip, FTRACE_ADDR);
377 rec->flags |= FTRACE_FL_ENABLED;
383 * If this record is set not to trace and is
384 * not enabled, do nothing.
386 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
387 if (fl == FTRACE_FL_NOTRACE)
390 new = ftrace_call_replace(ip, FTRACE_ADDR);
392 old = ftrace_call_replace(ip, FTRACE_ADDR);
395 if (rec->flags & FTRACE_FL_ENABLED)
397 rec->flags |= FTRACE_FL_ENABLED;
399 if (!(rec->flags & FTRACE_FL_ENABLED))
401 rec->flags &= ~FTRACE_FL_ENABLED;
405 return ftrace_modify_code(ip, old, new);
408 static void ftrace_replace_code(int enable)
411 unsigned char *new = NULL, *old = NULL;
412 struct dyn_ftrace *rec;
413 struct ftrace_page *pg;
416 old = ftrace_nop_replace();
418 new = ftrace_nop_replace();
420 for (pg = ftrace_pages_start; pg; pg = pg->next) {
421 for (i = 0; i < pg->index; i++) {
422 rec = &pg->records[i];
424 /* don't modify code that has already faulted */
425 if (rec->flags & FTRACE_FL_FAILED)
428 /* ignore updates to this record's mcount site */
429 if (get_kprobe((void *)rec->ip)) {
433 unfreeze_record(rec);
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_free_rec(rec);
448 static void print_ip_ins(const char *fmt, unsigned char *p)
452 printk(KERN_CONT "%s", fmt);
454 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
455 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
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 ret = ftrace_modify_code(ip, call, nop);
474 FTRACE_WARN_ON_ONCE(1);
475 pr_info("ftrace faulted on modifying ");
479 FTRACE_WARN_ON_ONCE(1);
480 pr_info("ftrace failed to modify ");
482 print_ip_ins(" expected: ", call);
483 print_ip_ins(" actual: ", (unsigned char *)ip);
484 print_ip_ins(" replace: ", nop);
485 printk(KERN_CONT "\n");
488 FTRACE_WARN_ON_ONCE(1);
489 pr_info("ftrace faulted on writing ");
493 FTRACE_WARN_ON_ONCE(1);
494 pr_info("ftrace faulted on unknown error ");
498 rec->flags |= FTRACE_FL_FAILED;
504 static int __ftrace_modify_code(void *data)
508 if (*command & FTRACE_ENABLE_CALLS)
509 ftrace_replace_code(1);
510 else if (*command & FTRACE_DISABLE_CALLS)
511 ftrace_replace_code(0);
513 if (*command & FTRACE_UPDATE_TRACE_FUNC)
514 ftrace_update_ftrace_func(ftrace_trace_function);
519 static void ftrace_run_update_code(int command)
521 stop_machine(__ftrace_modify_code, &command, NULL);
524 static ftrace_func_t saved_ftrace_func;
525 static int ftrace_start;
526 static DEFINE_MUTEX(ftrace_start_lock);
528 static void ftrace_startup(void)
532 if (unlikely(ftrace_disabled))
535 mutex_lock(&ftrace_start_lock);
537 if (ftrace_start == 1)
538 command |= FTRACE_ENABLE_CALLS;
540 if (saved_ftrace_func != ftrace_trace_function) {
541 saved_ftrace_func = ftrace_trace_function;
542 command |= FTRACE_UPDATE_TRACE_FUNC;
545 if (!command || !ftrace_enabled)
548 ftrace_run_update_code(command);
550 mutex_unlock(&ftrace_start_lock);
553 static void ftrace_shutdown(void)
557 if (unlikely(ftrace_disabled))
560 mutex_lock(&ftrace_start_lock);
563 command |= FTRACE_DISABLE_CALLS;
565 if (saved_ftrace_func != ftrace_trace_function) {
566 saved_ftrace_func = ftrace_trace_function;
567 command |= FTRACE_UPDATE_TRACE_FUNC;
570 if (!command || !ftrace_enabled)
573 ftrace_run_update_code(command);
575 mutex_unlock(&ftrace_start_lock);
578 static void ftrace_startup_sysctl(void)
580 int command = FTRACE_ENABLE_MCOUNT;
582 if (unlikely(ftrace_disabled))
585 mutex_lock(&ftrace_start_lock);
586 /* Force update next time */
587 saved_ftrace_func = NULL;
588 /* ftrace_start is true if we want ftrace running */
590 command |= FTRACE_ENABLE_CALLS;
592 ftrace_run_update_code(command);
593 mutex_unlock(&ftrace_start_lock);
596 static void ftrace_shutdown_sysctl(void)
598 int command = FTRACE_DISABLE_MCOUNT;
600 if (unlikely(ftrace_disabled))
603 mutex_lock(&ftrace_start_lock);
604 /* ftrace_start is true if ftrace is running */
606 command |= FTRACE_DISABLE_CALLS;
608 ftrace_run_update_code(command);
609 mutex_unlock(&ftrace_start_lock);
612 static cycle_t ftrace_update_time;
613 static unsigned long ftrace_update_cnt;
614 unsigned long ftrace_update_tot_cnt;
616 static int ftrace_update_code(void)
618 struct dyn_ftrace *p, *t;
621 start = ftrace_now(raw_smp_processor_id());
622 ftrace_update_cnt = 0;
624 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
626 /* If something went wrong, bail without enabling anything */
627 if (unlikely(ftrace_disabled))
630 list_del_init(&p->list);
632 /* convert record (i.e, patch mcount-call with NOP) */
633 if (ftrace_code_disable(p)) {
634 p->flags |= FTRACE_FL_CONVERTED;
640 stop = ftrace_now(raw_smp_processor_id());
641 ftrace_update_time = stop - start;
642 ftrace_update_tot_cnt += ftrace_update_cnt;
647 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
649 struct ftrace_page *pg;
653 /* allocate a few pages */
654 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
655 if (!ftrace_pages_start)
659 * Allocate a few more pages.
661 * TODO: have some parser search vmlinux before
662 * final linking to find all calls to ftrace.
664 * a) know how many pages to allocate.
666 * b) set up the table then.
668 * The dynamic code is still necessary for
672 pg = ftrace_pages = ftrace_pages_start;
674 cnt = num_to_init / ENTRIES_PER_PAGE;
675 pr_info("ftrace: allocating %ld entries in %d pages\n",
678 for (i = 0; i < cnt; i++) {
679 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
681 /* If we fail, we'll try later anyway */
692 FTRACE_ITER_FILTER = (1 << 0),
693 FTRACE_ITER_CONT = (1 << 1),
694 FTRACE_ITER_NOTRACE = (1 << 2),
695 FTRACE_ITER_FAILURES = (1 << 3),
698 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
700 struct ftrace_iterator {
702 struct ftrace_page *pg;
705 unsigned char buffer[FTRACE_BUFF_MAX+1];
711 t_next(struct seq_file *m, void *v, loff_t *pos)
713 struct ftrace_iterator *iter = m->private;
714 struct dyn_ftrace *rec = NULL;
718 /* should not be called from interrupt context */
719 spin_lock(&ftrace_lock);
721 if (iter->idx >= iter->pg->index) {
722 if (iter->pg->next) {
723 iter->pg = iter->pg->next;
728 rec = &iter->pg->records[iter->idx++];
729 if ((rec->flags & FTRACE_FL_FREE) ||
731 (!(iter->flags & FTRACE_ITER_FAILURES) &&
732 (rec->flags & FTRACE_FL_FAILED)) ||
734 ((iter->flags & FTRACE_ITER_FAILURES) &&
735 !(rec->flags & FTRACE_FL_FAILED)) ||
737 ((iter->flags & FTRACE_ITER_NOTRACE) &&
738 !(rec->flags & FTRACE_FL_NOTRACE))) {
743 spin_unlock(&ftrace_lock);
750 static void *t_start(struct seq_file *m, loff_t *pos)
752 struct ftrace_iterator *iter = m->private;
756 if (*pos != iter->pos) {
757 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
761 p = t_next(m, p, &l);
767 static void t_stop(struct seq_file *m, void *p)
771 static int t_show(struct seq_file *m, void *v)
773 struct dyn_ftrace *rec = v;
774 char str[KSYM_SYMBOL_LEN];
779 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
781 seq_printf(m, "%s\n", str);
786 static struct seq_operations show_ftrace_seq_ops = {
794 ftrace_avail_open(struct inode *inode, struct file *file)
796 struct ftrace_iterator *iter;
799 if (unlikely(ftrace_disabled))
802 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
806 iter->pg = ftrace_pages_start;
809 ret = seq_open(file, &show_ftrace_seq_ops);
811 struct seq_file *m = file->private_data;
821 int ftrace_avail_release(struct inode *inode, struct file *file)
823 struct seq_file *m = (struct seq_file *)file->private_data;
824 struct ftrace_iterator *iter = m->private;
826 seq_release(inode, file);
833 ftrace_failures_open(struct inode *inode, struct file *file)
837 struct ftrace_iterator *iter;
839 ret = ftrace_avail_open(inode, file);
841 m = (struct seq_file *)file->private_data;
842 iter = (struct ftrace_iterator *)m->private;
843 iter->flags = FTRACE_ITER_FAILURES;
850 static void ftrace_filter_reset(int enable)
852 struct ftrace_page *pg;
853 struct dyn_ftrace *rec;
854 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
857 /* should not be called from interrupt context */
858 spin_lock(&ftrace_lock);
861 pg = ftrace_pages_start;
863 for (i = 0; i < pg->index; i++) {
864 rec = &pg->records[i];
865 if (rec->flags & FTRACE_FL_FAILED)
871 spin_unlock(&ftrace_lock);
875 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
877 struct ftrace_iterator *iter;
880 if (unlikely(ftrace_disabled))
883 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
887 mutex_lock(&ftrace_regex_lock);
888 if ((file->f_mode & FMODE_WRITE) &&
889 !(file->f_flags & O_APPEND))
890 ftrace_filter_reset(enable);
892 if (file->f_mode & FMODE_READ) {
893 iter->pg = ftrace_pages_start;
895 iter->flags = enable ? FTRACE_ITER_FILTER :
898 ret = seq_open(file, &show_ftrace_seq_ops);
900 struct seq_file *m = file->private_data;
905 file->private_data = iter;
906 mutex_unlock(&ftrace_regex_lock);
912 ftrace_filter_open(struct inode *inode, struct file *file)
914 return ftrace_regex_open(inode, file, 1);
918 ftrace_notrace_open(struct inode *inode, struct file *file)
920 return ftrace_regex_open(inode, file, 0);
924 ftrace_regex_read(struct file *file, char __user *ubuf,
925 size_t cnt, loff_t *ppos)
927 if (file->f_mode & FMODE_READ)
928 return seq_read(file, ubuf, cnt, ppos);
934 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
938 if (file->f_mode & FMODE_READ)
939 ret = seq_lseek(file, offset, origin);
941 file->f_pos = ret = 1;
954 ftrace_match(unsigned char *buff, int len, int enable)
956 char str[KSYM_SYMBOL_LEN];
958 struct ftrace_page *pg;
959 struct dyn_ftrace *rec;
960 int type = MATCH_FULL;
961 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
962 unsigned i, match = 0, search_len = 0;
964 for (i = 0; i < len; i++) {
965 if (buff[i] == '*') {
967 search = buff + i + 1;
968 type = MATCH_END_ONLY;
969 search_len = len - (i + 1);
971 if (type == MATCH_END_ONLY) {
972 type = MATCH_MIDDLE_ONLY;
975 type = MATCH_FRONT_ONLY;
983 /* should not be called from interrupt context */
984 spin_lock(&ftrace_lock);
987 pg = ftrace_pages_start;
989 for (i = 0; i < pg->index; i++) {
993 rec = &pg->records[i];
994 if (rec->flags & FTRACE_FL_FAILED)
996 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
999 if (strcmp(str, buff) == 0)
1002 case MATCH_FRONT_ONLY:
1003 if (memcmp(str, buff, match) == 0)
1006 case MATCH_MIDDLE_ONLY:
1007 if (strstr(str, search))
1010 case MATCH_END_ONLY:
1011 ptr = strstr(str, search);
1012 if (ptr && (ptr[search_len] == 0))
1021 spin_unlock(&ftrace_lock);
1025 ftrace_regex_write(struct file *file, const char __user *ubuf,
1026 size_t cnt, loff_t *ppos, int enable)
1028 struct ftrace_iterator *iter;
1033 if (!cnt || cnt < 0)
1036 mutex_lock(&ftrace_regex_lock);
1038 if (file->f_mode & FMODE_READ) {
1039 struct seq_file *m = file->private_data;
1042 iter = file->private_data;
1045 iter->flags &= ~FTRACE_ITER_CONT;
1046 iter->buffer_idx = 0;
1049 ret = get_user(ch, ubuf++);
1055 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1056 /* skip white space */
1057 while (cnt && isspace(ch)) {
1058 ret = get_user(ch, ubuf++);
1066 file->f_pos += read;
1071 iter->buffer_idx = 0;
1074 while (cnt && !isspace(ch)) {
1075 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1076 iter->buffer[iter->buffer_idx++] = ch;
1081 ret = get_user(ch, ubuf++);
1090 iter->buffer[iter->buffer_idx] = 0;
1091 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1092 iter->buffer_idx = 0;
1094 iter->flags |= FTRACE_ITER_CONT;
1097 file->f_pos += read;
1101 mutex_unlock(&ftrace_regex_lock);
1107 ftrace_filter_write(struct file *file, const char __user *ubuf,
1108 size_t cnt, loff_t *ppos)
1110 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1114 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1115 size_t cnt, loff_t *ppos)
1117 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1121 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1123 if (unlikely(ftrace_disabled))
1126 mutex_lock(&ftrace_regex_lock);
1128 ftrace_filter_reset(enable);
1130 ftrace_match(buf, len, enable);
1131 mutex_unlock(&ftrace_regex_lock);
1135 * ftrace_set_filter - set a function to filter on in ftrace
1136 * @buf - the string that holds the function filter text.
1137 * @len - the length of the string.
1138 * @reset - non zero to reset all filters before applying this filter.
1140 * Filters denote which functions should be enabled when tracing is enabled.
1141 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1143 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1145 ftrace_set_regex(buf, len, reset, 1);
1149 * ftrace_set_notrace - set a function to not trace in ftrace
1150 * @buf - the string that holds the function notrace text.
1151 * @len - the length of the string.
1152 * @reset - non zero to reset all filters before applying this filter.
1154 * Notrace Filters denote which functions should not be enabled when tracing
1155 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1158 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1160 ftrace_set_regex(buf, len, reset, 0);
1164 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1166 struct seq_file *m = (struct seq_file *)file->private_data;
1167 struct ftrace_iterator *iter;
1169 mutex_lock(&ftrace_regex_lock);
1170 if (file->f_mode & FMODE_READ) {
1173 seq_release(inode, file);
1175 iter = file->private_data;
1177 if (iter->buffer_idx) {
1179 iter->buffer[iter->buffer_idx] = 0;
1180 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1183 mutex_lock(&ftrace_sysctl_lock);
1184 mutex_lock(&ftrace_start_lock);
1185 if (iter->filtered && ftrace_start && ftrace_enabled)
1186 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1187 mutex_unlock(&ftrace_start_lock);
1188 mutex_unlock(&ftrace_sysctl_lock);
1191 mutex_unlock(&ftrace_regex_lock);
1196 ftrace_filter_release(struct inode *inode, struct file *file)
1198 return ftrace_regex_release(inode, file, 1);
1202 ftrace_notrace_release(struct inode *inode, struct file *file)
1204 return ftrace_regex_release(inode, file, 0);
1207 static struct file_operations ftrace_avail_fops = {
1208 .open = ftrace_avail_open,
1210 .llseek = seq_lseek,
1211 .release = ftrace_avail_release,
1214 static struct file_operations ftrace_failures_fops = {
1215 .open = ftrace_failures_open,
1217 .llseek = seq_lseek,
1218 .release = ftrace_avail_release,
1221 static struct file_operations ftrace_filter_fops = {
1222 .open = ftrace_filter_open,
1223 .read = ftrace_regex_read,
1224 .write = ftrace_filter_write,
1225 .llseek = ftrace_regex_lseek,
1226 .release = ftrace_filter_release,
1229 static struct file_operations ftrace_notrace_fops = {
1230 .open = ftrace_notrace_open,
1231 .read = ftrace_regex_read,
1232 .write = ftrace_notrace_write,
1233 .llseek = ftrace_regex_lseek,
1234 .release = ftrace_notrace_release,
1237 static __init int ftrace_init_debugfs(void)
1239 struct dentry *d_tracer;
1240 struct dentry *entry;
1242 d_tracer = tracing_init_dentry();
1244 entry = debugfs_create_file("available_filter_functions", 0444,
1245 d_tracer, NULL, &ftrace_avail_fops);
1247 pr_warning("Could not create debugfs "
1248 "'available_filter_functions' entry\n");
1250 entry = debugfs_create_file("failures", 0444,
1251 d_tracer, NULL, &ftrace_failures_fops);
1253 pr_warning("Could not create debugfs 'failures' entry\n");
1255 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1256 NULL, &ftrace_filter_fops);
1258 pr_warning("Could not create debugfs "
1259 "'set_ftrace_filter' entry\n");
1261 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1262 NULL, &ftrace_notrace_fops);
1264 pr_warning("Could not create debugfs "
1265 "'set_ftrace_notrace' entry\n");
1270 fs_initcall(ftrace_init_debugfs);
1272 static int ftrace_convert_nops(unsigned long *start,
1277 unsigned long flags;
1279 mutex_lock(&ftrace_start_lock);
1282 addr = ftrace_call_adjust(*p++);
1283 ftrace_record_ip(addr);
1286 /* disable interrupts to prevent kstop machine */
1287 local_irq_save(flags);
1288 ftrace_update_code();
1289 local_irq_restore(flags);
1290 mutex_unlock(&ftrace_start_lock);
1295 void ftrace_init_module(unsigned long *start, unsigned long *end)
1297 if (ftrace_disabled || start == end)
1299 ftrace_convert_nops(start, end);
1302 extern unsigned long __start_mcount_loc[];
1303 extern unsigned long __stop_mcount_loc[];
1305 void __init ftrace_init(void)
1307 unsigned long count, addr, flags;
1310 /* Keep the ftrace pointer to the stub */
1311 addr = (unsigned long)ftrace_stub;
1313 local_irq_save(flags);
1314 ftrace_dyn_arch_init(&addr);
1315 local_irq_restore(flags);
1317 /* ftrace_dyn_arch_init places the return code in addr */
1321 count = __stop_mcount_loc - __start_mcount_loc;
1323 ret = ftrace_dyn_table_alloc(count);
1327 last_ftrace_enabled = ftrace_enabled = 1;
1329 ret = ftrace_convert_nops(__start_mcount_loc,
1334 ftrace_disabled = 1;
1339 static int __init ftrace_nodyn_init(void)
1344 device_initcall(ftrace_nodyn_init);
1346 # define ftrace_startup() do { } while (0)
1347 # define ftrace_shutdown() do { } while (0)
1348 # define ftrace_startup_sysctl() do { } while (0)
1349 # define ftrace_shutdown_sysctl() do { } while (0)
1350 #endif /* CONFIG_DYNAMIC_FTRACE */
1353 * ftrace_kill - kill ftrace
1355 * This function should be used by panic code. It stops ftrace
1356 * but in a not so nice way. If you need to simply kill ftrace
1357 * from a non-atomic section, use ftrace_kill.
1359 void ftrace_kill(void)
1361 ftrace_disabled = 1;
1363 clear_ftrace_function();
1367 * register_ftrace_function - register a function for profiling
1368 * @ops - ops structure that holds the function for profiling.
1370 * Register a function to be called by all functions in the
1373 * Note: @ops->func and all the functions it calls must be labeled
1374 * with "notrace", otherwise it will go into a
1377 int register_ftrace_function(struct ftrace_ops *ops)
1381 if (unlikely(ftrace_disabled))
1384 mutex_lock(&ftrace_sysctl_lock);
1385 ret = __register_ftrace_function(ops);
1387 mutex_unlock(&ftrace_sysctl_lock);
1393 * unregister_ftrace_function - unresgister a function for profiling.
1394 * @ops - ops structure that holds the function to unregister
1396 * Unregister a function that was added to be called by ftrace profiling.
1398 int unregister_ftrace_function(struct ftrace_ops *ops)
1402 mutex_lock(&ftrace_sysctl_lock);
1403 ret = __unregister_ftrace_function(ops);
1405 mutex_unlock(&ftrace_sysctl_lock);
1411 ftrace_enable_sysctl(struct ctl_table *table, int write,
1412 struct file *file, void __user *buffer, size_t *lenp,
1417 if (unlikely(ftrace_disabled))
1420 mutex_lock(&ftrace_sysctl_lock);
1422 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1424 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1427 last_ftrace_enabled = ftrace_enabled;
1429 if (ftrace_enabled) {
1431 ftrace_startup_sysctl();
1433 /* we are starting ftrace again */
1434 if (ftrace_list != &ftrace_list_end) {
1435 if (ftrace_list->next == &ftrace_list_end)
1436 ftrace_trace_function = ftrace_list->func;
1438 ftrace_trace_function = ftrace_list_func;
1442 /* stopping ftrace calls (just send to ftrace_stub) */
1443 ftrace_trace_function = ftrace_stub;
1445 ftrace_shutdown_sysctl();
1449 mutex_unlock(&ftrace_sysctl_lock);