2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugfs.h>
21 #include <linux/hardirq.h>
22 #include <linux/kthread.h>
23 #include <linux/uaccess.h>
24 #include <linux/ftrace.h>
25 #include <linux/sysctl.h>
26 #include <linux/ctype.h>
27 #include <linux/hash.h>
28 #include <linux/list.h>
32 /* ftrace_enabled is a method to turn ftrace on or off */
33 int ftrace_enabled __read_mostly;
34 static int last_ftrace_enabled;
37 * ftrace_disabled is set when an anomaly is discovered.
38 * ftrace_disabled is much stronger than ftrace_enabled.
40 static int ftrace_disabled __read_mostly;
42 static DEFINE_SPINLOCK(ftrace_lock);
43 static DEFINE_MUTEX(ftrace_sysctl_lock);
45 static struct ftrace_ops ftrace_list_end __read_mostly =
50 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
51 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
53 void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
55 struct ftrace_ops *op = ftrace_list;
57 /* in case someone actually ports this to alpha! */
58 read_barrier_depends();
60 while (op != &ftrace_list_end) {
62 read_barrier_depends();
63 op->func(ip, parent_ip);
69 * clear_ftrace_function - reset the ftrace function
71 * This NULLs the ftrace function and in essence stops
72 * tracing. There may be lag
74 void clear_ftrace_function(void)
76 ftrace_trace_function = ftrace_stub;
79 static int __register_ftrace_function(struct ftrace_ops *ops)
81 /* Should never be called by interrupts */
82 spin_lock(&ftrace_lock);
84 ops->next = ftrace_list;
86 * We are entering ops into the ftrace_list but another
87 * CPU might be walking that list. We need to make sure
88 * the ops->next pointer is valid before another CPU sees
89 * the ops pointer included into the ftrace_list.
96 * For one func, simply call it directly.
97 * For more than one func, call the chain.
99 if (ops->next == &ftrace_list_end)
100 ftrace_trace_function = ops->func;
102 ftrace_trace_function = ftrace_list_func;
105 spin_unlock(&ftrace_lock);
110 static int __unregister_ftrace_function(struct ftrace_ops *ops)
112 struct ftrace_ops **p;
115 spin_lock(&ftrace_lock);
118 * If we are removing the last function, then simply point
119 * to the ftrace_stub.
121 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
122 ftrace_trace_function = ftrace_stub;
123 ftrace_list = &ftrace_list_end;
127 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
138 if (ftrace_enabled) {
139 /* If we only have one func left, then call that directly */
140 if (ftrace_list == &ftrace_list_end ||
141 ftrace_list->next == &ftrace_list_end)
142 ftrace_trace_function = ftrace_list->func;
146 spin_unlock(&ftrace_lock);
151 #ifdef CONFIG_DYNAMIC_FTRACE
153 static struct task_struct *ftraced_task;
156 FTRACE_ENABLE_CALLS = (1 << 0),
157 FTRACE_DISABLE_CALLS = (1 << 1),
158 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
159 FTRACE_ENABLE_MCOUNT = (1 << 3),
160 FTRACE_DISABLE_MCOUNT = (1 << 4),
163 static int ftrace_filtered;
165 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
167 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
169 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
170 static DEFINE_MUTEX(ftraced_lock);
171 static DEFINE_MUTEX(ftrace_regex_lock);
174 struct ftrace_page *next;
176 struct dyn_ftrace records[];
179 #define ENTRIES_PER_PAGE \
180 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
182 /* estimate from running different kernels */
183 #define NR_TO_INIT 10000
185 static struct ftrace_page *ftrace_pages_start;
186 static struct ftrace_page *ftrace_pages;
188 static int ftraced_trigger;
189 static int ftraced_suspend;
190 static int ftraced_stop;
192 static int ftrace_record_suspend;
194 static struct dyn_ftrace *ftrace_free_records;
197 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
199 struct dyn_ftrace *p;
200 struct hlist_node *t;
203 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
214 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
216 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
219 /* called from kstop_machine */
220 static inline void ftrace_del_hash(struct dyn_ftrace *node)
222 hlist_del(&node->node);
225 static void ftrace_free_rec(struct dyn_ftrace *rec)
227 /* no locking, only called from kstop_machine */
229 rec->ip = (unsigned long)ftrace_free_records;
230 ftrace_free_records = rec;
231 rec->flags |= FTRACE_FL_FREE;
234 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
236 struct dyn_ftrace *rec;
238 /* First check for freed records */
239 if (ftrace_free_records) {
240 rec = ftrace_free_records;
242 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
244 ftrace_free_records = NULL;
250 ftrace_free_records = (void *)rec->ip;
251 memset(rec, 0, sizeof(*rec));
255 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
256 if (!ftrace_pages->next)
258 ftrace_pages = ftrace_pages->next;
261 return &ftrace_pages->records[ftrace_pages->index++];
265 ftrace_record_ip(unsigned long ip)
267 struct dyn_ftrace *node;
274 if (!ftrace_enabled || ftrace_disabled)
277 resched = need_resched();
278 preempt_disable_notrace();
281 * We simply need to protect against recursion.
282 * Use the the raw version of smp_processor_id and not
283 * __get_cpu_var which can call debug hooks that can
284 * cause a recursive crash here.
286 cpu = raw_smp_processor_id();
287 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
288 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
291 if (unlikely(ftrace_record_suspend))
294 key = hash_long(ip, FTRACE_HASHBITS);
296 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
298 if (ftrace_ip_in_hash(ip, key))
301 atomic = irqs_disabled();
303 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
305 /* This ip may have hit the hash before the lock */
306 if (ftrace_ip_in_hash(ip, key))
309 node = ftrace_alloc_dyn_node(ip);
315 ftrace_add_hash(node, key);
320 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
322 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
324 /* prevent recursion with scheduler */
326 preempt_enable_no_resched_notrace();
328 preempt_enable_notrace();
331 #define FTRACE_ADDR ((long)(ftrace_caller))
332 #define MCOUNT_ADDR ((long)(mcount))
335 __ftrace_replace_code(struct dyn_ftrace *rec,
336 unsigned char *old, unsigned char *new, int enable)
338 unsigned long ip, fl;
342 if (ftrace_filtered && enable) {
344 * If filtering is on:
346 * If this record is set to be filtered and
347 * is enabled then do nothing.
349 * If this record is set to be filtered and
350 * it is not enabled, enable it.
352 * If this record is not set to be filtered
353 * and it is not enabled do nothing.
355 * If this record is set not to trace then
358 * If this record is not set to be filtered and
359 * it is enabled, disable it.
361 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
363 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
364 (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
368 * If it is enabled disable it,
369 * otherwise enable it!
371 if (fl == FTRACE_FL_ENABLED) {
372 /* swap new and old */
374 old = ftrace_call_replace(ip, FTRACE_ADDR);
375 rec->flags &= ~FTRACE_FL_ENABLED;
377 new = ftrace_call_replace(ip, FTRACE_ADDR);
378 rec->flags |= FTRACE_FL_ENABLED;
384 * If this record is set not to trace and is
385 * not enabled, do nothing.
387 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
388 if (fl == FTRACE_FL_NOTRACE)
391 new = ftrace_call_replace(ip, FTRACE_ADDR);
393 old = ftrace_call_replace(ip, FTRACE_ADDR);
396 if (rec->flags & FTRACE_FL_ENABLED)
398 rec->flags |= FTRACE_FL_ENABLED;
400 if (!(rec->flags & FTRACE_FL_ENABLED))
402 rec->flags &= ~FTRACE_FL_ENABLED;
406 return ftrace_modify_code(ip, old, new);
409 static void ftrace_replace_code(int enable)
412 unsigned char *new = NULL, *old = NULL;
413 struct dyn_ftrace *rec;
414 struct ftrace_page *pg;
417 old = ftrace_nop_replace();
419 new = ftrace_nop_replace();
421 for (pg = ftrace_pages_start; pg; pg = pg->next) {
422 for (i = 0; i < pg->index; i++) {
423 rec = &pg->records[i];
425 /* don't modify code that has already faulted */
426 if (rec->flags & FTRACE_FL_FAILED)
429 failed = __ftrace_replace_code(rec, old, new, enable);
430 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
431 rec->flags |= FTRACE_FL_FAILED;
432 if ((system_state == SYSTEM_BOOTING) ||
433 !kernel_text_address(rec->ip)) {
434 ftrace_del_hash(rec);
435 ftrace_free_rec(rec);
442 static void ftrace_shutdown_replenish(void)
444 if (ftrace_pages->next)
447 /* allocate another page */
448 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
452 ftrace_code_disable(struct dyn_ftrace *rec)
455 unsigned char *nop, *call;
460 nop = ftrace_nop_replace();
461 call = ftrace_call_replace(ip, MCOUNT_ADDR);
463 failed = ftrace_modify_code(ip, call, nop);
465 rec->flags |= FTRACE_FL_FAILED;
471 static int __ftrace_update_code(void *ignore);
473 static int __ftrace_modify_code(void *data)
478 if (*command & FTRACE_ENABLE_CALLS) {
480 * Update any recorded ips now that we have the
483 __ftrace_update_code(NULL);
484 ftrace_replace_code(1);
485 } else if (*command & FTRACE_DISABLE_CALLS)
486 ftrace_replace_code(0);
488 if (*command & FTRACE_UPDATE_TRACE_FUNC)
489 ftrace_update_ftrace_func(ftrace_trace_function);
491 if (*command & FTRACE_ENABLE_MCOUNT) {
492 addr = (unsigned long)ftrace_record_ip;
493 ftrace_mcount_set(&addr);
494 } else if (*command & FTRACE_DISABLE_MCOUNT) {
495 addr = (unsigned long)ftrace_stub;
496 ftrace_mcount_set(&addr);
502 static void ftrace_run_update_code(int command)
504 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
507 void ftrace_disable_daemon(void)
509 /* Stop the daemon from calling kstop_machine */
510 mutex_lock(&ftraced_lock);
512 mutex_unlock(&ftraced_lock);
514 ftrace_force_update();
517 void ftrace_enable_daemon(void)
519 mutex_lock(&ftraced_lock);
521 mutex_unlock(&ftraced_lock);
523 ftrace_force_update();
526 static ftrace_func_t saved_ftrace_func;
528 static void ftrace_startup(void)
532 if (unlikely(ftrace_disabled))
535 mutex_lock(&ftraced_lock);
537 if (ftraced_suspend == 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(&ftraced_lock);
553 static void ftrace_shutdown(void)
557 if (unlikely(ftrace_disabled))
560 mutex_lock(&ftraced_lock);
562 if (!ftraced_suspend)
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(&ftraced_lock);
578 static void ftrace_startup_sysctl(void)
580 int command = FTRACE_ENABLE_MCOUNT;
582 if (unlikely(ftrace_disabled))
585 mutex_lock(&ftraced_lock);
586 /* Force update next time */
587 saved_ftrace_func = NULL;
588 /* ftraced_suspend is true if we want ftrace running */
590 command |= FTRACE_ENABLE_CALLS;
592 ftrace_run_update_code(command);
593 mutex_unlock(&ftraced_lock);
596 static void ftrace_shutdown_sysctl(void)
598 int command = FTRACE_DISABLE_MCOUNT;
600 if (unlikely(ftrace_disabled))
603 mutex_lock(&ftraced_lock);
604 /* ftraced_suspend is true if ftrace is running */
606 command |= FTRACE_DISABLE_CALLS;
608 ftrace_run_update_code(command);
609 mutex_unlock(&ftraced_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 *ignore)
618 struct dyn_ftrace *p;
619 struct hlist_node *t, *n;
620 int save_ftrace_enabled;
624 /* Don't be recording funcs now */
625 ftrace_record_suspend++;
626 save_ftrace_enabled = ftrace_enabled;
629 start = ftrace_now(raw_smp_processor_id());
630 ftrace_update_cnt = 0;
632 /* No locks needed, the machine is stopped! */
633 for (i = 0; i < FTRACE_HASHSIZE; i++) {
634 /* all CPUS are stopped, we are safe to modify code */
635 hlist_for_each_entry_safe(p, t, n, &ftrace_hash[i], node) {
636 /* Skip over failed records which have not been
638 if (p->flags & FTRACE_FL_FAILED)
641 /* Unconverted records are always at the head of the
642 * hash bucket. Once we encounter a converted record,
643 * simply skip over to the next bucket. Saves ftraced
644 * some processor cycles (ftrace does its bid for
645 * global warming :-p ). */
646 if (p->flags & (FTRACE_FL_CONVERTED))
649 if (ftrace_code_disable(p)) {
650 p->flags |= FTRACE_FL_CONVERTED;
653 if ((system_state == SYSTEM_BOOTING) ||
654 !kernel_text_address(p->ip)) {
663 stop = ftrace_now(raw_smp_processor_id());
664 ftrace_update_time = stop - start;
665 ftrace_update_tot_cnt += ftrace_update_cnt;
668 ftrace_enabled = save_ftrace_enabled;
669 ftrace_record_suspend--;
674 static int ftrace_update_code(void)
676 if (unlikely(ftrace_disabled) ||
677 !ftrace_enabled || !ftraced_trigger)
680 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
685 static int ftraced(void *ignore)
689 while (!kthread_should_stop()) {
691 set_current_state(TASK_INTERRUPTIBLE);
693 /* check once a second */
694 schedule_timeout(HZ);
696 if (unlikely(ftrace_disabled))
699 mutex_lock(&ftrace_sysctl_lock);
700 mutex_lock(&ftraced_lock);
701 if (!ftraced_suspend && !ftraced_stop &&
702 ftrace_update_code()) {
703 usecs = nsecs_to_usecs(ftrace_update_time);
704 if (ftrace_update_tot_cnt > 100000) {
705 ftrace_update_tot_cnt = 0;
706 pr_info("hm, dftrace overflow: %lu change%s"
707 " (%lu total) in %lu usec%s\n",
709 ftrace_update_cnt != 1 ? "s" : "",
710 ftrace_update_tot_cnt,
711 usecs, usecs != 1 ? "s" : "");
716 mutex_unlock(&ftraced_lock);
717 mutex_unlock(&ftrace_sysctl_lock);
719 ftrace_shutdown_replenish();
721 __set_current_state(TASK_RUNNING);
725 static int __init ftrace_dyn_table_alloc(void)
727 struct ftrace_page *pg;
731 /* allocate a few pages */
732 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
733 if (!ftrace_pages_start)
737 * Allocate a few more pages.
739 * TODO: have some parser search vmlinux before
740 * final linking to find all calls to ftrace.
742 * a) know how many pages to allocate.
744 * b) set up the table then.
746 * The dynamic code is still necessary for
750 pg = ftrace_pages = ftrace_pages_start;
752 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
754 for (i = 0; i < cnt; i++) {
755 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
757 /* If we fail, we'll try later anyway */
768 FTRACE_ITER_FILTER = (1 << 0),
769 FTRACE_ITER_CONT = (1 << 1),
770 FTRACE_ITER_NOTRACE = (1 << 2),
771 FTRACE_ITER_FAILURES = (1 << 3),
774 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
776 struct ftrace_iterator {
778 struct ftrace_page *pg;
781 unsigned char buffer[FTRACE_BUFF_MAX+1];
787 t_next(struct seq_file *m, void *v, loff_t *pos)
789 struct ftrace_iterator *iter = m->private;
790 struct dyn_ftrace *rec = NULL;
795 if (iter->idx >= iter->pg->index) {
796 if (iter->pg->next) {
797 iter->pg = iter->pg->next;
802 rec = &iter->pg->records[iter->idx++];
803 if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
804 (rec->flags & FTRACE_FL_FAILED)) ||
806 ((iter->flags & FTRACE_ITER_FAILURES) &&
807 (!(rec->flags & FTRACE_FL_FAILED) ||
808 (rec->flags & FTRACE_FL_FREE))) ||
810 ((iter->flags & FTRACE_ITER_FILTER) &&
811 !(rec->flags & FTRACE_FL_FILTER)) ||
813 ((iter->flags & FTRACE_ITER_NOTRACE) &&
814 !(rec->flags & FTRACE_FL_NOTRACE))) {
825 static void *t_start(struct seq_file *m, loff_t *pos)
827 struct ftrace_iterator *iter = m->private;
831 if (*pos != iter->pos) {
832 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
836 p = t_next(m, p, &l);
842 static void t_stop(struct seq_file *m, void *p)
846 static int t_show(struct seq_file *m, void *v)
848 struct dyn_ftrace *rec = v;
849 char str[KSYM_SYMBOL_LEN];
854 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
856 seq_printf(m, "%s\n", str);
861 static struct seq_operations show_ftrace_seq_ops = {
869 ftrace_avail_open(struct inode *inode, struct file *file)
871 struct ftrace_iterator *iter;
874 if (unlikely(ftrace_disabled))
877 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
881 iter->pg = ftrace_pages_start;
884 ret = seq_open(file, &show_ftrace_seq_ops);
886 struct seq_file *m = file->private_data;
896 int ftrace_avail_release(struct inode *inode, struct file *file)
898 struct seq_file *m = (struct seq_file *)file->private_data;
899 struct ftrace_iterator *iter = m->private;
901 seq_release(inode, file);
908 ftrace_failures_open(struct inode *inode, struct file *file)
912 struct ftrace_iterator *iter;
914 ret = ftrace_avail_open(inode, file);
916 m = (struct seq_file *)file->private_data;
917 iter = (struct ftrace_iterator *)m->private;
918 iter->flags = FTRACE_ITER_FAILURES;
925 static void ftrace_filter_reset(int enable)
927 struct ftrace_page *pg;
928 struct dyn_ftrace *rec;
929 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
932 /* keep kstop machine from running */
936 pg = ftrace_pages_start;
938 for (i = 0; i < pg->index; i++) {
939 rec = &pg->records[i];
940 if (rec->flags & FTRACE_FL_FAILED)
950 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
952 struct ftrace_iterator *iter;
955 if (unlikely(ftrace_disabled))
958 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
962 mutex_lock(&ftrace_regex_lock);
963 if ((file->f_mode & FMODE_WRITE) &&
964 !(file->f_flags & O_APPEND))
965 ftrace_filter_reset(enable);
967 if (file->f_mode & FMODE_READ) {
968 iter->pg = ftrace_pages_start;
970 iter->flags = enable ? FTRACE_ITER_FILTER :
973 ret = seq_open(file, &show_ftrace_seq_ops);
975 struct seq_file *m = file->private_data;
980 file->private_data = iter;
981 mutex_unlock(&ftrace_regex_lock);
987 ftrace_filter_open(struct inode *inode, struct file *file)
989 return ftrace_regex_open(inode, file, 1);
993 ftrace_notrace_open(struct inode *inode, struct file *file)
995 return ftrace_regex_open(inode, file, 0);
999 ftrace_regex_read(struct file *file, char __user *ubuf,
1000 size_t cnt, loff_t *ppos)
1002 if (file->f_mode & FMODE_READ)
1003 return seq_read(file, ubuf, cnt, ppos);
1009 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1013 if (file->f_mode & FMODE_READ)
1014 ret = seq_lseek(file, offset, origin);
1016 file->f_pos = ret = 1;
1029 ftrace_match(unsigned char *buff, int len, int enable)
1031 char str[KSYM_SYMBOL_LEN];
1032 char *search = NULL;
1033 struct ftrace_page *pg;
1034 struct dyn_ftrace *rec;
1035 int type = MATCH_FULL;
1036 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1037 unsigned i, match = 0, search_len = 0;
1039 for (i = 0; i < len; i++) {
1040 if (buff[i] == '*') {
1042 search = buff + i + 1;
1043 type = MATCH_END_ONLY;
1044 search_len = len - (i + 1);
1046 if (type == MATCH_END_ONLY) {
1047 type = MATCH_MIDDLE_ONLY;
1050 type = MATCH_FRONT_ONLY;
1058 /* keep kstop machine from running */
1061 ftrace_filtered = 1;
1062 pg = ftrace_pages_start;
1064 for (i = 0; i < pg->index; i++) {
1068 rec = &pg->records[i];
1069 if (rec->flags & FTRACE_FL_FAILED)
1071 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1074 if (strcmp(str, buff) == 0)
1077 case MATCH_FRONT_ONLY:
1078 if (memcmp(str, buff, match) == 0)
1081 case MATCH_MIDDLE_ONLY:
1082 if (strstr(str, search))
1085 case MATCH_END_ONLY:
1086 ptr = strstr(str, search);
1087 if (ptr && (ptr[search_len] == 0))
1100 ftrace_regex_write(struct file *file, const char __user *ubuf,
1101 size_t cnt, loff_t *ppos, int enable)
1103 struct ftrace_iterator *iter;
1108 if (!cnt || cnt < 0)
1111 mutex_lock(&ftrace_regex_lock);
1113 if (file->f_mode & FMODE_READ) {
1114 struct seq_file *m = file->private_data;
1117 iter = file->private_data;
1120 iter->flags &= ~FTRACE_ITER_CONT;
1121 iter->buffer_idx = 0;
1124 ret = get_user(ch, ubuf++);
1130 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1131 /* skip white space */
1132 while (cnt && isspace(ch)) {
1133 ret = get_user(ch, ubuf++);
1141 file->f_pos += read;
1146 iter->buffer_idx = 0;
1149 while (cnt && !isspace(ch)) {
1150 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1151 iter->buffer[iter->buffer_idx++] = ch;
1156 ret = get_user(ch, ubuf++);
1165 iter->buffer[iter->buffer_idx] = 0;
1166 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1167 iter->buffer_idx = 0;
1169 iter->flags |= FTRACE_ITER_CONT;
1172 file->f_pos += read;
1176 mutex_unlock(&ftrace_regex_lock);
1182 ftrace_filter_write(struct file *file, const char __user *ubuf,
1183 size_t cnt, loff_t *ppos)
1185 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1189 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1190 size_t cnt, loff_t *ppos)
1192 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1196 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1198 if (unlikely(ftrace_disabled))
1201 mutex_lock(&ftrace_regex_lock);
1203 ftrace_filter_reset(enable);
1205 ftrace_match(buf, len, enable);
1206 mutex_unlock(&ftrace_regex_lock);
1210 * ftrace_set_filter - set a function to filter on in ftrace
1211 * @buf - the string that holds the function filter text.
1212 * @len - the length of the string.
1213 * @reset - non zero to reset all filters before applying this filter.
1215 * Filters denote which functions should be enabled when tracing is enabled.
1216 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1218 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1220 ftrace_set_regex(buf, len, reset, 1);
1224 * ftrace_set_notrace - set a function to not trace in ftrace
1225 * @buf - the string that holds the function notrace text.
1226 * @len - the length of the string.
1227 * @reset - non zero to reset all filters before applying this filter.
1229 * Notrace Filters denote which functions should not be enabled when tracing
1230 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1233 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1235 ftrace_set_regex(buf, len, reset, 0);
1239 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1241 struct seq_file *m = (struct seq_file *)file->private_data;
1242 struct ftrace_iterator *iter;
1244 mutex_lock(&ftrace_regex_lock);
1245 if (file->f_mode & FMODE_READ) {
1248 seq_release(inode, file);
1250 iter = file->private_data;
1252 if (iter->buffer_idx) {
1254 iter->buffer[iter->buffer_idx] = 0;
1255 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1258 mutex_lock(&ftrace_sysctl_lock);
1259 mutex_lock(&ftraced_lock);
1260 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1261 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1262 mutex_unlock(&ftraced_lock);
1263 mutex_unlock(&ftrace_sysctl_lock);
1266 mutex_unlock(&ftrace_regex_lock);
1271 ftrace_filter_release(struct inode *inode, struct file *file)
1273 return ftrace_regex_release(inode, file, 1);
1277 ftrace_notrace_release(struct inode *inode, struct file *file)
1279 return ftrace_regex_release(inode, file, 0);
1283 ftraced_read(struct file *filp, char __user *ubuf,
1284 size_t cnt, loff_t *ppos)
1286 /* don't worry about races */
1287 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1288 int r = strlen(buf);
1290 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1294 ftraced_write(struct file *filp, const char __user *ubuf,
1295 size_t cnt, loff_t *ppos)
1301 if (cnt >= sizeof(buf))
1304 if (copy_from_user(&buf, ubuf, cnt))
1307 if (strncmp(buf, "enable", 6) == 0)
1309 else if (strncmp(buf, "disable", 7) == 0)
1314 ret = strict_strtoul(buf, 10, &val);
1322 ftrace_enable_daemon();
1324 ftrace_disable_daemon();
1331 static struct file_operations ftrace_avail_fops = {
1332 .open = ftrace_avail_open,
1334 .llseek = seq_lseek,
1335 .release = ftrace_avail_release,
1338 static struct file_operations ftrace_failures_fops = {
1339 .open = ftrace_failures_open,
1341 .llseek = seq_lseek,
1342 .release = ftrace_avail_release,
1345 static struct file_operations ftrace_filter_fops = {
1346 .open = ftrace_filter_open,
1347 .read = ftrace_regex_read,
1348 .write = ftrace_filter_write,
1349 .llseek = ftrace_regex_lseek,
1350 .release = ftrace_filter_release,
1353 static struct file_operations ftrace_notrace_fops = {
1354 .open = ftrace_notrace_open,
1355 .read = ftrace_regex_read,
1356 .write = ftrace_notrace_write,
1357 .llseek = ftrace_regex_lseek,
1358 .release = ftrace_notrace_release,
1361 static struct file_operations ftraced_fops = {
1362 .open = tracing_open_generic,
1363 .read = ftraced_read,
1364 .write = ftraced_write,
1368 * ftrace_force_update - force an update to all recording ftrace functions
1370 int ftrace_force_update(void)
1374 if (unlikely(ftrace_disabled))
1377 mutex_lock(&ftrace_sysctl_lock);
1378 mutex_lock(&ftraced_lock);
1381 * If ftraced_trigger is not set, then there is nothing
1384 if (ftraced_trigger && !ftrace_update_code())
1387 mutex_unlock(&ftraced_lock);
1388 mutex_unlock(&ftrace_sysctl_lock);
1393 static void ftrace_force_shutdown(void)
1395 struct task_struct *task;
1396 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1398 mutex_lock(&ftraced_lock);
1399 task = ftraced_task;
1400 ftraced_task = NULL;
1401 ftraced_suspend = -1;
1402 ftrace_run_update_code(command);
1403 mutex_unlock(&ftraced_lock);
1409 static __init int ftrace_init_debugfs(void)
1411 struct dentry *d_tracer;
1412 struct dentry *entry;
1414 d_tracer = tracing_init_dentry();
1416 entry = debugfs_create_file("available_filter_functions", 0444,
1417 d_tracer, NULL, &ftrace_avail_fops);
1419 pr_warning("Could not create debugfs "
1420 "'available_filter_functions' entry\n");
1422 entry = debugfs_create_file("failures", 0444,
1423 d_tracer, NULL, &ftrace_failures_fops);
1425 pr_warning("Could not create debugfs 'failures' entry\n");
1427 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1428 NULL, &ftrace_filter_fops);
1430 pr_warning("Could not create debugfs "
1431 "'set_ftrace_filter' entry\n");
1433 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1434 NULL, &ftrace_notrace_fops);
1436 pr_warning("Could not create debugfs "
1437 "'set_ftrace_notrace' entry\n");
1439 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1440 NULL, &ftraced_fops);
1442 pr_warning("Could not create debugfs "
1443 "'ftraced_enabled' entry\n");
1447 fs_initcall(ftrace_init_debugfs);
1449 static int __init ftrace_dynamic_init(void)
1451 struct task_struct *p;
1455 addr = (unsigned long)ftrace_record_ip;
1457 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1459 /* ftrace_dyn_arch_init places the return code in addr */
1465 ret = ftrace_dyn_table_alloc();
1469 p = kthread_run(ftraced, NULL, "ftraced");
1475 last_ftrace_enabled = ftrace_enabled = 1;
1481 ftrace_disabled = 1;
1485 core_initcall(ftrace_dynamic_init);
1487 # define ftrace_startup() do { } while (0)
1488 # define ftrace_shutdown() do { } while (0)
1489 # define ftrace_startup_sysctl() do { } while (0)
1490 # define ftrace_shutdown_sysctl() do { } while (0)
1491 # define ftrace_force_shutdown() do { } while (0)
1492 #endif /* CONFIG_DYNAMIC_FTRACE */
1495 * ftrace_kill - totally shutdown ftrace
1497 * This is a safety measure. If something was detected that seems
1498 * wrong, calling this function will keep ftrace from doing
1499 * any more modifications, and updates.
1500 * used when something went wrong.
1502 void ftrace_kill(void)
1504 mutex_lock(&ftrace_sysctl_lock);
1505 ftrace_disabled = 1;
1508 clear_ftrace_function();
1509 mutex_unlock(&ftrace_sysctl_lock);
1511 /* Try to totally disable ftrace */
1512 ftrace_force_shutdown();
1516 * register_ftrace_function - register a function for profiling
1517 * @ops - ops structure that holds the function for profiling.
1519 * Register a function to be called by all functions in the
1522 * Note: @ops->func and all the functions it calls must be labeled
1523 * with "notrace", otherwise it will go into a
1526 int register_ftrace_function(struct ftrace_ops *ops)
1530 if (unlikely(ftrace_disabled))
1533 mutex_lock(&ftrace_sysctl_lock);
1534 ret = __register_ftrace_function(ops);
1536 mutex_unlock(&ftrace_sysctl_lock);
1542 * unregister_ftrace_function - unresgister a function for profiling.
1543 * @ops - ops structure that holds the function to unregister
1545 * Unregister a function that was added to be called by ftrace profiling.
1547 int unregister_ftrace_function(struct ftrace_ops *ops)
1551 mutex_lock(&ftrace_sysctl_lock);
1552 ret = __unregister_ftrace_function(ops);
1554 mutex_unlock(&ftrace_sysctl_lock);
1560 ftrace_enable_sysctl(struct ctl_table *table, int write,
1561 struct file *file, void __user *buffer, size_t *lenp,
1566 if (unlikely(ftrace_disabled))
1569 mutex_lock(&ftrace_sysctl_lock);
1571 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1573 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1576 last_ftrace_enabled = ftrace_enabled;
1578 if (ftrace_enabled) {
1580 ftrace_startup_sysctl();
1582 /* we are starting ftrace again */
1583 if (ftrace_list != &ftrace_list_end) {
1584 if (ftrace_list->next == &ftrace_list_end)
1585 ftrace_trace_function = ftrace_list->func;
1587 ftrace_trace_function = ftrace_list_func;
1591 /* stopping ftrace calls (just send to ftrace_stub) */
1592 ftrace_trace_function = ftrace_stub;
1594 ftrace_shutdown_sysctl();
1598 mutex_unlock(&ftrace_sysctl_lock);