ftrace: user update and disable dynamic ftrace daemon
[linux-2.6/kvm.git] / kernel / trace / ftrace.c
blobf762f5a2d331705681b2170f823a9c842df95aad
1 /*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugfs.h>
21 #include <linux/hardirq.h>
22 #include <linux/kthread.h>
23 #include <linux/uaccess.h>
24 #include <linux/ftrace.h>
25 #include <linux/sysctl.h>
26 #include <linux/ctype.h>
27 #include <linux/hash.h>
28 #include <linux/list.h>
30 #include "trace.h"
32 /* ftrace_enabled is a method to turn ftrace on or off */
33 int ftrace_enabled __read_mostly;
34 static int last_ftrace_enabled;
37 * ftrace_disabled is set when an anomaly is discovered.
38 * ftrace_disabled is much stronger than ftrace_enabled.
40 static int ftrace_disabled __read_mostly;
42 static DEFINE_SPINLOCK(ftrace_lock);
43 static DEFINE_MUTEX(ftrace_sysctl_lock);
45 static struct ftrace_ops ftrace_list_end __read_mostly =
47 .func = ftrace_stub,
50 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
51 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
53 void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
55 struct ftrace_ops *op = ftrace_list;
57 /* in case someone actually ports this to alpha! */
58 read_barrier_depends();
60 while (op != &ftrace_list_end) {
61 /* silly alpha */
62 read_barrier_depends();
63 op->func(ip, parent_ip);
64 op = op->next;
68 /**
69 * clear_ftrace_function - reset the ftrace function
71 * This NULLs the ftrace function and in essence stops
72 * tracing. There may be lag
74 void clear_ftrace_function(void)
76 ftrace_trace_function = ftrace_stub;
79 static int __register_ftrace_function(struct ftrace_ops *ops)
81 /* Should never be called by interrupts */
82 spin_lock(&ftrace_lock);
84 ops->next = ftrace_list;
86 * We are entering ops into the ftrace_list but another
87 * CPU might be walking that list. We need to make sure
88 * the ops->next pointer is valid before another CPU sees
89 * the ops pointer included into the ftrace_list.
91 smp_wmb();
92 ftrace_list = ops;
94 if (ftrace_enabled) {
96 * For one func, simply call it directly.
97 * For more than one func, call the chain.
99 if (ops->next == &ftrace_list_end)
100 ftrace_trace_function = ops->func;
101 else
102 ftrace_trace_function = ftrace_list_func;
105 spin_unlock(&ftrace_lock);
107 return 0;
110 static int __unregister_ftrace_function(struct ftrace_ops *ops)
112 struct ftrace_ops **p;
113 int ret = 0;
115 spin_lock(&ftrace_lock);
118 * If we are removing the last function, then simply point
119 * to the ftrace_stub.
121 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
122 ftrace_trace_function = ftrace_stub;
123 ftrace_list = &ftrace_list_end;
124 goto out;
127 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
128 if (*p == ops)
129 break;
131 if (*p != ops) {
132 ret = -1;
133 goto out;
136 *p = (*p)->next;
138 if (ftrace_enabled) {
139 /* If we only have one func left, then call that directly */
140 if (ftrace_list == &ftrace_list_end ||
141 ftrace_list->next == &ftrace_list_end)
142 ftrace_trace_function = ftrace_list->func;
145 out:
146 spin_unlock(&ftrace_lock);
148 return ret;
151 #ifdef CONFIG_DYNAMIC_FTRACE
153 static struct task_struct *ftraced_task;
155 enum {
156 FTRACE_ENABLE_CALLS = (1 << 0),
157 FTRACE_DISABLE_CALLS = (1 << 1),
158 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
159 FTRACE_ENABLE_MCOUNT = (1 << 3),
160 FTRACE_DISABLE_MCOUNT = (1 << 4),
163 static int ftrace_filtered;
165 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
167 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
169 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
170 static DEFINE_MUTEX(ftraced_lock);
171 static DEFINE_MUTEX(ftrace_regex_lock);
173 struct ftrace_page {
174 struct ftrace_page *next;
175 unsigned long index;
176 struct dyn_ftrace records[];
179 #define ENTRIES_PER_PAGE \
180 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
182 /* estimate from running different kernels */
183 #define NR_TO_INIT 10000
185 static struct ftrace_page *ftrace_pages_start;
186 static struct ftrace_page *ftrace_pages;
188 static int ftraced_trigger;
189 static int ftraced_suspend;
190 static int ftraced_stop;
192 static int ftrace_record_suspend;
194 static struct dyn_ftrace *ftrace_free_records;
196 static inline int
197 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
199 struct dyn_ftrace *p;
200 struct hlist_node *t;
201 int found = 0;
203 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
204 if (p->ip == ip) {
205 found = 1;
206 break;
210 return found;
213 static inline void
214 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
216 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
219 static void ftrace_free_rec(struct dyn_ftrace *rec)
221 /* no locking, only called from kstop_machine */
223 rec->ip = (unsigned long)ftrace_free_records;
224 ftrace_free_records = rec;
225 rec->flags |= FTRACE_FL_FREE;
228 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
230 struct dyn_ftrace *rec;
232 /* First check for freed records */
233 if (ftrace_free_records) {
234 rec = ftrace_free_records;
236 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
237 WARN_ON_ONCE(1);
238 ftrace_free_records = NULL;
239 ftrace_disabled = 1;
240 ftrace_enabled = 0;
241 return NULL;
244 ftrace_free_records = (void *)rec->ip;
245 memset(rec, 0, sizeof(*rec));
246 return rec;
249 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
250 if (!ftrace_pages->next)
251 return NULL;
252 ftrace_pages = ftrace_pages->next;
255 return &ftrace_pages->records[ftrace_pages->index++];
258 static void
259 ftrace_record_ip(unsigned long ip)
261 struct dyn_ftrace *node;
262 unsigned long flags;
263 unsigned long key;
264 int resched;
265 int atomic;
266 int cpu;
268 if (!ftrace_enabled || ftrace_disabled)
269 return;
271 resched = need_resched();
272 preempt_disable_notrace();
275 * We simply need to protect against recursion.
276 * Use the the raw version of smp_processor_id and not
277 * __get_cpu_var which can call debug hooks that can
278 * cause a recursive crash here.
280 cpu = raw_smp_processor_id();
281 per_cpu(ftrace_shutdown_disable_cpu, cpu)++;
282 if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1)
283 goto out;
285 if (unlikely(ftrace_record_suspend))
286 goto out;
288 key = hash_long(ip, FTRACE_HASHBITS);
290 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
292 if (ftrace_ip_in_hash(ip, key))
293 goto out;
295 atomic = irqs_disabled();
297 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
299 /* This ip may have hit the hash before the lock */
300 if (ftrace_ip_in_hash(ip, key))
301 goto out_unlock;
304 * There's a slight race that the ftraced will update the
305 * hash and reset here. If it is already converted, skip it.
307 if (ftrace_ip_converted(ip))
308 goto out_unlock;
310 node = ftrace_alloc_dyn_node(ip);
311 if (!node)
312 goto out_unlock;
314 node->ip = ip;
316 ftrace_add_hash(node, key);
318 ftraced_trigger = 1;
320 out_unlock:
321 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
322 out:
323 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
325 /* prevent recursion with scheduler */
326 if (resched)
327 preempt_enable_no_resched_notrace();
328 else
329 preempt_enable_notrace();
332 #define FTRACE_ADDR ((long)(ftrace_caller))
333 #define MCOUNT_ADDR ((long)(mcount))
335 static void
336 __ftrace_replace_code(struct dyn_ftrace *rec,
337 unsigned char *old, unsigned char *new, int enable)
339 unsigned long ip, fl;
340 int failed;
342 ip = rec->ip;
344 if (ftrace_filtered && enable) {
346 * If filtering is on:
348 * If this record is set to be filtered and
349 * is enabled then do nothing.
351 * If this record is set to be filtered and
352 * it is not enabled, enable it.
354 * If this record is not set to be filtered
355 * and it is not enabled do nothing.
357 * If this record is set not to trace then
358 * do nothing.
360 * If this record is not set to be filtered and
361 * it is enabled, disable it.
363 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
365 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
366 (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
367 return;
370 * If it is enabled disable it,
371 * otherwise enable it!
373 if (fl == FTRACE_FL_ENABLED) {
374 /* swap new and old */
375 new = old;
376 old = ftrace_call_replace(ip, FTRACE_ADDR);
377 rec->flags &= ~FTRACE_FL_ENABLED;
378 } else {
379 new = ftrace_call_replace(ip, FTRACE_ADDR);
380 rec->flags |= FTRACE_FL_ENABLED;
382 } else {
384 if (enable) {
386 * If this record is set not to trace and is
387 * not enabled, do nothing.
389 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
390 if (fl == FTRACE_FL_NOTRACE)
391 return;
393 new = ftrace_call_replace(ip, FTRACE_ADDR);
394 } else
395 old = ftrace_call_replace(ip, FTRACE_ADDR);
397 if (enable) {
398 if (rec->flags & FTRACE_FL_ENABLED)
399 return;
400 rec->flags |= FTRACE_FL_ENABLED;
401 } else {
402 if (!(rec->flags & FTRACE_FL_ENABLED))
403 return;
404 rec->flags &= ~FTRACE_FL_ENABLED;
408 failed = ftrace_modify_code(ip, old, new);
409 if (failed) {
410 unsigned long key;
411 /* It is possible that the function hasn't been converted yet */
412 key = hash_long(ip, FTRACE_HASHBITS);
413 if (!ftrace_ip_in_hash(ip, key)) {
414 rec->flags |= FTRACE_FL_FAILED;
415 ftrace_free_rec(rec);
421 static void ftrace_replace_code(int enable)
423 unsigned char *new = NULL, *old = NULL;
424 struct dyn_ftrace *rec;
425 struct ftrace_page *pg;
426 int i;
428 if (enable)
429 old = ftrace_nop_replace();
430 else
431 new = ftrace_nop_replace();
433 for (pg = ftrace_pages_start; pg; pg = pg->next) {
434 for (i = 0; i < pg->index; i++) {
435 rec = &pg->records[i];
437 /* don't modify code that has already faulted */
438 if (rec->flags & FTRACE_FL_FAILED)
439 continue;
441 __ftrace_replace_code(rec, old, new, enable);
446 static void ftrace_shutdown_replenish(void)
448 if (ftrace_pages->next)
449 return;
451 /* allocate another page */
452 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
455 static int
456 ftrace_code_disable(struct dyn_ftrace *rec)
458 unsigned long ip;
459 unsigned char *nop, *call;
460 int failed;
462 ip = rec->ip;
464 nop = ftrace_nop_replace();
465 call = ftrace_call_replace(ip, MCOUNT_ADDR);
467 failed = ftrace_modify_code(ip, call, nop);
468 if (failed) {
469 rec->flags |= FTRACE_FL_FAILED;
470 ftrace_free_rec(rec);
471 return 0;
473 return 1;
476 static int __ftrace_update_code(void *ignore);
478 static int __ftrace_modify_code(void *data)
480 unsigned long addr;
481 int *command = data;
483 if (*command & FTRACE_ENABLE_CALLS) {
485 * Update any recorded ips now that we have the
486 * machine stopped
488 __ftrace_update_code(NULL);
489 ftrace_replace_code(1);
490 } else if (*command & FTRACE_DISABLE_CALLS)
491 ftrace_replace_code(0);
493 if (*command & FTRACE_UPDATE_TRACE_FUNC)
494 ftrace_update_ftrace_func(ftrace_trace_function);
496 if (*command & FTRACE_ENABLE_MCOUNT) {
497 addr = (unsigned long)ftrace_record_ip;
498 ftrace_mcount_set(&addr);
499 } else if (*command & FTRACE_DISABLE_MCOUNT) {
500 addr = (unsigned long)ftrace_stub;
501 ftrace_mcount_set(&addr);
504 return 0;
507 static void ftrace_run_update_code(int command)
509 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
512 void ftrace_disable_daemon(void)
514 /* Stop the daemon from calling kstop_machine */
515 mutex_lock(&ftraced_lock);
516 ftraced_stop = 1;
517 mutex_unlock(&ftraced_lock);
519 ftrace_force_update();
522 void ftrace_enable_daemon(void)
524 mutex_lock(&ftraced_lock);
525 ftraced_stop = 0;
526 mutex_unlock(&ftraced_lock);
528 ftrace_force_update();
531 static ftrace_func_t saved_ftrace_func;
533 static void ftrace_startup(void)
535 int command = 0;
537 if (unlikely(ftrace_disabled))
538 return;
540 mutex_lock(&ftraced_lock);
541 ftraced_suspend++;
542 if (ftraced_suspend == 1)
543 command |= FTRACE_ENABLE_CALLS;
545 if (saved_ftrace_func != ftrace_trace_function) {
546 saved_ftrace_func = ftrace_trace_function;
547 command |= FTRACE_UPDATE_TRACE_FUNC;
550 if (!command || !ftrace_enabled)
551 goto out;
553 ftrace_run_update_code(command);
554 out:
555 mutex_unlock(&ftraced_lock);
558 static void ftrace_shutdown(void)
560 int command = 0;
562 if (unlikely(ftrace_disabled))
563 return;
565 mutex_lock(&ftraced_lock);
566 ftraced_suspend--;
567 if (!ftraced_suspend)
568 command |= FTRACE_DISABLE_CALLS;
570 if (saved_ftrace_func != ftrace_trace_function) {
571 saved_ftrace_func = ftrace_trace_function;
572 command |= FTRACE_UPDATE_TRACE_FUNC;
575 if (!command || !ftrace_enabled)
576 goto out;
578 ftrace_run_update_code(command);
579 out:
580 mutex_unlock(&ftraced_lock);
583 static void ftrace_startup_sysctl(void)
585 int command = FTRACE_ENABLE_MCOUNT;
587 if (unlikely(ftrace_disabled))
588 return;
590 mutex_lock(&ftraced_lock);
591 /* Force update next time */
592 saved_ftrace_func = NULL;
593 /* ftraced_suspend is true if we want ftrace running */
594 if (ftraced_suspend)
595 command |= FTRACE_ENABLE_CALLS;
597 ftrace_run_update_code(command);
598 mutex_unlock(&ftraced_lock);
601 static void ftrace_shutdown_sysctl(void)
603 int command = FTRACE_DISABLE_MCOUNT;
605 if (unlikely(ftrace_disabled))
606 return;
608 mutex_lock(&ftraced_lock);
609 /* ftraced_suspend is true if ftrace is running */
610 if (ftraced_suspend)
611 command |= FTRACE_DISABLE_CALLS;
613 ftrace_run_update_code(command);
614 mutex_unlock(&ftraced_lock);
617 static cycle_t ftrace_update_time;
618 static unsigned long ftrace_update_cnt;
619 unsigned long ftrace_update_tot_cnt;
621 static int __ftrace_update_code(void *ignore)
623 struct dyn_ftrace *p;
624 struct hlist_head head;
625 struct hlist_node *t;
626 int save_ftrace_enabled;
627 cycle_t start, stop;
628 int i;
630 /* Don't be recording funcs now */
631 ftrace_record_suspend++;
632 save_ftrace_enabled = ftrace_enabled;
633 ftrace_enabled = 0;
635 start = ftrace_now(raw_smp_processor_id());
636 ftrace_update_cnt = 0;
638 /* No locks needed, the machine is stopped! */
639 for (i = 0; i < FTRACE_HASHSIZE; i++) {
640 if (hlist_empty(&ftrace_hash[i]))
641 continue;
643 head = ftrace_hash[i];
644 INIT_HLIST_HEAD(&ftrace_hash[i]);
646 /* all CPUS are stopped, we are safe to modify code */
647 hlist_for_each_entry(p, t, &head, node) {
648 if (ftrace_code_disable(p))
649 ftrace_update_cnt++;
654 stop = ftrace_now(raw_smp_processor_id());
655 ftrace_update_time = stop - start;
656 ftrace_update_tot_cnt += ftrace_update_cnt;
657 ftraced_trigger = 0;
659 ftrace_enabled = save_ftrace_enabled;
660 ftrace_record_suspend--;
662 return 0;
665 static int ftrace_update_code(void)
667 if (unlikely(ftrace_disabled) ||
668 !ftrace_enabled || !ftraced_trigger)
669 return 0;
671 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
673 return 1;
676 static int ftraced(void *ignore)
678 unsigned long usecs;
680 while (!kthread_should_stop()) {
682 set_current_state(TASK_INTERRUPTIBLE);
684 /* check once a second */
685 schedule_timeout(HZ);
687 if (unlikely(ftrace_disabled))
688 continue;
690 mutex_lock(&ftrace_sysctl_lock);
691 mutex_lock(&ftraced_lock);
692 if (!ftraced_suspend && !ftraced_stop &&
693 ftrace_update_code()) {
694 usecs = nsecs_to_usecs(ftrace_update_time);
695 if (ftrace_update_tot_cnt > 100000) {
696 ftrace_update_tot_cnt = 0;
697 pr_info("hm, dftrace overflow: %lu change%s"
698 " (%lu total) in %lu usec%s\n",
699 ftrace_update_cnt,
700 ftrace_update_cnt != 1 ? "s" : "",
701 ftrace_update_tot_cnt,
702 usecs, usecs != 1 ? "s" : "");
703 ftrace_disabled = 1;
704 WARN_ON_ONCE(1);
707 mutex_unlock(&ftraced_lock);
708 mutex_unlock(&ftrace_sysctl_lock);
710 ftrace_shutdown_replenish();
712 __set_current_state(TASK_RUNNING);
713 return 0;
716 static int __init ftrace_dyn_table_alloc(void)
718 struct ftrace_page *pg;
719 int cnt;
720 int i;
722 /* allocate a few pages */
723 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
724 if (!ftrace_pages_start)
725 return -1;
728 * Allocate a few more pages.
730 * TODO: have some parser search vmlinux before
731 * final linking to find all calls to ftrace.
732 * Then we can:
733 * a) know how many pages to allocate.
734 * and/or
735 * b) set up the table then.
737 * The dynamic code is still necessary for
738 * modules.
741 pg = ftrace_pages = ftrace_pages_start;
743 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
745 for (i = 0; i < cnt; i++) {
746 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
748 /* If we fail, we'll try later anyway */
749 if (!pg->next)
750 break;
752 pg = pg->next;
755 return 0;
758 enum {
759 FTRACE_ITER_FILTER = (1 << 0),
760 FTRACE_ITER_CONT = (1 << 1),
761 FTRACE_ITER_NOTRACE = (1 << 2),
764 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
766 struct ftrace_iterator {
767 loff_t pos;
768 struct ftrace_page *pg;
769 unsigned idx;
770 unsigned flags;
771 unsigned char buffer[FTRACE_BUFF_MAX+1];
772 unsigned buffer_idx;
773 unsigned filtered;
776 static void *
777 t_next(struct seq_file *m, void *v, loff_t *pos)
779 struct ftrace_iterator *iter = m->private;
780 struct dyn_ftrace *rec = NULL;
782 (*pos)++;
784 retry:
785 if (iter->idx >= iter->pg->index) {
786 if (iter->pg->next) {
787 iter->pg = iter->pg->next;
788 iter->idx = 0;
789 goto retry;
791 } else {
792 rec = &iter->pg->records[iter->idx++];
793 if ((rec->flags & FTRACE_FL_FAILED) ||
794 ((iter->flags & FTRACE_ITER_FILTER) &&
795 !(rec->flags & FTRACE_FL_FILTER)) ||
796 ((iter->flags & FTRACE_ITER_NOTRACE) &&
797 !(rec->flags & FTRACE_FL_NOTRACE))) {
798 rec = NULL;
799 goto retry;
803 iter->pos = *pos;
805 return rec;
808 static void *t_start(struct seq_file *m, loff_t *pos)
810 struct ftrace_iterator *iter = m->private;
811 void *p = NULL;
812 loff_t l = -1;
814 if (*pos != iter->pos) {
815 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
817 } else {
818 l = *pos;
819 p = t_next(m, p, &l);
822 return p;
825 static void t_stop(struct seq_file *m, void *p)
829 static int t_show(struct seq_file *m, void *v)
831 struct dyn_ftrace *rec = v;
832 char str[KSYM_SYMBOL_LEN];
834 if (!rec)
835 return 0;
837 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
839 seq_printf(m, "%s\n", str);
841 return 0;
844 static struct seq_operations show_ftrace_seq_ops = {
845 .start = t_start,
846 .next = t_next,
847 .stop = t_stop,
848 .show = t_show,
851 static int
852 ftrace_avail_open(struct inode *inode, struct file *file)
854 struct ftrace_iterator *iter;
855 int ret;
857 if (unlikely(ftrace_disabled))
858 return -ENODEV;
860 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
861 if (!iter)
862 return -ENOMEM;
864 iter->pg = ftrace_pages_start;
865 iter->pos = -1;
867 ret = seq_open(file, &show_ftrace_seq_ops);
868 if (!ret) {
869 struct seq_file *m = file->private_data;
871 m->private = iter;
872 } else {
873 kfree(iter);
876 return ret;
879 int ftrace_avail_release(struct inode *inode, struct file *file)
881 struct seq_file *m = (struct seq_file *)file->private_data;
882 struct ftrace_iterator *iter = m->private;
884 seq_release(inode, file);
885 kfree(iter);
887 return 0;
890 static void ftrace_filter_reset(int enable)
892 struct ftrace_page *pg;
893 struct dyn_ftrace *rec;
894 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
895 unsigned i;
897 /* keep kstop machine from running */
898 preempt_disable();
899 if (enable)
900 ftrace_filtered = 0;
901 pg = ftrace_pages_start;
902 while (pg) {
903 for (i = 0; i < pg->index; i++) {
904 rec = &pg->records[i];
905 if (rec->flags & FTRACE_FL_FAILED)
906 continue;
907 rec->flags &= ~type;
909 pg = pg->next;
911 preempt_enable();
914 static int
915 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
917 struct ftrace_iterator *iter;
918 int ret = 0;
920 if (unlikely(ftrace_disabled))
921 return -ENODEV;
923 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
924 if (!iter)
925 return -ENOMEM;
927 mutex_lock(&ftrace_regex_lock);
928 if ((file->f_mode & FMODE_WRITE) &&
929 !(file->f_flags & O_APPEND))
930 ftrace_filter_reset(enable);
932 if (file->f_mode & FMODE_READ) {
933 iter->pg = ftrace_pages_start;
934 iter->pos = -1;
935 iter->flags = enable ? FTRACE_ITER_FILTER :
936 FTRACE_ITER_NOTRACE;
938 ret = seq_open(file, &show_ftrace_seq_ops);
939 if (!ret) {
940 struct seq_file *m = file->private_data;
941 m->private = iter;
942 } else
943 kfree(iter);
944 } else
945 file->private_data = iter;
946 mutex_unlock(&ftrace_regex_lock);
948 return ret;
951 static int
952 ftrace_filter_open(struct inode *inode, struct file *file)
954 return ftrace_regex_open(inode, file, 1);
957 static int
958 ftrace_notrace_open(struct inode *inode, struct file *file)
960 return ftrace_regex_open(inode, file, 0);
963 static ssize_t
964 ftrace_regex_read(struct file *file, char __user *ubuf,
965 size_t cnt, loff_t *ppos)
967 if (file->f_mode & FMODE_READ)
968 return seq_read(file, ubuf, cnt, ppos);
969 else
970 return -EPERM;
973 static loff_t
974 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
976 loff_t ret;
978 if (file->f_mode & FMODE_READ)
979 ret = seq_lseek(file, offset, origin);
980 else
981 file->f_pos = ret = 1;
983 return ret;
986 enum {
987 MATCH_FULL,
988 MATCH_FRONT_ONLY,
989 MATCH_MIDDLE_ONLY,
990 MATCH_END_ONLY,
993 static void
994 ftrace_match(unsigned char *buff, int len, int enable)
996 char str[KSYM_SYMBOL_LEN];
997 char *search = NULL;
998 struct ftrace_page *pg;
999 struct dyn_ftrace *rec;
1000 int type = MATCH_FULL;
1001 unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1002 unsigned i, match = 0, search_len = 0;
1004 for (i = 0; i < len; i++) {
1005 if (buff[i] == '*') {
1006 if (!i) {
1007 search = buff + i + 1;
1008 type = MATCH_END_ONLY;
1009 search_len = len - (i + 1);
1010 } else {
1011 if (type == MATCH_END_ONLY) {
1012 type = MATCH_MIDDLE_ONLY;
1013 } else {
1014 match = i;
1015 type = MATCH_FRONT_ONLY;
1017 buff[i] = 0;
1018 break;
1023 /* keep kstop machine from running */
1024 preempt_disable();
1025 if (enable)
1026 ftrace_filtered = 1;
1027 pg = ftrace_pages_start;
1028 while (pg) {
1029 for (i = 0; i < pg->index; i++) {
1030 int matched = 0;
1031 char *ptr;
1033 rec = &pg->records[i];
1034 if (rec->flags & FTRACE_FL_FAILED)
1035 continue;
1036 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1037 switch (type) {
1038 case MATCH_FULL:
1039 if (strcmp(str, buff) == 0)
1040 matched = 1;
1041 break;
1042 case MATCH_FRONT_ONLY:
1043 if (memcmp(str, buff, match) == 0)
1044 matched = 1;
1045 break;
1046 case MATCH_MIDDLE_ONLY:
1047 if (strstr(str, search))
1048 matched = 1;
1049 break;
1050 case MATCH_END_ONLY:
1051 ptr = strstr(str, search);
1052 if (ptr && (ptr[search_len] == 0))
1053 matched = 1;
1054 break;
1056 if (matched)
1057 rec->flags |= flag;
1059 pg = pg->next;
1061 preempt_enable();
1064 static ssize_t
1065 ftrace_regex_write(struct file *file, const char __user *ubuf,
1066 size_t cnt, loff_t *ppos, int enable)
1068 struct ftrace_iterator *iter;
1069 char ch;
1070 size_t read = 0;
1071 ssize_t ret;
1073 if (!cnt || cnt < 0)
1074 return 0;
1076 mutex_lock(&ftrace_regex_lock);
1078 if (file->f_mode & FMODE_READ) {
1079 struct seq_file *m = file->private_data;
1080 iter = m->private;
1081 } else
1082 iter = file->private_data;
1084 if (!*ppos) {
1085 iter->flags &= ~FTRACE_ITER_CONT;
1086 iter->buffer_idx = 0;
1089 ret = get_user(ch, ubuf++);
1090 if (ret)
1091 goto out;
1092 read++;
1093 cnt--;
1095 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1096 /* skip white space */
1097 while (cnt && isspace(ch)) {
1098 ret = get_user(ch, ubuf++);
1099 if (ret)
1100 goto out;
1101 read++;
1102 cnt--;
1105 if (isspace(ch)) {
1106 file->f_pos += read;
1107 ret = read;
1108 goto out;
1111 iter->buffer_idx = 0;
1114 while (cnt && !isspace(ch)) {
1115 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1116 iter->buffer[iter->buffer_idx++] = ch;
1117 else {
1118 ret = -EINVAL;
1119 goto out;
1121 ret = get_user(ch, ubuf++);
1122 if (ret)
1123 goto out;
1124 read++;
1125 cnt--;
1128 if (isspace(ch)) {
1129 iter->filtered++;
1130 iter->buffer[iter->buffer_idx] = 0;
1131 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1132 iter->buffer_idx = 0;
1133 } else
1134 iter->flags |= FTRACE_ITER_CONT;
1137 file->f_pos += read;
1139 ret = read;
1140 out:
1141 mutex_unlock(&ftrace_regex_lock);
1143 return ret;
1146 static ssize_t
1147 ftrace_filter_write(struct file *file, const char __user *ubuf,
1148 size_t cnt, loff_t *ppos)
1150 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1153 static ssize_t
1154 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1155 size_t cnt, loff_t *ppos)
1157 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1160 static void
1161 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1163 if (unlikely(ftrace_disabled))
1164 return;
1166 mutex_lock(&ftrace_regex_lock);
1167 if (reset)
1168 ftrace_filter_reset(enable);
1169 if (buf)
1170 ftrace_match(buf, len, enable);
1171 mutex_unlock(&ftrace_regex_lock);
1175 * ftrace_set_filter - set a function to filter on in ftrace
1176 * @buf - the string that holds the function filter text.
1177 * @len - the length of the string.
1178 * @reset - non zero to reset all filters before applying this filter.
1180 * Filters denote which functions should be enabled when tracing is enabled.
1181 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1183 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1185 ftrace_set_regex(buf, len, reset, 1);
1189 * ftrace_set_notrace - set a function to not trace in ftrace
1190 * @buf - the string that holds the function notrace text.
1191 * @len - the length of the string.
1192 * @reset - non zero to reset all filters before applying this filter.
1194 * Notrace Filters denote which functions should not be enabled when tracing
1195 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1196 * for tracing.
1198 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1200 ftrace_set_regex(buf, len, reset, 0);
1203 static int
1204 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1206 struct seq_file *m = (struct seq_file *)file->private_data;
1207 struct ftrace_iterator *iter;
1209 mutex_lock(&ftrace_regex_lock);
1210 if (file->f_mode & FMODE_READ) {
1211 iter = m->private;
1213 seq_release(inode, file);
1214 } else
1215 iter = file->private_data;
1217 if (iter->buffer_idx) {
1218 iter->filtered++;
1219 iter->buffer[iter->buffer_idx] = 0;
1220 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1223 mutex_lock(&ftrace_sysctl_lock);
1224 mutex_lock(&ftraced_lock);
1225 if (iter->filtered && ftraced_suspend && ftrace_enabled)
1226 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1227 mutex_unlock(&ftraced_lock);
1228 mutex_unlock(&ftrace_sysctl_lock);
1230 kfree(iter);
1231 mutex_unlock(&ftrace_regex_lock);
1232 return 0;
1235 static int
1236 ftrace_filter_release(struct inode *inode, struct file *file)
1238 return ftrace_regex_release(inode, file, 1);
1241 static int
1242 ftrace_notrace_release(struct inode *inode, struct file *file)
1244 return ftrace_regex_release(inode, file, 0);
1247 static ssize_t
1248 ftraced_read(struct file *filp, char __user *ubuf,
1249 size_t cnt, loff_t *ppos)
1251 /* don't worry about races */
1252 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1253 int r = strlen(buf);
1255 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1258 static ssize_t
1259 ftraced_write(struct file *filp, const char __user *ubuf,
1260 size_t cnt, loff_t *ppos)
1262 char buf[64];
1263 long val;
1264 int ret;
1266 if (cnt >= sizeof(buf))
1267 return -EINVAL;
1269 if (copy_from_user(&buf, ubuf, cnt))
1270 return -EFAULT;
1272 if (strncmp(buf, "enable", 6) == 0)
1273 val = 1;
1274 else if (strncmp(buf, "disable", 7) == 0)
1275 val = 0;
1276 else {
1277 buf[cnt] = 0;
1279 ret = strict_strtoul(buf, 10, &val);
1280 if (ret < 0)
1281 return ret;
1283 val = !!val;
1286 if (val)
1287 ftrace_enable_daemon();
1288 else
1289 ftrace_disable_daemon();
1291 filp->f_pos += cnt;
1293 return cnt;
1296 static struct file_operations ftrace_avail_fops = {
1297 .open = ftrace_avail_open,
1298 .read = seq_read,
1299 .llseek = seq_lseek,
1300 .release = ftrace_avail_release,
1303 static struct file_operations ftrace_filter_fops = {
1304 .open = ftrace_filter_open,
1305 .read = ftrace_regex_read,
1306 .write = ftrace_filter_write,
1307 .llseek = ftrace_regex_lseek,
1308 .release = ftrace_filter_release,
1311 static struct file_operations ftrace_notrace_fops = {
1312 .open = ftrace_notrace_open,
1313 .read = ftrace_regex_read,
1314 .write = ftrace_notrace_write,
1315 .llseek = ftrace_regex_lseek,
1316 .release = ftrace_notrace_release,
1319 static struct file_operations ftraced_fops = {
1320 .open = tracing_open_generic,
1321 .read = ftraced_read,
1322 .write = ftraced_write,
1326 * ftrace_force_update - force an update to all recording ftrace functions
1328 int ftrace_force_update(void)
1330 int ret = 0;
1332 if (unlikely(ftrace_disabled))
1333 return -ENODEV;
1335 mutex_lock(&ftrace_sysctl_lock);
1336 mutex_lock(&ftraced_lock);
1339 * If ftraced_trigger is not set, then there is nothing
1340 * to update.
1342 if (ftraced_trigger && !ftrace_update_code())
1343 ret = -EBUSY;
1345 mutex_unlock(&ftraced_lock);
1346 mutex_unlock(&ftrace_sysctl_lock);
1348 return ret;
1351 static void ftrace_force_shutdown(void)
1353 struct task_struct *task;
1354 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1356 mutex_lock(&ftraced_lock);
1357 task = ftraced_task;
1358 ftraced_task = NULL;
1359 ftraced_suspend = -1;
1360 ftrace_run_update_code(command);
1361 mutex_unlock(&ftraced_lock);
1363 if (task)
1364 kthread_stop(task);
1367 static __init int ftrace_init_debugfs(void)
1369 struct dentry *d_tracer;
1370 struct dentry *entry;
1372 d_tracer = tracing_init_dentry();
1374 entry = debugfs_create_file("available_filter_functions", 0444,
1375 d_tracer, NULL, &ftrace_avail_fops);
1376 if (!entry)
1377 pr_warning("Could not create debugfs "
1378 "'available_filter_functions' entry\n");
1380 entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1381 NULL, &ftrace_filter_fops);
1382 if (!entry)
1383 pr_warning("Could not create debugfs "
1384 "'set_ftrace_filter' entry\n");
1386 entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1387 NULL, &ftrace_notrace_fops);
1388 if (!entry)
1389 pr_warning("Could not create debugfs "
1390 "'set_ftrace_notrace' entry\n");
1392 entry = debugfs_create_file("ftraced_enabled", 0644, d_tracer,
1393 NULL, &ftraced_fops);
1394 if (!entry)
1395 pr_warning("Could not create debugfs "
1396 "'ftraced_enabled' entry\n");
1397 return 0;
1400 fs_initcall(ftrace_init_debugfs);
1402 static int __init ftrace_dynamic_init(void)
1404 struct task_struct *p;
1405 unsigned long addr;
1406 int ret;
1408 addr = (unsigned long)ftrace_record_ip;
1410 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1412 /* ftrace_dyn_arch_init places the return code in addr */
1413 if (addr) {
1414 ret = (int)addr;
1415 goto failed;
1418 ret = ftrace_dyn_table_alloc();
1419 if (ret)
1420 goto failed;
1422 p = kthread_run(ftraced, NULL, "ftraced");
1423 if (IS_ERR(p)) {
1424 ret = -1;
1425 goto failed;
1428 last_ftrace_enabled = ftrace_enabled = 1;
1429 ftraced_task = p;
1431 return 0;
1433 failed:
1434 ftrace_disabled = 1;
1435 return ret;
1438 core_initcall(ftrace_dynamic_init);
1439 #else
1440 # define ftrace_startup() do { } while (0)
1441 # define ftrace_shutdown() do { } while (0)
1442 # define ftrace_startup_sysctl() do { } while (0)
1443 # define ftrace_shutdown_sysctl() do { } while (0)
1444 # define ftrace_force_shutdown() do { } while (0)
1445 #endif /* CONFIG_DYNAMIC_FTRACE */
1448 * ftrace_kill - totally shutdown ftrace
1450 * This is a safety measure. If something was detected that seems
1451 * wrong, calling this function will keep ftrace from doing
1452 * any more modifications, and updates.
1453 * used when something went wrong.
1455 void ftrace_kill(void)
1457 mutex_lock(&ftrace_sysctl_lock);
1458 ftrace_disabled = 1;
1459 ftrace_enabled = 0;
1461 clear_ftrace_function();
1462 mutex_unlock(&ftrace_sysctl_lock);
1464 /* Try to totally disable ftrace */
1465 ftrace_force_shutdown();
1469 * register_ftrace_function - register a function for profiling
1470 * @ops - ops structure that holds the function for profiling.
1472 * Register a function to be called by all functions in the
1473 * kernel.
1475 * Note: @ops->func and all the functions it calls must be labeled
1476 * with "notrace", otherwise it will go into a
1477 * recursive loop.
1479 int register_ftrace_function(struct ftrace_ops *ops)
1481 int ret;
1483 if (unlikely(ftrace_disabled))
1484 return -1;
1486 mutex_lock(&ftrace_sysctl_lock);
1487 ret = __register_ftrace_function(ops);
1488 ftrace_startup();
1489 mutex_unlock(&ftrace_sysctl_lock);
1491 return ret;
1495 * unregister_ftrace_function - unresgister a function for profiling.
1496 * @ops - ops structure that holds the function to unregister
1498 * Unregister a function that was added to be called by ftrace profiling.
1500 int unregister_ftrace_function(struct ftrace_ops *ops)
1502 int ret;
1504 mutex_lock(&ftrace_sysctl_lock);
1505 ret = __unregister_ftrace_function(ops);
1506 ftrace_shutdown();
1507 mutex_unlock(&ftrace_sysctl_lock);
1509 return ret;
1513 ftrace_enable_sysctl(struct ctl_table *table, int write,
1514 struct file *file, void __user *buffer, size_t *lenp,
1515 loff_t *ppos)
1517 int ret;
1519 if (unlikely(ftrace_disabled))
1520 return -ENODEV;
1522 mutex_lock(&ftrace_sysctl_lock);
1524 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1526 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1527 goto out;
1529 last_ftrace_enabled = ftrace_enabled;
1531 if (ftrace_enabled) {
1533 ftrace_startup_sysctl();
1535 /* we are starting ftrace again */
1536 if (ftrace_list != &ftrace_list_end) {
1537 if (ftrace_list->next == &ftrace_list_end)
1538 ftrace_trace_function = ftrace_list->func;
1539 else
1540 ftrace_trace_function = ftrace_list_func;
1543 } else {
1544 /* stopping ftrace calls (just send to ftrace_stub) */
1545 ftrace_trace_function = ftrace_stub;
1547 ftrace_shutdown_sysctl();
1550 out:
1551 mutex_unlock(&ftrace_sysctl_lock);
1552 return ret;