ftrace: user update and disable dynamic ftrace daemon
[deliverable/linux.git] / kernel / trace / ftrace.c
1 /*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
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>
29
30 #include "trace.h"
31
32 /* ftrace_enabled is a method to turn ftrace on or off */
33 int ftrace_enabled __read_mostly;
34 static int last_ftrace_enabled;
35
36 /*
37 * ftrace_disabled is set when an anomaly is discovered.
38 * ftrace_disabled is much stronger than ftrace_enabled.
39 */
40 static int ftrace_disabled __read_mostly;
41
42 static DEFINE_SPINLOCK(ftrace_lock);
43 static DEFINE_MUTEX(ftrace_sysctl_lock);
44
45 static struct ftrace_ops ftrace_list_end __read_mostly =
46 {
47 .func = ftrace_stub,
48 };
49
50 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
51 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
52
53 void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
54 {
55 struct ftrace_ops *op = ftrace_list;
56
57 /* in case someone actually ports this to alpha! */
58 read_barrier_depends();
59
60 while (op != &ftrace_list_end) {
61 /* silly alpha */
62 read_barrier_depends();
63 op->func(ip, parent_ip);
64 op = op->next;
65 };
66 }
67
68 /**
69 * clear_ftrace_function - reset the ftrace function
70 *
71 * This NULLs the ftrace function and in essence stops
72 * tracing. There may be lag
73 */
74 void clear_ftrace_function(void)
75 {
76 ftrace_trace_function = ftrace_stub;
77 }
78
79 static int __register_ftrace_function(struct ftrace_ops *ops)
80 {
81 /* Should never be called by interrupts */
82 spin_lock(&ftrace_lock);
83
84 ops->next = ftrace_list;
85 /*
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.
90 */
91 smp_wmb();
92 ftrace_list = ops;
93
94 if (ftrace_enabled) {
95 /*
96 * For one func, simply call it directly.
97 * For more than one func, call the chain.
98 */
99 if (ops->next == &ftrace_list_end)
100 ftrace_trace_function = ops->func;
101 else
102 ftrace_trace_function = ftrace_list_func;
103 }
104
105 spin_unlock(&ftrace_lock);
106
107 return 0;
108 }
109
110 static int __unregister_ftrace_function(struct ftrace_ops *ops)
111 {
112 struct ftrace_ops **p;
113 int ret = 0;
114
115 spin_lock(&ftrace_lock);
116
117 /*
118 * If we are removing the last function, then simply point
119 * to the ftrace_stub.
120 */
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;
125 }
126
127 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
128 if (*p == ops)
129 break;
130
131 if (*p != ops) {
132 ret = -1;
133 goto out;
134 }
135
136 *p = (*p)->next;
137
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;
143 }
144
145 out:
146 spin_unlock(&ftrace_lock);
147
148 return ret;
149 }
150
151 #ifdef CONFIG_DYNAMIC_FTRACE
152
153 static struct task_struct *ftraced_task;
154
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),
161 };
162
163 static int ftrace_filtered;
164
165 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
166
167 static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
168
169 static DEFINE_SPINLOCK(ftrace_shutdown_lock);
170 static DEFINE_MUTEX(ftraced_lock);
171 static DEFINE_MUTEX(ftrace_regex_lock);
172
173 struct ftrace_page {
174 struct ftrace_page *next;
175 unsigned long index;
176 struct dyn_ftrace records[];
177 };
178
179 #define ENTRIES_PER_PAGE \
180 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
181
182 /* estimate from running different kernels */
183 #define NR_TO_INIT 10000
184
185 static struct ftrace_page *ftrace_pages_start;
186 static struct ftrace_page *ftrace_pages;
187
188 static int ftraced_trigger;
189 static int ftraced_suspend;
190 static int ftraced_stop;
191
192 static int ftrace_record_suspend;
193
194 static struct dyn_ftrace *ftrace_free_records;
195
196 static inline int
197 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
198 {
199 struct dyn_ftrace *p;
200 struct hlist_node *t;
201 int found = 0;
202
203 hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) {
204 if (p->ip == ip) {
205 found = 1;
206 break;
207 }
208 }
209
210 return found;
211 }
212
213 static inline void
214 ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
215 {
216 hlist_add_head_rcu(&node->node, &ftrace_hash[key]);
217 }
218
219 static void ftrace_free_rec(struct dyn_ftrace *rec)
220 {
221 /* no locking, only called from kstop_machine */
222
223 rec->ip = (unsigned long)ftrace_free_records;
224 ftrace_free_records = rec;
225 rec->flags |= FTRACE_FL_FREE;
226 }
227
228 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
229 {
230 struct dyn_ftrace *rec;
231
232 /* First check for freed records */
233 if (ftrace_free_records) {
234 rec = ftrace_free_records;
235
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;
242 }
243
244 ftrace_free_records = (void *)rec->ip;
245 memset(rec, 0, sizeof(*rec));
246 return rec;
247 }
248
249 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
250 if (!ftrace_pages->next)
251 return NULL;
252 ftrace_pages = ftrace_pages->next;
253 }
254
255 return &ftrace_pages->records[ftrace_pages->index++];
256 }
257
258 static void
259 ftrace_record_ip(unsigned long ip)
260 {
261 struct dyn_ftrace *node;
262 unsigned long flags;
263 unsigned long key;
264 int resched;
265 int atomic;
266 int cpu;
267
268 if (!ftrace_enabled || ftrace_disabled)
269 return;
270
271 resched = need_resched();
272 preempt_disable_notrace();
273
274 /*
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.
279 */
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;
284
285 if (unlikely(ftrace_record_suspend))
286 goto out;
287
288 key = hash_long(ip, FTRACE_HASHBITS);
289
290 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
291
292 if (ftrace_ip_in_hash(ip, key))
293 goto out;
294
295 atomic = irqs_disabled();
296
297 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
298
299 /* This ip may have hit the hash before the lock */
300 if (ftrace_ip_in_hash(ip, key))
301 goto out_unlock;
302
303 /*
304 * There's a slight race that the ftraced will update the
305 * hash and reset here. If it is already converted, skip it.
306 */
307 if (ftrace_ip_converted(ip))
308 goto out_unlock;
309
310 node = ftrace_alloc_dyn_node(ip);
311 if (!node)
312 goto out_unlock;
313
314 node->ip = ip;
315
316 ftrace_add_hash(node, key);
317
318 ftraced_trigger = 1;
319
320 out_unlock:
321 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
322 out:
323 per_cpu(ftrace_shutdown_disable_cpu, cpu)--;
324
325 /* prevent recursion with scheduler */
326 if (resched)
327 preempt_enable_no_resched_notrace();
328 else
329 preempt_enable_notrace();
330 }
331
332 #define FTRACE_ADDR ((long)(ftrace_caller))
333 #define MCOUNT_ADDR ((long)(mcount))
334
335 static void
336 __ftrace_replace_code(struct dyn_ftrace *rec,
337 unsigned char *old, unsigned char *new, int enable)
338 {
339 unsigned long ip, fl;
340 int failed;
341
342 ip = rec->ip;
343
344 if (ftrace_filtered && enable) {
345 /*
346 * If filtering is on:
347 *
348 * If this record is set to be filtered and
349 * is enabled then do nothing.
350 *
351 * If this record is set to be filtered and
352 * it is not enabled, enable it.
353 *
354 * If this record is not set to be filtered
355 * and it is not enabled do nothing.
356 *
357 * If this record is set not to trace then
358 * do nothing.
359 *
360 * If this record is not set to be filtered and
361 * it is enabled, disable it.
362 */
363 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
364
365 if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
366 (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
367 return;
368
369 /*
370 * If it is enabled disable it,
371 * otherwise enable it!
372 */
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;
381 }
382 } else {
383
384 if (enable) {
385 /*
386 * If this record is set not to trace and is
387 * not enabled, do nothing.
388 */
389 fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
390 if (fl == FTRACE_FL_NOTRACE)
391 return;
392
393 new = ftrace_call_replace(ip, FTRACE_ADDR);
394 } else
395 old = ftrace_call_replace(ip, FTRACE_ADDR);
396
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;
405 }
406 }
407
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);
416 }
417
418 }
419 }
420
421 static void ftrace_replace_code(int enable)
422 {
423 unsigned char *new = NULL, *old = NULL;
424 struct dyn_ftrace *rec;
425 struct ftrace_page *pg;
426 int i;
427
428 if (enable)
429 old = ftrace_nop_replace();
430 else
431 new = ftrace_nop_replace();
432
433 for (pg = ftrace_pages_start; pg; pg = pg->next) {
434 for (i = 0; i < pg->index; i++) {
435 rec = &pg->records[i];
436
437 /* don't modify code that has already faulted */
438 if (rec->flags & FTRACE_FL_FAILED)
439 continue;
440
441 __ftrace_replace_code(rec, old, new, enable);
442 }
443 }
444 }
445
446 static void ftrace_shutdown_replenish(void)
447 {
448 if (ftrace_pages->next)
449 return;
450
451 /* allocate another page */
452 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
453 }
454
455 static int
456 ftrace_code_disable(struct dyn_ftrace *rec)
457 {
458 unsigned long ip;
459 unsigned char *nop, *call;
460 int failed;
461
462 ip = rec->ip;
463
464 nop = ftrace_nop_replace();
465 call = ftrace_call_replace(ip, MCOUNT_ADDR);
466
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;
472 }
473 return 1;
474 }
475
476 static int __ftrace_update_code(void *ignore);
477
478 static int __ftrace_modify_code(void *data)
479 {
480 unsigned long addr;
481 int *command = data;
482
483 if (*command & FTRACE_ENABLE_CALLS) {
484 /*
485 * Update any recorded ips now that we have the
486 * machine stopped
487 */
488 __ftrace_update_code(NULL);
489 ftrace_replace_code(1);
490 } else if (*command & FTRACE_DISABLE_CALLS)
491 ftrace_replace_code(0);
492
493 if (*command & FTRACE_UPDATE_TRACE_FUNC)
494 ftrace_update_ftrace_func(ftrace_trace_function);
495
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);
502 }
503
504 return 0;
505 }
506
507 static void ftrace_run_update_code(int command)
508 {
509 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
510 }
511
512 void ftrace_disable_daemon(void)
513 {
514 /* Stop the daemon from calling kstop_machine */
515 mutex_lock(&ftraced_lock);
516 ftraced_stop = 1;
517 mutex_unlock(&ftraced_lock);
518
519 ftrace_force_update();
520 }
521
522 void ftrace_enable_daemon(void)
523 {
524 mutex_lock(&ftraced_lock);
525 ftraced_stop = 0;
526 mutex_unlock(&ftraced_lock);
527
528 ftrace_force_update();
529 }
530
531 static ftrace_func_t saved_ftrace_func;
532
533 static void ftrace_startup(void)
534 {
535 int command = 0;
536
537 if (unlikely(ftrace_disabled))
538 return;
539
540 mutex_lock(&ftraced_lock);
541 ftraced_suspend++;
542 if (ftraced_suspend == 1)
543 command |= FTRACE_ENABLE_CALLS;
544
545 if (saved_ftrace_func != ftrace_trace_function) {
546 saved_ftrace_func = ftrace_trace_function;
547 command |= FTRACE_UPDATE_TRACE_FUNC;
548 }
549
550 if (!command || !ftrace_enabled)
551 goto out;
552
553 ftrace_run_update_code(command);
554 out:
555 mutex_unlock(&ftraced_lock);
556 }
557
558 static void ftrace_shutdown(void)
559 {
560 int command = 0;
561
562 if (unlikely(ftrace_disabled))
563 return;
564
565 mutex_lock(&ftraced_lock);
566 ftraced_suspend--;
567 if (!ftraced_suspend)
568 command |= FTRACE_DISABLE_CALLS;
569
570 if (saved_ftrace_func != ftrace_trace_function) {
571 saved_ftrace_func = ftrace_trace_function;
572 command |= FTRACE_UPDATE_TRACE_FUNC;
573 }
574
575 if (!command || !ftrace_enabled)
576 goto out;
577
578 ftrace_run_update_code(command);
579 out:
580 mutex_unlock(&ftraced_lock);
581 }
582
583 static void ftrace_startup_sysctl(void)
584 {
585 int command = FTRACE_ENABLE_MCOUNT;
586
587 if (unlikely(ftrace_disabled))
588 return;
589
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;
596
597 ftrace_run_update_code(command);
598 mutex_unlock(&ftraced_lock);
599 }
600
601 static void ftrace_shutdown_sysctl(void)
602 {
603 int command = FTRACE_DISABLE_MCOUNT;
604
605 if (unlikely(ftrace_disabled))
606 return;
607
608 mutex_lock(&ftraced_lock);
609 /* ftraced_suspend is true if ftrace is running */
610 if (ftraced_suspend)
611 command |= FTRACE_DISABLE_CALLS;
612
613 ftrace_run_update_code(command);
614 mutex_unlock(&ftraced_lock);
615 }
616
617 static cycle_t ftrace_update_time;
618 static unsigned long ftrace_update_cnt;
619 unsigned long ftrace_update_tot_cnt;
620
621 static int __ftrace_update_code(void *ignore)
622 {
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;
629
630 /* Don't be recording funcs now */
631 ftrace_record_suspend++;
632 save_ftrace_enabled = ftrace_enabled;
633 ftrace_enabled = 0;
634
635 start = ftrace_now(raw_smp_processor_id());
636 ftrace_update_cnt = 0;
637
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;
642
643 head = ftrace_hash[i];
644 INIT_HLIST_HEAD(&ftrace_hash[i]);
645
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++;
650 }
651
652 }
653
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;
658
659 ftrace_enabled = save_ftrace_enabled;
660 ftrace_record_suspend--;
661
662 return 0;
663 }
664
665 static int ftrace_update_code(void)
666 {
667 if (unlikely(ftrace_disabled) ||
668 !ftrace_enabled || !ftraced_trigger)
669 return 0;
670
671 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
672
673 return 1;
674 }
675
676 static int ftraced(void *ignore)
677 {
678 unsigned long usecs;
679
680 while (!kthread_should_stop()) {
681
682 set_current_state(TASK_INTERRUPTIBLE);
683
684 /* check once a second */
685 schedule_timeout(HZ);
686
687 if (unlikely(ftrace_disabled))
688 continue;
689
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);
705 }
706 }
707 mutex_unlock(&ftraced_lock);
708 mutex_unlock(&ftrace_sysctl_lock);
709
710 ftrace_shutdown_replenish();
711 }
712 __set_current_state(TASK_RUNNING);
713 return 0;
714 }
715
716 static int __init ftrace_dyn_table_alloc(void)
717 {
718 struct ftrace_page *pg;
719 int cnt;
720 int i;
721
722 /* allocate a few pages */
723 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
724 if (!ftrace_pages_start)
725 return -1;
726
727 /*
728 * Allocate a few more pages.
729 *
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.
736 *
737 * The dynamic code is still necessary for
738 * modules.
739 */
740
741 pg = ftrace_pages = ftrace_pages_start;
742
743 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
744
745 for (i = 0; i < cnt; i++) {
746 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
747
748 /* If we fail, we'll try later anyway */
749 if (!pg->next)
750 break;
751
752 pg = pg->next;
753 }
754
755 return 0;
756 }
757
758 enum {
759 FTRACE_ITER_FILTER = (1 << 0),
760 FTRACE_ITER_CONT = (1 << 1),
761 FTRACE_ITER_NOTRACE = (1 << 2),
762 };
763
764 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
765
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;
774 };
775
776 static void *
777 t_next(struct seq_file *m, void *v, loff_t *pos)
778 {
779 struct ftrace_iterator *iter = m->private;
780 struct dyn_ftrace *rec = NULL;
781
782 (*pos)++;
783
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;
790 }
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;
800 }
801 }
802
803 iter->pos = *pos;
804
805 return rec;
806 }
807
808 static void *t_start(struct seq_file *m, loff_t *pos)
809 {
810 struct ftrace_iterator *iter = m->private;
811 void *p = NULL;
812 loff_t l = -1;
813
814 if (*pos != iter->pos) {
815 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
816 ;
817 } else {
818 l = *pos;
819 p = t_next(m, p, &l);
820 }
821
822 return p;
823 }
824
825 static void t_stop(struct seq_file *m, void *p)
826 {
827 }
828
829 static int t_show(struct seq_file *m, void *v)
830 {
831 struct dyn_ftrace *rec = v;
832 char str[KSYM_SYMBOL_LEN];
833
834 if (!rec)
835 return 0;
836
837 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
838
839 seq_printf(m, "%s\n", str);
840
841 return 0;
842 }
843
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,
849 };
850
851 static int
852 ftrace_avail_open(struct inode *inode, struct file *file)
853 {
854 struct ftrace_iterator *iter;
855 int ret;
856
857 if (unlikely(ftrace_disabled))
858 return -ENODEV;
859
860 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
861 if (!iter)
862 return -ENOMEM;
863
864 iter->pg = ftrace_pages_start;
865 iter->pos = -1;
866
867 ret = seq_open(file, &show_ftrace_seq_ops);
868 if (!ret) {
869 struct seq_file *m = file->private_data;
870
871 m->private = iter;
872 } else {
873 kfree(iter);
874 }
875
876 return ret;
877 }
878
879 int ftrace_avail_release(struct inode *inode, struct file *file)
880 {
881 struct seq_file *m = (struct seq_file *)file->private_data;
882 struct ftrace_iterator *iter = m->private;
883
884 seq_release(inode, file);
885 kfree(iter);
886
887 return 0;
888 }
889
890 static void ftrace_filter_reset(int enable)
891 {
892 struct ftrace_page *pg;
893 struct dyn_ftrace *rec;
894 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
895 unsigned i;
896
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;
908 }
909 pg = pg->next;
910 }
911 preempt_enable();
912 }
913
914 static int
915 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
916 {
917 struct ftrace_iterator *iter;
918 int ret = 0;
919
920 if (unlikely(ftrace_disabled))
921 return -ENODEV;
922
923 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
924 if (!iter)
925 return -ENOMEM;
926
927 mutex_lock(&ftrace_regex_lock);
928 if ((file->f_mode & FMODE_WRITE) &&
929 !(file->f_flags & O_APPEND))
930 ftrace_filter_reset(enable);
931
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;
937
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);
947
948 return ret;
949 }
950
951 static int
952 ftrace_filter_open(struct inode *inode, struct file *file)
953 {
954 return ftrace_regex_open(inode, file, 1);
955 }
956
957 static int
958 ftrace_notrace_open(struct inode *inode, struct file *file)
959 {
960 return ftrace_regex_open(inode, file, 0);
961 }
962
963 static ssize_t
964 ftrace_regex_read(struct file *file, char __user *ubuf,
965 size_t cnt, loff_t *ppos)
966 {
967 if (file->f_mode & FMODE_READ)
968 return seq_read(file, ubuf, cnt, ppos);
969 else
970 return -EPERM;
971 }
972
973 static loff_t
974 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
975 {
976 loff_t ret;
977
978 if (file->f_mode & FMODE_READ)
979 ret = seq_lseek(file, offset, origin);
980 else
981 file->f_pos = ret = 1;
982
983 return ret;
984 }
985
986 enum {
987 MATCH_FULL,
988 MATCH_FRONT_ONLY,
989 MATCH_MIDDLE_ONLY,
990 MATCH_END_ONLY,
991 };
992
993 static void
994 ftrace_match(unsigned char *buff, int len, int enable)
995 {
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;
1003
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;
1016 }
1017 buff[i] = 0;
1018 break;
1019 }
1020 }
1021 }
1022
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;
1032
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;
1055 }
1056 if (matched)
1057 rec->flags |= flag;
1058 }
1059 pg = pg->next;
1060 }
1061 preempt_enable();
1062 }
1063
1064 static ssize_t
1065 ftrace_regex_write(struct file *file, const char __user *ubuf,
1066 size_t cnt, loff_t *ppos, int enable)
1067 {
1068 struct ftrace_iterator *iter;
1069 char ch;
1070 size_t read = 0;
1071 ssize_t ret;
1072
1073 if (!cnt || cnt < 0)
1074 return 0;
1075
1076 mutex_lock(&ftrace_regex_lock);
1077
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;
1083
1084 if (!*ppos) {
1085 iter->flags &= ~FTRACE_ITER_CONT;
1086 iter->buffer_idx = 0;
1087 }
1088
1089 ret = get_user(ch, ubuf++);
1090 if (ret)
1091 goto out;
1092 read++;
1093 cnt--;
1094
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--;
1103 }
1104
1105 if (isspace(ch)) {
1106 file->f_pos += read;
1107 ret = read;
1108 goto out;
1109 }
1110
1111 iter->buffer_idx = 0;
1112 }
1113
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;
1120 }
1121 ret = get_user(ch, ubuf++);
1122 if (ret)
1123 goto out;
1124 read++;
1125 cnt--;
1126 }
1127
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;
1135
1136
1137 file->f_pos += read;
1138
1139 ret = read;
1140 out:
1141 mutex_unlock(&ftrace_regex_lock);
1142
1143 return ret;
1144 }
1145
1146 static ssize_t
1147 ftrace_filter_write(struct file *file, const char __user *ubuf,
1148 size_t cnt, loff_t *ppos)
1149 {
1150 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1151 }
1152
1153 static ssize_t
1154 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1155 size_t cnt, loff_t *ppos)
1156 {
1157 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1158 }
1159
1160 static void
1161 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1162 {
1163 if (unlikely(ftrace_disabled))
1164 return;
1165
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);
1172 }
1173
1174 /**
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.
1179 *
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.
1182 */
1183 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1184 {
1185 ftrace_set_regex(buf, len, reset, 1);
1186 }
1187
1188 /**
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.
1193 *
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.
1197 */
1198 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1199 {
1200 ftrace_set_regex(buf, len, reset, 0);
1201 }
1202
1203 static int
1204 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1205 {
1206 struct seq_file *m = (struct seq_file *)file->private_data;
1207 struct ftrace_iterator *iter;
1208
1209 mutex_lock(&ftrace_regex_lock);
1210 if (file->f_mode & FMODE_READ) {
1211 iter = m->private;
1212
1213 seq_release(inode, file);
1214 } else
1215 iter = file->private_data;
1216
1217 if (iter->buffer_idx) {
1218 iter->filtered++;
1219 iter->buffer[iter->buffer_idx] = 0;
1220 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1221 }
1222
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);
1229
1230 kfree(iter);
1231 mutex_unlock(&ftrace_regex_lock);
1232 return 0;
1233 }
1234
1235 static int
1236 ftrace_filter_release(struct inode *inode, struct file *file)
1237 {
1238 return ftrace_regex_release(inode, file, 1);
1239 }
1240
1241 static int
1242 ftrace_notrace_release(struct inode *inode, struct file *file)
1243 {
1244 return ftrace_regex_release(inode, file, 0);
1245 }
1246
1247 static ssize_t
1248 ftraced_read(struct file *filp, char __user *ubuf,
1249 size_t cnt, loff_t *ppos)
1250 {
1251 /* don't worry about races */
1252 char *buf = ftraced_stop ? "disabled\n" : "enabled\n";
1253 int r = strlen(buf);
1254
1255 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1256 }
1257
1258 static ssize_t
1259 ftraced_write(struct file *filp, const char __user *ubuf,
1260 size_t cnt, loff_t *ppos)
1261 {
1262 char buf[64];
1263 long val;
1264 int ret;
1265
1266 if (cnt >= sizeof(buf))
1267 return -EINVAL;
1268
1269 if (copy_from_user(&buf, ubuf, cnt))
1270 return -EFAULT;
1271
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;
1278
1279 ret = strict_strtoul(buf, 10, &val);
1280 if (ret < 0)
1281 return ret;
1282
1283 val = !!val;
1284 }
1285
1286 if (val)
1287 ftrace_enable_daemon();
1288 else
1289 ftrace_disable_daemon();
1290
1291 filp->f_pos += cnt;
1292
1293 return cnt;
1294 }
1295
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,
1301 };
1302
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,
1309 };
1310
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,
1317 };
1318
1319 static struct file_operations ftraced_fops = {
1320 .open = tracing_open_generic,
1321 .read = ftraced_read,
1322 .write = ftraced_write,
1323 };
1324
1325 /**
1326 * ftrace_force_update - force an update to all recording ftrace functions
1327 */
1328 int ftrace_force_update(void)
1329 {
1330 int ret = 0;
1331
1332 if (unlikely(ftrace_disabled))
1333 return -ENODEV;
1334
1335 mutex_lock(&ftrace_sysctl_lock);
1336 mutex_lock(&ftraced_lock);
1337
1338 /*
1339 * If ftraced_trigger is not set, then there is nothing
1340 * to update.
1341 */
1342 if (ftraced_trigger && !ftrace_update_code())
1343 ret = -EBUSY;
1344
1345 mutex_unlock(&ftraced_lock);
1346 mutex_unlock(&ftrace_sysctl_lock);
1347
1348 return ret;
1349 }
1350
1351 static void ftrace_force_shutdown(void)
1352 {
1353 struct task_struct *task;
1354 int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC;
1355
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);
1362
1363 if (task)
1364 kthread_stop(task);
1365 }
1366
1367 static __init int ftrace_init_debugfs(void)
1368 {
1369 struct dentry *d_tracer;
1370 struct dentry *entry;
1371
1372 d_tracer = tracing_init_dentry();
1373
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");
1379
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");
1385
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");
1391
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;
1398 }
1399
1400 fs_initcall(ftrace_init_debugfs);
1401
1402 static int __init ftrace_dynamic_init(void)
1403 {
1404 struct task_struct *p;
1405 unsigned long addr;
1406 int ret;
1407
1408 addr = (unsigned long)ftrace_record_ip;
1409
1410 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
1411
1412 /* ftrace_dyn_arch_init places the return code in addr */
1413 if (addr) {
1414 ret = (int)addr;
1415 goto failed;
1416 }
1417
1418 ret = ftrace_dyn_table_alloc();
1419 if (ret)
1420 goto failed;
1421
1422 p = kthread_run(ftraced, NULL, "ftraced");
1423 if (IS_ERR(p)) {
1424 ret = -1;
1425 goto failed;
1426 }
1427
1428 last_ftrace_enabled = ftrace_enabled = 1;
1429 ftraced_task = p;
1430
1431 return 0;
1432
1433 failed:
1434 ftrace_disabled = 1;
1435 return ret;
1436 }
1437
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 */
1446
1447 /**
1448 * ftrace_kill - totally shutdown ftrace
1449 *
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.
1454 */
1455 void ftrace_kill(void)
1456 {
1457 mutex_lock(&ftrace_sysctl_lock);
1458 ftrace_disabled = 1;
1459 ftrace_enabled = 0;
1460
1461 clear_ftrace_function();
1462 mutex_unlock(&ftrace_sysctl_lock);
1463
1464 /* Try to totally disable ftrace */
1465 ftrace_force_shutdown();
1466 }
1467
1468 /**
1469 * register_ftrace_function - register a function for profiling
1470 * @ops - ops structure that holds the function for profiling.
1471 *
1472 * Register a function to be called by all functions in the
1473 * kernel.
1474 *
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.
1478 */
1479 int register_ftrace_function(struct ftrace_ops *ops)
1480 {
1481 int ret;
1482
1483 if (unlikely(ftrace_disabled))
1484 return -1;
1485
1486 mutex_lock(&ftrace_sysctl_lock);
1487 ret = __register_ftrace_function(ops);
1488 ftrace_startup();
1489 mutex_unlock(&ftrace_sysctl_lock);
1490
1491 return ret;
1492 }
1493
1494 /**
1495 * unregister_ftrace_function - unresgister a function for profiling.
1496 * @ops - ops structure that holds the function to unregister
1497 *
1498 * Unregister a function that was added to be called by ftrace profiling.
1499 */
1500 int unregister_ftrace_function(struct ftrace_ops *ops)
1501 {
1502 int ret;
1503
1504 mutex_lock(&ftrace_sysctl_lock);
1505 ret = __unregister_ftrace_function(ops);
1506 ftrace_shutdown();
1507 mutex_unlock(&ftrace_sysctl_lock);
1508
1509 return ret;
1510 }
1511
1512 int
1513 ftrace_enable_sysctl(struct ctl_table *table, int write,
1514 struct file *file, void __user *buffer, size_t *lenp,
1515 loff_t *ppos)
1516 {
1517 int ret;
1518
1519 if (unlikely(ftrace_disabled))
1520 return -ENODEV;
1521
1522 mutex_lock(&ftrace_sysctl_lock);
1523
1524 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
1525
1526 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1527 goto out;
1528
1529 last_ftrace_enabled = ftrace_enabled;
1530
1531 if (ftrace_enabled) {
1532
1533 ftrace_startup_sysctl();
1534
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;
1541 }
1542
1543 } else {
1544 /* stopping ftrace calls (just send to ftrace_stub) */
1545 ftrace_trace_function = ftrace_stub;
1546
1547 ftrace_shutdown_sysctl();
1548 }
1549
1550 out:
1551 mutex_unlock(&ftrace_sysctl_lock);
1552 return ret;
1553 }
This page took 0.087545 seconds and 6 git commands to generate.