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 set not to trace and
359 * it is enabled then disable it.
361 * If this record is not set to be filtered and
362 * it is enabled, disable it.
365 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
368 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
369 (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
370 !fl || (fl == FTRACE_FL_NOTRACE))
374 * If it is enabled disable it,
375 * otherwise enable it!
377 if (fl & FTRACE_FL_ENABLED) {
378 /* swap new and old */
380 old = ftrace_call_replace(ip, FTRACE_ADDR);
381 rec->flags &= ~FTRACE_FL_ENABLED;
383 new = ftrace_call_replace(ip, FTRACE_ADDR);
384 rec->flags |= FTRACE_FL_ENABLED;
390 * If this record is set not to trace and is
391 * not enabled, do nothing.
393 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
394 if (fl == FTRACE_FL_NOTRACE)
397 new = ftrace_call_replace(ip, FTRACE_ADDR);
399 old = ftrace_call_replace(ip, FTRACE_ADDR);
402 if (rec->flags & FTRACE_FL_ENABLED)
404 rec->flags |= FTRACE_FL_ENABLED;
406 if (!(rec->flags & FTRACE_FL_ENABLED))
408 rec->flags &= ~FTRACE_FL_ENABLED;
412 return ftrace_modify_code(ip, old, new);
415 static void ftrace_replace_code(int enable)
418 unsigned char *new = NULL, *old = NULL;
419 struct dyn_ftrace *rec;
420 struct ftrace_page *pg;
423 old = ftrace_nop_replace();
425 new = ftrace_nop_replace();
427 for (pg = ftrace_pages_start; pg; pg = pg->next) {
428 for (i = 0; i < pg->index; i++) {
429 rec = &pg->records[i];
431 /* don't modify code that has already faulted */
432 if (rec->flags & FTRACE_FL_FAILED)
435 failed = __ftrace_replace_code(rec, old, new, enable);
436 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
437 rec->flags |= FTRACE_FL_FAILED;
438 if ((system_state == SYSTEM_BOOTING) ||
439 !core_kernel_text(rec->ip)) {
440 ftrace_del_hash(rec);
441 ftrace_free_rec(rec);
448 static void ftrace_shutdown_replenish(void)
450 if (ftrace_pages->next)
453 /* allocate another page */
454 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
458 ftrace_code_disable(struct dyn_ftrace *rec)
461 unsigned char *nop, *call;
466 nop = ftrace_nop_replace();
467 call = ftrace_call_replace(ip, MCOUNT_ADDR);
469 failed = ftrace_modify_code(ip, call, nop);
471 rec->flags |= FTRACE_FL_FAILED;
477 static int __ftrace_update_code(void *ignore);
479 static int __ftrace_modify_code(void *data)
484 if (*command & FTRACE_ENABLE_CALLS) {
486 * Update any recorded ips now that we have the
489 __ftrace_update_code(NULL);
490 ftrace_replace_code(1);
491 } else if (*command & FTRACE_DISABLE_CALLS)
492 ftrace_replace_code(0);
494 if (*command & FTRACE_UPDATE_TRACE_FUNC)
495 ftrace_update_ftrace_func(ftrace_trace_function);
497 if (*command & FTRACE_ENABLE_MCOUNT) {
498 addr = (unsigned long)ftrace_record_ip;
499 ftrace_mcount_set(&addr);
500 } else if (*command & FTRACE_DISABLE_MCOUNT) {
501 addr = (unsigned long)ftrace_stub;
502 ftrace_mcount_set(&addr);
508 static void ftrace_run_update_code(int command)
510 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
513 void ftrace_disable_daemon(void)
515 /* Stop the daemon from calling kstop_machine */
516 mutex_lock(&ftraced_lock);
518 mutex_unlock(&ftraced_lock);
520 ftrace_force_update();
523 void ftrace_enable_daemon(void)
525 mutex_lock(&ftraced_lock);
527 mutex_unlock(&ftraced_lock);
529 ftrace_force_update();
532 static ftrace_func_t saved_ftrace_func;
534 static void ftrace_startup(void)
538 if (unlikely(ftrace_disabled))
541 mutex_lock(&ftraced_lock);
543 if (ftraced_suspend == 1)
544 command |= FTRACE_ENABLE_CALLS;
546 if (saved_ftrace_func != ftrace_trace_function) {
547 saved_ftrace_func = ftrace_trace_function;
548 command |= FTRACE_UPDATE_TRACE_FUNC;
551 if (!command || !ftrace_enabled)
554 ftrace_run_update_code(command);
556 mutex_unlock(&ftraced_lock);
559 static void ftrace_shutdown(void)
563 if (unlikely(ftrace_disabled))
566 mutex_lock(&ftraced_lock);
568 if (!ftraced_suspend)
569 command |= FTRACE_DISABLE_CALLS;
571 if (saved_ftrace_func != ftrace_trace_function) {
572 saved_ftrace_func = ftrace_trace_function;
573 command |= FTRACE_UPDATE_TRACE_FUNC;
576 if (!command || !ftrace_enabled)
579 ftrace_run_update_code(command);
581 mutex_unlock(&ftraced_lock);
584 static void ftrace_startup_sysctl(void)
586 int command = FTRACE_ENABLE_MCOUNT;
588 if (unlikely(ftrace_disabled))
591 mutex_lock(&ftraced_lock);
592 /* Force update next time */
593 saved_ftrace_func = NULL;
594 /* ftraced_suspend is true if we want ftrace running */
596 command |= FTRACE_ENABLE_CALLS;
598 ftrace_run_update_code(command);
599 mutex_unlock(&ftraced_lock);
602 static void ftrace_shutdown_sysctl(void)
604 int command = FTRACE_DISABLE_MCOUNT;
606 if (unlikely(ftrace_disabled))
609 mutex_lock(&ftraced_lock);
610 /* ftraced_suspend is true if ftrace is running */
612 command |= FTRACE_DISABLE_CALLS;
614 ftrace_run_update_code(command);
615 mutex_unlock(&ftraced_lock);
618 static cycle_t ftrace_update_time;
619 static unsigned long ftrace_update_cnt;
620 unsigned long ftrace_update_tot_cnt;
622 static int __ftrace_update_code(void *ignore)
624 struct dyn_ftrace *p;
625 struct hlist_node *t, *n;
626 int save_ftrace_enabled;
630 /* Don't be recording funcs now */
631 ftrace_record_suspend++;
632 save_ftrace_enabled = ftrace_enabled;
635 start = ftrace_now(raw_smp_processor_id());
636 ftrace_update_cnt = 0;
638 /* No locks needed, the machine is stopped! */
639 for (i = 0; i < FTRACE_HASHSIZE; i++) {
640 /* all CPUS are stopped, we are safe to modify code */
641 hlist_for_each_entry_safe(p, t, n, &ftrace_hash[i], node) {
642 /* Skip over failed records which have not been
644 if (p->flags & FTRACE_FL_FAILED)
647 /* Unconverted records are always at the head of the
648 * hash bucket. Once we encounter a converted record,
649 * simply skip over to the next bucket. Saves ftraced
650 * some processor cycles (ftrace does its bid for
651 * global warming :-p ). */
652 if (p->flags & (FTRACE_FL_CONVERTED))
655 if (ftrace_code_disable(p)) {
656 p->flags |= FTRACE_FL_CONVERTED;
659 if ((system_state == SYSTEM_BOOTING) ||
660 !core_kernel_text(p->ip)) {
668 stop = ftrace_now(raw_smp_processor_id());
669 ftrace_update_time = stop - start;
670 ftrace_update_tot_cnt += ftrace_update_cnt;
673 ftrace_enabled = save_ftrace_enabled;
674 ftrace_record_suspend--;
679 static int ftrace_update_code(void)
681 if (unlikely(ftrace_disabled) ||
682 !ftrace_enabled || !ftraced_trigger)
685 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
690 static int ftraced(void *ignore)
694 while (!kthread_should_stop()) {
696 set_current_state(TASK_INTERRUPTIBLE);
698 /* check once a second */
699 schedule_timeout(HZ);
701 if (unlikely(ftrace_disabled))
704 mutex_lock(&ftrace_sysctl_lock);
705 mutex_lock(&ftraced_lock);
706 if (!ftraced_suspend && !ftraced_stop &&
707 ftrace_update_code()) {
708 usecs = nsecs_to_usecs(ftrace_update_time);
709 if (ftrace_update_tot_cnt > 100000) {
710 ftrace_update_tot_cnt = 0;
711 pr_info("hm, dftrace overflow: %lu change%s"
712 " (%lu total) in %lu usec%s\n",
714 ftrace_update_cnt != 1 ? "s" : "",
715 ftrace_update_tot_cnt,
716 usecs, usecs != 1 ? "s" : "");
721 mutex_unlock(&ftraced_lock);
722 mutex_unlock(&ftrace_sysctl_lock);
724 ftrace_shutdown_replenish();
726 __set_current_state(TASK_RUNNING);
730 static int __init ftrace_dyn_table_alloc(void)
732 struct ftrace_page *pg;
736 /* allocate a few pages */
737 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
738 if (!ftrace_pages_start)
742 * Allocate a few more pages.
744 * TODO: have some parser search vmlinux before
745 * final linking to find all calls to ftrace.
747 * a) know how many pages to allocate.
749 * b) set up the table then.
751 * The dynamic code is still necessary for
755 pg = ftrace_pages = ftrace_pages_start;
757 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
759 for (i = 0; i < cnt; i++) {
760 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
762 /* If we fail, we'll try later anyway */
773 FTRACE_ITER_FILTER = (1 << 0),
774 FTRACE_ITER_CONT = (1 << 1),
775 FTRACE_ITER_NOTRACE = (1 << 2),
776 FTRACE_ITER_FAILURES = (1 << 3),
779 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
781 struct ftrace_iterator {
783 struct ftrace_page *pg;
786 unsigned char buffer[FTRACE_BUFF_MAX+1];
792 t_next(struct seq_file *m, void *v, loff_t *pos)
794 struct ftrace_iterator *iter = m->private;
795 struct dyn_ftrace *rec = NULL;
800 if (iter->idx >= iter->pg->index) {
801 if (iter->pg->next) {
802 iter->pg = iter->pg->next;
807 rec = &iter->pg->records[iter->idx++];
808 if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
809 (rec->flags & FTRACE_FL_FAILED)) ||
811 ((iter->flags & FTRACE_ITER_FAILURES) &&
812 (!(rec->flags & FTRACE_FL_FAILED) ||
813 (rec->flags & FTRACE_FL_FREE))) ||
815 ((iter->flags & FTRACE_ITER_FILTER) &&
816 !(rec->flags & FTRACE_FL_FILTER)) ||
818 ((iter->flags & FTRACE_ITER_NOTRACE) &&
819 !(rec->flags & FTRACE_FL_NOTRACE))) {
830 static void *t_start(struct seq_file *m, loff_t *pos)
832 struct ftrace_iterator *iter = m->private;
836 if (*pos != iter->pos) {
837 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
841 p = t_next(m, p, &l);
847 static void t_stop(struct seq_file *m, void *p)
851 static int t_show(struct seq_file *m, void *v)
853 struct dyn_ftrace *rec = v;
854 char str[KSYM_SYMBOL_LEN];
859 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
861 seq_printf(m, "%s\n", str);
866 static struct seq_operations show_ftrace_seq_ops = {
874 ftrace_avail_open(struct inode *inode, struct file *file)
876 struct ftrace_iterator *iter;
879 if (unlikely(ftrace_disabled))
882 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
886 iter->pg = ftrace_pages_start;
889 ret = seq_open(file, &show_ftrace_seq_ops);
891 struct seq_file *m = file->private_data;
901 int ftrace_avail_release(struct inode *inode, struct file *file)
903 struct seq_file *m = (struct seq_file *)file->private_data;
904 struct ftrace_iterator *iter = m->private;
906 seq_release(inode, file);
913 ftrace_failures_open(struct inode *inode, struct file *file)
917 struct ftrace_iterator *iter;
919 ret = ftrace_avail_open(inode, file);
921 m = (struct seq_file *)file->private_data;
922 iter = (struct ftrace_iterator *)m->private;
923 iter->flags = FTRACE_ITER_FAILURES;
930 static void ftrace_filter_reset(int enable)
932 struct ftrace_page *pg;
933 struct dyn_ftrace *rec;
934 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
937 /* keep kstop machine from running */
941 pg = ftrace_pages_start;
943 for (i = 0; i < pg->index; i++) {
944 rec = &pg->records[i];
945 if (rec->flags & FTRACE_FL_FAILED)
955 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
957 struct ftrace_iterator *iter;
960 if (unlikely(ftrace_disabled))
963 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
967 mutex_lock(&ftrace_regex_lock);
968 if ((file->f_mode & FMODE_WRITE) &&
969 !(file->f_flags & O_APPEND))
970 ftrace_filter_reset(enable);
972 if (file->f_mode & FMODE_READ) {
973 iter->pg = ftrace_pages_start;
975 iter->flags = enable ? FTRACE_ITER_FILTER :
978 ret = seq_open(file, &show_ftrace_seq_ops);
980 struct seq_file *m = file->private_data;
985 file->private_data = iter;
986 mutex_unlock(&ftrace_regex_lock);
992 ftrace_filter_open(struct inode *inode, struct file *file)
994 return ftrace_regex_open(inode, file, 1);
998 ftrace_notrace_open(struct inode *inode, struct file *file)
1000 return ftrace_regex_open(inode, file, 0);
1004 ftrace_regex_read(struct file *file, char __user *ubuf,
1005 size_t cnt, loff_t *ppos)
1007 if (file->f_mode & FMODE_READ)
1008 return seq_read(file, ubuf, cnt, ppos);
1014 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1018 if (file->f_mode & FMODE_READ)
1019 ret = seq_lseek(file, offset, origin);
1021 file->f_pos = ret = 1;
1034 ftrace_match(unsigned char *buff, int len, int enable)
1036 char str[KSYM_SYMBOL_LEN];
1037 char *search = NULL;
1038 struct ftrace_page *pg;
1039 struct dyn_ftrace *rec;
1040 int type = MATCH_FULL;
1041 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1042 unsigned i, match = 0, search_len = 0;
1044 for (i = 0; i < len; i++) {
1045 if (buff[i] == '*') {
1047 search = buff + i + 1;
1048 type = MATCH_END_ONLY;
1049 search_len = len - (i + 1);
1051 if (type == MATCH_END_ONLY) {
1052 type = MATCH_MIDDLE_ONLY;
1055 type = MATCH_FRONT_ONLY;
1063 /* keep kstop machine from running */
1066 ftrace_filtered = 1;
1067 pg = ftrace_pages_start;
1069 for (i = 0; i < pg->index; i++) {
1073 rec = &pg->records[i];
1074 if (rec->flags & FTRACE_FL_FAILED)
1076 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1079 if (strcmp(str, buff) == 0)
1082 case MATCH_FRONT_ONLY:
1083 if (memcmp(str, buff, match) == 0)
1086 case MATCH_MIDDLE_ONLY:
1087 if (strstr(str, search))
1090 case MATCH_END_ONLY:
1091 ptr = strstr(str, search);
1092 if (ptr && (ptr[search_len] == 0))
1105 ftrace_regex_write(struct file *file, const char __user *ubuf,
1106 size_t cnt, loff_t *ppos, int enable)
1108 struct ftrace_iterator *iter;
1113 if (!cnt || cnt < 0)
1116 mutex_lock(&ftrace_regex_lock);
1118 if (file->f_mode & FMODE_READ) {
1119 struct seq_file *m = file->private_data;
1122 iter = file->private_data;
1125 iter->flags &= ~FTRACE_ITER_CONT;
1126 iter->buffer_idx = 0;
1129 ret = get_user(ch, ubuf++);
1135 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1136 /* skip white space */
1137 while (cnt && isspace(ch)) {
1138 ret = get_user(ch, ubuf++);
1146 file->f_pos += read;
1151 iter->buffer_idx = 0;
1154 while (cnt && !isspace(ch)) {
1155 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1156 iter->buffer[iter->buffer_idx++] = ch;
1161 ret = get_user(ch, ubuf++);
1170 iter->buffer[iter->buffer_idx] = 0;
1171 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1172 iter->buffer_idx = 0;
1174 iter->flags |= FTRACE_ITER_CONT;
1177 file->f_pos += read;
1181 mutex_unlock(&ftrace_regex_lock);
1187 ftrace_filter_write(struct file *file, const char __user *ubuf,
1188 size_t cnt, loff_t *ppos)
1190 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1194 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1195 size_t cnt, loff_t *ppos)
1197 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1201 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1203 if (unlikely(ftrace_disabled))
1206 mutex_lock(&ftrace_regex_lock);
1208 ftrace_filter_reset(enable);
1210 ftrace_match(buf, len, enable);
1211 mutex_unlock(&ftrace_regex_lock);
1215 * ftrace_set_filter - set a function to filter on in ftrace
1216 * @buf - the string that holds the function filter text.
1217 * @len - the length of the string.
1218 * @reset - non zero to reset all filters before applying this filter.
1220 * Filters denote which functions should be enabled when tracing is enabled.
1221 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1223 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1225 ftrace_set_regex(buf, len, reset, 1);
1229 * ftrace_set_notrace - set a function to not trace in ftrace
1230 * @buf - the string that holds the function notrace text.
1231 * @len - the length of the string.
1232 * @reset - non zero to reset all filters before applying this filter.
1234 * Notrace Filters denote which functions should not be enabled when tracing
1235 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1238 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1240 ftrace_set_regex(buf, len, reset, 0);
1244 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1246 struct seq_file *m = (struct seq_file *)file->private_data;
1247 struct ftrace_iterator *iter;
1249 mutex_lock(&ftrace_regex_lock);
1250 if (file->f_mode & FMODE_READ) {
1253 seq_release(inode, file);
1255 iter = file->private_data;
1257 if (iter->buffer_idx) {
1259 iter->buffer[iter->buffer_idx] = 0;
1260 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1263 mutex_lock(&ftrace_sysctl_lock);
1264 mutex_lock(&ftraced_lock);
1265 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1266 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1267 mutex_unlock(&ftraced_lock);
1268 mutex_unlock(&ftrace_sysctl_lock);
1271 mutex_unlock(&ftrace_regex_lock);
1276 ftrace_filter_release(struct inode *inode, struct file *file)
1278 return ftrace_regex_release(inode, file, 1);
1282 ftrace_notrace_release(struct inode *inode, struct file *file)
1284 return ftrace_regex_release(inode, file, 0);
1288 ftraced_read(struct file *filp, char __user *ubuf,
1289 size_t cnt, loff_t *ppos)
1291 /* don't worry about races */
1292 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1293 int r = strlen(buf);
1295 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1299 ftraced_write(struct file *filp, const char __user *ubuf,
1300 size_t cnt, loff_t *ppos)
1306 if (cnt >= sizeof(buf))
1309 if (copy_from_user(&buf, ubuf, cnt))
1312 if (strncmp(buf, "enable", 6) == 0)
1314 else if (strncmp(buf, "disable", 7) == 0)
1319 ret = strict_strtoul(buf, 10, &val);
1327 ftrace_enable_daemon();
1329 ftrace_disable_daemon();
1336 static struct file_operations ftrace_avail_fops = {
1337 .open = ftrace_avail_open,
1339 .llseek = seq_lseek,
1340 .release = ftrace_avail_release,
1343 static struct file_operations ftrace_failures_fops = {
1344 .open = ftrace_failures_open,
1346 .llseek = seq_lseek,
1347 .release = ftrace_avail_release,
1350 static struct file_operations ftrace_filter_fops = {
1351 .open = ftrace_filter_open,
1352 .read = ftrace_regex_read,
1353 .write = ftrace_filter_write,
1354 .llseek = ftrace_regex_lseek,
1355 .release = ftrace_filter_release,
1358 static struct file_operations ftrace_notrace_fops = {
1359 .open = ftrace_notrace_open,
1360 .read = ftrace_regex_read,
1361 .write = ftrace_notrace_write,
1362 .llseek = ftrace_regex_lseek,
1363 .release = ftrace_notrace_release,
1366 static struct file_operations ftraced_fops = {
1367 .open = tracing_open_generic,
1368 .read = ftraced_read,
1369 .write = ftraced_write,
1373 * ftrace_force_update - force an update to all recording ftrace functions
1375 int ftrace_force_update(void)
1379 if (unlikely(ftrace_disabled))
1382 mutex_lock(&ftrace_sysctl_lock);
1383 mutex_lock(&ftraced_lock);
1386 * If ftraced_trigger is not set, then there is nothing
1389 if (ftraced_trigger && !ftrace_update_code())
1392 mutex_unlock(&ftraced_lock);
1393 mutex_unlock(&ftrace_sysctl_lock);
1398 static void ftrace_force_shutdown(void)
1400 struct task_struct *task;
1401 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1403 mutex_lock(&ftraced_lock);
1404 task = ftraced_task;
1405 ftraced_task = NULL;
1406 ftraced_suspend = -1;
1407 ftrace_run_update_code(command);
1408 mutex_unlock(&ftraced_lock);
1414 static __init int ftrace_init_debugfs(void)
1416 struct dentry *d_tracer;
1417 struct dentry *entry;
1419 d_tracer = tracing_init_dentry();
1421 entry = debugfs_create_file("available_filter_functions", 0444,
1422 d_tracer, NULL, &ftrace_avail_fops);
1424 pr_warning("Could not create debugfs "
1425 "'available_filter_functions' entry\n");
1427 entry = debugfs_create_file("failures", 0444,
1428 d_tracer, NULL, &ftrace_failures_fops);
1430 pr_warning("Could not create debugfs 'failures' entry\n");
1432 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1433 NULL, &ftrace_filter_fops);
1435 pr_warning("Could not create debugfs "
1436 "'set_ftrace_filter' entry\n");
1438 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1439 NULL, &ftrace_notrace_fops);
1441 pr_warning("Could not create debugfs "
1442 "'set_ftrace_notrace' entry\n");
1444 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1445 NULL, &ftraced_fops);
1447 pr_warning("Could not create debugfs "
1448 "'ftraced_enabled' entry\n");
1452 fs_initcall(ftrace_init_debugfs);
1454 static int __init ftrace_dynamic_init(void)
1456 struct task_struct *p;
1460 addr = (unsigned long)ftrace_record_ip;
1462 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1464 /* ftrace_dyn_arch_init places the return code in addr */
1470 ret = ftrace_dyn_table_alloc();
1474 p = kthread_run(ftraced, NULL, "ftraced");
1480 last_ftrace_enabled = ftrace_enabled = 1;
1486 ftrace_disabled = 1;
1490 core_initcall(ftrace_dynamic_init);
1492 # define ftrace_startup() do { } while (0)
1493 # define ftrace_shutdown() do { } while (0)
1494 # define ftrace_startup_sysctl() do { } while (0)
1495 # define ftrace_shutdown_sysctl() do { } while (0)
1496 # define ftrace_force_shutdown() do { } while (0)
1497 #endif /* CONFIG_DYNAMIC_FTRACE */
1500 * ftrace_kill - totally shutdown ftrace
1502 * This is a safety measure. If something was detected that seems
1503 * wrong, calling this function will keep ftrace from doing
1504 * any more modifications, and updates.
1505 * used when something went wrong.
1507 void ftrace_kill(void)
1509 mutex_lock(&ftrace_sysctl_lock);
1510 ftrace_disabled = 1;
1513 clear_ftrace_function();
1514 mutex_unlock(&ftrace_sysctl_lock);
1516 /* Try to totally disable ftrace */
1517 ftrace_force_shutdown();
1521 * register_ftrace_function - register a function for profiling
1522 * @ops - ops structure that holds the function for profiling.
1524 * Register a function to be called by all functions in the
1527 * Note: @ops->func and all the functions it calls must be labeled
1528 * with "notrace", otherwise it will go into a
1531 int register_ftrace_function(struct ftrace_ops *ops)
1535 if (unlikely(ftrace_disabled))
1538 mutex_lock(&ftrace_sysctl_lock);
1539 ret = __register_ftrace_function(ops);
1541 mutex_unlock(&ftrace_sysctl_lock);
1547 * unregister_ftrace_function - unresgister a function for profiling.
1548 * @ops - ops structure that holds the function to unregister
1550 * Unregister a function that was added to be called by ftrace profiling.
1552 int unregister_ftrace_function(struct ftrace_ops *ops)
1556 mutex_lock(&ftrace_sysctl_lock);
1557 ret = __unregister_ftrace_function(ops);
1559 mutex_unlock(&ftrace_sysctl_lock);
1565 ftrace_enable_sysctl(struct ctl_table *table, int write,
1566 struct file *file, void __user *buffer, size_t *lenp,
1571 if (unlikely(ftrace_disabled))
1574 mutex_lock(&ftrace_sysctl_lock);
1576 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1578 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1581 last_ftrace_enabled = ftrace_enabled;
1583 if (ftrace_enabled) {
1585 ftrace_startup_sysctl();
1587 /* we are starting ftrace again */
1588 if (ftrace_list != &ftrace_list_end) {
1589 if (ftrace_list->next == &ftrace_list_end)
1590 ftrace_trace_function = ftrace_list->func;
1592 ftrace_trace_function = ftrace_list_func;
1596 /* stopping ftrace calls (just send to ftrace_stub) */
1597 ftrace_trace_function = ftrace_stub;
1599 ftrace_shutdown_sysctl();
1603 mutex_unlock(&ftrace_sysctl_lock);