Merge tag 'trace-v4.13-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[muen/linux.git] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 Nadia Yvette Chambers
13  */
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/tracefs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <linux/ftrace.h>
30 #include <linux/module.h>
31 #include <linux/percpu.h>
32 #include <linux/splice.h>
33 #include <linux/kdebug.h>
34 #include <linux/string.h>
35 #include <linux/mount.h>
36 #include <linux/rwsem.h>
37 #include <linux/slab.h>
38 #include <linux/ctype.h>
39 #include <linux/init.h>
40 #include <linux/poll.h>
41 #include <linux/nmi.h>
42 #include <linux/fs.h>
43 #include <linux/trace.h>
44 #include <linux/sched/rt.h>
45
46 #include "trace.h"
47 #include "trace_output.h"
48
49 /*
50  * On boot up, the ring buffer is set to the minimum size, so that
51  * we do not waste memory on systems that are not using tracing.
52  */
53 bool ring_buffer_expanded;
54
55 /*
56  * We need to change this state when a selftest is running.
57  * A selftest will lurk into the ring-buffer to count the
58  * entries inserted during the selftest although some concurrent
59  * insertions into the ring-buffer such as trace_printk could occurred
60  * at the same time, giving false positive or negative results.
61  */
62 static bool __read_mostly tracing_selftest_running;
63
64 /*
65  * If a tracer is running, we do not want to run SELFTEST.
66  */
67 bool __read_mostly tracing_selftest_disabled;
68
69 /* Pipe tracepoints to printk */
70 struct trace_iterator *tracepoint_print_iter;
71 int tracepoint_printk;
72 static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
73
74 /* For tracers that don't implement custom flags */
75 static struct tracer_opt dummy_tracer_opt[] = {
76         { }
77 };
78
79 static int
80 dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
81 {
82         return 0;
83 }
84
85 /*
86  * To prevent the comm cache from being overwritten when no
87  * tracing is active, only save the comm when a trace event
88  * occurred.
89  */
90 static DEFINE_PER_CPU(bool, trace_taskinfo_save);
91
92 /*
93  * Kill all tracing for good (never come back).
94  * It is initialized to 1 but will turn to zero if the initialization
95  * of the tracer is successful. But that is the only place that sets
96  * this back to zero.
97  */
98 static int tracing_disabled = 1;
99
100 cpumask_var_t __read_mostly     tracing_buffer_mask;
101
102 /*
103  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
104  *
105  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
106  * is set, then ftrace_dump is called. This will output the contents
107  * of the ftrace buffers to the console.  This is very useful for
108  * capturing traces that lead to crashes and outputing it to a
109  * serial console.
110  *
111  * It is default off, but you can enable it with either specifying
112  * "ftrace_dump_on_oops" in the kernel command line, or setting
113  * /proc/sys/kernel/ftrace_dump_on_oops
114  * Set 1 if you want to dump buffers of all CPUs
115  * Set 2 if you want to dump the buffer of the CPU that triggered oops
116  */
117
118 enum ftrace_dump_mode ftrace_dump_on_oops;
119
120 /* When set, tracing will stop when a WARN*() is hit */
121 int __disable_trace_on_warning;
122
123 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
124 /* Map of enums to their values, for "eval_map" file */
125 struct trace_eval_map_head {
126         struct module                   *mod;
127         unsigned long                   length;
128 };
129
130 union trace_eval_map_item;
131
132 struct trace_eval_map_tail {
133         /*
134          * "end" is first and points to NULL as it must be different
135          * than "mod" or "eval_string"
136          */
137         union trace_eval_map_item       *next;
138         const char                      *end;   /* points to NULL */
139 };
140
141 static DEFINE_MUTEX(trace_eval_mutex);
142
143 /*
144  * The trace_eval_maps are saved in an array with two extra elements,
145  * one at the beginning, and one at the end. The beginning item contains
146  * the count of the saved maps (head.length), and the module they
147  * belong to if not built in (head.mod). The ending item contains a
148  * pointer to the next array of saved eval_map items.
149  */
150 union trace_eval_map_item {
151         struct trace_eval_map           map;
152         struct trace_eval_map_head      head;
153         struct trace_eval_map_tail      tail;
154 };
155
156 static union trace_eval_map_item *trace_eval_maps;
157 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
158
159 static int tracing_set_tracer(struct trace_array *tr, const char *buf);
160
161 #define MAX_TRACER_SIZE         100
162 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
163 static char *default_bootup_tracer;
164
165 static bool allocate_snapshot;
166
167 static int __init set_cmdline_ftrace(char *str)
168 {
169         strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
170         default_bootup_tracer = bootup_tracer_buf;
171         /* We are using ftrace early, expand it */
172         ring_buffer_expanded = true;
173         return 1;
174 }
175 __setup("ftrace=", set_cmdline_ftrace);
176
177 static int __init set_ftrace_dump_on_oops(char *str)
178 {
179         if (*str++ != '=' || !*str) {
180                 ftrace_dump_on_oops = DUMP_ALL;
181                 return 1;
182         }
183
184         if (!strcmp("orig_cpu", str)) {
185                 ftrace_dump_on_oops = DUMP_ORIG;
186                 return 1;
187         }
188
189         return 0;
190 }
191 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
192
193 static int __init stop_trace_on_warning(char *str)
194 {
195         if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
196                 __disable_trace_on_warning = 1;
197         return 1;
198 }
199 __setup("traceoff_on_warning", stop_trace_on_warning);
200
201 static int __init boot_alloc_snapshot(char *str)
202 {
203         allocate_snapshot = true;
204         /* We also need the main ring buffer expanded */
205         ring_buffer_expanded = true;
206         return 1;
207 }
208 __setup("alloc_snapshot", boot_alloc_snapshot);
209
210
211 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
212
213 static int __init set_trace_boot_options(char *str)
214 {
215         strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
216         return 0;
217 }
218 __setup("trace_options=", set_trace_boot_options);
219
220 static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
221 static char *trace_boot_clock __initdata;
222
223 static int __init set_trace_boot_clock(char *str)
224 {
225         strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
226         trace_boot_clock = trace_boot_clock_buf;
227         return 0;
228 }
229 __setup("trace_clock=", set_trace_boot_clock);
230
231 static int __init set_tracepoint_printk(char *str)
232 {
233         if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
234                 tracepoint_printk = 1;
235         return 1;
236 }
237 __setup("tp_printk", set_tracepoint_printk);
238
239 unsigned long long ns2usecs(u64 nsec)
240 {
241         nsec += 500;
242         do_div(nsec, 1000);
243         return nsec;
244 }
245
246 /* trace_flags holds trace_options default values */
247 #define TRACE_DEFAULT_FLAGS                                             \
248         (FUNCTION_DEFAULT_FLAGS |                                       \
249          TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |                  \
250          TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO |                \
251          TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |                 \
252          TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
253
254 /* trace_options that are only supported by global_trace */
255 #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK |                      \
256                TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
257
258 /* trace_flags that are default zero for instances */
259 #define ZEROED_TRACE_FLAGS \
260         (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
261
262 /*
263  * The global_trace is the descriptor that holds the top-level tracing
264  * buffers for the live tracing.
265  */
266 static struct trace_array global_trace = {
267         .trace_flags = TRACE_DEFAULT_FLAGS,
268 };
269
270 LIST_HEAD(ftrace_trace_arrays);
271
272 int trace_array_get(struct trace_array *this_tr)
273 {
274         struct trace_array *tr;
275         int ret = -ENODEV;
276
277         mutex_lock(&trace_types_lock);
278         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
279                 if (tr == this_tr) {
280                         tr->ref++;
281                         ret = 0;
282                         break;
283                 }
284         }
285         mutex_unlock(&trace_types_lock);
286
287         return ret;
288 }
289
290 static void __trace_array_put(struct trace_array *this_tr)
291 {
292         WARN_ON(!this_tr->ref);
293         this_tr->ref--;
294 }
295
296 void trace_array_put(struct trace_array *this_tr)
297 {
298         mutex_lock(&trace_types_lock);
299         __trace_array_put(this_tr);
300         mutex_unlock(&trace_types_lock);
301 }
302
303 int call_filter_check_discard(struct trace_event_call *call, void *rec,
304                               struct ring_buffer *buffer,
305                               struct ring_buffer_event *event)
306 {
307         if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
308             !filter_match_preds(call->filter, rec)) {
309                 __trace_event_discard_commit(buffer, event);
310                 return 1;
311         }
312
313         return 0;
314 }
315
316 void trace_free_pid_list(struct trace_pid_list *pid_list)
317 {
318         vfree(pid_list->pids);
319         kfree(pid_list);
320 }
321
322 /**
323  * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
324  * @filtered_pids: The list of pids to check
325  * @search_pid: The PID to find in @filtered_pids
326  *
327  * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
328  */
329 bool
330 trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
331 {
332         /*
333          * If pid_max changed after filtered_pids was created, we
334          * by default ignore all pids greater than the previous pid_max.
335          */
336         if (search_pid >= filtered_pids->pid_max)
337                 return false;
338
339         return test_bit(search_pid, filtered_pids->pids);
340 }
341
342 /**
343  * trace_ignore_this_task - should a task be ignored for tracing
344  * @filtered_pids: The list of pids to check
345  * @task: The task that should be ignored if not filtered
346  *
347  * Checks if @task should be traced or not from @filtered_pids.
348  * Returns true if @task should *NOT* be traced.
349  * Returns false if @task should be traced.
350  */
351 bool
352 trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
353 {
354         /*
355          * Return false, because if filtered_pids does not exist,
356          * all pids are good to trace.
357          */
358         if (!filtered_pids)
359                 return false;
360
361         return !trace_find_filtered_pid(filtered_pids, task->pid);
362 }
363
364 /**
365  * trace_pid_filter_add_remove - Add or remove a task from a pid_list
366  * @pid_list: The list to modify
367  * @self: The current task for fork or NULL for exit
368  * @task: The task to add or remove
369  *
370  * If adding a task, if @self is defined, the task is only added if @self
371  * is also included in @pid_list. This happens on fork and tasks should
372  * only be added when the parent is listed. If @self is NULL, then the
373  * @task pid will be removed from the list, which would happen on exit
374  * of a task.
375  */
376 void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
377                                   struct task_struct *self,
378                                   struct task_struct *task)
379 {
380         if (!pid_list)
381                 return;
382
383         /* For forks, we only add if the forking task is listed */
384         if (self) {
385                 if (!trace_find_filtered_pid(pid_list, self->pid))
386                         return;
387         }
388
389         /* Sorry, but we don't support pid_max changing after setting */
390         if (task->pid >= pid_list->pid_max)
391                 return;
392
393         /* "self" is set for forks, and NULL for exits */
394         if (self)
395                 set_bit(task->pid, pid_list->pids);
396         else
397                 clear_bit(task->pid, pid_list->pids);
398 }
399
400 /**
401  * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
402  * @pid_list: The pid list to show
403  * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
404  * @pos: The position of the file
405  *
406  * This is used by the seq_file "next" operation to iterate the pids
407  * listed in a trace_pid_list structure.
408  *
409  * Returns the pid+1 as we want to display pid of zero, but NULL would
410  * stop the iteration.
411  */
412 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
413 {
414         unsigned long pid = (unsigned long)v;
415
416         (*pos)++;
417
418         /* pid already is +1 of the actual prevous bit */
419         pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
420
421         /* Return pid + 1 to allow zero to be represented */
422         if (pid < pid_list->pid_max)
423                 return (void *)(pid + 1);
424
425         return NULL;
426 }
427
428 /**
429  * trace_pid_start - Used for seq_file to start reading pid lists
430  * @pid_list: The pid list to show
431  * @pos: The position of the file
432  *
433  * This is used by seq_file "start" operation to start the iteration
434  * of listing pids.
435  *
436  * Returns the pid+1 as we want to display pid of zero, but NULL would
437  * stop the iteration.
438  */
439 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
440 {
441         unsigned long pid;
442         loff_t l = 0;
443
444         pid = find_first_bit(pid_list->pids, pid_list->pid_max);
445         if (pid >= pid_list->pid_max)
446                 return NULL;
447
448         /* Return pid + 1 so that zero can be the exit value */
449         for (pid++; pid && l < *pos;
450              pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
451                 ;
452         return (void *)pid;
453 }
454
455 /**
456  * trace_pid_show - show the current pid in seq_file processing
457  * @m: The seq_file structure to write into
458  * @v: A void pointer of the pid (+1) value to display
459  *
460  * Can be directly used by seq_file operations to display the current
461  * pid value.
462  */
463 int trace_pid_show(struct seq_file *m, void *v)
464 {
465         unsigned long pid = (unsigned long)v - 1;
466
467         seq_printf(m, "%lu\n", pid);
468         return 0;
469 }
470
471 /* 128 should be much more than enough */
472 #define PID_BUF_SIZE            127
473
474 int trace_pid_write(struct trace_pid_list *filtered_pids,
475                     struct trace_pid_list **new_pid_list,
476                     const char __user *ubuf, size_t cnt)
477 {
478         struct trace_pid_list *pid_list;
479         struct trace_parser parser;
480         unsigned long val;
481         int nr_pids = 0;
482         ssize_t read = 0;
483         ssize_t ret = 0;
484         loff_t pos;
485         pid_t pid;
486
487         if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
488                 return -ENOMEM;
489
490         /*
491          * Always recreate a new array. The write is an all or nothing
492          * operation. Always create a new array when adding new pids by
493          * the user. If the operation fails, then the current list is
494          * not modified.
495          */
496         pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
497         if (!pid_list)
498                 return -ENOMEM;
499
500         pid_list->pid_max = READ_ONCE(pid_max);
501
502         /* Only truncating will shrink pid_max */
503         if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
504                 pid_list->pid_max = filtered_pids->pid_max;
505
506         pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
507         if (!pid_list->pids) {
508                 kfree(pid_list);
509                 return -ENOMEM;
510         }
511
512         if (filtered_pids) {
513                 /* copy the current bits to the new max */
514                 for_each_set_bit(pid, filtered_pids->pids,
515                                  filtered_pids->pid_max) {
516                         set_bit(pid, pid_list->pids);
517                         nr_pids++;
518                 }
519         }
520
521         while (cnt > 0) {
522
523                 pos = 0;
524
525                 ret = trace_get_user(&parser, ubuf, cnt, &pos);
526                 if (ret < 0 || !trace_parser_loaded(&parser))
527                         break;
528
529                 read += ret;
530                 ubuf += ret;
531                 cnt -= ret;
532
533                 parser.buffer[parser.idx] = 0;
534
535                 ret = -EINVAL;
536                 if (kstrtoul(parser.buffer, 0, &val))
537                         break;
538                 if (val >= pid_list->pid_max)
539                         break;
540
541                 pid = (pid_t)val;
542
543                 set_bit(pid, pid_list->pids);
544                 nr_pids++;
545
546                 trace_parser_clear(&parser);
547                 ret = 0;
548         }
549         trace_parser_put(&parser);
550
551         if (ret < 0) {
552                 trace_free_pid_list(pid_list);
553                 return ret;
554         }
555
556         if (!nr_pids) {
557                 /* Cleared the list of pids */
558                 trace_free_pid_list(pid_list);
559                 read = ret;
560                 pid_list = NULL;
561         }
562
563         *new_pid_list = pid_list;
564
565         return read;
566 }
567
568 static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
569 {
570         u64 ts;
571
572         /* Early boot up does not have a buffer yet */
573         if (!buf->buffer)
574                 return trace_clock_local();
575
576         ts = ring_buffer_time_stamp(buf->buffer, cpu);
577         ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
578
579         return ts;
580 }
581
582 u64 ftrace_now(int cpu)
583 {
584         return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
585 }
586
587 /**
588  * tracing_is_enabled - Show if global_trace has been disabled
589  *
590  * Shows if the global trace has been enabled or not. It uses the
591  * mirror flag "buffer_disabled" to be used in fast paths such as for
592  * the irqsoff tracer. But it may be inaccurate due to races. If you
593  * need to know the accurate state, use tracing_is_on() which is a little
594  * slower, but accurate.
595  */
596 int tracing_is_enabled(void)
597 {
598         /*
599          * For quick access (irqsoff uses this in fast path), just
600          * return the mirror variable of the state of the ring buffer.
601          * It's a little racy, but we don't really care.
602          */
603         smp_rmb();
604         return !global_trace.buffer_disabled;
605 }
606
607 /*
608  * trace_buf_size is the size in bytes that is allocated
609  * for a buffer. Note, the number of bytes is always rounded
610  * to page size.
611  *
612  * This number is purposely set to a low number of 16384.
613  * If the dump on oops happens, it will be much appreciated
614  * to not have to wait for all that output. Anyway this can be
615  * boot time and run time configurable.
616  */
617 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
618
619 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
620
621 /* trace_types holds a link list of available tracers. */
622 static struct tracer            *trace_types __read_mostly;
623
624 /*
625  * trace_types_lock is used to protect the trace_types list.
626  */
627 DEFINE_MUTEX(trace_types_lock);
628
629 /*
630  * serialize the access of the ring buffer
631  *
632  * ring buffer serializes readers, but it is low level protection.
633  * The validity of the events (which returns by ring_buffer_peek() ..etc)
634  * are not protected by ring buffer.
635  *
636  * The content of events may become garbage if we allow other process consumes
637  * these events concurrently:
638  *   A) the page of the consumed events may become a normal page
639  *      (not reader page) in ring buffer, and this page will be rewrited
640  *      by events producer.
641  *   B) The page of the consumed events may become a page for splice_read,
642  *      and this page will be returned to system.
643  *
644  * These primitives allow multi process access to different cpu ring buffer
645  * concurrently.
646  *
647  * These primitives don't distinguish read-only and read-consume access.
648  * Multi read-only access are also serialized.
649  */
650
651 #ifdef CONFIG_SMP
652 static DECLARE_RWSEM(all_cpu_access_lock);
653 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
654
655 static inline void trace_access_lock(int cpu)
656 {
657         if (cpu == RING_BUFFER_ALL_CPUS) {
658                 /* gain it for accessing the whole ring buffer. */
659                 down_write(&all_cpu_access_lock);
660         } else {
661                 /* gain it for accessing a cpu ring buffer. */
662
663                 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
664                 down_read(&all_cpu_access_lock);
665
666                 /* Secondly block other access to this @cpu ring buffer. */
667                 mutex_lock(&per_cpu(cpu_access_lock, cpu));
668         }
669 }
670
671 static inline void trace_access_unlock(int cpu)
672 {
673         if (cpu == RING_BUFFER_ALL_CPUS) {
674                 up_write(&all_cpu_access_lock);
675         } else {
676                 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
677                 up_read(&all_cpu_access_lock);
678         }
679 }
680
681 static inline void trace_access_lock_init(void)
682 {
683         int cpu;
684
685         for_each_possible_cpu(cpu)
686                 mutex_init(&per_cpu(cpu_access_lock, cpu));
687 }
688
689 #else
690
691 static DEFINE_MUTEX(access_lock);
692
693 static inline void trace_access_lock(int cpu)
694 {
695         (void)cpu;
696         mutex_lock(&access_lock);
697 }
698
699 static inline void trace_access_unlock(int cpu)
700 {
701         (void)cpu;
702         mutex_unlock(&access_lock);
703 }
704
705 static inline void trace_access_lock_init(void)
706 {
707 }
708
709 #endif
710
711 #ifdef CONFIG_STACKTRACE
712 static void __ftrace_trace_stack(struct ring_buffer *buffer,
713                                  unsigned long flags,
714                                  int skip, int pc, struct pt_regs *regs);
715 static inline void ftrace_trace_stack(struct trace_array *tr,
716                                       struct ring_buffer *buffer,
717                                       unsigned long flags,
718                                       int skip, int pc, struct pt_regs *regs);
719
720 #else
721 static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
722                                         unsigned long flags,
723                                         int skip, int pc, struct pt_regs *regs)
724 {
725 }
726 static inline void ftrace_trace_stack(struct trace_array *tr,
727                                       struct ring_buffer *buffer,
728                                       unsigned long flags,
729                                       int skip, int pc, struct pt_regs *regs)
730 {
731 }
732
733 #endif
734
735 static __always_inline void
736 trace_event_setup(struct ring_buffer_event *event,
737                   int type, unsigned long flags, int pc)
738 {
739         struct trace_entry *ent = ring_buffer_event_data(event);
740
741         tracing_generic_entry_update(ent, flags, pc);
742         ent->type = type;
743 }
744
745 static __always_inline struct ring_buffer_event *
746 __trace_buffer_lock_reserve(struct ring_buffer *buffer,
747                           int type,
748                           unsigned long len,
749                           unsigned long flags, int pc)
750 {
751         struct ring_buffer_event *event;
752
753         event = ring_buffer_lock_reserve(buffer, len);
754         if (event != NULL)
755                 trace_event_setup(event, type, flags, pc);
756
757         return event;
758 }
759
760 void tracer_tracing_on(struct trace_array *tr)
761 {
762         if (tr->trace_buffer.buffer)
763                 ring_buffer_record_on(tr->trace_buffer.buffer);
764         /*
765          * This flag is looked at when buffers haven't been allocated
766          * yet, or by some tracers (like irqsoff), that just want to
767          * know if the ring buffer has been disabled, but it can handle
768          * races of where it gets disabled but we still do a record.
769          * As the check is in the fast path of the tracers, it is more
770          * important to be fast than accurate.
771          */
772         tr->buffer_disabled = 0;
773         /* Make the flag seen by readers */
774         smp_wmb();
775 }
776
777 /**
778  * tracing_on - enable tracing buffers
779  *
780  * This function enables tracing buffers that may have been
781  * disabled with tracing_off.
782  */
783 void tracing_on(void)
784 {
785         tracer_tracing_on(&global_trace);
786 }
787 EXPORT_SYMBOL_GPL(tracing_on);
788
789
790 static __always_inline void
791 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
792 {
793         __this_cpu_write(trace_taskinfo_save, true);
794
795         /* If this is the temp buffer, we need to commit fully */
796         if (this_cpu_read(trace_buffered_event) == event) {
797                 /* Length is in event->array[0] */
798                 ring_buffer_write(buffer, event->array[0], &event->array[1]);
799                 /* Release the temp buffer */
800                 this_cpu_dec(trace_buffered_event_cnt);
801         } else
802                 ring_buffer_unlock_commit(buffer, event);
803 }
804
805 /**
806  * __trace_puts - write a constant string into the trace buffer.
807  * @ip:    The address of the caller
808  * @str:   The constant string to write
809  * @size:  The size of the string.
810  */
811 int __trace_puts(unsigned long ip, const char *str, int size)
812 {
813         struct ring_buffer_event *event;
814         struct ring_buffer *buffer;
815         struct print_entry *entry;
816         unsigned long irq_flags;
817         int alloc;
818         int pc;
819
820         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
821                 return 0;
822
823         pc = preempt_count();
824
825         if (unlikely(tracing_selftest_running || tracing_disabled))
826                 return 0;
827
828         alloc = sizeof(*entry) + size + 2; /* possible \n added */
829
830         local_save_flags(irq_flags);
831         buffer = global_trace.trace_buffer.buffer;
832         event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 
833                                             irq_flags, pc);
834         if (!event)
835                 return 0;
836
837         entry = ring_buffer_event_data(event);
838         entry->ip = ip;
839
840         memcpy(&entry->buf, str, size);
841
842         /* Add a newline if necessary */
843         if (entry->buf[size - 1] != '\n') {
844                 entry->buf[size] = '\n';
845                 entry->buf[size + 1] = '\0';
846         } else
847                 entry->buf[size] = '\0';
848
849         __buffer_unlock_commit(buffer, event);
850         ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
851
852         return size;
853 }
854 EXPORT_SYMBOL_GPL(__trace_puts);
855
856 /**
857  * __trace_bputs - write the pointer to a constant string into trace buffer
858  * @ip:    The address of the caller
859  * @str:   The constant string to write to the buffer to
860  */
861 int __trace_bputs(unsigned long ip, const char *str)
862 {
863         struct ring_buffer_event *event;
864         struct ring_buffer *buffer;
865         struct bputs_entry *entry;
866         unsigned long irq_flags;
867         int size = sizeof(struct bputs_entry);
868         int pc;
869
870         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
871                 return 0;
872
873         pc = preempt_count();
874
875         if (unlikely(tracing_selftest_running || tracing_disabled))
876                 return 0;
877
878         local_save_flags(irq_flags);
879         buffer = global_trace.trace_buffer.buffer;
880         event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
881                                             irq_flags, pc);
882         if (!event)
883                 return 0;
884
885         entry = ring_buffer_event_data(event);
886         entry->ip                       = ip;
887         entry->str                      = str;
888
889         __buffer_unlock_commit(buffer, event);
890         ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
891
892         return 1;
893 }
894 EXPORT_SYMBOL_GPL(__trace_bputs);
895
896 #ifdef CONFIG_TRACER_SNAPSHOT
897 static void tracing_snapshot_instance(struct trace_array *tr)
898 {
899         struct tracer *tracer = tr->current_trace;
900         unsigned long flags;
901
902         if (in_nmi()) {
903                 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
904                 internal_trace_puts("*** snapshot is being ignored        ***\n");
905                 return;
906         }
907
908         if (!tr->allocated_snapshot) {
909                 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
910                 internal_trace_puts("*** stopping trace here!   ***\n");
911                 tracing_off();
912                 return;
913         }
914
915         /* Note, snapshot can not be used when the tracer uses it */
916         if (tracer->use_max_tr) {
917                 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
918                 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
919                 return;
920         }
921
922         local_irq_save(flags);
923         update_max_tr(tr, current, smp_processor_id());
924         local_irq_restore(flags);
925 }
926
927 /**
928  * trace_snapshot - take a snapshot of the current buffer.
929  *
930  * This causes a swap between the snapshot buffer and the current live
931  * tracing buffer. You can use this to take snapshots of the live
932  * trace when some condition is triggered, but continue to trace.
933  *
934  * Note, make sure to allocate the snapshot with either
935  * a tracing_snapshot_alloc(), or by doing it manually
936  * with: echo 1 > /sys/kernel/debug/tracing/snapshot
937  *
938  * If the snapshot buffer is not allocated, it will stop tracing.
939  * Basically making a permanent snapshot.
940  */
941 void tracing_snapshot(void)
942 {
943         struct trace_array *tr = &global_trace;
944
945         tracing_snapshot_instance(tr);
946 }
947 EXPORT_SYMBOL_GPL(tracing_snapshot);
948
949 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
950                                         struct trace_buffer *size_buf, int cpu_id);
951 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
952
953 static int alloc_snapshot(struct trace_array *tr)
954 {
955         int ret;
956
957         if (!tr->allocated_snapshot) {
958
959                 /* allocate spare buffer */
960                 ret = resize_buffer_duplicate_size(&tr->max_buffer,
961                                    &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
962                 if (ret < 0)
963                         return ret;
964
965                 tr->allocated_snapshot = true;
966         }
967
968         return 0;
969 }
970
971 static void free_snapshot(struct trace_array *tr)
972 {
973         /*
974          * We don't free the ring buffer. instead, resize it because
975          * The max_tr ring buffer has some state (e.g. ring->clock) and
976          * we want preserve it.
977          */
978         ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
979         set_buffer_entries(&tr->max_buffer, 1);
980         tracing_reset_online_cpus(&tr->max_buffer);
981         tr->allocated_snapshot = false;
982 }
983
984 /**
985  * tracing_alloc_snapshot - allocate snapshot buffer.
986  *
987  * This only allocates the snapshot buffer if it isn't already
988  * allocated - it doesn't also take a snapshot.
989  *
990  * This is meant to be used in cases where the snapshot buffer needs
991  * to be set up for events that can't sleep but need to be able to
992  * trigger a snapshot.
993  */
994 int tracing_alloc_snapshot(void)
995 {
996         struct trace_array *tr = &global_trace;
997         int ret;
998
999         ret = alloc_snapshot(tr);
1000         WARN_ON(ret < 0);
1001
1002         return ret;
1003 }
1004 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1005
1006 /**
1007  * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
1008  *
1009  * This is similar to trace_snapshot(), but it will allocate the
1010  * snapshot buffer if it isn't already allocated. Use this only
1011  * where it is safe to sleep, as the allocation may sleep.
1012  *
1013  * This causes a swap between the snapshot buffer and the current live
1014  * tracing buffer. You can use this to take snapshots of the live
1015  * trace when some condition is triggered, but continue to trace.
1016  */
1017 void tracing_snapshot_alloc(void)
1018 {
1019         int ret;
1020
1021         ret = tracing_alloc_snapshot();
1022         if (ret < 0)
1023                 return;
1024
1025         tracing_snapshot();
1026 }
1027 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1028 #else
1029 void tracing_snapshot(void)
1030 {
1031         WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1032 }
1033 EXPORT_SYMBOL_GPL(tracing_snapshot);
1034 int tracing_alloc_snapshot(void)
1035 {
1036         WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1037         return -ENODEV;
1038 }
1039 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1040 void tracing_snapshot_alloc(void)
1041 {
1042         /* Give warning */
1043         tracing_snapshot();
1044 }
1045 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1046 #endif /* CONFIG_TRACER_SNAPSHOT */
1047
1048 void tracer_tracing_off(struct trace_array *tr)
1049 {
1050         if (tr->trace_buffer.buffer)
1051                 ring_buffer_record_off(tr->trace_buffer.buffer);
1052         /*
1053          * This flag is looked at when buffers haven't been allocated
1054          * yet, or by some tracers (like irqsoff), that just want to
1055          * know if the ring buffer has been disabled, but it can handle
1056          * races of where it gets disabled but we still do a record.
1057          * As the check is in the fast path of the tracers, it is more
1058          * important to be fast than accurate.
1059          */
1060         tr->buffer_disabled = 1;
1061         /* Make the flag seen by readers */
1062         smp_wmb();
1063 }
1064
1065 /**
1066  * tracing_off - turn off tracing buffers
1067  *
1068  * This function stops the tracing buffers from recording data.
1069  * It does not disable any overhead the tracers themselves may
1070  * be causing. This function simply causes all recording to
1071  * the ring buffers to fail.
1072  */
1073 void tracing_off(void)
1074 {
1075         tracer_tracing_off(&global_trace);
1076 }
1077 EXPORT_SYMBOL_GPL(tracing_off);
1078
1079 void disable_trace_on_warning(void)
1080 {
1081         if (__disable_trace_on_warning)
1082                 tracing_off();
1083 }
1084
1085 /**
1086  * tracer_tracing_is_on - show real state of ring buffer enabled
1087  * @tr : the trace array to know if ring buffer is enabled
1088  *
1089  * Shows real state of the ring buffer if it is enabled or not.
1090  */
1091 int tracer_tracing_is_on(struct trace_array *tr)
1092 {
1093         if (tr->trace_buffer.buffer)
1094                 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1095         return !tr->buffer_disabled;
1096 }
1097
1098 /**
1099  * tracing_is_on - show state of ring buffers enabled
1100  */
1101 int tracing_is_on(void)
1102 {
1103         return tracer_tracing_is_on(&global_trace);
1104 }
1105 EXPORT_SYMBOL_GPL(tracing_is_on);
1106
1107 static int __init set_buf_size(char *str)
1108 {
1109         unsigned long buf_size;
1110
1111         if (!str)
1112                 return 0;
1113         buf_size = memparse(str, &str);
1114         /* nr_entries can not be zero */
1115         if (buf_size == 0)
1116                 return 0;
1117         trace_buf_size = buf_size;
1118         return 1;
1119 }
1120 __setup("trace_buf_size=", set_buf_size);
1121
1122 static int __init set_tracing_thresh(char *str)
1123 {
1124         unsigned long threshold;
1125         int ret;
1126
1127         if (!str)
1128                 return 0;
1129         ret = kstrtoul(str, 0, &threshold);
1130         if (ret < 0)
1131                 return 0;
1132         tracing_thresh = threshold * 1000;
1133         return 1;
1134 }
1135 __setup("tracing_thresh=", set_tracing_thresh);
1136
1137 unsigned long nsecs_to_usecs(unsigned long nsecs)
1138 {
1139         return nsecs / 1000;
1140 }
1141
1142 /*
1143  * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1144  * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
1145  * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1146  * of strings in the order that the evals (enum) were defined.
1147  */
1148 #undef C
1149 #define C(a, b) b
1150
1151 /* These must match the bit postions in trace_iterator_flags */
1152 static const char *trace_options[] = {
1153         TRACE_FLAGS
1154         NULL
1155 };
1156
1157 static struct {
1158         u64 (*func)(void);
1159         const char *name;
1160         int in_ns;              /* is this clock in nanoseconds? */
1161 } trace_clocks[] = {
1162         { trace_clock_local,            "local",        1 },
1163         { trace_clock_global,           "global",       1 },
1164         { trace_clock_counter,          "counter",      0 },
1165         { trace_clock_jiffies,          "uptime",       0 },
1166         { trace_clock,                  "perf",         1 },
1167         { ktime_get_mono_fast_ns,       "mono",         1 },
1168         { ktime_get_raw_fast_ns,        "mono_raw",     1 },
1169         { ktime_get_boot_fast_ns,       "boot",         1 },
1170         ARCH_TRACE_CLOCKS
1171 };
1172
1173 /*
1174  * trace_parser_get_init - gets the buffer for trace parser
1175  */
1176 int trace_parser_get_init(struct trace_parser *parser, int size)
1177 {
1178         memset(parser, 0, sizeof(*parser));
1179
1180         parser->buffer = kmalloc(size, GFP_KERNEL);
1181         if (!parser->buffer)
1182                 return 1;
1183
1184         parser->size = size;
1185         return 0;
1186 }
1187
1188 /*
1189  * trace_parser_put - frees the buffer for trace parser
1190  */
1191 void trace_parser_put(struct trace_parser *parser)
1192 {
1193         kfree(parser->buffer);
1194         parser->buffer = NULL;
1195 }
1196
1197 /*
1198  * trace_get_user - reads the user input string separated by  space
1199  * (matched by isspace(ch))
1200  *
1201  * For each string found the 'struct trace_parser' is updated,
1202  * and the function returns.
1203  *
1204  * Returns number of bytes read.
1205  *
1206  * See kernel/trace/trace.h for 'struct trace_parser' details.
1207  */
1208 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1209         size_t cnt, loff_t *ppos)
1210 {
1211         char ch;
1212         size_t read = 0;
1213         ssize_t ret;
1214
1215         if (!*ppos)
1216                 trace_parser_clear(parser);
1217
1218         ret = get_user(ch, ubuf++);
1219         if (ret)
1220                 goto out;
1221
1222         read++;
1223         cnt--;
1224
1225         /*
1226          * The parser is not finished with the last write,
1227          * continue reading the user input without skipping spaces.
1228          */
1229         if (!parser->cont) {
1230                 /* skip white space */
1231                 while (cnt && isspace(ch)) {
1232                         ret = get_user(ch, ubuf++);
1233                         if (ret)
1234                                 goto out;
1235                         read++;
1236                         cnt--;
1237                 }
1238
1239                 /* only spaces were written */
1240                 if (isspace(ch)) {
1241                         *ppos += read;
1242                         ret = read;
1243                         goto out;
1244                 }
1245
1246                 parser->idx = 0;
1247         }
1248
1249         /* read the non-space input */
1250         while (cnt && !isspace(ch)) {
1251                 if (parser->idx < parser->size - 1)
1252                         parser->buffer[parser->idx++] = ch;
1253                 else {
1254                         ret = -EINVAL;
1255                         goto out;
1256                 }
1257                 ret = get_user(ch, ubuf++);
1258                 if (ret)
1259                         goto out;
1260                 read++;
1261                 cnt--;
1262         }
1263
1264         /* We either got finished input or we have to wait for another call. */
1265         if (isspace(ch)) {
1266                 parser->buffer[parser->idx] = 0;
1267                 parser->cont = false;
1268         } else if (parser->idx < parser->size - 1) {
1269                 parser->cont = true;
1270                 parser->buffer[parser->idx++] = ch;
1271         } else {
1272                 ret = -EINVAL;
1273                 goto out;
1274         }
1275
1276         *ppos += read;
1277         ret = read;
1278
1279 out:
1280         return ret;
1281 }
1282
1283 /* TODO add a seq_buf_to_buffer() */
1284 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
1285 {
1286         int len;
1287
1288         if (trace_seq_used(s) <= s->seq.readpos)
1289                 return -EBUSY;
1290
1291         len = trace_seq_used(s) - s->seq.readpos;
1292         if (cnt > len)
1293                 cnt = len;
1294         memcpy(buf, s->buffer + s->seq.readpos, cnt);
1295
1296         s->seq.readpos += cnt;
1297         return cnt;
1298 }
1299
1300 unsigned long __read_mostly     tracing_thresh;
1301
1302 #ifdef CONFIG_TRACER_MAX_TRACE
1303 /*
1304  * Copy the new maximum trace into the separate maximum-trace
1305  * structure. (this way the maximum trace is permanently saved,
1306  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
1307  */
1308 static void
1309 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1310 {
1311         struct trace_buffer *trace_buf = &tr->trace_buffer;
1312         struct trace_buffer *max_buf = &tr->max_buffer;
1313         struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1314         struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
1315
1316         max_buf->cpu = cpu;
1317         max_buf->time_start = data->preempt_timestamp;
1318
1319         max_data->saved_latency = tr->max_latency;
1320         max_data->critical_start = data->critical_start;
1321         max_data->critical_end = data->critical_end;
1322
1323         memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
1324         max_data->pid = tsk->pid;
1325         /*
1326          * If tsk == current, then use current_uid(), as that does not use
1327          * RCU. The irq tracer can be called out of RCU scope.
1328          */
1329         if (tsk == current)
1330                 max_data->uid = current_uid();
1331         else
1332                 max_data->uid = task_uid(tsk);
1333
1334         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1335         max_data->policy = tsk->policy;
1336         max_data->rt_priority = tsk->rt_priority;
1337
1338         /* record this tasks comm */
1339         tracing_record_cmdline(tsk);
1340 }
1341
1342 /**
1343  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1344  * @tr: tracer
1345  * @tsk: the task with the latency
1346  * @cpu: The cpu that initiated the trace.
1347  *
1348  * Flip the buffers between the @tr and the max_tr and record information
1349  * about which task was the cause of this latency.
1350  */
1351 void
1352 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1353 {
1354         struct ring_buffer *buf;
1355
1356         if (tr->stop_count)
1357                 return;
1358
1359         WARN_ON_ONCE(!irqs_disabled());
1360
1361         if (!tr->allocated_snapshot) {
1362                 /* Only the nop tracer should hit this when disabling */
1363                 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1364                 return;
1365         }
1366
1367         arch_spin_lock(&tr->max_lock);
1368
1369         buf = tr->trace_buffer.buffer;
1370         tr->trace_buffer.buffer = tr->max_buffer.buffer;
1371         tr->max_buffer.buffer = buf;
1372
1373         __update_max_tr(tr, tsk, cpu);
1374         arch_spin_unlock(&tr->max_lock);
1375 }
1376
1377 /**
1378  * update_max_tr_single - only copy one trace over, and reset the rest
1379  * @tr - tracer
1380  * @tsk - task with the latency
1381  * @cpu - the cpu of the buffer to copy.
1382  *
1383  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1384  */
1385 void
1386 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1387 {
1388         int ret;
1389
1390         if (tr->stop_count)
1391                 return;
1392
1393         WARN_ON_ONCE(!irqs_disabled());
1394         if (!tr->allocated_snapshot) {
1395                 /* Only the nop tracer should hit this when disabling */
1396                 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1397                 return;
1398         }
1399
1400         arch_spin_lock(&tr->max_lock);
1401
1402         ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1403
1404         if (ret == -EBUSY) {
1405                 /*
1406                  * We failed to swap the buffer due to a commit taking
1407                  * place on this CPU. We fail to record, but we reset
1408                  * the max trace buffer (no one writes directly to it)
1409                  * and flag that it failed.
1410                  */
1411                 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1412                         "Failed to swap buffers due to commit in progress\n");
1413         }
1414
1415         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1416
1417         __update_max_tr(tr, tsk, cpu);
1418         arch_spin_unlock(&tr->max_lock);
1419 }
1420 #endif /* CONFIG_TRACER_MAX_TRACE */
1421
1422 static int wait_on_pipe(struct trace_iterator *iter, bool full)
1423 {
1424         /* Iterators are static, they should be filled or empty */
1425         if (trace_buffer_iter(iter, iter->cpu_file))
1426                 return 0;
1427
1428         return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1429                                 full);
1430 }
1431
1432 #ifdef CONFIG_FTRACE_STARTUP_TEST
1433 static bool selftests_can_run;
1434
1435 struct trace_selftests {
1436         struct list_head                list;
1437         struct tracer                   *type;
1438 };
1439
1440 static LIST_HEAD(postponed_selftests);
1441
1442 static int save_selftest(struct tracer *type)
1443 {
1444         struct trace_selftests *selftest;
1445
1446         selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1447         if (!selftest)
1448                 return -ENOMEM;
1449
1450         selftest->type = type;
1451         list_add(&selftest->list, &postponed_selftests);
1452         return 0;
1453 }
1454
1455 static int run_tracer_selftest(struct tracer *type)
1456 {
1457         struct trace_array *tr = &global_trace;
1458         struct tracer *saved_tracer = tr->current_trace;
1459         int ret;
1460
1461         if (!type->selftest || tracing_selftest_disabled)
1462                 return 0;
1463
1464         /*
1465          * If a tracer registers early in boot up (before scheduling is
1466          * initialized and such), then do not run its selftests yet.
1467          * Instead, run it a little later in the boot process.
1468          */
1469         if (!selftests_can_run)
1470                 return save_selftest(type);
1471
1472         /*
1473          * Run a selftest on this tracer.
1474          * Here we reset the trace buffer, and set the current
1475          * tracer to be this tracer. The tracer can then run some
1476          * internal tracing to verify that everything is in order.
1477          * If we fail, we do not register this tracer.
1478          */
1479         tracing_reset_online_cpus(&tr->trace_buffer);
1480
1481         tr->current_trace = type;
1482
1483 #ifdef CONFIG_TRACER_MAX_TRACE
1484         if (type->use_max_tr) {
1485                 /* If we expanded the buffers, make sure the max is expanded too */
1486                 if (ring_buffer_expanded)
1487                         ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1488                                            RING_BUFFER_ALL_CPUS);
1489                 tr->allocated_snapshot = true;
1490         }
1491 #endif
1492
1493         /* the test is responsible for initializing and enabling */
1494         pr_info("Testing tracer %s: ", type->name);
1495         ret = type->selftest(type, tr);
1496         /* the test is responsible for resetting too */
1497         tr->current_trace = saved_tracer;
1498         if (ret) {
1499                 printk(KERN_CONT "FAILED!\n");
1500                 /* Add the warning after printing 'FAILED' */
1501                 WARN_ON(1);
1502                 return -1;
1503         }
1504         /* Only reset on passing, to avoid touching corrupted buffers */
1505         tracing_reset_online_cpus(&tr->trace_buffer);
1506
1507 #ifdef CONFIG_TRACER_MAX_TRACE
1508         if (type->use_max_tr) {
1509                 tr->allocated_snapshot = false;
1510
1511                 /* Shrink the max buffer again */
1512                 if (ring_buffer_expanded)
1513                         ring_buffer_resize(tr->max_buffer.buffer, 1,
1514                                            RING_BUFFER_ALL_CPUS);
1515         }
1516 #endif
1517
1518         printk(KERN_CONT "PASSED\n");
1519         return 0;
1520 }
1521
1522 static __init int init_trace_selftests(void)
1523 {
1524         struct trace_selftests *p, *n;
1525         struct tracer *t, **last;
1526         int ret;
1527
1528         selftests_can_run = true;
1529
1530         mutex_lock(&trace_types_lock);
1531
1532         if (list_empty(&postponed_selftests))
1533                 goto out;
1534
1535         pr_info("Running postponed tracer tests:\n");
1536
1537         list_for_each_entry_safe(p, n, &postponed_selftests, list) {
1538                 ret = run_tracer_selftest(p->type);
1539                 /* If the test fails, then warn and remove from available_tracers */
1540                 if (ret < 0) {
1541                         WARN(1, "tracer: %s failed selftest, disabling\n",
1542                              p->type->name);
1543                         last = &trace_types;
1544                         for (t = trace_types; t; t = t->next) {
1545                                 if (t == p->type) {
1546                                         *last = t->next;
1547                                         break;
1548                                 }
1549                                 last = &t->next;
1550                         }
1551                 }
1552                 list_del(&p->list);
1553                 kfree(p);
1554         }
1555
1556  out:
1557         mutex_unlock(&trace_types_lock);
1558
1559         return 0;
1560 }
1561 core_initcall(init_trace_selftests);
1562 #else
1563 static inline int run_tracer_selftest(struct tracer *type)
1564 {
1565         return 0;
1566 }
1567 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1568
1569 static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1570
1571 static void __init apply_trace_boot_options(void);
1572
1573 /**
1574  * register_tracer - register a tracer with the ftrace system.
1575  * @type - the plugin for the tracer
1576  *
1577  * Register a new plugin tracer.
1578  */
1579 int __init register_tracer(struct tracer *type)
1580 {
1581         struct tracer *t;
1582         int ret = 0;
1583
1584         if (!type->name) {
1585                 pr_info("Tracer must have a name\n");
1586                 return -1;
1587         }
1588
1589         if (strlen(type->name) >= MAX_TRACER_SIZE) {
1590                 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1591                 return -1;
1592         }
1593
1594         mutex_lock(&trace_types_lock);
1595
1596         tracing_selftest_running = true;
1597
1598         for (t = trace_types; t; t = t->next) {
1599                 if (strcmp(type->name, t->name) == 0) {
1600                         /* already found */
1601                         pr_info("Tracer %s already registered\n",
1602                                 type->name);
1603                         ret = -1;
1604                         goto out;
1605                 }
1606         }
1607
1608         if (!type->set_flag)
1609                 type->set_flag = &dummy_set_flag;
1610         if (!type->flags) {
1611                 /*allocate a dummy tracer_flags*/
1612                 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
1613                 if (!type->flags) {
1614                         ret = -ENOMEM;
1615                         goto out;
1616                 }
1617                 type->flags->val = 0;
1618                 type->flags->opts = dummy_tracer_opt;
1619         } else
1620                 if (!type->flags->opts)
1621                         type->flags->opts = dummy_tracer_opt;
1622
1623         /* store the tracer for __set_tracer_option */
1624         type->flags->trace = type;
1625
1626         ret = run_tracer_selftest(type);
1627         if (ret < 0)
1628                 goto out;
1629
1630         type->next = trace_types;
1631         trace_types = type;
1632         add_tracer_options(&global_trace, type);
1633
1634  out:
1635         tracing_selftest_running = false;
1636         mutex_unlock(&trace_types_lock);
1637
1638         if (ret || !default_bootup_tracer)
1639                 goto out_unlock;
1640
1641         if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1642                 goto out_unlock;
1643
1644         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1645         /* Do we want this tracer to start on bootup? */
1646         tracing_set_tracer(&global_trace, type->name);
1647         default_bootup_tracer = NULL;
1648
1649         apply_trace_boot_options();
1650
1651         /* disable other selftests, since this will break it. */
1652         tracing_selftest_disabled = true;
1653 #ifdef CONFIG_FTRACE_STARTUP_TEST
1654         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1655                type->name);
1656 #endif
1657
1658  out_unlock:
1659         return ret;
1660 }
1661
1662 void tracing_reset(struct trace_buffer *buf, int cpu)
1663 {
1664         struct ring_buffer *buffer = buf->buffer;
1665
1666         if (!buffer)
1667                 return;
1668
1669         ring_buffer_record_disable(buffer);
1670
1671         /* Make sure all commits have finished */
1672         synchronize_sched();
1673         ring_buffer_reset_cpu(buffer, cpu);
1674
1675         ring_buffer_record_enable(buffer);
1676 }
1677
1678 void tracing_reset_online_cpus(struct trace_buffer *buf)
1679 {
1680         struct ring_buffer *buffer = buf->buffer;
1681         int cpu;
1682
1683         if (!buffer)
1684                 return;
1685
1686         ring_buffer_record_disable(buffer);
1687
1688         /* Make sure all commits have finished */
1689         synchronize_sched();
1690
1691         buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1692
1693         for_each_online_cpu(cpu)
1694                 ring_buffer_reset_cpu(buffer, cpu);
1695
1696         ring_buffer_record_enable(buffer);
1697 }
1698
1699 /* Must have trace_types_lock held */
1700 void tracing_reset_all_online_cpus(void)
1701 {
1702         struct trace_array *tr;
1703
1704         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1705                 tracing_reset_online_cpus(&tr->trace_buffer);
1706 #ifdef CONFIG_TRACER_MAX_TRACE
1707                 tracing_reset_online_cpus(&tr->max_buffer);
1708 #endif
1709         }
1710 }
1711
1712 static int *tgid_map;
1713
1714 #define SAVED_CMDLINES_DEFAULT 128
1715 #define NO_CMDLINE_MAP UINT_MAX
1716 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1717 struct saved_cmdlines_buffer {
1718         unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1719         unsigned *map_cmdline_to_pid;
1720         unsigned cmdline_num;
1721         int cmdline_idx;
1722         char *saved_cmdlines;
1723 };
1724 static struct saved_cmdlines_buffer *savedcmd;
1725
1726 /* temporary disable recording */
1727 static atomic_t trace_record_taskinfo_disabled __read_mostly;
1728
1729 static inline char *get_saved_cmdlines(int idx)
1730 {
1731         return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1732 }
1733
1734 static inline void set_cmdline(int idx, const char *cmdline)
1735 {
1736         memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1737 }
1738
1739 static int allocate_cmdlines_buffer(unsigned int val,
1740                                     struct saved_cmdlines_buffer *s)
1741 {
1742         s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1743                                         GFP_KERNEL);
1744         if (!s->map_cmdline_to_pid)
1745                 return -ENOMEM;
1746
1747         s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1748         if (!s->saved_cmdlines) {
1749                 kfree(s->map_cmdline_to_pid);
1750                 return -ENOMEM;
1751         }
1752
1753         s->cmdline_idx = 0;
1754         s->cmdline_num = val;
1755         memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1756                sizeof(s->map_pid_to_cmdline));
1757         memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1758                val * sizeof(*s->map_cmdline_to_pid));
1759
1760         return 0;
1761 }
1762
1763 static int trace_create_savedcmd(void)
1764 {
1765         int ret;
1766
1767         savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
1768         if (!savedcmd)
1769                 return -ENOMEM;
1770
1771         ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1772         if (ret < 0) {
1773                 kfree(savedcmd);
1774                 savedcmd = NULL;
1775                 return -ENOMEM;
1776         }
1777
1778         return 0;
1779 }
1780
1781 int is_tracing_stopped(void)
1782 {
1783         return global_trace.stop_count;
1784 }
1785
1786 /**
1787  * tracing_start - quick start of the tracer
1788  *
1789  * If tracing is enabled but was stopped by tracing_stop,
1790  * this will start the tracer back up.
1791  */
1792 void tracing_start(void)
1793 {
1794         struct ring_buffer *buffer;
1795         unsigned long flags;
1796
1797         if (tracing_disabled)
1798                 return;
1799
1800         raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1801         if (--global_trace.stop_count) {
1802                 if (global_trace.stop_count < 0) {
1803                         /* Someone screwed up their debugging */
1804                         WARN_ON_ONCE(1);
1805                         global_trace.stop_count = 0;
1806                 }
1807                 goto out;
1808         }
1809
1810         /* Prevent the buffers from switching */
1811         arch_spin_lock(&global_trace.max_lock);
1812
1813         buffer = global_trace.trace_buffer.buffer;
1814         if (buffer)
1815                 ring_buffer_record_enable(buffer);
1816
1817 #ifdef CONFIG_TRACER_MAX_TRACE
1818         buffer = global_trace.max_buffer.buffer;
1819         if (buffer)
1820                 ring_buffer_record_enable(buffer);
1821 #endif
1822
1823         arch_spin_unlock(&global_trace.max_lock);
1824
1825  out:
1826         raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1827 }
1828
1829 static void tracing_start_tr(struct trace_array *tr)
1830 {
1831         struct ring_buffer *buffer;
1832         unsigned long flags;
1833
1834         if (tracing_disabled)
1835                 return;
1836
1837         /* If global, we need to also start the max tracer */
1838         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1839                 return tracing_start();
1840
1841         raw_spin_lock_irqsave(&tr->start_lock, flags);
1842
1843         if (--tr->stop_count) {
1844                 if (tr->stop_count < 0) {
1845                         /* Someone screwed up their debugging */
1846                         WARN_ON_ONCE(1);
1847                         tr->stop_count = 0;
1848                 }
1849                 goto out;
1850         }
1851
1852         buffer = tr->trace_buffer.buffer;
1853         if (buffer)
1854                 ring_buffer_record_enable(buffer);
1855
1856  out:
1857         raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1858 }
1859
1860 /**
1861  * tracing_stop - quick stop of the tracer
1862  *
1863  * Light weight way to stop tracing. Use in conjunction with
1864  * tracing_start.
1865  */
1866 void tracing_stop(void)
1867 {
1868         struct ring_buffer *buffer;
1869         unsigned long flags;
1870
1871         raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1872         if (global_trace.stop_count++)
1873                 goto out;
1874
1875         /* Prevent the buffers from switching */
1876         arch_spin_lock(&global_trace.max_lock);
1877
1878         buffer = global_trace.trace_buffer.buffer;
1879         if (buffer)
1880                 ring_buffer_record_disable(buffer);
1881
1882 #ifdef CONFIG_TRACER_MAX_TRACE
1883         buffer = global_trace.max_buffer.buffer;
1884         if (buffer)
1885                 ring_buffer_record_disable(buffer);
1886 #endif
1887
1888         arch_spin_unlock(&global_trace.max_lock);
1889
1890  out:
1891         raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1892 }
1893
1894 static void tracing_stop_tr(struct trace_array *tr)
1895 {
1896         struct ring_buffer *buffer;
1897         unsigned long flags;
1898
1899         /* If global, we need to also stop the max tracer */
1900         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1901                 return tracing_stop();
1902
1903         raw_spin_lock_irqsave(&tr->start_lock, flags);
1904         if (tr->stop_count++)
1905                 goto out;
1906
1907         buffer = tr->trace_buffer.buffer;
1908         if (buffer)
1909                 ring_buffer_record_disable(buffer);
1910
1911  out:
1912         raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1913 }
1914
1915 static int trace_save_cmdline(struct task_struct *tsk)
1916 {
1917         unsigned pid, idx;
1918
1919         /* treat recording of idle task as a success */
1920         if (!tsk->pid)
1921                 return 1;
1922
1923         if (unlikely(tsk->pid > PID_MAX_DEFAULT))
1924                 return 0;
1925
1926         /*
1927          * It's not the end of the world if we don't get
1928          * the lock, but we also don't want to spin
1929          * nor do we want to disable interrupts,
1930          * so if we miss here, then better luck next time.
1931          */
1932         if (!arch_spin_trylock(&trace_cmdline_lock))
1933                 return 0;
1934
1935         idx = savedcmd->map_pid_to_cmdline[tsk->pid];
1936         if (idx == NO_CMDLINE_MAP) {
1937                 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
1938
1939                 /*
1940                  * Check whether the cmdline buffer at idx has a pid
1941                  * mapped. We are going to overwrite that entry so we
1942                  * need to clear the map_pid_to_cmdline. Otherwise we
1943                  * would read the new comm for the old pid.
1944                  */
1945                 pid = savedcmd->map_cmdline_to_pid[idx];
1946                 if (pid != NO_CMDLINE_MAP)
1947                         savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1948
1949                 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1950                 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
1951
1952                 savedcmd->cmdline_idx = idx;
1953         }
1954
1955         set_cmdline(idx, tsk->comm);
1956
1957         arch_spin_unlock(&trace_cmdline_lock);
1958
1959         return 1;
1960 }
1961
1962 static void __trace_find_cmdline(int pid, char comm[])
1963 {
1964         unsigned map;
1965
1966         if (!pid) {
1967                 strcpy(comm, "<idle>");
1968                 return;
1969         }
1970
1971         if (WARN_ON_ONCE(pid < 0)) {
1972                 strcpy(comm, "<XXX>");
1973                 return;
1974         }
1975
1976         if (pid > PID_MAX_DEFAULT) {
1977                 strcpy(comm, "<...>");
1978                 return;
1979         }
1980
1981         map = savedcmd->map_pid_to_cmdline[pid];
1982         if (map != NO_CMDLINE_MAP)
1983                 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
1984         else
1985                 strcpy(comm, "<...>");
1986 }
1987
1988 void trace_find_cmdline(int pid, char comm[])
1989 {
1990         preempt_disable();
1991         arch_spin_lock(&trace_cmdline_lock);
1992
1993         __trace_find_cmdline(pid, comm);
1994
1995         arch_spin_unlock(&trace_cmdline_lock);
1996         preempt_enable();
1997 }
1998
1999 int trace_find_tgid(int pid)
2000 {
2001         if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2002                 return 0;
2003
2004         return tgid_map[pid];
2005 }
2006
2007 static int trace_save_tgid(struct task_struct *tsk)
2008 {
2009         /* treat recording of idle task as a success */
2010         if (!tsk->pid)
2011                 return 1;
2012
2013         if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
2014                 return 0;
2015
2016         tgid_map[tsk->pid] = tsk->tgid;
2017         return 1;
2018 }
2019
2020 static bool tracing_record_taskinfo_skip(int flags)
2021 {
2022         if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2023                 return true;
2024         if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2025                 return true;
2026         if (!__this_cpu_read(trace_taskinfo_save))
2027                 return true;
2028         return false;
2029 }
2030
2031 /**
2032  * tracing_record_taskinfo - record the task info of a task
2033  *
2034  * @task  - task to record
2035  * @flags - TRACE_RECORD_CMDLINE for recording comm
2036  *        - TRACE_RECORD_TGID for recording tgid
2037  */
2038 void tracing_record_taskinfo(struct task_struct *task, int flags)
2039 {
2040         bool done;
2041
2042         if (tracing_record_taskinfo_skip(flags))
2043                 return;
2044
2045         /*
2046          * Record as much task information as possible. If some fail, continue
2047          * to try to record the others.
2048          */
2049         done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2050         done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2051
2052         /* If recording any information failed, retry again soon. */
2053         if (!done)
2054                 return;
2055
2056         __this_cpu_write(trace_taskinfo_save, false);
2057 }
2058
2059 /**
2060  * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2061  *
2062  * @prev - previous task during sched_switch
2063  * @next - next task during sched_switch
2064  * @flags - TRACE_RECORD_CMDLINE for recording comm
2065  *          TRACE_RECORD_TGID for recording tgid
2066  */
2067 void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2068                                           struct task_struct *next, int flags)
2069 {
2070         bool done;
2071
2072         if (tracing_record_taskinfo_skip(flags))
2073                 return;
2074
2075         /*
2076          * Record as much task information as possible. If some fail, continue
2077          * to try to record the others.
2078          */
2079         done  = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2080         done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2081         done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2082         done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
2083
2084         /* If recording any information failed, retry again soon. */
2085         if (!done)
2086                 return;
2087
2088         __this_cpu_write(trace_taskinfo_save, false);
2089 }
2090
2091 /* Helpers to record a specific task information */
2092 void tracing_record_cmdline(struct task_struct *task)
2093 {
2094         tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2095 }
2096
2097 void tracing_record_tgid(struct task_struct *task)
2098 {
2099         tracing_record_taskinfo(task, TRACE_RECORD_TGID);
2100 }
2101
2102 /*
2103  * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2104  * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2105  * simplifies those functions and keeps them in sync.
2106  */
2107 enum print_line_t trace_handle_return(struct trace_seq *s)
2108 {
2109         return trace_seq_has_overflowed(s) ?
2110                 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2111 }
2112 EXPORT_SYMBOL_GPL(trace_handle_return);
2113
2114 void
2115 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2116                              int pc)
2117 {
2118         struct task_struct *tsk = current;
2119
2120         entry->preempt_count            = pc & 0xff;
2121         entry->pid                      = (tsk) ? tsk->pid : 0;
2122         entry->flags =
2123 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2124                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
2125 #else
2126                 TRACE_FLAG_IRQS_NOSUPPORT |
2127 #endif
2128                 ((pc & NMI_MASK    ) ? TRACE_FLAG_NMI     : 0) |
2129                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
2130                 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
2131                 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2132                 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
2133 }
2134 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
2135
2136 struct ring_buffer_event *
2137 trace_buffer_lock_reserve(struct ring_buffer *buffer,
2138                           int type,
2139                           unsigned long len,
2140                           unsigned long flags, int pc)
2141 {
2142         return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
2143 }
2144
2145 DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2146 DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2147 static int trace_buffered_event_ref;
2148
2149 /**
2150  * trace_buffered_event_enable - enable buffering events
2151  *
2152  * When events are being filtered, it is quicker to use a temporary
2153  * buffer to write the event data into if there's a likely chance
2154  * that it will not be committed. The discard of the ring buffer
2155  * is not as fast as committing, and is much slower than copying
2156  * a commit.
2157  *
2158  * When an event is to be filtered, allocate per cpu buffers to
2159  * write the event data into, and if the event is filtered and discarded
2160  * it is simply dropped, otherwise, the entire data is to be committed
2161  * in one shot.
2162  */
2163 void trace_buffered_event_enable(void)
2164 {
2165         struct ring_buffer_event *event;
2166         struct page *page;
2167         int cpu;
2168
2169         WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2170
2171         if (trace_buffered_event_ref++)
2172                 return;
2173
2174         for_each_tracing_cpu(cpu) {
2175                 page = alloc_pages_node(cpu_to_node(cpu),
2176                                         GFP_KERNEL | __GFP_NORETRY, 0);
2177                 if (!page)
2178                         goto failed;
2179
2180                 event = page_address(page);
2181                 memset(event, 0, sizeof(*event));
2182
2183                 per_cpu(trace_buffered_event, cpu) = event;
2184
2185                 preempt_disable();
2186                 if (cpu == smp_processor_id() &&
2187                     this_cpu_read(trace_buffered_event) !=
2188                     per_cpu(trace_buffered_event, cpu))
2189                         WARN_ON_ONCE(1);
2190                 preempt_enable();
2191         }
2192
2193         return;
2194  failed:
2195         trace_buffered_event_disable();
2196 }
2197
2198 static void enable_trace_buffered_event(void *data)
2199 {
2200         /* Probably not needed, but do it anyway */
2201         smp_rmb();
2202         this_cpu_dec(trace_buffered_event_cnt);
2203 }
2204
2205 static void disable_trace_buffered_event(void *data)
2206 {
2207         this_cpu_inc(trace_buffered_event_cnt);
2208 }
2209
2210 /**
2211  * trace_buffered_event_disable - disable buffering events
2212  *
2213  * When a filter is removed, it is faster to not use the buffered
2214  * events, and to commit directly into the ring buffer. Free up
2215  * the temp buffers when there are no more users. This requires
2216  * special synchronization with current events.
2217  */
2218 void trace_buffered_event_disable(void)
2219 {
2220         int cpu;
2221
2222         WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2223
2224         if (WARN_ON_ONCE(!trace_buffered_event_ref))
2225                 return;
2226
2227         if (--trace_buffered_event_ref)
2228                 return;
2229
2230         preempt_disable();
2231         /* For each CPU, set the buffer as used. */
2232         smp_call_function_many(tracing_buffer_mask,
2233                                disable_trace_buffered_event, NULL, 1);
2234         preempt_enable();
2235
2236         /* Wait for all current users to finish */
2237         synchronize_sched();
2238
2239         for_each_tracing_cpu(cpu) {
2240                 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2241                 per_cpu(trace_buffered_event, cpu) = NULL;
2242         }
2243         /*
2244          * Make sure trace_buffered_event is NULL before clearing
2245          * trace_buffered_event_cnt.
2246          */
2247         smp_wmb();
2248
2249         preempt_disable();
2250         /* Do the work on each cpu */
2251         smp_call_function_many(tracing_buffer_mask,
2252                                enable_trace_buffered_event, NULL, 1);
2253         preempt_enable();
2254 }
2255
2256 static struct ring_buffer *temp_buffer;
2257
2258 struct ring_buffer_event *
2259 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
2260                           struct trace_event_file *trace_file,
2261                           int type, unsigned long len,
2262                           unsigned long flags, int pc)
2263 {
2264         struct ring_buffer_event *entry;
2265         int val;
2266
2267         *current_rb = trace_file->tr->trace_buffer.buffer;
2268
2269         if ((trace_file->flags &
2270              (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2271             (entry = this_cpu_read(trace_buffered_event))) {
2272                 /* Try to use the per cpu buffer first */
2273                 val = this_cpu_inc_return(trace_buffered_event_cnt);
2274                 if (val == 1) {
2275                         trace_event_setup(entry, type, flags, pc);
2276                         entry->array[0] = len;
2277                         return entry;
2278                 }
2279                 this_cpu_dec(trace_buffered_event_cnt);
2280         }
2281
2282         entry = __trace_buffer_lock_reserve(*current_rb,
2283                                             type, len, flags, pc);
2284         /*
2285          * If tracing is off, but we have triggers enabled
2286          * we still need to look at the event data. Use the temp_buffer
2287          * to store the trace event for the tigger to use. It's recusive
2288          * safe and will not be recorded anywhere.
2289          */
2290         if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2291                 *current_rb = temp_buffer;
2292                 entry = __trace_buffer_lock_reserve(*current_rb,
2293                                                     type, len, flags, pc);
2294         }
2295         return entry;
2296 }
2297 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2298
2299 static DEFINE_SPINLOCK(tracepoint_iter_lock);
2300 static DEFINE_MUTEX(tracepoint_printk_mutex);
2301
2302 static void output_printk(struct trace_event_buffer *fbuffer)
2303 {
2304         struct trace_event_call *event_call;
2305         struct trace_event *event;
2306         unsigned long flags;
2307         struct trace_iterator *iter = tracepoint_print_iter;
2308
2309         /* We should never get here if iter is NULL */
2310         if (WARN_ON_ONCE(!iter))
2311                 return;
2312
2313         event_call = fbuffer->trace_file->event_call;
2314         if (!event_call || !event_call->event.funcs ||
2315             !event_call->event.funcs->trace)
2316                 return;
2317
2318         event = &fbuffer->trace_file->event_call->event;
2319
2320         spin_lock_irqsave(&tracepoint_iter_lock, flags);
2321         trace_seq_init(&iter->seq);
2322         iter->ent = fbuffer->entry;
2323         event_call->event.funcs->trace(iter, 0, event);
2324         trace_seq_putc(&iter->seq, 0);
2325         printk("%s", iter->seq.buffer);
2326
2327         spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2328 }
2329
2330 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2331                              void __user *buffer, size_t *lenp,
2332                              loff_t *ppos)
2333 {
2334         int save_tracepoint_printk;
2335         int ret;
2336
2337         mutex_lock(&tracepoint_printk_mutex);
2338         save_tracepoint_printk = tracepoint_printk;
2339
2340         ret = proc_dointvec(table, write, buffer, lenp, ppos);
2341
2342         /*
2343          * This will force exiting early, as tracepoint_printk
2344          * is always zero when tracepoint_printk_iter is not allocated
2345          */
2346         if (!tracepoint_print_iter)
2347                 tracepoint_printk = 0;
2348
2349         if (save_tracepoint_printk == tracepoint_printk)
2350                 goto out;
2351
2352         if (tracepoint_printk)
2353                 static_key_enable(&tracepoint_printk_key.key);
2354         else
2355                 static_key_disable(&tracepoint_printk_key.key);
2356
2357  out:
2358         mutex_unlock(&tracepoint_printk_mutex);
2359
2360         return ret;
2361 }
2362
2363 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2364 {
2365         if (static_key_false(&tracepoint_printk_key.key))
2366                 output_printk(fbuffer);
2367
2368         event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2369                                     fbuffer->event, fbuffer->entry,
2370                                     fbuffer->flags, fbuffer->pc);
2371 }
2372 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2373
2374 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2375                                      struct ring_buffer *buffer,
2376                                      struct ring_buffer_event *event,
2377                                      unsigned long flags, int pc,
2378                                      struct pt_regs *regs)
2379 {
2380         __buffer_unlock_commit(buffer, event);
2381
2382         /*
2383          * If regs is not set, then skip the following callers:
2384          *   trace_buffer_unlock_commit_regs
2385          *   event_trigger_unlock_commit
2386          *   trace_event_buffer_commit
2387          *   trace_event_raw_event_sched_switch
2388          * Note, we can still get here via blktrace, wakeup tracer
2389          * and mmiotrace, but that's ok if they lose a function or
2390          * two. They are that meaningful.
2391          */
2392         ftrace_trace_stack(tr, buffer, flags, regs ? 0 : 4, pc, regs);
2393         ftrace_trace_userstack(buffer, flags, pc);
2394 }
2395
2396 /*
2397  * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2398  */
2399 void
2400 trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2401                                    struct ring_buffer_event *event)
2402 {
2403         __buffer_unlock_commit(buffer, event);
2404 }
2405
2406 static void
2407 trace_process_export(struct trace_export *export,
2408                struct ring_buffer_event *event)
2409 {
2410         struct trace_entry *entry;
2411         unsigned int size = 0;
2412
2413         entry = ring_buffer_event_data(event);
2414         size = ring_buffer_event_length(event);
2415         export->write(entry, size);
2416 }
2417
2418 static DEFINE_MUTEX(ftrace_export_lock);
2419
2420 static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2421
2422 static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2423
2424 static inline void ftrace_exports_enable(void)
2425 {
2426         static_branch_enable(&ftrace_exports_enabled);
2427 }
2428
2429 static inline void ftrace_exports_disable(void)
2430 {
2431         static_branch_disable(&ftrace_exports_enabled);
2432 }
2433
2434 void ftrace_exports(struct ring_buffer_event *event)
2435 {
2436         struct trace_export *export;
2437
2438         preempt_disable_notrace();
2439
2440         export = rcu_dereference_raw_notrace(ftrace_exports_list);
2441         while (export) {
2442                 trace_process_export(export, event);
2443                 export = rcu_dereference_raw_notrace(export->next);
2444         }
2445
2446         preempt_enable_notrace();
2447 }
2448
2449 static inline void
2450 add_trace_export(struct trace_export **list, struct trace_export *export)
2451 {
2452         rcu_assign_pointer(export->next, *list);
2453         /*
2454          * We are entering export into the list but another
2455          * CPU might be walking that list. We need to make sure
2456          * the export->next pointer is valid before another CPU sees
2457          * the export pointer included into the list.
2458          */
2459         rcu_assign_pointer(*list, export);
2460 }
2461
2462 static inline int
2463 rm_trace_export(struct trace_export **list, struct trace_export *export)
2464 {
2465         struct trace_export **p;
2466
2467         for (p = list; *p != NULL; p = &(*p)->next)
2468                 if (*p == export)
2469                         break;
2470
2471         if (*p != export)
2472                 return -1;
2473
2474         rcu_assign_pointer(*p, (*p)->next);
2475
2476         return 0;
2477 }
2478
2479 static inline void
2480 add_ftrace_export(struct trace_export **list, struct trace_export *export)
2481 {
2482         if (*list == NULL)
2483                 ftrace_exports_enable();
2484
2485         add_trace_export(list, export);
2486 }
2487
2488 static inline int
2489 rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2490 {
2491         int ret;
2492
2493         ret = rm_trace_export(list, export);
2494         if (*list == NULL)
2495                 ftrace_exports_disable();
2496
2497         return ret;
2498 }
2499
2500 int register_ftrace_export(struct trace_export *export)
2501 {
2502         if (WARN_ON_ONCE(!export->write))
2503                 return -1;
2504
2505         mutex_lock(&ftrace_export_lock);
2506
2507         add_ftrace_export(&ftrace_exports_list, export);
2508
2509         mutex_unlock(&ftrace_export_lock);
2510
2511         return 0;
2512 }
2513 EXPORT_SYMBOL_GPL(register_ftrace_export);
2514
2515 int unregister_ftrace_export(struct trace_export *export)
2516 {
2517         int ret;
2518
2519         mutex_lock(&ftrace_export_lock);
2520
2521         ret = rm_ftrace_export(&ftrace_exports_list, export);
2522
2523         mutex_unlock(&ftrace_export_lock);
2524
2525         return ret;
2526 }
2527 EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2528
2529 void
2530 trace_function(struct trace_array *tr,
2531                unsigned long ip, unsigned long parent_ip, unsigned long flags,
2532                int pc)
2533 {
2534         struct trace_event_call *call = &event_function;
2535         struct ring_buffer *buffer = tr->trace_buffer.buffer;
2536         struct ring_buffer_event *event;
2537         struct ftrace_entry *entry;
2538
2539         event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2540                                             flags, pc);
2541         if (!event)
2542                 return;
2543         entry   = ring_buffer_event_data(event);
2544         entry->ip                       = ip;
2545         entry->parent_ip                = parent_ip;
2546
2547         if (!call_filter_check_discard(call, entry, buffer, event)) {
2548                 if (static_branch_unlikely(&ftrace_exports_enabled))
2549                         ftrace_exports(event);
2550                 __buffer_unlock_commit(buffer, event);
2551         }
2552 }
2553
2554 #ifdef CONFIG_STACKTRACE
2555
2556 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2557 struct ftrace_stack {
2558         unsigned long           calls[FTRACE_STACK_MAX_ENTRIES];
2559 };
2560
2561 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2562 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2563
2564 static void __ftrace_trace_stack(struct ring_buffer *buffer,
2565                                  unsigned long flags,
2566                                  int skip, int pc, struct pt_regs *regs)
2567 {
2568         struct trace_event_call *call = &event_kernel_stack;
2569         struct ring_buffer_event *event;
2570         struct stack_entry *entry;
2571         struct stack_trace trace;
2572         int use_stack;
2573         int size = FTRACE_STACK_ENTRIES;
2574
2575         trace.nr_entries        = 0;
2576         trace.skip              = skip;
2577
2578         /*
2579          * Add two, for this function and the call to save_stack_trace()
2580          * If regs is set, then these functions will not be in the way.
2581          */
2582         if (!regs)
2583                 trace.skip += 2;
2584
2585         /*
2586          * Since events can happen in NMIs there's no safe way to
2587          * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2588          * or NMI comes in, it will just have to use the default
2589          * FTRACE_STACK_SIZE.
2590          */
2591         preempt_disable_notrace();
2592
2593         use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
2594         /*
2595          * We don't need any atomic variables, just a barrier.
2596          * If an interrupt comes in, we don't care, because it would
2597          * have exited and put the counter back to what we want.
2598          * We just need a barrier to keep gcc from moving things
2599          * around.
2600          */
2601         barrier();
2602         if (use_stack == 1) {
2603                 trace.entries           = this_cpu_ptr(ftrace_stack.calls);
2604                 trace.max_entries       = FTRACE_STACK_MAX_ENTRIES;
2605
2606                 if (regs)
2607                         save_stack_trace_regs(regs, &trace);
2608                 else
2609                         save_stack_trace(&trace);
2610
2611                 if (trace.nr_entries > size)
2612                         size = trace.nr_entries;
2613         } else
2614                 /* From now on, use_stack is a boolean */
2615                 use_stack = 0;
2616
2617         size *= sizeof(unsigned long);
2618
2619         event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2620                                             sizeof(*entry) + size, flags, pc);
2621         if (!event)
2622                 goto out;
2623         entry = ring_buffer_event_data(event);
2624
2625         memset(&entry->caller, 0, size);
2626
2627         if (use_stack)
2628                 memcpy(&entry->caller, trace.entries,
2629                        trace.nr_entries * sizeof(unsigned long));
2630         else {
2631                 trace.max_entries       = FTRACE_STACK_ENTRIES;
2632                 trace.entries           = entry->caller;
2633                 if (regs)
2634                         save_stack_trace_regs(regs, &trace);
2635                 else
2636                         save_stack_trace(&trace);
2637         }
2638
2639         entry->size = trace.nr_entries;
2640
2641         if (!call_filter_check_discard(call, entry, buffer, event))
2642                 __buffer_unlock_commit(buffer, event);
2643
2644  out:
2645         /* Again, don't let gcc optimize things here */
2646         barrier();
2647         __this_cpu_dec(ftrace_stack_reserve);
2648         preempt_enable_notrace();
2649
2650 }
2651
2652 static inline void ftrace_trace_stack(struct trace_array *tr,
2653                                       struct ring_buffer *buffer,
2654                                       unsigned long flags,
2655                                       int skip, int pc, struct pt_regs *regs)
2656 {
2657         if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
2658                 return;
2659
2660         __ftrace_trace_stack(buffer, flags, skip, pc, regs);
2661 }
2662
2663 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2664                    int pc)
2665 {
2666         struct ring_buffer *buffer = tr->trace_buffer.buffer;
2667
2668         if (rcu_is_watching()) {
2669                 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2670                 return;
2671         }
2672
2673         /*
2674          * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2675          * but if the above rcu_is_watching() failed, then the NMI
2676          * triggered someplace critical, and rcu_irq_enter() should
2677          * not be called from NMI.
2678          */
2679         if (unlikely(in_nmi()))
2680                 return;
2681
2682         /*
2683          * It is possible that a function is being traced in a
2684          * location that RCU is not watching. A call to
2685          * rcu_irq_enter() will make sure that it is, but there's
2686          * a few internal rcu functions that could be traced
2687          * where that wont work either. In those cases, we just
2688          * do nothing.
2689          */
2690         if (unlikely(rcu_irq_enter_disabled()))
2691                 return;
2692
2693         rcu_irq_enter_irqson();
2694         __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2695         rcu_irq_exit_irqson();
2696 }
2697
2698 /**
2699  * trace_dump_stack - record a stack back trace in the trace buffer
2700  * @skip: Number of functions to skip (helper handlers)
2701  */
2702 void trace_dump_stack(int skip)
2703 {
2704         unsigned long flags;
2705
2706         if (tracing_disabled || tracing_selftest_running)
2707                 return;
2708
2709         local_save_flags(flags);
2710
2711         /*
2712          * Skip 3 more, seems to get us at the caller of
2713          * this function.
2714          */
2715         skip += 3;
2716         __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2717                              flags, skip, preempt_count(), NULL);
2718 }
2719
2720 static DEFINE_PER_CPU(int, user_stack_count);
2721
2722 void
2723 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
2724 {
2725         struct trace_event_call *call = &event_user_stack;
2726         struct ring_buffer_event *event;
2727         struct userstack_entry *entry;
2728         struct stack_trace trace;
2729
2730         if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
2731                 return;
2732
2733         /*
2734          * NMIs can not handle page faults, even with fix ups.
2735          * The save user stack can (and often does) fault.
2736          */
2737         if (unlikely(in_nmi()))
2738                 return;
2739
2740         /*
2741          * prevent recursion, since the user stack tracing may
2742          * trigger other kernel events.
2743          */
2744         preempt_disable();
2745         if (__this_cpu_read(user_stack_count))
2746                 goto out;
2747
2748         __this_cpu_inc(user_stack_count);
2749
2750         event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
2751                                             sizeof(*entry), flags, pc);
2752         if (!event)
2753                 goto out_drop_count;
2754         entry   = ring_buffer_event_data(event);
2755
2756         entry->tgid             = current->tgid;
2757         memset(&entry->caller, 0, sizeof(entry->caller));
2758
2759         trace.nr_entries        = 0;
2760         trace.max_entries       = FTRACE_STACK_ENTRIES;
2761         trace.skip              = 0;
2762         trace.entries           = entry->caller;
2763
2764         save_stack_trace_user(&trace);
2765         if (!call_filter_check_discard(call, entry, buffer, event))
2766                 __buffer_unlock_commit(buffer, event);
2767
2768  out_drop_count:
2769         __this_cpu_dec(user_stack_count);
2770  out:
2771         preempt_enable();
2772 }
2773
2774 #ifdef UNUSED
2775 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
2776 {
2777         ftrace_trace_userstack(tr, flags, preempt_count());
2778 }
2779 #endif /* UNUSED */
2780
2781 #endif /* CONFIG_STACKTRACE */
2782
2783 /* created for use with alloc_percpu */
2784 struct trace_buffer_struct {
2785         int nesting;
2786         char buffer[4][TRACE_BUF_SIZE];
2787 };
2788
2789 static struct trace_buffer_struct *trace_percpu_buffer;
2790
2791 /*
2792  * Thise allows for lockless recording.  If we're nested too deeply, then
2793  * this returns NULL.
2794  */
2795 static char *get_trace_buf(void)
2796 {
2797         struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
2798
2799         if (!buffer || buffer->nesting >= 4)
2800                 return NULL;
2801
2802         return &buffer->buffer[buffer->nesting++][0];
2803 }
2804
2805 static void put_trace_buf(void)
2806 {
2807         this_cpu_dec(trace_percpu_buffer->nesting);
2808 }
2809
2810 static int alloc_percpu_trace_buffer(void)
2811 {
2812         struct trace_buffer_struct *buffers;
2813
2814         buffers = alloc_percpu(struct trace_buffer_struct);
2815         if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
2816                 return -ENOMEM;
2817
2818         trace_percpu_buffer = buffers;
2819         return 0;
2820 }
2821
2822 static int buffers_allocated;
2823
2824 void trace_printk_init_buffers(void)
2825 {
2826         if (buffers_allocated)
2827                 return;
2828
2829         if (alloc_percpu_trace_buffer())
2830                 return;
2831
2832         /* trace_printk() is for debug use only. Don't use it in production. */
2833
2834         pr_warn("\n");
2835         pr_warn("**********************************************************\n");
2836         pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
2837         pr_warn("**                                                      **\n");
2838         pr_warn("** trace_printk() being used. Allocating extra memory.  **\n");
2839         pr_warn("**                                                      **\n");
2840         pr_warn("** This means that this is a DEBUG kernel and it is     **\n");
2841         pr_warn("** unsafe for production use.                           **\n");
2842         pr_warn("**                                                      **\n");
2843         pr_warn("** If you see this message and you are not debugging    **\n");
2844         pr_warn("** the kernel, report this immediately to your vendor!  **\n");
2845         pr_warn("**                                                      **\n");
2846         pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
2847         pr_warn("**********************************************************\n");
2848
2849         /* Expand the buffers to set size */
2850         tracing_update_buffers();
2851
2852         buffers_allocated = 1;
2853
2854         /*
2855          * trace_printk_init_buffers() can be called by modules.
2856          * If that happens, then we need to start cmdline recording
2857          * directly here. If the global_trace.buffer is already
2858          * allocated here, then this was called by module code.
2859          */
2860         if (global_trace.trace_buffer.buffer)
2861                 tracing_start_cmdline_record();
2862 }
2863
2864 void trace_printk_start_comm(void)
2865 {
2866         /* Start tracing comms if trace printk is set */
2867         if (!buffers_allocated)
2868                 return;
2869         tracing_start_cmdline_record();
2870 }
2871
2872 static void trace_printk_start_stop_comm(int enabled)
2873 {
2874         if (!buffers_allocated)
2875                 return;
2876
2877         if (enabled)
2878                 tracing_start_cmdline_record();
2879         else
2880                 tracing_stop_cmdline_record();
2881 }
2882
2883 /**
2884  * trace_vbprintk - write binary msg to tracing buffer
2885  *
2886  */
2887 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
2888 {
2889         struct trace_event_call *call = &event_bprint;
2890         struct ring_buffer_event *event;
2891         struct ring_buffer *buffer;
2892         struct trace_array *tr = &global_trace;
2893         struct bprint_entry *entry;
2894         unsigned long flags;
2895         char *tbuffer;
2896         int len = 0, size, pc;
2897
2898         if (unlikely(tracing_selftest_running || tracing_disabled))
2899                 return 0;
2900
2901         /* Don't pollute graph traces with trace_vprintk internals */
2902         pause_graph_tracing();
2903
2904         pc = preempt_count();
2905         preempt_disable_notrace();
2906
2907         tbuffer = get_trace_buf();
2908         if (!tbuffer) {
2909                 len = 0;
2910                 goto out_nobuffer;
2911         }
2912
2913         len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2914
2915         if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2916                 goto out;
2917
2918         local_save_flags(flags);
2919         size = sizeof(*entry) + sizeof(u32) * len;
2920         buffer = tr->trace_buffer.buffer;
2921         event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2922                                             flags, pc);
2923         if (!event)
2924                 goto out;
2925         entry = ring_buffer_event_data(event);
2926         entry->ip                       = ip;
2927         entry->fmt                      = fmt;
2928
2929         memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2930         if (!call_filter_check_discard(call, entry, buffer, event)) {
2931                 __buffer_unlock_commit(buffer, event);
2932                 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
2933         }
2934
2935 out:
2936         put_trace_buf();
2937
2938 out_nobuffer:
2939         preempt_enable_notrace();
2940         unpause_graph_tracing();
2941
2942         return len;
2943 }
2944 EXPORT_SYMBOL_GPL(trace_vbprintk);
2945
2946 static int
2947 __trace_array_vprintk(struct ring_buffer *buffer,
2948                       unsigned long ip, const char *fmt, va_list args)
2949 {
2950         struct trace_event_call *call = &event_print;
2951         struct ring_buffer_event *event;
2952         int len = 0, size, pc;
2953         struct print_entry *entry;
2954         unsigned long flags;
2955         char *tbuffer;
2956
2957         if (tracing_disabled || tracing_selftest_running)
2958                 return 0;
2959
2960         /* Don't pollute graph traces with trace_vprintk internals */
2961         pause_graph_tracing();
2962
2963         pc = preempt_count();
2964         preempt_disable_notrace();
2965
2966
2967         tbuffer = get_trace_buf();
2968         if (!tbuffer) {
2969                 len = 0;
2970                 goto out_nobuffer;
2971         }
2972
2973         len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2974
2975         local_save_flags(flags);
2976         size = sizeof(*entry) + len + 1;
2977         event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2978                                             flags, pc);
2979         if (!event)
2980                 goto out;
2981         entry = ring_buffer_event_data(event);
2982         entry->ip = ip;
2983
2984         memcpy(&entry->buf, tbuffer, len + 1);
2985         if (!call_filter_check_discard(call, entry, buffer, event)) {
2986                 __buffer_unlock_commit(buffer, event);
2987                 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
2988         }
2989
2990 out:
2991         put_trace_buf();
2992
2993 out_nobuffer:
2994         preempt_enable_notrace();
2995         unpause_graph_tracing();
2996
2997         return len;
2998 }
2999
3000 int trace_array_vprintk(struct trace_array *tr,
3001                         unsigned long ip, const char *fmt, va_list args)
3002 {
3003         return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3004 }
3005
3006 int trace_array_printk(struct trace_array *tr,
3007                        unsigned long ip, const char *fmt, ...)
3008 {
3009         int ret;
3010         va_list ap;
3011
3012         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3013                 return 0;
3014
3015         va_start(ap, fmt);
3016         ret = trace_array_vprintk(tr, ip, fmt, ap);
3017         va_end(ap);
3018         return ret;
3019 }
3020
3021 int trace_array_printk_buf(struct ring_buffer *buffer,
3022                            unsigned long ip, const char *fmt, ...)
3023 {
3024         int ret;
3025         va_list ap;
3026
3027         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3028                 return 0;
3029
3030         va_start(ap, fmt);
3031         ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3032         va_end(ap);
3033         return ret;
3034 }
3035
3036 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3037 {
3038         return trace_array_vprintk(&global_trace, ip, fmt, args);
3039 }
3040 EXPORT_SYMBOL_GPL(trace_vprintk);
3041
3042 static void trace_iterator_increment(struct trace_iterator *iter)
3043 {
3044         struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3045
3046         iter->idx++;
3047         if (buf_iter)
3048                 ring_buffer_read(buf_iter, NULL);
3049 }
3050
3051 static struct trace_entry *
3052 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3053                 unsigned long *lost_events)
3054 {
3055         struct ring_buffer_event *event;
3056         struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
3057
3058         if (buf_iter)
3059                 event = ring_buffer_iter_peek(buf_iter, ts);
3060         else
3061                 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
3062                                          lost_events);
3063
3064         if (event) {
3065                 iter->ent_size = ring_buffer_event_length(event);
3066                 return ring_buffer_event_data(event);
3067         }
3068         iter->ent_size = 0;
3069         return NULL;
3070 }
3071
3072 static struct trace_entry *
3073 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3074                   unsigned long *missing_events, u64 *ent_ts)
3075 {
3076         struct ring_buffer *buffer = iter->trace_buffer->buffer;
3077         struct trace_entry *ent, *next = NULL;
3078         unsigned long lost_events = 0, next_lost = 0;
3079         int cpu_file = iter->cpu_file;
3080         u64 next_ts = 0, ts;
3081         int next_cpu = -1;
3082         int next_size = 0;
3083         int cpu;
3084
3085         /*
3086          * If we are in a per_cpu trace file, don't bother by iterating over
3087          * all cpu and peek directly.
3088          */
3089         if (cpu_file > RING_BUFFER_ALL_CPUS) {
3090                 if (ring_buffer_empty_cpu(buffer, cpu_file))
3091                         return NULL;
3092                 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
3093                 if (ent_cpu)
3094                         *ent_cpu = cpu_file;
3095
3096                 return ent;
3097         }
3098
3099         for_each_tracing_cpu(cpu) {
3100
3101                 if (ring_buffer_empty_cpu(buffer, cpu))
3102                         continue;
3103
3104                 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
3105
3106                 /*
3107                  * Pick the entry with the smallest timestamp:
3108                  */
3109                 if (ent && (!next || ts < next_ts)) {
3110                         next = ent;
3111                         next_cpu = cpu;
3112                         next_ts = ts;
3113                         next_lost = lost_events;
3114                         next_size = iter->ent_size;
3115                 }
3116         }
3117
3118         iter->ent_size = next_size;
3119
3120         if (ent_cpu)
3121                 *ent_cpu = next_cpu;
3122
3123         if (ent_ts)
3124                 *ent_ts = next_ts;
3125
3126         if (missing_events)
3127                 *missing_events = next_lost;
3128
3129         return next;
3130 }
3131
3132 /* Find the next real entry, without updating the iterator itself */
3133 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3134                                           int *ent_cpu, u64 *ent_ts)
3135 {
3136         return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3137 }
3138
3139 /* Find the next real entry, and increment the iterator to the next entry */
3140 void *trace_find_next_entry_inc(struct trace_iterator *iter)
3141 {
3142         iter->ent = __find_next_entry(iter, &iter->cpu,
3143                                       &iter->lost_events, &iter->ts);
3144
3145         if (iter->ent)
3146                 trace_iterator_increment(iter);
3147
3148         return iter->ent ? iter : NULL;
3149 }
3150
3151 static void trace_consume(struct trace_iterator *iter)
3152 {
3153         ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
3154                             &iter->lost_events);
3155 }
3156
3157 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
3158 {
3159         struct trace_iterator *iter = m->private;
3160         int i = (int)*pos;
3161         void *ent;
3162
3163         WARN_ON_ONCE(iter->leftover);
3164
3165         (*pos)++;
3166
3167         /* can't go backwards */
3168         if (iter->idx > i)
3169                 return NULL;
3170
3171         if (iter->idx < 0)
3172                 ent = trace_find_next_entry_inc(iter);
3173         else
3174                 ent = iter;
3175
3176         while (ent && iter->idx < i)
3177                 ent = trace_find_next_entry_inc(iter);
3178
3179         iter->pos = *pos;
3180
3181         return ent;
3182 }
3183
3184 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
3185 {
3186         struct ring_buffer_event *event;
3187         struct ring_buffer_iter *buf_iter;
3188         unsigned long entries = 0;
3189         u64 ts;
3190
3191         per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
3192
3193         buf_iter = trace_buffer_iter(iter, cpu);
3194         if (!buf_iter)
3195                 return;
3196
3197         ring_buffer_iter_reset(buf_iter);
3198
3199         /*
3200          * We could have the case with the max latency tracers
3201          * that a reset never took place on a cpu. This is evident
3202          * by the timestamp being before the start of the buffer.
3203          */
3204         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
3205                 if (ts >= iter->trace_buffer->time_start)
3206                         break;
3207                 entries++;
3208                 ring_buffer_read(buf_iter, NULL);
3209         }
3210
3211         per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
3212 }
3213
3214 /*
3215  * The current tracer is copied to avoid a global locking
3216  * all around.
3217  */
3218 static void *s_start(struct seq_file *m, loff_t *pos)
3219 {
3220         struct trace_iterator *iter = m->private;
3221         struct trace_array *tr = iter->tr;
3222         int cpu_file = iter->cpu_file;
3223         void *p = NULL;
3224         loff_t l = 0;
3225         int cpu;
3226
3227         /*
3228          * copy the tracer to avoid using a global lock all around.
3229          * iter->trace is a copy of current_trace, the pointer to the
3230          * name may be used instead of a strcmp(), as iter->trace->name
3231          * will point to the same string as current_trace->name.
3232          */
3233         mutex_lock(&trace_types_lock);
3234         if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3235                 *iter->trace = *tr->current_trace;
3236         mutex_unlock(&trace_types_lock);
3237
3238 #ifdef CONFIG_TRACER_MAX_TRACE
3239         if (iter->snapshot && iter->trace->use_max_tr)
3240                 return ERR_PTR(-EBUSY);
3241 #endif
3242
3243         if (!iter->snapshot)
3244                 atomic_inc(&trace_record_taskinfo_disabled);
3245
3246         if (*pos != iter->pos) {
3247                 iter->ent = NULL;
3248                 iter->cpu = 0;
3249                 iter->idx = -1;
3250
3251                 if (cpu_file == RING_BUFFER_ALL_CPUS) {
3252                         for_each_tracing_cpu(cpu)
3253                                 tracing_iter_reset(iter, cpu);
3254                 } else
3255                         tracing_iter_reset(iter, cpu_file);
3256
3257                 iter->leftover = 0;
3258                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3259                         ;
3260
3261         } else {
3262                 /*
3263                  * If we overflowed the seq_file before, then we want
3264                  * to just reuse the trace_seq buffer again.
3265                  */
3266                 if (iter->leftover)
3267                         p = iter;
3268                 else {
3269                         l = *pos - 1;
3270                         p = s_next(m, p, &l);
3271                 }
3272         }
3273
3274         trace_event_read_lock();
3275         trace_access_lock(cpu_file);
3276         return p;
3277 }
3278
3279 static void s_stop(struct seq_file *m, void *p)
3280 {
3281         struct trace_iterator *iter = m->private;
3282
3283 #ifdef CONFIG_TRACER_MAX_TRACE
3284         if (iter->snapshot && iter->trace->use_max_tr)
3285                 return;
3286 #endif
3287
3288         if (!iter->snapshot)
3289                 atomic_dec(&trace_record_taskinfo_disabled);
3290
3291         trace_access_unlock(iter->cpu_file);
3292         trace_event_read_unlock();
3293 }
3294
3295 static void
3296 get_total_entries(struct trace_buffer *buf,
3297                   unsigned long *total, unsigned long *entries)
3298 {
3299         unsigned long count;
3300         int cpu;
3301
3302         *total = 0;
3303         *entries = 0;
3304
3305         for_each_tracing_cpu(cpu) {
3306                 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3307                 /*
3308                  * If this buffer has skipped entries, then we hold all
3309                  * entries for the trace and we need to ignore the
3310                  * ones before the time stamp.
3311                  */
3312                 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3313                         count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3314                         /* total is the same as the entries */
3315                         *total += count;
3316                 } else
3317                         *total += count +
3318                                 ring_buffer_overrun_cpu(buf->buffer, cpu);
3319                 *entries += count;
3320         }
3321 }
3322
3323 static void print_lat_help_header(struct seq_file *m)
3324 {
3325         seq_puts(m, "#                  _------=> CPU#            \n"
3326                     "#                 / _-----=> irqs-off        \n"
3327                     "#                | / _----=> need-resched    \n"
3328                     "#                || / _---=> hardirq/softirq \n"
3329                     "#                ||| / _--=> preempt-depth   \n"
3330                     "#                |||| /     delay            \n"
3331                     "#  cmd     pid   ||||| time  |   caller      \n"
3332                     "#     \\   /      |||||  \\    |   /         \n");
3333 }
3334
3335 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
3336 {
3337         unsigned long total;
3338         unsigned long entries;
3339
3340         get_total_entries(buf, &total, &entries);
3341         seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu   #P:%d\n",
3342                    entries, total, num_online_cpus());
3343         seq_puts(m, "#\n");
3344 }
3345
3346 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3347                                    unsigned int flags)
3348 {
3349         bool tgid = flags & TRACE_ITER_RECORD_TGID;
3350
3351         print_event_info(buf, m);
3352
3353         seq_printf(m, "#           TASK-PID   CPU#   %s  TIMESTAMP  FUNCTION\n", tgid ? "TGID     " : "");
3354         seq_printf(m, "#              | |       |    %s     |         |\n",      tgid ? "  |      " : "");
3355 }
3356
3357 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3358                                        unsigned int flags)
3359 {
3360         bool tgid = flags & TRACE_ITER_RECORD_TGID;
3361         const char tgid_space[] = "          ";
3362         const char space[] = "  ";
3363
3364         seq_printf(m, "#                          %s  _-----=> irqs-off\n",
3365                    tgid ? tgid_space : space);
3366         seq_printf(m, "#                          %s / _----=> need-resched\n",
3367                    tgid ? tgid_space : space);
3368         seq_printf(m, "#                          %s| / _---=> hardirq/softirq\n",
3369                    tgid ? tgid_space : space);
3370         seq_printf(m, "#                          %s|| / _--=> preempt-depth\n",
3371                    tgid ? tgid_space : space);
3372         seq_printf(m, "#                          %s||| /     delay\n",
3373                    tgid ? tgid_space : space);
3374         seq_printf(m, "#           TASK-PID   CPU#%s||||    TIMESTAMP  FUNCTION\n",
3375                    tgid ? "   TGID   " : space);
3376         seq_printf(m, "#              | |       | %s||||       |         |\n",
3377                    tgid ? "     |    " : space);
3378 }
3379
3380 void
3381 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3382 {
3383         unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
3384         struct trace_buffer *buf = iter->trace_buffer;
3385         struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
3386         struct tracer *type = iter->trace;
3387         unsigned long entries;
3388         unsigned long total;
3389         const char *name = "preemption";
3390
3391         name = type->name;
3392
3393         get_total_entries(buf, &total, &entries);
3394
3395         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
3396                    name, UTS_RELEASE);
3397         seq_puts(m, "# -----------------------------------"
3398                  "---------------------------------\n");
3399         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
3400                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
3401                    nsecs_to_usecs(data->saved_latency),
3402                    entries,
3403                    total,
3404                    buf->cpu,
3405 #if defined(CONFIG_PREEMPT_NONE)
3406                    "server",
3407 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
3408                    "desktop",
3409 #elif defined(CONFIG_PREEMPT)
3410                    "preempt",
3411 #else
3412                    "unknown",
3413 #endif
3414                    /* These are reserved for later use */
3415                    0, 0, 0, 0);
3416 #ifdef CONFIG_SMP
3417         seq_printf(m, " #P:%d)\n", num_online_cpus());
3418 #else
3419         seq_puts(m, ")\n");
3420 #endif
3421         seq_puts(m, "#    -----------------\n");
3422         seq_printf(m, "#    | task: %.16s-%d "
3423                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
3424                    data->comm, data->pid,
3425                    from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
3426                    data->policy, data->rt_priority);
3427         seq_puts(m, "#    -----------------\n");
3428
3429         if (data->critical_start) {
3430                 seq_puts(m, "#  => started at: ");
3431                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3432                 trace_print_seq(m, &iter->seq);
3433                 seq_puts(m, "\n#  => ended at:   ");
3434                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3435                 trace_print_seq(m, &iter->seq);
3436                 seq_puts(m, "\n#\n");
3437         }
3438
3439         seq_puts(m, "#\n");
3440 }
3441
3442 static void test_cpu_buff_start(struct trace_iterator *iter)
3443 {
3444         struct trace_seq *s = &iter->seq;
3445         struct trace_array *tr = iter->tr;
3446
3447         if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
3448                 return;
3449
3450         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3451                 return;
3452
3453         if (cpumask_available(iter->started) &&
3454             cpumask_test_cpu(iter->cpu, iter->started))
3455                 return;
3456
3457         if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
3458                 return;
3459
3460         if (cpumask_available(iter->started))
3461                 cpumask_set_cpu(iter->cpu, iter->started);
3462
3463         /* Don't print started cpu buffer for the first entry of the trace */
3464         if (iter->idx > 1)
3465                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3466                                 iter->cpu);
3467 }
3468
3469 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
3470 {
3471         struct trace_array *tr = iter->tr;
3472         struct trace_seq *s = &iter->seq;
3473         unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
3474         struct trace_entry *entry;
3475         struct trace_event *event;
3476
3477         entry = iter->ent;
3478
3479         test_cpu_buff_start(iter);
3480
3481         event = ftrace_find_event(entry->type);
3482
3483         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3484                 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3485                         trace_print_lat_context(iter);
3486                 else
3487                         trace_print_context(iter);
3488         }
3489
3490         if (trace_seq_has_overflowed(s))
3491                 return TRACE_TYPE_PARTIAL_LINE;
3492
3493         if (event)
3494                 return event->funcs->trace(iter, sym_flags, event);
3495
3496         trace_seq_printf(s, "Unknown type %d\n", entry->type);
3497
3498         return trace_handle_return(s);
3499 }
3500
3501 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
3502 {
3503         struct trace_array *tr = iter->tr;
3504         struct trace_seq *s = &iter->seq;
3505         struct trace_entry *entry;
3506         struct trace_event *event;
3507
3508         entry = iter->ent;
3509
3510         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
3511                 trace_seq_printf(s, "%d %d %llu ",
3512                                  entry->pid, iter->cpu, iter->ts);
3513
3514         if (trace_seq_has_overflowed(s))
3515                 return TRACE_TYPE_PARTIAL_LINE;
3516
3517         event = ftrace_find_event(entry->type);
3518         if (event)
3519                 return event->funcs->raw(iter, 0, event);
3520
3521         trace_seq_printf(s, "%d ?\n", entry->type);
3522
3523         return trace_handle_return(s);
3524 }
3525
3526 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
3527 {
3528         struct trace_array *tr = iter->tr;
3529         struct trace_seq *s = &iter->seq;
3530         unsigned char newline = '\n';
3531         struct trace_entry *entry;
3532         struct trace_event *event;
3533
3534         entry = iter->ent;
3535
3536         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3537                 SEQ_PUT_HEX_FIELD(s, entry->pid);
3538                 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3539                 SEQ_PUT_HEX_FIELD(s, iter->ts);
3540                 if (trace_seq_has_overflowed(s))
3541                         return TRACE_TYPE_PARTIAL_LINE;
3542         }
3543
3544         event = ftrace_find_event(entry->type);
3545         if (event) {
3546                 enum print_line_t ret = event->funcs->hex(iter, 0, event);
3547                 if (ret != TRACE_TYPE_HANDLED)
3548                         return ret;
3549         }
3550
3551         SEQ_PUT_FIELD(s, newline);
3552
3553         return trace_handle_return(s);
3554 }
3555
3556 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
3557 {
3558         struct trace_array *tr = iter->tr;
3559         struct trace_seq *s = &iter->seq;
3560         struct trace_entry *entry;
3561         struct trace_event *event;
3562
3563         entry = iter->ent;
3564
3565         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3566                 SEQ_PUT_FIELD(s, entry->pid);
3567                 SEQ_PUT_FIELD(s, iter->cpu);
3568                 SEQ_PUT_FIELD(s, iter->ts);
3569                 if (trace_seq_has_overflowed(s))
3570                         return TRACE_TYPE_PARTIAL_LINE;
3571         }
3572
3573         event = ftrace_find_event(entry->type);
3574         return event ? event->funcs->binary(iter, 0, event) :
3575                 TRACE_TYPE_HANDLED;
3576 }
3577
3578 int trace_empty(struct trace_iterator *iter)
3579 {
3580         struct ring_buffer_iter *buf_iter;
3581         int cpu;
3582
3583         /* If we are looking at one CPU buffer, only check that one */
3584         if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
3585                 cpu = iter->cpu_file;
3586                 buf_iter = trace_buffer_iter(iter, cpu);
3587                 if (buf_iter) {
3588                         if (!ring_buffer_iter_empty(buf_iter))
3589                                 return 0;
3590                 } else {
3591                         if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
3592                                 return 0;
3593                 }
3594                 return 1;
3595         }
3596
3597         for_each_tracing_cpu(cpu) {
3598                 buf_iter = trace_buffer_iter(iter, cpu);
3599                 if (buf_iter) {
3600                         if (!ring_buffer_iter_empty(buf_iter))
3601                                 return 0;
3602                 } else {
3603                         if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
3604                                 return 0;
3605                 }
3606         }
3607
3608         return 1;
3609 }
3610
3611 /*  Called with trace_event_read_lock() held. */
3612 enum print_line_t print_trace_line(struct trace_iterator *iter)
3613 {
3614         struct trace_array *tr = iter->tr;
3615         unsigned long trace_flags = tr->trace_flags;
3616         enum print_line_t ret;
3617
3618         if (iter->lost_events) {
3619                 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3620                                  iter->cpu, iter->lost_events);
3621                 if (trace_seq_has_overflowed(&iter->seq))
3622                         return TRACE_TYPE_PARTIAL_LINE;
3623         }
3624
3625         if (iter->trace && iter->trace->print_line) {
3626                 ret = iter->trace->print_line(iter);
3627                 if (ret != TRACE_TYPE_UNHANDLED)
3628                         return ret;
3629         }
3630
3631         if (iter->ent->type == TRACE_BPUTS &&
3632                         trace_flags & TRACE_ITER_PRINTK &&
3633                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3634                 return trace_print_bputs_msg_only(iter);
3635
3636         if (iter->ent->type == TRACE_BPRINT &&
3637                         trace_flags & TRACE_ITER_PRINTK &&
3638                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3639                 return trace_print_bprintk_msg_only(iter);
3640
3641         if (iter->ent->type == TRACE_PRINT &&
3642                         trace_flags & TRACE_ITER_PRINTK &&
3643                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3644                 return trace_print_printk_msg_only(iter);
3645