ftrace: fix build failure
[linux-2.6] / 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/kprobes.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/ctype.h>
28 #include <linux/list.h>
29
30 #include <asm/ftrace.h>
31
32 #include "trace.h"
33
34 #define FTRACE_WARN_ON(cond)                    \
35         do {                                    \
36                 if (WARN_ON(cond))              \
37                         ftrace_kill();          \
38         } while (0)
39
40 #define FTRACE_WARN_ON_ONCE(cond)               \
41         do {                                    \
42                 if (WARN_ON_ONCE(cond))         \
43                         ftrace_kill();          \
44         } while (0)
45
46 /* ftrace_enabled is a method to turn ftrace on or off */
47 int ftrace_enabled __read_mostly;
48 static int last_ftrace_enabled;
49
50 /*
51  * ftrace_disabled is set when an anomaly is discovered.
52  * ftrace_disabled is much stronger than ftrace_enabled.
53  */
54 static int ftrace_disabled __read_mostly;
55
56 static DEFINE_SPINLOCK(ftrace_lock);
57 static DEFINE_MUTEX(ftrace_sysctl_lock);
58
59 static struct ftrace_ops ftrace_list_end __read_mostly =
60 {
61         .func = ftrace_stub,
62 };
63
64 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
65 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
66
67 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
68 {
69         struct ftrace_ops *op = ftrace_list;
70
71         /* in case someone actually ports this to alpha! */
72         read_barrier_depends();
73
74         while (op != &ftrace_list_end) {
75                 /* silly alpha */
76                 read_barrier_depends();
77                 op->func(ip, parent_ip);
78                 op = op->next;
79         };
80 }
81
82 /**
83  * clear_ftrace_function - reset the ftrace function
84  *
85  * This NULLs the ftrace function and in essence stops
86  * tracing.  There may be lag
87  */
88 void clear_ftrace_function(void)
89 {
90         ftrace_trace_function = ftrace_stub;
91 }
92
93 static int __register_ftrace_function(struct ftrace_ops *ops)
94 {
95         /* should not be called from interrupt context */
96         spin_lock(&ftrace_lock);
97
98         ops->next = ftrace_list;
99         /*
100          * We are entering ops into the ftrace_list but another
101          * CPU might be walking that list. We need to make sure
102          * the ops->next pointer is valid before another CPU sees
103          * the ops pointer included into the ftrace_list.
104          */
105         smp_wmb();
106         ftrace_list = ops;
107
108         if (ftrace_enabled) {
109                 /*
110                  * For one func, simply call it directly.
111                  * For more than one func, call the chain.
112                  */
113                 if (ops->next == &ftrace_list_end)
114                         ftrace_trace_function = ops->func;
115                 else
116                         ftrace_trace_function = ftrace_list_func;
117         }
118
119         spin_unlock(&ftrace_lock);
120
121         return 0;
122 }
123
124 static int __unregister_ftrace_function(struct ftrace_ops *ops)
125 {
126         struct ftrace_ops **p;
127         int ret = 0;
128
129         /* should not be called from interrupt context */
130         spin_lock(&ftrace_lock);
131
132         /*
133          * If we are removing the last function, then simply point
134          * to the ftrace_stub.
135          */
136         if (ftrace_list == ops && ops->next == &ftrace_list_end) {
137                 ftrace_trace_function = ftrace_stub;
138                 ftrace_list = &ftrace_list_end;
139                 goto out;
140         }
141
142         for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
143                 if (*p == ops)
144                         break;
145
146         if (*p != ops) {
147                 ret = -1;
148                 goto out;
149         }
150
151         *p = (*p)->next;
152
153         if (ftrace_enabled) {
154                 /* If we only have one func left, then call that directly */
155                 if (ftrace_list == &ftrace_list_end ||
156                     ftrace_list->next == &ftrace_list_end)
157                         ftrace_trace_function = ftrace_list->func;
158         }
159
160  out:
161         spin_unlock(&ftrace_lock);
162
163         return ret;
164 }
165
166 #ifdef CONFIG_DYNAMIC_FTRACE
167 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
168 # error Dynamic ftrace depends on MCOUNT_RECORD
169 #endif
170
171 /*
172  * Since MCOUNT_ADDR may point to mcount itself, we do not want
173  * to get it confused by reading a reference in the code as we
174  * are parsing on objcopy output of text. Use a variable for
175  * it instead.
176  */
177 static unsigned long mcount_addr = MCOUNT_ADDR;
178
179 enum {
180         FTRACE_ENABLE_CALLS             = (1 << 0),
181         FTRACE_DISABLE_CALLS            = (1 << 1),
182         FTRACE_UPDATE_TRACE_FUNC        = (1 << 2),
183         FTRACE_ENABLE_MCOUNT            = (1 << 3),
184         FTRACE_DISABLE_MCOUNT           = (1 << 4),
185 };
186
187 static int ftrace_filtered;
188 static int tracing_on;
189 static int frozen_record_count;
190
191 static LIST_HEAD(ftrace_new_addrs);
192
193 static DEFINE_MUTEX(ftrace_regex_lock);
194
195 struct ftrace_page {
196         struct ftrace_page      *next;
197         unsigned long           index;
198         struct dyn_ftrace       records[];
199 };
200
201 #define ENTRIES_PER_PAGE \
202   ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
203
204 /* estimate from running different kernels */
205 #define NR_TO_INIT              10000
206
207 static struct ftrace_page       *ftrace_pages_start;
208 static struct ftrace_page       *ftrace_pages;
209
210 static struct dyn_ftrace *ftrace_free_records;
211
212
213 #ifdef CONFIG_KPROBES
214 static inline void freeze_record(struct dyn_ftrace *rec)
215 {
216         if (!(rec->flags & FTRACE_FL_FROZEN)) {
217                 rec->flags |= FTRACE_FL_FROZEN;
218                 frozen_record_count++;
219         }
220 }
221
222 static inline void unfreeze_record(struct dyn_ftrace *rec)
223 {
224         if (rec->flags & FTRACE_FL_FROZEN) {
225                 rec->flags &= ~FTRACE_FL_FROZEN;
226                 frozen_record_count--;
227         }
228 }
229
230 static inline int record_frozen(struct dyn_ftrace *rec)
231 {
232         return rec->flags & FTRACE_FL_FROZEN;
233 }
234 #else
235 # define freeze_record(rec)                     ({ 0; })
236 # define unfreeze_record(rec)                   ({ 0; })
237 # define record_frozen(rec)                     ({ 0; })
238 #endif /* CONFIG_KPROBES */
239
240 static void ftrace_free_rec(struct dyn_ftrace *rec)
241 {
242         rec->ip = (unsigned long)ftrace_free_records;
243         ftrace_free_records = rec;
244         rec->flags |= FTRACE_FL_FREE;
245 }
246
247 void ftrace_release(void *start, unsigned long size)
248 {
249         struct dyn_ftrace *rec;
250         struct ftrace_page *pg;
251         unsigned long s = (unsigned long)start;
252         unsigned long e = s + size;
253         int i;
254
255         if (ftrace_disabled || !start)
256                 return;
257
258         /* should not be called from interrupt context */
259         spin_lock(&ftrace_lock);
260
261         for (pg = ftrace_pages_start; pg; pg = pg->next) {
262                 for (i = 0; i < pg->index; i++) {
263                         rec = &pg->records[i];
264
265                         if ((rec->ip >= s) && (rec->ip < e))
266                                 ftrace_free_rec(rec);
267                 }
268         }
269         spin_unlock(&ftrace_lock);
270 }
271
272 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
273 {
274         struct dyn_ftrace *rec;
275
276         /* First check for freed records */
277         if (ftrace_free_records) {
278                 rec = ftrace_free_records;
279
280                 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
281                         FTRACE_WARN_ON_ONCE(1);
282                         ftrace_free_records = NULL;
283                         return NULL;
284                 }
285
286                 ftrace_free_records = (void *)rec->ip;
287                 memset(rec, 0, sizeof(*rec));
288                 return rec;
289         }
290
291         if (ftrace_pages->index == ENTRIES_PER_PAGE) {
292                 if (!ftrace_pages->next) {
293                         /* allocate another page */
294                         ftrace_pages->next =
295                                 (void *)get_zeroed_page(GFP_KERNEL);
296                         if (!ftrace_pages->next)
297                                 return NULL;
298                 }
299                 ftrace_pages = ftrace_pages->next;
300         }
301
302         return &ftrace_pages->records[ftrace_pages->index++];
303 }
304
305 static struct dyn_ftrace *
306 ftrace_record_ip(unsigned long ip)
307 {
308         struct dyn_ftrace *rec;
309
310         if (!ftrace_enabled || ftrace_disabled)
311                 return NULL;
312
313         rec = ftrace_alloc_dyn_node(ip);
314         if (!rec)
315                 return NULL;
316
317         rec->ip = ip;
318
319         list_add(&rec->list, &ftrace_new_addrs);
320
321         return rec;
322 }
323
324 #define FTRACE_ADDR ((long)(ftrace_caller))
325
326 static int
327 __ftrace_replace_code(struct dyn_ftrace *rec,
328                       unsigned char *old, unsigned char *new, int enable)
329 {
330         unsigned long ip, fl;
331
332         ip = rec->ip;
333
334         if (ftrace_filtered && enable) {
335                 /*
336                  * If filtering is on:
337                  *
338                  * If this record is set to be filtered and
339                  * is enabled then do nothing.
340                  *
341                  * If this record is set to be filtered and
342                  * it is not enabled, enable it.
343                  *
344                  * If this record is not set to be filtered
345                  * and it is not enabled do nothing.
346                  *
347                  * If this record is set not to trace then
348                  * do nothing.
349                  *
350                  * If this record is set not to trace and
351                  * it is enabled then disable it.
352                  *
353                  * If this record is not set to be filtered and
354                  * it is enabled, disable it.
355                  */
356
357                 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
358                                    FTRACE_FL_ENABLED);
359
360                 if ((fl ==  (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
361                     (fl ==  (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
362                     !fl || (fl == FTRACE_FL_NOTRACE))
363                         return 0;
364
365                 /*
366                  * If it is enabled disable it,
367                  * otherwise enable it!
368                  */
369                 if (fl & FTRACE_FL_ENABLED) {
370                         /* swap new and old */
371                         new = old;
372                         old = ftrace_call_replace(ip, FTRACE_ADDR);
373                         rec->flags &= ~FTRACE_FL_ENABLED;
374                 } else {
375                         new = ftrace_call_replace(ip, FTRACE_ADDR);
376                         rec->flags |= FTRACE_FL_ENABLED;
377                 }
378         } else {
379
380                 if (enable) {
381                         /*
382                          * If this record is set not to trace and is
383                          * not enabled, do nothing.
384                          */
385                         fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED);
386                         if (fl == FTRACE_FL_NOTRACE)
387                                 return 0;
388
389                         new = ftrace_call_replace(ip, FTRACE_ADDR);
390                 } else
391                         old = ftrace_call_replace(ip, FTRACE_ADDR);
392
393                 if (enable) {
394                         if (rec->flags & FTRACE_FL_ENABLED)
395                                 return 0;
396                         rec->flags |= FTRACE_FL_ENABLED;
397                 } else {
398                         if (!(rec->flags & FTRACE_FL_ENABLED))
399                                 return 0;
400                         rec->flags &= ~FTRACE_FL_ENABLED;
401                 }
402         }
403
404         return ftrace_modify_code(ip, old, new);
405 }
406
407 static void ftrace_replace_code(int enable)
408 {
409         int i, failed;
410         unsigned char *new = NULL, *old = NULL;
411         struct dyn_ftrace *rec;
412         struct ftrace_page *pg;
413
414         if (enable)
415                 old = ftrace_nop_replace();
416         else
417                 new = ftrace_nop_replace();
418
419         for (pg = ftrace_pages_start; pg; pg = pg->next) {
420                 for (i = 0; i < pg->index; i++) {
421                         rec = &pg->records[i];
422
423                         /* don't modify code that has already faulted */
424                         if (rec->flags & FTRACE_FL_FAILED)
425                                 continue;
426
427                         /* ignore updates to this record's mcount site */
428                         if (get_kprobe((void *)rec->ip)) {
429                                 freeze_record(rec);
430                                 continue;
431                         } else {
432                                 unfreeze_record(rec);
433                         }
434
435                         failed = __ftrace_replace_code(rec, old, new, enable);
436                         if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
437                                 rec->flags |= FTRACE_FL_FAILED;
438                                 if ((system_state == SYSTEM_BOOTING) ||
439                                     !core_kernel_text(rec->ip)) {
440                                         ftrace_free_rec(rec);
441                                 }
442                         }
443                 }
444         }
445 }
446
447 static void print_ip_ins(const char *fmt, unsigned char *p)
448 {
449         int i;
450
451         printk(KERN_CONT "%s", fmt);
452
453         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
454                 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
455 }
456
457 static int
458 ftrace_code_disable(struct dyn_ftrace *rec)
459 {
460         unsigned long ip;
461         unsigned char *nop, *call;
462         int ret;
463
464         ip = rec->ip;
465
466         nop = ftrace_nop_replace();
467         call = ftrace_call_replace(ip, mcount_addr);
468
469         ret = ftrace_modify_code(ip, call, nop);
470         if (ret) {
471                 switch (ret) {
472                 case -EFAULT:
473                         FTRACE_WARN_ON_ONCE(1);
474                         pr_info("ftrace faulted on modifying ");
475                         print_ip_sym(ip);
476                         break;
477                 case -EINVAL:
478                         FTRACE_WARN_ON_ONCE(1);
479                         pr_info("ftrace failed to modify ");
480                         print_ip_sym(ip);
481                         print_ip_ins(" expected: ", call);
482                         print_ip_ins(" actual: ", (unsigned char *)ip);
483                         print_ip_ins(" replace: ", nop);
484                         printk(KERN_CONT "\n");
485                         break;
486                 case -EPERM:
487                         FTRACE_WARN_ON_ONCE(1);
488                         pr_info("ftrace faulted on writing ");
489                         print_ip_sym(ip);
490                         break;
491                 default:
492                         FTRACE_WARN_ON_ONCE(1);
493                         pr_info("ftrace faulted on unknown error ");
494                         print_ip_sym(ip);
495                 }
496
497                 rec->flags |= FTRACE_FL_FAILED;
498                 return 0;
499         }
500         return 1;
501 }
502
503 static int __ftrace_modify_code(void *data)
504 {
505         int *command = data;
506
507         if (*command & FTRACE_ENABLE_CALLS) {
508                 ftrace_replace_code(1);
509                 tracing_on = 1;
510         } else if (*command & FTRACE_DISABLE_CALLS) {
511                 ftrace_replace_code(0);
512                 tracing_on = 0;
513         }
514
515         if (*command & FTRACE_UPDATE_TRACE_FUNC)
516                 ftrace_update_ftrace_func(ftrace_trace_function);
517
518         return 0;
519 }
520
521 static void ftrace_run_update_code(int command)
522 {
523         stop_machine(__ftrace_modify_code, &command, NULL);
524 }
525
526 static ftrace_func_t saved_ftrace_func;
527 static int ftrace_start;
528 static DEFINE_MUTEX(ftrace_start_lock);
529
530 static void ftrace_startup(void)
531 {
532         int command = 0;
533
534         if (unlikely(ftrace_disabled))
535                 return;
536
537         mutex_lock(&ftrace_start_lock);
538         ftrace_start++;
539         if (ftrace_start == 1)
540                 command |= FTRACE_ENABLE_CALLS;
541
542         if (saved_ftrace_func != ftrace_trace_function) {
543                 saved_ftrace_func = ftrace_trace_function;
544                 command |= FTRACE_UPDATE_TRACE_FUNC;
545         }
546
547         if (!command || !ftrace_enabled)
548                 goto out;
549
550         ftrace_run_update_code(command);
551  out:
552         mutex_unlock(&ftrace_start_lock);
553 }
554
555 static void ftrace_shutdown(void)
556 {
557         int command = 0;
558
559         if (unlikely(ftrace_disabled))
560                 return;
561
562         mutex_lock(&ftrace_start_lock);
563         ftrace_start--;
564         if (!ftrace_start)
565                 command |= FTRACE_DISABLE_CALLS;
566
567         if (saved_ftrace_func != ftrace_trace_function) {
568                 saved_ftrace_func = ftrace_trace_function;
569                 command |= FTRACE_UPDATE_TRACE_FUNC;
570         }
571
572         if (!command || !ftrace_enabled)
573                 goto out;
574
575         ftrace_run_update_code(command);
576  out:
577         mutex_unlock(&ftrace_start_lock);
578 }
579
580 static void ftrace_startup_sysctl(void)
581 {
582         int command = FTRACE_ENABLE_MCOUNT;
583
584         if (unlikely(ftrace_disabled))
585                 return;
586
587         mutex_lock(&ftrace_start_lock);
588         /* Force update next time */
589         saved_ftrace_func = NULL;
590         /* ftrace_start is true if we want ftrace running */
591         if (ftrace_start)
592                 command |= FTRACE_ENABLE_CALLS;
593
594         ftrace_run_update_code(command);
595         mutex_unlock(&ftrace_start_lock);
596 }
597
598 static void ftrace_shutdown_sysctl(void)
599 {
600         int command = FTRACE_DISABLE_MCOUNT;
601
602         if (unlikely(ftrace_disabled))
603                 return;
604
605         mutex_lock(&ftrace_start_lock);
606         /* ftrace_start is true if ftrace is running */
607         if (ftrace_start)
608                 command |= FTRACE_DISABLE_CALLS;
609
610         ftrace_run_update_code(command);
611         mutex_unlock(&ftrace_start_lock);
612 }
613
614 static cycle_t          ftrace_update_time;
615 static unsigned long    ftrace_update_cnt;
616 unsigned long           ftrace_update_tot_cnt;
617
618 static int ftrace_update_code(void)
619 {
620         struct dyn_ftrace *p, *t;
621         cycle_t start, stop;
622
623         start = ftrace_now(raw_smp_processor_id());
624         ftrace_update_cnt = 0;
625
626         list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
627
628                 /* If something went wrong, bail without enabling anything */
629                 if (unlikely(ftrace_disabled))
630                         return -1;
631
632                 list_del_init(&p->list);
633
634                 /* convert record (i.e, patch mcount-call with NOP) */
635                 if (ftrace_code_disable(p)) {
636                         p->flags |= FTRACE_FL_CONVERTED;
637                         ftrace_update_cnt++;
638                 } else
639                         ftrace_free_rec(p);
640         }
641
642         stop = ftrace_now(raw_smp_processor_id());
643         ftrace_update_time = stop - start;
644         ftrace_update_tot_cnt += ftrace_update_cnt;
645
646         return 0;
647 }
648
649 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
650 {
651         struct ftrace_page *pg;
652         int cnt;
653         int i;
654
655         /* allocate a few pages */
656         ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
657         if (!ftrace_pages_start)
658                 return -1;
659
660         /*
661          * Allocate a few more pages.
662          *
663          * TODO: have some parser search vmlinux before
664          *   final linking to find all calls to ftrace.
665          *   Then we can:
666          *    a) know how many pages to allocate.
667          *     and/or
668          *    b) set up the table then.
669          *
670          *  The dynamic code is still necessary for
671          *  modules.
672          */
673
674         pg = ftrace_pages = ftrace_pages_start;
675
676         cnt = num_to_init / ENTRIES_PER_PAGE;
677         pr_info("ftrace: allocating %ld entries in %d pages\n",
678                 num_to_init, cnt);
679
680         for (i = 0; i < cnt; i++) {
681                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
682
683                 /* If we fail, we'll try later anyway */
684                 if (!pg->next)
685                         break;
686
687                 pg = pg->next;
688         }
689
690         return 0;
691 }
692
693 enum {
694         FTRACE_ITER_FILTER      = (1 << 0),
695         FTRACE_ITER_CONT        = (1 << 1),
696         FTRACE_ITER_NOTRACE     = (1 << 2),
697         FTRACE_ITER_FAILURES    = (1 << 3),
698 };
699
700 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
701
702 struct ftrace_iterator {
703         loff_t                  pos;
704         struct ftrace_page      *pg;
705         unsigned                idx;
706         unsigned                flags;
707         unsigned char           buffer[FTRACE_BUFF_MAX+1];
708         unsigned                buffer_idx;
709         unsigned                filtered;
710 };
711
712 static void *
713 t_next(struct seq_file *m, void *v, loff_t *pos)
714 {
715         struct ftrace_iterator *iter = m->private;
716         struct dyn_ftrace *rec = NULL;
717
718         (*pos)++;
719
720         /* should not be called from interrupt context */
721         spin_lock(&ftrace_lock);
722  retry:
723         if (iter->idx >= iter->pg->index) {
724                 if (iter->pg->next) {
725                         iter->pg = iter->pg->next;
726                         iter->idx = 0;
727                         goto retry;
728                 }
729         } else {
730                 rec = &iter->pg->records[iter->idx++];
731                 if ((rec->flags & FTRACE_FL_FREE) ||
732
733                     (!(iter->flags & FTRACE_ITER_FAILURES) &&
734                      (rec->flags & FTRACE_FL_FAILED)) ||
735
736                     ((iter->flags & FTRACE_ITER_FAILURES) &&
737                      !(rec->flags & FTRACE_FL_FAILED)) ||
738
739                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
740                      !(rec->flags & FTRACE_FL_NOTRACE))) {
741                         rec = NULL;
742                         goto retry;
743                 }
744         }
745         spin_unlock(&ftrace_lock);
746
747         iter->pos = *pos;
748
749         return rec;
750 }
751
752 static void *t_start(struct seq_file *m, loff_t *pos)
753 {
754         struct ftrace_iterator *iter = m->private;
755         void *p = NULL;
756         loff_t l = -1;
757
758         if (*pos != iter->pos) {
759                 for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l))
760                         ;
761         } else {
762                 l = *pos;
763                 p = t_next(m, p, &l);
764         }
765
766         return p;
767 }
768
769 static void t_stop(struct seq_file *m, void *p)
770 {
771 }
772
773 static int t_show(struct seq_file *m, void *v)
774 {
775         struct dyn_ftrace *rec = v;
776         char str[KSYM_SYMBOL_LEN];
777
778         if (!rec)
779                 return 0;
780
781         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
782
783         seq_printf(m, "%s\n", str);
784
785         return 0;
786 }
787
788 static struct seq_operations show_ftrace_seq_ops = {
789         .start = t_start,
790         .next = t_next,
791         .stop = t_stop,
792         .show = t_show,
793 };
794
795 static int
796 ftrace_avail_open(struct inode *inode, struct file *file)
797 {
798         struct ftrace_iterator *iter;
799         int ret;
800
801         if (unlikely(ftrace_disabled))
802                 return -ENODEV;
803
804         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
805         if (!iter)
806                 return -ENOMEM;
807
808         iter->pg = ftrace_pages_start;
809         iter->pos = -1;
810
811         ret = seq_open(file, &show_ftrace_seq_ops);
812         if (!ret) {
813                 struct seq_file *m = file->private_data;
814
815                 m->private = iter;
816         } else {
817                 kfree(iter);
818         }
819
820         return ret;
821 }
822
823 int ftrace_avail_release(struct inode *inode, struct file *file)
824 {
825         struct seq_file *m = (struct seq_file *)file->private_data;
826         struct ftrace_iterator *iter = m->private;
827
828         seq_release(inode, file);
829         kfree(iter);
830
831         return 0;
832 }
833
834 static int
835 ftrace_failures_open(struct inode *inode, struct file *file)
836 {
837         int ret;
838         struct seq_file *m;
839         struct ftrace_iterator *iter;
840
841         ret = ftrace_avail_open(inode, file);
842         if (!ret) {
843                 m = (struct seq_file *)file->private_data;
844                 iter = (struct ftrace_iterator *)m->private;
845                 iter->flags = FTRACE_ITER_FAILURES;
846         }
847
848         return ret;
849 }
850
851
852 static void ftrace_filter_reset(int enable)
853 {
854         struct ftrace_page *pg;
855         struct dyn_ftrace *rec;
856         unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
857         unsigned i;
858
859         /* should not be called from interrupt context */
860         spin_lock(&ftrace_lock);
861         if (enable)
862                 ftrace_filtered = 0;
863         pg = ftrace_pages_start;
864         while (pg) {
865                 for (i = 0; i < pg->index; i++) {
866                         rec = &pg->records[i];
867                         if (rec->flags & FTRACE_FL_FAILED)
868                                 continue;
869                         rec->flags &= ~type;
870                 }
871                 pg = pg->next;
872         }
873         spin_unlock(&ftrace_lock);
874 }
875
876 static int
877 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
878 {
879         struct ftrace_iterator *iter;
880         int ret = 0;
881
882         if (unlikely(ftrace_disabled))
883                 return -ENODEV;
884
885         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
886         if (!iter)
887                 return -ENOMEM;
888
889         mutex_lock(&ftrace_regex_lock);
890         if ((file->f_mode & FMODE_WRITE) &&
891             !(file->f_flags & O_APPEND))
892                 ftrace_filter_reset(enable);
893
894         if (file->f_mode & FMODE_READ) {
895                 iter->pg = ftrace_pages_start;
896                 iter->pos = -1;
897                 iter->flags = enable ? FTRACE_ITER_FILTER :
898                         FTRACE_ITER_NOTRACE;
899
900                 ret = seq_open(file, &show_ftrace_seq_ops);
901                 if (!ret) {
902                         struct seq_file *m = file->private_data;
903                         m->private = iter;
904                 } else
905                         kfree(iter);
906         } else
907                 file->private_data = iter;
908         mutex_unlock(&ftrace_regex_lock);
909
910         return ret;
911 }
912
913 static int
914 ftrace_filter_open(struct inode *inode, struct file *file)
915 {
916         return ftrace_regex_open(inode, file, 1);
917 }
918
919 static int
920 ftrace_notrace_open(struct inode *inode, struct file *file)
921 {
922         return ftrace_regex_open(inode, file, 0);
923 }
924
925 static ssize_t
926 ftrace_regex_read(struct file *file, char __user *ubuf,
927                        size_t cnt, loff_t *ppos)
928 {
929         if (file->f_mode & FMODE_READ)
930                 return seq_read(file, ubuf, cnt, ppos);
931         else
932                 return -EPERM;
933 }
934
935 static loff_t
936 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
937 {
938         loff_t ret;
939
940         if (file->f_mode & FMODE_READ)
941                 ret = seq_lseek(file, offset, origin);
942         else
943                 file->f_pos = ret = 1;
944
945         return ret;
946 }
947
948 enum {
949         MATCH_FULL,
950         MATCH_FRONT_ONLY,
951         MATCH_MIDDLE_ONLY,
952         MATCH_END_ONLY,
953 };
954
955 static void
956 ftrace_match(unsigned char *buff, int len, int enable)
957 {
958         char str[KSYM_SYMBOL_LEN];
959         char *search = NULL;
960         struct ftrace_page *pg;
961         struct dyn_ftrace *rec;
962         int type = MATCH_FULL;
963         unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
964         unsigned i, match = 0, search_len = 0;
965
966         for (i = 0; i < len; i++) {
967                 if (buff[i] == '*') {
968                         if (!i) {
969                                 search = buff + i + 1;
970                                 type = MATCH_END_ONLY;
971                                 search_len = len - (i + 1);
972                         } else {
973                                 if (type == MATCH_END_ONLY) {
974                                         type = MATCH_MIDDLE_ONLY;
975                                 } else {
976                                         match = i;
977                                         type = MATCH_FRONT_ONLY;
978                                 }
979                                 buff[i] = 0;
980                                 break;
981                         }
982                 }
983         }
984
985         /* should not be called from interrupt context */
986         spin_lock(&ftrace_lock);
987         if (enable)
988                 ftrace_filtered = 1;
989         pg = ftrace_pages_start;
990         while (pg) {
991                 for (i = 0; i < pg->index; i++) {
992                         int matched = 0;
993                         char *ptr;
994
995                         rec = &pg->records[i];
996                         if (rec->flags & FTRACE_FL_FAILED)
997                                 continue;
998                         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
999                         switch (type) {
1000                         case MATCH_FULL:
1001                                 if (strcmp(str, buff) == 0)
1002                                         matched = 1;
1003                                 break;
1004                         case MATCH_FRONT_ONLY:
1005                                 if (memcmp(str, buff, match) == 0)
1006                                         matched = 1;
1007                                 break;
1008                         case MATCH_MIDDLE_ONLY:
1009                                 if (strstr(str, search))
1010                                         matched = 1;
1011                                 break;
1012                         case MATCH_END_ONLY:
1013                                 ptr = strstr(str, search);
1014                                 if (ptr && (ptr[search_len] == 0))
1015                                         matched = 1;
1016                                 break;
1017                         }
1018                         if (matched)
1019                                 rec->flags |= flag;
1020                 }
1021                 pg = pg->next;
1022         }
1023         spin_unlock(&ftrace_lock);
1024 }
1025
1026 static ssize_t
1027 ftrace_regex_write(struct file *file, const char __user *ubuf,
1028                    size_t cnt, loff_t *ppos, int enable)
1029 {
1030         struct ftrace_iterator *iter;
1031         char ch;
1032         size_t read = 0;
1033         ssize_t ret;
1034
1035         if (!cnt || cnt < 0)
1036                 return 0;
1037
1038         mutex_lock(&ftrace_regex_lock);
1039
1040         if (file->f_mode & FMODE_READ) {
1041                 struct seq_file *m = file->private_data;
1042                 iter = m->private;
1043         } else
1044                 iter = file->private_data;
1045
1046         if (!*ppos) {
1047                 iter->flags &= ~FTRACE_ITER_CONT;
1048                 iter->buffer_idx = 0;
1049         }
1050
1051         ret = get_user(ch, ubuf++);
1052         if (ret)
1053                 goto out;
1054         read++;
1055         cnt--;
1056
1057         if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1058                 /* skip white space */
1059                 while (cnt && isspace(ch)) {
1060                         ret = get_user(ch, ubuf++);
1061                         if (ret)
1062                                 goto out;
1063                         read++;
1064                         cnt--;
1065                 }
1066
1067                 if (isspace(ch)) {
1068                         file->f_pos += read;
1069                         ret = read;
1070                         goto out;
1071                 }
1072
1073                 iter->buffer_idx = 0;
1074         }
1075
1076         while (cnt && !isspace(ch)) {
1077                 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1078                         iter->buffer[iter->buffer_idx++] = ch;
1079                 else {
1080                         ret = -EINVAL;
1081                         goto out;
1082                 }
1083                 ret = get_user(ch, ubuf++);
1084                 if (ret)
1085                         goto out;
1086                 read++;
1087                 cnt--;
1088         }
1089
1090         if (isspace(ch)) {
1091                 iter->filtered++;
1092                 iter->buffer[iter->buffer_idx] = 0;
1093                 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1094                 iter->buffer_idx = 0;
1095         } else
1096                 iter->flags |= FTRACE_ITER_CONT;
1097
1098
1099         file->f_pos += read;
1100
1101         ret = read;
1102  out:
1103         mutex_unlock(&ftrace_regex_lock);
1104
1105         return ret;
1106 }
1107
1108 static ssize_t
1109 ftrace_filter_write(struct file *file, const char __user *ubuf,
1110                     size_t cnt, loff_t *ppos)
1111 {
1112         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1113 }
1114
1115 static ssize_t
1116 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1117                      size_t cnt, loff_t *ppos)
1118 {
1119         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1120 }
1121
1122 static void
1123 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1124 {
1125         if (unlikely(ftrace_disabled))
1126                 return;
1127
1128         mutex_lock(&ftrace_regex_lock);
1129         if (reset)
1130                 ftrace_filter_reset(enable);
1131         if (buf)
1132                 ftrace_match(buf, len, enable);
1133         mutex_unlock(&ftrace_regex_lock);
1134 }
1135
1136 /**
1137  * ftrace_set_filter - set a function to filter on in ftrace
1138  * @buf - the string that holds the function filter text.
1139  * @len - the length of the string.
1140  * @reset - non zero to reset all filters before applying this filter.
1141  *
1142  * Filters denote which functions should be enabled when tracing is enabled.
1143  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1144  */
1145 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1146 {
1147         ftrace_set_regex(buf, len, reset, 1);
1148 }
1149
1150 /**
1151  * ftrace_set_notrace - set a function to not trace in ftrace
1152  * @buf - the string that holds the function notrace text.
1153  * @len - the length of the string.
1154  * @reset - non zero to reset all filters before applying this filter.
1155  *
1156  * Notrace Filters denote which functions should not be enabled when tracing
1157  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1158  * for tracing.
1159  */
1160 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1161 {
1162         ftrace_set_regex(buf, len, reset, 0);
1163 }
1164
1165 static int
1166 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1167 {
1168         struct seq_file *m = (struct seq_file *)file->private_data;
1169         struct ftrace_iterator *iter;
1170
1171         mutex_lock(&ftrace_regex_lock);
1172         if (file->f_mode & FMODE_READ) {
1173                 iter = m->private;
1174
1175                 seq_release(inode, file);
1176         } else
1177                 iter = file->private_data;
1178
1179         if (iter->buffer_idx) {
1180                 iter->filtered++;
1181                 iter->buffer[iter->buffer_idx] = 0;
1182                 ftrace_match(iter->buffer, iter->buffer_idx, enable);
1183         }
1184
1185         mutex_lock(&ftrace_sysctl_lock);
1186         mutex_lock(&ftrace_start_lock);
1187         if (iter->filtered && ftrace_start && ftrace_enabled)
1188                 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1189         mutex_unlock(&ftrace_start_lock);
1190         mutex_unlock(&ftrace_sysctl_lock);
1191
1192         kfree(iter);
1193         mutex_unlock(&ftrace_regex_lock);
1194         return 0;
1195 }
1196
1197 static int
1198 ftrace_filter_release(struct inode *inode, struct file *file)
1199 {
1200         return ftrace_regex_release(inode, file, 1);
1201 }
1202
1203 static int
1204 ftrace_notrace_release(struct inode *inode, struct file *file)
1205 {
1206         return ftrace_regex_release(inode, file, 0);
1207 }
1208
1209 static struct file_operations ftrace_avail_fops = {
1210         .open = ftrace_avail_open,
1211         .read = seq_read,
1212         .llseek = seq_lseek,
1213         .release = ftrace_avail_release,
1214 };
1215
1216 static struct file_operations ftrace_failures_fops = {
1217         .open = ftrace_failures_open,
1218         .read = seq_read,
1219         .llseek = seq_lseek,
1220         .release = ftrace_avail_release,
1221 };
1222
1223 static struct file_operations ftrace_filter_fops = {
1224         .open = ftrace_filter_open,
1225         .read = ftrace_regex_read,
1226         .write = ftrace_filter_write,
1227         .llseek = ftrace_regex_lseek,
1228         .release = ftrace_filter_release,
1229 };
1230
1231 static struct file_operations ftrace_notrace_fops = {
1232         .open = ftrace_notrace_open,
1233         .read = ftrace_regex_read,
1234         .write = ftrace_notrace_write,
1235         .llseek = ftrace_regex_lseek,
1236         .release = ftrace_notrace_release,
1237 };
1238
1239 static __init int ftrace_init_debugfs(void)
1240 {
1241         struct dentry *d_tracer;
1242         struct dentry *entry;
1243
1244         d_tracer = tracing_init_dentry();
1245
1246         entry = debugfs_create_file("available_filter_functions", 0444,
1247                                     d_tracer, NULL, &ftrace_avail_fops);
1248         if (!entry)
1249                 pr_warning("Could not create debugfs "
1250                            "'available_filter_functions' entry\n");
1251
1252         entry = debugfs_create_file("failures", 0444,
1253                                     d_tracer, NULL, &ftrace_failures_fops);
1254         if (!entry)
1255                 pr_warning("Could not create debugfs 'failures' entry\n");
1256
1257         entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
1258                                     NULL, &ftrace_filter_fops);
1259         if (!entry)
1260                 pr_warning("Could not create debugfs "
1261                            "'set_ftrace_filter' entry\n");
1262
1263         entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
1264                                     NULL, &ftrace_notrace_fops);
1265         if (!entry)
1266                 pr_warning("Could not create debugfs "
1267                            "'set_ftrace_notrace' entry\n");
1268
1269         return 0;
1270 }
1271
1272 fs_initcall(ftrace_init_debugfs);
1273
1274 static int ftrace_convert_nops(unsigned long *start,
1275                                unsigned long *end)
1276 {
1277         unsigned long *p;
1278         unsigned long addr;
1279         unsigned long flags;
1280
1281         mutex_lock(&ftrace_start_lock);
1282         p = start;
1283         while (p < end) {
1284                 addr = ftrace_call_adjust(*p++);
1285                 ftrace_record_ip(addr);
1286         }
1287
1288         /* disable interrupts to prevent kstop machine */
1289         local_irq_save(flags);
1290         ftrace_update_code();
1291         local_irq_restore(flags);
1292         mutex_unlock(&ftrace_start_lock);
1293
1294         return 0;
1295 }
1296
1297 void ftrace_init_module(unsigned long *start, unsigned long *end)
1298 {
1299         if (ftrace_disabled || start == end)
1300                 return;
1301         ftrace_convert_nops(start, end);
1302 }
1303
1304 extern unsigned long __start_mcount_loc[];
1305 extern unsigned long __stop_mcount_loc[];
1306
1307 void __init ftrace_init(void)
1308 {
1309         unsigned long count, addr, flags;
1310         int ret;
1311
1312         /* Keep the ftrace pointer to the stub */
1313         addr = (unsigned long)ftrace_stub;
1314
1315         local_irq_save(flags);
1316         ftrace_dyn_arch_init(&addr);
1317         local_irq_restore(flags);
1318
1319         /* ftrace_dyn_arch_init places the return code in addr */
1320         if (addr)
1321                 goto failed;
1322
1323         count = __stop_mcount_loc - __start_mcount_loc;
1324
1325         ret = ftrace_dyn_table_alloc(count);
1326         if (ret)
1327                 goto failed;
1328
1329         last_ftrace_enabled = ftrace_enabled = 1;
1330
1331         ret = ftrace_convert_nops(__start_mcount_loc,
1332                                   __stop_mcount_loc);
1333
1334         return;
1335  failed:
1336         ftrace_disabled = 1;
1337 }
1338
1339 #else
1340 # define ftrace_startup()               do { } while (0)
1341 # define ftrace_shutdown()              do { } while (0)
1342 # define ftrace_startup_sysctl()        do { } while (0)
1343 # define ftrace_shutdown_sysctl()       do { } while (0)
1344 #endif /* CONFIG_DYNAMIC_FTRACE */
1345
1346 /**
1347  * ftrace_kill - kill ftrace
1348  *
1349  * This function should be used by panic code. It stops ftrace
1350  * but in a not so nice way. If you need to simply kill ftrace
1351  * from a non-atomic section, use ftrace_kill.
1352  */
1353 void ftrace_kill(void)
1354 {
1355         ftrace_disabled = 1;
1356         ftrace_enabled = 0;
1357         clear_ftrace_function();
1358 }
1359
1360 /**
1361  * register_ftrace_function - register a function for profiling
1362  * @ops - ops structure that holds the function for profiling.
1363  *
1364  * Register a function to be called by all functions in the
1365  * kernel.
1366  *
1367  * Note: @ops->func and all the functions it calls must be labeled
1368  *       with "notrace", otherwise it will go into a
1369  *       recursive loop.
1370  */
1371 int register_ftrace_function(struct ftrace_ops *ops)
1372 {
1373         int ret;
1374
1375         if (unlikely(ftrace_disabled))
1376                 return -1;
1377
1378         mutex_lock(&ftrace_sysctl_lock);
1379         ret = __register_ftrace_function(ops);
1380         ftrace_startup();
1381         mutex_unlock(&ftrace_sysctl_lock);
1382
1383         return ret;
1384 }
1385
1386 /**
1387  * unregister_ftrace_function - unresgister a function for profiling.
1388  * @ops - ops structure that holds the function to unregister
1389  *
1390  * Unregister a function that was added to be called by ftrace profiling.
1391  */
1392 int unregister_ftrace_function(struct ftrace_ops *ops)
1393 {
1394         int ret;
1395
1396         mutex_lock(&ftrace_sysctl_lock);
1397         ret = __unregister_ftrace_function(ops);
1398         ftrace_shutdown();
1399         mutex_unlock(&ftrace_sysctl_lock);
1400
1401         return ret;
1402 }
1403
1404 int
1405 ftrace_enable_sysctl(struct ctl_table *table, int write,
1406                      struct file *file, void __user *buffer, size_t *lenp,
1407                      loff_t *ppos)
1408 {
1409         int ret;
1410
1411         if (unlikely(ftrace_disabled))
1412                 return -ENODEV;
1413
1414         mutex_lock(&ftrace_sysctl_lock);
1415
1416         ret  = proc_dointvec(table, write, file, buffer, lenp, ppos);
1417
1418         if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
1419                 goto out;
1420
1421         last_ftrace_enabled = ftrace_enabled;
1422
1423         if (ftrace_enabled) {
1424
1425                 ftrace_startup_sysctl();
1426
1427                 /* we are starting ftrace again */
1428                 if (ftrace_list != &ftrace_list_end) {
1429                         if (ftrace_list->next == &ftrace_list_end)
1430                                 ftrace_trace_function = ftrace_list->func;
1431                         else
1432                                 ftrace_trace_function = ftrace_list_func;
1433                 }
1434
1435         } else {
1436                 /* stopping ftrace calls (just send to ftrace_stub) */
1437                 ftrace_trace_function = ftrace_stub;
1438
1439                 ftrace_shutdown_sysctl();
1440         }
1441
1442  out:
1443         mutex_unlock(&ftrace_sysctl_lock);
1444         return ret;
1445 }