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;
154 static DECLARE_WAIT_QUEUE_HEAD(ftraced_waiters);
155 static unsigned long ftraced_iteration_counter;
158 FTRACE_ENABLE_CALLS = (1 << 0),
159 FTRACE_DISABLE_CALLS = (1 << 1),
160 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
161 FTRACE_ENABLE_MCOUNT = (1 << 3),
162 FTRACE_DISABLE_MCOUNT = (1 << 4),
165 static int ftrace_filtered;
167 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
169 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
171 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
172 static DEFINE_MUTEX(ftraced_lock);
173 static DEFINE_MUTEX(ftrace_regex_lock);
176 struct ftrace_page *next;
178 struct dyn_ftrace records[];
181 #define ENTRIES_PER_PAGE \
182 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
184 /* estimate from running different kernels */
185 #define NR_TO_INIT 10000
187 static struct ftrace_page *ftrace_pages_start;
188 static struct ftrace_page *ftrace_pages;
190 static int ftraced_trigger;
191 static int ftraced_suspend;
193 static int ftrace_record_suspend;
195 static struct dyn_ftrace *ftrace_free_records;
198 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
200 struct dyn_ftrace *p;
201 struct hlist_node *t;
204 hlist_for_each_entry(p, t, &ftrace_hash[key], node) {
215 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
217 hlist_add_head(&node->node, &ftrace_hash[key]);
220 static void ftrace_free_rec(struct dyn_ftrace *rec)
222 /* no locking, only called from kstop_machine */
224 rec->ip = (unsigned long)ftrace_free_records;
225 ftrace_free_records = rec;
226 rec->flags |= FTRACE_FL_FREE;
229 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
231 struct dyn_ftrace *rec;
233 /* First check for freed records */
234 if (ftrace_free_records) {
235 rec = ftrace_free_records;
237 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
239 ftrace_free_records = NULL;
245 ftrace_free_records = (void *)rec->ip;
246 memset(rec, 0, sizeof(*rec));
250 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
251 if (!ftrace_pages->next)
253 ftrace_pages = ftrace_pages->next;
256 return &ftrace_pages->records[ftrace_pages->index++];
260 ftrace_record_ip(unsigned long ip)
262 struct dyn_ftrace *node;
269 if (!ftrace_enabled || ftrace_disabled)
272 resched = need_resched();
273 preempt_disable_notrace();
276 * We simply need to protect against recursion.
277 * Use the the raw version of smp_processor_id and not
278 * __get_cpu_var which can call debug hooks that can
279 * cause a recursive crash here.
281 cpu = raw_smp_processor_id();
282 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
283 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
286 if (unlikely(ftrace_record_suspend))
289 key = hash_long(ip, FTRACE_HASHBITS);
291 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
293 if (ftrace_ip_in_hash(ip, key))
296 atomic = irqs_disabled();
298 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
300 /* This ip may have hit the hash before the lock */
301 if (ftrace_ip_in_hash(ip, key))
305 * There's a slight race that the ftraced will update the
306 * hash and reset here. If it is already converted, skip it.
308 if (ftrace_ip_converted(ip))
311 node = ftrace_alloc_dyn_node(ip);
317 ftrace_add_hash(node, key);
322 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
324 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
326 /* prevent recursion with scheduler */
328 preempt_enable_no_resched_notrace();
330 preempt_enable_notrace();
333 #define FTRACE_ADDR ((long)(ftrace_caller))
334 #define MCOUNT_ADDR ((long)(mcount))
337 __ftrace_replace_code(struct dyn_ftrace *rec,
338 unsigned char *old, unsigned char *new, int enable)
340 unsigned long ip, fl;
345 if (ftrace_filtered && enable) {
347 * If filtering is on:
349 * If this record is set to be filtered and
350 * is enabled then do nothing.
352 * If this record is set to be filtered and
353 * it is not enabled, enable it.
355 * If this record is not set to be filtered
356 * and it is not enabled do nothing.
358 * If this record is set not to trace then
361 * If this record is not set to be filtered and
362 * it is enabled, disable it.
364 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
366 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
367 (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
371 * If it is enabled disable it,
372 * otherwise enable it!
374 if (fl == FTRACE_FL_ENABLED) {
375 /* swap new and old */
377 old = ftrace_call_replace(ip, FTRACE_ADDR);
378 rec->flags &= ~FTRACE_FL_ENABLED;
380 new = ftrace_call_replace(ip, FTRACE_ADDR);
381 rec->flags |= FTRACE_FL_ENABLED;
387 * If this record is set not to trace and is
388 * not enabled, do nothing.
390 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
391 if (fl == FTRACE_FL_NOTRACE)
394 new = ftrace_call_replace(ip, FTRACE_ADDR);
396 old = ftrace_call_replace(ip, FTRACE_ADDR);
399 if (rec->flags & FTRACE_FL_ENABLED)
401 rec->flags |= FTRACE_FL_ENABLED;
403 if (!(rec->flags & FTRACE_FL_ENABLED))
405 rec->flags &= ~FTRACE_FL_ENABLED;
409 failed = ftrace_modify_code(ip, old, new);
412 /* It is possible that the function hasn't been converted yet */
413 key = hash_long(ip, FTRACE_HASHBITS);
414 if (!ftrace_ip_in_hash(ip, key)) {
415 rec->flags |= FTRACE_FL_FAILED;
416 ftrace_free_rec(rec);
422 static void ftrace_replace_code(int enable)
424 unsigned char *new = NULL, *old = NULL;
425 struct dyn_ftrace *rec;
426 struct ftrace_page *pg;
430 old = ftrace_nop_replace();
432 new = ftrace_nop_replace();
434 for (pg = ftrace_pages_start; pg; pg = pg->next) {
435 for (i = 0; i < pg->index; i++) {
436 rec = &pg->records[i];
438 /* don't modify code that has already faulted */
439 if (rec->flags & FTRACE_FL_FAILED)
442 __ftrace_replace_code(rec, old, new, enable);
447 static void ftrace_shutdown_replenish(void)
449 if (ftrace_pages->next)
452 /* allocate another page */
453 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
457 ftrace_code_disable(struct dyn_ftrace *rec)
460 unsigned char *nop, *call;
465 nop = ftrace_nop_replace();
466 call = ftrace_call_replace(ip, MCOUNT_ADDR);
468 failed = ftrace_modify_code(ip, call, nop);
470 rec->flags |= FTRACE_FL_FAILED;
471 ftrace_free_rec(rec);
475 static int __ftrace_modify_code(void *data)
480 if (*command & FTRACE_ENABLE_CALLS)
481 ftrace_replace_code(1);
482 else if (*command & FTRACE_DISABLE_CALLS)
483 ftrace_replace_code(0);
485 if (*command & FTRACE_UPDATE_TRACE_FUNC)
486 ftrace_update_ftrace_func(ftrace_trace_function);
488 if (*command & FTRACE_ENABLE_MCOUNT) {
489 addr = (unsigned long)ftrace_record_ip;
490 ftrace_mcount_set(&addr);
491 } else if (*command & FTRACE_DISABLE_MCOUNT) {
492 addr = (unsigned long)ftrace_stub;
493 ftrace_mcount_set(&addr);
499 static void ftrace_run_update_code(int command)
501 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
504 static ftrace_func_t saved_ftrace_func;
506 static void ftrace_startup(void)
510 if (unlikely(ftrace_disabled))
513 mutex_lock(&ftraced_lock);
515 if (ftraced_suspend == 1)
516 command |= FTRACE_ENABLE_CALLS;
518 if (saved_ftrace_func != ftrace_trace_function) {
519 saved_ftrace_func = ftrace_trace_function;
520 command |= FTRACE_UPDATE_TRACE_FUNC;
523 if (!command || !ftrace_enabled)
526 ftrace_run_update_code(command);
528 mutex_unlock(&ftraced_lock);
531 static void ftrace_shutdown(void)
535 if (unlikely(ftrace_disabled))
538 mutex_lock(&ftraced_lock);
540 if (!ftraced_suspend)
541 command |= FTRACE_DISABLE_CALLS;
543 if (saved_ftrace_func != ftrace_trace_function) {
544 saved_ftrace_func = ftrace_trace_function;
545 command |= FTRACE_UPDATE_TRACE_FUNC;
548 if (!command || !ftrace_enabled)
551 ftrace_run_update_code(command);
553 mutex_unlock(&ftraced_lock);
556 static void ftrace_startup_sysctl(void)
558 int command = FTRACE_ENABLE_MCOUNT;
560 if (unlikely(ftrace_disabled))
563 mutex_lock(&ftraced_lock);
564 /* Force update next time */
565 saved_ftrace_func = NULL;
566 /* ftraced_suspend is true if we want ftrace running */
568 command |= FTRACE_ENABLE_CALLS;
570 ftrace_run_update_code(command);
571 mutex_unlock(&ftraced_lock);
574 static void ftrace_shutdown_sysctl(void)
576 int command = FTRACE_DISABLE_MCOUNT;
578 if (unlikely(ftrace_disabled))
581 mutex_lock(&ftraced_lock);
582 /* ftraced_suspend is true if ftrace is running */
584 command |= FTRACE_DISABLE_CALLS;
586 ftrace_run_update_code(command);
587 mutex_unlock(&ftraced_lock);
590 static cycle_t ftrace_update_time;
591 static unsigned long ftrace_update_cnt;
592 unsigned long ftrace_update_tot_cnt;
594 static int __ftrace_update_code(void *ignore)
596 struct dyn_ftrace *p;
597 struct hlist_head head;
598 struct hlist_node *t;
599 int save_ftrace_enabled;
603 /* Don't be recording funcs now */
604 save_ftrace_enabled = ftrace_enabled;
607 start = ftrace_now(raw_smp_processor_id());
608 ftrace_update_cnt = 0;
610 /* No locks needed, the machine is stopped! */
611 for (i = 0; i < FTRACE_HASHSIZE; i++) {
612 if (hlist_empty(&ftrace_hash[i]))
615 head = ftrace_hash[i];
616 INIT_HLIST_HEAD(&ftrace_hash[i]);
618 /* all CPUS are stopped, we are safe to modify code */
619 hlist_for_each_entry(p, t, &head, node) {
620 ftrace_code_disable(p);
626 stop = ftrace_now(raw_smp_processor_id());
627 ftrace_update_time = stop - start;
628 ftrace_update_tot_cnt += ftrace_update_cnt;
630 ftrace_enabled = save_ftrace_enabled;
635 static void ftrace_update_code(void)
637 if (unlikely(ftrace_disabled))
640 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
643 static int ftraced(void *ignore)
647 while (!kthread_should_stop()) {
649 set_current_state(TASK_INTERRUPTIBLE);
651 /* check once a second */
652 schedule_timeout(HZ);
654 if (unlikely(ftrace_disabled))
657 mutex_lock(&ftrace_sysctl_lock);
658 mutex_lock(&ftraced_lock);
659 if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
660 ftrace_record_suspend++;
661 ftrace_update_code();
662 usecs = nsecs_to_usecs(ftrace_update_time);
663 if (ftrace_update_tot_cnt > 100000) {
664 ftrace_update_tot_cnt = 0;
665 pr_info("hm, dftrace overflow: %lu change%s"
666 " (%lu total) in %lu usec%s\n",
668 ftrace_update_cnt != 1 ? "s" : "",
669 ftrace_update_tot_cnt,
670 usecs, usecs != 1 ? "s" : "");
675 ftrace_record_suspend--;
677 ftraced_iteration_counter++;
678 mutex_unlock(&ftraced_lock);
679 mutex_unlock(&ftrace_sysctl_lock);
681 wake_up_interruptible(&ftraced_waiters);
683 ftrace_shutdown_replenish();
685 __set_current_state(TASK_RUNNING);
689 static int __init ftrace_dyn_table_alloc(void)
691 struct ftrace_page *pg;
695 /* allocate a few pages */
696 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
697 if (!ftrace_pages_start)
701 * Allocate a few more pages.
703 * TODO: have some parser search vmlinux before
704 * final linking to find all calls to ftrace.
706 * a) know how many pages to allocate.
708 * b) set up the table then.
710 * The dynamic code is still necessary for
714 pg = ftrace_pages = ftrace_pages_start;
716 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
718 for (i = 0; i < cnt; i++) {
719 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
721 /* If we fail, we'll try later anyway */
732 FTRACE_ITER_FILTER = (1 << 0),
733 FTRACE_ITER_CONT = (1 << 1),
734 FTRACE_ITER_NOTRACE = (1 << 2),
737 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
739 struct ftrace_iterator {
741 struct ftrace_page *pg;
744 unsigned char buffer[FTRACE_BUFF_MAX+1];
750 t_next(struct seq_file *m, void *v, loff_t *pos)
752 struct ftrace_iterator *iter = m->private;
753 struct dyn_ftrace *rec = NULL;
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_FAILED) ||
767 ((iter->flags & FTRACE_ITER_FILTER) &&
768 !(rec->flags & FTRACE_FL_FILTER)) ||
769 ((iter->flags & FTRACE_ITER_NOTRACE) &&
770 !(rec->flags & FTRACE_FL_NOTRACE))) {
781 static void *t_start(struct seq_file *m, loff_t *pos)
783 struct ftrace_iterator *iter = m->private;
787 if (*pos != iter->pos) {
788 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
792 p = t_next(m, p, &l);
798 static void t_stop(struct seq_file *m, void *p)
802 static int t_show(struct seq_file *m, void *v)
804 struct dyn_ftrace *rec = v;
805 char str[KSYM_SYMBOL_LEN];
810 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
812 seq_printf(m, "%s\n", str);
817 static struct seq_operations show_ftrace_seq_ops = {
825 ftrace_avail_open(struct inode *inode, struct file *file)
827 struct ftrace_iterator *iter;
830 if (unlikely(ftrace_disabled))
833 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
837 iter->pg = ftrace_pages_start;
840 ret = seq_open(file, &show_ftrace_seq_ops);
842 struct seq_file *m = file->private_data;
852 int ftrace_avail_release(struct inode *inode, struct file *file)
854 struct seq_file *m = (struct seq_file *)file->private_data;
855 struct ftrace_iterator *iter = m->private;
857 seq_release(inode, file);
863 static void ftrace_filter_reset(int enable)
865 struct ftrace_page *pg;
866 struct dyn_ftrace *rec;
867 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
870 /* keep kstop machine from running */
874 pg = ftrace_pages_start;
876 for (i = 0; i < pg->index; i++) {
877 rec = &pg->records[i];
878 if (rec->flags & FTRACE_FL_FAILED)
888 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
890 struct ftrace_iterator *iter;
893 if (unlikely(ftrace_disabled))
896 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
900 mutex_lock(&ftrace_regex_lock);
901 if ((file->f_mode & FMODE_WRITE) &&
902 !(file->f_flags & O_APPEND))
903 ftrace_filter_reset(enable);
905 if (file->f_mode & FMODE_READ) {
906 iter->pg = ftrace_pages_start;
908 iter->flags = enable ? FTRACE_ITER_FILTER :
911 ret = seq_open(file, &show_ftrace_seq_ops);
913 struct seq_file *m = file->private_data;
918 file->private_data = iter;
919 mutex_unlock(&ftrace_regex_lock);
925 ftrace_filter_open(struct inode *inode, struct file *file)
927 return ftrace_regex_open(inode, file, 1);
931 ftrace_notrace_open(struct inode *inode, struct file *file)
933 return ftrace_regex_open(inode, file, 0);
937 ftrace_regex_read(struct file *file, char __user *ubuf,
938 size_t cnt, loff_t *ppos)
940 if (file->f_mode & FMODE_READ)
941 return seq_read(file, ubuf, cnt, ppos);
947 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
951 if (file->f_mode & FMODE_READ)
952 ret = seq_lseek(file, offset, origin);
954 file->f_pos = ret = 1;
967 ftrace_match(unsigned char *buff, int len, int enable)
969 char str[KSYM_SYMBOL_LEN];
971 struct ftrace_page *pg;
972 struct dyn_ftrace *rec;
973 int type = MATCH_FULL;
974 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
975 unsigned i, match = 0, search_len = 0;
977 for (i = 0; i < len; i++) {
978 if (buff[i] == '*') {
980 search = buff + i + 1;
981 type = MATCH_END_ONLY;
982 search_len = len - (i + 1);
984 if (type == MATCH_END_ONLY) {
985 type = MATCH_MIDDLE_ONLY;
988 type = MATCH_FRONT_ONLY;
996 /* keep kstop machine from running */
1000 pg = ftrace_pages_start;
1002 for (i = 0; i < pg->index; i++) {
1006 rec = &pg->records[i];
1007 if (rec->flags & FTRACE_FL_FAILED)
1009 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1012 if (strcmp(str, buff) == 0)
1015 case MATCH_FRONT_ONLY:
1016 if (memcmp(str, buff, match) == 0)
1019 case MATCH_MIDDLE_ONLY:
1020 if (strstr(str, search))
1023 case MATCH_END_ONLY:
1024 ptr = strstr(str, search);
1025 if (ptr && (ptr[search_len] == 0))
1038 ftrace_regex_write(struct file *file, const char __user *ubuf,
1039 size_t cnt, loff_t *ppos, int enable)
1041 struct ftrace_iterator *iter;
1046 if (!cnt || cnt < 0)
1049 mutex_lock(&ftrace_regex_lock);
1051 if (file->f_mode & FMODE_READ) {
1052 struct seq_file *m = file->private_data;
1055 iter = file->private_data;
1058 iter->flags &= ~FTRACE_ITER_CONT;
1059 iter->buffer_idx = 0;
1062 ret = get_user(ch, ubuf++);
1068 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1069 /* skip white space */
1070 while (cnt && isspace(ch)) {
1071 ret = get_user(ch, ubuf++);
1079 file->f_pos += read;
1084 iter->buffer_idx = 0;
1087 while (cnt && !isspace(ch)) {
1088 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1089 iter->buffer[iter->buffer_idx++] = ch;
1094 ret = get_user(ch, ubuf++);
1103 iter->buffer[iter->buffer_idx] = 0;
1104 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1105 iter->buffer_idx = 0;
1107 iter->flags |= FTRACE_ITER_CONT;
1110 file->f_pos += read;
1114 mutex_unlock(&ftrace_regex_lock);
1120 ftrace_filter_write(struct file *file, const char __user *ubuf,
1121 size_t cnt, loff_t *ppos)
1123 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1127 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1128 size_t cnt, loff_t *ppos)
1130 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1134 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1136 if (unlikely(ftrace_disabled))
1139 mutex_lock(&ftrace_regex_lock);
1141 ftrace_filter_reset(enable);
1143 ftrace_match(buf, len, enable);
1144 mutex_unlock(&ftrace_regex_lock);
1148 * ftrace_set_filter - set a function to filter on in ftrace
1149 * @buf - the string that holds the function filter text.
1150 * @len - the length of the string.
1151 * @reset - non zero to reset all filters before applying this filter.
1153 * Filters denote which functions should be enabled when tracing is enabled.
1154 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1156 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1158 ftrace_set_regex(buf, len, reset, 1);
1162 * ftrace_set_notrace - set a function to not trace in ftrace
1163 * @buf - the string that holds the function notrace text.
1164 * @len - the length of the string.
1165 * @reset - non zero to reset all filters before applying this filter.
1167 * Notrace Filters denote which functions should not be enabled when tracing
1168 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1171 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1173 ftrace_set_regex(buf, len, reset, 0);
1177 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1179 struct seq_file *m = (struct seq_file *)file->private_data;
1180 struct ftrace_iterator *iter;
1182 mutex_lock(&ftrace_regex_lock);
1183 if (file->f_mode & FMODE_READ) {
1186 seq_release(inode, file);
1188 iter = file->private_data;
1190 if (iter->buffer_idx) {
1192 iter->buffer[iter->buffer_idx] = 0;
1193 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1196 mutex_lock(&ftrace_sysctl_lock);
1197 mutex_lock(&ftraced_lock);
1198 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1199 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1200 mutex_unlock(&ftraced_lock);
1201 mutex_unlock(&ftrace_sysctl_lock);
1204 mutex_unlock(&ftrace_regex_lock);
1209 ftrace_filter_release(struct inode *inode, struct file *file)
1211 return ftrace_regex_release(inode, file, 1);
1215 ftrace_notrace_release(struct inode *inode, struct file *file)
1217 return ftrace_regex_release(inode, file, 0);
1220 static struct file_operations ftrace_avail_fops = {
1221 .open = ftrace_avail_open,
1223 .llseek = seq_lseek,
1224 .release = ftrace_avail_release,
1227 static struct file_operations ftrace_filter_fops = {
1228 .open = ftrace_filter_open,
1229 .read = ftrace_regex_read,
1230 .write = ftrace_filter_write,
1231 .llseek = ftrace_regex_lseek,
1232 .release = ftrace_filter_release,
1235 static struct file_operations ftrace_notrace_fops = {
1236 .open = ftrace_notrace_open,
1237 .read = ftrace_regex_read,
1238 .write = ftrace_notrace_write,
1239 .llseek = ftrace_regex_lseek,
1240 .release = ftrace_notrace_release,
1244 * ftrace_force_update - force an update to all recording ftrace functions
1246 * The ftrace dynamic update daemon only wakes up once a second.
1247 * There may be cases where an update needs to be done immediately
1248 * for tests or internal kernel tracing to begin. This function
1249 * wakes the daemon to do an update and will not return until the
1250 * update is complete.
1252 int ftrace_force_update(void)
1254 unsigned long last_counter;
1255 DECLARE_WAITQUEUE(wait, current);
1258 if (unlikely(ftrace_disabled))
1261 mutex_lock(&ftraced_lock);
1262 last_counter = ftraced_iteration_counter;
1264 set_current_state(TASK_INTERRUPTIBLE);
1265 add_wait_queue(&ftraced_waiters, &wait);
1267 if (unlikely(!ftraced_task)) {
1273 mutex_unlock(&ftraced_lock);
1274 wake_up_process(ftraced_task);
1276 mutex_lock(&ftraced_lock);
1277 if (signal_pending(current)) {
1281 set_current_state(TASK_INTERRUPTIBLE);
1282 } while (last_counter == ftraced_iteration_counter);
1285 mutex_unlock(&ftraced_lock);
1286 remove_wait_queue(&ftraced_waiters, &wait);
1287 set_current_state(TASK_RUNNING);
1292 static void ftrace_force_shutdown(void)
1294 struct task_struct *task;
1295 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1297 mutex_lock(&ftraced_lock);
1298 task = ftraced_task;
1299 ftraced_task = NULL;
1300 ftraced_suspend = -1;
1301 ftrace_run_update_code(command);
1302 mutex_unlock(&ftraced_lock);
1308 static __init int ftrace_init_debugfs(void)
1310 struct dentry *d_tracer;
1311 struct dentry *entry;
1313 d_tracer = tracing_init_dentry();
1315 entry = debugfs_create_file("available_filter_functions", 0444,
1316 d_tracer, NULL, &ftrace_avail_fops);
1318 pr_warning("Could not create debugfs "
1319 "'available_filter_functions' entry\n");
1321 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1322 NULL, &ftrace_filter_fops);
1324 pr_warning("Could not create debugfs "
1325 "'set_ftrace_filter' entry\n");
1327 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1328 NULL, &ftrace_notrace_fops);
1330 pr_warning("Could not create debugfs "
1331 "'set_ftrace_notrace' entry\n");
1335 fs_initcall(ftrace_init_debugfs);
1337 static int __init ftrace_dynamic_init(void)
1339 struct task_struct *p;
1343 addr = (unsigned long)ftrace_record_ip;
1345 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1347 /* ftrace_dyn_arch_init places the return code in addr */
1353 ret = ftrace_dyn_table_alloc();
1357 p = kthread_run(ftraced, NULL, "ftraced");
1363 last_ftrace_enabled = ftrace_enabled = 1;
1369 ftrace_disabled = 1;
1373 core_initcall(ftrace_dynamic_init);
1375 # define ftrace_startup() do { } while (0)
1376 # define ftrace_shutdown() do { } while (0)
1377 # define ftrace_startup_sysctl() do { } while (0)
1378 # define ftrace_shutdown_sysctl() do { } while (0)
1379 # define ftrace_force_shutdown() do { } while (0)
1380 #endif /* CONFIG_DYNAMIC_FTRACE */
1383 * ftrace_kill - totally shutdown ftrace
1385 * This is a safety measure. If something was detected that seems
1386 * wrong, calling this function will keep ftrace from doing
1387 * any more modifications, and updates.
1388 * used when something went wrong.
1390 void ftrace_kill(void)
1392 mutex_lock(&ftrace_sysctl_lock);
1393 ftrace_disabled = 1;
1396 clear_ftrace_function();
1397 mutex_unlock(&ftrace_sysctl_lock);
1399 /* Try to totally disable ftrace */
1400 ftrace_force_shutdown();
1404 * register_ftrace_function - register a function for profiling
1405 * @ops - ops structure that holds the function for profiling.
1407 * Register a function to be called by all functions in the
1410 * Note: @ops->func and all the functions it calls must be labeled
1411 * with "notrace", otherwise it will go into a
1414 int register_ftrace_function(struct ftrace_ops *ops)
1418 if (unlikely(ftrace_disabled))
1421 mutex_lock(&ftrace_sysctl_lock);
1422 ret = __register_ftrace_function(ops);
1424 mutex_unlock(&ftrace_sysctl_lock);
1430 * unregister_ftrace_function - unresgister a function for profiling.
1431 * @ops - ops structure that holds the function to unregister
1433 * Unregister a function that was added to be called by ftrace profiling.
1435 int unregister_ftrace_function(struct ftrace_ops *ops)
1439 mutex_lock(&ftrace_sysctl_lock);
1440 ret = __unregister_ftrace_function(ops);
1442 mutex_unlock(&ftrace_sysctl_lock);
1448 ftrace_enable_sysctl(struct ctl_table *table, int write,
1449 struct file *file, void __user *buffer, size_t *lenp,
1454 if (unlikely(ftrace_disabled))
1457 mutex_lock(&ftrace_sysctl_lock);
1459 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1461 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1464 last_ftrace_enabled = ftrace_enabled;
1466 if (ftrace_enabled) {
1468 ftrace_startup_sysctl();
1470 /* we are starting ftrace again */
1471 if (ftrace_list != &ftrace_list_end) {
1472 if (ftrace_list->next == &ftrace_list_end)
1473 ftrace_trace_function = ftrace_list->func;
1475 ftrace_trace_function = ftrace_list_func;
1479 /* stopping ftrace calls (just send to ftrace_stub) */
1480 ftrace_trace_function = ftrace_stub;
1482 ftrace_shutdown_sysctl();
1486 mutex_unlock(&ftrace_sysctl_lock);