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);
563 } while_for_each_ftrace_rec();
567 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
574 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
577 rec->flags |= FTRACE_FL_FAILED;
583 static int __ftrace_modify_code(void *data)
587 if (*command & FTRACE_ENABLE_CALLS)
588 ftrace_replace_code(1);
589 else if (*command & FTRACE_DISABLE_CALLS)
590 ftrace_replace_code(0);
592 if (*command & FTRACE_UPDATE_TRACE_FUNC)
593 ftrace_update_ftrace_func(ftrace_trace_function);
595 if (*command & FTRACE_START_FUNC_RET)
596 ftrace_enable_ftrace_graph_caller();
597 else if (*command & FTRACE_STOP_FUNC_RET)
598 ftrace_disable_ftrace_graph_caller();
603 static void ftrace_run_update_code(int command)
605 stop_machine(__ftrace_modify_code, &command, NULL);
608 static ftrace_func_t saved_ftrace_func;
609 static int ftrace_start_up;
611 static void ftrace_startup_enable(int command)
613 if (saved_ftrace_func != ftrace_trace_function) {
614 saved_ftrace_func = ftrace_trace_function;
615 command |= FTRACE_UPDATE_TRACE_FUNC;
618 if (!command || !ftrace_enabled)
621 ftrace_run_update_code(command);
624 static void ftrace_startup(int command)
626 if (unlikely(ftrace_disabled))
630 command |= FTRACE_ENABLE_CALLS;
632 ftrace_startup_enable(command);
635 static void ftrace_shutdown(int command)
637 if (unlikely(ftrace_disabled))
641 if (!ftrace_start_up)
642 command |= FTRACE_DISABLE_CALLS;
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_sysctl(void)
657 int command = FTRACE_ENABLE_MCOUNT;
659 if (unlikely(ftrace_disabled))
662 /* Force update next time */
663 saved_ftrace_func = NULL;
664 /* ftrace_start_up is true if we want ftrace running */
666 command |= FTRACE_ENABLE_CALLS;
668 ftrace_run_update_code(command);
671 static void ftrace_shutdown_sysctl(void)
673 int command = FTRACE_DISABLE_MCOUNT;
675 if (unlikely(ftrace_disabled))
678 /* ftrace_start_up is true if ftrace is running */
680 command |= FTRACE_DISABLE_CALLS;
682 ftrace_run_update_code(command);
685 static cycle_t ftrace_update_time;
686 static unsigned long ftrace_update_cnt;
687 unsigned long ftrace_update_tot_cnt;
689 static int ftrace_update_code(struct module *mod)
691 struct dyn_ftrace *p, *t;
694 start = ftrace_now(raw_smp_processor_id());
695 ftrace_update_cnt = 0;
697 list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
699 /* If something went wrong, bail without enabling anything */
700 if (unlikely(ftrace_disabled))
703 list_del_init(&p->list);
705 /* convert record (i.e, patch mcount-call with NOP) */
706 if (ftrace_code_disable(mod, p)) {
707 p->flags |= FTRACE_FL_CONVERTED;
713 stop = ftrace_now(raw_smp_processor_id());
714 ftrace_update_time = stop - start;
715 ftrace_update_tot_cnt += ftrace_update_cnt;
720 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
722 struct ftrace_page *pg;
726 /* allocate a few pages */
727 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
728 if (!ftrace_pages_start)
732 * Allocate a few more pages.
734 * TODO: have some parser search vmlinux before
735 * final linking to find all calls to ftrace.
737 * a) know how many pages to allocate.
739 * b) set up the table then.
741 * The dynamic code is still necessary for
745 pg = ftrace_pages = ftrace_pages_start;
747 cnt = num_to_init / ENTRIES_PER_PAGE;
748 pr_info("ftrace: allocating %ld entries in %d pages\n",
749 num_to_init, cnt + 1);
751 for (i = 0; i < cnt; i++) {
752 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
754 /* If we fail, we'll try later anyway */
765 FTRACE_ITER_FILTER = (1 << 0),
766 FTRACE_ITER_CONT = (1 << 1),
767 FTRACE_ITER_NOTRACE = (1 << 2),
768 FTRACE_ITER_FAILURES = (1 << 3),
769 FTRACE_ITER_PRINTALL = (1 << 4),
770 FTRACE_ITER_HASH = (1 << 5),
773 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
775 struct ftrace_iterator {
776 struct ftrace_page *pg;
780 unsigned char buffer[FTRACE_BUFF_MAX+1];
786 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
788 struct ftrace_iterator *iter = m->private;
789 struct hlist_node *hnd = v;
790 struct hlist_head *hhd;
792 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
797 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
800 hhd = &ftrace_func_hash[iter->hidx];
802 if (hlist_empty(hhd)) {
821 static void *t_hash_start(struct seq_file *m, loff_t *pos)
823 struct ftrace_iterator *iter = m->private;
826 iter->flags |= FTRACE_ITER_HASH;
828 return t_hash_next(m, p, pos);
831 static int t_hash_show(struct seq_file *m, void *v)
833 struct ftrace_func_probe *rec;
834 struct hlist_node *hnd = v;
835 char str[KSYM_SYMBOL_LEN];
837 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
840 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
842 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
843 seq_printf(m, "%s:", str);
845 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
846 seq_printf(m, "%s", str);
849 seq_printf(m, ":%p", rec->data);
856 t_next(struct seq_file *m, void *v, loff_t *pos)
858 struct ftrace_iterator *iter = m->private;
859 struct dyn_ftrace *rec = NULL;
861 if (iter->flags & FTRACE_ITER_HASH)
862 return t_hash_next(m, v, pos);
866 if (iter->flags & FTRACE_ITER_PRINTALL)
870 if (iter->idx >= iter->pg->index) {
871 if (iter->pg->next) {
872 iter->pg = iter->pg->next;
879 rec = &iter->pg->records[iter->idx++];
880 if ((rec->flags & FTRACE_FL_FREE) ||
882 (!(iter->flags & FTRACE_ITER_FAILURES) &&
883 (rec->flags & FTRACE_FL_FAILED)) ||
885 ((iter->flags & FTRACE_ITER_FAILURES) &&
886 !(rec->flags & FTRACE_FL_FAILED)) ||
888 ((iter->flags & FTRACE_ITER_FILTER) &&
889 !(rec->flags & FTRACE_FL_FILTER)) ||
891 ((iter->flags & FTRACE_ITER_NOTRACE) &&
892 !(rec->flags & FTRACE_FL_NOTRACE))) {
901 static void *t_start(struct seq_file *m, loff_t *pos)
903 struct ftrace_iterator *iter = m->private;
906 mutex_lock(&ftrace_lock);
908 * For set_ftrace_filter reading, if we have the filter
909 * off, we can short cut and just print out that all
910 * functions are enabled.
912 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
914 return t_hash_start(m, pos);
915 iter->flags |= FTRACE_ITER_PRINTALL;
920 if (iter->flags & FTRACE_ITER_HASH)
921 return t_hash_start(m, pos);
930 p = t_next(m, p, pos);
933 return t_hash_start(m, pos);
938 static void t_stop(struct seq_file *m, void *p)
940 mutex_unlock(&ftrace_lock);
943 static int t_show(struct seq_file *m, void *v)
945 struct ftrace_iterator *iter = m->private;
946 struct dyn_ftrace *rec = v;
947 char str[KSYM_SYMBOL_LEN];
949 if (iter->flags & FTRACE_ITER_HASH)
950 return t_hash_show(m, v);
952 if (iter->flags & FTRACE_ITER_PRINTALL) {
953 seq_printf(m, "#### all functions enabled ####\n");
960 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
962 seq_printf(m, "%s\n", str);
967 static struct seq_operations show_ftrace_seq_ops = {
975 ftrace_avail_open(struct inode *inode, struct file *file)
977 struct ftrace_iterator *iter;
980 if (unlikely(ftrace_disabled))
983 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
987 iter->pg = ftrace_pages_start;
989 ret = seq_open(file, &show_ftrace_seq_ops);
991 struct seq_file *m = file->private_data;
1001 int ftrace_avail_release(struct inode *inode, struct file *file)
1003 struct seq_file *m = (struct seq_file *)file->private_data;
1004 struct ftrace_iterator *iter = m->private;
1006 seq_release(inode, file);
1013 ftrace_failures_open(struct inode *inode, struct file *file)
1017 struct ftrace_iterator *iter;
1019 ret = ftrace_avail_open(inode, file);
1021 m = (struct seq_file *)file->private_data;
1022 iter = (struct ftrace_iterator *)m->private;
1023 iter->flags = FTRACE_ITER_FAILURES;
1030 static void ftrace_filter_reset(int enable)
1032 struct ftrace_page *pg;
1033 struct dyn_ftrace *rec;
1034 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1036 mutex_lock(&ftrace_lock);
1038 ftrace_filtered = 0;
1039 do_for_each_ftrace_rec(pg, rec) {
1040 if (rec->flags & FTRACE_FL_FAILED)
1042 rec->flags &= ~type;
1043 } while_for_each_ftrace_rec();
1044 mutex_unlock(&ftrace_lock);
1048 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1050 struct ftrace_iterator *iter;
1053 if (unlikely(ftrace_disabled))
1056 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1060 mutex_lock(&ftrace_regex_lock);
1061 if ((file->f_mode & FMODE_WRITE) &&
1062 !(file->f_flags & O_APPEND))
1063 ftrace_filter_reset(enable);
1065 if (file->f_mode & FMODE_READ) {
1066 iter->pg = ftrace_pages_start;
1067 iter->flags = enable ? FTRACE_ITER_FILTER :
1068 FTRACE_ITER_NOTRACE;
1070 ret = seq_open(file, &show_ftrace_seq_ops);
1072 struct seq_file *m = file->private_data;
1077 file->private_data = iter;
1078 mutex_unlock(&ftrace_regex_lock);
1084 ftrace_filter_open(struct inode *inode, struct file *file)
1086 return ftrace_regex_open(inode, file, 1);
1090 ftrace_notrace_open(struct inode *inode, struct file *file)
1092 return ftrace_regex_open(inode, file, 0);
1096 ftrace_regex_read(struct file *file, char __user *ubuf,
1097 size_t cnt, loff_t *ppos)
1099 if (file->f_mode & FMODE_READ)
1100 return seq_read(file, ubuf, cnt, ppos);
1106 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1110 if (file->f_mode & FMODE_READ)
1111 ret = seq_lseek(file, offset, origin);
1113 file->f_pos = ret = 1;
1126 * (static function - no need for kernel doc)
1128 * Pass in a buffer containing a glob and this function will
1129 * set search to point to the search part of the buffer and
1130 * return the type of search it is (see enum above).
1131 * This does modify buff.
1133 * Returns enum type.
1134 * search returns the pointer to use for comparison.
1135 * not returns 1 if buff started with a '!'
1139 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1141 int type = MATCH_FULL;
1144 if (buff[0] == '!') {
1153 for (i = 0; i < len; i++) {
1154 if (buff[i] == '*') {
1157 type = MATCH_END_ONLY;
1159 if (type == MATCH_END_ONLY)
1160 type = MATCH_MIDDLE_ONLY;
1162 type = MATCH_FRONT_ONLY;
1172 static int ftrace_match(char *str, char *regex, int len, int type)
1179 if (strcmp(str, regex) == 0)
1182 case MATCH_FRONT_ONLY:
1183 if (strncmp(str, regex, len) == 0)
1186 case MATCH_MIDDLE_ONLY:
1187 if (strstr(str, regex))
1190 case MATCH_END_ONLY:
1191 ptr = strstr(str, regex);
1192 if (ptr && (ptr[len] == 0))
1201 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1203 char str[KSYM_SYMBOL_LEN];
1205 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1206 return ftrace_match(str, regex, len, type);
1209 static void ftrace_match_records(char *buff, int len, int enable)
1211 unsigned int search_len;
1212 struct ftrace_page *pg;
1213 struct dyn_ftrace *rec;
1219 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1220 type = ftrace_setup_glob(buff, len, &search, ¬);
1222 search_len = strlen(search);
1224 mutex_lock(&ftrace_lock);
1225 do_for_each_ftrace_rec(pg, rec) {
1227 if (rec->flags & FTRACE_FL_FAILED)
1230 if (ftrace_match_record(rec, search, search_len, type)) {
1232 rec->flags &= ~flag;
1237 * Only enable filtering if we have a function that
1240 if (enable && (rec->flags & FTRACE_FL_FILTER))
1241 ftrace_filtered = 1;
1242 } while_for_each_ftrace_rec();
1243 mutex_unlock(&ftrace_lock);
1247 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1248 char *regex, int len, int type)
1250 char str[KSYM_SYMBOL_LEN];
1253 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1255 if (!modname || strcmp(modname, mod))
1258 /* blank search means to match all funcs in the mod */
1260 return ftrace_match(str, regex, len, type);
1265 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1267 unsigned search_len = 0;
1268 struct ftrace_page *pg;
1269 struct dyn_ftrace *rec;
1270 int type = MATCH_FULL;
1271 char *search = buff;
1275 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1277 /* blank or '*' mean the same */
1278 if (strcmp(buff, "*") == 0)
1281 /* handle the case of 'dont filter this module' */
1282 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1288 type = ftrace_setup_glob(buff, strlen(buff), &search, ¬);
1289 search_len = strlen(search);
1292 mutex_lock(&ftrace_lock);
1293 do_for_each_ftrace_rec(pg, rec) {
1295 if (rec->flags & FTRACE_FL_FAILED)
1298 if (ftrace_match_module_record(rec, mod,
1299 search, search_len, type)) {
1301 rec->flags &= ~flag;
1305 if (enable && (rec->flags & FTRACE_FL_FILTER))
1306 ftrace_filtered = 1;
1308 } while_for_each_ftrace_rec();
1309 mutex_unlock(&ftrace_lock);
1313 * We register the module command as a template to show others how
1314 * to register the a command as well.
1318 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1323 * cmd == 'mod' because we only registered this func
1324 * for the 'mod' ftrace_func_command.
1325 * But if you register one func with multiple commands,
1326 * you can tell which command was used by the cmd
1330 /* we must have a module name */
1334 mod = strsep(¶m, ":");
1338 ftrace_match_module_records(func, mod, enable);
1342 static struct ftrace_func_command ftrace_mod_cmd = {
1344 .func = ftrace_mod_callback,
1347 static int __init ftrace_mod_cmd_init(void)
1349 return register_ftrace_command(&ftrace_mod_cmd);
1351 device_initcall(ftrace_mod_cmd_init);
1354 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1356 struct ftrace_func_probe *entry;
1357 struct hlist_head *hhd;
1358 struct hlist_node *n;
1362 key = hash_long(ip, FTRACE_HASH_BITS);
1364 hhd = &ftrace_func_hash[key];
1366 if (hlist_empty(hhd))
1370 * Disable preemption for these calls to prevent a RCU grace
1371 * period. This syncs the hash iteration and freeing of items
1372 * on the hash. rcu_read_lock is too dangerous here.
1374 resched = ftrace_preempt_disable();
1375 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1376 if (entry->ip == ip)
1377 entry->ops->func(ip, parent_ip, &entry->data);
1379 ftrace_preempt_enable(resched);
1382 static struct ftrace_ops trace_probe_ops __read_mostly =
1384 .func = function_trace_probe_call,
1387 static int ftrace_probe_registered;
1389 static void __enable_ftrace_function_probe(void)
1393 if (ftrace_probe_registered)
1396 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1397 struct hlist_head *hhd = &ftrace_func_hash[i];
1401 /* Nothing registered? */
1402 if (i == FTRACE_FUNC_HASHSIZE)
1405 __register_ftrace_function(&trace_probe_ops);
1407 ftrace_probe_registered = 1;
1410 static void __disable_ftrace_function_probe(void)
1414 if (!ftrace_probe_registered)
1417 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1418 struct hlist_head *hhd = &ftrace_func_hash[i];
1423 /* no more funcs left */
1424 __unregister_ftrace_function(&trace_probe_ops);
1426 ftrace_probe_registered = 0;
1430 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1432 struct ftrace_func_probe *entry =
1433 container_of(rhp, struct ftrace_func_probe, rcu);
1435 if (entry->ops->free)
1436 entry->ops->free(&entry->data);
1442 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1445 struct ftrace_func_probe *entry;
1446 struct ftrace_page *pg;
1447 struct dyn_ftrace *rec;
1453 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1454 len = strlen(search);
1456 /* we do not support '!' for function probes */
1460 mutex_lock(&ftrace_lock);
1461 do_for_each_ftrace_rec(pg, rec) {
1463 if (rec->flags & FTRACE_FL_FAILED)
1466 if (!ftrace_match_record(rec, search, len, type))
1469 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1471 /* If we did not process any, then return error */
1482 * The caller might want to do something special
1483 * for each function we find. We call the callback
1484 * to give the caller an opportunity to do so.
1486 if (ops->callback) {
1487 if (ops->callback(rec->ip, &entry->data) < 0) {
1488 /* caller does not like this func */
1495 entry->ip = rec->ip;
1497 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1498 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1500 } while_for_each_ftrace_rec();
1501 __enable_ftrace_function_probe();
1504 mutex_unlock(&ftrace_lock);
1510 PROBE_TEST_FUNC = 1,
1515 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1516 void *data, int flags)
1518 struct ftrace_func_probe *entry;
1519 struct hlist_node *n, *tmp;
1520 char str[KSYM_SYMBOL_LEN];
1521 int type = MATCH_FULL;
1525 if (glob && (strcmp(glob, "*") || !strlen(glob)))
1530 type = ftrace_setup_glob(glob, strlen(glob), &search, ¬);
1531 len = strlen(search);
1533 /* we do not support '!' for function probes */
1538 mutex_lock(&ftrace_lock);
1539 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1540 struct hlist_head *hhd = &ftrace_func_hash[i];
1542 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1544 /* break up if statements for readability */
1545 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
1548 if ((flags & PROBE_TEST_DATA) && entry->data != data)
1551 /* do this last, since it is the most expensive */
1553 kallsyms_lookup(entry->ip, NULL, NULL,
1555 if (!ftrace_match(str, glob, len, type))
1559 hlist_del(&entry->node);
1560 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1563 __disable_ftrace_function_probe();
1564 mutex_unlock(&ftrace_lock);
1568 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1571 __unregister_ftrace_function_probe(glob, ops, data,
1572 PROBE_TEST_FUNC | PROBE_TEST_DATA);
1576 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
1578 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
1581 void unregister_ftrace_function_probe_all(char *glob)
1583 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
1586 static LIST_HEAD(ftrace_commands);
1587 static DEFINE_MUTEX(ftrace_cmd_mutex);
1589 int register_ftrace_command(struct ftrace_func_command *cmd)
1591 struct ftrace_func_command *p;
1594 mutex_lock(&ftrace_cmd_mutex);
1595 list_for_each_entry(p, &ftrace_commands, list) {
1596 if (strcmp(cmd->name, p->name) == 0) {
1601 list_add(&cmd->list, &ftrace_commands);
1603 mutex_unlock(&ftrace_cmd_mutex);
1608 int unregister_ftrace_command(struct ftrace_func_command *cmd)
1610 struct ftrace_func_command *p, *n;
1613 mutex_lock(&ftrace_cmd_mutex);
1614 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1615 if (strcmp(cmd->name, p->name) == 0) {
1617 list_del_init(&p->list);
1622 mutex_unlock(&ftrace_cmd_mutex);
1627 static int ftrace_process_regex(char *buff, int len, int enable)
1629 char *func, *command, *next = buff;
1630 struct ftrace_func_command *p;
1633 func = strsep(&next, ":");
1636 ftrace_match_records(func, len, enable);
1642 command = strsep(&next, ":");
1644 mutex_lock(&ftrace_cmd_mutex);
1645 list_for_each_entry(p, &ftrace_commands, list) {
1646 if (strcmp(p->name, command) == 0) {
1647 ret = p->func(func, command, next, enable);
1652 mutex_unlock(&ftrace_cmd_mutex);
1658 ftrace_regex_write(struct file *file, const char __user *ubuf,
1659 size_t cnt, loff_t *ppos, int enable)
1661 struct ftrace_iterator *iter;
1666 if (!cnt || cnt < 0)
1669 mutex_lock(&ftrace_regex_lock);
1671 if (file->f_mode & FMODE_READ) {
1672 struct seq_file *m = file->private_data;
1675 iter = file->private_data;
1678 iter->flags &= ~FTRACE_ITER_CONT;
1679 iter->buffer_idx = 0;
1682 ret = get_user(ch, ubuf++);
1688 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1689 /* skip white space */
1690 while (cnt && isspace(ch)) {
1691 ret = get_user(ch, ubuf++);
1699 file->f_pos += read;
1704 iter->buffer_idx = 0;
1707 while (cnt && !isspace(ch)) {
1708 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1709 iter->buffer[iter->buffer_idx++] = ch;
1714 ret = get_user(ch, ubuf++);
1723 iter->buffer[iter->buffer_idx] = 0;
1724 ret = ftrace_process_regex(iter->buffer,
1725 iter->buffer_idx, enable);
1728 iter->buffer_idx = 0;
1730 iter->flags |= FTRACE_ITER_CONT;
1733 file->f_pos += read;
1737 mutex_unlock(&ftrace_regex_lock);
1743 ftrace_filter_write(struct file *file, const char __user *ubuf,
1744 size_t cnt, loff_t *ppos)
1746 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1750 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1751 size_t cnt, loff_t *ppos)
1753 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1757 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1759 if (unlikely(ftrace_disabled))
1762 mutex_lock(&ftrace_regex_lock);
1764 ftrace_filter_reset(enable);
1766 ftrace_match_records(buf, len, enable);
1767 mutex_unlock(&ftrace_regex_lock);
1771 * ftrace_set_filter - set a function to filter on in ftrace
1772 * @buf - the string that holds the function filter text.
1773 * @len - the length of the string.
1774 * @reset - non zero to reset all filters before applying this filter.
1776 * Filters denote which functions should be enabled when tracing is enabled.
1777 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1779 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1781 ftrace_set_regex(buf, len, reset, 1);
1785 * ftrace_set_notrace - set a function to not trace in ftrace
1786 * @buf - the string that holds the function notrace text.
1787 * @len - the length of the string.
1788 * @reset - non zero to reset all filters before applying this filter.
1790 * Notrace Filters denote which functions should not be enabled when tracing
1791 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1794 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1796 ftrace_set_regex(buf, len, reset, 0);
1800 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1802 struct seq_file *m = (struct seq_file *)file->private_data;
1803 struct ftrace_iterator *iter;
1805 mutex_lock(&ftrace_regex_lock);
1806 if (file->f_mode & FMODE_READ) {
1809 seq_release(inode, file);
1811 iter = file->private_data;
1813 if (iter->buffer_idx) {
1815 iter->buffer[iter->buffer_idx] = 0;
1816 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
1819 mutex_lock(&ftrace_lock);
1820 if (ftrace_start_up && ftrace_enabled)
1821 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1822 mutex_unlock(&ftrace_lock);
1825 mutex_unlock(&ftrace_regex_lock);
1830 ftrace_filter_release(struct inode *inode, struct file *file)
1832 return ftrace_regex_release(inode, file, 1);
1836 ftrace_notrace_release(struct inode *inode, struct file *file)
1838 return ftrace_regex_release(inode, file, 0);
1841 static struct file_operations ftrace_avail_fops = {
1842 .open = ftrace_avail_open,
1844 .llseek = seq_lseek,
1845 .release = ftrace_avail_release,
1848 static struct file_operations ftrace_failures_fops = {
1849 .open = ftrace_failures_open,
1851 .llseek = seq_lseek,
1852 .release = ftrace_avail_release,
1855 static struct file_operations ftrace_filter_fops = {
1856 .open = ftrace_filter_open,
1857 .read = ftrace_regex_read,
1858 .write = ftrace_filter_write,
1859 .llseek = ftrace_regex_lseek,
1860 .release = ftrace_filter_release,
1863 static struct file_operations ftrace_notrace_fops = {
1864 .open = ftrace_notrace_open,
1865 .read = ftrace_regex_read,
1866 .write = ftrace_notrace_write,
1867 .llseek = ftrace_regex_lseek,
1868 .release = ftrace_notrace_release,
1871 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1873 static DEFINE_MUTEX(graph_lock);
1875 int ftrace_graph_count;
1876 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1879 g_next(struct seq_file *m, void *v, loff_t *pos)
1881 unsigned long *array = m->private;
1886 if (index >= ftrace_graph_count)
1889 return &array[index];
1892 static void *g_start(struct seq_file *m, loff_t *pos)
1896 mutex_lock(&graph_lock);
1898 p = g_next(m, p, pos);
1903 static void g_stop(struct seq_file *m, void *p)
1905 mutex_unlock(&graph_lock);
1908 static int g_show(struct seq_file *m, void *v)
1910 unsigned long *ptr = v;
1911 char str[KSYM_SYMBOL_LEN];
1916 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1918 seq_printf(m, "%s\n", str);
1923 static struct seq_operations ftrace_graph_seq_ops = {
1931 ftrace_graph_open(struct inode *inode, struct file *file)
1935 if (unlikely(ftrace_disabled))
1938 mutex_lock(&graph_lock);
1939 if ((file->f_mode & FMODE_WRITE) &&
1940 !(file->f_flags & O_APPEND)) {
1941 ftrace_graph_count = 0;
1942 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1945 if (file->f_mode & FMODE_READ) {
1946 ret = seq_open(file, &ftrace_graph_seq_ops);
1948 struct seq_file *m = file->private_data;
1949 m->private = ftrace_graph_funcs;
1952 file->private_data = ftrace_graph_funcs;
1953 mutex_unlock(&graph_lock);
1959 ftrace_graph_read(struct file *file, char __user *ubuf,
1960 size_t cnt, loff_t *ppos)
1962 if (file->f_mode & FMODE_READ)
1963 return seq_read(file, ubuf, cnt, ppos);
1969 ftrace_set_func(unsigned long *array, int idx, char *buffer)
1971 char str[KSYM_SYMBOL_LEN];
1972 struct dyn_ftrace *rec;
1973 struct ftrace_page *pg;
1977 if (ftrace_disabled)
1980 mutex_lock(&ftrace_lock);
1981 do_for_each_ftrace_rec(pg, rec) {
1983 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
1986 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1987 if (strcmp(str, buffer) == 0) {
1988 /* Return 1 if we add it to the array */
1990 for (j = 0; j < idx; j++)
1991 if (array[j] == rec->ip) {
1996 array[idx] = rec->ip;
1999 } while_for_each_ftrace_rec();
2001 mutex_unlock(&ftrace_lock);
2003 return found ? 0 : -EINVAL;
2007 ftrace_graph_write(struct file *file, const char __user *ubuf,
2008 size_t cnt, loff_t *ppos)
2010 unsigned char buffer[FTRACE_BUFF_MAX+1];
2011 unsigned long *array;
2017 if (!cnt || cnt < 0)
2020 mutex_lock(&graph_lock);
2022 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2027 if (file->f_mode & FMODE_READ) {
2028 struct seq_file *m = file->private_data;
2031 array = file->private_data;
2033 ret = get_user(ch, ubuf++);
2039 /* skip white space */
2040 while (cnt && isspace(ch)) {
2041 ret = get_user(ch, ubuf++);
2054 while (cnt && !isspace(ch)) {
2055 if (index < FTRACE_BUFF_MAX)
2056 buffer[index++] = ch;
2061 ret = get_user(ch, ubuf++);
2069 /* we allow only one at a time */
2070 ret = ftrace_set_func(array, ftrace_graph_count, buffer);
2074 ftrace_graph_count++;
2076 file->f_pos += read;
2080 mutex_unlock(&graph_lock);
2085 static const struct file_operations ftrace_graph_fops = {
2086 .open = ftrace_graph_open,
2087 .read = ftrace_graph_read,
2088 .write = ftrace_graph_write,
2090 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2092 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2094 struct dentry *entry;
2096 entry = debugfs_create_file("available_filter_functions", 0444,
2097 d_tracer, NULL, &ftrace_avail_fops);
2099 pr_warning("Could not create debugfs "
2100 "'available_filter_functions' entry\n");
2102 entry = debugfs_create_file("failures", 0444,
2103 d_tracer, NULL, &ftrace_failures_fops);
2105 pr_warning("Could not create debugfs 'failures' entry\n");
2107 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2108 NULL, &ftrace_filter_fops);
2110 pr_warning("Could not create debugfs "
2111 "'set_ftrace_filter' entry\n");
2113 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2114 NULL, &ftrace_notrace_fops);
2116 pr_warning("Could not create debugfs "
2117 "'set_ftrace_notrace' entry\n");
2119 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2120 entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2122 &ftrace_graph_fops);
2124 pr_warning("Could not create debugfs "
2125 "'set_graph_function' entry\n");
2126 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2131 static int ftrace_convert_nops(struct module *mod,
2132 unsigned long *start,
2137 unsigned long flags;
2139 mutex_lock(&ftrace_lock);
2142 addr = ftrace_call_adjust(*p++);
2144 * Some architecture linkers will pad between
2145 * the different mcount_loc sections of different
2146 * object files to satisfy alignments.
2147 * Skip any NULL pointers.
2151 ftrace_record_ip(addr);
2154 /* disable interrupts to prevent kstop machine */
2155 local_irq_save(flags);
2156 ftrace_update_code(mod);
2157 local_irq_restore(flags);
2158 mutex_unlock(&ftrace_lock);
2163 void ftrace_init_module(struct module *mod,
2164 unsigned long *start, unsigned long *end)
2166 if (ftrace_disabled || start == end)
2168 ftrace_convert_nops(mod, start, end);
2171 extern unsigned long __start_mcount_loc[];
2172 extern unsigned long __stop_mcount_loc[];
2174 void __init ftrace_init(void)
2176 unsigned long count, addr, flags;
2179 /* Keep the ftrace pointer to the stub */
2180 addr = (unsigned long)ftrace_stub;
2182 local_irq_save(flags);
2183 ftrace_dyn_arch_init(&addr);
2184 local_irq_restore(flags);
2186 /* ftrace_dyn_arch_init places the return code in addr */
2190 count = __stop_mcount_loc - __start_mcount_loc;
2192 ret = ftrace_dyn_table_alloc(count);
2196 last_ftrace_enabled = ftrace_enabled = 1;
2198 ret = ftrace_convert_nops(NULL,
2204 ftrace_disabled = 1;
2209 static int __init ftrace_nodyn_init(void)
2214 device_initcall(ftrace_nodyn_init);
2216 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2217 static inline void ftrace_startup_enable(int command) { }
2218 /* Keep as macros so we do not need to define the commands */
2219 # define ftrace_startup(command) do { } while (0)
2220 # define ftrace_shutdown(command) do { } while (0)
2221 # define ftrace_startup_sysctl() do { } while (0)
2222 # define ftrace_shutdown_sysctl() do { } while (0)
2223 #endif /* CONFIG_DYNAMIC_FTRACE */
2226 ftrace_pid_read(struct file *file, char __user *ubuf,
2227 size_t cnt, loff_t *ppos)
2232 if (ftrace_pid_trace == ftrace_swapper_pid)
2233 r = sprintf(buf, "swapper tasks\n");
2234 else if (ftrace_pid_trace)
2235 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
2237 r = sprintf(buf, "no pid\n");
2239 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2242 static void clear_ftrace_swapper(void)
2244 struct task_struct *p;
2248 for_each_online_cpu(cpu) {
2250 clear_tsk_trace_trace(p);
2255 static void set_ftrace_swapper(void)
2257 struct task_struct *p;
2261 for_each_online_cpu(cpu) {
2263 set_tsk_trace_trace(p);
2268 static void clear_ftrace_pid(struct pid *pid)
2270 struct task_struct *p;
2273 do_each_pid_task(pid, PIDTYPE_PID, p) {
2274 clear_tsk_trace_trace(p);
2275 } while_each_pid_task(pid, PIDTYPE_PID, p);
2281 static void set_ftrace_pid(struct pid *pid)
2283 struct task_struct *p;
2286 do_each_pid_task(pid, PIDTYPE_PID, p) {
2287 set_tsk_trace_trace(p);
2288 } while_each_pid_task(pid, PIDTYPE_PID, p);
2292 static void clear_ftrace_pid_task(struct pid **pid)
2294 if (*pid == ftrace_swapper_pid)
2295 clear_ftrace_swapper();
2297 clear_ftrace_pid(*pid);
2302 static void set_ftrace_pid_task(struct pid *pid)
2304 if (pid == ftrace_swapper_pid)
2305 set_ftrace_swapper();
2307 set_ftrace_pid(pid);
2311 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2312 size_t cnt, loff_t *ppos)
2319 if (cnt >= sizeof(buf))
2322 if (copy_from_user(&buf, ubuf, cnt))
2327 ret = strict_strtol(buf, 10, &val);
2331 mutex_lock(&ftrace_lock);
2333 /* disable pid tracing */
2334 if (!ftrace_pid_trace)
2337 clear_ftrace_pid_task(&ftrace_pid_trace);
2340 /* swapper task is special */
2342 pid = ftrace_swapper_pid;
2343 if (pid == ftrace_pid_trace)
2346 pid = find_get_pid(val);
2348 if (pid == ftrace_pid_trace) {
2354 if (ftrace_pid_trace)
2355 clear_ftrace_pid_task(&ftrace_pid_trace);
2360 ftrace_pid_trace = pid;
2362 set_ftrace_pid_task(ftrace_pid_trace);
2365 /* update the function call */
2366 ftrace_update_pid_func();
2367 ftrace_startup_enable(0);
2370 mutex_unlock(&ftrace_lock);
2375 static struct file_operations ftrace_pid_fops = {
2376 .read = ftrace_pid_read,
2377 .write = ftrace_pid_write,
2380 static __init int ftrace_init_debugfs(void)
2382 struct dentry *d_tracer;
2383 struct dentry *entry;
2385 d_tracer = tracing_init_dentry();
2389 ftrace_init_dyn_debugfs(d_tracer);
2391 entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2392 NULL, &ftrace_pid_fops);
2394 pr_warning("Could not create debugfs "
2395 "'set_ftrace_pid' entry\n");
2398 fs_initcall(ftrace_init_debugfs);
2401 * ftrace_kill - kill ftrace
2403 * This function should be used by panic code. It stops ftrace
2404 * but in a not so nice way. If you need to simply kill ftrace
2405 * from a non-atomic section, use ftrace_kill.
2407 void ftrace_kill(void)
2409 ftrace_disabled = 1;
2411 clear_ftrace_function();
2415 * register_ftrace_function - register a function for profiling
2416 * @ops - ops structure that holds the function for profiling.
2418 * Register a function to be called by all functions in the
2421 * Note: @ops->func and all the functions it calls must be labeled
2422 * with "notrace", otherwise it will go into a
2425 int register_ftrace_function(struct ftrace_ops *ops)
2429 if (unlikely(ftrace_disabled))
2432 mutex_lock(&ftrace_lock);
2434 ret = __register_ftrace_function(ops);
2437 mutex_unlock(&ftrace_lock);
2442 * unregister_ftrace_function - unregister a function for profiling.
2443 * @ops - ops structure that holds the function to unregister
2445 * Unregister a function that was added to be called by ftrace profiling.
2447 int unregister_ftrace_function(struct ftrace_ops *ops)
2451 mutex_lock(&ftrace_lock);
2452 ret = __unregister_ftrace_function(ops);
2454 mutex_unlock(&ftrace_lock);
2460 ftrace_enable_sysctl(struct ctl_table *table, int write,
2461 struct file *file, void __user *buffer, size_t *lenp,
2466 if (unlikely(ftrace_disabled))
2469 mutex_lock(&ftrace_lock);
2471 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
2473 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2476 last_ftrace_enabled = ftrace_enabled;
2478 if (ftrace_enabled) {
2480 ftrace_startup_sysctl();
2482 /* we are starting ftrace again */
2483 if (ftrace_list != &ftrace_list_end) {
2484 if (ftrace_list->next == &ftrace_list_end)
2485 ftrace_trace_function = ftrace_list->func;
2487 ftrace_trace_function = ftrace_list_func;
2491 /* stopping ftrace calls (just send to ftrace_stub) */
2492 ftrace_trace_function = ftrace_stub;
2494 ftrace_shutdown_sysctl();
2498 mutex_unlock(&ftrace_lock);
2502 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2504 static atomic_t ftrace_graph_active;
2505 static struct notifier_block ftrace_suspend_notifier;
2507 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2512 /* The callbacks that hook a function */
2513 trace_func_graph_ret_t ftrace_graph_return =
2514 (trace_func_graph_ret_t)ftrace_stub;
2515 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
2517 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2518 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2522 unsigned long flags;
2523 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2524 struct task_struct *g, *t;
2526 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2527 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2528 * sizeof(struct ftrace_ret_stack),
2530 if (!ret_stack_list[i]) {
2538 read_lock_irqsave(&tasklist_lock, flags);
2539 do_each_thread(g, t) {
2545 if (t->ret_stack == NULL) {
2546 t->curr_ret_stack = -1;
2547 /* Make sure IRQs see the -1 first: */
2549 t->ret_stack = ret_stack_list[start++];
2550 atomic_set(&t->tracing_graph_pause, 0);
2551 atomic_set(&t->trace_overrun, 0);
2553 } while_each_thread(g, t);
2556 read_unlock_irqrestore(&tasklist_lock, flags);
2558 for (i = start; i < end; i++)
2559 kfree(ret_stack_list[i]);
2563 /* Allocate a return stack for each task */
2564 static int start_graph_tracing(void)
2566 struct ftrace_ret_stack **ret_stack_list;
2569 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2570 sizeof(struct ftrace_ret_stack *),
2573 if (!ret_stack_list)
2577 ret = alloc_retstack_tasklist(ret_stack_list);
2578 } while (ret == -EAGAIN);
2580 kfree(ret_stack_list);
2585 * Hibernation protection.
2586 * The state of the current task is too much unstable during
2587 * suspend/restore to disk. We want to protect against that.
2590 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2594 case PM_HIBERNATION_PREPARE:
2595 pause_graph_tracing();
2598 case PM_POST_HIBERNATION:
2599 unpause_graph_tracing();
2605 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2606 trace_func_graph_ent_t entryfunc)
2610 mutex_lock(&ftrace_lock);
2612 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2613 register_pm_notifier(&ftrace_suspend_notifier);
2615 atomic_inc(&ftrace_graph_active);
2616 ret = start_graph_tracing();
2618 atomic_dec(&ftrace_graph_active);
2622 ftrace_graph_return = retfunc;
2623 ftrace_graph_entry = entryfunc;
2625 ftrace_startup(FTRACE_START_FUNC_RET);
2628 mutex_unlock(&ftrace_lock);
2632 void unregister_ftrace_graph(void)
2634 mutex_lock(&ftrace_lock);
2636 atomic_dec(&ftrace_graph_active);
2637 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
2638 ftrace_graph_entry = ftrace_graph_entry_stub;
2639 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
2640 unregister_pm_notifier(&ftrace_suspend_notifier);
2642 mutex_unlock(&ftrace_lock);
2645 /* Allocate a return stack for newly created task */
2646 void ftrace_graph_init_task(struct task_struct *t)
2648 if (atomic_read(&ftrace_graph_active)) {
2649 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2650 * sizeof(struct ftrace_ret_stack),
2654 t->curr_ret_stack = -1;
2655 atomic_set(&t->tracing_graph_pause, 0);
2656 atomic_set(&t->trace_overrun, 0);
2658 t->ret_stack = NULL;
2661 void ftrace_graph_exit_task(struct task_struct *t)
2663 struct ftrace_ret_stack *ret_stack = t->ret_stack;
2665 t->ret_stack = NULL;
2666 /* NULL must become visible to IRQs before we free it: */
2672 void ftrace_graph_stop(void)