]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/trace/trace_functions.c
2c8961b35401f654678cb9f53c694540325b9008
[karo-tx-linux.git] / kernel / trace / trace_functions.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Based on code from the latency_tracer, that is:
8  *
9  *  Copyright (C) 2004-2006 Ingo Molnar
10  *  Copyright (C) 2004 Nadia Yvette Chambers
11  */
12 #include <linux/ring_buffer.h>
13 #include <linux/debugfs.h>
14 #include <linux/uaccess.h>
15 #include <linux/ftrace.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18
19 #include "trace.h"
20
21 static void tracing_start_function_trace(struct trace_array *tr);
22 static void tracing_stop_function_trace(struct trace_array *tr);
23 static void
24 function_trace_call(unsigned long ip, unsigned long parent_ip,
25                     struct ftrace_ops *op, struct pt_regs *pt_regs);
26 static void
27 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
28                           struct ftrace_ops *op, struct pt_regs *pt_regs);
29 static struct tracer_flags func_flags;
30
31 /* Our option */
32 enum {
33         TRACE_FUNC_OPT_STACK    = 0x1,
34 };
35
36 static int allocate_ftrace_ops(struct trace_array *tr)
37 {
38         struct ftrace_ops *ops;
39
40         ops = kzalloc(sizeof(*ops), GFP_KERNEL);
41         if (!ops)
42                 return -ENOMEM;
43
44         /* Currently only the non stack verision is supported */
45         ops->func = function_trace_call;
46         ops->flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_PID;
47
48         tr->ops = ops;
49         ops->private = tr;
50         return 0;
51 }
52
53
54 int ftrace_create_function_files(struct trace_array *tr,
55                                  struct dentry *parent)
56 {
57         int ret;
58
59         /*
60          * The top level array uses the "global_ops", and the files are
61          * created on boot up.
62          */
63         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
64                 return 0;
65
66         ret = allocate_ftrace_ops(tr);
67         if (ret)
68                 return ret;
69
70         ftrace_create_filter_files(tr->ops, parent);
71
72         return 0;
73 }
74
75 void ftrace_destroy_function_files(struct trace_array *tr)
76 {
77         ftrace_destroy_filter_files(tr->ops);
78         kfree(tr->ops);
79         tr->ops = NULL;
80 }
81
82 static int function_trace_init(struct trace_array *tr)
83 {
84         ftrace_func_t func;
85
86         /*
87          * Instance trace_arrays get their ops allocated
88          * at instance creation. Unless it failed
89          * the allocation.
90          */
91         if (!tr->ops)
92                 return -ENOMEM;
93
94         /* Currently only the global instance can do stack tracing */
95         if (tr->flags & TRACE_ARRAY_FL_GLOBAL &&
96             func_flags.val & TRACE_FUNC_OPT_STACK)
97                 func = function_stack_trace_call;
98         else
99                 func = function_trace_call;
100
101         ftrace_init_array_ops(tr, func);
102
103         tr->trace_buffer.cpu = get_cpu();
104         put_cpu();
105
106         tracing_start_cmdline_record();
107         tracing_start_function_trace(tr);
108         return 0;
109 }
110
111 static void function_trace_reset(struct trace_array *tr)
112 {
113         tracing_stop_function_trace(tr);
114         tracing_stop_cmdline_record();
115         ftrace_reset_array_ops(tr);
116 }
117
118 static void function_trace_start(struct trace_array *tr)
119 {
120         tracing_reset_online_cpus(&tr->trace_buffer);
121 }
122
123 static void
124 function_trace_call(unsigned long ip, unsigned long parent_ip,
125                     struct ftrace_ops *op, struct pt_regs *pt_regs)
126 {
127         struct trace_array *tr = op->private;
128         struct trace_array_cpu *data;
129         unsigned long flags;
130         int bit;
131         int cpu;
132         int pc;
133
134         if (unlikely(!tr->function_enabled))
135                 return;
136
137         pc = preempt_count();
138         preempt_disable_notrace();
139
140         bit = trace_test_and_set_recursion(TRACE_FTRACE_START, TRACE_FTRACE_MAX);
141         if (bit < 0)
142                 goto out;
143
144         cpu = smp_processor_id();
145         data = per_cpu_ptr(tr->trace_buffer.data, cpu);
146         if (!atomic_read(&data->disabled)) {
147                 local_save_flags(flags);
148                 trace_function(tr, ip, parent_ip, flags, pc);
149         }
150         trace_clear_recursion(bit);
151
152  out:
153         preempt_enable_notrace();
154 }
155
156 static void
157 function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
158                           struct ftrace_ops *op, struct pt_regs *pt_regs)
159 {
160         struct trace_array *tr = op->private;
161         struct trace_array_cpu *data;
162         unsigned long flags;
163         long disabled;
164         int cpu;
165         int pc;
166
167         if (unlikely(!tr->function_enabled))
168                 return;
169
170         /*
171          * Need to use raw, since this must be called before the
172          * recursive protection is performed.
173          */
174         local_irq_save(flags);
175         cpu = raw_smp_processor_id();
176         data = per_cpu_ptr(tr->trace_buffer.data, cpu);
177         disabled = atomic_inc_return(&data->disabled);
178
179         if (likely(disabled == 1)) {
180                 pc = preempt_count();
181                 trace_function(tr, ip, parent_ip, flags, pc);
182                 /*
183                  * skip over 5 funcs:
184                  *    __ftrace_trace_stack,
185                  *    __trace_stack,
186                  *    function_stack_trace_call
187                  *    ftrace_list_func
188                  *    ftrace_call
189                  */
190                 __trace_stack(tr, flags, 5, pc);
191         }
192
193         atomic_dec(&data->disabled);
194         local_irq_restore(flags);
195 }
196
197 static struct tracer_opt func_opts[] = {
198 #ifdef CONFIG_STACKTRACE
199         { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
200 #endif
201         { } /* Always set a last empty entry */
202 };
203
204 static struct tracer_flags func_flags = {
205         .val = 0, /* By default: all flags disabled */
206         .opts = func_opts
207 };
208
209 static void tracing_start_function_trace(struct trace_array *tr)
210 {
211         tr->function_enabled = 0;
212         register_ftrace_function(tr->ops);
213         tr->function_enabled = 1;
214 }
215
216 static void tracing_stop_function_trace(struct trace_array *tr)
217 {
218         tr->function_enabled = 0;
219         unregister_ftrace_function(tr->ops);
220 }
221
222 static struct tracer function_trace;
223
224 static int
225 func_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
226 {
227         switch (bit) {
228         case TRACE_FUNC_OPT_STACK:
229                 /* do nothing if already set */
230                 if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
231                         break;
232
233                 /* We can change this flag when not running. */
234                 if (tr->current_trace != &function_trace)
235                         break;
236
237                 unregister_ftrace_function(tr->ops);
238
239                 if (set) {
240                         tr->ops->func = function_stack_trace_call;
241                         register_ftrace_function(tr->ops);
242                 } else {
243                         tr->ops->func = function_trace_call;
244                         register_ftrace_function(tr->ops);
245                 }
246
247                 break;
248         default:
249                 return -EINVAL;
250         }
251
252         return 0;
253 }
254
255 static struct tracer function_trace __tracer_data =
256 {
257         .name           = "function",
258         .init           = function_trace_init,
259         .reset          = function_trace_reset,
260         .start          = function_trace_start,
261         .flags          = &func_flags,
262         .set_flag       = func_set_flag,
263         .allow_instances = true,
264 #ifdef CONFIG_FTRACE_SELFTEST
265         .selftest       = trace_selftest_startup_function,
266 #endif
267 };
268
269 #ifdef CONFIG_DYNAMIC_FTRACE
270 static void update_traceon_count(struct ftrace_probe_ops *ops,
271                                  unsigned long ip, bool on)
272 {
273         struct ftrace_func_mapper *mapper = ops->private_data;
274         long *count;
275         long old_count;
276
277         /*
278          * Tracing gets disabled (or enabled) once per count.
279          * This function can be called at the same time on multiple CPUs.
280          * It is fine if both disable (or enable) tracing, as disabling
281          * (or enabling) the second time doesn't do anything as the
282          * state of the tracer is already disabled (or enabled).
283          * What needs to be synchronized in this case is that the count
284          * only gets decremented once, even if the tracer is disabled
285          * (or enabled) twice, as the second one is really a nop.
286          *
287          * The memory barriers guarantee that we only decrement the
288          * counter once. First the count is read to a local variable
289          * and a read barrier is used to make sure that it is loaded
290          * before checking if the tracer is in the state we want.
291          * If the tracer is not in the state we want, then the count
292          * is guaranteed to be the old count.
293          *
294          * Next the tracer is set to the state we want (disabled or enabled)
295          * then a write memory barrier is used to make sure that
296          * the new state is visible before changing the counter by
297          * one minus the old counter. This guarantees that another CPU
298          * executing this code will see the new state before seeing
299          * the new counter value, and would not do anything if the new
300          * counter is seen.
301          *
302          * Note, there is no synchronization between this and a user
303          * setting the tracing_on file. But we currently don't care
304          * about that.
305          */
306         count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
307         old_count = *count;
308
309         if (old_count <= 0)
310                 return;
311
312         /* Make sure we see count before checking tracing state */
313         smp_rmb();
314
315         if (on == !!tracing_is_on())
316                 return;
317
318         if (on)
319                 tracing_on();
320         else
321                 tracing_off();
322
323         /* Make sure tracing state is visible before updating count */
324         smp_wmb();
325
326         *count = old_count - 1;
327 }
328
329 static void
330 ftrace_traceon_count(unsigned long ip, unsigned long parent_ip,
331                      struct ftrace_probe_ops *ops, void **data)
332 {
333         update_traceon_count(ops, ip, 1);
334 }
335
336 static void
337 ftrace_traceoff_count(unsigned long ip, unsigned long parent_ip,
338                       struct ftrace_probe_ops *ops, void **data)
339 {
340         update_traceon_count(ops, ip, 0);
341 }
342
343 static void
344 ftrace_traceon(unsigned long ip, unsigned long parent_ip,
345                struct ftrace_probe_ops *ops, void **data)
346 {
347         if (tracing_is_on())
348                 return;
349
350         tracing_on();
351 }
352
353 static void
354 ftrace_traceoff(unsigned long ip, unsigned long parent_ip,
355                 struct ftrace_probe_ops *ops, void **data)
356 {
357         if (!tracing_is_on())
358                 return;
359
360         tracing_off();
361 }
362
363 /*
364  * Skip 4:
365  *   ftrace_stacktrace()
366  *   function_trace_probe_call()
367  *   ftrace_ops_list_func()
368  *   ftrace_call()
369  */
370 #define STACK_SKIP 4
371
372 static void
373 ftrace_stacktrace(unsigned long ip, unsigned long parent_ip,
374                   struct ftrace_probe_ops *ops, void **data)
375 {
376         trace_dump_stack(STACK_SKIP);
377 }
378
379 static void
380 ftrace_stacktrace_count(unsigned long ip, unsigned long parent_ip,
381                         struct ftrace_probe_ops *ops, void **data)
382 {
383         struct ftrace_func_mapper *mapper = ops->private_data;
384         long *count;
385         long old_count;
386         long new_count;
387
388         if (!tracing_is_on())
389                 return;
390
391         /* unlimited? */
392         if (!mapper) {
393                 trace_dump_stack(STACK_SKIP);
394                 return;
395         }
396
397         count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
398
399         /*
400          * Stack traces should only execute the number of times the
401          * user specified in the counter.
402          */
403         do {
404                 old_count = *count;
405
406                 if (!old_count)
407                         return;
408
409                 new_count = old_count - 1;
410                 new_count = cmpxchg(count, old_count, new_count);
411                 if (new_count == old_count)
412                         trace_dump_stack(STACK_SKIP);
413
414                 if (!tracing_is_on())
415                         return;
416
417         } while (new_count != old_count);
418 }
419
420 static int update_count(struct ftrace_probe_ops *ops, unsigned long ip)
421 {
422         struct ftrace_func_mapper *mapper = ops->private_data;
423         long *count = NULL;
424
425         if (mapper)
426                 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
427
428         if (count) {
429                 if (*count <= 0)
430                         return 0;
431                 (*count)--;
432         }
433
434         return 1;
435 }
436
437 static void
438 ftrace_dump_probe(unsigned long ip, unsigned long parent_ip,
439         struct ftrace_probe_ops *ops, void **data)
440 {
441         if (update_count(ops, ip))
442                 ftrace_dump(DUMP_ALL);
443 }
444
445 /* Only dump the current CPU buffer. */
446 static void
447 ftrace_cpudump_probe(unsigned long ip, unsigned long parent_ip,
448         struct ftrace_probe_ops *ops, void **data)
449 {
450         if (update_count(ops, ip))
451                 ftrace_dump(DUMP_ORIG);
452 }
453
454 static int
455 ftrace_probe_print(const char *name, struct seq_file *m,
456                    unsigned long ip, struct ftrace_probe_ops *ops)
457 {
458         struct ftrace_func_mapper *mapper = ops->private_data;
459         long *count = NULL;
460
461         seq_printf(m, "%ps:%s", (void *)ip, name);
462
463         if (mapper)
464                 count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
465
466         if (count)
467                 seq_printf(m, ":count=%ld\n", *count);
468         else
469                 seq_puts(m, ":unlimited\n");
470
471         return 0;
472 }
473
474 static int
475 ftrace_traceon_print(struct seq_file *m, unsigned long ip,
476                          struct ftrace_probe_ops *ops, void *data)
477 {
478         return ftrace_probe_print("traceon", m, ip, ops);
479 }
480
481 static int
482 ftrace_traceoff_print(struct seq_file *m, unsigned long ip,
483                          struct ftrace_probe_ops *ops, void *data)
484 {
485         return ftrace_probe_print("traceoff", m, ip, ops);
486 }
487
488 static int
489 ftrace_stacktrace_print(struct seq_file *m, unsigned long ip,
490                         struct ftrace_probe_ops *ops, void *data)
491 {
492         return ftrace_probe_print("stacktrace", m, ip, ops);
493 }
494
495 static int
496 ftrace_dump_print(struct seq_file *m, unsigned long ip,
497                         struct ftrace_probe_ops *ops, void *data)
498 {
499         return ftrace_probe_print("dump", m, ip, ops);
500 }
501
502 static int
503 ftrace_cpudump_print(struct seq_file *m, unsigned long ip,
504                         struct ftrace_probe_ops *ops, void *data)
505 {
506         return ftrace_probe_print("cpudump", m, ip, ops);
507 }
508
509
510 static int
511 ftrace_count_init(struct ftrace_probe_ops *ops, unsigned long ip,
512                      void *data)
513 {
514         struct ftrace_func_mapper *mapper = ops->private_data;
515
516         return ftrace_func_mapper_add_ip(mapper, ip, data);
517 }
518
519 static void
520 ftrace_count_free(struct ftrace_probe_ops *ops, unsigned long ip,
521                   void **_data)
522 {
523         struct ftrace_func_mapper *mapper = ops->private_data;
524
525         ftrace_func_mapper_remove_ip(mapper, ip);
526 }
527
528 static struct ftrace_probe_ops traceon_count_probe_ops = {
529         .func                   = ftrace_traceon_count,
530         .print                  = ftrace_traceon_print,
531         .init                   = ftrace_count_init,
532         .free                   = ftrace_count_free,
533 };
534
535 static struct ftrace_probe_ops traceoff_count_probe_ops = {
536         .func                   = ftrace_traceoff_count,
537         .print                  = ftrace_traceoff_print,
538         .init                   = ftrace_count_init,
539         .free                   = ftrace_count_free,
540 };
541
542 static struct ftrace_probe_ops stacktrace_count_probe_ops = {
543         .func                   = ftrace_stacktrace_count,
544         .print                  = ftrace_stacktrace_print,
545         .init                   = ftrace_count_init,
546         .free                   = ftrace_count_free,
547 };
548
549 static struct ftrace_probe_ops dump_probe_ops = {
550         .func                   = ftrace_dump_probe,
551         .print                  = ftrace_dump_print,
552         .init                   = ftrace_count_init,
553         .free                   = ftrace_count_free,
554 };
555
556 static struct ftrace_probe_ops cpudump_probe_ops = {
557         .func                   = ftrace_cpudump_probe,
558         .print                  = ftrace_cpudump_print,
559 };
560
561 static struct ftrace_probe_ops traceon_probe_ops = {
562         .func                   = ftrace_traceon,
563         .print                  = ftrace_traceon_print,
564 };
565
566 static struct ftrace_probe_ops traceoff_probe_ops = {
567         .func                   = ftrace_traceoff,
568         .print                  = ftrace_traceoff_print,
569 };
570
571 static struct ftrace_probe_ops stacktrace_probe_ops = {
572         .func                   = ftrace_stacktrace,
573         .print                  = ftrace_stacktrace_print,
574 };
575
576 static int
577 ftrace_trace_probe_callback(struct trace_array *tr,
578                             struct ftrace_probe_ops *ops,
579                             struct ftrace_hash *hash, char *glob,
580                             char *cmd, char *param, int enable)
581 {
582         void *count = (void *)-1;
583         char *number;
584         int ret;
585
586         /* hash funcs only work with set_ftrace_filter */
587         if (!enable)
588                 return -EINVAL;
589
590         if (glob[0] == '!')
591                 return unregister_ftrace_function_probe_func(glob+1, ops);
592
593         if (!param)
594                 goto out_reg;
595
596         number = strsep(&param, ":");
597
598         if (!strlen(number))
599                 goto out_reg;
600
601         if (!ops->private_data) {
602                 ops->private_data = allocate_ftrace_func_mapper();
603                 if (!ops->private_data)
604                         return -ENOMEM;
605         }
606
607         /*
608          * We use the callback data field (which is a pointer)
609          * as our counter.
610          */
611         ret = kstrtoul(number, 0, (unsigned long *)&count);
612         if (ret)
613                 return ret;
614
615  out_reg:
616         ret = register_ftrace_function_probe(glob, tr, ops, count);
617
618         return ret < 0 ? ret : 0;
619 }
620
621 static int
622 ftrace_trace_onoff_callback(struct trace_array *tr, struct ftrace_hash *hash,
623                             char *glob, char *cmd, char *param, int enable)
624 {
625         struct ftrace_probe_ops *ops;
626
627         /* we register both traceon and traceoff to this callback */
628         if (strcmp(cmd, "traceon") == 0)
629                 ops = param ? &traceon_count_probe_ops : &traceon_probe_ops;
630         else
631                 ops = param ? &traceoff_count_probe_ops : &traceoff_probe_ops;
632
633         return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
634                                            param, enable);
635 }
636
637 static int
638 ftrace_stacktrace_callback(struct trace_array *tr, struct ftrace_hash *hash,
639                            char *glob, char *cmd, char *param, int enable)
640 {
641         struct ftrace_probe_ops *ops;
642
643         ops = param ? &stacktrace_count_probe_ops : &stacktrace_probe_ops;
644
645         return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
646                                            param, enable);
647 }
648
649 static int
650 ftrace_dump_callback(struct trace_array *tr, struct ftrace_hash *hash,
651                            char *glob, char *cmd, char *param, int enable)
652 {
653         struct ftrace_probe_ops *ops;
654
655         ops = &dump_probe_ops;
656
657         /* Only dump once. */
658         return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
659                                            "1", enable);
660 }
661
662 static int
663 ftrace_cpudump_callback(struct trace_array *tr, struct ftrace_hash *hash,
664                            char *glob, char *cmd, char *param, int enable)
665 {
666         struct ftrace_probe_ops *ops;
667
668         ops = &cpudump_probe_ops;
669
670         /* Only dump once. */
671         return ftrace_trace_probe_callback(tr, ops, hash, glob, cmd,
672                                            "1", enable);
673 }
674
675 static struct ftrace_func_command ftrace_traceon_cmd = {
676         .name                   = "traceon",
677         .func                   = ftrace_trace_onoff_callback,
678 };
679
680 static struct ftrace_func_command ftrace_traceoff_cmd = {
681         .name                   = "traceoff",
682         .func                   = ftrace_trace_onoff_callback,
683 };
684
685 static struct ftrace_func_command ftrace_stacktrace_cmd = {
686         .name                   = "stacktrace",
687         .func                   = ftrace_stacktrace_callback,
688 };
689
690 static struct ftrace_func_command ftrace_dump_cmd = {
691         .name                   = "dump",
692         .func                   = ftrace_dump_callback,
693 };
694
695 static struct ftrace_func_command ftrace_cpudump_cmd = {
696         .name                   = "cpudump",
697         .func                   = ftrace_cpudump_callback,
698 };
699
700 static int __init init_func_cmd_traceon(void)
701 {
702         int ret;
703
704         ret = register_ftrace_command(&ftrace_traceoff_cmd);
705         if (ret)
706                 return ret;
707
708         ret = register_ftrace_command(&ftrace_traceon_cmd);
709         if (ret)
710                 goto out_free_traceoff;
711
712         ret = register_ftrace_command(&ftrace_stacktrace_cmd);
713         if (ret)
714                 goto out_free_traceon;
715
716         ret = register_ftrace_command(&ftrace_dump_cmd);
717         if (ret)
718                 goto out_free_stacktrace;
719
720         ret = register_ftrace_command(&ftrace_cpudump_cmd);
721         if (ret)
722                 goto out_free_dump;
723
724         return 0;
725
726  out_free_dump:
727         unregister_ftrace_command(&ftrace_dump_cmd);
728  out_free_stacktrace:
729         unregister_ftrace_command(&ftrace_stacktrace_cmd);
730  out_free_traceon:
731         unregister_ftrace_command(&ftrace_traceon_cmd);
732  out_free_traceoff:
733         unregister_ftrace_command(&ftrace_traceoff_cmd);
734
735         return ret;
736 }
737 #else
738 static inline int init_func_cmd_traceon(void)
739 {
740         return 0;
741 }
742 #endif /* CONFIG_DYNAMIC_FTRACE */
743
744 __init int init_function_trace(void)
745 {
746         init_func_cmd_traceon();
747         return register_tracer(&function_trace);
748 }