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 <trace/sched.h>
34 #include <asm/ftrace.h>
38 #define FTRACE_WARN_ON(cond) \
44 #define FTRACE_WARN_ON_ONCE(cond) \
46 if (WARN_ON_ONCE(cond)) \
50 /* hash bits for specific function selection */
51 #define FTRACE_HASH_BITS 7
52 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
54 /* ftrace_enabled is a method to turn ftrace on or off */
55 int ftrace_enabled __read_mostly;
56 static int last_ftrace_enabled;
58 /* Quick disabling of function tracer. */
59 int function_trace_stop;
62 * ftrace_disabled is set when an anomaly is discovered.
63 * ftrace_disabled is much stronger than ftrace_enabled.
65 static int ftrace_disabled __read_mostly;
67 static DEFINE_MUTEX(ftrace_lock);
69 static struct ftrace_ops ftrace_list_end __read_mostly =
74 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
75 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
76 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
77 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
79 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
81 struct ftrace_ops *op = ftrace_list;
83 /* in case someone actually ports this to alpha! */
84 read_barrier_depends();
86 while (op != &ftrace_list_end) {
88 read_barrier_depends();
89 op->func(ip, parent_ip);
94 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
96 if (!test_tsk_trace_trace(current))
99 ftrace_pid_function(ip, parent_ip);
102 static void set_ftrace_pid_function(ftrace_func_t func)
104 /* do not set ftrace_pid_function to itself! */
105 if (func != ftrace_pid_func)
106 ftrace_pid_function = func;
110 * clear_ftrace_function - reset the ftrace function
112 * This NULLs the ftrace function and in essence stops
113 * tracing. There may be lag
115 void clear_ftrace_function(void)
117 ftrace_trace_function = ftrace_stub;
118 __ftrace_trace_function = ftrace_stub;
119 ftrace_pid_function = ftrace_stub;
122 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
124 * For those archs that do not test ftrace_trace_stop in their
125 * mcount call site, we need to do it from C.
127 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
129 if (function_trace_stop)
132 __ftrace_trace_function(ip, parent_ip);
136 static int __register_ftrace_function(struct ftrace_ops *ops)
138 ops->next = ftrace_list;
140 * We are entering ops into the ftrace_list but another
141 * CPU might be walking that list. We need to make sure
142 * the ops->next pointer is valid before another CPU sees
143 * the ops pointer included into the ftrace_list.
148 if (ftrace_enabled) {
151 if (ops->next == &ftrace_list_end)
154 func = ftrace_list_func;
156 if (ftrace_pid_trace) {
157 set_ftrace_pid_function(func);
158 func = ftrace_pid_func;
162 * For one func, simply call it directly.
163 * For more than one func, call the chain.
165 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
166 ftrace_trace_function = func;
168 __ftrace_trace_function = func;
169 ftrace_trace_function = ftrace_test_stop_func;
176 static int __unregister_ftrace_function(struct ftrace_ops *ops)
178 struct ftrace_ops **p;
181 * If we are removing the last function, then simply point
182 * to the ftrace_stub.
184 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
185 ftrace_trace_function = ftrace_stub;
186 ftrace_list = &ftrace_list_end;
190 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
199 if (ftrace_enabled) {
200 /* If we only have one func left, then call that directly */
201 if (ftrace_list->next == &ftrace_list_end) {
202 ftrace_func_t func = ftrace_list->func;
204 if (ftrace_pid_trace) {
205 set_ftrace_pid_function(func);
206 func = ftrace_pid_func;
208 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
209 ftrace_trace_function = func;
211 __ftrace_trace_function = func;
219 static void ftrace_update_pid_func(void)
223 if (ftrace_trace_function == ftrace_stub)
226 func = ftrace_trace_function;
228 if (ftrace_pid_trace) {
229 set_ftrace_pid_function(func);
230 func = ftrace_pid_func;
232 if (func == ftrace_pid_func)
233 func = ftrace_pid_function;
236 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
237 ftrace_trace_function = func;
239 __ftrace_trace_function = func;
243 /* set when tracing only a pid */
244 struct pid *ftrace_pid_trace;
245 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
247 #ifdef CONFIG_DYNAMIC_FTRACE
249 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
250 # error Dynamic ftrace depends on MCOUNT_RECORD
253 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
255 struct ftrace_func_probe {
256 struct hlist_node node;
257 struct ftrace_probe_ops *ops;
266 FTRACE_ENABLE_CALLS = (1 << 0),
267 FTRACE_DISABLE_CALLS = (1 << 1),
268 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
269 FTRACE_ENABLE_MCOUNT = (1 << 3),
270 FTRACE_DISABLE_MCOUNT = (1 << 4),
271 FTRACE_START_FUNC_RET = (1 << 5),
272 FTRACE_STOP_FUNC_RET = (1 << 6),
275 static int ftrace_filtered;
277 static struct dyn_ftrace *ftrace_new_addrs;
279 static DEFINE_MUTEX(ftrace_regex_lock);
282 struct ftrace_page *next;
284 struct dyn_ftrace records[];
287 #define ENTRIES_PER_PAGE \
288 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
290 /* estimate from running different kernels */
291 #define NR_TO_INIT 10000
293 static struct ftrace_page *ftrace_pages_start;
294 static struct ftrace_page *ftrace_pages;
296 static struct dyn_ftrace *ftrace_free_records;
299 * This is a double for. Do not use 'break' to break out of the loop,
300 * you must use a goto.
302 #define do_for_each_ftrace_rec(pg, rec) \
303 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
305 for (_____i = 0; _____i < pg->index; _____i++) { \
306 rec = &pg->records[_____i];
308 #define while_for_each_ftrace_rec() \
312 #ifdef CONFIG_KPROBES
314 static int frozen_record_count;
316 static inline void freeze_record(struct dyn_ftrace *rec)
318 if (!(rec->flags & FTRACE_FL_FROZEN)) {
319 rec->flags |= FTRACE_FL_FROZEN;
320 frozen_record_count++;
324 static inline void unfreeze_record(struct dyn_ftrace *rec)
326 if (rec->flags & FTRACE_FL_FROZEN) {
327 rec->flags &= ~FTRACE_FL_FROZEN;
328 frozen_record_count--;
332 static inline int record_frozen(struct dyn_ftrace *rec)
334 return rec->flags & FTRACE_FL_FROZEN;
337 # define freeze_record(rec) ({ 0; })
338 # define unfreeze_record(rec) ({ 0; })
339 # define record_frozen(rec) ({ 0; })
340 #endif /* CONFIG_KPROBES */
342 static void ftrace_free_rec(struct dyn_ftrace *rec)
344 rec->freelist = ftrace_free_records;
345 ftrace_free_records = rec;
346 rec->flags |= FTRACE_FL_FREE;
349 void ftrace_release(void *start, unsigned long size)
351 struct dyn_ftrace *rec;
352 struct ftrace_page *pg;
353 unsigned long s = (unsigned long)start;
354 unsigned long e = s + size;
356 if (ftrace_disabled || !start)
359 mutex_lock(&ftrace_lock);
360 do_for_each_ftrace_rec(pg, rec) {
361 if ((rec->ip >= s) && (rec->ip < e)) {
363 * rec->ip is changed in ftrace_free_rec()
364 * It should not between s and e if record was freed.
366 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
367 ftrace_free_rec(rec);
369 } while_for_each_ftrace_rec();
370 mutex_unlock(&ftrace_lock);
373 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
375 struct dyn_ftrace *rec;
377 /* First check for freed records */
378 if (ftrace_free_records) {
379 rec = ftrace_free_records;
381 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
382 FTRACE_WARN_ON_ONCE(1);
383 ftrace_free_records = NULL;
387 ftrace_free_records = rec->freelist;
388 memset(rec, 0, sizeof(*rec));
392 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
393 if (!ftrace_pages->next) {
394 /* allocate another page */
396 (void *)get_zeroed_page(GFP_KERNEL);
397 if (!ftrace_pages->next)
400 ftrace_pages = ftrace_pages->next;
403 return &ftrace_pages->records[ftrace_pages->index++];
406 static struct dyn_ftrace *
407 ftrace_record_ip(unsigned long ip)
409 struct dyn_ftrace *rec;
414 rec = ftrace_alloc_dyn_node(ip);
419 rec->newlist = ftrace_new_addrs;
420 ftrace_new_addrs = rec;
425 static void print_ip_ins(const char *fmt, unsigned char *p)
429 printk(KERN_CONT "%s", fmt);
431 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
432 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
435 static void ftrace_bug(int failed, unsigned long ip)
439 FTRACE_WARN_ON_ONCE(1);
440 pr_info("ftrace faulted on modifying ");
444 FTRACE_WARN_ON_ONCE(1);
445 pr_info("ftrace failed to modify ");
447 print_ip_ins(" actual: ", (unsigned char *)ip);
448 printk(KERN_CONT "\n");
451 FTRACE_WARN_ON_ONCE(1);
452 pr_info("ftrace faulted on writing ");
456 FTRACE_WARN_ON_ONCE(1);
457 pr_info("ftrace faulted on unknown error ");
464 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
466 unsigned long ftrace_addr;
467 unsigned long ip, fl;
469 ftrace_addr = (unsigned long)FTRACE_ADDR;
474 * If this record is not to be traced and
475 * it is not enabled then do nothing.
477 * If this record is not to be traced and
478 * it is enabled then disable it.
481 if (rec->flags & FTRACE_FL_NOTRACE) {
482 if (rec->flags & FTRACE_FL_ENABLED)
483 rec->flags &= ~FTRACE_FL_ENABLED;
487 } else if (ftrace_filtered && enable) {
492 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
494 /* Record is filtered and enabled, do nothing */
495 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
498 /* Record is not filtered or enabled, do nothing */
502 /* Record is not filtered but enabled, disable it */
503 if (fl == FTRACE_FL_ENABLED)
504 rec->flags &= ~FTRACE_FL_ENABLED;
506 /* Otherwise record is filtered but not enabled, enable it */
507 rec->flags |= FTRACE_FL_ENABLED;
509 /* Disable or not filtered */
512 /* if record is enabled, do nothing */
513 if (rec->flags & FTRACE_FL_ENABLED)
516 rec->flags |= FTRACE_FL_ENABLED;
520 /* if record is not enabled, do nothing */
521 if (!(rec->flags & FTRACE_FL_ENABLED))
524 rec->flags &= ~FTRACE_FL_ENABLED;
528 if (rec->flags & FTRACE_FL_ENABLED)
529 return ftrace_make_call(rec, ftrace_addr);
531 return ftrace_make_nop(NULL, rec, ftrace_addr);
534 static void ftrace_replace_code(int enable)
536 struct dyn_ftrace *rec;
537 struct ftrace_page *pg;
540 do_for_each_ftrace_rec(pg, rec) {
542 * Skip over free records, records that have
543 * failed and not converted.
545 if (rec->flags & FTRACE_FL_FREE ||
546 rec->flags & FTRACE_FL_FAILED ||
547 !(rec->flags & FTRACE_FL_CONVERTED))
550 /* ignore updates to this record's mcount site */
551 if (get_kprobe((void *)rec->ip)) {
555 unfreeze_record(rec);
558 failed = __ftrace_replace_code(rec, enable);
560 rec->flags |= FTRACE_FL_FAILED;
561 if ((system_state == SYSTEM_BOOTING) ||
562 !core_kernel_text(rec->ip)) {
563 ftrace_free_rec(rec);
565 ftrace_bug(failed, rec->ip);
566 /* Stop processing */
570 } while_for_each_ftrace_rec();
574 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
581 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
584 rec->flags |= FTRACE_FL_FAILED;
591 * archs can override this function if they must do something
592 * before the modifying code is performed.
594 int __weak ftrace_arch_code_modify_prepare(void)
600 * archs can override this function if they must do something
601 * after the modifying code is performed.
603 int __weak ftrace_arch_code_modify_post_process(void)
608 static int __ftrace_modify_code(void *data)
612 if (*command & FTRACE_ENABLE_CALLS)
613 ftrace_replace_code(1);
614 else if (*command & FTRACE_DISABLE_CALLS)
615 ftrace_replace_code(0);
617 if (*command & FTRACE_UPDATE_TRACE_FUNC)
618 ftrace_update_ftrace_func(ftrace_trace_function);
620 if (*command & FTRACE_START_FUNC_RET)
621 ftrace_enable_ftrace_graph_caller();
622 else if (*command & FTRACE_STOP_FUNC_RET)
623 ftrace_disable_ftrace_graph_caller();
628 static void ftrace_run_update_code(int command)
632 ret = ftrace_arch_code_modify_prepare();
637 stop_machine(__ftrace_modify_code, &command, NULL);
639 ret = ftrace_arch_code_modify_post_process();
643 static ftrace_func_t saved_ftrace_func;
644 static int ftrace_start_up;
646 static void ftrace_startup_enable(int command)
648 if (saved_ftrace_func != ftrace_trace_function) {
649 saved_ftrace_func = ftrace_trace_function;
650 command |= FTRACE_UPDATE_TRACE_FUNC;
653 if (!command || !ftrace_enabled)
656 ftrace_run_update_code(command);
659 static void ftrace_startup(int command)
661 if (unlikely(ftrace_disabled))
665 command |= FTRACE_ENABLE_CALLS;
667 ftrace_startup_enable(command);
670 static void ftrace_shutdown(int command)
672 if (unlikely(ftrace_disabled))
676 if (!ftrace_start_up)
677 command |= FTRACE_DISABLE_CALLS;
679 if (saved_ftrace_func != ftrace_trace_function) {
680 saved_ftrace_func = ftrace_trace_function;
681 command |= FTRACE_UPDATE_TRACE_FUNC;
684 if (!command || !ftrace_enabled)
687 ftrace_run_update_code(command);
690 static void ftrace_startup_sysctl(void)
692 int command = FTRACE_ENABLE_MCOUNT;
694 if (unlikely(ftrace_disabled))
697 /* Force update next time */
698 saved_ftrace_func = NULL;
699 /* ftrace_start_up is true if we want ftrace running */
701 command |= FTRACE_ENABLE_CALLS;
703 ftrace_run_update_code(command);
706 static void ftrace_shutdown_sysctl(void)
708 int command = FTRACE_DISABLE_MCOUNT;
710 if (unlikely(ftrace_disabled))
713 /* ftrace_start_up is true if ftrace is running */
715 command |= FTRACE_DISABLE_CALLS;
717 ftrace_run_update_code(command);
720 static cycle_t ftrace_update_time;
721 static unsigned long ftrace_update_cnt;
722 unsigned long ftrace_update_tot_cnt;
724 static int ftrace_update_code(struct module *mod)
726 struct dyn_ftrace *p;
729 start = ftrace_now(raw_smp_processor_id());
730 ftrace_update_cnt = 0;
732 while (ftrace_new_addrs) {
734 /* If something went wrong, bail without enabling anything */
735 if (unlikely(ftrace_disabled))
738 p = ftrace_new_addrs;
739 ftrace_new_addrs = p->newlist;
742 /* convert record (i.e, patch mcount-call with NOP) */
743 if (ftrace_code_disable(mod, p)) {
744 p->flags |= FTRACE_FL_CONVERTED;
750 stop = ftrace_now(raw_smp_processor_id());
751 ftrace_update_time = stop - start;
752 ftrace_update_tot_cnt += ftrace_update_cnt;
757 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
759 struct ftrace_page *pg;
763 /* allocate a few pages */
764 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
765 if (!ftrace_pages_start)
769 * Allocate a few more pages.
771 * TODO: have some parser search vmlinux before
772 * final linking to find all calls to ftrace.
774 * a) know how many pages to allocate.
776 * b) set up the table then.
778 * The dynamic code is still necessary for
782 pg = ftrace_pages = ftrace_pages_start;
784 cnt = num_to_init / ENTRIES_PER_PAGE;
785 pr_info("ftrace: allocating %ld entries in %d pages\n",
786 num_to_init, cnt + 1);
788 for (i = 0; i < cnt; i++) {
789 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
791 /* If we fail, we'll try later anyway */
802 FTRACE_ITER_FILTER = (1 << 0),
803 FTRACE_ITER_CONT = (1 << 1),
804 FTRACE_ITER_NOTRACE = (1 << 2),
805 FTRACE_ITER_FAILURES = (1 << 3),
806 FTRACE_ITER_PRINTALL = (1 << 4),
807 FTRACE_ITER_HASH = (1 << 5),
810 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
812 struct ftrace_iterator {
813 struct ftrace_page *pg;
817 unsigned char buffer[FTRACE_BUFF_MAX+1];
823 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
825 struct ftrace_iterator *iter = m->private;
826 struct hlist_node *hnd = v;
827 struct hlist_head *hhd;
829 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
834 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
837 hhd = &ftrace_func_hash[iter->hidx];
839 if (hlist_empty(hhd)) {
858 static void *t_hash_start(struct seq_file *m, loff_t *pos)
860 struct ftrace_iterator *iter = m->private;
863 iter->flags |= FTRACE_ITER_HASH;
865 return t_hash_next(m, p, pos);
868 static int t_hash_show(struct seq_file *m, void *v)
870 struct ftrace_func_probe *rec;
871 struct hlist_node *hnd = v;
872 char str[KSYM_SYMBOL_LEN];
874 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
877 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
879 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
880 seq_printf(m, "%s:", str);
882 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
883 seq_printf(m, "%s", str);
886 seq_printf(m, ":%p", rec->data);
893 t_next(struct seq_file *m, void *v, loff_t *pos)
895 struct ftrace_iterator *iter = m->private;
896 struct dyn_ftrace *rec = NULL;
898 if (iter->flags & FTRACE_ITER_HASH)
899 return t_hash_next(m, v, pos);
903 if (iter->flags & FTRACE_ITER_PRINTALL)
907 if (iter->idx >= iter->pg->index) {
908 if (iter->pg->next) {
909 iter->pg = iter->pg->next;
916 rec = &iter->pg->records[iter->idx++];
917 if ((rec->flags & FTRACE_FL_FREE) ||
919 (!(iter->flags & FTRACE_ITER_FAILURES) &&
920 (rec->flags & FTRACE_FL_FAILED)) ||
922 ((iter->flags & FTRACE_ITER_FAILURES) &&
923 !(rec->flags & FTRACE_FL_FAILED)) ||
925 ((iter->flags & FTRACE_ITER_FILTER) &&
926 !(rec->flags & FTRACE_FL_FILTER)) ||
928 ((iter->flags & FTRACE_ITER_NOTRACE) &&
929 !(rec->flags & FTRACE_FL_NOTRACE))) {
938 static void *t_start(struct seq_file *m, loff_t *pos)
940 struct ftrace_iterator *iter = m->private;
943 mutex_lock(&ftrace_lock);
945 * For set_ftrace_filter reading, if we have the filter
946 * off, we can short cut and just print out that all
947 * functions are enabled.
949 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
951 return t_hash_start(m, pos);
952 iter->flags |= FTRACE_ITER_PRINTALL;
957 if (iter->flags & FTRACE_ITER_HASH)
958 return t_hash_start(m, pos);
967 p = t_next(m, p, pos);
970 return t_hash_start(m, pos);
975 static void t_stop(struct seq_file *m, void *p)
977 mutex_unlock(&ftrace_lock);
980 static int t_show(struct seq_file *m, void *v)
982 struct ftrace_iterator *iter = m->private;
983 struct dyn_ftrace *rec = v;
984 char str[KSYM_SYMBOL_LEN];
986 if (iter->flags & FTRACE_ITER_HASH)
987 return t_hash_show(m, v);
989 if (iter->flags & FTRACE_ITER_PRINTALL) {
990 seq_printf(m, "#### all functions enabled ####\n");
997 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
999 seq_printf(m, "%s\n", str);
1004 static struct seq_operations show_ftrace_seq_ops = {
1012 ftrace_avail_open(struct inode *inode, struct file *file)
1014 struct ftrace_iterator *iter;
1017 if (unlikely(ftrace_disabled))
1020 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1024 iter->pg = ftrace_pages_start;
1026 ret = seq_open(file, &show_ftrace_seq_ops);
1028 struct seq_file *m = file->private_data;
1038 int ftrace_avail_release(struct inode *inode, struct file *file)
1040 struct seq_file *m = (struct seq_file *)file->private_data;
1041 struct ftrace_iterator *iter = m->private;
1043 seq_release(inode, file);
1050 ftrace_failures_open(struct inode *inode, struct file *file)
1054 struct ftrace_iterator *iter;
1056 ret = ftrace_avail_open(inode, file);
1058 m = (struct seq_file *)file->private_data;
1059 iter = (struct ftrace_iterator *)m->private;
1060 iter->flags = FTRACE_ITER_FAILURES;
1067 static void ftrace_filter_reset(int enable)
1069 struct ftrace_page *pg;
1070 struct dyn_ftrace *rec;
1071 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1073 mutex_lock(&ftrace_lock);
1075 ftrace_filtered = 0;
1076 do_for_each_ftrace_rec(pg, rec) {
1077 if (rec->flags & FTRACE_FL_FAILED)
1079 rec->flags &= ~type;
1080 } while_for_each_ftrace_rec();
1081 mutex_unlock(&ftrace_lock);
1085 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1087 struct ftrace_iterator *iter;
1090 if (unlikely(ftrace_disabled))
1093 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1097 mutex_lock(&ftrace_regex_lock);
1098 if ((file->f_mode & FMODE_WRITE) &&
1099 !(file->f_flags & O_APPEND))
1100 ftrace_filter_reset(enable);
1102 if (file->f_mode & FMODE_READ) {
1103 iter->pg = ftrace_pages_start;
1104 iter->flags = enable ? FTRACE_ITER_FILTER :
1105 FTRACE_ITER_NOTRACE;
1107 ret = seq_open(file, &show_ftrace_seq_ops);
1109 struct seq_file *m = file->private_data;
1114 file->private_data = iter;
1115 mutex_unlock(&ftrace_regex_lock);
1121 ftrace_filter_open(struct inode *inode, struct file *file)
1123 return ftrace_regex_open(inode, file, 1);
1127 ftrace_notrace_open(struct inode *inode, struct file *file)
1129 return ftrace_regex_open(inode, file, 0);
1133 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1137 if (file->f_mode & FMODE_READ)
1138 ret = seq_lseek(file, offset, origin);
1140 file->f_pos = ret = 1;
1153 * (static function - no need for kernel doc)
1155 * Pass in a buffer containing a glob and this function will
1156 * set search to point to the search part of the buffer and
1157 * return the type of search it is (see enum above).
1158 * This does modify buff.
1160 * Returns enum type.
1161 * search returns the pointer to use for comparison.
1162 * not returns 1 if buff started with a '!'
1166 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1168 int type = MATCH_FULL;
1171 if (buff[0] == '!') {
1180 for (i = 0; i < len; i++) {
1181 if (buff[i] == '*') {
1184 type = MATCH_END_ONLY;
1186 if (type == MATCH_END_ONLY)
1187 type = MATCH_MIDDLE_ONLY;
1189 type = MATCH_FRONT_ONLY;
1199 static int ftrace_match(char *str, char *regex, int len, int type)
1206 if (strcmp(str, regex) == 0)
1209 case MATCH_FRONT_ONLY:
1210 if (strncmp(str, regex, len) == 0)
1213 case MATCH_MIDDLE_ONLY:
1214 if (strstr(str, regex))
1217 case MATCH_END_ONLY:
1218 ptr = strstr(str, regex);
1219 if (ptr && (ptr[len] == 0))
1228 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1230 char str[KSYM_SYMBOL_LEN];
1232 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1233 return ftrace_match(str, regex, len, type);
1236 static void ftrace_match_records(char *buff, int len, int enable)
1238 unsigned int search_len;
1239 struct ftrace_page *pg;
1240 struct dyn_ftrace *rec;
1246 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1247 type = ftrace_setup_glob(buff, len, &search, ¬);
1249 search_len = strlen(search);
1251 mutex_lock(&ftrace_lock);
1252 do_for_each_ftrace_rec(pg, rec) {
1254 if (rec->flags & FTRACE_FL_FAILED)
1257 if (ftrace_match_record(rec, search, search_len, type)) {
1259 rec->flags &= ~flag;
1264 * Only enable filtering if we have a function that
1267 if (enable && (rec->flags & FTRACE_FL_FILTER))
1268 ftrace_filtered = 1;
1269 } while_for_each_ftrace_rec();
1270 mutex_unlock(&ftrace_lock);
1274 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1275 char *regex, int len, int type)
1277 char str[KSYM_SYMBOL_LEN];
1280 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1282 if (!modname || strcmp(modname, mod))
1285 /* blank search means to match all funcs in the mod */
1287 return ftrace_match(str, regex, len, type);
1292 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1294 unsigned search_len = 0;
1295 struct ftrace_page *pg;
1296 struct dyn_ftrace *rec;
1297 int type = MATCH_FULL;
1298 char *search = buff;
1302 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1304 /* blank or '*' mean the same */
1305 if (strcmp(buff, "*") == 0)
1308 /* handle the case of 'dont filter this module' */
1309 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1315 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1316 search_len = strlen(search);
1319 mutex_lock(&ftrace_lock);
1320 do_for_each_ftrace_rec(pg, rec) {
1322 if (rec->flags & FTRACE_FL_FAILED)
1325 if (ftrace_match_module_record(rec, mod,
1326 search, search_len, type)) {
1328 rec->flags &= ~flag;
1332 if (enable && (rec->flags & FTRACE_FL_FILTER))
1333 ftrace_filtered = 1;
1335 } while_for_each_ftrace_rec();
1336 mutex_unlock(&ftrace_lock);
1340 * We register the module command as a template to show others how
1341 * to register the a command as well.
1345 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1350 * cmd == 'mod' because we only registered this func
1351 * for the 'mod' ftrace_func_command.
1352 * But if you register one func with multiple commands,
1353 * you can tell which command was used by the cmd
1357 /* we must have a module name */
1361 mod = strsep(¶m, ":");
1365 ftrace_match_module_records(func, mod, enable);
1369 static struct ftrace_func_command ftrace_mod_cmd = {
1371 .func = ftrace_mod_callback,
1374 static int __init ftrace_mod_cmd_init(void)
1376 return register_ftrace_command(&ftrace_mod_cmd);
1378 device_initcall(ftrace_mod_cmd_init);
1381 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1383 struct ftrace_func_probe *entry;
1384 struct hlist_head *hhd;
1385 struct hlist_node *n;
1389 key = hash_long(ip, FTRACE_HASH_BITS);
1391 hhd = &ftrace_func_hash[key];
1393 if (hlist_empty(hhd))
1397 * Disable preemption for these calls to prevent a RCU grace
1398 * period. This syncs the hash iteration and freeing of items
1399 * on the hash. rcu_read_lock is too dangerous here.
1401 resched = ftrace_preempt_disable();
1402 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1403 if (entry->ip == ip)
1404 entry->ops->func(ip, parent_ip, &entry->data);
1406 ftrace_preempt_enable(resched);
1409 static struct ftrace_ops trace_probe_ops __read_mostly =
1411 .func = function_trace_probe_call,
1414 static int ftrace_probe_registered;
1416 static void __enable_ftrace_function_probe(void)
1420 if (ftrace_probe_registered)
1423 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1424 struct hlist_head *hhd = &ftrace_func_hash[i];
1428 /* Nothing registered? */
1429 if (i == FTRACE_FUNC_HASHSIZE)
1432 __register_ftrace_function(&trace_probe_ops);
1434 ftrace_probe_registered = 1;
1437 static void __disable_ftrace_function_probe(void)
1441 if (!ftrace_probe_registered)
1444 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1445 struct hlist_head *hhd = &ftrace_func_hash[i];
1450 /* no more funcs left */
1451 __unregister_ftrace_function(&trace_probe_ops);
1453 ftrace_probe_registered = 0;
1457 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1459 struct ftrace_func_probe *entry =
1460 container_of(rhp, struct ftrace_func_probe, rcu);
1462 if (entry->ops->free)
1463 entry->ops->free(&entry->data);
1469 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1472 struct ftrace_func_probe *entry;
1473 struct ftrace_page *pg;
1474 struct dyn_ftrace *rec;
1480 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1481 len = strlen(search);
1483 /* we do not support '!' for function probes */
1487 mutex_lock(&ftrace_lock);
1488 do_for_each_ftrace_rec(pg, rec) {
1490 if (rec->flags & FTRACE_FL_FAILED)
1493 if (!ftrace_match_record(rec, search, len, type))
1496 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1498 /* If we did not process any, then return error */
1509 * The caller might want to do something special
1510 * for each function we find. We call the callback
1511 * to give the caller an opportunity to do so.
1513 if (ops->callback) {
1514 if (ops->callback(rec->ip, &entry->data) < 0) {
1515 /* caller does not like this func */
1522 entry->ip = rec->ip;
1524 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1525 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1527 } while_for_each_ftrace_rec();
1528 __enable_ftrace_function_probe();
1531 mutex_unlock(&ftrace_lock);
1537 PROBE_TEST_FUNC = 1,
1542 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1543 void *data, int flags)
1545 struct ftrace_func_probe *entry;
1546 struct hlist_node *n, *tmp;
1547 char str[KSYM_SYMBOL_LEN];
1548 int type = MATCH_FULL;
1552 if (glob && (strcmp(glob, "*") || !strlen(glob)))
1557 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1558 len = strlen(search);
1560 /* we do not support '!' for function probes */
1565 mutex_lock(&ftrace_lock);
1566 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1567 struct hlist_head *hhd = &ftrace_func_hash[i];
1569 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1571 /* break up if statements for readability */
1572 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
1575 if ((flags & PROBE_TEST_DATA) && entry->data != data)
1578 /* do this last, since it is the most expensive */
1580 kallsyms_lookup(entry->ip, NULL, NULL,
1582 if (!ftrace_match(str, glob, len, type))
1586 hlist_del(&entry->node);
1587 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1590 __disable_ftrace_function_probe();
1591 mutex_unlock(&ftrace_lock);
1595 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1598 __unregister_ftrace_function_probe(glob, ops, data,
1599 PROBE_TEST_FUNC | PROBE_TEST_DATA);
1603 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
1605 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
1608 void unregister_ftrace_function_probe_all(char *glob)
1610 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
1613 static LIST_HEAD(ftrace_commands);
1614 static DEFINE_MUTEX(ftrace_cmd_mutex);
1616 int register_ftrace_command(struct ftrace_func_command *cmd)
1618 struct ftrace_func_command *p;
1621 mutex_lock(&ftrace_cmd_mutex);
1622 list_for_each_entry(p, &ftrace_commands, list) {
1623 if (strcmp(cmd->name, p->name) == 0) {
1628 list_add(&cmd->list, &ftrace_commands);
1630 mutex_unlock(&ftrace_cmd_mutex);
1635 int unregister_ftrace_command(struct ftrace_func_command *cmd)
1637 struct ftrace_func_command *p, *n;
1640 mutex_lock(&ftrace_cmd_mutex);
1641 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1642 if (strcmp(cmd->name, p->name) == 0) {
1644 list_del_init(&p->list);
1649 mutex_unlock(&ftrace_cmd_mutex);
1654 static int ftrace_process_regex(char *buff, int len, int enable)
1656 char *func, *command, *next = buff;
1657 struct ftrace_func_command *p;
1660 func = strsep(&next, ":");
1663 ftrace_match_records(func, len, enable);
1669 command = strsep(&next, ":");
1671 mutex_lock(&ftrace_cmd_mutex);
1672 list_for_each_entry(p, &ftrace_commands, list) {
1673 if (strcmp(p->name, command) == 0) {
1674 ret = p->func(func, command, next, enable);
1679 mutex_unlock(&ftrace_cmd_mutex);
1685 ftrace_regex_write(struct file *file, const char __user *ubuf,
1686 size_t cnt, loff_t *ppos, int enable)
1688 struct ftrace_iterator *iter;
1693 if (!cnt || cnt < 0)
1696 mutex_lock(&ftrace_regex_lock);
1698 if (file->f_mode & FMODE_READ) {
1699 struct seq_file *m = file->private_data;
1702 iter = file->private_data;
1705 iter->flags &= ~FTRACE_ITER_CONT;
1706 iter->buffer_idx = 0;
1709 ret = get_user(ch, ubuf++);
1715 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1716 /* skip white space */
1717 while (cnt && isspace(ch)) {
1718 ret = get_user(ch, ubuf++);
1726 file->f_pos += read;
1731 iter->buffer_idx = 0;
1734 while (cnt && !isspace(ch)) {
1735 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1736 iter->buffer[iter->buffer_idx++] = ch;
1741 ret = get_user(ch, ubuf++);
1750 iter->buffer[iter->buffer_idx] = 0;
1751 ret = ftrace_process_regex(iter->buffer,
1752 iter->buffer_idx, enable);
1755 iter->buffer_idx = 0;
1757 iter->flags |= FTRACE_ITER_CONT;
1760 file->f_pos += read;
1764 mutex_unlock(&ftrace_regex_lock);
1770 ftrace_filter_write(struct file *file, const char __user *ubuf,
1771 size_t cnt, loff_t *ppos)
1773 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1777 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1778 size_t cnt, loff_t *ppos)
1780 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1784 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1786 if (unlikely(ftrace_disabled))
1789 mutex_lock(&ftrace_regex_lock);
1791 ftrace_filter_reset(enable);
1793 ftrace_match_records(buf, len, enable);
1794 mutex_unlock(&ftrace_regex_lock);
1798 * ftrace_set_filter - set a function to filter on in ftrace
1799 * @buf - the string that holds the function filter text.
1800 * @len - the length of the string.
1801 * @reset - non zero to reset all filters before applying this filter.
1803 * Filters denote which functions should be enabled when tracing is enabled.
1804 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1806 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1808 ftrace_set_regex(buf, len, reset, 1);
1812 * ftrace_set_notrace - set a function to not trace in ftrace
1813 * @buf - the string that holds the function notrace text.
1814 * @len - the length of the string.
1815 * @reset - non zero to reset all filters before applying this filter.
1817 * Notrace Filters denote which functions should not be enabled when tracing
1818 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1821 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1823 ftrace_set_regex(buf, len, reset, 0);
1827 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1829 struct seq_file *m = (struct seq_file *)file->private_data;
1830 struct ftrace_iterator *iter;
1832 mutex_lock(&ftrace_regex_lock);
1833 if (file->f_mode & FMODE_READ) {
1836 seq_release(inode, file);
1838 iter = file->private_data;
1840 if (iter->buffer_idx) {
1842 iter->buffer[iter->buffer_idx] = 0;
1843 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
1846 mutex_lock(&ftrace_lock);
1847 if (ftrace_start_up && ftrace_enabled)
1848 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1849 mutex_unlock(&ftrace_lock);
1852 mutex_unlock(&ftrace_regex_lock);
1857 ftrace_filter_release(struct inode *inode, struct file *file)
1859 return ftrace_regex_release(inode, file, 1);
1863 ftrace_notrace_release(struct inode *inode, struct file *file)
1865 return ftrace_regex_release(inode, file, 0);
1868 static const struct file_operations ftrace_avail_fops = {
1869 .open = ftrace_avail_open,
1871 .llseek = seq_lseek,
1872 .release = ftrace_avail_release,
1875 static const struct file_operations ftrace_failures_fops = {
1876 .open = ftrace_failures_open,
1878 .llseek = seq_lseek,
1879 .release = ftrace_avail_release,
1882 static const struct file_operations ftrace_filter_fops = {
1883 .open = ftrace_filter_open,
1885 .write = ftrace_filter_write,
1886 .llseek = ftrace_regex_lseek,
1887 .release = ftrace_filter_release,
1890 static const struct file_operations ftrace_notrace_fops = {
1891 .open = ftrace_notrace_open,
1893 .write = ftrace_notrace_write,
1894 .llseek = ftrace_regex_lseek,
1895 .release = ftrace_notrace_release,
1898 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1900 static DEFINE_MUTEX(graph_lock);
1902 int ftrace_graph_count;
1903 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1906 g_next(struct seq_file *m, void *v, loff_t *pos)
1908 unsigned long *array = m->private;
1913 if (index >= ftrace_graph_count)
1916 return &array[index];
1919 static void *g_start(struct seq_file *m, loff_t *pos)
1923 mutex_lock(&graph_lock);
1925 /* Nothing, tell g_show to print all functions are enabled */
1926 if (!ftrace_graph_count && !*pos)
1929 p = g_next(m, p, pos);
1934 static void g_stop(struct seq_file *m, void *p)
1936 mutex_unlock(&graph_lock);
1939 static int g_show(struct seq_file *m, void *v)
1941 unsigned long *ptr = v;
1942 char str[KSYM_SYMBOL_LEN];
1947 if (ptr == (unsigned long *)1) {
1948 seq_printf(m, "#### all functions enabled ####\n");
1952 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1954 seq_printf(m, "%s\n", str);
1959 static struct seq_operations ftrace_graph_seq_ops = {
1967 ftrace_graph_open(struct inode *inode, struct file *file)
1971 if (unlikely(ftrace_disabled))
1974 mutex_lock(&graph_lock);
1975 if ((file->f_mode & FMODE_WRITE) &&
1976 !(file->f_flags & O_APPEND)) {
1977 ftrace_graph_count = 0;
1978 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1981 if (file->f_mode & FMODE_READ) {
1982 ret = seq_open(file, &ftrace_graph_seq_ops);
1984 struct seq_file *m = file->private_data;
1985 m->private = ftrace_graph_funcs;
1988 file->private_data = ftrace_graph_funcs;
1989 mutex_unlock(&graph_lock);
1995 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
1997 struct dyn_ftrace *rec;
1998 struct ftrace_page *pg;
2006 if (ftrace_disabled)
2010 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2014 search_len = strlen(search);
2016 mutex_lock(&ftrace_lock);
2017 do_for_each_ftrace_rec(pg, rec) {
2019 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2022 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2025 if (ftrace_match_record(rec, search, search_len, type)) {
2026 /* ensure it is not already in the array */
2028 for (i = 0; i < *idx; i++)
2029 if (array[i] == rec->ip) {
2034 array[(*idx)++] = rec->ip;
2038 } while_for_each_ftrace_rec();
2040 mutex_unlock(&ftrace_lock);
2042 return found ? 0 : -EINVAL;
2046 ftrace_graph_write(struct file *file, const char __user *ubuf,
2047 size_t cnt, loff_t *ppos)
2049 unsigned char buffer[FTRACE_BUFF_MAX+1];
2050 unsigned long *array;
2056 if (!cnt || cnt < 0)
2059 mutex_lock(&graph_lock);
2061 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2066 if (file->f_mode & FMODE_READ) {
2067 struct seq_file *m = file->private_data;
2070 array = file->private_data;
2072 ret = get_user(ch, ubuf++);
2078 /* skip white space */
2079 while (cnt && isspace(ch)) {
2080 ret = get_user(ch, ubuf++);
2093 while (cnt && !isspace(ch)) {
2094 if (index < FTRACE_BUFF_MAX)
2095 buffer[index++] = ch;
2100 ret = get_user(ch, ubuf++);
2108 /* we allow only one expression at a time */
2109 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2113 file->f_pos += read;
2117 mutex_unlock(&graph_lock);
2122 static const struct file_operations ftrace_graph_fops = {
2123 .open = ftrace_graph_open,
2125 .write = ftrace_graph_write,
2127 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2129 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2131 struct dentry *entry;
2133 entry = debugfs_create_file("available_filter_functions", 0444,
2134 d_tracer, NULL, &ftrace_avail_fops);
2136 pr_warning("Could not create debugfs "
2137 "'available_filter_functions' entry\n");
2139 entry = debugfs_create_file("failures", 0444,
2140 d_tracer, NULL, &ftrace_failures_fops);
2142 pr_warning("Could not create debugfs 'failures' entry\n");
2144 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2145 NULL, &ftrace_filter_fops);
2147 pr_warning("Could not create debugfs "
2148 "'set_ftrace_filter' entry\n");
2150 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2151 NULL, &ftrace_notrace_fops);
2153 pr_warning("Could not create debugfs "
2154 "'set_ftrace_notrace' entry\n");
2156 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2157 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2159 &ftrace_graph_fops);
2161 pr_warning("Could not create debugfs "
2162 "'set_graph_function' entry\n");
2163 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2168 static int ftrace_convert_nops(struct module *mod,
2169 unsigned long *start,
2174 unsigned long flags;
2176 mutex_lock(&ftrace_lock);
2179 addr = ftrace_call_adjust(*p++);
2181 * Some architecture linkers will pad between
2182 * the different mcount_loc sections of different
2183 * object files to satisfy alignments.
2184 * Skip any NULL pointers.
2188 ftrace_record_ip(addr);
2191 /* disable interrupts to prevent kstop machine */
2192 local_irq_save(flags);
2193 ftrace_update_code(mod);
2194 local_irq_restore(flags);
2195 mutex_unlock(&ftrace_lock);
2200 void ftrace_init_module(struct module *mod,
2201 unsigned long *start, unsigned long *end)
2203 if (ftrace_disabled || start == end)
2205 ftrace_convert_nops(mod, start, end);
2208 extern unsigned long __start_mcount_loc[];
2209 extern unsigned long __stop_mcount_loc[];
2211 void __init ftrace_init(void)
2213 unsigned long count, addr, flags;
2216 /* Keep the ftrace pointer to the stub */
2217 addr = (unsigned long)ftrace_stub;
2219 local_irq_save(flags);
2220 ftrace_dyn_arch_init(&addr);
2221 local_irq_restore(flags);
2223 /* ftrace_dyn_arch_init places the return code in addr */
2227 count = __stop_mcount_loc - __start_mcount_loc;
2229 ret = ftrace_dyn_table_alloc(count);
2233 last_ftrace_enabled = ftrace_enabled = 1;
2235 ret = ftrace_convert_nops(NULL,
2241 ftrace_disabled = 1;
2246 static int __init ftrace_nodyn_init(void)
2251 device_initcall(ftrace_nodyn_init);
2253 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2254 static inline void ftrace_startup_enable(int command) { }
2255 /* Keep as macros so we do not need to define the commands */
2256 # define ftrace_startup(command) do { } while (0)
2257 # define ftrace_shutdown(command) do { } while (0)
2258 # define ftrace_startup_sysctl() do { } while (0)
2259 # define ftrace_shutdown_sysctl() do { } while (0)
2260 #endif /* CONFIG_DYNAMIC_FTRACE */
2263 ftrace_pid_read(struct file *file, char __user *ubuf,
2264 size_t cnt, loff_t *ppos)
2269 if (ftrace_pid_trace == ftrace_swapper_pid)
2270 r = sprintf(buf, "swapper tasks\n");
2271 else if (ftrace_pid_trace)
2272 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2274 r = sprintf(buf, "no pid\n");
2276 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2279 static void clear_ftrace_swapper(void)
2281 struct task_struct *p;
2285 for_each_online_cpu(cpu) {
2287 clear_tsk_trace_trace(p);
2292 static void set_ftrace_swapper(void)
2294 struct task_struct *p;
2298 for_each_online_cpu(cpu) {
2300 set_tsk_trace_trace(p);
2305 static void clear_ftrace_pid(struct pid *pid)
2307 struct task_struct *p;
2310 do_each_pid_task(pid, PIDTYPE_PID, p) {
2311 clear_tsk_trace_trace(p);
2312 } while_each_pid_task(pid, PIDTYPE_PID, p);
2318 static void set_ftrace_pid(struct pid *pid)
2320 struct task_struct *p;
2323 do_each_pid_task(pid, PIDTYPE_PID, p) {
2324 set_tsk_trace_trace(p);
2325 } while_each_pid_task(pid, PIDTYPE_PID, p);
2329 static void clear_ftrace_pid_task(struct pid **pid)
2331 if (*pid == ftrace_swapper_pid)
2332 clear_ftrace_swapper();
2334 clear_ftrace_pid(*pid);
2339 static void set_ftrace_pid_task(struct pid *pid)
2341 if (pid == ftrace_swapper_pid)
2342 set_ftrace_swapper();
2344 set_ftrace_pid(pid);
2348 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2349 size_t cnt, loff_t *ppos)
2356 if (cnt >= sizeof(buf))
2359 if (copy_from_user(&buf, ubuf, cnt))
2364 ret = strict_strtol(buf, 10, &val);
2368 mutex_lock(&ftrace_lock);
2370 /* disable pid tracing */
2371 if (!ftrace_pid_trace)
2374 clear_ftrace_pid_task(&ftrace_pid_trace);
2377 /* swapper task is special */
2379 pid = ftrace_swapper_pid;
2380 if (pid == ftrace_pid_trace)
2383 pid = find_get_pid(val);
2385 if (pid == ftrace_pid_trace) {
2391 if (ftrace_pid_trace)
2392 clear_ftrace_pid_task(&ftrace_pid_trace);
2397 ftrace_pid_trace = pid;
2399 set_ftrace_pid_task(ftrace_pid_trace);
2402 /* update the function call */
2403 ftrace_update_pid_func();
2404 ftrace_startup_enable(0);
2407 mutex_unlock(&ftrace_lock);
2412 static const struct file_operations ftrace_pid_fops = {
2413 .read = ftrace_pid_read,
2414 .write = ftrace_pid_write,
2417 static __init int ftrace_init_debugfs(void)
2419 struct dentry *d_tracer;
2420 struct dentry *entry;
2422 d_tracer = tracing_init_dentry();
2426 ftrace_init_dyn_debugfs(d_tracer);
2428 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2429 NULL, &ftrace_pid_fops);
2431 pr_warning("Could not create debugfs "
2432 "'set_ftrace_pid' entry\n");
2435 fs_initcall(ftrace_init_debugfs);
2438 * ftrace_kill - kill ftrace
2440 * This function should be used by panic code. It stops ftrace
2441 * but in a not so nice way. If you need to simply kill ftrace
2442 * from a non-atomic section, use ftrace_kill.
2444 void ftrace_kill(void)
2446 ftrace_disabled = 1;
2448 clear_ftrace_function();
2452 * register_ftrace_function - register a function for profiling
2453 * @ops - ops structure that holds the function for profiling.
2455 * Register a function to be called by all functions in the
2458 * Note: @ops->func and all the functions it calls must be labeled
2459 * with "notrace", otherwise it will go into a
2462 int register_ftrace_function(struct ftrace_ops *ops)
2466 if (unlikely(ftrace_disabled))
2469 mutex_lock(&ftrace_lock);
2471 ret = __register_ftrace_function(ops);
2474 mutex_unlock(&ftrace_lock);
2479 * unregister_ftrace_function - unregister a function for profiling.
2480 * @ops - ops structure that holds the function to unregister
2482 * Unregister a function that was added to be called by ftrace profiling.
2484 int unregister_ftrace_function(struct ftrace_ops *ops)
2488 mutex_lock(&ftrace_lock);
2489 ret = __unregister_ftrace_function(ops);
2491 mutex_unlock(&ftrace_lock);
2497 ftrace_enable_sysctl(struct ctl_table *table, int write,
2498 struct file *file, void __user *buffer, size_t *lenp,
2503 if (unlikely(ftrace_disabled))
2506 mutex_lock(&ftrace_lock);
2508 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
2510 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2513 last_ftrace_enabled = ftrace_enabled;
2515 if (ftrace_enabled) {
2517 ftrace_startup_sysctl();
2519 /* we are starting ftrace again */
2520 if (ftrace_list != &ftrace_list_end) {
2521 if (ftrace_list->next == &ftrace_list_end)
2522 ftrace_trace_function = ftrace_list->func;
2524 ftrace_trace_function = ftrace_list_func;
2528 /* stopping ftrace calls (just send to ftrace_stub) */
2529 ftrace_trace_function = ftrace_stub;
2531 ftrace_shutdown_sysctl();
2535 mutex_unlock(&ftrace_lock);
2539 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2541 static atomic_t ftrace_graph_active;
2542 static struct notifier_block ftrace_suspend_notifier;
2544 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2549 /* The callbacks that hook a function */
2550 trace_func_graph_ret_t ftrace_graph_return =
2551 (trace_func_graph_ret_t)ftrace_stub;
2552 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
2554 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2555 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2559 unsigned long flags;
2560 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2561 struct task_struct *g, *t;
2563 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2564 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2565 * sizeof(struct ftrace_ret_stack),
2567 if (!ret_stack_list[i]) {
2575 read_lock_irqsave(&tasklist_lock, flags);
2576 do_each_thread(g, t) {
2582 if (t->ret_stack == NULL) {
2583 t->curr_ret_stack = -1;
2584 /* Make sure IRQs see the -1 first: */
2586 t->ret_stack = ret_stack_list[start++];
2587 atomic_set(&t->tracing_graph_pause, 0);
2588 atomic_set(&t->trace_overrun, 0);
2590 } while_each_thread(g, t);
2593 read_unlock_irqrestore(&tasklist_lock, flags);
2595 for (i = start; i < end; i++)
2596 kfree(ret_stack_list[i]);
2601 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
2602 struct task_struct *next)
2604 unsigned long long timestamp;
2608 * Does the user want to count the time a function was asleep.
2609 * If so, do not update the time stamps.
2611 if (trace_flags & TRACE_ITER_SLEEP_TIME)
2614 timestamp = trace_clock_local();
2616 prev->ftrace_timestamp = timestamp;
2618 /* only process tasks that we timestamped */
2619 if (!next->ftrace_timestamp)
2623 * Update all the counters in next to make up for the
2624 * time next was sleeping.
2626 timestamp -= next->ftrace_timestamp;
2628 for (index = next->curr_ret_stack; index >= 0; index--)
2629 next->ret_stack[index].calltime += timestamp;
2632 /* Allocate a return stack for each task */
2633 static int start_graph_tracing(void)
2635 struct ftrace_ret_stack **ret_stack_list;
2638 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2639 sizeof(struct ftrace_ret_stack *),
2642 if (!ret_stack_list)
2645 /* The cpu_boot init_task->ret_stack will never be freed */
2646 for_each_online_cpu(cpu)
2647 ftrace_graph_init_task(idle_task(cpu));
2650 ret = alloc_retstack_tasklist(ret_stack_list);
2651 } while (ret == -EAGAIN);
2654 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
2656 pr_info("ftrace_graph: Couldn't activate tracepoint"
2657 " probe to kernel_sched_switch\n");
2660 kfree(ret_stack_list);
2665 * Hibernation protection.
2666 * The state of the current task is too much unstable during
2667 * suspend/restore to disk. We want to protect against that.
2670 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2674 case PM_HIBERNATION_PREPARE:
2675 pause_graph_tracing();
2678 case PM_POST_HIBERNATION:
2679 unpause_graph_tracing();
2685 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2686 trace_func_graph_ent_t entryfunc)
2690 mutex_lock(&ftrace_lock);
2692 /* we currently allow only one tracer registered at a time */
2693 if (atomic_read(&ftrace_graph_active)) {
2698 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2699 register_pm_notifier(&ftrace_suspend_notifier);
2701 atomic_inc(&ftrace_graph_active);
2702 ret = start_graph_tracing();
2704 atomic_dec(&ftrace_graph_active);
2708 ftrace_graph_return = retfunc;
2709 ftrace_graph_entry = entryfunc;
2711 ftrace_startup(FTRACE_START_FUNC_RET);
2714 mutex_unlock(&ftrace_lock);
2718 void unregister_ftrace_graph(void)
2720 mutex_lock(&ftrace_lock);
2722 if (!unlikely(atomic_read(&ftrace_graph_active)))
2725 atomic_dec(&ftrace_graph_active);
2726 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
2727 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
2728 ftrace_graph_entry = ftrace_graph_entry_stub;
2729 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
2730 unregister_pm_notifier(&ftrace_suspend_notifier);
2733 mutex_unlock(&ftrace_lock);
2736 /* Allocate a return stack for newly created task */
2737 void ftrace_graph_init_task(struct task_struct *t)
2739 if (atomic_read(&ftrace_graph_active)) {
2740 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2741 * sizeof(struct ftrace_ret_stack),
2745 t->curr_ret_stack = -1;
2746 atomic_set(&t->tracing_graph_pause, 0);
2747 atomic_set(&t->trace_overrun, 0);
2748 t->ftrace_timestamp = 0;
2750 t->ret_stack = NULL;
2753 void ftrace_graph_exit_task(struct task_struct *t)
2755 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2757 t->ret_stack = NULL;
2758 /* NULL must become visible to IRQs before we free it: */
2764 void ftrace_graph_stop(void)