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 mutex_lock(&ftrace_lock);
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 mutex_unlock(&ftrace_lock);
246 /* set when tracing only a pid */
247 struct pid *ftrace_pid_trace;
248 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
250 #ifdef CONFIG_DYNAMIC_FTRACE
252 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
253 # error Dynamic ftrace depends on MCOUNT_RECORD
256 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
258 struct ftrace_func_probe {
259 struct hlist_node node;
260 struct ftrace_probe_ops *ops;
269 FTRACE_ENABLE_CALLS = (1 << 0),
270 FTRACE_DISABLE_CALLS = (1 << 1),
271 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
272 FTRACE_ENABLE_MCOUNT = (1 << 3),
273 FTRACE_DISABLE_MCOUNT = (1 << 4),
274 FTRACE_START_FUNC_RET = (1 << 5),
275 FTRACE_STOP_FUNC_RET = (1 << 6),
278 static int ftrace_filtered;
280 static LIST_HEAD(ftrace_new_addrs);
282 static DEFINE_MUTEX(ftrace_regex_lock);
285 struct ftrace_page *next;
287 struct dyn_ftrace records[];
290 #define ENTRIES_PER_PAGE \
291 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
293 /* estimate from running different kernels */
294 #define NR_TO_INIT 10000
296 static struct ftrace_page *ftrace_pages_start;
297 static struct ftrace_page *ftrace_pages;
299 static struct dyn_ftrace *ftrace_free_records;
302 * This is a double for. Do not use 'break' to break out of the loop,
303 * you must use a goto.
305 #define do_for_each_ftrace_rec(pg, rec) \
306 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
308 for (_____i = 0; _____i < pg->index; _____i++) { \
309 rec = &pg->records[_____i];
311 #define while_for_each_ftrace_rec() \
315 #ifdef CONFIG_KPROBES
317 static int frozen_record_count;
319 static inline void freeze_record(struct dyn_ftrace *rec)
321 if (!(rec->flags & FTRACE_FL_FROZEN)) {
322 rec->flags |= FTRACE_FL_FROZEN;
323 frozen_record_count++;
327 static inline void unfreeze_record(struct dyn_ftrace *rec)
329 if (rec->flags & FTRACE_FL_FROZEN) {
330 rec->flags &= ~FTRACE_FL_FROZEN;
331 frozen_record_count--;
335 static inline int record_frozen(struct dyn_ftrace *rec)
337 return rec->flags & FTRACE_FL_FROZEN;
340 # define freeze_record(rec) ({ 0; })
341 # define unfreeze_record(rec) ({ 0; })
342 # define record_frozen(rec) ({ 0; })
343 #endif /* CONFIG_KPROBES */
345 static void ftrace_free_rec(struct dyn_ftrace *rec)
347 rec->ip = (unsigned long)ftrace_free_records;
348 ftrace_free_records = rec;
349 rec->flags |= FTRACE_FL_FREE;
352 void ftrace_release(void *start, unsigned long size)
354 struct dyn_ftrace *rec;
355 struct ftrace_page *pg;
356 unsigned long s = (unsigned long)start;
357 unsigned long e = s + size;
359 if (ftrace_disabled || !start)
362 mutex_lock(&ftrace_lock);
363 do_for_each_ftrace_rec(pg, rec) {
364 if ((rec->ip >= s) && (rec->ip < e))
365 ftrace_free_rec(rec);
366 } while_for_each_ftrace_rec();
367 mutex_unlock(&ftrace_lock);
370 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
372 struct dyn_ftrace *rec;
374 /* First check for freed records */
375 if (ftrace_free_records) {
376 rec = ftrace_free_records;
378 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
379 FTRACE_WARN_ON_ONCE(1);
380 ftrace_free_records = NULL;
384 ftrace_free_records = (void *)rec->ip;
385 memset(rec, 0, sizeof(*rec));
389 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
390 if (!ftrace_pages->next) {
391 /* allocate another page */
393 (void *)get_zeroed_page(GFP_KERNEL);
394 if (!ftrace_pages->next)
397 ftrace_pages = ftrace_pages->next;
400 return &ftrace_pages->records[ftrace_pages->index++];
403 static struct dyn_ftrace *
404 ftrace_record_ip(unsigned long ip)
406 struct dyn_ftrace *rec;
411 rec = ftrace_alloc_dyn_node(ip);
417 list_add(&rec->list, &ftrace_new_addrs);
422 static void print_ip_ins(const char *fmt, unsigned char *p)
426 printk(KERN_CONT "%s", fmt);
428 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
429 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
432 static void ftrace_bug(int failed, unsigned long ip)
436 FTRACE_WARN_ON_ONCE(1);
437 pr_info("ftrace faulted on modifying ");
441 FTRACE_WARN_ON_ONCE(1);
442 pr_info("ftrace failed to modify ");
444 print_ip_ins(" actual: ", (unsigned char *)ip);
445 printk(KERN_CONT "\n");
448 FTRACE_WARN_ON_ONCE(1);
449 pr_info("ftrace faulted on writing ");
453 FTRACE_WARN_ON_ONCE(1);
454 pr_info("ftrace faulted on unknown error ");
461 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
463 unsigned long ftrace_addr;
464 unsigned long ip, fl;
466 ftrace_addr = (unsigned long)FTRACE_ADDR;
471 * If this record is not to be traced and
472 * it is not enabled then do nothing.
474 * If this record is not to be traced and
475 * it is enabled then disable it.
478 if (rec->flags & FTRACE_FL_NOTRACE) {
479 if (rec->flags & FTRACE_FL_ENABLED)
480 rec->flags &= ~FTRACE_FL_ENABLED;
484 } else if (ftrace_filtered && enable) {
489 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
491 /* Record is filtered and enabled, do nothing */
492 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
495 /* Record is not filtered or enabled, do nothing */
499 /* Record is not filtered but enabled, disable it */
500 if (fl == FTRACE_FL_ENABLED)
501 rec->flags &= ~FTRACE_FL_ENABLED;
503 /* Otherwise record is filtered but not enabled, enable it */
504 rec->flags |= FTRACE_FL_ENABLED;
506 /* Disable or not filtered */
509 /* if record is enabled, do nothing */
510 if (rec->flags & FTRACE_FL_ENABLED)
513 rec->flags |= FTRACE_FL_ENABLED;
517 /* if record is not enabled, do nothing */
518 if (!(rec->flags & FTRACE_FL_ENABLED))
521 rec->flags &= ~FTRACE_FL_ENABLED;
525 if (rec->flags & FTRACE_FL_ENABLED)
526 return ftrace_make_call(rec, ftrace_addr);
528 return ftrace_make_nop(NULL, rec, ftrace_addr);
531 static void ftrace_replace_code(int enable)
533 struct dyn_ftrace *rec;
534 struct ftrace_page *pg;
537 do_for_each_ftrace_rec(pg, rec) {
539 * Skip over free records and records that have
542 if (rec->flags & FTRACE_FL_FREE ||
543 rec->flags & FTRACE_FL_FAILED)
546 /* ignore updates to this record's mcount site */
547 if (get_kprobe((void *)rec->ip)) {
551 unfreeze_record(rec);
554 failed = __ftrace_replace_code(rec, enable);
555 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
556 rec->flags |= FTRACE_FL_FAILED;
557 if ((system_state == SYSTEM_BOOTING) ||
558 !core_kernel_text(rec->ip)) {
559 ftrace_free_rec(rec);
561 ftrace_bug(failed, rec->ip);
562 /* Stop processing */
566 } while_for_each_ftrace_rec();
570 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
577 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
580 rec->flags |= FTRACE_FL_FAILED;
587 * archs can override this function if they must do something
588 * before the modifying code is performed.
590 int __weak ftrace_arch_code_modify_prepare(void)
596 * archs can override this function if they must do something
597 * after the modifying code is performed.
599 int __weak ftrace_arch_code_modify_post_process(void)
604 static int __ftrace_modify_code(void *data)
608 if (*command & FTRACE_ENABLE_CALLS)
609 ftrace_replace_code(1);
610 else if (*command & FTRACE_DISABLE_CALLS)
611 ftrace_replace_code(0);
613 if (*command & FTRACE_UPDATE_TRACE_FUNC)
614 ftrace_update_ftrace_func(ftrace_trace_function);
616 if (*command & FTRACE_START_FUNC_RET)
617 ftrace_enable_ftrace_graph_caller();
618 else if (*command & FTRACE_STOP_FUNC_RET)
619 ftrace_disable_ftrace_graph_caller();
624 static void ftrace_run_update_code(int command)
628 ret = ftrace_arch_code_modify_prepare();
633 stop_machine(__ftrace_modify_code, &command, NULL);
635 ret = ftrace_arch_code_modify_post_process();
639 static ftrace_func_t saved_ftrace_func;
640 static int ftrace_start_up;
642 static void ftrace_startup_enable(int command)
644 if (saved_ftrace_func != ftrace_trace_function) {
645 saved_ftrace_func = ftrace_trace_function;
646 command |= FTRACE_UPDATE_TRACE_FUNC;
649 if (!command || !ftrace_enabled)
652 ftrace_run_update_code(command);
655 static void ftrace_startup(int command)
657 if (unlikely(ftrace_disabled))
661 command |= FTRACE_ENABLE_CALLS;
663 ftrace_startup_enable(command);
666 static void ftrace_shutdown(int command)
668 if (unlikely(ftrace_disabled))
672 if (!ftrace_start_up)
673 command |= FTRACE_DISABLE_CALLS;
675 if (saved_ftrace_func != ftrace_trace_function) {
676 saved_ftrace_func = ftrace_trace_function;
677 command |= FTRACE_UPDATE_TRACE_FUNC;
680 if (!command || !ftrace_enabled)
683 ftrace_run_update_code(command);
686 static void ftrace_startup_sysctl(void)
688 int command = FTRACE_ENABLE_MCOUNT;
690 if (unlikely(ftrace_disabled))
693 /* Force update next time */
694 saved_ftrace_func = NULL;
695 /* ftrace_start_up is true if we want ftrace running */
697 command |= FTRACE_ENABLE_CALLS;
699 ftrace_run_update_code(command);
702 static void ftrace_shutdown_sysctl(void)
704 int command = FTRACE_DISABLE_MCOUNT;
706 if (unlikely(ftrace_disabled))
709 /* ftrace_start_up is true if ftrace is running */
711 command |= FTRACE_DISABLE_CALLS;
713 ftrace_run_update_code(command);
716 static cycle_t ftrace_update_time;
717 static unsigned long ftrace_update_cnt;
718 unsigned long ftrace_update_tot_cnt;
720 static int ftrace_update_code(struct module *mod)
722 struct dyn_ftrace *p, *t;
725 start = ftrace_now(raw_smp_processor_id());
726 ftrace_update_cnt = 0;
728 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
730 /* If something went wrong, bail without enabling anything */
731 if (unlikely(ftrace_disabled))
734 list_del_init(&p->list);
736 /* convert record (i.e, patch mcount-call with NOP) */
737 if (ftrace_code_disable(mod, p)) {
738 p->flags |= FTRACE_FL_CONVERTED;
744 stop = ftrace_now(raw_smp_processor_id());
745 ftrace_update_time = stop - start;
746 ftrace_update_tot_cnt += ftrace_update_cnt;
751 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
753 struct ftrace_page *pg;
757 /* allocate a few pages */
758 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
759 if (!ftrace_pages_start)
763 * Allocate a few more pages.
765 * TODO: have some parser search vmlinux before
766 * final linking to find all calls to ftrace.
768 * a) know how many pages to allocate.
770 * b) set up the table then.
772 * The dynamic code is still necessary for
776 pg = ftrace_pages = ftrace_pages_start;
778 cnt = num_to_init / ENTRIES_PER_PAGE;
779 pr_info("ftrace: allocating %ld entries in %d pages\n",
780 num_to_init, cnt + 1);
782 for (i = 0; i < cnt; i++) {
783 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
785 /* If we fail, we'll try later anyway */
796 FTRACE_ITER_FILTER = (1 << 0),
797 FTRACE_ITER_CONT = (1 << 1),
798 FTRACE_ITER_NOTRACE = (1 << 2),
799 FTRACE_ITER_FAILURES = (1 << 3),
800 FTRACE_ITER_PRINTALL = (1 << 4),
801 FTRACE_ITER_HASH = (1 << 5),
804 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
806 struct ftrace_iterator {
807 struct ftrace_page *pg;
811 unsigned char buffer[FTRACE_BUFF_MAX+1];
817 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
819 struct ftrace_iterator *iter = m->private;
820 struct hlist_node *hnd = v;
821 struct hlist_head *hhd;
823 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
828 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
831 hhd = &ftrace_func_hash[iter->hidx];
833 if (hlist_empty(hhd)) {
852 static void *t_hash_start(struct seq_file *m, loff_t *pos)
854 struct ftrace_iterator *iter = m->private;
857 iter->flags |= FTRACE_ITER_HASH;
859 return t_hash_next(m, p, pos);
862 static int t_hash_show(struct seq_file *m, void *v)
864 struct ftrace_func_probe *rec;
865 struct hlist_node *hnd = v;
866 char str[KSYM_SYMBOL_LEN];
868 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
871 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
873 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
874 seq_printf(m, "%s:", str);
876 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
877 seq_printf(m, "%s", str);
880 seq_printf(m, ":%p", rec->data);
887 t_next(struct seq_file *m, void *v, loff_t *pos)
889 struct ftrace_iterator *iter = m->private;
890 struct dyn_ftrace *rec = NULL;
892 if (iter->flags & FTRACE_ITER_HASH)
893 return t_hash_next(m, v, pos);
897 if (iter->flags & FTRACE_ITER_PRINTALL)
901 if (iter->idx >= iter->pg->index) {
902 if (iter->pg->next) {
903 iter->pg = iter->pg->next;
910 rec = &iter->pg->records[iter->idx++];
911 if ((rec->flags & FTRACE_FL_FREE) ||
913 (!(iter->flags & FTRACE_ITER_FAILURES) &&
914 (rec->flags & FTRACE_FL_FAILED)) ||
916 ((iter->flags & FTRACE_ITER_FAILURES) &&
917 !(rec->flags & FTRACE_FL_FAILED)) ||
919 ((iter->flags & FTRACE_ITER_FILTER) &&
920 !(rec->flags & FTRACE_FL_FILTER)) ||
922 ((iter->flags & FTRACE_ITER_NOTRACE) &&
923 !(rec->flags & FTRACE_FL_NOTRACE))) {
932 static void *t_start(struct seq_file *m, loff_t *pos)
934 struct ftrace_iterator *iter = m->private;
937 mutex_lock(&ftrace_lock);
939 * For set_ftrace_filter reading, if we have the filter
940 * off, we can short cut and just print out that all
941 * functions are enabled.
943 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
945 return t_hash_start(m, pos);
946 iter->flags |= FTRACE_ITER_PRINTALL;
951 if (iter->flags & FTRACE_ITER_HASH)
952 return t_hash_start(m, pos);
961 p = t_next(m, p, pos);
964 return t_hash_start(m, pos);
969 static void t_stop(struct seq_file *m, void *p)
971 mutex_unlock(&ftrace_lock);
974 static int t_show(struct seq_file *m, void *v)
976 struct ftrace_iterator *iter = m->private;
977 struct dyn_ftrace *rec = v;
978 char str[KSYM_SYMBOL_LEN];
980 if (iter->flags & FTRACE_ITER_HASH)
981 return t_hash_show(m, v);
983 if (iter->flags & FTRACE_ITER_PRINTALL) {
984 seq_printf(m, "#### all functions enabled ####\n");
991 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
993 seq_printf(m, "%s\n", str);
998 static struct seq_operations show_ftrace_seq_ops = {
1006 ftrace_avail_open(struct inode *inode, struct file *file)
1008 struct ftrace_iterator *iter;
1011 if (unlikely(ftrace_disabled))
1014 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1018 iter->pg = ftrace_pages_start;
1020 ret = seq_open(file, &show_ftrace_seq_ops);
1022 struct seq_file *m = file->private_data;
1032 int ftrace_avail_release(struct inode *inode, struct file *file)
1034 struct seq_file *m = (struct seq_file *)file->private_data;
1035 struct ftrace_iterator *iter = m->private;
1037 seq_release(inode, file);
1044 ftrace_failures_open(struct inode *inode, struct file *file)
1048 struct ftrace_iterator *iter;
1050 ret = ftrace_avail_open(inode, file);
1052 m = (struct seq_file *)file->private_data;
1053 iter = (struct ftrace_iterator *)m->private;
1054 iter->flags = FTRACE_ITER_FAILURES;
1061 static void ftrace_filter_reset(int enable)
1063 struct ftrace_page *pg;
1064 struct dyn_ftrace *rec;
1065 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1067 mutex_lock(&ftrace_lock);
1069 ftrace_filtered = 0;
1070 do_for_each_ftrace_rec(pg, rec) {
1071 if (rec->flags & FTRACE_FL_FAILED)
1073 rec->flags &= ~type;
1074 } while_for_each_ftrace_rec();
1075 mutex_unlock(&ftrace_lock);
1079 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1081 struct ftrace_iterator *iter;
1084 if (unlikely(ftrace_disabled))
1087 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1091 mutex_lock(&ftrace_regex_lock);
1092 if ((file->f_mode & FMODE_WRITE) &&
1093 !(file->f_flags & O_APPEND))
1094 ftrace_filter_reset(enable);
1096 if (file->f_mode & FMODE_READ) {
1097 iter->pg = ftrace_pages_start;
1098 iter->flags = enable ? FTRACE_ITER_FILTER :
1099 FTRACE_ITER_NOTRACE;
1101 ret = seq_open(file, &show_ftrace_seq_ops);
1103 struct seq_file *m = file->private_data;
1108 file->private_data = iter;
1109 mutex_unlock(&ftrace_regex_lock);
1115 ftrace_filter_open(struct inode *inode, struct file *file)
1117 return ftrace_regex_open(inode, file, 1);
1121 ftrace_notrace_open(struct inode *inode, struct file *file)
1123 return ftrace_regex_open(inode, file, 0);
1127 ftrace_regex_read(struct file *file, char __user *ubuf,
1128 size_t cnt, loff_t *ppos)
1130 if (file->f_mode & FMODE_READ)
1131 return seq_read(file, ubuf, cnt, ppos);
1137 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1141 if (file->f_mode & FMODE_READ)
1142 ret = seq_lseek(file, offset, origin);
1144 file->f_pos = ret = 1;
1157 * (static function - no need for kernel doc)
1159 * Pass in a buffer containing a glob and this function will
1160 * set search to point to the search part of the buffer and
1161 * return the type of search it is (see enum above).
1162 * This does modify buff.
1164 * Returns enum type.
1165 * search returns the pointer to use for comparison.
1166 * not returns 1 if buff started with a '!'
1170 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1172 int type = MATCH_FULL;
1175 if (buff[0] == '!') {
1184 for (i = 0; i < len; i++) {
1185 if (buff[i] == '*') {
1188 type = MATCH_END_ONLY;
1190 if (type == MATCH_END_ONLY)
1191 type = MATCH_MIDDLE_ONLY;
1193 type = MATCH_FRONT_ONLY;
1203 static int ftrace_match(char *str, char *regex, int len, int type)
1210 if (strcmp(str, regex) == 0)
1213 case MATCH_FRONT_ONLY:
1214 if (strncmp(str, regex, len) == 0)
1217 case MATCH_MIDDLE_ONLY:
1218 if (strstr(str, regex))
1221 case MATCH_END_ONLY:
1222 ptr = strstr(str, regex);
1223 if (ptr && (ptr[len] == 0))
1232 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1234 char str[KSYM_SYMBOL_LEN];
1236 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1237 return ftrace_match(str, regex, len, type);
1240 static void ftrace_match_records(char *buff, int len, int enable)
1242 unsigned int search_len;
1243 struct ftrace_page *pg;
1244 struct dyn_ftrace *rec;
1250 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1251 type = ftrace_setup_glob(buff, len, &search, ¬);
1253 search_len = strlen(search);
1255 mutex_lock(&ftrace_lock);
1256 do_for_each_ftrace_rec(pg, rec) {
1258 if (rec->flags & FTRACE_FL_FAILED)
1261 if (ftrace_match_record(rec, search, search_len, type)) {
1263 rec->flags &= ~flag;
1268 * Only enable filtering if we have a function that
1271 if (enable && (rec->flags & FTRACE_FL_FILTER))
1272 ftrace_filtered = 1;
1273 } while_for_each_ftrace_rec();
1274 mutex_unlock(&ftrace_lock);
1278 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1279 char *regex, int len, int type)
1281 char str[KSYM_SYMBOL_LEN];
1284 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1286 if (!modname || strcmp(modname, mod))
1289 /* blank search means to match all funcs in the mod */
1291 return ftrace_match(str, regex, len, type);
1296 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1298 unsigned search_len = 0;
1299 struct ftrace_page *pg;
1300 struct dyn_ftrace *rec;
1301 int type = MATCH_FULL;
1302 char *search = buff;
1306 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1308 /* blank or '*' mean the same */
1309 if (strcmp(buff, "*") == 0)
1312 /* handle the case of 'dont filter this module' */
1313 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1319 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1320 search_len = strlen(search);
1323 mutex_lock(&ftrace_lock);
1324 do_for_each_ftrace_rec(pg, rec) {
1326 if (rec->flags & FTRACE_FL_FAILED)
1329 if (ftrace_match_module_record(rec, mod,
1330 search, search_len, type)) {
1332 rec->flags &= ~flag;
1336 if (enable && (rec->flags & FTRACE_FL_FILTER))
1337 ftrace_filtered = 1;
1339 } while_for_each_ftrace_rec();
1340 mutex_unlock(&ftrace_lock);
1344 * We register the module command as a template to show others how
1345 * to register the a command as well.
1349 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1354 * cmd == 'mod' because we only registered this func
1355 * for the 'mod' ftrace_func_command.
1356 * But if you register one func with multiple commands,
1357 * you can tell which command was used by the cmd
1361 /* we must have a module name */
1365 mod = strsep(¶m, ":");
1369 ftrace_match_module_records(func, mod, enable);
1373 static struct ftrace_func_command ftrace_mod_cmd = {
1375 .func = ftrace_mod_callback,
1378 static int __init ftrace_mod_cmd_init(void)
1380 return register_ftrace_command(&ftrace_mod_cmd);
1382 device_initcall(ftrace_mod_cmd_init);
1385 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1387 struct ftrace_func_probe *entry;
1388 struct hlist_head *hhd;
1389 struct hlist_node *n;
1393 key = hash_long(ip, FTRACE_HASH_BITS);
1395 hhd = &ftrace_func_hash[key];
1397 if (hlist_empty(hhd))
1401 * Disable preemption for these calls to prevent a RCU grace
1402 * period. This syncs the hash iteration and freeing of items
1403 * on the hash. rcu_read_lock is too dangerous here.
1405 resched = ftrace_preempt_disable();
1406 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1407 if (entry->ip == ip)
1408 entry->ops->func(ip, parent_ip, &entry->data);
1410 ftrace_preempt_enable(resched);
1413 static struct ftrace_ops trace_probe_ops __read_mostly =
1415 .func = function_trace_probe_call,
1418 static int ftrace_probe_registered;
1420 static void __enable_ftrace_function_probe(void)
1424 if (ftrace_probe_registered)
1427 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1428 struct hlist_head *hhd = &ftrace_func_hash[i];
1432 /* Nothing registered? */
1433 if (i == FTRACE_FUNC_HASHSIZE)
1436 __register_ftrace_function(&trace_probe_ops);
1438 ftrace_probe_registered = 1;
1441 static void __disable_ftrace_function_probe(void)
1445 if (!ftrace_probe_registered)
1448 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1449 struct hlist_head *hhd = &ftrace_func_hash[i];
1454 /* no more funcs left */
1455 __unregister_ftrace_function(&trace_probe_ops);
1457 ftrace_probe_registered = 0;
1461 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1463 struct ftrace_func_probe *entry =
1464 container_of(rhp, struct ftrace_func_probe, rcu);
1466 if (entry->ops->free)
1467 entry->ops->free(&entry->data);
1473 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1476 struct ftrace_func_probe *entry;
1477 struct ftrace_page *pg;
1478 struct dyn_ftrace *rec;
1484 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1485 len = strlen(search);
1487 /* we do not support '!' for function probes */
1491 mutex_lock(&ftrace_lock);
1492 do_for_each_ftrace_rec(pg, rec) {
1494 if (rec->flags & FTRACE_FL_FAILED)
1497 if (!ftrace_match_record(rec, search, len, type))
1500 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1502 /* If we did not process any, then return error */
1513 * The caller might want to do something special
1514 * for each function we find. We call the callback
1515 * to give the caller an opportunity to do so.
1517 if (ops->callback) {
1518 if (ops->callback(rec->ip, &entry->data) < 0) {
1519 /* caller does not like this func */
1526 entry->ip = rec->ip;
1528 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1529 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1531 } while_for_each_ftrace_rec();
1532 __enable_ftrace_function_probe();
1535 mutex_unlock(&ftrace_lock);
1541 PROBE_TEST_FUNC = 1,
1546 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1547 void *data, int flags)
1549 struct ftrace_func_probe *entry;
1550 struct hlist_node *n, *tmp;
1551 char str[KSYM_SYMBOL_LEN];
1552 int type = MATCH_FULL;
1556 if (glob && (strcmp(glob, "*") || !strlen(glob)))
1561 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1562 len = strlen(search);
1564 /* we do not support '!' for function probes */
1569 mutex_lock(&ftrace_lock);
1570 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1571 struct hlist_head *hhd = &ftrace_func_hash[i];
1573 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1575 /* break up if statements for readability */
1576 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
1579 if ((flags & PROBE_TEST_DATA) && entry->data != data)
1582 /* do this last, since it is the most expensive */
1584 kallsyms_lookup(entry->ip, NULL, NULL,
1586 if (!ftrace_match(str, glob, len, type))
1590 hlist_del(&entry->node);
1591 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1594 __disable_ftrace_function_probe();
1595 mutex_unlock(&ftrace_lock);
1599 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1602 __unregister_ftrace_function_probe(glob, ops, data,
1603 PROBE_TEST_FUNC | PROBE_TEST_DATA);
1607 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
1609 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
1612 void unregister_ftrace_function_probe_all(char *glob)
1614 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
1617 static LIST_HEAD(ftrace_commands);
1618 static DEFINE_MUTEX(ftrace_cmd_mutex);
1620 int register_ftrace_command(struct ftrace_func_command *cmd)
1622 struct ftrace_func_command *p;
1625 mutex_lock(&ftrace_cmd_mutex);
1626 list_for_each_entry(p, &ftrace_commands, list) {
1627 if (strcmp(cmd->name, p->name) == 0) {
1632 list_add(&cmd->list, &ftrace_commands);
1634 mutex_unlock(&ftrace_cmd_mutex);
1639 int unregister_ftrace_command(struct ftrace_func_command *cmd)
1641 struct ftrace_func_command *p, *n;
1644 mutex_lock(&ftrace_cmd_mutex);
1645 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1646 if (strcmp(cmd->name, p->name) == 0) {
1648 list_del_init(&p->list);
1653 mutex_unlock(&ftrace_cmd_mutex);
1658 static int ftrace_process_regex(char *buff, int len, int enable)
1660 char *func, *command, *next = buff;
1661 struct ftrace_func_command *p;
1664 func = strsep(&next, ":");
1667 ftrace_match_records(func, len, enable);
1673 command = strsep(&next, ":");
1675 mutex_lock(&ftrace_cmd_mutex);
1676 list_for_each_entry(p, &ftrace_commands, list) {
1677 if (strcmp(p->name, command) == 0) {
1678 ret = p->func(func, command, next, enable);
1683 mutex_unlock(&ftrace_cmd_mutex);
1689 ftrace_regex_write(struct file *file, const char __user *ubuf,
1690 size_t cnt, loff_t *ppos, int enable)
1692 struct ftrace_iterator *iter;
1697 if (!cnt || cnt < 0)
1700 mutex_lock(&ftrace_regex_lock);
1702 if (file->f_mode & FMODE_READ) {
1703 struct seq_file *m = file->private_data;
1706 iter = file->private_data;
1709 iter->flags &= ~FTRACE_ITER_CONT;
1710 iter->buffer_idx = 0;
1713 ret = get_user(ch, ubuf++);
1719 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1720 /* skip white space */
1721 while (cnt && isspace(ch)) {
1722 ret = get_user(ch, ubuf++);
1730 file->f_pos += read;
1735 iter->buffer_idx = 0;
1738 while (cnt && !isspace(ch)) {
1739 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1740 iter->buffer[iter->buffer_idx++] = ch;
1745 ret = get_user(ch, ubuf++);
1754 iter->buffer[iter->buffer_idx] = 0;
1755 ret = ftrace_process_regex(iter->buffer,
1756 iter->buffer_idx, enable);
1759 iter->buffer_idx = 0;
1761 iter->flags |= FTRACE_ITER_CONT;
1764 file->f_pos += read;
1768 mutex_unlock(&ftrace_regex_lock);
1774 ftrace_filter_write(struct file *file, const char __user *ubuf,
1775 size_t cnt, loff_t *ppos)
1777 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1781 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1782 size_t cnt, loff_t *ppos)
1784 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1788 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1790 if (unlikely(ftrace_disabled))
1793 mutex_lock(&ftrace_regex_lock);
1795 ftrace_filter_reset(enable);
1797 ftrace_match_records(buf, len, enable);
1798 mutex_unlock(&ftrace_regex_lock);
1802 * ftrace_set_filter - set a function to filter on in ftrace
1803 * @buf - the string that holds the function filter text.
1804 * @len - the length of the string.
1805 * @reset - non zero to reset all filters before applying this filter.
1807 * Filters denote which functions should be enabled when tracing is enabled.
1808 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1810 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1812 ftrace_set_regex(buf, len, reset, 1);
1816 * ftrace_set_notrace - set a function to not trace in ftrace
1817 * @buf - the string that holds the function notrace text.
1818 * @len - the length of the string.
1819 * @reset - non zero to reset all filters before applying this filter.
1821 * Notrace Filters denote which functions should not be enabled when tracing
1822 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1825 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1827 ftrace_set_regex(buf, len, reset, 0);
1831 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1833 struct seq_file *m = (struct seq_file *)file->private_data;
1834 struct ftrace_iterator *iter;
1836 mutex_lock(&ftrace_regex_lock);
1837 if (file->f_mode & FMODE_READ) {
1840 seq_release(inode, file);
1842 iter = file->private_data;
1844 if (iter->buffer_idx) {
1846 iter->buffer[iter->buffer_idx] = 0;
1847 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
1850 mutex_lock(&ftrace_lock);
1851 if (ftrace_start_up && ftrace_enabled)
1852 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1853 mutex_unlock(&ftrace_lock);
1856 mutex_unlock(&ftrace_regex_lock);
1861 ftrace_filter_release(struct inode *inode, struct file *file)
1863 return ftrace_regex_release(inode, file, 1);
1867 ftrace_notrace_release(struct inode *inode, struct file *file)
1869 return ftrace_regex_release(inode, file, 0);
1872 static struct file_operations ftrace_avail_fops = {
1873 .open = ftrace_avail_open,
1875 .llseek = seq_lseek,
1876 .release = ftrace_avail_release,
1879 static struct file_operations ftrace_failures_fops = {
1880 .open = ftrace_failures_open,
1882 .llseek = seq_lseek,
1883 .release = ftrace_avail_release,
1886 static struct file_operations ftrace_filter_fops = {
1887 .open = ftrace_filter_open,
1888 .read = ftrace_regex_read,
1889 .write = ftrace_filter_write,
1890 .llseek = ftrace_regex_lseek,
1891 .release = ftrace_filter_release,
1894 static struct file_operations ftrace_notrace_fops = {
1895 .open = ftrace_notrace_open,
1896 .read = ftrace_regex_read,
1897 .write = ftrace_notrace_write,
1898 .llseek = ftrace_regex_lseek,
1899 .release = ftrace_notrace_release,
1902 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1904 static DEFINE_MUTEX(graph_lock);
1906 int ftrace_graph_count;
1907 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1910 g_next(struct seq_file *m, void *v, loff_t *pos)
1912 unsigned long *array = m->private;
1917 if (index >= ftrace_graph_count)
1920 return &array[index];
1923 static void *g_start(struct seq_file *m, loff_t *pos)
1927 mutex_lock(&graph_lock);
1929 /* Nothing, tell g_show to print all functions are enabled */
1930 if (!ftrace_graph_count && !*pos)
1933 p = g_next(m, p, pos);
1938 static void g_stop(struct seq_file *m, void *p)
1940 mutex_unlock(&graph_lock);
1943 static int g_show(struct seq_file *m, void *v)
1945 unsigned long *ptr = v;
1946 char str[KSYM_SYMBOL_LEN];
1951 if (ptr == (unsigned long *)1) {
1952 seq_printf(m, "#### all functions enabled ####\n");
1956 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1958 seq_printf(m, "%s\n", str);
1963 static struct seq_operations ftrace_graph_seq_ops = {
1971 ftrace_graph_open(struct inode *inode, struct file *file)
1975 if (unlikely(ftrace_disabled))
1978 mutex_lock(&graph_lock);
1979 if ((file->f_mode & FMODE_WRITE) &&
1980 !(file->f_flags & O_APPEND)) {
1981 ftrace_graph_count = 0;
1982 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1985 if (file->f_mode & FMODE_READ) {
1986 ret = seq_open(file, &ftrace_graph_seq_ops);
1988 struct seq_file *m = file->private_data;
1989 m->private = ftrace_graph_funcs;
1992 file->private_data = ftrace_graph_funcs;
1993 mutex_unlock(&graph_lock);
1999 ftrace_graph_read(struct file *file, char __user *ubuf,
2000 size_t cnt, loff_t *ppos)
2002 if (file->f_mode & FMODE_READ)
2003 return seq_read(file, ubuf, cnt, ppos);
2009 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2011 struct dyn_ftrace *rec;
2012 struct ftrace_page *pg;
2020 if (ftrace_disabled)
2024 type = ftrace_setup_glob(buffer, strlen(buffer), &search, ¬);
2028 search_len = strlen(search);
2030 mutex_lock(&ftrace_lock);
2031 do_for_each_ftrace_rec(pg, rec) {
2033 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2036 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2039 if (ftrace_match_record(rec, search, search_len, type)) {
2040 /* ensure it is not already in the array */
2042 for (i = 0; i < *idx; i++)
2043 if (array[i] == rec->ip) {
2048 array[(*idx)++] = rec->ip;
2052 } while_for_each_ftrace_rec();
2054 mutex_unlock(&ftrace_lock);
2056 return found ? 0 : -EINVAL;
2060 ftrace_graph_write(struct file *file, const char __user *ubuf,
2061 size_t cnt, loff_t *ppos)
2063 unsigned char buffer[FTRACE_BUFF_MAX+1];
2064 unsigned long *array;
2070 if (!cnt || cnt < 0)
2073 mutex_lock(&graph_lock);
2075 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2080 if (file->f_mode & FMODE_READ) {
2081 struct seq_file *m = file->private_data;
2084 array = file->private_data;
2086 ret = get_user(ch, ubuf++);
2092 /* skip white space */
2093 while (cnt && isspace(ch)) {
2094 ret = get_user(ch, ubuf++);
2107 while (cnt && !isspace(ch)) {
2108 if (index < FTRACE_BUFF_MAX)
2109 buffer[index++] = ch;
2114 ret = get_user(ch, ubuf++);
2122 /* we allow only one expression at a time */
2123 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
2127 file->f_pos += read;
2131 mutex_unlock(&graph_lock);
2136 static const struct file_operations ftrace_graph_fops = {
2137 .open = ftrace_graph_open,
2138 .read = ftrace_graph_read,
2139 .write = ftrace_graph_write,
2141 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2143 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2145 struct dentry *entry;
2147 entry = debugfs_create_file("available_filter_functions", 0444,
2148 d_tracer, NULL, &ftrace_avail_fops);
2150 pr_warning("Could not create debugfs "
2151 "'available_filter_functions' entry\n");
2153 entry = debugfs_create_file("failures", 0444,
2154 d_tracer, NULL, &ftrace_failures_fops);
2156 pr_warning("Could not create debugfs 'failures' entry\n");
2158 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2159 NULL, &ftrace_filter_fops);
2161 pr_warning("Could not create debugfs "
2162 "'set_ftrace_filter' entry\n");
2164 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2165 NULL, &ftrace_notrace_fops);
2167 pr_warning("Could not create debugfs "
2168 "'set_ftrace_notrace' entry\n");
2170 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2171 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2173 &ftrace_graph_fops);
2175 pr_warning("Could not create debugfs "
2176 "'set_graph_function' entry\n");
2177 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2182 static int ftrace_convert_nops(struct module *mod,
2183 unsigned long *start,
2188 unsigned long flags;
2190 mutex_lock(&ftrace_lock);
2193 addr = ftrace_call_adjust(*p++);
2195 * Some architecture linkers will pad between
2196 * the different mcount_loc sections of different
2197 * object files to satisfy alignments.
2198 * Skip any NULL pointers.
2202 ftrace_record_ip(addr);
2205 /* disable interrupts to prevent kstop machine */
2206 local_irq_save(flags);
2207 ftrace_update_code(mod);
2208 local_irq_restore(flags);
2209 mutex_unlock(&ftrace_lock);
2214 void ftrace_init_module(struct module *mod,
2215 unsigned long *start, unsigned long *end)
2217 if (ftrace_disabled || start == end)
2219 ftrace_convert_nops(mod, start, end);
2222 extern unsigned long __start_mcount_loc[];
2223 extern unsigned long __stop_mcount_loc[];
2225 void __init ftrace_init(void)
2227 unsigned long count, addr, flags;
2230 /* Keep the ftrace pointer to the stub */
2231 addr = (unsigned long)ftrace_stub;
2233 local_irq_save(flags);
2234 ftrace_dyn_arch_init(&addr);
2235 local_irq_restore(flags);
2237 /* ftrace_dyn_arch_init places the return code in addr */
2241 count = __stop_mcount_loc - __start_mcount_loc;
2243 ret = ftrace_dyn_table_alloc(count);
2247 last_ftrace_enabled = ftrace_enabled = 1;
2249 ret = ftrace_convert_nops(NULL,
2255 ftrace_disabled = 1;
2260 static int __init ftrace_nodyn_init(void)
2265 device_initcall(ftrace_nodyn_init);
2267 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2268 static inline void ftrace_startup_enable(int command) { }
2269 /* Keep as macros so we do not need to define the commands */
2270 # define ftrace_startup(command) do { } while (0)
2271 # define ftrace_shutdown(command) do { } while (0)
2272 # define ftrace_startup_sysctl() do { } while (0)
2273 # define ftrace_shutdown_sysctl() do { } while (0)
2274 #endif /* CONFIG_DYNAMIC_FTRACE */
2277 ftrace_pid_read(struct file *file, char __user *ubuf,
2278 size_t cnt, loff_t *ppos)
2283 if (ftrace_pid_trace == ftrace_swapper_pid)
2284 r = sprintf(buf, "swapper tasks\n");
2285 else if (ftrace_pid_trace)
2286 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
2288 r = sprintf(buf, "no pid\n");
2290 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2293 static void clear_ftrace_swapper(void)
2295 struct task_struct *p;
2299 for_each_online_cpu(cpu) {
2301 clear_tsk_trace_trace(p);
2306 static void set_ftrace_swapper(void)
2308 struct task_struct *p;
2312 for_each_online_cpu(cpu) {
2314 set_tsk_trace_trace(p);
2319 static void clear_ftrace_pid(struct pid *pid)
2321 struct task_struct *p;
2324 do_each_pid_task(pid, PIDTYPE_PID, p) {
2325 clear_tsk_trace_trace(p);
2326 } while_each_pid_task(pid, PIDTYPE_PID, p);
2332 static void set_ftrace_pid(struct pid *pid)
2334 struct task_struct *p;
2337 do_each_pid_task(pid, PIDTYPE_PID, p) {
2338 set_tsk_trace_trace(p);
2339 } while_each_pid_task(pid, PIDTYPE_PID, p);
2343 static void clear_ftrace_pid_task(struct pid **pid)
2345 if (*pid == ftrace_swapper_pid)
2346 clear_ftrace_swapper();
2348 clear_ftrace_pid(*pid);
2353 static void set_ftrace_pid_task(struct pid *pid)
2355 if (pid == ftrace_swapper_pid)
2356 set_ftrace_swapper();
2358 set_ftrace_pid(pid);
2362 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2363 size_t cnt, loff_t *ppos)
2370 if (cnt >= sizeof(buf))
2373 if (copy_from_user(&buf, ubuf, cnt))
2378 ret = strict_strtol(buf, 10, &val);
2382 mutex_lock(&ftrace_lock);
2384 /* disable pid tracing */
2385 if (!ftrace_pid_trace)
2388 clear_ftrace_pid_task(&ftrace_pid_trace);
2391 /* swapper task is special */
2393 pid = ftrace_swapper_pid;
2394 if (pid == ftrace_pid_trace)
2397 pid = find_get_pid(val);
2399 if (pid == ftrace_pid_trace) {
2405 if (ftrace_pid_trace)
2406 clear_ftrace_pid_task(&ftrace_pid_trace);
2411 ftrace_pid_trace = pid;
2413 set_ftrace_pid_task(ftrace_pid_trace);
2416 /* update the function call */
2417 ftrace_update_pid_func();
2418 ftrace_startup_enable(0);
2421 mutex_unlock(&ftrace_lock);
2426 static struct file_operations ftrace_pid_fops = {
2427 .read = ftrace_pid_read,
2428 .write = ftrace_pid_write,
2431 static __init int ftrace_init_debugfs(void)
2433 struct dentry *d_tracer;
2434 struct dentry *entry;
2436 d_tracer = tracing_init_dentry();
2440 ftrace_init_dyn_debugfs(d_tracer);
2442 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2443 NULL, &ftrace_pid_fops);
2445 pr_warning("Could not create debugfs "
2446 "'set_ftrace_pid' entry\n");
2449 fs_initcall(ftrace_init_debugfs);
2452 * ftrace_kill - kill ftrace
2454 * This function should be used by panic code. It stops ftrace
2455 * but in a not so nice way. If you need to simply kill ftrace
2456 * from a non-atomic section, use ftrace_kill.
2458 void ftrace_kill(void)
2460 ftrace_disabled = 1;
2462 clear_ftrace_function();
2466 * register_ftrace_function - register a function for profiling
2467 * @ops - ops structure that holds the function for profiling.
2469 * Register a function to be called by all functions in the
2472 * Note: @ops->func and all the functions it calls must be labeled
2473 * with "notrace", otherwise it will go into a
2476 int register_ftrace_function(struct ftrace_ops *ops)
2480 if (unlikely(ftrace_disabled))
2483 mutex_lock(&ftrace_lock);
2485 ret = __register_ftrace_function(ops);
2488 mutex_unlock(&ftrace_lock);
2493 * unregister_ftrace_function - unregister a function for profiling.
2494 * @ops - ops structure that holds the function to unregister
2496 * Unregister a function that was added to be called by ftrace profiling.
2498 int unregister_ftrace_function(struct ftrace_ops *ops)
2502 mutex_lock(&ftrace_lock);
2503 ret = __unregister_ftrace_function(ops);
2505 mutex_unlock(&ftrace_lock);
2511 ftrace_enable_sysctl(struct ctl_table *table, int write,
2512 struct file *file, void __user *buffer, size_t *lenp,
2517 if (unlikely(ftrace_disabled))
2520 mutex_lock(&ftrace_lock);
2522 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
2524 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2527 last_ftrace_enabled = ftrace_enabled;
2529 if (ftrace_enabled) {
2531 ftrace_startup_sysctl();
2533 /* we are starting ftrace again */
2534 if (ftrace_list != &ftrace_list_end) {
2535 if (ftrace_list->next == &ftrace_list_end)
2536 ftrace_trace_function = ftrace_list->func;
2538 ftrace_trace_function = ftrace_list_func;
2542 /* stopping ftrace calls (just send to ftrace_stub) */
2543 ftrace_trace_function = ftrace_stub;
2545 ftrace_shutdown_sysctl();
2549 mutex_unlock(&ftrace_lock);
2553 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2555 static atomic_t ftrace_graph_active;
2556 static struct notifier_block ftrace_suspend_notifier;
2558 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2563 /* The callbacks that hook a function */
2564 trace_func_graph_ret_t ftrace_graph_return =
2565 (trace_func_graph_ret_t)ftrace_stub;
2566 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
2568 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2569 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2573 unsigned long flags;
2574 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2575 struct task_struct *g, *t;
2577 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2578 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2579 * sizeof(struct ftrace_ret_stack),
2581 if (!ret_stack_list[i]) {
2589 read_lock_irqsave(&tasklist_lock, flags);
2590 do_each_thread(g, t) {
2596 if (t->ret_stack == NULL) {
2597 t->curr_ret_stack = -1;
2598 /* Make sure IRQs see the -1 first: */
2600 t->ret_stack = ret_stack_list[start++];
2601 atomic_set(&t->tracing_graph_pause, 0);
2602 atomic_set(&t->trace_overrun, 0);
2604 } while_each_thread(g, t);
2607 read_unlock_irqrestore(&tasklist_lock, flags);
2609 for (i = start; i < end; i++)
2610 kfree(ret_stack_list[i]);
2614 /* Allocate a return stack for each task */
2615 static int start_graph_tracing(void)
2617 struct ftrace_ret_stack **ret_stack_list;
2620 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2621 sizeof(struct ftrace_ret_stack *),
2624 if (!ret_stack_list)
2627 /* The cpu_boot init_task->ret_stack will never be freed */
2628 for_each_online_cpu(cpu)
2629 ftrace_graph_init_task(idle_task(cpu));
2632 ret = alloc_retstack_tasklist(ret_stack_list);
2633 } while (ret == -EAGAIN);
2635 kfree(ret_stack_list);
2640 * Hibernation protection.
2641 * The state of the current task is too much unstable during
2642 * suspend/restore to disk. We want to protect against that.
2645 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2649 case PM_HIBERNATION_PREPARE:
2650 pause_graph_tracing();
2653 case PM_POST_HIBERNATION:
2654 unpause_graph_tracing();
2660 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2661 trace_func_graph_ent_t entryfunc)
2665 mutex_lock(&ftrace_lock);
2667 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2668 register_pm_notifier(&ftrace_suspend_notifier);
2670 atomic_inc(&ftrace_graph_active);
2671 ret = start_graph_tracing();
2673 atomic_dec(&ftrace_graph_active);
2677 ftrace_graph_return = retfunc;
2678 ftrace_graph_entry = entryfunc;
2680 ftrace_startup(FTRACE_START_FUNC_RET);
2683 mutex_unlock(&ftrace_lock);
2687 void unregister_ftrace_graph(void)
2689 mutex_lock(&ftrace_lock);
2691 atomic_dec(&ftrace_graph_active);
2692 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
2693 ftrace_graph_entry = ftrace_graph_entry_stub;
2694 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
2695 unregister_pm_notifier(&ftrace_suspend_notifier);
2697 mutex_unlock(&ftrace_lock);
2700 /* Allocate a return stack for newly created task */
2701 void ftrace_graph_init_task(struct task_struct *t)
2703 if (atomic_read(&ftrace_graph_active)) {
2704 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2705 * sizeof(struct ftrace_ret_stack),
2709 t->curr_ret_stack = -1;
2710 atomic_set(&t->tracing_graph_pause, 0);
2711 atomic_set(&t->trace_overrun, 0);
2713 t->ret_stack = NULL;
2716 void ftrace_graph_exit_task(struct task_struct *t)
2718 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2720 t->ret_stack = NULL;
2721 /* NULL must become visible to IRQs before we free it: */
2727 void ftrace_graph_stop(void)