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/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <asm/ftrace.h>
36 #define FTRACE_WARN_ON(cond) \
42 #define FTRACE_WARN_ON_ONCE(cond) \
44 if (WARN_ON_ONCE(cond)) \
48 /* hash bits for specific function selection */
49 #define FTRACE_HASH_BITS 7
50 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
52 /* ftrace_enabled is a method to turn ftrace on or off */
53 int ftrace_enabled __read_mostly;
54 static int last_ftrace_enabled;
56 /* Quick disabling of function tracer. */
57 int function_trace_stop;
60 * ftrace_disabled is set when an anomaly is discovered.
61 * ftrace_disabled is much stronger than ftrace_enabled.
63 static int ftrace_disabled __read_mostly;
65 static DEFINE_MUTEX(ftrace_lock);
67 static struct ftrace_ops ftrace_list_end __read_mostly =
72 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
73 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
74 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
75 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
77 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
79 struct ftrace_ops *op = ftrace_list;
81 /* in case someone actually ports this to alpha! */
82 read_barrier_depends();
84 while (op != &ftrace_list_end) {
86 read_barrier_depends();
87 op->func(ip, parent_ip);
92 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
94 if (!test_tsk_trace_trace(current))
97 ftrace_pid_function(ip, parent_ip);
100 static void set_ftrace_pid_function(ftrace_func_t func)
102 /* do not set ftrace_pid_function to itself! */
103 if (func != ftrace_pid_func)
104 ftrace_pid_function = func;
108 * clear_ftrace_function - reset the ftrace function
110 * This NULLs the ftrace function and in essence stops
111 * tracing. There may be lag
113 void clear_ftrace_function(void)
115 ftrace_trace_function = ftrace_stub;
116 __ftrace_trace_function = ftrace_stub;
117 ftrace_pid_function = ftrace_stub;
120 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
122 * For those archs that do not test ftrace_trace_stop in their
123 * mcount call site, we need to do it from C.
125 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
127 if (function_trace_stop)
130 __ftrace_trace_function(ip, parent_ip);
134 static int __register_ftrace_function(struct ftrace_ops *ops)
136 ops->next = ftrace_list;
138 * We are entering ops into the ftrace_list but another
139 * CPU might be walking that list. We need to make sure
140 * the ops->next pointer is valid before another CPU sees
141 * the ops pointer included into the ftrace_list.
146 if (ftrace_enabled) {
149 if (ops->next == &ftrace_list_end)
152 func = ftrace_list_func;
154 if (ftrace_pid_trace) {
155 set_ftrace_pid_function(func);
156 func = ftrace_pid_func;
160 * For one func, simply call it directly.
161 * For more than one func, call the chain.
163 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
164 ftrace_trace_function = func;
166 __ftrace_trace_function = func;
167 ftrace_trace_function = ftrace_test_stop_func;
174 static int __unregister_ftrace_function(struct ftrace_ops *ops)
176 struct ftrace_ops **p;
179 * If we are removing the last function, then simply point
180 * to the ftrace_stub.
182 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
183 ftrace_trace_function = ftrace_stub;
184 ftrace_list = &ftrace_list_end;
188 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
197 if (ftrace_enabled) {
198 /* If we only have one func left, then call that directly */
199 if (ftrace_list->next == &ftrace_list_end) {
200 ftrace_func_t func = ftrace_list->func;
202 if (ftrace_pid_trace) {
203 set_ftrace_pid_function(func);
204 func = ftrace_pid_func;
206 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
207 ftrace_trace_function = func;
209 __ftrace_trace_function = func;
217 static void ftrace_update_pid_func(void)
221 if (ftrace_trace_function == ftrace_stub)
224 func = ftrace_trace_function;
226 if (ftrace_pid_trace) {
227 set_ftrace_pid_function(func);
228 func = ftrace_pid_func;
230 if (func == ftrace_pid_func)
231 func = ftrace_pid_function;
234 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
235 ftrace_trace_function = func;
237 __ftrace_trace_function = func;
241 /* set when tracing only a pid */
242 struct pid *ftrace_pid_trace;
243 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
245 #ifdef CONFIG_DYNAMIC_FTRACE
247 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
248 # error Dynamic ftrace depends on MCOUNT_RECORD
251 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
253 struct ftrace_func_probe {
254 struct hlist_node node;
255 struct ftrace_probe_ops *ops;
264 FTRACE_ENABLE_CALLS = (1 << 0),
265 FTRACE_DISABLE_CALLS = (1 << 1),
266 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
267 FTRACE_ENABLE_MCOUNT = (1 << 3),
268 FTRACE_DISABLE_MCOUNT = (1 << 4),
269 FTRACE_START_FUNC_RET = (1 << 5),
270 FTRACE_STOP_FUNC_RET = (1 << 6),
273 static int ftrace_filtered;
275 static LIST_HEAD(ftrace_new_addrs);
277 static DEFINE_MUTEX(ftrace_regex_lock);
280 struct ftrace_page *next;
282 struct dyn_ftrace records[];
285 #define ENTRIES_PER_PAGE \
286 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
288 /* estimate from running different kernels */
289 #define NR_TO_INIT 10000
291 static struct ftrace_page *ftrace_pages_start;
292 static struct ftrace_page *ftrace_pages;
294 static struct dyn_ftrace *ftrace_free_records;
297 * This is a double for. Do not use 'break' to break out of the loop,
298 * you must use a goto.
300 #define do_for_each_ftrace_rec(pg, rec) \
301 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
303 for (_____i = 0; _____i < pg->index; _____i++) { \
304 rec = &pg->records[_____i];
306 #define while_for_each_ftrace_rec() \
310 #ifdef CONFIG_KPROBES
312 static int frozen_record_count;
314 static inline void freeze_record(struct dyn_ftrace *rec)
316 if (!(rec->flags & FTRACE_FL_FROZEN)) {
317 rec->flags |= FTRACE_FL_FROZEN;
318 frozen_record_count++;
322 static inline void unfreeze_record(struct dyn_ftrace *rec)
324 if (rec->flags & FTRACE_FL_FROZEN) {
325 rec->flags &= ~FTRACE_FL_FROZEN;
326 frozen_record_count--;
330 static inline int record_frozen(struct dyn_ftrace *rec)
332 return rec->flags & FTRACE_FL_FROZEN;
335 # define freeze_record(rec) ({ 0; })
336 # define unfreeze_record(rec) ({ 0; })
337 # define record_frozen(rec) ({ 0; })
338 #endif /* CONFIG_KPROBES */
340 static void ftrace_free_rec(struct dyn_ftrace *rec)
342 rec->ip = (unsigned long)ftrace_free_records;
343 ftrace_free_records = rec;
344 rec->flags |= FTRACE_FL_FREE;
347 void ftrace_release(void *start, unsigned long size)
349 struct dyn_ftrace *rec;
350 struct ftrace_page *pg;
351 unsigned long s = (unsigned long)start;
352 unsigned long e = s + size;
354 if (ftrace_disabled || !start)
357 mutex_lock(&ftrace_lock);
358 do_for_each_ftrace_rec(pg, rec) {
359 if ((rec->ip >= s) && (rec->ip < e))
360 ftrace_free_rec(rec);
361 } while_for_each_ftrace_rec();
362 mutex_unlock(&ftrace_lock);
365 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
367 struct dyn_ftrace *rec;
369 /* First check for freed records */
370 if (ftrace_free_records) {
371 rec = ftrace_free_records;
373 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
374 FTRACE_WARN_ON_ONCE(1);
375 ftrace_free_records = NULL;
379 ftrace_free_records = (void *)rec->ip;
380 memset(rec, 0, sizeof(*rec));
384 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
385 if (!ftrace_pages->next) {
386 /* allocate another page */
388 (void *)get_zeroed_page(GFP_KERNEL);
389 if (!ftrace_pages->next)
392 ftrace_pages = ftrace_pages->next;
395 return &ftrace_pages->records[ftrace_pages->index++];
398 static struct dyn_ftrace *
399 ftrace_record_ip(unsigned long ip)
401 struct dyn_ftrace *rec;
406 rec = ftrace_alloc_dyn_node(ip);
412 list_add(&rec->list, &ftrace_new_addrs);
417 static void print_ip_ins(const char *fmt, unsigned char *p)
421 printk(KERN_CONT "%s", fmt);
423 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
424 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
427 static void ftrace_bug(int failed, unsigned long ip)
431 FTRACE_WARN_ON_ONCE(1);
432 pr_info("ftrace faulted on modifying ");
436 FTRACE_WARN_ON_ONCE(1);
437 pr_info("ftrace failed to modify ");
439 print_ip_ins(" actual: ", (unsigned char *)ip);
440 printk(KERN_CONT "\n");
443 FTRACE_WARN_ON_ONCE(1);
444 pr_info("ftrace faulted on writing ");
448 FTRACE_WARN_ON_ONCE(1);
449 pr_info("ftrace faulted on unknown error ");
456 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
458 unsigned long ftrace_addr;
459 unsigned long ip, fl;
461 ftrace_addr = (unsigned long)FTRACE_ADDR;
466 * If this record is not to be traced and
467 * it is not enabled then do nothing.
469 * If this record is not to be traced and
470 * it is enabled then disable it.
473 if (rec->flags & FTRACE_FL_NOTRACE) {
474 if (rec->flags & FTRACE_FL_ENABLED)
475 rec->flags &= ~FTRACE_FL_ENABLED;
479 } else if (ftrace_filtered && enable) {
484 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
486 /* Record is filtered and enabled, do nothing */
487 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
490 /* Record is not filtered or enabled, do nothing */
494 /* Record is not filtered but enabled, disable it */
495 if (fl == FTRACE_FL_ENABLED)
496 rec->flags &= ~FTRACE_FL_ENABLED;
498 /* Otherwise record is filtered but not enabled, enable it */
499 rec->flags |= FTRACE_FL_ENABLED;
501 /* Disable or not filtered */
504 /* if record is enabled, do nothing */
505 if (rec->flags & FTRACE_FL_ENABLED)
508 rec->flags |= FTRACE_FL_ENABLED;
512 /* if record is not enabled, do nothing */
513 if (!(rec->flags & FTRACE_FL_ENABLED))
516 rec->flags &= ~FTRACE_FL_ENABLED;
520 if (rec->flags & FTRACE_FL_ENABLED)
521 return ftrace_make_call(rec, ftrace_addr);
523 return ftrace_make_nop(NULL, rec, ftrace_addr);
526 static void ftrace_replace_code(int enable)
528 struct dyn_ftrace *rec;
529 struct ftrace_page *pg;
532 do_for_each_ftrace_rec(pg, rec) {
534 * Skip over free records and records that have
537 if (rec->flags & FTRACE_FL_FREE ||
538 rec->flags & FTRACE_FL_FAILED)
541 /* ignore updates to this record's mcount site */
542 if (get_kprobe((void *)rec->ip)) {
546 unfreeze_record(rec);
549 failed = __ftrace_replace_code(rec, enable);
550 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
551 rec->flags |= FTRACE_FL_FAILED;
552 if ((system_state == SYSTEM_BOOTING) ||
553 !core_kernel_text(rec->ip)) {
554 ftrace_free_rec(rec);
556 ftrace_bug(failed, rec->ip);
557 /* Stop processing */
561 } while_for_each_ftrace_rec();
565 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
572 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
575 rec->flags |= FTRACE_FL_FAILED;
582 * archs can override this function if they must do something
583 * before the modifying code is performed.
585 int __weak ftrace_arch_code_modify_prepare(void)
591 * archs can override this function if they must do something
592 * after the modifying code is performed.
594 int __weak ftrace_arch_code_modify_post_process(void)
599 static int __ftrace_modify_code(void *data)
603 if (*command & FTRACE_ENABLE_CALLS)
604 ftrace_replace_code(1);
605 else if (*command & FTRACE_DISABLE_CALLS)
606 ftrace_replace_code(0);
608 if (*command & FTRACE_UPDATE_TRACE_FUNC)
609 ftrace_update_ftrace_func(ftrace_trace_function);
611 if (*command & FTRACE_START_FUNC_RET)
612 ftrace_enable_ftrace_graph_caller();
613 else if (*command & FTRACE_STOP_FUNC_RET)
614 ftrace_disable_ftrace_graph_caller();
619 static void ftrace_run_update_code(int command)
623 ret = ftrace_arch_code_modify_prepare();
628 stop_machine(__ftrace_modify_code, &command, NULL);
630 ret = ftrace_arch_code_modify_post_process();
634 static ftrace_func_t saved_ftrace_func;
635 static int ftrace_start_up;
637 static void ftrace_startup_enable(int command)
639 if (saved_ftrace_func != ftrace_trace_function) {
640 saved_ftrace_func = ftrace_trace_function;
641 command |= FTRACE_UPDATE_TRACE_FUNC;
644 if (!command || !ftrace_enabled)
647 ftrace_run_update_code(command);
650 static void ftrace_startup(int command)
652 if (unlikely(ftrace_disabled))
656 command |= FTRACE_ENABLE_CALLS;
658 ftrace_startup_enable(command);
661 static void ftrace_shutdown(int command)
663 if (unlikely(ftrace_disabled))
667 if (!ftrace_start_up)
668 command |= FTRACE_DISABLE_CALLS;
670 if (saved_ftrace_func != ftrace_trace_function) {
671 saved_ftrace_func = ftrace_trace_function;
672 command |= FTRACE_UPDATE_TRACE_FUNC;
675 if (!command || !ftrace_enabled)
678 ftrace_run_update_code(command);
681 static void ftrace_startup_sysctl(void)
683 int command = FTRACE_ENABLE_MCOUNT;
685 if (unlikely(ftrace_disabled))
688 /* Force update next time */
689 saved_ftrace_func = NULL;
690 /* ftrace_start_up is true if we want ftrace running */
692 command |= FTRACE_ENABLE_CALLS;
694 ftrace_run_update_code(command);
697 static void ftrace_shutdown_sysctl(void)
699 int command = FTRACE_DISABLE_MCOUNT;
701 if (unlikely(ftrace_disabled))
704 /* ftrace_start_up is true if ftrace is running */
706 command |= FTRACE_DISABLE_CALLS;
708 ftrace_run_update_code(command);
711 static cycle_t ftrace_update_time;
712 static unsigned long ftrace_update_cnt;
713 unsigned long ftrace_update_tot_cnt;
715 static int ftrace_update_code(struct module *mod)
717 struct dyn_ftrace *p, *t;
720 start = ftrace_now(raw_smp_processor_id());
721 ftrace_update_cnt = 0;
723 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
725 /* If something went wrong, bail without enabling anything */
726 if (unlikely(ftrace_disabled))
729 list_del_init(&p->list);
731 /* convert record (i.e, patch mcount-call with NOP) */
732 if (ftrace_code_disable(mod, p)) {
733 p->flags |= FTRACE_FL_CONVERTED;
739 stop = ftrace_now(raw_smp_processor_id());
740 ftrace_update_time = stop - start;
741 ftrace_update_tot_cnt += ftrace_update_cnt;
746 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
748 struct ftrace_page *pg;
752 /* allocate a few pages */
753 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
754 if (!ftrace_pages_start)
758 * Allocate a few more pages.
760 * TODO: have some parser search vmlinux before
761 * final linking to find all calls to ftrace.
763 * a) know how many pages to allocate.
765 * b) set up the table then.
767 * The dynamic code is still necessary for
771 pg = ftrace_pages = ftrace_pages_start;
773 cnt = num_to_init / ENTRIES_PER_PAGE;
774 pr_info("ftrace: allocating %ld entries in %d pages\n",
775 num_to_init, cnt + 1);
777 for (i = 0; i < cnt; i++) {
778 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
780 /* If we fail, we'll try later anyway */
791 FTRACE_ITER_FILTER = (1 << 0),
792 FTRACE_ITER_CONT = (1 << 1),
793 FTRACE_ITER_NOTRACE = (1 << 2),
794 FTRACE_ITER_FAILURES = (1 << 3),
795 FTRACE_ITER_PRINTALL = (1 << 4),
796 FTRACE_ITER_HASH = (1 << 5),
799 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
801 struct ftrace_iterator {
802 struct ftrace_page *pg;
806 unsigned char buffer[FTRACE_BUFF_MAX+1];
812 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
814 struct ftrace_iterator *iter = m->private;
815 struct hlist_node *hnd = v;
816 struct hlist_head *hhd;
818 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
823 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
826 hhd = &ftrace_func_hash[iter->hidx];
828 if (hlist_empty(hhd)) {
847 static void *t_hash_start(struct seq_file *m, loff_t *pos)
849 struct ftrace_iterator *iter = m->private;
852 iter->flags |= FTRACE_ITER_HASH;
854 return t_hash_next(m, p, pos);
857 static int t_hash_show(struct seq_file *m, void *v)
859 struct ftrace_func_probe *rec;
860 struct hlist_node *hnd = v;
861 char str[KSYM_SYMBOL_LEN];
863 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
866 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
868 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
869 seq_printf(m, "%s:", str);
871 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
872 seq_printf(m, "%s", str);
875 seq_printf(m, ":%p", rec->data);
882 t_next(struct seq_file *m, void *v, loff_t *pos)
884 struct ftrace_iterator *iter = m->private;
885 struct dyn_ftrace *rec = NULL;
887 if (iter->flags & FTRACE_ITER_HASH)
888 return t_hash_next(m, v, pos);
892 if (iter->flags & FTRACE_ITER_PRINTALL)
896 if (iter->idx >= iter->pg->index) {
897 if (iter->pg->next) {
898 iter->pg = iter->pg->next;
905 rec = &iter->pg->records[iter->idx++];
906 if ((rec->flags & FTRACE_FL_FREE) ||
908 (!(iter->flags & FTRACE_ITER_FAILURES) &&
909 (rec->flags & FTRACE_FL_FAILED)) ||
911 ((iter->flags & FTRACE_ITER_FAILURES) &&
912 !(rec->flags & FTRACE_FL_FAILED)) ||
914 ((iter->flags & FTRACE_ITER_FILTER) &&
915 !(rec->flags & FTRACE_FL_FILTER)) ||
917 ((iter->flags & FTRACE_ITER_NOTRACE) &&
918 !(rec->flags & FTRACE_FL_NOTRACE))) {
927 static void *t_start(struct seq_file *m, loff_t *pos)
929 struct ftrace_iterator *iter = m->private;
932 mutex_lock(&ftrace_lock);
934 * For set_ftrace_filter reading, if we have the filter
935 * off, we can short cut and just print out that all
936 * functions are enabled.
938 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
940 return t_hash_start(m, pos);
941 iter->flags |= FTRACE_ITER_PRINTALL;
946 if (iter->flags & FTRACE_ITER_HASH)
947 return t_hash_start(m, pos);
956 p = t_next(m, p, pos);
959 return t_hash_start(m, pos);
964 static void t_stop(struct seq_file *m, void *p)
966 mutex_unlock(&ftrace_lock);
969 static int t_show(struct seq_file *m, void *v)
971 struct ftrace_iterator *iter = m->private;
972 struct dyn_ftrace *rec = v;
973 char str[KSYM_SYMBOL_LEN];
975 if (iter->flags & FTRACE_ITER_HASH)
976 return t_hash_show(m, v);
978 if (iter->flags & FTRACE_ITER_PRINTALL) {
979 seq_printf(m, "#### all functions enabled ####\n");
986 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
988 seq_printf(m, "%s\n", str);
993 static struct seq_operations show_ftrace_seq_ops = {
1001 ftrace_avail_open(struct inode *inode, struct file *file)
1003 struct ftrace_iterator *iter;
1006 if (unlikely(ftrace_disabled))
1009 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1013 iter->pg = ftrace_pages_start;
1015 ret = seq_open(file, &show_ftrace_seq_ops);
1017 struct seq_file *m = file->private_data;
1027 int ftrace_avail_release(struct inode *inode, struct file *file)
1029 struct seq_file *m = (struct seq_file *)file->private_data;
1030 struct ftrace_iterator *iter = m->private;
1032 seq_release(inode, file);
1039 ftrace_failures_open(struct inode *inode, struct file *file)
1043 struct ftrace_iterator *iter;
1045 ret = ftrace_avail_open(inode, file);
1047 m = (struct seq_file *)file->private_data;
1048 iter = (struct ftrace_iterator *)m->private;
1049 iter->flags = FTRACE_ITER_FAILURES;
1056 static void ftrace_filter_reset(int enable)
1058 struct ftrace_page *pg;
1059 struct dyn_ftrace *rec;
1060 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1062 mutex_lock(&ftrace_lock);
1064 ftrace_filtered = 0;
1065 do_for_each_ftrace_rec(pg, rec) {
1066 if (rec->flags & FTRACE_FL_FAILED)
1068 rec->flags &= ~type;
1069 } while_for_each_ftrace_rec();
1070 mutex_unlock(&ftrace_lock);
1074 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1076 struct ftrace_iterator *iter;
1079 if (unlikely(ftrace_disabled))
1082 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1086 mutex_lock(&ftrace_regex_lock);
1087 if ((file->f_mode & FMODE_WRITE) &&
1088 !(file->f_flags & O_APPEND))
1089 ftrace_filter_reset(enable);
1091 if (file->f_mode & FMODE_READ) {
1092 iter->pg = ftrace_pages_start;
1093 iter->flags = enable ? FTRACE_ITER_FILTER :
1094 FTRACE_ITER_NOTRACE;
1096 ret = seq_open(file, &show_ftrace_seq_ops);
1098 struct seq_file *m = file->private_data;
1103 file->private_data = iter;
1104 mutex_unlock(&ftrace_regex_lock);
1110 ftrace_filter_open(struct inode *inode, struct file *file)
1112 return ftrace_regex_open(inode, file, 1);
1116 ftrace_notrace_open(struct inode *inode, struct file *file)
1118 return ftrace_regex_open(inode, file, 0);
1122 ftrace_regex_read(struct file *file, char __user *ubuf,
1123 size_t cnt, loff_t *ppos)
1125 if (file->f_mode & FMODE_READ)
1126 return seq_read(file, ubuf, cnt, ppos);
1132 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1136 if (file->f_mode & FMODE_READ)
1137 ret = seq_lseek(file, offset, origin);
1139 file->f_pos = ret = 1;
1152 * (static function - no need for kernel doc)
1154 * Pass in a buffer containing a glob and this function will
1155 * set search to point to the search part of the buffer and
1156 * return the type of search it is (see enum above).
1157 * This does modify buff.
1159 * Returns enum type.
1160 * search returns the pointer to use for comparison.
1161 * not returns 1 if buff started with a '!'
1165 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1167 int type = MATCH_FULL;
1170 if (buff[0] == '!') {
1179 for (i = 0; i < len; i++) {
1180 if (buff[i] == '*') {
1183 type = MATCH_END_ONLY;
1185 if (type == MATCH_END_ONLY)
1186 type = MATCH_MIDDLE_ONLY;
1188 type = MATCH_FRONT_ONLY;
1198 static int ftrace_match(char *str, char *regex, int len, int type)
1205 if (strcmp(str, regex) == 0)
1208 case MATCH_FRONT_ONLY:
1209 if (strncmp(str, regex, len) == 0)
1212 case MATCH_MIDDLE_ONLY:
1213 if (strstr(str, regex))
1216 case MATCH_END_ONLY:
1217 ptr = strstr(str, regex);
1218 if (ptr && (ptr[len] == 0))
1227 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1229 char str[KSYM_SYMBOL_LEN];
1231 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1232 return ftrace_match(str, regex, len, type);
1235 static void ftrace_match_records(char *buff, int len, int enable)
1237 unsigned int search_len;
1238 struct ftrace_page *pg;
1239 struct dyn_ftrace *rec;
1245 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1246 type = ftrace_setup_glob(buff, len, &search, ¬);
1248 search_len = strlen(search);
1250 mutex_lock(&ftrace_lock);
1251 do_for_each_ftrace_rec(pg, rec) {
1253 if (rec->flags & FTRACE_FL_FAILED)
1256 if (ftrace_match_record(rec, search, search_len, type)) {
1258 rec->flags &= ~flag;
1263 * Only enable filtering if we have a function that
1266 if (enable && (rec->flags & FTRACE_FL_FILTER))
1267 ftrace_filtered = 1;
1268 } while_for_each_ftrace_rec();
1269 mutex_unlock(&ftrace_lock);
1273 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1274 char *regex, int len, int type)
1276 char str[KSYM_SYMBOL_LEN];
1279 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1281 if (!modname || strcmp(modname, mod))
1284 /* blank search means to match all funcs in the mod */
1286 return ftrace_match(str, regex, len, type);
1291 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1293 unsigned search_len = 0;
1294 struct ftrace_page *pg;
1295 struct dyn_ftrace *rec;
1296 int type = MATCH_FULL;
1297 char *search = buff;
1301 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1303 /* blank or '*' mean the same */
1304 if (strcmp(buff, "*") == 0)
1307 /* handle the case of 'dont filter this module' */
1308 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1314 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1315 search_len = strlen(search);
1318 mutex_lock(&ftrace_lock);
1319 do_for_each_ftrace_rec(pg, rec) {
1321 if (rec->flags & FTRACE_FL_FAILED)
1324 if (ftrace_match_module_record(rec, mod,
1325 search, search_len, type)) {
1327 rec->flags &= ~flag;
1331 if (enable && (rec->flags & FTRACE_FL_FILTER))
1332 ftrace_filtered = 1;
1334 } while_for_each_ftrace_rec();
1335 mutex_unlock(&ftrace_lock);
1339 * We register the module command as a template to show others how
1340 * to register the a command as well.
1344 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1349 * cmd == 'mod' because we only registered this func
1350 * for the 'mod' ftrace_func_command.
1351 * But if you register one func with multiple commands,
1352 * you can tell which command was used by the cmd
1356 /* we must have a module name */
1360 mod = strsep(¶m, ":");
1364 ftrace_match_module_records(func, mod, enable);
1368 static struct ftrace_func_command ftrace_mod_cmd = {
1370 .func = ftrace_mod_callback,
1373 static int __init ftrace_mod_cmd_init(void)
1375 return register_ftrace_command(&ftrace_mod_cmd);
1377 device_initcall(ftrace_mod_cmd_init);
1380 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1382 struct ftrace_func_probe *entry;
1383 struct hlist_head *hhd;
1384 struct hlist_node *n;
1388 key = hash_long(ip, FTRACE_HASH_BITS);
1390 hhd = &ftrace_func_hash[key];
1392 if (hlist_empty(hhd))
1396 * Disable preemption for these calls to prevent a RCU grace
1397 * period. This syncs the hash iteration and freeing of items
1398 * on the hash. rcu_read_lock is too dangerous here.
1400 resched = ftrace_preempt_disable();
1401 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1402 if (entry->ip == ip)
1403 entry->ops->func(ip, parent_ip, &entry->data);
1405 ftrace_preempt_enable(resched);
1408 static struct ftrace_ops trace_probe_ops __read_mostly =
1410 .func = function_trace_probe_call,
1413 static int ftrace_probe_registered;
1415 static void __enable_ftrace_function_probe(void)
1419 if (ftrace_probe_registered)
1422 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1423 struct hlist_head *hhd = &ftrace_func_hash[i];
1427 /* Nothing registered? */
1428 if (i == FTRACE_FUNC_HASHSIZE)
1431 __register_ftrace_function(&trace_probe_ops);
1433 ftrace_probe_registered = 1;
1436 static void __disable_ftrace_function_probe(void)
1440 if (!ftrace_probe_registered)
1443 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1444 struct hlist_head *hhd = &ftrace_func_hash[i];
1449 /* no more funcs left */
1450 __unregister_ftrace_function(&trace_probe_ops);
1452 ftrace_probe_registered = 0;
1456 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1458 struct ftrace_func_probe *entry =
1459 container_of(rhp, struct ftrace_func_probe, rcu);
1461 if (entry->ops->free)
1462 entry->ops->free(&entry->data);
1468 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1471 struct ftrace_func_probe *entry;
1472 struct ftrace_page *pg;
1473 struct dyn_ftrace *rec;
1479 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1480 len = strlen(search);
1482 /* we do not support '!' for function probes */
1486 mutex_lock(&ftrace_lock);
1487 do_for_each_ftrace_rec(pg, rec) {
1489 if (rec->flags & FTRACE_FL_FAILED)
1492 if (!ftrace_match_record(rec, search, len, type))
1495 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1497 /* If we did not process any, then return error */
1508 * The caller might want to do something special
1509 * for each function we find. We call the callback
1510 * to give the caller an opportunity to do so.
1512 if (ops->callback) {
1513 if (ops->callback(rec->ip, &entry->data) < 0) {
1514 /* caller does not like this func */
1521 entry->ip = rec->ip;
1523 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1524 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1526 } while_for_each_ftrace_rec();
1527 __enable_ftrace_function_probe();
1530 mutex_unlock(&ftrace_lock);
1536 PROBE_TEST_FUNC = 1,
1541 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1542 void *data, int flags)
1544 struct ftrace_func_probe *entry;
1545 struct hlist_node *n, *tmp;
1546 char str[KSYM_SYMBOL_LEN];
1547 int type = MATCH_FULL;
1551 if (glob && (strcmp(glob, "*") || !strlen(glob)))
1556 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1557 len = strlen(search);
1559 /* we do not support '!' for function probes */
1564 mutex_lock(&ftrace_lock);
1565 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1566 struct hlist_head *hhd = &ftrace_func_hash[i];
1568 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1570 /* break up if statements for readability */
1571 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
1574 if ((flags & PROBE_TEST_DATA) && entry->data != data)
1577 /* do this last, since it is the most expensive */
1579 kallsyms_lookup(entry->ip, NULL, NULL,
1581 if (!ftrace_match(str, glob, len, type))
1585 hlist_del(&entry->node);
1586 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1589 __disable_ftrace_function_probe();
1590 mutex_unlock(&ftrace_lock);
1594 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1597 __unregister_ftrace_function_probe(glob, ops, data,
1598 PROBE_TEST_FUNC | PROBE_TEST_DATA);
1602 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
1604 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
1607 void unregister_ftrace_function_probe_all(char *glob)
1609 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
1612 static LIST_HEAD(ftrace_commands);
1613 static DEFINE_MUTEX(ftrace_cmd_mutex);
1615 int register_ftrace_command(struct ftrace_func_command *cmd)
1617 struct ftrace_func_command *p;
1620 mutex_lock(&ftrace_cmd_mutex);
1621 list_for_each_entry(p, &ftrace_commands, list) {
1622 if (strcmp(cmd->name, p->name) == 0) {
1627 list_add(&cmd->list, &ftrace_commands);
1629 mutex_unlock(&ftrace_cmd_mutex);
1634 int unregister_ftrace_command(struct ftrace_func_command *cmd)
1636 struct ftrace_func_command *p, *n;
1639 mutex_lock(&ftrace_cmd_mutex);
1640 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1641 if (strcmp(cmd->name, p->name) == 0) {
1643 list_del_init(&p->list);
1648 mutex_unlock(&ftrace_cmd_mutex);
1653 static int ftrace_process_regex(char *buff, int len, int enable)
1655 char *func, *command, *next = buff;
1656 struct ftrace_func_command *p;
1659 func = strsep(&next, ":");
1662 ftrace_match_records(func, len, enable);
1668 command = strsep(&next, ":");
1670 mutex_lock(&ftrace_cmd_mutex);
1671 list_for_each_entry(p, &ftrace_commands, list) {
1672 if (strcmp(p->name, command) == 0) {
1673 ret = p->func(func, command, next, enable);
1678 mutex_unlock(&ftrace_cmd_mutex);
1684 ftrace_regex_write(struct file *file, const char __user *ubuf,
1685 size_t cnt, loff_t *ppos, int enable)
1687 struct ftrace_iterator *iter;
1692 if (!cnt || cnt < 0)
1695 mutex_lock(&ftrace_regex_lock);
1697 if (file->f_mode & FMODE_READ) {
1698 struct seq_file *m = file->private_data;
1701 iter = file->private_data;
1704 iter->flags &= ~FTRACE_ITER_CONT;
1705 iter->buffer_idx = 0;
1708 ret = get_user(ch, ubuf++);
1714 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1715 /* skip white space */
1716 while (cnt && isspace(ch)) {
1717 ret = get_user(ch, ubuf++);
1725 file->f_pos += read;
1730 iter->buffer_idx = 0;
1733 while (cnt && !isspace(ch)) {
1734 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1735 iter->buffer[iter->buffer_idx++] = ch;
1740 ret = get_user(ch, ubuf++);
1749 iter->buffer[iter->buffer_idx] = 0;
1750 ret = ftrace_process_regex(iter->buffer,
1751 iter->buffer_idx, enable);
1754 iter->buffer_idx = 0;
1756 iter->flags |= FTRACE_ITER_CONT;
1759 file->f_pos += read;
1763 mutex_unlock(&ftrace_regex_lock);
1769 ftrace_filter_write(struct file *file, const char __user *ubuf,
1770 size_t cnt, loff_t *ppos)
1772 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1776 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1777 size_t cnt, loff_t *ppos)
1779 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1783 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1785 if (unlikely(ftrace_disabled))
1788 mutex_lock(&ftrace_regex_lock);
1790 ftrace_filter_reset(enable);
1792 ftrace_match_records(buf, len, enable);
1793 mutex_unlock(&ftrace_regex_lock);
1797 * ftrace_set_filter - set a function to filter on in ftrace
1798 * @buf - the string that holds the function filter text.
1799 * @len - the length of the string.
1800 * @reset - non zero to reset all filters before applying this filter.
1802 * Filters denote which functions should be enabled when tracing is enabled.
1803 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1805 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1807 ftrace_set_regex(buf, len, reset, 1);
1811 * ftrace_set_notrace - set a function to not trace in ftrace
1812 * @buf - the string that holds the function notrace text.
1813 * @len - the length of the string.
1814 * @reset - non zero to reset all filters before applying this filter.
1816 * Notrace Filters denote which functions should not be enabled when tracing
1817 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1820 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1822 ftrace_set_regex(buf, len, reset, 0);
1826 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1828 struct seq_file *m = (struct seq_file *)file->private_data;
1829 struct ftrace_iterator *iter;
1831 mutex_lock(&ftrace_regex_lock);
1832 if (file->f_mode & FMODE_READ) {
1835 seq_release(inode, file);
1837 iter = file->private_data;
1839 if (iter->buffer_idx) {
1841 iter->buffer[iter->buffer_idx] = 0;
1842 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
1845 mutex_lock(&ftrace_lock);
1846 if (ftrace_start_up && ftrace_enabled)
1847 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1848 mutex_unlock(&ftrace_lock);
1851 mutex_unlock(&ftrace_regex_lock);
1856 ftrace_filter_release(struct inode *inode, struct file *file)
1858 return ftrace_regex_release(inode, file, 1);
1862 ftrace_notrace_release(struct inode *inode, struct file *file)
1864 return ftrace_regex_release(inode, file, 0);
1867 static const struct file_operations ftrace_avail_fops = {
1868 .open = ftrace_avail_open,
1870 .llseek = seq_lseek,
1871 .release = ftrace_avail_release,
1874 static const struct file_operations ftrace_failures_fops = {
1875 .open = ftrace_failures_open,
1877 .llseek = seq_lseek,
1878 .release = ftrace_avail_release,
1881 static const struct file_operations ftrace_filter_fops = {
1882 .open = ftrace_filter_open,
1883 .read = ftrace_regex_read,
1884 .write = ftrace_filter_write,
1885 .llseek = ftrace_regex_lseek,
1886 .release = ftrace_filter_release,
1889 static const struct file_operations ftrace_notrace_fops = {
1890 .open = ftrace_notrace_open,
1891 .read = ftrace_regex_read,
1892 .write = ftrace_notrace_write,
1893 .llseek = ftrace_regex_lseek,
1894 .release = ftrace_notrace_release,
1897 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1899 static DEFINE_MUTEX(graph_lock);
1901 int ftrace_graph_count;
1902 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1905 g_next(struct seq_file *m, void *v, loff_t *pos)
1907 unsigned long *array = m->private;
1912 if (index >= ftrace_graph_count)
1915 return &array[index];
1918 static void *g_start(struct seq_file *m, loff_t *pos)
1922 mutex_lock(&graph_lock);
1924 /* Nothing, tell g_show to print all functions are enabled */
1925 if (!ftrace_graph_count && !*pos)
1928 p = g_next(m, p, pos);
1933 static void g_stop(struct seq_file *m, void *p)
1935 mutex_unlock(&graph_lock);
1938 static int g_show(struct seq_file *m, void *v)
1940 unsigned long *ptr = v;
1941 char str[KSYM_SYMBOL_LEN];
1946 if (ptr == (unsigned long *)1) {
1947 seq_printf(m, "#### all functions enabled ####\n");
1951 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1953 seq_printf(m, "%s\n", str);
1958 static struct seq_operations ftrace_graph_seq_ops = {
1966 ftrace_graph_open(struct inode *inode, struct file *file)
1970 if (unlikely(ftrace_disabled))
1973 mutex_lock(&graph_lock);
1974 if ((file->f_mode & FMODE_WRITE) &&
1975 !(file->f_flags & O_APPEND)) {
1976 ftrace_graph_count = 0;
1977 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1980 if (file->f_mode & FMODE_READ) {
1981 ret = seq_open(file, &ftrace_graph_seq_ops);
1983 struct seq_file *m = file->private_data;
1984 m->private = ftrace_graph_funcs;
1987 file->private_data = ftrace_graph_funcs;
1988 mutex_unlock(&graph_lock);
1994 ftrace_graph_read(struct file *file, char __user *ubuf,
1995 size_t cnt, loff_t *ppos)
1997 if (file->f_mode & FMODE_READ)
1998 return seq_read(file, ubuf, cnt, ppos);
2004 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2006 struct dyn_ftrace *rec;
2007 struct ftrace_page *pg;
2015 if (ftrace_disabled)
2019 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2023 search_len = strlen(search);
2025 mutex_lock(&ftrace_lock);
2026 do_for_each_ftrace_rec(pg, rec) {
2028 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2031 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2034 if (ftrace_match_record(rec, search, search_len, type)) {
2035 /* ensure it is not already in the array */
2037 for (i = 0; i < *idx; i++)
2038 if (array[i] == rec->ip) {
2043 array[(*idx)++] = rec->ip;
2047 } while_for_each_ftrace_rec();
2049 mutex_unlock(&ftrace_lock);
2051 return found ? 0 : -EINVAL;
2055 ftrace_graph_write(struct file *file, const char __user *ubuf,
2056 size_t cnt, loff_t *ppos)
2058 unsigned char buffer[FTRACE_BUFF_MAX+1];
2059 unsigned long *array;
2065 if (!cnt || cnt < 0)
2068 mutex_lock(&graph_lock);
2070 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2075 if (file->f_mode & FMODE_READ) {
2076 struct seq_file *m = file->private_data;
2079 array = file->private_data;
2081 ret = get_user(ch, ubuf++);
2087 /* skip white space */
2088 while (cnt && isspace(ch)) {
2089 ret = get_user(ch, ubuf++);
2102 while (cnt && !isspace(ch)) {
2103 if (index < FTRACE_BUFF_MAX)
2104 buffer[index++] = ch;
2109 ret = get_user(ch, ubuf++);
2117 /* we allow only one expression at a time */
2118 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2122 file->f_pos += read;
2126 mutex_unlock(&graph_lock);
2131 static const struct file_operations ftrace_graph_fops = {
2132 .open = ftrace_graph_open,
2133 .read = ftrace_graph_read,
2134 .write = ftrace_graph_write,
2136 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2138 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2140 struct dentry *entry;
2142 entry = debugfs_create_file("available_filter_functions", 0444,
2143 d_tracer, NULL, &ftrace_avail_fops);
2145 pr_warning("Could not create debugfs "
2146 "'available_filter_functions' entry\n");
2148 entry = debugfs_create_file("failures", 0444,
2149 d_tracer, NULL, &ftrace_failures_fops);
2151 pr_warning("Could not create debugfs 'failures' entry\n");
2153 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2154 NULL, &ftrace_filter_fops);
2156 pr_warning("Could not create debugfs "
2157 "'set_ftrace_filter' entry\n");
2159 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2160 NULL, &ftrace_notrace_fops);
2162 pr_warning("Could not create debugfs "
2163 "'set_ftrace_notrace' entry\n");
2165 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2166 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2168 &ftrace_graph_fops);
2170 pr_warning("Could not create debugfs "
2171 "'set_graph_function' entry\n");
2172 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2177 static int ftrace_convert_nops(struct module *mod,
2178 unsigned long *start,
2183 unsigned long flags;
2185 mutex_lock(&ftrace_lock);
2188 addr = ftrace_call_adjust(*p++);
2190 * Some architecture linkers will pad between
2191 * the different mcount_loc sections of different
2192 * object files to satisfy alignments.
2193 * Skip any NULL pointers.
2197 ftrace_record_ip(addr);
2200 /* disable interrupts to prevent kstop machine */
2201 local_irq_save(flags);
2202 ftrace_update_code(mod);
2203 local_irq_restore(flags);
2204 mutex_unlock(&ftrace_lock);
2209 void ftrace_init_module(struct module *mod,
2210 unsigned long *start, unsigned long *end)
2212 if (ftrace_disabled || start == end)
2214 ftrace_convert_nops(mod, start, end);
2217 extern unsigned long __start_mcount_loc[];
2218 extern unsigned long __stop_mcount_loc[];
2220 void __init ftrace_init(void)
2222 unsigned long count, addr, flags;
2225 /* Keep the ftrace pointer to the stub */
2226 addr = (unsigned long)ftrace_stub;
2228 local_irq_save(flags);
2229 ftrace_dyn_arch_init(&addr);
2230 local_irq_restore(flags);
2232 /* ftrace_dyn_arch_init places the return code in addr */
2236 count = __stop_mcount_loc - __start_mcount_loc;
2238 ret = ftrace_dyn_table_alloc(count);
2242 last_ftrace_enabled = ftrace_enabled = 1;
2244 ret = ftrace_convert_nops(NULL,
2250 ftrace_disabled = 1;
2255 static int __init ftrace_nodyn_init(void)
2260 device_initcall(ftrace_nodyn_init);
2262 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2263 static inline void ftrace_startup_enable(int command) { }
2264 /* Keep as macros so we do not need to define the commands */
2265 # define ftrace_startup(command) do { } while (0)
2266 # define ftrace_shutdown(command) do { } while (0)
2267 # define ftrace_startup_sysctl() do { } while (0)
2268 # define ftrace_shutdown_sysctl() do { } while (0)
2269 #endif /* CONFIG_DYNAMIC_FTRACE */
2272 ftrace_pid_read(struct file *file, char __user *ubuf,
2273 size_t cnt, loff_t *ppos)
2278 if (ftrace_pid_trace == ftrace_swapper_pid)
2279 r = sprintf(buf, "swapper tasks\n");
2280 else if (ftrace_pid_trace)
2281 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
2283 r = sprintf(buf, "no pid\n");
2285 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2288 static void clear_ftrace_swapper(void)
2290 struct task_struct *p;
2294 for_each_online_cpu(cpu) {
2296 clear_tsk_trace_trace(p);
2301 static void set_ftrace_swapper(void)
2303 struct task_struct *p;
2307 for_each_online_cpu(cpu) {
2309 set_tsk_trace_trace(p);
2314 static void clear_ftrace_pid(struct pid *pid)
2316 struct task_struct *p;
2319 do_each_pid_task(pid, PIDTYPE_PID, p) {
2320 clear_tsk_trace_trace(p);
2321 } while_each_pid_task(pid, PIDTYPE_PID, p);
2327 static void set_ftrace_pid(struct pid *pid)
2329 struct task_struct *p;
2332 do_each_pid_task(pid, PIDTYPE_PID, p) {
2333 set_tsk_trace_trace(p);
2334 } while_each_pid_task(pid, PIDTYPE_PID, p);
2338 static void clear_ftrace_pid_task(struct pid **pid)
2340 if (*pid == ftrace_swapper_pid)
2341 clear_ftrace_swapper();
2343 clear_ftrace_pid(*pid);
2348 static void set_ftrace_pid_task(struct pid *pid)
2350 if (pid == ftrace_swapper_pid)
2351 set_ftrace_swapper();
2353 set_ftrace_pid(pid);
2357 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2358 size_t cnt, loff_t *ppos)
2365 if (cnt >= sizeof(buf))
2368 if (copy_from_user(&buf, ubuf, cnt))
2373 ret = strict_strtol(buf, 10, &val);
2377 mutex_lock(&ftrace_lock);
2379 /* disable pid tracing */
2380 if (!ftrace_pid_trace)
2383 clear_ftrace_pid_task(&ftrace_pid_trace);
2386 /* swapper task is special */
2388 pid = ftrace_swapper_pid;
2389 if (pid == ftrace_pid_trace)
2392 pid = find_get_pid(val);
2394 if (pid == ftrace_pid_trace) {
2400 if (ftrace_pid_trace)
2401 clear_ftrace_pid_task(&ftrace_pid_trace);
2406 ftrace_pid_trace = pid;
2408 set_ftrace_pid_task(ftrace_pid_trace);
2411 /* update the function call */
2412 ftrace_update_pid_func();
2413 ftrace_startup_enable(0);
2416 mutex_unlock(&ftrace_lock);
2421 static const struct file_operations ftrace_pid_fops = {
2422 .read = ftrace_pid_read,
2423 .write = ftrace_pid_write,
2426 static __init int ftrace_init_debugfs(void)
2428 struct dentry *d_tracer;
2429 struct dentry *entry;
2431 d_tracer = tracing_init_dentry();
2435 ftrace_init_dyn_debugfs(d_tracer);
2437 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2438 NULL, &ftrace_pid_fops);
2440 pr_warning("Could not create debugfs "
2441 "'set_ftrace_pid' entry\n");
2444 fs_initcall(ftrace_init_debugfs);
2447 * ftrace_kill - kill ftrace
2449 * This function should be used by panic code. It stops ftrace
2450 * but in a not so nice way. If you need to simply kill ftrace
2451 * from a non-atomic section, use ftrace_kill.
2453 void ftrace_kill(void)
2455 ftrace_disabled = 1;
2457 clear_ftrace_function();
2461 * register_ftrace_function - register a function for profiling
2462 * @ops - ops structure that holds the function for profiling.
2464 * Register a function to be called by all functions in the
2467 * Note: @ops->func and all the functions it calls must be labeled
2468 * with "notrace", otherwise it will go into a
2471 int register_ftrace_function(struct ftrace_ops *ops)
2475 if (unlikely(ftrace_disabled))
2478 mutex_lock(&ftrace_lock);
2480 ret = __register_ftrace_function(ops);
2483 mutex_unlock(&ftrace_lock);
2488 * unregister_ftrace_function - unregister a function for profiling.
2489 * @ops - ops structure that holds the function to unregister
2491 * Unregister a function that was added to be called by ftrace profiling.
2493 int unregister_ftrace_function(struct ftrace_ops *ops)
2497 mutex_lock(&ftrace_lock);
2498 ret = __unregister_ftrace_function(ops);
2500 mutex_unlock(&ftrace_lock);
2506 ftrace_enable_sysctl(struct ctl_table *table, int write,
2507 struct file *file, void __user *buffer, size_t *lenp,
2512 if (unlikely(ftrace_disabled))
2515 mutex_lock(&ftrace_lock);
2517 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
2519 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2522 last_ftrace_enabled = ftrace_enabled;
2524 if (ftrace_enabled) {
2526 ftrace_startup_sysctl();
2528 /* we are starting ftrace again */
2529 if (ftrace_list != &ftrace_list_end) {
2530 if (ftrace_list->next == &ftrace_list_end)
2531 ftrace_trace_function = ftrace_list->func;
2533 ftrace_trace_function = ftrace_list_func;
2537 /* stopping ftrace calls (just send to ftrace_stub) */
2538 ftrace_trace_function = ftrace_stub;
2540 ftrace_shutdown_sysctl();
2544 mutex_unlock(&ftrace_lock);
2548 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2550 static atomic_t ftrace_graph_active;
2551 static struct notifier_block ftrace_suspend_notifier;
2553 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2558 /* The callbacks that hook a function */
2559 trace_func_graph_ret_t ftrace_graph_return =
2560 (trace_func_graph_ret_t)ftrace_stub;
2561 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
2563 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2564 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2568 unsigned long flags;
2569 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2570 struct task_struct *g, *t;
2572 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2573 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2574 * sizeof(struct ftrace_ret_stack),
2576 if (!ret_stack_list[i]) {
2584 read_lock_irqsave(&tasklist_lock, flags);
2585 do_each_thread(g, t) {
2591 if (t->ret_stack == NULL) {
2592 t->curr_ret_stack = -1;
2593 /* Make sure IRQs see the -1 first: */
2595 t->ret_stack = ret_stack_list[start++];
2596 atomic_set(&t->tracing_graph_pause, 0);
2597 atomic_set(&t->trace_overrun, 0);
2599 } while_each_thread(g, t);
2602 read_unlock_irqrestore(&tasklist_lock, flags);
2604 for (i = start; i < end; i++)
2605 kfree(ret_stack_list[i]);
2609 /* Allocate a return stack for each task */
2610 static int start_graph_tracing(void)
2612 struct ftrace_ret_stack **ret_stack_list;
2615 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2616 sizeof(struct ftrace_ret_stack *),
2619 if (!ret_stack_list)
2622 /* The cpu_boot init_task->ret_stack will never be freed */
2623 for_each_online_cpu(cpu)
2624 ftrace_graph_init_task(idle_task(cpu));
2627 ret = alloc_retstack_tasklist(ret_stack_list);
2628 } while (ret == -EAGAIN);
2630 kfree(ret_stack_list);
2635 * Hibernation protection.
2636 * The state of the current task is too much unstable during
2637 * suspend/restore to disk. We want to protect against that.
2640 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2644 case PM_HIBERNATION_PREPARE:
2645 pause_graph_tracing();
2648 case PM_POST_HIBERNATION:
2649 unpause_graph_tracing();
2655 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2656 trace_func_graph_ent_t entryfunc)
2660 mutex_lock(&ftrace_lock);
2662 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2663 register_pm_notifier(&ftrace_suspend_notifier);
2665 atomic_inc(&ftrace_graph_active);
2666 ret = start_graph_tracing();
2668 atomic_dec(&ftrace_graph_active);
2672 ftrace_graph_return = retfunc;
2673 ftrace_graph_entry = entryfunc;
2675 ftrace_startup(FTRACE_START_FUNC_RET);
2678 mutex_unlock(&ftrace_lock);
2682 void unregister_ftrace_graph(void)
2684 mutex_lock(&ftrace_lock);
2686 atomic_dec(&ftrace_graph_active);
2687 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
2688 ftrace_graph_entry = ftrace_graph_entry_stub;
2689 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
2690 unregister_pm_notifier(&ftrace_suspend_notifier);
2692 mutex_unlock(&ftrace_lock);
2695 /* Allocate a return stack for newly created task */
2696 void ftrace_graph_init_task(struct task_struct *t)
2698 if (atomic_read(&ftrace_graph_active)) {
2699 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2700 * sizeof(struct ftrace_ret_stack),
2704 t->curr_ret_stack = -1;
2705 atomic_set(&t->tracing_graph_pause, 0);
2706 atomic_set(&t->trace_overrun, 0);
2708 t->ret_stack = NULL;
2711 void ftrace_graph_exit_task(struct task_struct *t)
2713 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2715 t->ret_stack = NULL;
2716 /* NULL must become visible to IRQs before we free it: */
2722 void ftrace_graph_stop(void)