Branch data Line data Source code
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/pagemap.h>
24 : : #include <linux/hardirq.h>
25 : : #include <linux/linkage.h>
26 : : #include <linux/uaccess.h>
27 : : #include <linux/kprobes.h>
28 : : #include <linux/ftrace.h>
29 : : #include <linux/module.h>
30 : : #include <linux/percpu.h>
31 : : #include <linux/splice.h>
32 : : #include <linux/kdebug.h>
33 : : #include <linux/string.h>
34 : : #include <linux/rwsem.h>
35 : : #include <linux/slab.h>
36 : : #include <linux/ctype.h>
37 : : #include <linux/init.h>
38 : : #include <linux/poll.h>
39 : : #include <linux/nmi.h>
40 : : #include <linux/fs.h>
41 : : #include <linux/sched/rt.h>
42 : :
43 : : #include "trace.h"
44 : : #include "trace_output.h"
45 : :
46 : : /*
47 : : * On boot up, the ring buffer is set to the minimum size, so that
48 : : * we do not waste memory on systems that are not using tracing.
49 : : */
50 : : bool ring_buffer_expanded;
51 : :
52 : : /*
53 : : * We need to change this state when a selftest is running.
54 : : * A selftest will lurk into the ring-buffer to count the
55 : : * entries inserted during the selftest although some concurrent
56 : : * insertions into the ring-buffer such as trace_printk could occurred
57 : : * at the same time, giving false positive or negative results.
58 : : */
59 : : static bool __read_mostly tracing_selftest_running;
60 : :
61 : : /*
62 : : * If a tracer is running, we do not want to run SELFTEST.
63 : : */
64 : : bool __read_mostly tracing_selftest_disabled;
65 : :
66 : : /* For tracers that don't implement custom flags */
67 : : static struct tracer_opt dummy_tracer_opt[] = {
68 : : { }
69 : : };
70 : :
71 : : static struct tracer_flags dummy_tracer_flags = {
72 : : .val = 0,
73 : : .opts = dummy_tracer_opt
74 : : };
75 : :
76 : 0 : static int dummy_set_flag(u32 old_flags, u32 bit, int set)
77 : : {
78 : 0 : return 0;
79 : : }
80 : :
81 : : /*
82 : : * To prevent the comm cache from being overwritten when no
83 : : * tracing is active, only save the comm when a trace event
84 : : * occurred.
85 : : */
86 : : static DEFINE_PER_CPU(bool, trace_cmdline_save);
87 : :
88 : : /*
89 : : * Kill all tracing for good (never come back).
90 : : * It is initialized to 1 but will turn to zero if the initialization
91 : : * of the tracer is successful. But that is the only place that sets
92 : : * this back to zero.
93 : : */
94 : : static int tracing_disabled = 1;
95 : :
96 : : DEFINE_PER_CPU(int, ftrace_cpu_disabled);
97 : :
98 : : cpumask_var_t __read_mostly tracing_buffer_mask;
99 : :
100 : : /*
101 : : * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
102 : : *
103 : : * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
104 : : * is set, then ftrace_dump is called. This will output the contents
105 : : * of the ftrace buffers to the console. This is very useful for
106 : : * capturing traces that lead to crashes and outputing it to a
107 : : * serial console.
108 : : *
109 : : * It is default off, but you can enable it with either specifying
110 : : * "ftrace_dump_on_oops" in the kernel command line, or setting
111 : : * /proc/sys/kernel/ftrace_dump_on_oops
112 : : * Set 1 if you want to dump buffers of all CPUs
113 : : * Set 2 if you want to dump the buffer of the CPU that triggered oops
114 : : */
115 : :
116 : : enum ftrace_dump_mode ftrace_dump_on_oops;
117 : :
118 : : /* When set, tracing will stop when a WARN*() is hit */
119 : : int __disable_trace_on_warning;
120 : :
121 : : static int tracing_set_tracer(const char *buf);
122 : :
123 : : #define MAX_TRACER_SIZE 100
124 : : static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
125 : : static char *default_bootup_tracer;
126 : :
127 : : static bool allocate_snapshot;
128 : :
129 : 0 : static int __init set_cmdline_ftrace(char *str)
130 : : {
131 : 0 : strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
132 : 0 : default_bootup_tracer = bootup_tracer_buf;
133 : : /* We are using ftrace early, expand it */
134 : 0 : ring_buffer_expanded = true;
135 : 0 : return 1;
136 : : }
137 : : __setup("ftrace=", set_cmdline_ftrace);
138 : :
139 : 0 : static int __init set_ftrace_dump_on_oops(char *str)
140 : : {
141 [ # # ][ # # ]: 0 : if (*str++ != '=' || !*str) {
142 : 0 : ftrace_dump_on_oops = DUMP_ALL;
143 : 0 : return 1;
144 : : }
145 : :
146 [ # # ]: 0 : if (!strcmp("orig_cpu", str)) {
147 : 0 : ftrace_dump_on_oops = DUMP_ORIG;
148 : 0 : return 1;
149 : : }
150 : :
151 : : return 0;
152 : : }
153 : : __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
154 : :
155 : 0 : static int __init stop_trace_on_warning(char *str)
156 : : {
157 : 0 : __disable_trace_on_warning = 1;
158 : 0 : return 1;
159 : : }
160 : : __setup("traceoff_on_warning=", stop_trace_on_warning);
161 : :
162 : 0 : static int __init boot_alloc_snapshot(char *str)
163 : : {
164 : 0 : allocate_snapshot = true;
165 : : /* We also need the main ring buffer expanded */
166 : 0 : ring_buffer_expanded = true;
167 : 0 : return 1;
168 : : }
169 : : __setup("alloc_snapshot", boot_alloc_snapshot);
170 : :
171 : :
172 : : static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
173 : : static char *trace_boot_options __initdata;
174 : :
175 : 0 : static int __init set_trace_boot_options(char *str)
176 : : {
177 : 0 : strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
178 : 0 : trace_boot_options = trace_boot_options_buf;
179 : 0 : return 0;
180 : : }
181 : : __setup("trace_options=", set_trace_boot_options);
182 : :
183 : :
184 : 0 : unsigned long long ns2usecs(cycle_t nsec)
185 : : {
186 : 0 : nsec += 500;
187 : 0 : do_div(nsec, 1000);
188 : 0 : return nsec;
189 : : }
190 : :
191 : : /*
192 : : * The global_trace is the descriptor that holds the tracing
193 : : * buffers for the live tracing. For each CPU, it contains
194 : : * a link list of pages that will store trace entries. The
195 : : * page descriptor of the pages in the memory is used to hold
196 : : * the link list by linking the lru item in the page descriptor
197 : : * to each of the pages in the buffer per CPU.
198 : : *
199 : : * For each active CPU there is a data field that holds the
200 : : * pages for the buffer for that CPU. Each CPU has the same number
201 : : * of pages allocated for its buffer.
202 : : */
203 : : static struct trace_array global_trace;
204 : :
205 : : LIST_HEAD(ftrace_trace_arrays);
206 : :
207 : 0 : int trace_array_get(struct trace_array *this_tr)
208 : : {
209 : : struct trace_array *tr;
210 : : int ret = -ENODEV;
211 : :
212 : 0 : mutex_lock(&trace_types_lock);
213 [ # # ]: 0 : list_for_each_entry(tr, &ftrace_trace_arrays, list) {
214 [ # # ]: 0 : if (tr == this_tr) {
215 : 0 : tr->ref++;
216 : : ret = 0;
217 : 0 : break;
218 : : }
219 : : }
220 : 0 : mutex_unlock(&trace_types_lock);
221 : :
222 : 0 : return ret;
223 : : }
224 : :
225 : 0 : static void __trace_array_put(struct trace_array *this_tr)
226 : : {
227 [ # # ]: 0 : WARN_ON(!this_tr->ref);
228 : 0 : this_tr->ref--;
229 : 0 : }
230 : :
231 : 0 : void trace_array_put(struct trace_array *this_tr)
232 : : {
233 : 0 : mutex_lock(&trace_types_lock);
234 : 0 : __trace_array_put(this_tr);
235 : 0 : mutex_unlock(&trace_types_lock);
236 : 0 : }
237 : :
238 : 0 : int filter_check_discard(struct ftrace_event_file *file, void *rec,
239 : : struct ring_buffer *buffer,
240 : : struct ring_buffer_event *event)
241 : : {
242 [ # # # # ]: 0 : if (unlikely(file->flags & FTRACE_EVENT_FL_FILTERED) &&
243 : 0 : !filter_match_preds(file->filter, rec)) {
244 : 0 : ring_buffer_discard_commit(buffer, event);
245 : 0 : return 1;
246 : : }
247 : :
248 : : return 0;
249 : : }
250 : : EXPORT_SYMBOL_GPL(filter_check_discard);
251 : :
252 : 0 : int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
253 : : struct ring_buffer *buffer,
254 : : struct ring_buffer_event *event)
255 : : {
256 [ # # # # ]: 0 : if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
257 : 0 : !filter_match_preds(call->filter, rec)) {
258 : 0 : ring_buffer_discard_commit(buffer, event);
259 : 0 : return 1;
260 : : }
261 : :
262 : : return 0;
263 : : }
264 : : EXPORT_SYMBOL_GPL(call_filter_check_discard);
265 : :
266 : 0 : cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
267 : : {
268 : : u64 ts;
269 : :
270 : : /* Early boot up does not have a buffer yet */
271 [ # # ]: 0 : if (!buf->buffer)
272 : 0 : return trace_clock_local();
273 : :
274 : 0 : ts = ring_buffer_time_stamp(buf->buffer, cpu);
275 : 0 : ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
276 : :
277 : 0 : return ts;
278 : : }
279 : :
280 : 0 : cycle_t ftrace_now(int cpu)
281 : : {
282 : 0 : return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
283 : : }
284 : :
285 : : /**
286 : : * tracing_is_enabled - Show if global_trace has been disabled
287 : : *
288 : : * Shows if the global trace has been enabled or not. It uses the
289 : : * mirror flag "buffer_disabled" to be used in fast paths such as for
290 : : * the irqsoff tracer. But it may be inaccurate due to races. If you
291 : : * need to know the accurate state, use tracing_is_on() which is a little
292 : : * slower, but accurate.
293 : : */
294 : 0 : int tracing_is_enabled(void)
295 : : {
296 : : /*
297 : : * For quick access (irqsoff uses this in fast path), just
298 : : * return the mirror variable of the state of the ring buffer.
299 : : * It's a little racy, but we don't really care.
300 : : */
301 : 0 : smp_rmb();
302 : 0 : return !global_trace.buffer_disabled;
303 : : }
304 : :
305 : : /*
306 : : * trace_buf_size is the size in bytes that is allocated
307 : : * for a buffer. Note, the number of bytes is always rounded
308 : : * to page size.
309 : : *
310 : : * This number is purposely set to a low number of 16384.
311 : : * If the dump on oops happens, it will be much appreciated
312 : : * to not have to wait for all that output. Anyway this can be
313 : : * boot time and run time configurable.
314 : : */
315 : : #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
316 : :
317 : : static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
318 : :
319 : : /* trace_types holds a link list of available tracers. */
320 : : static struct tracer *trace_types __read_mostly;
321 : :
322 : : /*
323 : : * trace_types_lock is used to protect the trace_types list.
324 : : */
325 : : DEFINE_MUTEX(trace_types_lock);
326 : :
327 : : /*
328 : : * serialize the access of the ring buffer
329 : : *
330 : : * ring buffer serializes readers, but it is low level protection.
331 : : * The validity of the events (which returns by ring_buffer_peek() ..etc)
332 : : * are not protected by ring buffer.
333 : : *
334 : : * The content of events may become garbage if we allow other process consumes
335 : : * these events concurrently:
336 : : * A) the page of the consumed events may become a normal page
337 : : * (not reader page) in ring buffer, and this page will be rewrited
338 : : * by events producer.
339 : : * B) The page of the consumed events may become a page for splice_read,
340 : : * and this page will be returned to system.
341 : : *
342 : : * These primitives allow multi process access to different cpu ring buffer
343 : : * concurrently.
344 : : *
345 : : * These primitives don't distinguish read-only and read-consume access.
346 : : * Multi read-only access are also serialized.
347 : : */
348 : :
349 : : #ifdef CONFIG_SMP
350 : : static DECLARE_RWSEM(all_cpu_access_lock);
351 : : static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
352 : :
353 : : static inline void trace_access_lock(int cpu)
354 : : {
355 [ # # ][ # # : 0 : if (cpu == RING_BUFFER_ALL_CPUS) {
# # # # #
# ]
356 : : /* gain it for accessing the whole ring buffer. */
357 : 0 : down_write(&all_cpu_access_lock);
358 : : } else {
359 : : /* gain it for accessing a cpu ring buffer. */
360 : :
361 : : /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
362 : 0 : down_read(&all_cpu_access_lock);
363 : :
364 : : /* Secondly block other access to this @cpu ring buffer. */
365 : 0 : mutex_lock(&per_cpu(cpu_access_lock, cpu));
366 : : }
367 : : }
368 : :
369 : : static inline void trace_access_unlock(int cpu)
370 : : {
371 [ # # # # ]: 0 : if (cpu == RING_BUFFER_ALL_CPUS) {
[ # # ][ # # ]
[ # # ]
372 : 0 : up_write(&all_cpu_access_lock);
373 : : } else {
374 : 0 : mutex_unlock(&per_cpu(cpu_access_lock, cpu));
375 : 0 : up_read(&all_cpu_access_lock);
376 : : }
377 : : }
378 : :
379 : : static inline void trace_access_lock_init(void)
380 : : {
381 : : int cpu;
382 : :
383 [ # # ]: 0 : for_each_possible_cpu(cpu)
384 : 0 : mutex_init(&per_cpu(cpu_access_lock, cpu));
385 : : }
386 : :
387 : : #else
388 : :
389 : : static DEFINE_MUTEX(access_lock);
390 : :
391 : : static inline void trace_access_lock(int cpu)
392 : : {
393 : : (void)cpu;
394 : : mutex_lock(&access_lock);
395 : : }
396 : :
397 : : static inline void trace_access_unlock(int cpu)
398 : : {
399 : : (void)cpu;
400 : : mutex_unlock(&access_lock);
401 : : }
402 : :
403 : : static inline void trace_access_lock_init(void)
404 : : {
405 : : }
406 : :
407 : : #endif
408 : :
409 : : /* trace_flags holds trace_options default values */
410 : : unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
411 : : TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
412 : : TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
413 : : TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
414 : :
415 : : static void tracer_tracing_on(struct trace_array *tr)
416 : : {
417 [ # # ][ # # ]: 0 : if (tr->trace_buffer.buffer)
418 : 0 : ring_buffer_record_on(tr->trace_buffer.buffer);
419 : : /*
420 : : * This flag is looked at when buffers haven't been allocated
421 : : * yet, or by some tracers (like irqsoff), that just want to
422 : : * know if the ring buffer has been disabled, but it can handle
423 : : * races of where it gets disabled but we still do a record.
424 : : * As the check is in the fast path of the tracers, it is more
425 : : * important to be fast than accurate.
426 : : */
427 : 0 : tr->buffer_disabled = 0;
428 : : /* Make the flag seen by readers */
429 : 0 : smp_wmb();
430 : : }
431 : :
432 : : /**
433 : : * tracing_on - enable tracing buffers
434 : : *
435 : : * This function enables tracing buffers that may have been
436 : : * disabled with tracing_off.
437 : : */
438 : 0 : void tracing_on(void)
439 : : {
440 : 0 : tracer_tracing_on(&global_trace);
441 : 0 : }
442 : : EXPORT_SYMBOL_GPL(tracing_on);
443 : :
444 : : /**
445 : : * __trace_puts - write a constant string into the trace buffer.
446 : : * @ip: The address of the caller
447 : : * @str: The constant string to write
448 : : * @size: The size of the string.
449 : : */
450 : 0 : int __trace_puts(unsigned long ip, const char *str, int size)
451 : : {
452 : : struct ring_buffer_event *event;
453 : : struct ring_buffer *buffer;
454 : : struct print_entry *entry;
455 : : unsigned long irq_flags;
456 : : int alloc;
457 : :
458 [ # # ][ # # ]: 0 : if (unlikely(tracing_selftest_running || tracing_disabled))
459 : : return 0;
460 : :
461 : 0 : alloc = sizeof(*entry) + size + 2; /* possible \n added */
462 : :
463 : : local_save_flags(irq_flags);
464 : 0 : buffer = global_trace.trace_buffer.buffer;
465 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
466 : : irq_flags, preempt_count());
467 [ # # ]: 0 : if (!event)
468 : : return 0;
469 : :
470 : 0 : entry = ring_buffer_event_data(event);
471 : 0 : entry->ip = ip;
472 : :
473 : 0 : memcpy(&entry->buf, str, size);
474 : :
475 : : /* Add a newline if necessary */
476 [ # # ]: 0 : if (entry->buf[size - 1] != '\n') {
477 : 0 : entry->buf[size] = '\n';
478 : 0 : entry->buf[size + 1] = '\0';
479 : : } else
480 : 0 : entry->buf[size] = '\0';
481 : :
482 : : __buffer_unlock_commit(buffer, event);
483 : :
484 : 0 : return size;
485 : : }
486 : : EXPORT_SYMBOL_GPL(__trace_puts);
487 : :
488 : : /**
489 : : * __trace_bputs - write the pointer to a constant string into trace buffer
490 : : * @ip: The address of the caller
491 : : * @str: The constant string to write to the buffer to
492 : : */
493 : 0 : int __trace_bputs(unsigned long ip, const char *str)
494 : : {
495 : : struct ring_buffer_event *event;
496 : : struct ring_buffer *buffer;
497 : : struct bputs_entry *entry;
498 : : unsigned long irq_flags;
499 : : int size = sizeof(struct bputs_entry);
500 : :
501 [ # # ][ # # ]: 0 : if (unlikely(tracing_selftest_running || tracing_disabled))
502 : : return 0;
503 : :
504 : : local_save_flags(irq_flags);
505 : 0 : buffer = global_trace.trace_buffer.buffer;
506 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
507 : : irq_flags, preempt_count());
508 [ # # ]: 0 : if (!event)
509 : : return 0;
510 : :
511 : 0 : entry = ring_buffer_event_data(event);
512 : 0 : entry->ip = ip;
513 : 0 : entry->str = str;
514 : :
515 : : __buffer_unlock_commit(buffer, event);
516 : :
517 : 0 : return 1;
518 : : }
519 : : EXPORT_SYMBOL_GPL(__trace_bputs);
520 : :
521 : : #ifdef CONFIG_TRACER_SNAPSHOT
522 : : /**
523 : : * trace_snapshot - take a snapshot of the current buffer.
524 : : *
525 : : * This causes a swap between the snapshot buffer and the current live
526 : : * tracing buffer. You can use this to take snapshots of the live
527 : : * trace when some condition is triggered, but continue to trace.
528 : : *
529 : : * Note, make sure to allocate the snapshot with either
530 : : * a tracing_snapshot_alloc(), or by doing it manually
531 : : * with: echo 1 > /sys/kernel/debug/tracing/snapshot
532 : : *
533 : : * If the snapshot buffer is not allocated, it will stop tracing.
534 : : * Basically making a permanent snapshot.
535 : : */
536 : : void tracing_snapshot(void)
537 : : {
538 : : struct trace_array *tr = &global_trace;
539 : : struct tracer *tracer = tr->current_trace;
540 : : unsigned long flags;
541 : :
542 : : if (in_nmi()) {
543 : : internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
544 : : internal_trace_puts("*** snapshot is being ignored ***\n");
545 : : return;
546 : : }
547 : :
548 : : if (!tr->allocated_snapshot) {
549 : : internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
550 : : internal_trace_puts("*** stopping trace here! ***\n");
551 : : tracing_off();
552 : : return;
553 : : }
554 : :
555 : : /* Note, snapshot can not be used when the tracer uses it */
556 : : if (tracer->use_max_tr) {
557 : : internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
558 : : internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
559 : : return;
560 : : }
561 : :
562 : : local_irq_save(flags);
563 : : update_max_tr(tr, current, smp_processor_id());
564 : : local_irq_restore(flags);
565 : : }
566 : : EXPORT_SYMBOL_GPL(tracing_snapshot);
567 : :
568 : : static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
569 : : struct trace_buffer *size_buf, int cpu_id);
570 : : static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
571 : :
572 : : static int alloc_snapshot(struct trace_array *tr)
573 : : {
574 : : int ret;
575 : :
576 : : if (!tr->allocated_snapshot) {
577 : :
578 : : /* allocate spare buffer */
579 : : ret = resize_buffer_duplicate_size(&tr->max_buffer,
580 : : &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
581 : : if (ret < 0)
582 : : return ret;
583 : :
584 : : tr->allocated_snapshot = true;
585 : : }
586 : :
587 : : return 0;
588 : : }
589 : :
590 : : void free_snapshot(struct trace_array *tr)
591 : : {
592 : : /*
593 : : * We don't free the ring buffer. instead, resize it because
594 : : * The max_tr ring buffer has some state (e.g. ring->clock) and
595 : : * we want preserve it.
596 : : */
597 : : ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
598 : : set_buffer_entries(&tr->max_buffer, 1);
599 : : tracing_reset_online_cpus(&tr->max_buffer);
600 : : tr->allocated_snapshot = false;
601 : : }
602 : :
603 : : /**
604 : : * tracing_alloc_snapshot - allocate snapshot buffer.
605 : : *
606 : : * This only allocates the snapshot buffer if it isn't already
607 : : * allocated - it doesn't also take a snapshot.
608 : : *
609 : : * This is meant to be used in cases where the snapshot buffer needs
610 : : * to be set up for events that can't sleep but need to be able to
611 : : * trigger a snapshot.
612 : : */
613 : : int tracing_alloc_snapshot(void)
614 : : {
615 : : struct trace_array *tr = &global_trace;
616 : : int ret;
617 : :
618 : : ret = alloc_snapshot(tr);
619 : : WARN_ON(ret < 0);
620 : :
621 : : return ret;
622 : : }
623 : : EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
624 : :
625 : : /**
626 : : * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
627 : : *
628 : : * This is similar to trace_snapshot(), but it will allocate the
629 : : * snapshot buffer if it isn't already allocated. Use this only
630 : : * where it is safe to sleep, as the allocation may sleep.
631 : : *
632 : : * This causes a swap between the snapshot buffer and the current live
633 : : * tracing buffer. You can use this to take snapshots of the live
634 : : * trace when some condition is triggered, but continue to trace.
635 : : */
636 : : void tracing_snapshot_alloc(void)
637 : : {
638 : : int ret;
639 : :
640 : : ret = tracing_alloc_snapshot();
641 : : if (ret < 0)
642 : : return;
643 : :
644 : : tracing_snapshot();
645 : : }
646 : : EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
647 : : #else
648 : 0 : void tracing_snapshot(void)
649 : : {
650 [ # # ][ # # ]: 0 : WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
651 : 0 : }
652 : : EXPORT_SYMBOL_GPL(tracing_snapshot);
653 : 0 : int tracing_alloc_snapshot(void)
654 : : {
655 [ # # ][ # # ]: 0 : WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
656 : 0 : return -ENODEV;
657 : : }
658 : : EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
659 : 0 : void tracing_snapshot_alloc(void)
660 : : {
661 : : /* Give warning */
662 : 0 : tracing_snapshot();
663 : 0 : }
664 : : EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
665 : : #endif /* CONFIG_TRACER_SNAPSHOT */
666 : :
667 : : static void tracer_tracing_off(struct trace_array *tr)
668 : : {
669 [ # # ][ # # ]: 0 : if (tr->trace_buffer.buffer)
[ # # ]
670 : 0 : ring_buffer_record_off(tr->trace_buffer.buffer);
671 : : /*
672 : : * This flag is looked at when buffers haven't been allocated
673 : : * yet, or by some tracers (like irqsoff), that just want to
674 : : * know if the ring buffer has been disabled, but it can handle
675 : : * races of where it gets disabled but we still do a record.
676 : : * As the check is in the fast path of the tracers, it is more
677 : : * important to be fast than accurate.
678 : : */
679 : 0 : tr->buffer_disabled = 1;
680 : : /* Make the flag seen by readers */
681 : 0 : smp_wmb();
682 : : }
683 : :
684 : : /**
685 : : * tracing_off - turn off tracing buffers
686 : : *
687 : : * This function stops the tracing buffers from recording data.
688 : : * It does not disable any overhead the tracers themselves may
689 : : * be causing. This function simply causes all recording to
690 : : * the ring buffers to fail.
691 : : */
692 : 0 : void tracing_off(void)
693 : : {
694 : 0 : tracer_tracing_off(&global_trace);
695 : 0 : }
696 : : EXPORT_SYMBOL_GPL(tracing_off);
697 : :
698 : 0 : void disable_trace_on_warning(void)
699 : : {
700 [ # # ]: 0 : if (__disable_trace_on_warning)
701 : 0 : tracing_off();
702 : 0 : }
703 : :
704 : : /**
705 : : * tracer_tracing_is_on - show real state of ring buffer enabled
706 : : * @tr : the trace array to know if ring buffer is enabled
707 : : *
708 : : * Shows real state of the ring buffer if it is enabled or not.
709 : : */
710 : : static int tracer_tracing_is_on(struct trace_array *tr)
711 : : {
712 [ # # ][ # # ]: 0 : if (tr->trace_buffer.buffer)
713 : 0 : return ring_buffer_record_is_on(tr->trace_buffer.buffer);
714 : 0 : return !tr->buffer_disabled;
715 : : }
716 : :
717 : : /**
718 : : * tracing_is_on - show state of ring buffers enabled
719 : : */
720 : 0 : int tracing_is_on(void)
721 : : {
722 : 0 : return tracer_tracing_is_on(&global_trace);
723 : : }
724 : : EXPORT_SYMBOL_GPL(tracing_is_on);
725 : :
726 : 0 : static int __init set_buf_size(char *str)
727 : : {
728 : : unsigned long buf_size;
729 : :
730 [ # # ]: 0 : if (!str)
731 : : return 0;
732 : 0 : buf_size = memparse(str, &str);
733 : : /* nr_entries can not be zero */
734 [ # # ]: 0 : if (buf_size == 0)
735 : : return 0;
736 : 0 : trace_buf_size = buf_size;
737 : 0 : return 1;
738 : : }
739 : : __setup("trace_buf_size=", set_buf_size);
740 : :
741 : 0 : static int __init set_tracing_thresh(char *str)
742 : : {
743 : : unsigned long threshold;
744 : : int ret;
745 : :
746 [ # # ]: 0 : if (!str)
747 : : return 0;
748 : : ret = kstrtoul(str, 0, &threshold);
749 [ # # ]: 0 : if (ret < 0)
750 : : return 0;
751 : 0 : tracing_thresh = threshold * 1000;
752 : 0 : return 1;
753 : : }
754 : : __setup("tracing_thresh=", set_tracing_thresh);
755 : :
756 : 0 : unsigned long nsecs_to_usecs(unsigned long nsecs)
757 : : {
758 : 0 : return nsecs / 1000;
759 : : }
760 : :
761 : : /* These must match the bit postions in trace_iterator_flags */
762 : : static const char *trace_options[] = {
763 : : "print-parent",
764 : : "sym-offset",
765 : : "sym-addr",
766 : : "verbose",
767 : : "raw",
768 : : "hex",
769 : : "bin",
770 : : "block",
771 : : "stacktrace",
772 : : "trace_printk",
773 : : "ftrace_preempt",
774 : : "branch",
775 : : "annotate",
776 : : "userstacktrace",
777 : : "sym-userobj",
778 : : "printk-msg-only",
779 : : "context-info",
780 : : "latency-format",
781 : : "sleep-time",
782 : : "graph-time",
783 : : "record-cmd",
784 : : "overwrite",
785 : : "disable_on_free",
786 : : "irq-info",
787 : : "markers",
788 : : "function-trace",
789 : : "print-tgid",
790 : : NULL
791 : : };
792 : :
793 : : static struct {
794 : : u64 (*func)(void);
795 : : const char *name;
796 : : int in_ns; /* is this clock in nanoseconds? */
797 : : } trace_clocks[] = {
798 : : { trace_clock_local, "local", 1 },
799 : : { trace_clock_global, "global", 1 },
800 : : { trace_clock_counter, "counter", 0 },
801 : : { trace_clock_jiffies, "uptime", 1 },
802 : : { trace_clock, "perf", 1 },
803 : : ARCH_TRACE_CLOCKS
804 : : };
805 : :
806 : : /*
807 : : * trace_parser_get_init - gets the buffer for trace parser
808 : : */
809 : 0 : int trace_parser_get_init(struct trace_parser *parser, int size)
810 : : {
811 : 0 : memset(parser, 0, sizeof(*parser));
812 : :
813 : 0 : parser->buffer = kmalloc(size, GFP_KERNEL);
814 [ # # ]: 0 : if (!parser->buffer)
815 : : return 1;
816 : :
817 : 0 : parser->size = size;
818 : 0 : return 0;
819 : : }
820 : :
821 : : /*
822 : : * trace_parser_put - frees the buffer for trace parser
823 : : */
824 : 0 : void trace_parser_put(struct trace_parser *parser)
825 : : {
826 : 0 : kfree(parser->buffer);
827 : 0 : }
828 : :
829 : : /*
830 : : * trace_get_user - reads the user input string separated by space
831 : : * (matched by isspace(ch))
832 : : *
833 : : * For each string found the 'struct trace_parser' is updated,
834 : : * and the function returns.
835 : : *
836 : : * Returns number of bytes read.
837 : : *
838 : : * See kernel/trace/trace.h for 'struct trace_parser' details.
839 : : */
840 : 0 : int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
841 : : size_t cnt, loff_t *ppos)
842 : : {
843 : : char ch;
844 : : size_t read = 0;
845 : : ssize_t ret;
846 : :
847 [ # # ]: 0 : if (!*ppos)
848 : : trace_parser_clear(parser);
849 : :
850 : 0 : ret = get_user(ch, ubuf++);
851 [ # # ]: 0 : if (ret)
852 : : goto out;
853 : :
854 : : read++;
855 : 0 : cnt--;
856 : :
857 : : /*
858 : : * The parser is not finished with the last write,
859 : : * continue reading the user input without skipping spaces.
860 : : */
861 [ # # ]: 0 : if (!parser->cont) {
862 : : /* skip white space */
863 [ # # ][ # # ]: 0 : while (cnt && isspace(ch)) {
864 : 0 : ret = get_user(ch, ubuf++);
865 [ # # ]: 0 : if (ret)
866 : : goto out;
867 : 0 : read++;
868 : 0 : cnt--;
869 : : }
870 : :
871 : : /* only spaces were written */
872 [ # # ]: 0 : if (isspace(ch)) {
873 : 0 : *ppos += read;
874 : 0 : ret = read;
875 : 0 : goto out;
876 : : }
877 : :
878 : 0 : parser->idx = 0;
879 : : }
880 : :
881 : : /* read the non-space input */
882 [ # # ][ # # ]: 0 : while (cnt && !isspace(ch)) {
883 [ # # ]: 0 : if (parser->idx < parser->size - 1)
884 : 0 : parser->buffer[parser->idx++] = ch;
885 : : else {
886 : : ret = -EINVAL;
887 : : goto out;
888 : : }
889 : 0 : ret = get_user(ch, ubuf++);
890 [ # # ]: 0 : if (ret)
891 : : goto out;
892 : 0 : read++;
893 : 0 : cnt--;
894 : : }
895 : :
896 : : /* We either got finished input or we have to wait for another call. */
897 [ # # ]: 0 : if (isspace(ch)) {
898 : 0 : parser->buffer[parser->idx] = 0;
899 : 0 : parser->cont = false;
900 [ # # ]: 0 : } else if (parser->idx < parser->size - 1) {
901 : 0 : parser->cont = true;
902 : 0 : parser->buffer[parser->idx++] = ch;
903 : : } else {
904 : : ret = -EINVAL;
905 : : goto out;
906 : : }
907 : :
908 : 0 : *ppos += read;
909 : 0 : ret = read;
910 : :
911 : : out:
912 : 0 : return ret;
913 : : }
914 : :
915 : 0 : ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
916 : : {
917 : : int len;
918 : : int ret;
919 : :
920 [ # # ]: 0 : if (!cnt)
921 : : return 0;
922 : :
923 [ # # ]: 0 : if (s->len <= s->readpos)
924 : : return -EBUSY;
925 : :
926 : 0 : len = s->len - s->readpos;
927 [ # # ]: 0 : if (cnt > len)
928 : : cnt = len;
929 : 0 : ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
930 [ # # ]: 0 : if (ret == cnt)
931 : : return -EFAULT;
932 : :
933 : 0 : cnt -= ret;
934 : :
935 : 0 : s->readpos += cnt;
936 : 0 : return cnt;
937 : : }
938 : :
939 : 0 : static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
940 : : {
941 : : int len;
942 : :
943 [ # # ]: 0 : if (s->len <= s->readpos)
944 : : return -EBUSY;
945 : :
946 : 0 : len = s->len - s->readpos;
947 [ # # ]: 0 : if (cnt > len)
948 : : cnt = len;
949 : 0 : memcpy(buf, s->buffer + s->readpos, cnt);
950 : :
951 : 0 : s->readpos += cnt;
952 : 0 : return cnt;
953 : : }
954 : :
955 : : /*
956 : : * ftrace_max_lock is used to protect the swapping of buffers
957 : : * when taking a max snapshot. The buffers themselves are
958 : : * protected by per_cpu spinlocks. But the action of the swap
959 : : * needs its own lock.
960 : : *
961 : : * This is defined as a arch_spinlock_t in order to help
962 : : * with performance when lockdep debugging is enabled.
963 : : *
964 : : * It is also used in other places outside the update_max_tr
965 : : * so it needs to be defined outside of the
966 : : * CONFIG_TRACER_MAX_TRACE.
967 : : */
968 : : static arch_spinlock_t ftrace_max_lock =
969 : : (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
970 : :
971 : : unsigned long __read_mostly tracing_thresh;
972 : :
973 : : #ifdef CONFIG_TRACER_MAX_TRACE
974 : : unsigned long __read_mostly tracing_max_latency;
975 : :
976 : : /*
977 : : * Copy the new maximum trace into the separate maximum-trace
978 : : * structure. (this way the maximum trace is permanently saved,
979 : : * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
980 : : */
981 : : static void
982 : : __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
983 : : {
984 : : struct trace_buffer *trace_buf = &tr->trace_buffer;
985 : : struct trace_buffer *max_buf = &tr->max_buffer;
986 : : struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
987 : : struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
988 : :
989 : : max_buf->cpu = cpu;
990 : : max_buf->time_start = data->preempt_timestamp;
991 : :
992 : : max_data->saved_latency = tracing_max_latency;
993 : : max_data->critical_start = data->critical_start;
994 : : max_data->critical_end = data->critical_end;
995 : :
996 : : memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
997 : : max_data->pid = tsk->pid;
998 : : /*
999 : : * If tsk == current, then use current_uid(), as that does not use
1000 : : * RCU. The irq tracer can be called out of RCU scope.
1001 : : */
1002 : : if (tsk == current)
1003 : : max_data->uid = current_uid();
1004 : : else
1005 : : max_data->uid = task_uid(tsk);
1006 : :
1007 : : max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1008 : : max_data->policy = tsk->policy;
1009 : : max_data->rt_priority = tsk->rt_priority;
1010 : :
1011 : : /* record this tasks comm */
1012 : : tracing_record_cmdline(tsk);
1013 : : }
1014 : :
1015 : : /**
1016 : : * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1017 : : * @tr: tracer
1018 : : * @tsk: the task with the latency
1019 : : * @cpu: The cpu that initiated the trace.
1020 : : *
1021 : : * Flip the buffers between the @tr and the max_tr and record information
1022 : : * about which task was the cause of this latency.
1023 : : */
1024 : : void
1025 : : update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1026 : : {
1027 : : struct ring_buffer *buf;
1028 : :
1029 : : if (tr->stop_count)
1030 : : return;
1031 : :
1032 : : WARN_ON_ONCE(!irqs_disabled());
1033 : :
1034 : : if (!tr->allocated_snapshot) {
1035 : : /* Only the nop tracer should hit this when disabling */
1036 : : WARN_ON_ONCE(tr->current_trace != &nop_trace);
1037 : : return;
1038 : : }
1039 : :
1040 : : arch_spin_lock(&ftrace_max_lock);
1041 : :
1042 : : buf = tr->trace_buffer.buffer;
1043 : : tr->trace_buffer.buffer = tr->max_buffer.buffer;
1044 : : tr->max_buffer.buffer = buf;
1045 : :
1046 : : __update_max_tr(tr, tsk, cpu);
1047 : : arch_spin_unlock(&ftrace_max_lock);
1048 : : }
1049 : :
1050 : : /**
1051 : : * update_max_tr_single - only copy one trace over, and reset the rest
1052 : : * @tr - tracer
1053 : : * @tsk - task with the latency
1054 : : * @cpu - the cpu of the buffer to copy.
1055 : : *
1056 : : * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1057 : : */
1058 : : void
1059 : : update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1060 : : {
1061 : : int ret;
1062 : :
1063 : : if (tr->stop_count)
1064 : : return;
1065 : :
1066 : : WARN_ON_ONCE(!irqs_disabled());
1067 : : if (!tr->allocated_snapshot) {
1068 : : /* Only the nop tracer should hit this when disabling */
1069 : : WARN_ON_ONCE(tr->current_trace != &nop_trace);
1070 : : return;
1071 : : }
1072 : :
1073 : : arch_spin_lock(&ftrace_max_lock);
1074 : :
1075 : : ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1076 : :
1077 : : if (ret == -EBUSY) {
1078 : : /*
1079 : : * We failed to swap the buffer due to a commit taking
1080 : : * place on this CPU. We fail to record, but we reset
1081 : : * the max trace buffer (no one writes directly to it)
1082 : : * and flag that it failed.
1083 : : */
1084 : : trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1085 : : "Failed to swap buffers due to commit in progress\n");
1086 : : }
1087 : :
1088 : : WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1089 : :
1090 : : __update_max_tr(tr, tsk, cpu);
1091 : : arch_spin_unlock(&ftrace_max_lock);
1092 : : }
1093 : : #endif /* CONFIG_TRACER_MAX_TRACE */
1094 : :
1095 : 0 : static void default_wait_pipe(struct trace_iterator *iter)
1096 : : {
1097 : : /* Iterators are static, they should be filled or empty */
1098 [ # # ]: 0 : if (trace_buffer_iter(iter, iter->cpu_file))
1099 : 0 : return;
1100 : :
1101 : 0 : ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
1102 : : }
1103 : :
1104 : : #ifdef CONFIG_FTRACE_STARTUP_TEST
1105 : : static int run_tracer_selftest(struct tracer *type)
1106 : : {
1107 : : struct trace_array *tr = &global_trace;
1108 : : struct tracer *saved_tracer = tr->current_trace;
1109 : : int ret;
1110 : :
1111 : : if (!type->selftest || tracing_selftest_disabled)
1112 : : return 0;
1113 : :
1114 : : /*
1115 : : * Run a selftest on this tracer.
1116 : : * Here we reset the trace buffer, and set the current
1117 : : * tracer to be this tracer. The tracer can then run some
1118 : : * internal tracing to verify that everything is in order.
1119 : : * If we fail, we do not register this tracer.
1120 : : */
1121 : : tracing_reset_online_cpus(&tr->trace_buffer);
1122 : :
1123 : : tr->current_trace = type;
1124 : :
1125 : : #ifdef CONFIG_TRACER_MAX_TRACE
1126 : : if (type->use_max_tr) {
1127 : : /* If we expanded the buffers, make sure the max is expanded too */
1128 : : if (ring_buffer_expanded)
1129 : : ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1130 : : RING_BUFFER_ALL_CPUS);
1131 : : tr->allocated_snapshot = true;
1132 : : }
1133 : : #endif
1134 : :
1135 : : /* the test is responsible for initializing and enabling */
1136 : : pr_info("Testing tracer %s: ", type->name);
1137 : : ret = type->selftest(type, tr);
1138 : : /* the test is responsible for resetting too */
1139 : : tr->current_trace = saved_tracer;
1140 : : if (ret) {
1141 : : printk(KERN_CONT "FAILED!\n");
1142 : : /* Add the warning after printing 'FAILED' */
1143 : : WARN_ON(1);
1144 : : return -1;
1145 : : }
1146 : : /* Only reset on passing, to avoid touching corrupted buffers */
1147 : : tracing_reset_online_cpus(&tr->trace_buffer);
1148 : :
1149 : : #ifdef CONFIG_TRACER_MAX_TRACE
1150 : : if (type->use_max_tr) {
1151 : : tr->allocated_snapshot = false;
1152 : :
1153 : : /* Shrink the max buffer again */
1154 : : if (ring_buffer_expanded)
1155 : : ring_buffer_resize(tr->max_buffer.buffer, 1,
1156 : : RING_BUFFER_ALL_CPUS);
1157 : : }
1158 : : #endif
1159 : :
1160 : : printk(KERN_CONT "PASSED\n");
1161 : : return 0;
1162 : : }
1163 : : #else
1164 : : static inline int run_tracer_selftest(struct tracer *type)
1165 : : {
1166 : : return 0;
1167 : : }
1168 : : #endif /* CONFIG_FTRACE_STARTUP_TEST */
1169 : :
1170 : : /**
1171 : : * register_tracer - register a tracer with the ftrace system.
1172 : : * @type - the plugin for the tracer
1173 : : *
1174 : : * Register a new plugin tracer.
1175 : : */
1176 : 0 : int register_tracer(struct tracer *type)
1177 : : {
1178 : : struct tracer *t;
1179 : : int ret = 0;
1180 : :
1181 [ # # ]: 0 : if (!type->name) {
1182 : 0 : pr_info("Tracer must have a name\n");
1183 : 0 : return -1;
1184 : : }
1185 : :
1186 [ # # ]: 0 : if (strlen(type->name) >= MAX_TRACER_SIZE) {
1187 : 0 : pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1188 : 0 : return -1;
1189 : : }
1190 : :
1191 : 0 : mutex_lock(&trace_types_lock);
1192 : :
1193 : 0 : tracing_selftest_running = true;
1194 : :
1195 [ # # ]: 0 : for (t = trace_types; t; t = t->next) {
1196 [ # # ]: 0 : if (strcmp(type->name, t->name) == 0) {
1197 : : /* already found */
1198 : 0 : pr_info("Tracer %s already registered\n",
1199 : : type->name);
1200 : : ret = -1;
1201 : 0 : goto out;
1202 : : }
1203 : : }
1204 : :
1205 [ # # ]: 0 : if (!type->set_flag)
1206 : 0 : type->set_flag = &dummy_set_flag;
1207 [ # # ]: 0 : if (!type->flags)
1208 : 0 : type->flags = &dummy_tracer_flags;
1209 : : else
1210 [ # # ]: 0 : if (!type->flags->opts)
1211 : 0 : type->flags->opts = dummy_tracer_opt;
1212 [ # # ]: 0 : if (!type->wait_pipe)
1213 : 0 : type->wait_pipe = default_wait_pipe;
1214 : :
1215 : : ret = run_tracer_selftest(type);
1216 : : if (ret < 0)
1217 : : goto out;
1218 : :
1219 : 0 : type->next = trace_types;
1220 : 0 : trace_types = type;
1221 : :
1222 : : out:
1223 : 0 : tracing_selftest_running = false;
1224 : 0 : mutex_unlock(&trace_types_lock);
1225 : :
1226 [ # # ][ # # ]: 0 : if (ret || !default_bootup_tracer)
1227 : : goto out_unlock;
1228 : :
1229 [ # # ]: 0 : if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1230 : : goto out_unlock;
1231 : :
1232 : 0 : printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1233 : : /* Do we want this tracer to start on bootup? */
1234 : 0 : tracing_set_tracer(type->name);
1235 : 0 : default_bootup_tracer = NULL;
1236 : : /* disable other selftests, since this will break it. */
1237 : 0 : tracing_selftest_disabled = true;
1238 : : #ifdef CONFIG_FTRACE_STARTUP_TEST
1239 : : printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1240 : : type->name);
1241 : : #endif
1242 : :
1243 : : out_unlock:
1244 : 0 : return ret;
1245 : : }
1246 : :
1247 : 0 : void tracing_reset(struct trace_buffer *buf, int cpu)
1248 : : {
1249 : 0 : struct ring_buffer *buffer = buf->buffer;
1250 : :
1251 [ # # ]: 0 : if (!buffer)
1252 : 0 : return;
1253 : :
1254 : 0 : ring_buffer_record_disable(buffer);
1255 : :
1256 : : /* Make sure all commits have finished */
1257 : 0 : synchronize_sched();
1258 : 0 : ring_buffer_reset_cpu(buffer, cpu);
1259 : :
1260 : 0 : ring_buffer_record_enable(buffer);
1261 : : }
1262 : :
1263 : 0 : void tracing_reset_online_cpus(struct trace_buffer *buf)
1264 : : {
1265 : 0 : struct ring_buffer *buffer = buf->buffer;
1266 : : int cpu;
1267 : :
1268 [ # # ]: 0 : if (!buffer)
1269 : 0 : return;
1270 : :
1271 : 0 : ring_buffer_record_disable(buffer);
1272 : :
1273 : : /* Make sure all commits have finished */
1274 : 0 : synchronize_sched();
1275 : :
1276 : 0 : buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1277 : :
1278 [ # # ]: 0 : for_each_online_cpu(cpu)
1279 : 0 : ring_buffer_reset_cpu(buffer, cpu);
1280 : :
1281 : 0 : ring_buffer_record_enable(buffer);
1282 : : }
1283 : :
1284 : : /* Must have trace_types_lock held */
1285 : 0 : void tracing_reset_all_online_cpus(void)
1286 : : {
1287 : : struct trace_array *tr;
1288 : :
1289 [ # # ]: 0 : list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1290 : 0 : tracing_reset_online_cpus(&tr->trace_buffer);
1291 : : #ifdef CONFIG_TRACER_MAX_TRACE
1292 : : tracing_reset_online_cpus(&tr->max_buffer);
1293 : : #endif
1294 : : }
1295 : 0 : }
1296 : :
1297 : : #define SAVED_CMDLINES 128
1298 : : #define NO_CMDLINE_MAP UINT_MAX
1299 : : static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1300 : : static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
1301 : : static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
1302 : : static unsigned saved_tgids[SAVED_CMDLINES];
1303 : : static int cmdline_idx;
1304 : : static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1305 : :
1306 : : /* temporary disable recording */
1307 : : static atomic_t trace_record_cmdline_disabled __read_mostly;
1308 : :
1309 : 0 : static void trace_init_cmdlines(void)
1310 : : {
1311 : 0 : memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
1312 : 0 : memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
1313 : 0 : cmdline_idx = 0;
1314 : 0 : }
1315 : :
1316 : 0 : int is_tracing_stopped(void)
1317 : : {
1318 : 0 : return global_trace.stop_count;
1319 : : }
1320 : :
1321 : : /**
1322 : : * tracing_start - quick start of the tracer
1323 : : *
1324 : : * If tracing is enabled but was stopped by tracing_stop,
1325 : : * this will start the tracer back up.
1326 : : */
1327 : 0 : void tracing_start(void)
1328 : : {
1329 : : struct ring_buffer *buffer;
1330 : : unsigned long flags;
1331 : :
1332 [ # # ]: 0 : if (tracing_disabled)
1333 : 0 : return;
1334 : :
1335 : 0 : raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1336 [ # # ]: 0 : if (--global_trace.stop_count) {
1337 [ # # ]: 0 : if (global_trace.stop_count < 0) {
1338 : : /* Someone screwed up their debugging */
1339 [ # # ][ # # ]: 0 : WARN_ON_ONCE(1);
1340 : 0 : global_trace.stop_count = 0;
1341 : : }
1342 : : goto out;
1343 : : }
1344 : :
1345 : : /* Prevent the buffers from switching */
1346 : : arch_spin_lock(&ftrace_max_lock);
1347 : :
1348 : 0 : buffer = global_trace.trace_buffer.buffer;
1349 [ # # ]: 0 : if (buffer)
1350 : 0 : ring_buffer_record_enable(buffer);
1351 : :
1352 : : #ifdef CONFIG_TRACER_MAX_TRACE
1353 : : buffer = global_trace.max_buffer.buffer;
1354 : : if (buffer)
1355 : : ring_buffer_record_enable(buffer);
1356 : : #endif
1357 : :
1358 : : arch_spin_unlock(&ftrace_max_lock);
1359 : :
1360 : : ftrace_start();
1361 : : out:
1362 : 0 : raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1363 : : }
1364 : :
1365 : 0 : static void tracing_start_tr(struct trace_array *tr)
1366 : : {
1367 : : struct ring_buffer *buffer;
1368 : : unsigned long flags;
1369 : :
1370 [ # # ]: 0 : if (tracing_disabled)
1371 : : return;
1372 : :
1373 : : /* If global, we need to also start the max tracer */
1374 [ # # ]: 0 : if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1375 : 0 : return tracing_start();
1376 : :
1377 : 0 : raw_spin_lock_irqsave(&tr->start_lock, flags);
1378 : :
1379 [ # # ]: 0 : if (--tr->stop_count) {
1380 [ # # ]: 0 : if (tr->stop_count < 0) {
1381 : : /* Someone screwed up their debugging */
1382 [ # # ][ # # ]: 0 : WARN_ON_ONCE(1);
1383 : 0 : tr->stop_count = 0;
1384 : : }
1385 : : goto out;
1386 : : }
1387 : :
1388 : 0 : buffer = tr->trace_buffer.buffer;
1389 [ # # ]: 0 : if (buffer)
1390 : 0 : ring_buffer_record_enable(buffer);
1391 : :
1392 : : out:
1393 : 0 : raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1394 : : }
1395 : :
1396 : : /**
1397 : : * tracing_stop - quick stop of the tracer
1398 : : *
1399 : : * Light weight way to stop tracing. Use in conjunction with
1400 : : * tracing_start.
1401 : : */
1402 : 0 : void tracing_stop(void)
1403 : : {
1404 : : struct ring_buffer *buffer;
1405 : : unsigned long flags;
1406 : :
1407 : : ftrace_stop();
1408 : 0 : raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1409 [ # # ]: 0 : if (global_trace.stop_count++)
1410 : : goto out;
1411 : :
1412 : : /* Prevent the buffers from switching */
1413 : : arch_spin_lock(&ftrace_max_lock);
1414 : :
1415 : 0 : buffer = global_trace.trace_buffer.buffer;
1416 [ # # ]: 0 : if (buffer)
1417 : 0 : ring_buffer_record_disable(buffer);
1418 : :
1419 : : #ifdef CONFIG_TRACER_MAX_TRACE
1420 : : buffer = global_trace.max_buffer.buffer;
1421 : : if (buffer)
1422 : : ring_buffer_record_disable(buffer);
1423 : : #endif
1424 : :
1425 : : arch_spin_unlock(&ftrace_max_lock);
1426 : :
1427 : : out:
1428 : 0 : raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1429 : 0 : }
1430 : :
1431 : 0 : static void tracing_stop_tr(struct trace_array *tr)
1432 : : {
1433 : : struct ring_buffer *buffer;
1434 : : unsigned long flags;
1435 : :
1436 : : /* If global, we need to also stop the max tracer */
1437 [ # # ]: 0 : if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1438 : 0 : return tracing_stop();
1439 : :
1440 : 0 : raw_spin_lock_irqsave(&tr->start_lock, flags);
1441 [ # # ]: 0 : if (tr->stop_count++)
1442 : : goto out;
1443 : :
1444 : 0 : buffer = tr->trace_buffer.buffer;
1445 [ # # ]: 0 : if (buffer)
1446 : 0 : ring_buffer_record_disable(buffer);
1447 : :
1448 : : out:
1449 : 0 : raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1450 : : }
1451 : :
1452 : : void trace_stop_cmdline_recording(void);
1453 : :
1454 : 0 : static void trace_save_cmdline(struct task_struct *tsk)
1455 : : {
1456 : : unsigned pid, idx;
1457 : :
1458 [ # # ][ # # ]: 0 : if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1459 : : return;
1460 : :
1461 : : /*
1462 : : * It's not the end of the world if we don't get
1463 : : * the lock, but we also don't want to spin
1464 : : * nor do we want to disable interrupts,
1465 : : * so if we miss here, then better luck next time.
1466 : : */
1467 [ # # ]: 0 : if (!arch_spin_trylock(&trace_cmdline_lock))
1468 : : return;
1469 : :
1470 : 0 : idx = map_pid_to_cmdline[tsk->pid];
1471 [ # # ]: 0 : if (idx == NO_CMDLINE_MAP) {
1472 : 0 : idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1473 : :
1474 : : /*
1475 : : * Check whether the cmdline buffer at idx has a pid
1476 : : * mapped. We are going to overwrite that entry so we
1477 : : * need to clear the map_pid_to_cmdline. Otherwise we
1478 : : * would read the new comm for the old pid.
1479 : : */
1480 : 0 : pid = map_cmdline_to_pid[idx];
1481 [ # # ]: 0 : if (pid != NO_CMDLINE_MAP)
1482 : 0 : map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1483 : :
1484 : 0 : map_cmdline_to_pid[idx] = tsk->pid;
1485 : 0 : map_pid_to_cmdline[tsk->pid] = idx;
1486 : :
1487 : 0 : cmdline_idx = idx;
1488 : : }
1489 : :
1490 : 0 : memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1491 : 0 : saved_tgids[idx] = tsk->tgid;
1492 : :
1493 : : arch_spin_unlock(&trace_cmdline_lock);
1494 : : }
1495 : :
1496 : 0 : void trace_find_cmdline(int pid, char comm[])
1497 : : {
1498 : : unsigned map;
1499 : :
1500 [ # # ]: 0 : if (!pid) {
1501 : 0 : strcpy(comm, "<idle>");
1502 : 0 : return;
1503 : : }
1504 : :
1505 [ # # ][ # # ]: 0 : if (WARN_ON_ONCE(pid < 0)) {
[ # # ][ # # ]
1506 : 0 : strcpy(comm, "<XXX>");
1507 : 0 : return;
1508 : : }
1509 : :
1510 [ # # ]: 0 : if (pid > PID_MAX_DEFAULT) {
1511 : 0 : strcpy(comm, "<...>");
1512 : 0 : return;
1513 : : }
1514 : :
1515 : 0 : preempt_disable();
1516 : : arch_spin_lock(&trace_cmdline_lock);
1517 : 0 : map = map_pid_to_cmdline[pid];
1518 [ # # ]: 0 : if (map != NO_CMDLINE_MAP)
1519 : 0 : strcpy(comm, saved_cmdlines[map]);
1520 : : else
1521 : 0 : strcpy(comm, "<...>");
1522 : :
1523 : : arch_spin_unlock(&trace_cmdline_lock);
1524 : 0 : preempt_enable();
1525 : : }
1526 : :
1527 : 0 : int trace_find_tgid(int pid)
1528 : : {
1529 : : unsigned map;
1530 : : int tgid;
1531 : :
1532 : 0 : preempt_disable();
1533 : : arch_spin_lock(&trace_cmdline_lock);
1534 : 0 : map = map_pid_to_cmdline[pid];
1535 [ # # ]: 0 : if (map != NO_CMDLINE_MAP)
1536 : 0 : tgid = saved_tgids[map];
1537 : : else
1538 : : tgid = -1;
1539 : :
1540 : : arch_spin_unlock(&trace_cmdline_lock);
1541 : 0 : preempt_enable();
1542 : :
1543 : 0 : return tgid;
1544 : : }
1545 : :
1546 : 0 : void tracing_record_cmdline(struct task_struct *tsk)
1547 : : {
1548 [ # # ][ # # ]: 0 : if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
1549 : : return;
1550 : :
1551 [ # # ]: 0 : if (!__this_cpu_read(trace_cmdline_save))
1552 : : return;
1553 : :
1554 : 0 : __this_cpu_write(trace_cmdline_save, false);
1555 : :
1556 : 0 : trace_save_cmdline(tsk);
1557 : : }
1558 : :
1559 : : void
1560 : 0 : tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1561 : : int pc)
1562 : : {
1563 : 0 : struct task_struct *tsk = current;
1564 : :
1565 : 0 : entry->preempt_count = pc & 0xff;
1566 [ # # ]: 0 : entry->pid = (tsk) ? tsk->pid : 0;
1567 [ # # ][ # # ]: 0 : entry->flags =
[ # # ]
1568 : : #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
1569 : 0 : (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
1570 : : #else
1571 : : TRACE_FLAG_IRQS_NOSUPPORT |
1572 : : #endif
1573 : 0 : ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1574 : 0 : ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1575 : : (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
1576 : : (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
1577 : 0 : }
1578 : : EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
1579 : :
1580 : : struct ring_buffer_event *
1581 : 0 : trace_buffer_lock_reserve(struct ring_buffer *buffer,
1582 : : int type,
1583 : : unsigned long len,
1584 : : unsigned long flags, int pc)
1585 : : {
1586 : : struct ring_buffer_event *event;
1587 : :
1588 : 0 : event = ring_buffer_lock_reserve(buffer, len);
1589 [ # # ]: 0 : if (event != NULL) {
1590 : 0 : struct trace_entry *ent = ring_buffer_event_data(event);
1591 : :
1592 : 0 : tracing_generic_entry_update(ent, flags, pc);
1593 : 0 : ent->type = type;
1594 : : }
1595 : :
1596 : 0 : return event;
1597 : : }
1598 : :
1599 : : void
1600 : 0 : __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
1601 : : {
1602 : 0 : __this_cpu_write(trace_cmdline_save, true);
1603 : 0 : ring_buffer_unlock_commit(buffer, event);
1604 : 0 : }
1605 : :
1606 : : static inline void
1607 : : __trace_buffer_unlock_commit(struct ring_buffer *buffer,
1608 : : struct ring_buffer_event *event,
1609 : : unsigned long flags, int pc)
1610 : : {
1611 : 0 : __buffer_unlock_commit(buffer, event);
1612 : :
1613 : 0 : ftrace_trace_stack(buffer, flags, 6, pc);
1614 : 0 : ftrace_trace_userstack(buffer, flags, pc);
1615 : : }
1616 : :
1617 : 0 : void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1618 : : struct ring_buffer_event *event,
1619 : : unsigned long flags, int pc)
1620 : : {
1621 : : __trace_buffer_unlock_commit(buffer, event, flags, pc);
1622 : 0 : }
1623 : : EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
1624 : :
1625 : : static struct ring_buffer *temp_buffer;
1626 : :
1627 : : struct ring_buffer_event *
1628 : 0 : trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
1629 : : struct ftrace_event_file *ftrace_file,
1630 : : int type, unsigned long len,
1631 : : unsigned long flags, int pc)
1632 : : {
1633 : : struct ring_buffer_event *entry;
1634 : :
1635 : 0 : *current_rb = ftrace_file->tr->trace_buffer.buffer;
1636 : 0 : entry = trace_buffer_lock_reserve(*current_rb,
1637 : : type, len, flags, pc);
1638 : : /*
1639 : : * If tracing is off, but we have triggers enabled
1640 : : * we still need to look at the event data. Use the temp_buffer
1641 : : * to store the trace event for the tigger to use. It's recusive
1642 : : * safe and will not be recorded anywhere.
1643 : : */
1644 [ # # ][ # # ]: 0 : if (!entry && ftrace_file->flags & FTRACE_EVENT_FL_TRIGGER_COND) {
1645 : 0 : *current_rb = temp_buffer;
1646 : 0 : entry = trace_buffer_lock_reserve(*current_rb,
1647 : : type, len, flags, pc);
1648 : : }
1649 : 0 : return entry;
1650 : : }
1651 : : EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
1652 : :
1653 : : struct ring_buffer_event *
1654 : 0 : trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1655 : : int type, unsigned long len,
1656 : : unsigned long flags, int pc)
1657 : : {
1658 : 0 : *current_rb = global_trace.trace_buffer.buffer;
1659 : 0 : return trace_buffer_lock_reserve(*current_rb,
1660 : : type, len, flags, pc);
1661 : : }
1662 : : EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
1663 : :
1664 : 0 : void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1665 : : struct ring_buffer_event *event,
1666 : : unsigned long flags, int pc)
1667 : : {
1668 : : __trace_buffer_unlock_commit(buffer, event, flags, pc);
1669 : 0 : }
1670 : : EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
1671 : :
1672 : 0 : void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1673 : : struct ring_buffer_event *event,
1674 : : unsigned long flags, int pc,
1675 : : struct pt_regs *regs)
1676 : : {
1677 : : __buffer_unlock_commit(buffer, event);
1678 : :
1679 : : ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1680 : 0 : ftrace_trace_userstack(buffer, flags, pc);
1681 : 0 : }
1682 : : EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
1683 : :
1684 : 0 : void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1685 : : struct ring_buffer_event *event)
1686 : : {
1687 : 0 : ring_buffer_discard_commit(buffer, event);
1688 : 0 : }
1689 : : EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
1690 : :
1691 : : void
1692 : 0 : trace_function(struct trace_array *tr,
1693 : : unsigned long ip, unsigned long parent_ip, unsigned long flags,
1694 : : int pc)
1695 : : {
1696 : : struct ftrace_event_call *call = &event_function;
1697 : 0 : struct ring_buffer *buffer = tr->trace_buffer.buffer;
1698 : : struct ring_buffer_event *event;
1699 : : struct ftrace_entry *entry;
1700 : :
1701 : : /* If we are reading the ring buffer, don't trace */
1702 [ # # ]: 0 : if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
1703 : : return;
1704 : :
1705 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
1706 : : flags, pc);
1707 [ # # ]: 0 : if (!event)
1708 : : return;
1709 : 0 : entry = ring_buffer_event_data(event);
1710 : 0 : entry->ip = ip;
1711 : 0 : entry->parent_ip = parent_ip;
1712 : :
1713 [ # # ]: 0 : if (!call_filter_check_discard(call, entry, buffer, event))
1714 : : __buffer_unlock_commit(buffer, event);
1715 : : }
1716 : :
1717 : : #ifdef CONFIG_STACKTRACE
1718 : :
1719 : : #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1720 : : struct ftrace_stack {
1721 : : unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1722 : : };
1723 : :
1724 : : static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1725 : : static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1726 : :
1727 : 0 : static void __ftrace_trace_stack(struct ring_buffer *buffer,
1728 : : unsigned long flags,
1729 : : int skip, int pc, struct pt_regs *regs)
1730 : : {
1731 : : struct ftrace_event_call *call = &event_kernel_stack;
1732 : : struct ring_buffer_event *event;
1733 : : struct stack_entry *entry;
1734 : : struct stack_trace trace;
1735 : : int use_stack;
1736 : : int size = FTRACE_STACK_ENTRIES;
1737 : :
1738 : 0 : trace.nr_entries = 0;
1739 : 0 : trace.skip = skip;
1740 : :
1741 : : /*
1742 : : * Since events can happen in NMIs there's no safe way to
1743 : : * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1744 : : * or NMI comes in, it will just have to use the default
1745 : : * FTRACE_STACK_SIZE.
1746 : : */
1747 : 0 : preempt_disable_notrace();
1748 : :
1749 : 0 : use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
1750 : : /*
1751 : : * We don't need any atomic variables, just a barrier.
1752 : : * If an interrupt comes in, we don't care, because it would
1753 : : * have exited and put the counter back to what we want.
1754 : : * We just need a barrier to keep gcc from moving things
1755 : : * around.
1756 : : */
1757 : 0 : barrier();
1758 [ # # ]: 0 : if (use_stack == 1) {
1759 : 0 : trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1760 : 0 : trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1761 : :
1762 [ # # ]: 0 : if (regs)
1763 : 0 : save_stack_trace_regs(regs, &trace);
1764 : : else
1765 : 0 : save_stack_trace(&trace);
1766 : :
1767 [ # # ]: 0 : if (trace.nr_entries > size)
1768 : 0 : size = trace.nr_entries;
1769 : : } else
1770 : : /* From now on, use_stack is a boolean */
1771 : : use_stack = 0;
1772 : :
1773 : 0 : size *= sizeof(unsigned long);
1774 : :
1775 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1776 : 0 : sizeof(*entry) + size, flags, pc);
1777 [ # # ]: 0 : if (!event)
1778 : : goto out;
1779 : 0 : entry = ring_buffer_event_data(event);
1780 : :
1781 [ # # ]: 0 : memset(&entry->caller, 0, size);
1782 : :
1783 [ # # ]: 0 : if (use_stack)
1784 : 0 : memcpy(&entry->caller, trace.entries,
1785 : 0 : trace.nr_entries * sizeof(unsigned long));
1786 : : else {
1787 : 0 : trace.max_entries = FTRACE_STACK_ENTRIES;
1788 : 0 : trace.entries = entry->caller;
1789 [ # # ]: 0 : if (regs)
1790 : 0 : save_stack_trace_regs(regs, &trace);
1791 : : else
1792 : 0 : save_stack_trace(&trace);
1793 : : }
1794 : :
1795 : 0 : entry->size = trace.nr_entries;
1796 : :
1797 [ # # ]: 0 : if (!call_filter_check_discard(call, entry, buffer, event))
1798 : : __buffer_unlock_commit(buffer, event);
1799 : :
1800 : : out:
1801 : : /* Again, don't let gcc optimize things here */
1802 : 0 : barrier();
1803 : 0 : __this_cpu_dec(ftrace_stack_reserve);
1804 : 0 : preempt_enable_notrace();
1805 : :
1806 : 0 : }
1807 : :
1808 : 0 : void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1809 : : int skip, int pc, struct pt_regs *regs)
1810 : : {
1811 [ # # # # ]: 0 : if (!(trace_flags & TRACE_ITER_STACKTRACE))
1812 : 0 : return;
1813 : :
1814 : 0 : __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1815 : : }
1816 : :
1817 : 0 : void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1818 : : int skip, int pc)
1819 : : {
1820 [ # # # # ]: 0 : if (!(trace_flags & TRACE_ITER_STACKTRACE))
[ # # ]
1821 : 0 : return;
1822 : :
1823 : 0 : __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
1824 : : }
1825 : :
1826 : 0 : void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1827 : : int pc)
1828 : : {
1829 : 0 : __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
1830 : 0 : }
1831 : :
1832 : : /**
1833 : : * trace_dump_stack - record a stack back trace in the trace buffer
1834 : : * @skip: Number of functions to skip (helper handlers)
1835 : : */
1836 : 0 : void trace_dump_stack(int skip)
1837 : : {
1838 : : unsigned long flags;
1839 : :
1840 [ # # ][ # # ]: 0 : if (tracing_disabled || tracing_selftest_running)
1841 : 0 : return;
1842 : :
1843 : : local_save_flags(flags);
1844 : :
1845 : : /*
1846 : : * Skip 3 more, seems to get us at the caller of
1847 : : * this function.
1848 : : */
1849 : 0 : skip += 3;
1850 : 0 : __ftrace_trace_stack(global_trace.trace_buffer.buffer,
1851 : : flags, skip, preempt_count(), NULL);
1852 : : }
1853 : :
1854 : : static DEFINE_PER_CPU(int, user_stack_count);
1855 : :
1856 : : void
1857 : 0 : ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
1858 : : {
1859 : : struct ftrace_event_call *call = &event_user_stack;
1860 : : struct ring_buffer_event *event;
1861 : : struct userstack_entry *entry;
1862 : : struct stack_trace trace;
1863 : :
1864 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1865 : : return;
1866 : :
1867 : : /*
1868 : : * NMIs can not handle page faults, even with fix ups.
1869 : : * The save user stack can (and often does) fault.
1870 : : */
1871 [ # # ]: 0 : if (unlikely(in_nmi()))
1872 : : return;
1873 : :
1874 : : /*
1875 : : * prevent recursion, since the user stack tracing may
1876 : : * trigger other kernel events.
1877 : : */
1878 : 0 : preempt_disable();
1879 [ # # ]: 0 : if (__this_cpu_read(user_stack_count))
1880 : : goto out;
1881 : :
1882 : 0 : __this_cpu_inc(user_stack_count);
1883 : :
1884 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
1885 : : sizeof(*entry), flags, pc);
1886 [ # # ]: 0 : if (!event)
1887 : : goto out_drop_count;
1888 : 0 : entry = ring_buffer_event_data(event);
1889 : :
1890 : 0 : entry->tgid = current->tgid;
1891 : 0 : memset(&entry->caller, 0, sizeof(entry->caller));
1892 : :
1893 : : trace.nr_entries = 0;
1894 : : trace.max_entries = FTRACE_STACK_ENTRIES;
1895 : : trace.skip = 0;
1896 : : trace.entries = entry->caller;
1897 : :
1898 : : save_stack_trace_user(&trace);
1899 [ # # ]: 0 : if (!call_filter_check_discard(call, entry, buffer, event))
1900 : : __buffer_unlock_commit(buffer, event);
1901 : :
1902 : : out_drop_count:
1903 : 0 : __this_cpu_dec(user_stack_count);
1904 : : out:
1905 : 0 : preempt_enable();
1906 : : }
1907 : :
1908 : : #ifdef UNUSED
1909 : : static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1910 : : {
1911 : : ftrace_trace_userstack(tr, flags, preempt_count());
1912 : : }
1913 : : #endif /* UNUSED */
1914 : :
1915 : : #endif /* CONFIG_STACKTRACE */
1916 : :
1917 : : /* created for use with alloc_percpu */
1918 : : struct trace_buffer_struct {
1919 : : char buffer[TRACE_BUF_SIZE];
1920 : : };
1921 : :
1922 : : static struct trace_buffer_struct *trace_percpu_buffer;
1923 : : static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1924 : : static struct trace_buffer_struct *trace_percpu_irq_buffer;
1925 : : static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1926 : :
1927 : : /*
1928 : : * The buffer used is dependent on the context. There is a per cpu
1929 : : * buffer for normal context, softirq contex, hard irq context and
1930 : : * for NMI context. Thise allows for lockless recording.
1931 : : *
1932 : : * Note, if the buffers failed to be allocated, then this returns NULL
1933 : : */
1934 : 0 : static char *get_trace_buf(void)
1935 : : {
1936 : : struct trace_buffer_struct *percpu_buffer;
1937 : :
1938 : : /*
1939 : : * If we have allocated per cpu buffers, then we do not
1940 : : * need to do any locking.
1941 : : */
1942 [ # # ]: 0 : if (in_nmi())
1943 : 0 : percpu_buffer = trace_percpu_nmi_buffer;
1944 [ # # ]: 0 : else if (in_irq())
1945 : 0 : percpu_buffer = trace_percpu_irq_buffer;
1946 [ # # ]: 0 : else if (in_softirq())
1947 : 0 : percpu_buffer = trace_percpu_sirq_buffer;
1948 : : else
1949 : 0 : percpu_buffer = trace_percpu_buffer;
1950 : :
1951 [ # # ]: 0 : if (!percpu_buffer)
1952 : : return NULL;
1953 : :
1954 : 0 : return this_cpu_ptr(&percpu_buffer->buffer[0]);
1955 : : }
1956 : :
1957 : 0 : static int alloc_percpu_trace_buffer(void)
1958 : : {
1959 : : struct trace_buffer_struct *buffers;
1960 : : struct trace_buffer_struct *sirq_buffers;
1961 : : struct trace_buffer_struct *irq_buffers;
1962 : : struct trace_buffer_struct *nmi_buffers;
1963 : :
1964 : 0 : buffers = alloc_percpu(struct trace_buffer_struct);
1965 [ # # ]: 0 : if (!buffers)
1966 : : goto err_warn;
1967 : :
1968 : 0 : sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1969 [ # # ]: 0 : if (!sirq_buffers)
1970 : : goto err_sirq;
1971 : :
1972 : 0 : irq_buffers = alloc_percpu(struct trace_buffer_struct);
1973 [ # # ]: 0 : if (!irq_buffers)
1974 : : goto err_irq;
1975 : :
1976 : 0 : nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1977 [ # # ]: 0 : if (!nmi_buffers)
1978 : : goto err_nmi;
1979 : :
1980 : 0 : trace_percpu_buffer = buffers;
1981 : 0 : trace_percpu_sirq_buffer = sirq_buffers;
1982 : 0 : trace_percpu_irq_buffer = irq_buffers;
1983 : 0 : trace_percpu_nmi_buffer = nmi_buffers;
1984 : :
1985 : 0 : return 0;
1986 : :
1987 : : err_nmi:
1988 : 0 : free_percpu(irq_buffers);
1989 : : err_irq:
1990 : 0 : free_percpu(sirq_buffers);
1991 : : err_sirq:
1992 : 0 : free_percpu(buffers);
1993 : : err_warn:
1994 : 0 : WARN(1, "Could not allocate percpu trace_printk buffer");
1995 : 0 : return -ENOMEM;
1996 : : }
1997 : :
1998 : : static int buffers_allocated;
1999 : :
2000 : 0 : void trace_printk_init_buffers(void)
2001 : : {
2002 [ # # ]: 0 : if (buffers_allocated)
2003 : : return;
2004 : :
2005 [ # # ]: 0 : if (alloc_percpu_trace_buffer())
2006 : : return;
2007 : :
2008 : 0 : pr_info("ftrace: Allocated trace_printk buffers\n");
2009 : :
2010 : : /* Expand the buffers to set size */
2011 : 0 : tracing_update_buffers();
2012 : :
2013 : 0 : buffers_allocated = 1;
2014 : :
2015 : : /*
2016 : : * trace_printk_init_buffers() can be called by modules.
2017 : : * If that happens, then we need to start cmdline recording
2018 : : * directly here. If the global_trace.buffer is already
2019 : : * allocated here, then this was called by module code.
2020 : : */
2021 [ # # ]: 0 : if (global_trace.trace_buffer.buffer)
2022 : 0 : tracing_start_cmdline_record();
2023 : : }
2024 : :
2025 : 0 : void trace_printk_start_comm(void)
2026 : : {
2027 : : /* Start tracing comms if trace printk is set */
2028 [ # # ]: 0 : if (!buffers_allocated)
2029 : 0 : return;
2030 : 0 : tracing_start_cmdline_record();
2031 : : }
2032 : :
2033 : 0 : static void trace_printk_start_stop_comm(int enabled)
2034 : : {
2035 [ # # ]: 0 : if (!buffers_allocated)
2036 : 0 : return;
2037 : :
2038 [ # # ]: 0 : if (enabled)
2039 : 0 : tracing_start_cmdline_record();
2040 : : else
2041 : 0 : tracing_stop_cmdline_record();
2042 : : }
2043 : :
2044 : : /**
2045 : : * trace_vbprintk - write binary msg to tracing buffer
2046 : : *
2047 : : */
2048 : 0 : int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
2049 : : {
2050 : : struct ftrace_event_call *call = &event_bprint;
2051 : : struct ring_buffer_event *event;
2052 : : struct ring_buffer *buffer;
2053 : : struct trace_array *tr = &global_trace;
2054 : : struct bprint_entry *entry;
2055 : : unsigned long flags;
2056 : : char *tbuffer;
2057 : : int len = 0, size, pc;
2058 : :
2059 [ # # ][ # # ]: 0 : if (unlikely(tracing_selftest_running || tracing_disabled))
2060 : : return 0;
2061 : :
2062 : : /* Don't pollute graph traces with trace_vprintk internals */
2063 : : pause_graph_tracing();
2064 : :
2065 : : pc = preempt_count();
2066 : 0 : preempt_disable_notrace();
2067 : :
2068 : 0 : tbuffer = get_trace_buf();
2069 [ # # ]: 0 : if (!tbuffer) {
2070 : : len = 0;
2071 : : goto out;
2072 : : }
2073 : :
2074 : 0 : len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2075 : :
2076 [ # # ]: 0 : if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2077 : : goto out;
2078 : :
2079 : : local_save_flags(flags);
2080 : 0 : size = sizeof(*entry) + sizeof(u32) * len;
2081 : 0 : buffer = tr->trace_buffer.buffer;
2082 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2083 : : flags, pc);
2084 [ # # ]: 0 : if (!event)
2085 : : goto out;
2086 : 0 : entry = ring_buffer_event_data(event);
2087 : 0 : entry->ip = ip;
2088 : 0 : entry->fmt = fmt;
2089 : :
2090 : 0 : memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2091 [ # # ]: 0 : if (!call_filter_check_discard(call, entry, buffer, event)) {
2092 : : __buffer_unlock_commit(buffer, event);
2093 : : ftrace_trace_stack(buffer, flags, 6, pc);
2094 : : }
2095 : :
2096 : : out:
2097 : 0 : preempt_enable_notrace();
2098 : : unpause_graph_tracing();
2099 : :
2100 : 0 : return len;
2101 : : }
2102 : : EXPORT_SYMBOL_GPL(trace_vbprintk);
2103 : :
2104 : : static int
2105 : 0 : __trace_array_vprintk(struct ring_buffer *buffer,
2106 : : unsigned long ip, const char *fmt, va_list args)
2107 : : {
2108 : : struct ftrace_event_call *call = &event_print;
2109 : : struct ring_buffer_event *event;
2110 : : int len = 0, size, pc;
2111 : : struct print_entry *entry;
2112 : : unsigned long flags;
2113 : : char *tbuffer;
2114 : :
2115 [ # # ][ # # ]: 0 : if (tracing_disabled || tracing_selftest_running)
2116 : : return 0;
2117 : :
2118 : : /* Don't pollute graph traces with trace_vprintk internals */
2119 : : pause_graph_tracing();
2120 : :
2121 : : pc = preempt_count();
2122 : 0 : preempt_disable_notrace();
2123 : :
2124 : :
2125 : 0 : tbuffer = get_trace_buf();
2126 [ # # ]: 0 : if (!tbuffer) {
2127 : : len = 0;
2128 : : goto out;
2129 : : }
2130 : :
2131 : 0 : len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2132 [ # # ]: 0 : if (len > TRACE_BUF_SIZE)
2133 : : goto out;
2134 : :
2135 : : local_save_flags(flags);
2136 : 0 : size = sizeof(*entry) + len + 1;
2137 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2138 : : flags, pc);
2139 [ # # ]: 0 : if (!event)
2140 : : goto out;
2141 : 0 : entry = ring_buffer_event_data(event);
2142 : 0 : entry->ip = ip;
2143 : :
2144 : 0 : memcpy(&entry->buf, tbuffer, len);
2145 : 0 : entry->buf[len] = '\0';
2146 [ # # ]: 0 : if (!call_filter_check_discard(call, entry, buffer, event)) {
2147 : : __buffer_unlock_commit(buffer, event);
2148 : : ftrace_trace_stack(buffer, flags, 6, pc);
2149 : : }
2150 : : out:
2151 : 0 : preempt_enable_notrace();
2152 : : unpause_graph_tracing();
2153 : :
2154 : 0 : return len;
2155 : : }
2156 : :
2157 : 0 : int trace_array_vprintk(struct trace_array *tr,
2158 : : unsigned long ip, const char *fmt, va_list args)
2159 : : {
2160 : 0 : return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
2161 : : }
2162 : :
2163 : 0 : int trace_array_printk(struct trace_array *tr,
2164 : : unsigned long ip, const char *fmt, ...)
2165 : : {
2166 : : int ret;
2167 : : va_list ap;
2168 : :
2169 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_PRINTK))
2170 : : return 0;
2171 : :
2172 : 0 : va_start(ap, fmt);
2173 : : ret = trace_array_vprintk(tr, ip, fmt, ap);
2174 : 0 : va_end(ap);
2175 : 0 : return ret;
2176 : : }
2177 : :
2178 : 0 : int trace_array_printk_buf(struct ring_buffer *buffer,
2179 : : unsigned long ip, const char *fmt, ...)
2180 : : {
2181 : : int ret;
2182 : : va_list ap;
2183 : :
2184 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_PRINTK))
2185 : : return 0;
2186 : :
2187 : 0 : va_start(ap, fmt);
2188 : 0 : ret = __trace_array_vprintk(buffer, ip, fmt, ap);
2189 : 0 : va_end(ap);
2190 : 0 : return ret;
2191 : : }
2192 : :
2193 : 0 : int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
2194 : : {
2195 : 0 : return trace_array_vprintk(&global_trace, ip, fmt, args);
2196 : : }
2197 : : EXPORT_SYMBOL_GPL(trace_vprintk);
2198 : :
2199 : 0 : static void trace_iterator_increment(struct trace_iterator *iter)
2200 : : {
2201 : 0 : struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
2202 : :
2203 : 0 : iter->idx++;
2204 [ # # ]: 0 : if (buf_iter)
2205 : 0 : ring_buffer_read(buf_iter, NULL);
2206 : 0 : }
2207 : :
2208 : : static struct trace_entry *
2209 : 0 : peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
2210 : : unsigned long *lost_events)
2211 : : {
2212 : : struct ring_buffer_event *event;
2213 : : struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
2214 : :
2215 [ # # ]: 0 : if (buf_iter)
2216 : 0 : event = ring_buffer_iter_peek(buf_iter, ts);
2217 : : else
2218 : 0 : event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
2219 : : lost_events);
2220 : :
2221 [ # # ]: 0 : if (event) {
2222 : 0 : iter->ent_size = ring_buffer_event_length(event);
2223 : 0 : return ring_buffer_event_data(event);
2224 : : }
2225 : 0 : iter->ent_size = 0;
2226 : 0 : return NULL;
2227 : : }
2228 : :
2229 : : static struct trace_entry *
2230 : 0 : __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
2231 : : unsigned long *missing_events, u64 *ent_ts)
2232 : : {
2233 : 0 : struct ring_buffer *buffer = iter->trace_buffer->buffer;
2234 : : struct trace_entry *ent, *next = NULL;
2235 : 0 : unsigned long lost_events = 0, next_lost = 0;
2236 : 0 : int cpu_file = iter->cpu_file;
2237 : : u64 next_ts = 0, ts;
2238 : : int next_cpu = -1;
2239 : : int next_size = 0;
2240 : : int cpu;
2241 : :
2242 : : /*
2243 : : * If we are in a per_cpu trace file, don't bother by iterating over
2244 : : * all cpu and peek directly.
2245 : : */
2246 [ # # ]: 0 : if (cpu_file > RING_BUFFER_ALL_CPUS) {
2247 [ # # ]: 0 : if (ring_buffer_empty_cpu(buffer, cpu_file))
2248 : : return NULL;
2249 : 0 : ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
2250 [ # # ]: 0 : if (ent_cpu)
2251 : 0 : *ent_cpu = cpu_file;
2252 : :
2253 : 0 : return ent;
2254 : : }
2255 : :
2256 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
2257 : :
2258 [ # # ]: 0 : if (ring_buffer_empty_cpu(buffer, cpu))
2259 : 0 : continue;
2260 : :
2261 : 0 : ent = peek_next_entry(iter, cpu, &ts, &lost_events);
2262 : :
2263 : : /*
2264 : : * Pick the entry with the smallest timestamp:
2265 : : */
2266 [ # # ][ # # ]: 0 : if (ent && (!next || ts < next_ts)) {
[ # # ]
2267 : : next = ent;
2268 : : next_cpu = cpu;
2269 : 0 : next_ts = ts;
2270 : 0 : next_lost = lost_events;
2271 : 0 : next_size = iter->ent_size;
2272 : : }
2273 : : }
2274 : :
2275 : 0 : iter->ent_size = next_size;
2276 : :
2277 [ # # ]: 0 : if (ent_cpu)
2278 : 0 : *ent_cpu = next_cpu;
2279 : :
2280 [ # # ]: 0 : if (ent_ts)
2281 : 0 : *ent_ts = next_ts;
2282 : :
2283 [ # # ]: 0 : if (missing_events)
2284 : 0 : *missing_events = next_lost;
2285 : :
2286 : 0 : return next;
2287 : : }
2288 : :
2289 : : /* Find the next real entry, without updating the iterator itself */
2290 : 0 : struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
2291 : : int *ent_cpu, u64 *ent_ts)
2292 : : {
2293 : 0 : return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
2294 : : }
2295 : :
2296 : : /* Find the next real entry, and increment the iterator to the next entry */
2297 : 0 : void *trace_find_next_entry_inc(struct trace_iterator *iter)
2298 : : {
2299 : 0 : iter->ent = __find_next_entry(iter, &iter->cpu,
2300 : : &iter->lost_events, &iter->ts);
2301 : :
2302 [ # # ]: 0 : if (iter->ent)
2303 : 0 : trace_iterator_increment(iter);
2304 : :
2305 [ # # ]: 0 : return iter->ent ? iter : NULL;
2306 : : }
2307 : :
2308 : : static void trace_consume(struct trace_iterator *iter)
2309 : : {
2310 : 0 : ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
2311 : : &iter->lost_events);
2312 : : }
2313 : :
2314 : 0 : static void *s_next(struct seq_file *m, void *v, loff_t *pos)
2315 : : {
2316 : 0 : struct trace_iterator *iter = m->private;
2317 : 0 : int i = (int)*pos;
2318 : : void *ent;
2319 : :
2320 [ # # ][ # # ]: 0 : WARN_ON_ONCE(iter->leftover);
[ # # ]
2321 : :
2322 : 0 : (*pos)++;
2323 : :
2324 : : /* can't go backwards */
2325 [ # # ]: 0 : if (iter->idx > i)
2326 : : return NULL;
2327 : :
2328 [ # # ]: 0 : if (iter->idx < 0)
2329 : 0 : ent = trace_find_next_entry_inc(iter);
2330 : : else
2331 : : ent = iter;
2332 : :
2333 [ # # ][ # # ]: 0 : while (ent && iter->idx < i)
2334 : 0 : ent = trace_find_next_entry_inc(iter);
2335 : :
2336 : 0 : iter->pos = *pos;
2337 : :
2338 : 0 : return ent;
2339 : : }
2340 : :
2341 : 0 : void tracing_iter_reset(struct trace_iterator *iter, int cpu)
2342 : : {
2343 : : struct ring_buffer_event *event;
2344 : : struct ring_buffer_iter *buf_iter;
2345 : : unsigned long entries = 0;
2346 : : u64 ts;
2347 : :
2348 : 0 : per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
2349 : :
2350 : : buf_iter = trace_buffer_iter(iter, cpu);
2351 [ # # ]: 0 : if (!buf_iter)
2352 : 0 : return;
2353 : :
2354 : 0 : ring_buffer_iter_reset(buf_iter);
2355 : :
2356 : : /*
2357 : : * We could have the case with the max latency tracers
2358 : : * that a reset never took place on a cpu. This is evident
2359 : : * by the timestamp being before the start of the buffer.
2360 : : */
2361 [ # # ]: 0 : while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
2362 [ # # ]: 0 : if (ts >= iter->trace_buffer->time_start)
2363 : : break;
2364 : 0 : entries++;
2365 : 0 : ring_buffer_read(buf_iter, NULL);
2366 : : }
2367 : :
2368 : 0 : per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
2369 : : }
2370 : :
2371 : : /*
2372 : : * The current tracer is copied to avoid a global locking
2373 : : * all around.
2374 : : */
2375 : 0 : static void *s_start(struct seq_file *m, loff_t *pos)
2376 : : {
2377 : 0 : struct trace_iterator *iter = m->private;
2378 : 0 : struct trace_array *tr = iter->tr;
2379 : 0 : int cpu_file = iter->cpu_file;
2380 : : void *p = NULL;
2381 : 0 : loff_t l = 0;
2382 : : int cpu;
2383 : :
2384 : : /*
2385 : : * copy the tracer to avoid using a global lock all around.
2386 : : * iter->trace is a copy of current_trace, the pointer to the
2387 : : * name may be used instead of a strcmp(), as iter->trace->name
2388 : : * will point to the same string as current_trace->name.
2389 : : */
2390 : 0 : mutex_lock(&trace_types_lock);
2391 [ # # ][ # # ]: 0 : if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
2392 : 0 : *iter->trace = *tr->current_trace;
2393 : 0 : mutex_unlock(&trace_types_lock);
2394 : :
2395 : : #ifdef CONFIG_TRACER_MAX_TRACE
2396 : : if (iter->snapshot && iter->trace->use_max_tr)
2397 : : return ERR_PTR(-EBUSY);
2398 : : #endif
2399 : :
2400 [ # # ]: 0 : if (!iter->snapshot)
2401 : : atomic_inc(&trace_record_cmdline_disabled);
2402 : :
2403 [ # # ]: 0 : if (*pos != iter->pos) {
2404 : 0 : iter->ent = NULL;
2405 : 0 : iter->cpu = 0;
2406 : 0 : iter->idx = -1;
2407 : :
2408 [ # # ]: 0 : if (cpu_file == RING_BUFFER_ALL_CPUS) {
2409 [ # # ]: 0 : for_each_tracing_cpu(cpu)
2410 : 0 : tracing_iter_reset(iter, cpu);
2411 : : } else
2412 : 0 : tracing_iter_reset(iter, cpu_file);
2413 : :
2414 : 0 : iter->leftover = 0;
2415 [ # # ][ # # ]: 0 : for (p = iter; p && l < *pos; p = s_next(m, p, &l))
2416 : : ;
2417 : :
2418 : : } else {
2419 : : /*
2420 : : * If we overflowed the seq_file before, then we want
2421 : : * to just reuse the trace_seq buffer again.
2422 : : */
2423 [ # # ]: 0 : if (iter->leftover)
2424 : : p = iter;
2425 : : else {
2426 : 0 : l = *pos - 1;
2427 : 0 : p = s_next(m, p, &l);
2428 : : }
2429 : : }
2430 : :
2431 : 0 : trace_event_read_lock();
2432 : : trace_access_lock(cpu_file);
2433 : 0 : return p;
2434 : : }
2435 : :
2436 : 0 : static void s_stop(struct seq_file *m, void *p)
2437 : : {
2438 : 0 : struct trace_iterator *iter = m->private;
2439 : :
2440 : : #ifdef CONFIG_TRACER_MAX_TRACE
2441 : : if (iter->snapshot && iter->trace->use_max_tr)
2442 : : return;
2443 : : #endif
2444 : :
2445 [ # # ]: 0 : if (!iter->snapshot)
2446 : : atomic_dec(&trace_record_cmdline_disabled);
2447 : :
2448 : 0 : trace_access_unlock(iter->cpu_file);
2449 : 0 : trace_event_read_unlock();
2450 : 0 : }
2451 : :
2452 : : static void
2453 : 0 : get_total_entries(struct trace_buffer *buf,
2454 : : unsigned long *total, unsigned long *entries)
2455 : : {
2456 : : unsigned long count;
2457 : : int cpu;
2458 : :
2459 : 0 : *total = 0;
2460 : 0 : *entries = 0;
2461 : :
2462 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
2463 : 0 : count = ring_buffer_entries_cpu(buf->buffer, cpu);
2464 : : /*
2465 : : * If this buffer has skipped entries, then we hold all
2466 : : * entries for the trace and we need to ignore the
2467 : : * ones before the time stamp.
2468 : : */
2469 [ # # ]: 0 : if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
2470 : 0 : count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
2471 : : /* total is the same as the entries */
2472 : 0 : *total += count;
2473 : : } else
2474 : 0 : *total += count +
2475 : 0 : ring_buffer_overrun_cpu(buf->buffer, cpu);
2476 : 0 : *entries += count;
2477 : : }
2478 : 0 : }
2479 : :
2480 : 0 : static void print_lat_help_header(struct seq_file *m)
2481 : : {
2482 : 0 : seq_puts(m, "# _------=> CPU# \n");
2483 : 0 : seq_puts(m, "# / _-----=> irqs-off \n");
2484 : 0 : seq_puts(m, "# | / _----=> need-resched \n");
2485 : 0 : seq_puts(m, "# || / _---=> hardirq/softirq \n");
2486 : 0 : seq_puts(m, "# ||| / _--=> preempt-depth \n");
2487 : 0 : seq_puts(m, "# |||| / delay \n");
2488 : 0 : seq_puts(m, "# cmd pid ||||| time | caller \n");
2489 : 0 : seq_puts(m, "# \\ / ||||| \\ | / \n");
2490 : 0 : }
2491 : :
2492 : 0 : static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
2493 : : {
2494 : : unsigned long total;
2495 : : unsigned long entries;
2496 : :
2497 : 0 : get_total_entries(buf, &total, &entries);
2498 : 0 : seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2499 : : entries, total, num_online_cpus());
2500 : 0 : seq_puts(m, "#\n");
2501 : 0 : }
2502 : :
2503 : 0 : static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
2504 : : {
2505 : 0 : print_event_info(buf, m);
2506 : 0 : seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
2507 : 0 : seq_puts(m, "# | | | | |\n");
2508 : 0 : }
2509 : :
2510 : 0 : static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
2511 : : {
2512 : 0 : print_event_info(buf, m);
2513 : 0 : seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
2514 : 0 : seq_puts(m, "# | | | | | |\n");
2515 : 0 : }
2516 : :
2517 : 0 : static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
2518 : : {
2519 : 0 : print_event_info(buf, m);
2520 : 0 : seq_puts(m, "# _-----=> irqs-off\n");
2521 : 0 : seq_puts(m, "# / _----=> need-resched\n");
2522 : 0 : seq_puts(m, "# | / _---=> hardirq/softirq\n");
2523 : 0 : seq_puts(m, "# || / _--=> preempt-depth\n");
2524 : 0 : seq_puts(m, "# ||| / delay\n");
2525 : 0 : seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2526 : 0 : seq_puts(m, "# | | | |||| | |\n");
2527 : 0 : }
2528 : :
2529 : 0 : static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
2530 : : {
2531 : 0 : print_event_info(buf, m);
2532 : 0 : seq_puts(m, "# _-----=> irqs-off\n");
2533 : 0 : seq_puts(m, "# / _----=> need-resched\n");
2534 : 0 : seq_puts(m, "# | / _---=> hardirq/softirq\n");
2535 : 0 : seq_puts(m, "# || / _--=> preempt-depth\n");
2536 : 0 : seq_puts(m, "# ||| / delay\n");
2537 : 0 : seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
2538 : 0 : seq_puts(m, "# | | | | |||| | |\n");
2539 : 0 : }
2540 : :
2541 : : void
2542 : 0 : print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2543 : : {
2544 : 0 : unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2545 : 0 : struct trace_buffer *buf = iter->trace_buffer;
2546 : 0 : struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
2547 : 0 : struct tracer *type = iter->trace;
2548 : : unsigned long entries;
2549 : : unsigned long total;
2550 : : const char *name = "preemption";
2551 : :
2552 : 0 : name = type->name;
2553 : :
2554 : 0 : get_total_entries(buf, &total, &entries);
2555 : :
2556 : 0 : seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
2557 : : name, UTS_RELEASE);
2558 : 0 : seq_puts(m, "# -----------------------------------"
2559 : : "---------------------------------\n");
2560 : 0 : seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
2561 : : " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
2562 : : nsecs_to_usecs(data->saved_latency),
2563 : : entries,
2564 : : total,
2565 : : buf->cpu,
2566 : : #if defined(CONFIG_PREEMPT_NONE)
2567 : : "server",
2568 : : #elif defined(CONFIG_PREEMPT_VOLUNTARY)
2569 : : "desktop",
2570 : : #elif defined(CONFIG_PREEMPT)
2571 : : "preempt",
2572 : : #else
2573 : : "unknown",
2574 : : #endif
2575 : : /* These are reserved for later use */
2576 : : 0, 0, 0, 0);
2577 : : #ifdef CONFIG_SMP
2578 : 0 : seq_printf(m, " #P:%d)\n", num_online_cpus());
2579 : : #else
2580 : : seq_puts(m, ")\n");
2581 : : #endif
2582 : 0 : seq_puts(m, "# -----------------\n");
2583 : 0 : seq_printf(m, "# | task: %.16s-%d "
2584 : : "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2585 : 0 : data->comm, data->pid,
2586 : : from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
2587 : : data->policy, data->rt_priority);
2588 : 0 : seq_puts(m, "# -----------------\n");
2589 : :
2590 [ # # ]: 0 : if (data->critical_start) {
2591 : 0 : seq_puts(m, "# => started at: ");
2592 : 0 : seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2593 : 0 : trace_print_seq(m, &iter->seq);
2594 : 0 : seq_puts(m, "\n# => ended at: ");
2595 : 0 : seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2596 : 0 : trace_print_seq(m, &iter->seq);
2597 : 0 : seq_puts(m, "\n#\n");
2598 : : }
2599 : :
2600 : 0 : seq_puts(m, "#\n");
2601 : 0 : }
2602 : :
2603 : 0 : static void test_cpu_buff_start(struct trace_iterator *iter)
2604 : : {
2605 : 0 : struct trace_seq *s = &iter->seq;
2606 : :
2607 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_ANNOTATE))
2608 : : return;
2609 : :
2610 [ # # ]: 0 : if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2611 : : return;
2612 : :
2613 [ # # ]: 0 : if (cpumask_test_cpu(iter->cpu, iter->started))
2614 : : return;
2615 : :
2616 [ # # ]: 0 : if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
2617 : : return;
2618 : :
2619 : : cpumask_set_cpu(iter->cpu, iter->started);
2620 : :
2621 : : /* Don't print started cpu buffer for the first entry of the trace */
2622 [ # # ]: 0 : if (iter->idx > 1)
2623 : 0 : trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2624 : : iter->cpu);
2625 : : }
2626 : :
2627 : 0 : static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
2628 : : {
2629 : 0 : struct trace_seq *s = &iter->seq;
2630 : 0 : unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2631 : : struct trace_entry *entry;
2632 : : struct trace_event *event;
2633 : :
2634 : 0 : entry = iter->ent;
2635 : :
2636 : 0 : test_cpu_buff_start(iter);
2637 : :
2638 : 0 : event = ftrace_find_event(entry->type);
2639 : :
2640 [ # # ]: 0 : if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2641 [ # # ]: 0 : if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2642 [ # # ]: 0 : if (!trace_print_lat_context(iter))
2643 : : goto partial;
2644 : : } else {
2645 [ # # ]: 0 : if (!trace_print_context(iter))
2646 : : goto partial;
2647 : : }
2648 : : }
2649 : :
2650 [ # # ]: 0 : if (event)
2651 : 0 : return event->funcs->trace(iter, sym_flags, event);
2652 : :
2653 [ # # ]: 0 : if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2654 : : goto partial;
2655 : :
2656 : : return TRACE_TYPE_HANDLED;
2657 : : partial:
2658 : : return TRACE_TYPE_PARTIAL_LINE;
2659 : : }
2660 : :
2661 : 0 : static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
2662 : : {
2663 : 0 : struct trace_seq *s = &iter->seq;
2664 : : struct trace_entry *entry;
2665 : : struct trace_event *event;
2666 : :
2667 : 0 : entry = iter->ent;
2668 : :
2669 [ # # ]: 0 : if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2670 [ # # ]: 0 : if (!trace_seq_printf(s, "%d %d %llu ",
2671 : : entry->pid, iter->cpu, iter->ts))
2672 : : goto partial;
2673 : : }
2674 : :
2675 : 0 : event = ftrace_find_event(entry->type);
2676 [ # # ]: 0 : if (event)
2677 : 0 : return event->funcs->raw(iter, 0, event);
2678 : :
2679 [ # # ]: 0 : if (!trace_seq_printf(s, "%d ?\n", entry->type))
2680 : : goto partial;
2681 : :
2682 : : return TRACE_TYPE_HANDLED;
2683 : : partial:
2684 : : return TRACE_TYPE_PARTIAL_LINE;
2685 : : }
2686 : :
2687 : 0 : static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
2688 : : {
2689 : 0 : struct trace_seq *s = &iter->seq;
2690 : 0 : unsigned char newline = '\n';
2691 : : struct trace_entry *entry;
2692 : : struct trace_event *event;
2693 : :
2694 : 0 : entry = iter->ent;
2695 : :
2696 [ # # ]: 0 : if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2697 [ # # ]: 0 : SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2698 [ # # ]: 0 : SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2699 [ # # ]: 0 : SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2700 : : }
2701 : :
2702 : 0 : event = ftrace_find_event(entry->type);
2703 [ # # ]: 0 : if (event) {
2704 : 0 : enum print_line_t ret = event->funcs->hex(iter, 0, event);
2705 [ # # ]: 0 : if (ret != TRACE_TYPE_HANDLED)
2706 : : return ret;
2707 : : }
2708 : :
2709 [ # # ]: 0 : SEQ_PUT_FIELD_RET(s, newline);
2710 : :
2711 : 0 : return TRACE_TYPE_HANDLED;
2712 : : }
2713 : :
2714 : 0 : static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
2715 : : {
2716 : 0 : struct trace_seq *s = &iter->seq;
2717 : : struct trace_entry *entry;
2718 : : struct trace_event *event;
2719 : :
2720 : 0 : entry = iter->ent;
2721 : :
2722 [ # # ]: 0 : if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2723 [ # # ]: 0 : SEQ_PUT_FIELD_RET(s, entry->pid);
2724 [ # # ]: 0 : SEQ_PUT_FIELD_RET(s, iter->cpu);
2725 [ # # ]: 0 : SEQ_PUT_FIELD_RET(s, iter->ts);
2726 : : }
2727 : :
2728 : 0 : event = ftrace_find_event(entry->type);
2729 [ # # ]: 0 : return event ? event->funcs->binary(iter, 0, event) :
2730 : : TRACE_TYPE_HANDLED;
2731 : : }
2732 : :
2733 : 0 : int trace_empty(struct trace_iterator *iter)
2734 : : {
2735 : : struct ring_buffer_iter *buf_iter;
2736 : : int cpu;
2737 : :
2738 : : /* If we are looking at one CPU buffer, only check that one */
2739 [ # # ]: 0 : if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
2740 : : cpu = iter->cpu_file;
2741 : : buf_iter = trace_buffer_iter(iter, cpu);
2742 [ # # ]: 0 : if (buf_iter) {
2743 [ # # ]: 0 : if (!ring_buffer_iter_empty(buf_iter))
2744 : : return 0;
2745 : : } else {
2746 [ # # ]: 0 : if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2747 : : return 0;
2748 : : }
2749 : : return 1;
2750 : : }
2751 : :
2752 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
2753 : : buf_iter = trace_buffer_iter(iter, cpu);
2754 [ # # ]: 0 : if (buf_iter) {
2755 [ # # ]: 0 : if (!ring_buffer_iter_empty(buf_iter))
2756 : : return 0;
2757 : : } else {
2758 [ # # ]: 0 : if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
2759 : : return 0;
2760 : : }
2761 : : }
2762 : :
2763 : : return 1;
2764 : : }
2765 : :
2766 : : /* Called with trace_event_read_lock() held. */
2767 : 0 : enum print_line_t print_trace_line(struct trace_iterator *iter)
2768 : : {
2769 : : enum print_line_t ret;
2770 : :
2771 [ # # # # ]: 0 : if (iter->lost_events &&
2772 : 0 : !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2773 : : iter->cpu, iter->lost_events))
2774 : : return TRACE_TYPE_PARTIAL_LINE;
2775 : :
2776 [ # # ][ # # ]: 0 : if (iter->trace && iter->trace->print_line) {
2777 : 0 : ret = iter->trace->print_line(iter);
2778 [ # # ]: 0 : if (ret != TRACE_TYPE_UNHANDLED)
2779 : : return ret;
2780 : : }
2781 : :
2782 [ # # ]: 0 : if (iter->ent->type == TRACE_BPUTS &&
2783 [ # # ]: 0 : trace_flags & TRACE_ITER_PRINTK &&
2784 : : trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2785 : 0 : return trace_print_bputs_msg_only(iter);
2786 : :
2787 [ # # ]: 0 : if (iter->ent->type == TRACE_BPRINT &&
2788 [ # # ]: 0 : trace_flags & TRACE_ITER_PRINTK &&
2789 : : trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2790 : 0 : return trace_print_bprintk_msg_only(iter);
2791 : :
2792 [ # # ]: 0 : if (iter->ent->type == TRACE_PRINT &&
2793 [ # # ]: 0 : trace_flags & TRACE_ITER_PRINTK &&
2794 : : trace_flags & TRACE_ITER_PRINTK_MSGONLY)
2795 : 0 : return trace_print_printk_msg_only(iter);
2796 : :
2797 [ # # ]: 0 : if (trace_flags & TRACE_ITER_BIN)
2798 : 0 : return print_bin_fmt(iter);
2799 : :
2800 [ # # ]: 0 : if (trace_flags & TRACE_ITER_HEX)
2801 : 0 : return print_hex_fmt(iter);
2802 : :
2803 [ # # ]: 0 : if (trace_flags & TRACE_ITER_RAW)
2804 : 0 : return print_raw_fmt(iter);
2805 : :
2806 : 0 : return print_trace_fmt(iter);
2807 : : }
2808 : :
2809 : 0 : void trace_latency_header(struct seq_file *m)
2810 : : {
2811 : 0 : struct trace_iterator *iter = m->private;
2812 : :
2813 : : /* print nothing if the buffers are empty */
2814 [ # # ]: 0 : if (trace_empty(iter))
2815 : 0 : return;
2816 : :
2817 [ # # ]: 0 : if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2818 : 0 : print_trace_header(m, iter);
2819 : :
2820 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_VERBOSE))
2821 : 0 : print_lat_help_header(m);
2822 : : }
2823 : :
2824 : 0 : void trace_default_header(struct seq_file *m)
2825 : : {
2826 : 0 : struct trace_iterator *iter = m->private;
2827 : :
2828 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2829 : : return;
2830 : :
2831 [ # # ]: 0 : if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2832 : : /* print nothing if the buffers are empty */
2833 [ # # ]: 0 : if (trace_empty(iter))
2834 : : return;
2835 : 0 : print_trace_header(m, iter);
2836 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_VERBOSE))
2837 : 0 : print_lat_help_header(m);
2838 : : } else {
2839 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2840 [ # # ]: 0 : if (trace_flags & TRACE_ITER_IRQ_INFO)
2841 [ # # ]: 0 : if (trace_flags & TRACE_ITER_TGID)
2842 : 0 : print_func_help_header_irq_tgid(iter->trace_buffer, m);
2843 : : else
2844 : 0 : print_func_help_header_irq(iter->trace_buffer, m);
2845 : : else
2846 [ # # ]: 0 : if (trace_flags & TRACE_ITER_TGID)
2847 : 0 : print_func_help_header_tgid(iter->trace_buffer, m);
2848 : : else
2849 : 0 : print_func_help_header(iter->trace_buffer, m);
2850 : : }
2851 : : }
2852 : : }
2853 : :
2854 : 0 : static void test_ftrace_alive(struct seq_file *m)
2855 : : {
2856 [ # # ]: 0 : if (!ftrace_is_dead())
2857 : 0 : return;
2858 : 0 : seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2859 : 0 : seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2860 : : }
2861 : :
2862 : : #ifdef CONFIG_TRACER_MAX_TRACE
2863 : : static void show_snapshot_main_help(struct seq_file *m)
2864 : : {
2865 : : seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
2866 : : seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2867 : : seq_printf(m, "# Takes a snapshot of the main buffer.\n");
2868 : : seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n");
2869 : : seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2870 : : seq_printf(m, "# is not a '0' or '1')\n");
2871 : : }
2872 : :
2873 : : static void show_snapshot_percpu_help(struct seq_file *m)
2874 : : {
2875 : : seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
2876 : : #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
2877 : : seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
2878 : : seq_printf(m, "# Takes a snapshot of the main buffer for this cpu.\n");
2879 : : #else
2880 : : seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
2881 : : seq_printf(m, "# Must use main snapshot file to allocate.\n");
2882 : : #endif
2883 : : seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
2884 : : seq_printf(m, "# (Doesn't have to be '2' works with any number that\n");
2885 : : seq_printf(m, "# is not a '0' or '1')\n");
2886 : : }
2887 : :
2888 : : static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
2889 : : {
2890 : : if (iter->tr->allocated_snapshot)
2891 : : seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
2892 : : else
2893 : : seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
2894 : :
2895 : : seq_printf(m, "# Snapshot commands:\n");
2896 : : if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
2897 : : show_snapshot_main_help(m);
2898 : : else
2899 : : show_snapshot_percpu_help(m);
2900 : : }
2901 : : #else
2902 : : /* Should never be called */
2903 : : static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
2904 : : #endif
2905 : :
2906 : 0 : static int s_show(struct seq_file *m, void *v)
2907 : : {
2908 : : struct trace_iterator *iter = v;
2909 : : int ret;
2910 : :
2911 [ # # ]: 0 : if (iter->ent == NULL) {
2912 [ # # ]: 0 : if (iter->tr) {
2913 : 0 : seq_printf(m, "# tracer: %s\n", iter->trace->name);
2914 : 0 : seq_puts(m, "#\n");
2915 : 0 : test_ftrace_alive(m);
2916 : : }
2917 [ # # ][ # # ]: 0 : if (iter->snapshot && trace_empty(iter))
2918 : : print_snapshot_help(m, iter);
2919 [ # # ][ # # ]: 0 : else if (iter->trace && iter->trace->print_header)
2920 : 0 : iter->trace->print_header(m);
2921 : : else
2922 : 0 : trace_default_header(m);
2923 : :
2924 [ # # ]: 0 : } else if (iter->leftover) {
2925 : : /*
2926 : : * If we filled the seq_file buffer earlier, we
2927 : : * want to just show it now.
2928 : : */
2929 : 0 : ret = trace_print_seq(m, &iter->seq);
2930 : :
2931 : : /* ret should this time be zero, but you never know */
2932 : 0 : iter->leftover = ret;
2933 : :
2934 : : } else {
2935 : 0 : print_trace_line(iter);
2936 : 0 : ret = trace_print_seq(m, &iter->seq);
2937 : : /*
2938 : : * If we overflow the seq_file buffer, then it will
2939 : : * ask us for this data again at start up.
2940 : : * Use that instead.
2941 : : * ret is 0 if seq_file write succeeded.
2942 : : * -1 otherwise.
2943 : : */
2944 : 0 : iter->leftover = ret;
2945 : : }
2946 : :
2947 : 0 : return 0;
2948 : : }
2949 : :
2950 : : /*
2951 : : * Should be used after trace_array_get(), trace_types_lock
2952 : : * ensures that i_cdev was already initialized.
2953 : : */
2954 : : static inline int tracing_get_cpu(struct inode *inode)
2955 : : {
2956 [ # # # # ]: 0 : if (inode->i_cdev) /* See trace_create_cpu_file() */
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
2957 : 0 : return (long)inode->i_cdev - 1;
2958 : : return RING_BUFFER_ALL_CPUS;
2959 : : }
2960 : :
2961 : : static const struct seq_operations tracer_seq_ops = {
2962 : : .start = s_start,
2963 : : .next = s_next,
2964 : : .stop = s_stop,
2965 : : .show = s_show,
2966 : : };
2967 : :
2968 : : static struct trace_iterator *
2969 : 0 : __tracing_open(struct inode *inode, struct file *file, bool snapshot)
2970 : : {
2971 : 0 : struct trace_array *tr = inode->i_private;
2972 : : struct trace_iterator *iter;
2973 : : int cpu;
2974 : :
2975 [ # # ]: 0 : if (tracing_disabled)
2976 : : return ERR_PTR(-ENODEV);
2977 : :
2978 : 0 : iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
2979 [ # # ]: 0 : if (!iter)
2980 : : return ERR_PTR(-ENOMEM);
2981 : :
2982 : 0 : iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
2983 : : GFP_KERNEL);
2984 [ # # ]: 0 : if (!iter->buffer_iter)
2985 : : goto release;
2986 : :
2987 : : /*
2988 : : * We make a copy of the current tracer to avoid concurrent
2989 : : * changes on it while we are reading.
2990 : : */
2991 : 0 : mutex_lock(&trace_types_lock);
2992 : 0 : iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
2993 [ # # ]: 0 : if (!iter->trace)
2994 : : goto fail;
2995 : :
2996 : 0 : *iter->trace = *tr->current_trace;
2997 : :
2998 : : if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
2999 : : goto fail;
3000 : :
3001 : 0 : iter->tr = tr;
3002 : :
3003 : : #ifdef CONFIG_TRACER_MAX_TRACE
3004 : : /* Currently only the top directory has a snapshot */
3005 : : if (tr->current_trace->print_max || snapshot)
3006 : : iter->trace_buffer = &tr->max_buffer;
3007 : : else
3008 : : #endif
3009 : 0 : iter->trace_buffer = &tr->trace_buffer;
3010 : 0 : iter->snapshot = snapshot;
3011 : 0 : iter->pos = -1;
3012 : 0 : iter->cpu_file = tracing_get_cpu(inode);
3013 : 0 : mutex_init(&iter->mutex);
3014 : :
3015 : : /* Notify the tracer early; before we stop tracing. */
3016 [ # # ][ # # ]: 0 : if (iter->trace && iter->trace->open)
3017 : 0 : iter->trace->open(iter);
3018 : :
3019 : : /* Annotate start of buffers if we had overruns */
3020 [ # # ]: 0 : if (ring_buffer_overruns(iter->trace_buffer->buffer))
3021 : 0 : iter->iter_flags |= TRACE_FILE_ANNOTATE;
3022 : :
3023 : : /* Output in nanoseconds only if we are using a clock in nanoseconds. */
3024 [ # # ]: 0 : if (trace_clocks[tr->clock_id].in_ns)
3025 : 0 : iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
3026 : :
3027 : : /* stop the trace while dumping if we are not opening "snapshot" */
3028 [ # # ]: 0 : if (!iter->snapshot)
3029 : 0 : tracing_stop_tr(tr);
3030 : :
3031 [ # # ]: 0 : if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
3032 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
3033 : 0 : iter->buffer_iter[cpu] =
3034 : 0 : ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
3035 : : }
3036 : 0 : ring_buffer_read_prepare_sync();
3037 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
3038 : 0 : ring_buffer_read_start(iter->buffer_iter[cpu]);
3039 : 0 : tracing_iter_reset(iter, cpu);
3040 : : }
3041 : : } else {
3042 : : cpu = iter->cpu_file;
3043 : 0 : iter->buffer_iter[cpu] =
3044 : 0 : ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
3045 : 0 : ring_buffer_read_prepare_sync();
3046 : 0 : ring_buffer_read_start(iter->buffer_iter[cpu]);
3047 : 0 : tracing_iter_reset(iter, cpu);
3048 : : }
3049 : :
3050 : 0 : mutex_unlock(&trace_types_lock);
3051 : :
3052 : 0 : return iter;
3053 : :
3054 : : fail:
3055 : 0 : mutex_unlock(&trace_types_lock);
3056 : 0 : kfree(iter->trace);
3057 : 0 : kfree(iter->buffer_iter);
3058 : : release:
3059 : 0 : seq_release_private(inode, file);
3060 : 0 : return ERR_PTR(-ENOMEM);
3061 : : }
3062 : :
3063 : 0 : int tracing_open_generic(struct inode *inode, struct file *filp)
3064 : : {
3065 [ # # ]: 0 : if (tracing_disabled)
3066 : : return -ENODEV;
3067 : :
3068 : 0 : filp->private_data = inode->i_private;
3069 : 0 : return 0;
3070 : : }
3071 : :
3072 : 0 : bool tracing_is_disabled(void)
3073 : : {
3074 : 0 : return (tracing_disabled) ? true: false;
3075 : : }
3076 : :
3077 : : /*
3078 : : * Open and update trace_array ref count.
3079 : : * Must have the current trace_array passed to it.
3080 : : */
3081 : 0 : static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
3082 : : {
3083 : 0 : struct trace_array *tr = inode->i_private;
3084 : :
3085 [ # # ]: 0 : if (tracing_disabled)
3086 : : return -ENODEV;
3087 : :
3088 [ # # ]: 0 : if (trace_array_get(tr) < 0)
3089 : : return -ENODEV;
3090 : :
3091 : 0 : filp->private_data = inode->i_private;
3092 : :
3093 : 0 : return 0;
3094 : : }
3095 : :
3096 : 0 : static int tracing_release(struct inode *inode, struct file *file)
3097 : : {
3098 : 0 : struct trace_array *tr = inode->i_private;
3099 : 0 : struct seq_file *m = file->private_data;
3100 : : struct trace_iterator *iter;
3101 : : int cpu;
3102 : :
3103 [ # # ]: 0 : if (!(file->f_mode & FMODE_READ)) {
3104 : 0 : trace_array_put(tr);
3105 : 0 : return 0;
3106 : : }
3107 : :
3108 : : /* Writes do not use seq_file */
3109 : 0 : iter = m->private;
3110 : 0 : mutex_lock(&trace_types_lock);
3111 : :
3112 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
3113 [ # # ]: 0 : if (iter->buffer_iter[cpu])
3114 : 0 : ring_buffer_read_finish(iter->buffer_iter[cpu]);
3115 : : }
3116 : :
3117 [ # # ][ # # ]: 0 : if (iter->trace && iter->trace->close)
3118 : 0 : iter->trace->close(iter);
3119 : :
3120 [ # # ]: 0 : if (!iter->snapshot)
3121 : : /* reenable tracing if it was previously enabled */
3122 : 0 : tracing_start_tr(tr);
3123 : :
3124 : 0 : __trace_array_put(tr);
3125 : :
3126 : 0 : mutex_unlock(&trace_types_lock);
3127 : :
3128 : : mutex_destroy(&iter->mutex);
3129 : : free_cpumask_var(iter->started);
3130 : 0 : kfree(iter->trace);
3131 : 0 : kfree(iter->buffer_iter);
3132 : 0 : seq_release_private(inode, file);
3133 : :
3134 : 0 : return 0;
3135 : : }
3136 : :
3137 : 0 : static int tracing_release_generic_tr(struct inode *inode, struct file *file)
3138 : : {
3139 : 0 : struct trace_array *tr = inode->i_private;
3140 : :
3141 : 0 : trace_array_put(tr);
3142 : 0 : return 0;
3143 : : }
3144 : :
3145 : 0 : static int tracing_single_release_tr(struct inode *inode, struct file *file)
3146 : : {
3147 : 0 : struct trace_array *tr = inode->i_private;
3148 : :
3149 : 0 : trace_array_put(tr);
3150 : :
3151 : 0 : return single_release(inode, file);
3152 : : }
3153 : :
3154 : 0 : static int tracing_open(struct inode *inode, struct file *file)
3155 : : {
3156 : 0 : struct trace_array *tr = inode->i_private;
3157 : : struct trace_iterator *iter;
3158 : : int ret = 0;
3159 : :
3160 [ # # ]: 0 : if (trace_array_get(tr) < 0)
3161 : : return -ENODEV;
3162 : :
3163 : : /* If this file was open for write, then erase contents */
3164 [ # # ][ # # ]: 0 : if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
3165 : : int cpu = tracing_get_cpu(inode);
3166 : :
3167 [ # # ]: 0 : if (cpu == RING_BUFFER_ALL_CPUS)
3168 : 0 : tracing_reset_online_cpus(&tr->trace_buffer);
3169 : : else
3170 : 0 : tracing_reset(&tr->trace_buffer, cpu);
3171 : : }
3172 : :
3173 [ # # ]: 0 : if (file->f_mode & FMODE_READ) {
3174 : 0 : iter = __tracing_open(inode, file, false);
3175 [ # # ]: 0 : if (IS_ERR(iter))
3176 : : ret = PTR_ERR(iter);
3177 [ # # ]: 0 : else if (trace_flags & TRACE_ITER_LATENCY_FMT)
3178 : 0 : iter->iter_flags |= TRACE_FILE_LAT_FMT;
3179 : : }
3180 : :
3181 [ # # ]: 0 : if (ret < 0)
3182 : 0 : trace_array_put(tr);
3183 : :
3184 : 0 : return ret;
3185 : : }
3186 : :
3187 : : static void *
3188 : 0 : t_next(struct seq_file *m, void *v, loff_t *pos)
3189 : : {
3190 : : struct tracer *t = v;
3191 : :
3192 : 0 : (*pos)++;
3193 : :
3194 [ # # ][ # # ]: 0 : if (t)
3195 : 0 : t = t->next;
3196 : :
3197 : 0 : return t;
3198 : : }
3199 : :
3200 : 0 : static void *t_start(struct seq_file *m, loff_t *pos)
3201 : : {
3202 : : struct tracer *t;
3203 : : loff_t l = 0;
3204 : :
3205 : 0 : mutex_lock(&trace_types_lock);
3206 [ # # ][ # # ]: 0 : for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
3207 : : ;
3208 : :
3209 : 0 : return t;
3210 : : }
3211 : :
3212 : 0 : static void t_stop(struct seq_file *m, void *p)
3213 : : {
3214 : 0 : mutex_unlock(&trace_types_lock);
3215 : 0 : }
3216 : :
3217 : 0 : static int t_show(struct seq_file *m, void *v)
3218 : : {
3219 : : struct tracer *t = v;
3220 : :
3221 [ # # ]: 0 : if (!t)
3222 : : return 0;
3223 : :
3224 : 0 : seq_printf(m, "%s", t->name);
3225 [ # # ]: 0 : if (t->next)
3226 : 0 : seq_putc(m, ' ');
3227 : : else
3228 : 0 : seq_putc(m, '\n');
3229 : :
3230 : : return 0;
3231 : : }
3232 : :
3233 : : static const struct seq_operations show_traces_seq_ops = {
3234 : : .start = t_start,
3235 : : .next = t_next,
3236 : : .stop = t_stop,
3237 : : .show = t_show,
3238 : : };
3239 : :
3240 : 0 : static int show_traces_open(struct inode *inode, struct file *file)
3241 : : {
3242 [ # # ]: 0 : if (tracing_disabled)
3243 : : return -ENODEV;
3244 : :
3245 : 0 : return seq_open(file, &show_traces_seq_ops);
3246 : : }
3247 : :
3248 : : static ssize_t
3249 : 0 : tracing_write_stub(struct file *filp, const char __user *ubuf,
3250 : : size_t count, loff_t *ppos)
3251 : : {
3252 : 0 : return count;
3253 : : }
3254 : :
3255 : 0 : loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
3256 : : {
3257 : : int ret;
3258 : :
3259 [ # # ]: 0 : if (file->f_mode & FMODE_READ)
3260 : 0 : ret = seq_lseek(file, offset, whence);
3261 : : else
3262 : 0 : file->f_pos = ret = 0;
3263 : :
3264 : 0 : return ret;
3265 : : }
3266 : :
3267 : : static const struct file_operations tracing_fops = {
3268 : : .open = tracing_open,
3269 : : .read = seq_read,
3270 : : .write = tracing_write_stub,
3271 : : .llseek = tracing_lseek,
3272 : : .release = tracing_release,
3273 : : };
3274 : :
3275 : : static const struct file_operations show_traces_fops = {
3276 : : .open = show_traces_open,
3277 : : .read = seq_read,
3278 : : .release = seq_release,
3279 : : .llseek = seq_lseek,
3280 : : };
3281 : :
3282 : : /*
3283 : : * The tracer itself will not take this lock, but still we want
3284 : : * to provide a consistent cpumask to user-space:
3285 : : */
3286 : : static DEFINE_MUTEX(tracing_cpumask_update_lock);
3287 : :
3288 : : /*
3289 : : * Temporary storage for the character representation of the
3290 : : * CPU bitmask (and one more byte for the newline):
3291 : : */
3292 : : static char mask_str[NR_CPUS + 1];
3293 : :
3294 : : static ssize_t
3295 : 0 : tracing_cpumask_read(struct file *filp, char __user *ubuf,
3296 : : size_t count, loff_t *ppos)
3297 : : {
3298 : 0 : struct trace_array *tr = file_inode(filp)->i_private;
3299 : : int len;
3300 : :
3301 : 0 : mutex_lock(&tracing_cpumask_update_lock);
3302 : :
3303 : : len = cpumask_scnprintf(mask_str, count, tr->tracing_cpumask);
3304 [ # # ]: 0 : if (count - len < 2) {
3305 : : count = -EINVAL;
3306 : : goto out_err;
3307 : : }
3308 : 0 : len += sprintf(mask_str + len, "\n");
3309 : 0 : count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
3310 : :
3311 : : out_err:
3312 : 0 : mutex_unlock(&tracing_cpumask_update_lock);
3313 : :
3314 : 0 : return count;
3315 : : }
3316 : :
3317 : : static ssize_t
3318 : 0 : tracing_cpumask_write(struct file *filp, const char __user *ubuf,
3319 : : size_t count, loff_t *ppos)
3320 : : {
3321 : 0 : struct trace_array *tr = file_inode(filp)->i_private;
3322 : : cpumask_var_t tracing_cpumask_new;
3323 : : int err, cpu;
3324 : :
3325 : : if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
3326 : : return -ENOMEM;
3327 : :
3328 : : err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
3329 [ # # ]: 0 : if (err)
3330 : : goto err_unlock;
3331 : :
3332 : 0 : mutex_lock(&tracing_cpumask_update_lock);
3333 : :
3334 : : local_irq_disable();
3335 : : arch_spin_lock(&ftrace_max_lock);
3336 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
3337 : : /*
3338 : : * Increase/decrease the disabled counter if we are
3339 : : * about to flip a bit in the cpumask:
3340 : : */
3341 [ # # ][ # # ]: 0 : if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
3342 : : !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3343 : 0 : atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3344 : 0 : ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
3345 : : }
3346 [ # # ][ # # ]: 0 : if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
3347 : : cpumask_test_cpu(cpu, tracing_cpumask_new)) {
3348 : 0 : atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
3349 : 0 : ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
3350 : : }
3351 : : }
3352 : : arch_spin_unlock(&ftrace_max_lock);
3353 : : local_irq_enable();
3354 : :
3355 : : cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
3356 : :
3357 : 0 : mutex_unlock(&tracing_cpumask_update_lock);
3358 : : free_cpumask_var(tracing_cpumask_new);
3359 : :
3360 : 0 : return count;
3361 : :
3362 : : err_unlock:
3363 : : free_cpumask_var(tracing_cpumask_new);
3364 : :
3365 : : return err;
3366 : : }
3367 : :
3368 : : static const struct file_operations tracing_cpumask_fops = {
3369 : : .open = tracing_open_generic_tr,
3370 : : .read = tracing_cpumask_read,
3371 : : .write = tracing_cpumask_write,
3372 : : .release = tracing_release_generic_tr,
3373 : : .llseek = generic_file_llseek,
3374 : : };
3375 : :
3376 : 0 : static int tracing_trace_options_show(struct seq_file *m, void *v)
3377 : : {
3378 : : struct tracer_opt *trace_opts;
3379 : 0 : struct trace_array *tr = m->private;
3380 : : u32 tracer_flags;
3381 : : int i;
3382 : :
3383 : 0 : mutex_lock(&trace_types_lock);
3384 : 0 : tracer_flags = tr->current_trace->flags->val;
3385 : 0 : trace_opts = tr->current_trace->flags->opts;
3386 : :
3387 [ # # ]: 0 : for (i = 0; trace_options[i]; i++) {
3388 [ # # ]: 0 : if (trace_flags & (1 << i))
3389 : 0 : seq_printf(m, "%s\n", trace_options[i]);
3390 : : else
3391 : 0 : seq_printf(m, "no%s\n", trace_options[i]);
3392 : : }
3393 : :
3394 [ # # ]: 0 : for (i = 0; trace_opts[i].name; i++) {
3395 [ # # ]: 0 : if (tracer_flags & trace_opts[i].bit)
3396 : 0 : seq_printf(m, "%s\n", trace_opts[i].name);
3397 : : else
3398 : 0 : seq_printf(m, "no%s\n", trace_opts[i].name);
3399 : : }
3400 : 0 : mutex_unlock(&trace_types_lock);
3401 : :
3402 : 0 : return 0;
3403 : : }
3404 : :
3405 : 0 : static int __set_tracer_option(struct tracer *trace,
3406 : : struct tracer_flags *tracer_flags,
3407 : : struct tracer_opt *opts, int neg)
3408 : : {
3409 : : int ret;
3410 : :
3411 : 0 : ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
3412 [ # # ]: 0 : if (ret)
3413 : : return ret;
3414 : :
3415 [ # # ]: 0 : if (neg)
3416 : 0 : tracer_flags->val &= ~opts->bit;
3417 : : else
3418 : 0 : tracer_flags->val |= opts->bit;
3419 : : return 0;
3420 : : }
3421 : :
3422 : : /* Try to assign a tracer specific option */
3423 : 0 : static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
3424 : : {
3425 : 0 : struct tracer_flags *tracer_flags = trace->flags;
3426 : : struct tracer_opt *opts = NULL;
3427 : : int i;
3428 : :
3429 [ # # ]: 0 : for (i = 0; tracer_flags->opts[i].name; i++) {
3430 : : opts = &tracer_flags->opts[i];
3431 : :
3432 [ # # ]: 0 : if (strcmp(cmp, opts->name) == 0)
3433 : 0 : return __set_tracer_option(trace, trace->flags,
3434 : : opts, neg);
3435 : : }
3436 : :
3437 : : return -EINVAL;
3438 : : }
3439 : :
3440 : : /* Some tracers require overwrite to stay enabled */
3441 : 0 : int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
3442 : : {
3443 [ # # ][ # # ]: 0 : if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
[ # # ]
3444 : : return -1;
3445 : :
3446 : 0 : return 0;
3447 : : }
3448 : :
3449 : 0 : int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
3450 : : {
3451 : : /* do nothing if flag is already set */
3452 [ # # ]: 0 : if (!!(trace_flags & mask) == !!enabled)
3453 : : return 0;
3454 : :
3455 : : /* Give the tracer a chance to approve the change */
3456 [ # # ]: 0 : if (tr->current_trace->flag_changed)
3457 [ # # ]: 0 : if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
3458 : : return -EINVAL;
3459 : :
3460 [ # # ]: 0 : if (enabled)
3461 : 0 : trace_flags |= mask;
3462 : : else
3463 : 0 : trace_flags &= ~mask;
3464 : :
3465 [ # # ]: 0 : if (mask == TRACE_ITER_RECORD_CMD)
3466 : 0 : trace_event_enable_cmd_record(enabled);
3467 : :
3468 [ # # ]: 0 : if (mask == TRACE_ITER_OVERWRITE) {
3469 : 0 : ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
3470 : : #ifdef CONFIG_TRACER_MAX_TRACE
3471 : : ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
3472 : : #endif
3473 : : }
3474 : :
3475 [ # # ]: 0 : if (mask == TRACE_ITER_PRINTK)
3476 : 0 : trace_printk_start_stop_comm(enabled);
3477 : :
3478 : : return 0;
3479 : : }
3480 : :
3481 : 0 : static int trace_set_options(struct trace_array *tr, char *option)
3482 : : {
3483 : : char *cmp;
3484 : : int neg = 0;
3485 : : int ret = -ENODEV;
3486 : : int i;
3487 : :
3488 : : cmp = strstrip(option);
3489 : :
3490 [ # # ]: 0 : if (strncmp(cmp, "no", 2) == 0) {
3491 : : neg = 1;
3492 : 0 : cmp += 2;
3493 : : }
3494 : :
3495 : 0 : mutex_lock(&trace_types_lock);
3496 : :
3497 [ # # ]: 0 : for (i = 0; trace_options[i]; i++) {
3498 [ # # ]: 0 : if (strcmp(cmp, trace_options[i]) == 0) {
3499 : 0 : ret = set_tracer_flag(tr, 1 << i, !neg);
3500 : 0 : break;
3501 : : }
3502 : : }
3503 : :
3504 : : /* If no option could be set, test the specific tracer options */
3505 [ # # ]: 0 : if (!trace_options[i])
3506 : 0 : ret = set_tracer_option(tr->current_trace, cmp, neg);
3507 : :
3508 : 0 : mutex_unlock(&trace_types_lock);
3509 : :
3510 : 0 : return ret;
3511 : : }
3512 : :
3513 : : static ssize_t
3514 : 0 : tracing_trace_options_write(struct file *filp, const char __user *ubuf,
3515 : : size_t cnt, loff_t *ppos)
3516 : : {
3517 : 0 : struct seq_file *m = filp->private_data;
3518 : 0 : struct trace_array *tr = m->private;
3519 : : char buf[64];
3520 : : int ret;
3521 : :
3522 [ # # ]: 0 : if (cnt >= sizeof(buf))
3523 : : return -EINVAL;
3524 : :
3525 [ # # ]: 0 : if (copy_from_user(&buf, ubuf, cnt))
3526 : : return -EFAULT;
3527 : :
3528 : 0 : buf[cnt] = 0;
3529 : :
3530 : 0 : ret = trace_set_options(tr, buf);
3531 [ # # ]: 0 : if (ret < 0)
3532 : : return ret;
3533 : :
3534 : 0 : *ppos += cnt;
3535 : :
3536 : 0 : return cnt;
3537 : : }
3538 : :
3539 : 0 : static int tracing_trace_options_open(struct inode *inode, struct file *file)
3540 : : {
3541 : 0 : struct trace_array *tr = inode->i_private;
3542 : : int ret;
3543 : :
3544 [ # # ]: 0 : if (tracing_disabled)
3545 : : return -ENODEV;
3546 : :
3547 [ # # ]: 0 : if (trace_array_get(tr) < 0)
3548 : : return -ENODEV;
3549 : :
3550 : 0 : ret = single_open(file, tracing_trace_options_show, inode->i_private);
3551 [ # # ]: 0 : if (ret < 0)
3552 : 0 : trace_array_put(tr);
3553 : :
3554 : 0 : return ret;
3555 : : }
3556 : :
3557 : : static const struct file_operations tracing_iter_fops = {
3558 : : .open = tracing_trace_options_open,
3559 : : .read = seq_read,
3560 : : .llseek = seq_lseek,
3561 : : .release = tracing_single_release_tr,
3562 : : .write = tracing_trace_options_write,
3563 : : };
3564 : :
3565 : : static const char readme_msg[] =
3566 : : "tracing mini-HOWTO:\n\n"
3567 : : "# echo 0 > tracing_on : quick way to disable tracing\n"
3568 : : "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
3569 : : " Important files:\n"
3570 : : " trace\t\t\t- The static contents of the buffer\n"
3571 : : "\t\t\t To clear the buffer write into this file: echo > trace\n"
3572 : : " trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
3573 : : " current_tracer\t- function and latency tracers\n"
3574 : : " available_tracers\t- list of configured tracers for current_tracer\n"
3575 : : " buffer_size_kb\t- view and modify size of per cpu buffer\n"
3576 : : " buffer_total_size_kb - view total size of all cpu buffers\n\n"
3577 : : " trace_clock\t\t-change the clock used to order events\n"
3578 : : " local: Per cpu clock but may not be synced across CPUs\n"
3579 : : " global: Synced across CPUs but slows tracing down.\n"
3580 : : " counter: Not a clock, but just an increment\n"
3581 : : " uptime: Jiffy counter from time of boot\n"
3582 : : " perf: Same clock that perf events use\n"
3583 : : #ifdef CONFIG_X86_64
3584 : : " x86-tsc: TSC cycle counter\n"
3585 : : #endif
3586 : : "\n trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
3587 : : " tracing_cpumask\t- Limit which CPUs to trace\n"
3588 : : " instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
3589 : : "\t\t\t Remove sub-buffer with rmdir\n"
3590 : : " trace_options\t\t- Set format or modify how tracing happens\n"
3591 : : "\t\t\t Disable an option by adding a suffix 'no' to the\n"
3592 : : "\t\t\t option name\n"
3593 : : #ifdef CONFIG_DYNAMIC_FTRACE
3594 : : "\n available_filter_functions - list of functions that can be filtered on\n"
3595 : : " set_ftrace_filter\t- echo function name in here to only trace these\n"
3596 : : "\t\t\t functions\n"
3597 : : "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3598 : : "\t modules: Can select a group via module\n"
3599 : : "\t Format: :mod:<module-name>\n"
3600 : : "\t example: echo :mod:ext3 > set_ftrace_filter\n"
3601 : : "\t triggers: a command to perform when function is hit\n"
3602 : : "\t Format: <function>:<trigger>[:count]\n"
3603 : : "\t trigger: traceon, traceoff\n"
3604 : : "\t\t enable_event:<system>:<event>\n"
3605 : : "\t\t disable_event:<system>:<event>\n"
3606 : : #ifdef CONFIG_STACKTRACE
3607 : : "\t\t stacktrace\n"
3608 : : #endif
3609 : : #ifdef CONFIG_TRACER_SNAPSHOT
3610 : : "\t\t snapshot\n"
3611 : : #endif
3612 : : "\t example: echo do_fault:traceoff > set_ftrace_filter\n"
3613 : : "\t echo do_trap:traceoff:3 > set_ftrace_filter\n"
3614 : : "\t The first one will disable tracing every time do_fault is hit\n"
3615 : : "\t The second will disable tracing at most 3 times when do_trap is hit\n"
3616 : : "\t The first time do trap is hit and it disables tracing, the\n"
3617 : : "\t counter will decrement to 2. If tracing is already disabled,\n"
3618 : : "\t the counter will not decrement. It only decrements when the\n"
3619 : : "\t trigger did work\n"
3620 : : "\t To remove trigger without count:\n"
3621 : : "\t echo '!<function>:<trigger> > set_ftrace_filter\n"
3622 : : "\t To remove trigger with a count:\n"
3623 : : "\t echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
3624 : : " set_ftrace_notrace\t- echo function name in here to never trace.\n"
3625 : : "\t accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
3626 : : "\t modules: Can select a group via module command :mod:\n"
3627 : : "\t Does not accept triggers\n"
3628 : : #endif /* CONFIG_DYNAMIC_FTRACE */
3629 : : #ifdef CONFIG_FUNCTION_TRACER
3630 : : " set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
3631 : : "\t\t (function)\n"
3632 : : #endif
3633 : : #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3634 : : " set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
3635 : : " max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
3636 : : #endif
3637 : : #ifdef CONFIG_TRACER_SNAPSHOT
3638 : : "\n snapshot\t\t- Like 'trace' but shows the content of the static\n"
3639 : : "\t\t\t snapshot buffer. Read the contents for more\n"
3640 : : "\t\t\t information\n"
3641 : : #endif
3642 : : #ifdef CONFIG_STACK_TRACER
3643 : : " stack_trace\t\t- Shows the max stack trace when active\n"
3644 : : " stack_max_size\t- Shows current max stack size that was traced\n"
3645 : : "\t\t\t Write into this file to reset the max size (trigger a\n"
3646 : : "\t\t\t new trace)\n"
3647 : : #ifdef CONFIG_DYNAMIC_FTRACE
3648 : : " stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
3649 : : "\t\t\t traces\n"
3650 : : #endif
3651 : : #endif /* CONFIG_STACK_TRACER */
3652 : : " events/\t\t- Directory containing all trace event subsystems:\n"
3653 : : " enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
3654 : : " events/<system>/\t- Directory containing all trace events for <system>:\n"
3655 : : " enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
3656 : : "\t\t\t events\n"
3657 : : " filter\t\t- If set, only events passing filter are traced\n"
3658 : : " events/<system>/<event>/\t- Directory containing control files for\n"
3659 : : "\t\t\t <event>:\n"
3660 : : " enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
3661 : : " filter\t\t- If set, only events passing filter are traced\n"
3662 : : " trigger\t\t- If set, a command to perform when event is hit\n"
3663 : : "\t Format: <trigger>[:count][if <filter>]\n"
3664 : : "\t trigger: traceon, traceoff\n"
3665 : : "\t enable_event:<system>:<event>\n"
3666 : : "\t disable_event:<system>:<event>\n"
3667 : : #ifdef CONFIG_STACKTRACE
3668 : : "\t\t stacktrace\n"
3669 : : #endif
3670 : : #ifdef CONFIG_TRACER_SNAPSHOT
3671 : : "\t\t snapshot\n"
3672 : : #endif
3673 : : "\t example: echo traceoff > events/block/block_unplug/trigger\n"
3674 : : "\t echo traceoff:3 > events/block/block_unplug/trigger\n"
3675 : : "\t echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
3676 : : "\t events/block/block_unplug/trigger\n"
3677 : : "\t The first disables tracing every time block_unplug is hit.\n"
3678 : : "\t The second disables tracing the first 3 times block_unplug is hit.\n"
3679 : : "\t The third enables the kmalloc event the first 3 times block_unplug\n"
3680 : : "\t is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
3681 : : "\t Like function triggers, the counter is only decremented if it\n"
3682 : : "\t enabled or disabled tracing.\n"
3683 : : "\t To remove a trigger without a count:\n"
3684 : : "\t echo '!<trigger> > <system>/<event>/trigger\n"
3685 : : "\t To remove a trigger with a count:\n"
3686 : : "\t echo '!<trigger>:0 > <system>/<event>/trigger\n"
3687 : : "\t Filters can be ignored when removing a trigger.\n"
3688 : : ;
3689 : :
3690 : : static ssize_t
3691 : 0 : tracing_readme_read(struct file *filp, char __user *ubuf,
3692 : : size_t cnt, loff_t *ppos)
3693 : : {
3694 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos,
3695 : : readme_msg, strlen(readme_msg));
3696 : : }
3697 : :
3698 : : static const struct file_operations tracing_readme_fops = {
3699 : : .open = tracing_open_generic,
3700 : : .read = tracing_readme_read,
3701 : : .llseek = generic_file_llseek,
3702 : : };
3703 : :
3704 : : static ssize_t
3705 : 0 : tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
3706 : : size_t cnt, loff_t *ppos)
3707 : : {
3708 : : char *buf_comm;
3709 : : char *file_buf;
3710 : : char *buf;
3711 : : int len = 0;
3712 : : int pid;
3713 : : int i;
3714 : :
3715 : : file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
3716 [ # # ]: 0 : if (!file_buf)
3717 : : return -ENOMEM;
3718 : :
3719 : : buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
3720 [ # # ]: 0 : if (!buf_comm) {
3721 : 0 : kfree(file_buf);
3722 : 0 : return -ENOMEM;
3723 : : }
3724 : :
3725 : : buf = file_buf;
3726 : :
3727 [ # # ]: 0 : for (i = 0; i < SAVED_CMDLINES; i++) {
3728 : : int r;
3729 : :
3730 : 0 : pid = map_cmdline_to_pid[i];
3731 [ # # ]: 0 : if (pid == -1 || pid == NO_CMDLINE_MAP)
3732 : 0 : continue;
3733 : :
3734 : 0 : trace_find_cmdline(pid, buf_comm);
3735 : 0 : r = sprintf(buf, "%d %s\n", pid, buf_comm);
3736 : 0 : buf += r;
3737 : 0 : len += r;
3738 : : }
3739 : :
3740 : 0 : len = simple_read_from_buffer(ubuf, cnt, ppos,
3741 : : file_buf, len);
3742 : :
3743 : 0 : kfree(file_buf);
3744 : 0 : kfree(buf_comm);
3745 : :
3746 : 0 : return len;
3747 : : }
3748 : :
3749 : : static const struct file_operations tracing_saved_cmdlines_fops = {
3750 : : .open = tracing_open_generic,
3751 : : .read = tracing_saved_cmdlines_read,
3752 : : .llseek = generic_file_llseek,
3753 : : };
3754 : :
3755 : : static ssize_t
3756 : 0 : tracing_saved_tgids_read(struct file *file, char __user *ubuf,
3757 : : size_t cnt, loff_t *ppos)
3758 : : {
3759 : : char *file_buf;
3760 : : char *buf;
3761 : : int len = 0;
3762 : : int pid;
3763 : : int i;
3764 : :
3765 : : file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
3766 [ # # ]: 0 : if (!file_buf)
3767 : : return -ENOMEM;
3768 : :
3769 : : buf = file_buf;
3770 : :
3771 [ # # ]: 0 : for (i = 0; i < SAVED_CMDLINES; i++) {
3772 : : int tgid;
3773 : : int r;
3774 : :
3775 : 0 : pid = map_cmdline_to_pid[i];
3776 [ # # ]: 0 : if (pid == -1 || pid == NO_CMDLINE_MAP)
3777 : 0 : continue;
3778 : :
3779 : 0 : tgid = trace_find_tgid(pid);
3780 : 0 : r = sprintf(buf, "%d %d\n", pid, tgid);
3781 : 0 : buf += r;
3782 : 0 : len += r;
3783 : : }
3784 : :
3785 : 0 : len = simple_read_from_buffer(ubuf, cnt, ppos,
3786 : : file_buf, len);
3787 : :
3788 : 0 : kfree(file_buf);
3789 : :
3790 : 0 : return len;
3791 : : }
3792 : :
3793 : : static const struct file_operations tracing_saved_tgids_fops = {
3794 : : .open = tracing_open_generic,
3795 : : .read = tracing_saved_tgids_read,
3796 : : .llseek = generic_file_llseek,
3797 : : };
3798 : :
3799 : : static ssize_t
3800 : 0 : tracing_set_trace_read(struct file *filp, char __user *ubuf,
3801 : : size_t cnt, loff_t *ppos)
3802 : : {
3803 : 0 : struct trace_array *tr = filp->private_data;
3804 : : char buf[MAX_TRACER_SIZE+2];
3805 : : int r;
3806 : :
3807 : 0 : mutex_lock(&trace_types_lock);
3808 : 0 : r = sprintf(buf, "%s\n", tr->current_trace->name);
3809 : 0 : mutex_unlock(&trace_types_lock);
3810 : :
3811 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3812 : : }
3813 : :
3814 : 0 : int tracer_init(struct tracer *t, struct trace_array *tr)
3815 : : {
3816 : 0 : tracing_reset_online_cpus(&tr->trace_buffer);
3817 : 0 : return t->init(tr);
3818 : : }
3819 : :
3820 : 0 : static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
3821 : : {
3822 : : int cpu;
3823 : :
3824 [ # # ]: 0 : for_each_tracing_cpu(cpu)
3825 : 0 : per_cpu_ptr(buf->data, cpu)->entries = val;
3826 : 0 : }
3827 : :
3828 : : #ifdef CONFIG_TRACER_MAX_TRACE
3829 : : /* resize @tr's buffer to the size of @size_tr's entries */
3830 : : static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
3831 : : struct trace_buffer *size_buf, int cpu_id)
3832 : : {
3833 : : int cpu, ret = 0;
3834 : :
3835 : : if (cpu_id == RING_BUFFER_ALL_CPUS) {
3836 : : for_each_tracing_cpu(cpu) {
3837 : : ret = ring_buffer_resize(trace_buf->buffer,
3838 : : per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
3839 : : if (ret < 0)
3840 : : break;
3841 : : per_cpu_ptr(trace_buf->data, cpu)->entries =
3842 : : per_cpu_ptr(size_buf->data, cpu)->entries;
3843 : : }
3844 : : } else {
3845 : : ret = ring_buffer_resize(trace_buf->buffer,
3846 : : per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
3847 : : if (ret == 0)
3848 : : per_cpu_ptr(trace_buf->data, cpu_id)->entries =
3849 : : per_cpu_ptr(size_buf->data, cpu_id)->entries;
3850 : : }
3851 : :
3852 : : return ret;
3853 : : }
3854 : : #endif /* CONFIG_TRACER_MAX_TRACE */
3855 : :
3856 : 0 : static int __tracing_resize_ring_buffer(struct trace_array *tr,
3857 : : unsigned long size, int cpu)
3858 : : {
3859 : : int ret;
3860 : :
3861 : : /*
3862 : : * If kernel or user changes the size of the ring buffer
3863 : : * we use the size that was given, and we can forget about
3864 : : * expanding it later.
3865 : : */
3866 : 0 : ring_buffer_expanded = true;
3867 : :
3868 : : /* May be called before buffers are initialized */
3869 [ # # ]: 0 : if (!tr->trace_buffer.buffer)
3870 : : return 0;
3871 : :
3872 : 0 : ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
3873 [ # # ]: 0 : if (ret < 0)
3874 : : return ret;
3875 : :
3876 : : #ifdef CONFIG_TRACER_MAX_TRACE
3877 : : if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
3878 : : !tr->current_trace->use_max_tr)
3879 : : goto out;
3880 : :
3881 : : ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
3882 : : if (ret < 0) {
3883 : : int r = resize_buffer_duplicate_size(&tr->trace_buffer,
3884 : : &tr->trace_buffer, cpu);
3885 : : if (r < 0) {
3886 : : /*
3887 : : * AARGH! We are left with different
3888 : : * size max buffer!!!!
3889 : : * The max buffer is our "snapshot" buffer.
3890 : : * When a tracer needs a snapshot (one of the
3891 : : * latency tracers), it swaps the max buffer
3892 : : * with the saved snap shot. We succeeded to
3893 : : * update the size of the main buffer, but failed to
3894 : : * update the size of the max buffer. But when we tried
3895 : : * to reset the main buffer to the original size, we
3896 : : * failed there too. This is very unlikely to
3897 : : * happen, but if it does, warn and kill all
3898 : : * tracing.
3899 : : */
3900 : : WARN_ON(1);
3901 : : tracing_disabled = 1;
3902 : : }
3903 : : return ret;
3904 : : }
3905 : :
3906 : : if (cpu == RING_BUFFER_ALL_CPUS)
3907 : : set_buffer_entries(&tr->max_buffer, size);
3908 : : else
3909 : : per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
3910 : :
3911 : : out:
3912 : : #endif /* CONFIG_TRACER_MAX_TRACE */
3913 : :
3914 [ # # ]: 0 : if (cpu == RING_BUFFER_ALL_CPUS)
3915 : 0 : set_buffer_entries(&tr->trace_buffer, size);
3916 : : else
3917 : 0 : per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
3918 : :
3919 : 0 : return ret;
3920 : : }
3921 : :
3922 : 0 : static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
3923 : : unsigned long size, int cpu_id)
3924 : : {
3925 : : int ret = size;
3926 : :
3927 : 0 : mutex_lock(&trace_types_lock);
3928 : :
3929 [ # # ]: 0 : if (cpu_id != RING_BUFFER_ALL_CPUS) {
3930 : : /* make sure, this cpu is enabled in the mask */
3931 [ # # ]: 0 : if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3932 : : ret = -EINVAL;
3933 : : goto out;
3934 : : }
3935 : : }
3936 : :
3937 : 0 : ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
3938 [ # # ]: 0 : if (ret < 0)
3939 : : ret = -ENOMEM;
3940 : :
3941 : : out:
3942 : 0 : mutex_unlock(&trace_types_lock);
3943 : :
3944 : 0 : return ret;
3945 : : }
3946 : :
3947 : :
3948 : : /**
3949 : : * tracing_update_buffers - used by tracing facility to expand ring buffers
3950 : : *
3951 : : * To save on memory when the tracing is never used on a system with it
3952 : : * configured in. The ring buffers are set to a minimum size. But once
3953 : : * a user starts to use the tracing facility, then they need to grow
3954 : : * to their default size.
3955 : : *
3956 : : * This function is to be called when a tracer is about to be used.
3957 : : */
3958 : 0 : int tracing_update_buffers(void)
3959 : : {
3960 : : int ret = 0;
3961 : :
3962 : 0 : mutex_lock(&trace_types_lock);
3963 [ # # ]: 0 : if (!ring_buffer_expanded)
3964 : 0 : ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
3965 : : RING_BUFFER_ALL_CPUS);
3966 : 0 : mutex_unlock(&trace_types_lock);
3967 : :
3968 : 0 : return ret;
3969 : : }
3970 : :
3971 : : struct trace_option_dentry;
3972 : :
3973 : : static struct trace_option_dentry *
3974 : : create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
3975 : :
3976 : : static void
3977 : : destroy_trace_option_files(struct trace_option_dentry *topts);
3978 : :
3979 : 0 : static int tracing_set_tracer(const char *buf)
3980 : : {
3981 : : static struct trace_option_dentry *topts;
3982 : : struct trace_array *tr = &global_trace;
3983 : : struct tracer *t;
3984 : : #ifdef CONFIG_TRACER_MAX_TRACE
3985 : : bool had_max_tr;
3986 : : #endif
3987 : : int ret = 0;
3988 : :
3989 : 0 : mutex_lock(&trace_types_lock);
3990 : :
3991 [ # # ]: 0 : if (!ring_buffer_expanded) {
3992 : 0 : ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
3993 : : RING_BUFFER_ALL_CPUS);
3994 [ # # ]: 0 : if (ret < 0)
3995 : : goto out;
3996 : : ret = 0;
3997 : : }
3998 : :
3999 [ # # ]: 0 : for (t = trace_types; t; t = t->next) {
4000 [ # # ]: 0 : if (strcmp(t->name, buf) == 0)
4001 : : break;
4002 : : }
4003 [ # # ]: 0 : if (!t) {
4004 : : ret = -EINVAL;
4005 : : goto out;
4006 : : }
4007 [ # # ]: 0 : if (t == tr->current_trace)
4008 : : goto out;
4009 : :
4010 : : trace_branch_disable();
4011 : :
4012 : 0 : tr->current_trace->enabled = false;
4013 : :
4014 [ # # ]: 0 : if (tr->current_trace->reset)
4015 : 0 : tr->current_trace->reset(tr);
4016 : :
4017 : : /* Current trace needs to be nop_trace before synchronize_sched */
4018 : 0 : tr->current_trace = &nop_trace;
4019 : :
4020 : : #ifdef CONFIG_TRACER_MAX_TRACE
4021 : : had_max_tr = tr->allocated_snapshot;
4022 : :
4023 : : if (had_max_tr && !t->use_max_tr) {
4024 : : /*
4025 : : * We need to make sure that the update_max_tr sees that
4026 : : * current_trace changed to nop_trace to keep it from
4027 : : * swapping the buffers after we resize it.
4028 : : * The update_max_tr is called from interrupts disabled
4029 : : * so a synchronized_sched() is sufficient.
4030 : : */
4031 : : synchronize_sched();
4032 : : free_snapshot(tr);
4033 : : }
4034 : : #endif
4035 : 0 : destroy_trace_option_files(topts);
4036 : :
4037 : 0 : topts = create_trace_option_files(tr, t);
4038 : :
4039 : : #ifdef CONFIG_TRACER_MAX_TRACE
4040 : : if (t->use_max_tr && !had_max_tr) {
4041 : : ret = alloc_snapshot(tr);
4042 : : if (ret < 0)
4043 : : goto out;
4044 : : }
4045 : : #endif
4046 : :
4047 [ # # ]: 0 : if (t->init) {
4048 : : ret = tracer_init(t, tr);
4049 [ # # ]: 0 : if (ret)
4050 : : goto out;
4051 : : }
4052 : :
4053 : 0 : tr->current_trace = t;
4054 : 0 : tr->current_trace->enabled = true;
4055 : : trace_branch_enable(tr);
4056 : : out:
4057 : 0 : mutex_unlock(&trace_types_lock);
4058 : :
4059 : 0 : return ret;
4060 : : }
4061 : :
4062 : : static ssize_t
4063 : 0 : tracing_set_trace_write(struct file *filp, const char __user *ubuf,
4064 : : size_t cnt, loff_t *ppos)
4065 : : {
4066 : : char buf[MAX_TRACER_SIZE+1];
4067 : : int i;
4068 : : size_t ret;
4069 : : int err;
4070 : :
4071 : : ret = cnt;
4072 : :
4073 [ # # ]: 0 : if (cnt > MAX_TRACER_SIZE)
4074 : : cnt = MAX_TRACER_SIZE;
4075 : :
4076 [ # # ]: 0 : if (copy_from_user(&buf, ubuf, cnt))
4077 : : return -EFAULT;
4078 : :
4079 : 0 : buf[cnt] = 0;
4080 : :
4081 : : /* strip ending whitespace. */
4082 [ # # ][ # # ]: 0 : for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
4083 : 0 : buf[i] = 0;
4084 : :
4085 : 0 : err = tracing_set_tracer(buf);
4086 [ # # ]: 0 : if (err)
4087 : : return err;
4088 : :
4089 : 0 : *ppos += ret;
4090 : :
4091 : 0 : return ret;
4092 : : }
4093 : :
4094 : : static ssize_t
4095 : 0 : tracing_max_lat_read(struct file *filp, char __user *ubuf,
4096 : : size_t cnt, loff_t *ppos)
4097 : : {
4098 : 0 : unsigned long *ptr = filp->private_data;
4099 : : char buf[64];
4100 : : int r;
4101 : :
4102 [ # # ]: 0 : r = snprintf(buf, sizeof(buf), "%ld\n",
4103 : 0 : *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
4104 [ # # ]: 0 : if (r > sizeof(buf))
4105 : : r = sizeof(buf);
4106 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4107 : : }
4108 : :
4109 : : static ssize_t
4110 : 0 : tracing_max_lat_write(struct file *filp, const char __user *ubuf,
4111 : : size_t cnt, loff_t *ppos)
4112 : : {
4113 : 0 : unsigned long *ptr = filp->private_data;
4114 : : unsigned long val;
4115 : : int ret;
4116 : :
4117 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4118 [ # # ]: 0 : if (ret)
4119 : : return ret;
4120 : :
4121 : 0 : *ptr = val * 1000;
4122 : :
4123 : 0 : return cnt;
4124 : : }
4125 : :
4126 : 0 : static int tracing_open_pipe(struct inode *inode, struct file *filp)
4127 : : {
4128 : 0 : struct trace_array *tr = inode->i_private;
4129 : : struct trace_iterator *iter;
4130 : : int ret = 0;
4131 : :
4132 [ # # ]: 0 : if (tracing_disabled)
4133 : : return -ENODEV;
4134 : :
4135 [ # # ]: 0 : if (trace_array_get(tr) < 0)
4136 : : return -ENODEV;
4137 : :
4138 : 0 : mutex_lock(&trace_types_lock);
4139 : :
4140 : : /* create a buffer to store the information to pass to userspace */
4141 : : iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4142 [ # # ]: 0 : if (!iter) {
4143 : : ret = -ENOMEM;
4144 : 0 : __trace_array_put(tr);
4145 : 0 : goto out;
4146 : : }
4147 : :
4148 : : /*
4149 : : * We make a copy of the current tracer to avoid concurrent
4150 : : * changes on it while we are reading.
4151 : : */
4152 : 0 : iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
4153 [ # # ]: 0 : if (!iter->trace) {
4154 : : ret = -ENOMEM;
4155 : : goto fail;
4156 : : }
4157 : 0 : *iter->trace = *tr->current_trace;
4158 : :
4159 : : if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
4160 : : ret = -ENOMEM;
4161 : : goto fail;
4162 : : }
4163 : :
4164 : : /* trace pipe does not show start of buffer */
4165 : : cpumask_setall(iter->started);
4166 : :
4167 [ # # ]: 0 : if (trace_flags & TRACE_ITER_LATENCY_FMT)
4168 : 0 : iter->iter_flags |= TRACE_FILE_LAT_FMT;
4169 : :
4170 : : /* Output in nanoseconds only if we are using a clock in nanoseconds. */
4171 [ # # ]: 0 : if (trace_clocks[tr->clock_id].in_ns)
4172 : 0 : iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4173 : :
4174 : 0 : iter->tr = tr;
4175 : 0 : iter->trace_buffer = &tr->trace_buffer;
4176 : 0 : iter->cpu_file = tracing_get_cpu(inode);
4177 : 0 : mutex_init(&iter->mutex);
4178 : 0 : filp->private_data = iter;
4179 : :
4180 [ # # ]: 0 : if (iter->trace->pipe_open)
4181 : 0 : iter->trace->pipe_open(iter);
4182 : :
4183 : 0 : nonseekable_open(inode, filp);
4184 : : out:
4185 : 0 : mutex_unlock(&trace_types_lock);
4186 : 0 : return ret;
4187 : :
4188 : : fail:
4189 : 0 : kfree(iter->trace);
4190 : 0 : kfree(iter);
4191 : 0 : __trace_array_put(tr);
4192 : 0 : mutex_unlock(&trace_types_lock);
4193 : 0 : return ret;
4194 : : }
4195 : :
4196 : 0 : static int tracing_release_pipe(struct inode *inode, struct file *file)
4197 : : {
4198 : 0 : struct trace_iterator *iter = file->private_data;
4199 : 0 : struct trace_array *tr = inode->i_private;
4200 : :
4201 : 0 : mutex_lock(&trace_types_lock);
4202 : :
4203 [ # # ]: 0 : if (iter->trace->pipe_close)
4204 : 0 : iter->trace->pipe_close(iter);
4205 : :
4206 : 0 : mutex_unlock(&trace_types_lock);
4207 : :
4208 : : free_cpumask_var(iter->started);
4209 : : mutex_destroy(&iter->mutex);
4210 : 0 : kfree(iter->trace);
4211 : 0 : kfree(iter);
4212 : :
4213 : 0 : trace_array_put(tr);
4214 : :
4215 : 0 : return 0;
4216 : : }
4217 : :
4218 : : static unsigned int
4219 : 0 : trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
4220 : : {
4221 : : /* Iterators are static, they should be filled or empty */
4222 [ # # ]: 0 : if (trace_buffer_iter(iter, iter->cpu_file))
4223 : : return POLLIN | POLLRDNORM;
4224 : :
4225 [ # # ]: 0 : if (trace_flags & TRACE_ITER_BLOCK)
4226 : : /*
4227 : : * Always select as readable when in blocking mode
4228 : : */
4229 : : return POLLIN | POLLRDNORM;
4230 : : else
4231 : 0 : return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
4232 : : filp, poll_table);
4233 : : }
4234 : :
4235 : : static unsigned int
4236 : 0 : tracing_poll_pipe(struct file *filp, poll_table *poll_table)
4237 : : {
4238 : 0 : struct trace_iterator *iter = filp->private_data;
4239 : :
4240 : 0 : return trace_poll(iter, filp, poll_table);
4241 : : }
4242 : :
4243 : : /*
4244 : : * This is a make-shift waitqueue.
4245 : : * A tracer might use this callback on some rare cases:
4246 : : *
4247 : : * 1) the current tracer might hold the runqueue lock when it wakes up
4248 : : * a reader, hence a deadlock (sched, function, and function graph tracers)
4249 : : * 2) the function tracers, trace all functions, we don't want
4250 : : * the overhead of calling wake_up and friends
4251 : : * (and tracing them too)
4252 : : *
4253 : : * Anyway, this is really very primitive wakeup.
4254 : : */
4255 : 0 : void poll_wait_pipe(struct trace_iterator *iter)
4256 : : {
4257 : 0 : set_current_state(TASK_INTERRUPTIBLE);
4258 : : /* sleep for 100 msecs, and try again. */
4259 : 0 : schedule_timeout(HZ / 10);
4260 : 0 : }
4261 : :
4262 : : /* Must be called with trace_types_lock mutex held. */
4263 : 0 : static int tracing_wait_pipe(struct file *filp)
4264 : : {
4265 : 0 : struct trace_iterator *iter = filp->private_data;
4266 : :
4267 [ # # ]: 0 : while (trace_empty(iter)) {
4268 : :
4269 [ # # ]: 0 : if ((filp->f_flags & O_NONBLOCK)) {
4270 : : return -EAGAIN;
4271 : : }
4272 : :
4273 : 0 : mutex_unlock(&iter->mutex);
4274 : :
4275 : 0 : iter->trace->wait_pipe(iter);
4276 : :
4277 : 0 : mutex_lock(&iter->mutex);
4278 : :
4279 [ # # ]: 0 : if (signal_pending(current))
4280 : : return -EINTR;
4281 : :
4282 : : /*
4283 : : * We block until we read something and tracing is disabled.
4284 : : * We still block if tracing is disabled, but we have never
4285 : : * read anything. This allows a user to cat this file, and
4286 : : * then enable tracing. But after we have read something,
4287 : : * we give an EOF when tracing is again disabled.
4288 : : *
4289 : : * iter->pos will be 0 if we haven't read anything.
4290 : : */
4291 [ # # ][ # # ]: 0 : if (!tracing_is_on() && iter->pos)
4292 : : break;
4293 : : }
4294 : :
4295 : : return 1;
4296 : : }
4297 : :
4298 : : /*
4299 : : * Consumer reader.
4300 : : */
4301 : : static ssize_t
4302 : 0 : tracing_read_pipe(struct file *filp, char __user *ubuf,
4303 : : size_t cnt, loff_t *ppos)
4304 : : {
4305 : 0 : struct trace_iterator *iter = filp->private_data;
4306 : 0 : struct trace_array *tr = iter->tr;
4307 : : ssize_t sret;
4308 : :
4309 : : /* return any leftover data */
4310 : 0 : sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4311 [ # # ]: 0 : if (sret != -EBUSY)
4312 : : return sret;
4313 : :
4314 : : trace_seq_init(&iter->seq);
4315 : :
4316 : : /* copy the tracer to avoid using a global lock all around */
4317 : 0 : mutex_lock(&trace_types_lock);
4318 [ # # ]: 0 : if (unlikely(iter->trace->name != tr->current_trace->name))
4319 : 0 : *iter->trace = *tr->current_trace;
4320 : 0 : mutex_unlock(&trace_types_lock);
4321 : :
4322 : : /*
4323 : : * Avoid more than one consumer on a single file descriptor
4324 : : * This is just a matter of traces coherency, the ring buffer itself
4325 : : * is protected.
4326 : : */
4327 : 0 : mutex_lock(&iter->mutex);
4328 [ # # ]: 0 : if (iter->trace->read) {
4329 : 0 : sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
4330 [ # # ]: 0 : if (sret)
4331 : : goto out;
4332 : : }
4333 : :
4334 : : waitagain:
4335 : 0 : sret = tracing_wait_pipe(filp);
4336 [ # # ]: 0 : if (sret <= 0)
4337 : : goto out;
4338 : :
4339 : : /* stop when tracing is finished */
4340 [ # # ]: 0 : if (trace_empty(iter)) {
4341 : : sret = 0;
4342 : : goto out;
4343 : : }
4344 : :
4345 [ # # ]: 0 : if (cnt >= PAGE_SIZE)
4346 : : cnt = PAGE_SIZE - 1;
4347 : :
4348 : : /* reset all but tr, trace, and overruns */
4349 : 0 : memset(&iter->seq, 0,
4350 : : sizeof(struct trace_iterator) -
4351 : : offsetof(struct trace_iterator, seq));
4352 : : cpumask_clear(iter->started);
4353 : 0 : iter->pos = -1;
4354 : :
4355 : 0 : trace_event_read_lock();
4356 : 0 : trace_access_lock(iter->cpu_file);
4357 [ # # ]: 0 : while (trace_find_next_entry_inc(iter) != NULL) {
4358 : : enum print_line_t ret;
4359 : 0 : int len = iter->seq.len;
4360 : :
4361 : 0 : ret = print_trace_line(iter);
4362 [ # # ]: 0 : if (ret == TRACE_TYPE_PARTIAL_LINE) {
4363 : : /* don't print partial lines */
4364 : 0 : iter->seq.len = len;
4365 : 0 : break;
4366 : : }
4367 [ # # ]: 0 : if (ret != TRACE_TYPE_NO_CONSUME)
4368 : : trace_consume(iter);
4369 : :
4370 [ # # ]: 0 : if (iter->seq.len >= cnt)
4371 : : break;
4372 : :
4373 : : /*
4374 : : * Setting the full flag means we reached the trace_seq buffer
4375 : : * size and we should leave by partial output condition above.
4376 : : * One of the trace_seq_* functions is not used properly.
4377 : : */
4378 [ # # ][ # # ]: 0 : WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
[ # # ]
4379 : : iter->ent->type);
4380 : : }
4381 : 0 : trace_access_unlock(iter->cpu_file);
4382 : 0 : trace_event_read_unlock();
4383 : :
4384 : : /* Now copy what we have to the user */
4385 : 0 : sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
4386 [ # # ]: 0 : if (iter->seq.readpos >= iter->seq.len)
4387 : : trace_seq_init(&iter->seq);
4388 : :
4389 : : /*
4390 : : * If there was nothing to send to user, in spite of consuming trace
4391 : : * entries, go back to wait for more entries.
4392 : : */
4393 [ # # ]: 0 : if (sret == -EBUSY)
4394 : : goto waitagain;
4395 : :
4396 : : out:
4397 : 0 : mutex_unlock(&iter->mutex);
4398 : :
4399 : 0 : return sret;
4400 : : }
4401 : :
4402 : 0 : static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
4403 : : unsigned int idx)
4404 : : {
4405 : 0 : __free_page(spd->pages[idx]);
4406 : 0 : }
4407 : :
4408 : : static const struct pipe_buf_operations tracing_pipe_buf_ops = {
4409 : : .can_merge = 0,
4410 : : .map = generic_pipe_buf_map,
4411 : : .unmap = generic_pipe_buf_unmap,
4412 : : .confirm = generic_pipe_buf_confirm,
4413 : : .release = generic_pipe_buf_release,
4414 : : .steal = generic_pipe_buf_steal,
4415 : : .get = generic_pipe_buf_get,
4416 : : };
4417 : :
4418 : : static size_t
4419 : 0 : tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
4420 : : {
4421 : : size_t count;
4422 : : int ret;
4423 : :
4424 : : /* Seq buffer is page-sized, exactly what we need. */
4425 : : for (;;) {
4426 : 0 : count = iter->seq.len;
4427 : 0 : ret = print_trace_line(iter);
4428 : 0 : count = iter->seq.len - count;
4429 [ # # ]: 0 : if (rem < count) {
4430 : : rem = 0;
4431 : 0 : iter->seq.len -= count;
4432 : 0 : break;
4433 : : }
4434 [ # # ]: 0 : if (ret == TRACE_TYPE_PARTIAL_LINE) {
4435 : 0 : iter->seq.len -= count;
4436 : 0 : break;
4437 : : }
4438 : :
4439 [ # # ]: 0 : if (ret != TRACE_TYPE_NO_CONSUME)
4440 : : trace_consume(iter);
4441 : 0 : rem -= count;
4442 [ # # ]: 0 : if (!trace_find_next_entry_inc(iter)) {
4443 : : rem = 0;
4444 : 0 : iter->ent = NULL;
4445 : 0 : break;
4446 : : }
4447 : : }
4448 : :
4449 : 0 : return rem;
4450 : : }
4451 : :
4452 : 0 : static ssize_t tracing_splice_read_pipe(struct file *filp,
4453 : : loff_t *ppos,
4454 : : struct pipe_inode_info *pipe,
4455 : : size_t len,
4456 : : unsigned int flags)
4457 : : {
4458 : : struct page *pages_def[PIPE_DEF_BUFFERS];
4459 : : struct partial_page partial_def[PIPE_DEF_BUFFERS];
4460 : 0 : struct trace_iterator *iter = filp->private_data;
4461 : 0 : struct splice_pipe_desc spd = {
4462 : : .pages = pages_def,
4463 : : .partial = partial_def,
4464 : : .nr_pages = 0, /* This gets updated below. */
4465 : : .nr_pages_max = PIPE_DEF_BUFFERS,
4466 : : .flags = flags,
4467 : : .ops = &tracing_pipe_buf_ops,
4468 : : .spd_release = tracing_spd_release_pipe,
4469 : : };
4470 : 0 : struct trace_array *tr = iter->tr;
4471 : : ssize_t ret;
4472 : : size_t rem;
4473 : : unsigned int i;
4474 : :
4475 [ # # ]: 0 : if (splice_grow_spd(pipe, &spd))
4476 : : return -ENOMEM;
4477 : :
4478 : : /* copy the tracer to avoid using a global lock all around */
4479 : 0 : mutex_lock(&trace_types_lock);
4480 [ # # ]: 0 : if (unlikely(iter->trace->name != tr->current_trace->name))
4481 : 0 : *iter->trace = *tr->current_trace;
4482 : 0 : mutex_unlock(&trace_types_lock);
4483 : :
4484 : 0 : mutex_lock(&iter->mutex);
4485 : :
4486 [ # # ]: 0 : if (iter->trace->splice_read) {
4487 : 0 : ret = iter->trace->splice_read(iter, filp,
4488 : : ppos, pipe, len, flags);
4489 [ # # ]: 0 : if (ret)
4490 : : goto out_err;
4491 : : }
4492 : :
4493 : 0 : ret = tracing_wait_pipe(filp);
4494 [ # # ]: 0 : if (ret <= 0)
4495 : : goto out_err;
4496 : :
4497 [ # # ][ # # ]: 0 : if (!iter->ent && !trace_find_next_entry_inc(iter)) {
4498 : : ret = -EFAULT;
4499 : : goto out_err;
4500 : : }
4501 : :
4502 : 0 : trace_event_read_lock();
4503 : 0 : trace_access_lock(iter->cpu_file);
4504 : :
4505 : : /* Fill as many pages as possible. */
4506 [ # # ][ # # ]: 0 : for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
4507 : 0 : spd.pages[i] = alloc_page(GFP_KERNEL);
4508 [ # # ]: 0 : if (!spd.pages[i])
4509 : : break;
4510 : :
4511 : 0 : rem = tracing_fill_pipe_page(rem, iter);
4512 : :
4513 : : /* Copy the data into the page, so we can start over. */
4514 : 0 : ret = trace_seq_to_buffer(&iter->seq,
4515 : 0 : page_address(spd.pages[i]),
4516 : : iter->seq.len);
4517 [ # # ]: 0 : if (ret < 0) {
4518 : 0 : __free_page(spd.pages[i]);
4519 : 0 : break;
4520 : : }
4521 : 0 : spd.partial[i].offset = 0;
4522 : 0 : spd.partial[i].len = iter->seq.len;
4523 : :
4524 : : trace_seq_init(&iter->seq);
4525 : : }
4526 : :
4527 : 0 : trace_access_unlock(iter->cpu_file);
4528 : 0 : trace_event_read_unlock();
4529 : 0 : mutex_unlock(&iter->mutex);
4530 : :
4531 : 0 : spd.nr_pages = i;
4532 : :
4533 : 0 : ret = splice_to_pipe(pipe, &spd);
4534 : : out:
4535 : 0 : splice_shrink_spd(&spd);
4536 : 0 : return ret;
4537 : :
4538 : : out_err:
4539 : 0 : mutex_unlock(&iter->mutex);
4540 : 0 : goto out;
4541 : : }
4542 : :
4543 : : static ssize_t
4544 : 0 : tracing_entries_read(struct file *filp, char __user *ubuf,
4545 : : size_t cnt, loff_t *ppos)
4546 : : {
4547 : 0 : struct inode *inode = file_inode(filp);
4548 : 0 : struct trace_array *tr = inode->i_private;
4549 : : int cpu = tracing_get_cpu(inode);
4550 : : char buf[64];
4551 : : int r = 0;
4552 : : ssize_t ret;
4553 : :
4554 : 0 : mutex_lock(&trace_types_lock);
4555 : :
4556 [ # # ]: 0 : if (cpu == RING_BUFFER_ALL_CPUS) {
4557 : : int cpu, buf_size_same;
4558 : : unsigned long size;
4559 : :
4560 : : size = 0;
4561 : : buf_size_same = 1;
4562 : : /* check if all cpu sizes are same */
4563 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
4564 : : /* fill in the size from first enabled cpu */
4565 [ # # ]: 0 : if (size == 0)
4566 : 0 : size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
4567 [ # # ]: 0 : if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
4568 : : buf_size_same = 0;
4569 : : break;
4570 : : }
4571 : : }
4572 : :
4573 [ # # ]: 0 : if (buf_size_same) {
4574 [ # # ]: 0 : if (!ring_buffer_expanded)
4575 : 0 : r = sprintf(buf, "%lu (expanded: %lu)\n",
4576 : : size >> 10,
4577 : : trace_buf_size >> 10);
4578 : : else
4579 : 0 : r = sprintf(buf, "%lu\n", size >> 10);
4580 : : } else
4581 : 0 : r = sprintf(buf, "X\n");
4582 : : } else
4583 : 0 : r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
4584 : :
4585 : 0 : mutex_unlock(&trace_types_lock);
4586 : :
4587 : 0 : ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4588 : 0 : return ret;
4589 : : }
4590 : :
4591 : : static ssize_t
4592 : 0 : tracing_entries_write(struct file *filp, const char __user *ubuf,
4593 : : size_t cnt, loff_t *ppos)
4594 : : {
4595 : 0 : struct inode *inode = file_inode(filp);
4596 : 0 : struct trace_array *tr = inode->i_private;
4597 : : unsigned long val;
4598 : : int ret;
4599 : :
4600 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4601 [ # # ]: 0 : if (ret)
4602 : : return ret;
4603 : :
4604 : : /* must have at least 1 entry */
4605 [ # # ]: 0 : if (!val)
4606 : : return -EINVAL;
4607 : :
4608 : : /* value is in KB */
4609 : 0 : val <<= 10;
4610 : 0 : ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
4611 [ # # ]: 0 : if (ret < 0)
4612 : : return ret;
4613 : :
4614 : 0 : *ppos += cnt;
4615 : :
4616 : 0 : return cnt;
4617 : : }
4618 : :
4619 : : static ssize_t
4620 : 0 : tracing_total_entries_read(struct file *filp, char __user *ubuf,
4621 : : size_t cnt, loff_t *ppos)
4622 : : {
4623 : 0 : struct trace_array *tr = filp->private_data;
4624 : : char buf[64];
4625 : : int r, cpu;
4626 : : unsigned long size = 0, expanded_size = 0;
4627 : :
4628 : 0 : mutex_lock(&trace_types_lock);
4629 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
4630 : 0 : size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
4631 [ # # ]: 0 : if (!ring_buffer_expanded)
4632 : 0 : expanded_size += trace_buf_size >> 10;
4633 : : }
4634 [ # # ]: 0 : if (ring_buffer_expanded)
4635 : 0 : r = sprintf(buf, "%lu\n", size);
4636 : : else
4637 : 0 : r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
4638 : 0 : mutex_unlock(&trace_types_lock);
4639 : :
4640 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4641 : : }
4642 : :
4643 : : static ssize_t
4644 : 0 : tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
4645 : : size_t cnt, loff_t *ppos)
4646 : : {
4647 : : /*
4648 : : * There is no need to read what the user has written, this function
4649 : : * is just to make sure that there is no error when "echo" is used
4650 : : */
4651 : :
4652 : 0 : *ppos += cnt;
4653 : :
4654 : 0 : return cnt;
4655 : : }
4656 : :
4657 : : static int
4658 : 0 : tracing_free_buffer_release(struct inode *inode, struct file *filp)
4659 : : {
4660 : 0 : struct trace_array *tr = inode->i_private;
4661 : :
4662 : : /* disable tracing ? */
4663 [ # # ]: 0 : if (trace_flags & TRACE_ITER_STOP_ON_FREE)
4664 : : tracer_tracing_off(tr);
4665 : : /* resize the ring buffer to 0 */
4666 : 0 : tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
4667 : :
4668 : 0 : trace_array_put(tr);
4669 : :
4670 : 0 : return 0;
4671 : : }
4672 : :
4673 : : static ssize_t
4674 : 0 : tracing_mark_write(struct file *filp, const char __user *ubuf,
4675 : : size_t cnt, loff_t *fpos)
4676 : : {
4677 : 0 : unsigned long addr = (unsigned long)ubuf;
4678 : 0 : struct trace_array *tr = filp->private_data;
4679 : : struct ring_buffer_event *event;
4680 : : struct ring_buffer *buffer;
4681 : : struct print_entry *entry;
4682 : : unsigned long irq_flags;
4683 : : struct page *pages[2];
4684 : : void *map_page[2];
4685 : : int nr_pages = 1;
4686 : : ssize_t written;
4687 : : int offset;
4688 : : int size;
4689 : : int len;
4690 : : int ret;
4691 : : int i;
4692 : :
4693 [ # # ]: 0 : if (tracing_disabled)
4694 : : return -EINVAL;
4695 : :
4696 [ # # ]: 0 : if (!(trace_flags & TRACE_ITER_MARKERS))
4697 : : return -EINVAL;
4698 : :
4699 [ # # ]: 0 : if (cnt > TRACE_BUF_SIZE)
4700 : : cnt = TRACE_BUF_SIZE;
4701 : :
4702 : : /*
4703 : : * Userspace is injecting traces into the kernel trace buffer.
4704 : : * We want to be as non intrusive as possible.
4705 : : * To do so, we do not want to allocate any special buffers
4706 : : * or take any locks, but instead write the userspace data
4707 : : * straight into the ring buffer.
4708 : : *
4709 : : * First we need to pin the userspace buffer into memory,
4710 : : * which, most likely it is, because it just referenced it.
4711 : : * But there's no guarantee that it is. By using get_user_pages_fast()
4712 : : * and kmap_atomic/kunmap_atomic() we can get access to the
4713 : : * pages directly. We then write the data directly into the
4714 : : * ring buffer.
4715 : : */
4716 : : BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
4717 : :
4718 : : /* check if we cross pages */
4719 [ # # ]: 0 : if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
4720 : : nr_pages = 2;
4721 : :
4722 : 0 : offset = addr & (PAGE_SIZE - 1);
4723 : 0 : addr &= PAGE_MASK;
4724 : :
4725 : 0 : ret = get_user_pages_fast(addr, nr_pages, 0, pages);
4726 [ # # ]: 0 : if (ret < nr_pages) {
4727 [ # # ]: 0 : while (--ret >= 0)
4728 : 0 : put_page(pages[ret]);
4729 : : written = -EFAULT;
4730 : : goto out;
4731 : : }
4732 : :
4733 [ # # ]: 0 : for (i = 0; i < nr_pages; i++)
4734 : 0 : map_page[i] = kmap_atomic(pages[i]);
4735 : :
4736 : : local_save_flags(irq_flags);
4737 : 0 : size = sizeof(*entry) + cnt + 2; /* possible \n added */
4738 : 0 : buffer = tr->trace_buffer.buffer;
4739 : 0 : event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
4740 : : irq_flags, preempt_count());
4741 [ # # ]: 0 : if (!event) {
4742 : : /* Ring buffer disabled, return as if not open for write */
4743 : : written = -EBADF;
4744 : : goto out_unlock;
4745 : : }
4746 : :
4747 : 0 : entry = ring_buffer_event_data(event);
4748 : 0 : entry->ip = _THIS_IP_;
4749 : :
4750 [ # # ]: 0 : if (nr_pages == 2) {
4751 : 0 : len = PAGE_SIZE - offset;
4752 : 0 : memcpy(&entry->buf, map_page[0] + offset, len);
4753 : 0 : memcpy(&entry->buf[len], map_page[1], cnt - len);
4754 : : } else
4755 : 0 : memcpy(&entry->buf, map_page[0] + offset, cnt);
4756 : :
4757 [ # # ]: 0 : if (entry->buf[cnt - 1] != '\n') {
4758 : 0 : entry->buf[cnt] = '\n';
4759 : 0 : entry->buf[cnt + 1] = '\0';
4760 : : } else
4761 : 0 : entry->buf[cnt] = '\0';
4762 : :
4763 : : __buffer_unlock_commit(buffer, event);
4764 : :
4765 : 0 : written = cnt;
4766 : :
4767 : 0 : *fpos += written;
4768 : :
4769 : : out_unlock:
4770 [ # # ]: 0 : for (i = 0; i < nr_pages; i++){
4771 : 0 : kunmap_atomic(map_page[i]);
4772 : 0 : put_page(pages[i]);
4773 : : }
4774 : : out:
4775 : 0 : return written;
4776 : : }
4777 : :
4778 : 0 : static int tracing_clock_show(struct seq_file *m, void *v)
4779 : : {
4780 : 0 : struct trace_array *tr = m->private;
4781 : : int i;
4782 : :
4783 [ # # ]: 0 : for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
4784 [ # # ][ # # ]: 0 : seq_printf(m,
[ # # ]
4785 : : "%s%s%s%s", i ? " " : "",
4786 : 0 : i == tr->clock_id ? "[" : "", trace_clocks[i].name,
4787 : : i == tr->clock_id ? "]" : "");
4788 : 0 : seq_putc(m, '\n');
4789 : :
4790 : 0 : return 0;
4791 : : }
4792 : :
4793 : 0 : static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4794 : : size_t cnt, loff_t *fpos)
4795 : : {
4796 : 0 : struct seq_file *m = filp->private_data;
4797 : 0 : struct trace_array *tr = m->private;
4798 : : char buf[64];
4799 : : const char *clockstr;
4800 : : int i;
4801 : :
4802 [ # # ]: 0 : if (cnt >= sizeof(buf))
4803 : : return -EINVAL;
4804 : :
4805 [ # # ]: 0 : if (copy_from_user(&buf, ubuf, cnt))
4806 : : return -EFAULT;
4807 : :
4808 : 0 : buf[cnt] = 0;
4809 : :
4810 : : clockstr = strstrip(buf);
4811 : :
4812 [ # # ]: 0 : for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4813 [ # # ]: 0 : if (strcmp(trace_clocks[i].name, clockstr) == 0)
4814 : : break;
4815 : : }
4816 [ # # ]: 0 : if (i == ARRAY_SIZE(trace_clocks))
4817 : : return -EINVAL;
4818 : :
4819 : 0 : mutex_lock(&trace_types_lock);
4820 : :
4821 : 0 : tr->clock_id = i;
4822 : :
4823 : 0 : ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
4824 : :
4825 : : /*
4826 : : * New clock may not be consistent with the previous clock.
4827 : : * Reset the buffer so that it doesn't have incomparable timestamps.
4828 : : */
4829 : 0 : tracing_reset_online_cpus(&tr->trace_buffer);
4830 : :
4831 : : #ifdef CONFIG_TRACER_MAX_TRACE
4832 : : if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
4833 : : ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
4834 : : tracing_reset_online_cpus(&tr->max_buffer);
4835 : : #endif
4836 : :
4837 : 0 : mutex_unlock(&trace_types_lock);
4838 : :
4839 : 0 : *fpos += cnt;
4840 : :
4841 : 0 : return cnt;
4842 : : }
4843 : :
4844 : 0 : static int tracing_clock_open(struct inode *inode, struct file *file)
4845 : : {
4846 : 0 : struct trace_array *tr = inode->i_private;
4847 : : int ret;
4848 : :
4849 [ # # ]: 0 : if (tracing_disabled)
4850 : : return -ENODEV;
4851 : :
4852 [ # # ]: 0 : if (trace_array_get(tr))
4853 : : return -ENODEV;
4854 : :
4855 : 0 : ret = single_open(file, tracing_clock_show, inode->i_private);
4856 [ # # ]: 0 : if (ret < 0)
4857 : 0 : trace_array_put(tr);
4858 : :
4859 : 0 : return ret;
4860 : : }
4861 : :
4862 : : struct ftrace_buffer_info {
4863 : : struct trace_iterator iter;
4864 : : void *spare;
4865 : : unsigned int read;
4866 : : };
4867 : :
4868 : : #ifdef CONFIG_TRACER_SNAPSHOT
4869 : : static int tracing_snapshot_open(struct inode *inode, struct file *file)
4870 : : {
4871 : : struct trace_array *tr = inode->i_private;
4872 : : struct trace_iterator *iter;
4873 : : struct seq_file *m;
4874 : : int ret = 0;
4875 : :
4876 : : if (trace_array_get(tr) < 0)
4877 : : return -ENODEV;
4878 : :
4879 : : if (file->f_mode & FMODE_READ) {
4880 : : iter = __tracing_open(inode, file, true);
4881 : : if (IS_ERR(iter))
4882 : : ret = PTR_ERR(iter);
4883 : : } else {
4884 : : /* Writes still need the seq_file to hold the private data */
4885 : : ret = -ENOMEM;
4886 : : m = kzalloc(sizeof(*m), GFP_KERNEL);
4887 : : if (!m)
4888 : : goto out;
4889 : : iter = kzalloc(sizeof(*iter), GFP_KERNEL);
4890 : : if (!iter) {
4891 : : kfree(m);
4892 : : goto out;
4893 : : }
4894 : : ret = 0;
4895 : :
4896 : : iter->tr = tr;
4897 : : iter->trace_buffer = &tr->max_buffer;
4898 : : iter->cpu_file = tracing_get_cpu(inode);
4899 : : m->private = iter;
4900 : : file->private_data = m;
4901 : : }
4902 : : out:
4903 : : if (ret < 0)
4904 : : trace_array_put(tr);
4905 : :
4906 : : return ret;
4907 : : }
4908 : :
4909 : : static ssize_t
4910 : : tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
4911 : : loff_t *ppos)
4912 : : {
4913 : : struct seq_file *m = filp->private_data;
4914 : : struct trace_iterator *iter = m->private;
4915 : : struct trace_array *tr = iter->tr;
4916 : : unsigned long val;
4917 : : int ret;
4918 : :
4919 : : ret = tracing_update_buffers();
4920 : : if (ret < 0)
4921 : : return ret;
4922 : :
4923 : : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4924 : : if (ret)
4925 : : return ret;
4926 : :
4927 : : mutex_lock(&trace_types_lock);
4928 : :
4929 : : if (tr->current_trace->use_max_tr) {
4930 : : ret = -EBUSY;
4931 : : goto out;
4932 : : }
4933 : :
4934 : : switch (val) {
4935 : : case 0:
4936 : : if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4937 : : ret = -EINVAL;
4938 : : break;
4939 : : }
4940 : : if (tr->allocated_snapshot)
4941 : : free_snapshot(tr);
4942 : : break;
4943 : : case 1:
4944 : : /* Only allow per-cpu swap if the ring buffer supports it */
4945 : : #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
4946 : : if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4947 : : ret = -EINVAL;
4948 : : break;
4949 : : }
4950 : : #endif
4951 : : if (!tr->allocated_snapshot) {
4952 : : ret = alloc_snapshot(tr);
4953 : : if (ret < 0)
4954 : : break;
4955 : : }
4956 : : local_irq_disable();
4957 : : /* Now, we're going to swap */
4958 : : if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4959 : : update_max_tr(tr, current, smp_processor_id());
4960 : : else
4961 : : update_max_tr_single(tr, current, iter->cpu_file);
4962 : : local_irq_enable();
4963 : : break;
4964 : : default:
4965 : : if (tr->allocated_snapshot) {
4966 : : if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4967 : : tracing_reset_online_cpus(&tr->max_buffer);
4968 : : else
4969 : : tracing_reset(&tr->max_buffer, iter->cpu_file);
4970 : : }
4971 : : break;
4972 : : }
4973 : :
4974 : : if (ret >= 0) {
4975 : : *ppos += cnt;
4976 : : ret = cnt;
4977 : : }
4978 : : out:
4979 : : mutex_unlock(&trace_types_lock);
4980 : : return ret;
4981 : : }
4982 : :
4983 : : static int tracing_snapshot_release(struct inode *inode, struct file *file)
4984 : : {
4985 : : struct seq_file *m = file->private_data;
4986 : : int ret;
4987 : :
4988 : : ret = tracing_release(inode, file);
4989 : :
4990 : : if (file->f_mode & FMODE_READ)
4991 : : return ret;
4992 : :
4993 : : /* If write only, the seq_file is just a stub */
4994 : : if (m)
4995 : : kfree(m->private);
4996 : : kfree(m);
4997 : :
4998 : : return 0;
4999 : : }
5000 : :
5001 : : static int tracing_buffers_open(struct inode *inode, struct file *filp);
5002 : : static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
5003 : : size_t count, loff_t *ppos);
5004 : : static int tracing_buffers_release(struct inode *inode, struct file *file);
5005 : : static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5006 : : struct pipe_inode_info *pipe, size_t len, unsigned int flags);
5007 : :
5008 : : static int snapshot_raw_open(struct inode *inode, struct file *filp)
5009 : : {
5010 : : struct ftrace_buffer_info *info;
5011 : : int ret;
5012 : :
5013 : : ret = tracing_buffers_open(inode, filp);
5014 : : if (ret < 0)
5015 : : return ret;
5016 : :
5017 : : info = filp->private_data;
5018 : :
5019 : : if (info->iter.trace->use_max_tr) {
5020 : : tracing_buffers_release(inode, filp);
5021 : : return -EBUSY;
5022 : : }
5023 : :
5024 : : info->iter.snapshot = true;
5025 : : info->iter.trace_buffer = &info->iter.tr->max_buffer;
5026 : :
5027 : : return ret;
5028 : : }
5029 : :
5030 : : #endif /* CONFIG_TRACER_SNAPSHOT */
5031 : :
5032 : :
5033 : : static const struct file_operations tracing_max_lat_fops = {
5034 : : .open = tracing_open_generic,
5035 : : .read = tracing_max_lat_read,
5036 : : .write = tracing_max_lat_write,
5037 : : .llseek = generic_file_llseek,
5038 : : };
5039 : :
5040 : : static const struct file_operations set_tracer_fops = {
5041 : : .open = tracing_open_generic,
5042 : : .read = tracing_set_trace_read,
5043 : : .write = tracing_set_trace_write,
5044 : : .llseek = generic_file_llseek,
5045 : : };
5046 : :
5047 : : static const struct file_operations tracing_pipe_fops = {
5048 : : .open = tracing_open_pipe,
5049 : : .poll = tracing_poll_pipe,
5050 : : .read = tracing_read_pipe,
5051 : : .splice_read = tracing_splice_read_pipe,
5052 : : .release = tracing_release_pipe,
5053 : : .llseek = no_llseek,
5054 : : };
5055 : :
5056 : : static const struct file_operations tracing_entries_fops = {
5057 : : .open = tracing_open_generic_tr,
5058 : : .read = tracing_entries_read,
5059 : : .write = tracing_entries_write,
5060 : : .llseek = generic_file_llseek,
5061 : : .release = tracing_release_generic_tr,
5062 : : };
5063 : :
5064 : : static const struct file_operations tracing_total_entries_fops = {
5065 : : .open = tracing_open_generic_tr,
5066 : : .read = tracing_total_entries_read,
5067 : : .llseek = generic_file_llseek,
5068 : : .release = tracing_release_generic_tr,
5069 : : };
5070 : :
5071 : : static const struct file_operations tracing_free_buffer_fops = {
5072 : : .open = tracing_open_generic_tr,
5073 : : .write = tracing_free_buffer_write,
5074 : : .release = tracing_free_buffer_release,
5075 : : };
5076 : :
5077 : : static const struct file_operations tracing_mark_fops = {
5078 : : .open = tracing_open_generic_tr,
5079 : : .write = tracing_mark_write,
5080 : : .llseek = generic_file_llseek,
5081 : : .release = tracing_release_generic_tr,
5082 : : };
5083 : :
5084 : : static const struct file_operations trace_clock_fops = {
5085 : : .open = tracing_clock_open,
5086 : : .read = seq_read,
5087 : : .llseek = seq_lseek,
5088 : : .release = tracing_single_release_tr,
5089 : : .write = tracing_clock_write,
5090 : : };
5091 : :
5092 : : #ifdef CONFIG_TRACER_SNAPSHOT
5093 : : static const struct file_operations snapshot_fops = {
5094 : : .open = tracing_snapshot_open,
5095 : : .read = seq_read,
5096 : : .write = tracing_snapshot_write,
5097 : : .llseek = tracing_lseek,
5098 : : .release = tracing_snapshot_release,
5099 : : };
5100 : :
5101 : : static const struct file_operations snapshot_raw_fops = {
5102 : : .open = snapshot_raw_open,
5103 : : .read = tracing_buffers_read,
5104 : : .release = tracing_buffers_release,
5105 : : .splice_read = tracing_buffers_splice_read,
5106 : : .llseek = no_llseek,
5107 : : };
5108 : :
5109 : : #endif /* CONFIG_TRACER_SNAPSHOT */
5110 : :
5111 : 0 : static int tracing_buffers_open(struct inode *inode, struct file *filp)
5112 : : {
5113 : 0 : struct trace_array *tr = inode->i_private;
5114 : : struct ftrace_buffer_info *info;
5115 : : int ret;
5116 : :
5117 [ # # ]: 0 : if (tracing_disabled)
5118 : : return -ENODEV;
5119 : :
5120 [ # # ]: 0 : if (trace_array_get(tr) < 0)
5121 : : return -ENODEV;
5122 : :
5123 : : info = kzalloc(sizeof(*info), GFP_KERNEL);
5124 [ # # ]: 0 : if (!info) {
5125 : 0 : trace_array_put(tr);
5126 : 0 : return -ENOMEM;
5127 : : }
5128 : :
5129 : 0 : mutex_lock(&trace_types_lock);
5130 : :
5131 : 0 : info->iter.tr = tr;
5132 : 0 : info->iter.cpu_file = tracing_get_cpu(inode);
5133 : 0 : info->iter.trace = tr->current_trace;
5134 : 0 : info->iter.trace_buffer = &tr->trace_buffer;
5135 : 0 : info->spare = NULL;
5136 : : /* Force reading ring buffer for first read */
5137 : 0 : info->read = (unsigned int)-1;
5138 : :
5139 : 0 : filp->private_data = info;
5140 : :
5141 : 0 : mutex_unlock(&trace_types_lock);
5142 : :
5143 : 0 : ret = nonseekable_open(inode, filp);
5144 [ # # ]: 0 : if (ret < 0)
5145 : 0 : trace_array_put(tr);
5146 : :
5147 : 0 : return ret;
5148 : : }
5149 : :
5150 : : static unsigned int
5151 : 0 : tracing_buffers_poll(struct file *filp, poll_table *poll_table)
5152 : : {
5153 : 0 : struct ftrace_buffer_info *info = filp->private_data;
5154 : 0 : struct trace_iterator *iter = &info->iter;
5155 : :
5156 : 0 : return trace_poll(iter, filp, poll_table);
5157 : : }
5158 : :
5159 : : static ssize_t
5160 : 0 : tracing_buffers_read(struct file *filp, char __user *ubuf,
5161 : : size_t count, loff_t *ppos)
5162 : : {
5163 : 0 : struct ftrace_buffer_info *info = filp->private_data;
5164 : 0 : struct trace_iterator *iter = &info->iter;
5165 : : ssize_t ret;
5166 : : ssize_t size;
5167 : :
5168 [ # # ]: 0 : if (!count)
5169 : : return 0;
5170 : :
5171 : 0 : mutex_lock(&trace_types_lock);
5172 : :
5173 : : #ifdef CONFIG_TRACER_MAX_TRACE
5174 : : if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5175 : : size = -EBUSY;
5176 : : goto out_unlock;
5177 : : }
5178 : : #endif
5179 : :
5180 [ # # ]: 0 : if (!info->spare)
5181 : 0 : info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
5182 : : iter->cpu_file);
5183 : : size = -ENOMEM;
5184 [ # # ]: 0 : if (!info->spare)
5185 : : goto out_unlock;
5186 : :
5187 : : /* Do we have previous read data to read? */
5188 [ # # ]: 0 : if (info->read < PAGE_SIZE)
5189 : : goto read;
5190 : :
5191 : : again:
5192 : 0 : trace_access_lock(iter->cpu_file);
5193 : 0 : ret = ring_buffer_read_page(iter->trace_buffer->buffer,
5194 : : &info->spare,
5195 : : count,
5196 : : iter->cpu_file, 0);
5197 : 0 : trace_access_unlock(iter->cpu_file);
5198 : :
5199 [ # # ]: 0 : if (ret < 0) {
5200 [ # # ]: 0 : if (trace_empty(iter)) {
5201 [ # # ]: 0 : if ((filp->f_flags & O_NONBLOCK)) {
5202 : : size = -EAGAIN;
5203 : : goto out_unlock;
5204 : : }
5205 : 0 : mutex_unlock(&trace_types_lock);
5206 : 0 : iter->trace->wait_pipe(iter);
5207 : 0 : mutex_lock(&trace_types_lock);
5208 [ # # ]: 0 : if (signal_pending(current)) {
5209 : : size = -EINTR;
5210 : : goto out_unlock;
5211 : : }
5212 : : goto again;
5213 : : }
5214 : : size = 0;
5215 : : goto out_unlock;
5216 : : }
5217 : :
5218 : 0 : info->read = 0;
5219 : : read:
5220 : 0 : size = PAGE_SIZE - info->read;
5221 [ # # ]: 0 : if (size > count)
5222 : 0 : size = count;
5223 : :
5224 : 0 : ret = copy_to_user(ubuf, info->spare + info->read, size);
5225 [ # # ]: 0 : if (ret == size) {
5226 : : size = -EFAULT;
5227 : : goto out_unlock;
5228 : : }
5229 : 0 : size -= ret;
5230 : :
5231 : 0 : *ppos += size;
5232 : 0 : info->read += size;
5233 : :
5234 : : out_unlock:
5235 : 0 : mutex_unlock(&trace_types_lock);
5236 : :
5237 : 0 : return size;
5238 : : }
5239 : :
5240 : 0 : static int tracing_buffers_release(struct inode *inode, struct file *file)
5241 : : {
5242 : 0 : struct ftrace_buffer_info *info = file->private_data;
5243 : : struct trace_iterator *iter = &info->iter;
5244 : :
5245 : 0 : mutex_lock(&trace_types_lock);
5246 : :
5247 : 0 : __trace_array_put(iter->tr);
5248 : :
5249 [ # # ]: 0 : if (info->spare)
5250 : 0 : ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
5251 : 0 : kfree(info);
5252 : :
5253 : 0 : mutex_unlock(&trace_types_lock);
5254 : :
5255 : 0 : return 0;
5256 : : }
5257 : :
5258 : : struct buffer_ref {
5259 : : struct ring_buffer *buffer;
5260 : : void *page;
5261 : : int ref;
5262 : : };
5263 : :
5264 : 0 : static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
5265 : : struct pipe_buffer *buf)
5266 : : {
5267 : 0 : struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5268 : :
5269 [ # # ]: 0 : if (--ref->ref)
5270 : 0 : return;
5271 : :
5272 : 0 : ring_buffer_free_read_page(ref->buffer, ref->page);
5273 : 0 : kfree(ref);
5274 : 0 : buf->private = 0;
5275 : : }
5276 : :
5277 : 0 : static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
5278 : : struct pipe_buffer *buf)
5279 : : {
5280 : 0 : struct buffer_ref *ref = (struct buffer_ref *)buf->private;
5281 : :
5282 : 0 : ref->ref++;
5283 : 0 : }
5284 : :
5285 : : /* Pipe buffer operations for a buffer. */
5286 : : static const struct pipe_buf_operations buffer_pipe_buf_ops = {
5287 : : .can_merge = 0,
5288 : : .map = generic_pipe_buf_map,
5289 : : .unmap = generic_pipe_buf_unmap,
5290 : : .confirm = generic_pipe_buf_confirm,
5291 : : .release = buffer_pipe_buf_release,
5292 : : .steal = generic_pipe_buf_steal,
5293 : : .get = buffer_pipe_buf_get,
5294 : : };
5295 : :
5296 : : /*
5297 : : * Callback from splice_to_pipe(), if we need to release some pages
5298 : : * at the end of the spd in case we error'ed out in filling the pipe.
5299 : : */
5300 : 0 : static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
5301 : : {
5302 : 0 : struct buffer_ref *ref =
5303 : 0 : (struct buffer_ref *)spd->partial[i].private;
5304 : :
5305 [ # # ]: 0 : if (--ref->ref)
5306 : 0 : return;
5307 : :
5308 : 0 : ring_buffer_free_read_page(ref->buffer, ref->page);
5309 : 0 : kfree(ref);
5310 : 0 : spd->partial[i].private = 0;
5311 : : }
5312 : :
5313 : : static ssize_t
5314 : 0 : tracing_buffers_splice_read(struct file *file, loff_t *ppos,
5315 : : struct pipe_inode_info *pipe, size_t len,
5316 : : unsigned int flags)
5317 : : {
5318 : 0 : struct ftrace_buffer_info *info = file->private_data;
5319 : 0 : struct trace_iterator *iter = &info->iter;
5320 : : struct partial_page partial_def[PIPE_DEF_BUFFERS];
5321 : : struct page *pages_def[PIPE_DEF_BUFFERS];
5322 : 0 : struct splice_pipe_desc spd = {
5323 : : .pages = pages_def,
5324 : : .partial = partial_def,
5325 : : .nr_pages_max = PIPE_DEF_BUFFERS,
5326 : : .flags = flags,
5327 : : .ops = &buffer_pipe_buf_ops,
5328 : : .spd_release = buffer_spd_release,
5329 : : };
5330 : : struct buffer_ref *ref;
5331 : : int entries, size, i;
5332 : : ssize_t ret;
5333 : :
5334 : 0 : mutex_lock(&trace_types_lock);
5335 : :
5336 : : #ifdef CONFIG_TRACER_MAX_TRACE
5337 : : if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
5338 : : ret = -EBUSY;
5339 : : goto out;
5340 : : }
5341 : : #endif
5342 : :
5343 [ # # ]: 0 : if (splice_grow_spd(pipe, &spd)) {
5344 : : ret = -ENOMEM;
5345 : : goto out;
5346 : : }
5347 : :
5348 [ # # ]: 0 : if (*ppos & (PAGE_SIZE - 1)) {
5349 : : ret = -EINVAL;
5350 : : goto out;
5351 : : }
5352 : :
5353 [ # # ]: 0 : if (len & (PAGE_SIZE - 1)) {
5354 [ # # ]: 0 : if (len < PAGE_SIZE) {
5355 : : ret = -EINVAL;
5356 : : goto out;
5357 : : }
5358 : 0 : len &= PAGE_MASK;
5359 : : }
5360 : :
5361 : : again:
5362 : 0 : trace_access_lock(iter->cpu_file);
5363 : 0 : entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5364 : :
5365 [ # # ][ # # ]: 0 : for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
5366 : : struct page *page;
5367 : : int r;
5368 : :
5369 : : ref = kzalloc(sizeof(*ref), GFP_KERNEL);
5370 [ # # ]: 0 : if (!ref)
5371 : : break;
5372 : :
5373 : 0 : ref->ref = 1;
5374 : 0 : ref->buffer = iter->trace_buffer->buffer;
5375 : 0 : ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
5376 [ # # ]: 0 : if (!ref->page) {
5377 : 0 : kfree(ref);
5378 : 0 : break;
5379 : : }
5380 : :
5381 : 0 : r = ring_buffer_read_page(ref->buffer, &ref->page,
5382 : : len, iter->cpu_file, 1);
5383 [ # # ]: 0 : if (r < 0) {
5384 : 0 : ring_buffer_free_read_page(ref->buffer, ref->page);
5385 : 0 : kfree(ref);
5386 : 0 : break;
5387 : : }
5388 : :
5389 : : /*
5390 : : * zero out any left over data, this is going to
5391 : : * user land.
5392 : : */
5393 : 0 : size = ring_buffer_page_len(ref->page);
5394 [ # # ]: 0 : if (size < PAGE_SIZE)
5395 [ # # ]: 0 : memset(ref->page + size, 0, PAGE_SIZE - size);
5396 : :
5397 : 0 : page = virt_to_page(ref->page);
5398 : :
5399 : 0 : spd.pages[i] = page;
5400 : 0 : spd.partial[i].len = PAGE_SIZE;
5401 : 0 : spd.partial[i].offset = 0;
5402 : 0 : spd.partial[i].private = (unsigned long)ref;
5403 : 0 : spd.nr_pages++;
5404 : 0 : *ppos += PAGE_SIZE;
5405 : :
5406 : 0 : entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
5407 : : }
5408 : :
5409 : 0 : trace_access_unlock(iter->cpu_file);
5410 : 0 : spd.nr_pages = i;
5411 : :
5412 : : /* did we read anything? */
5413 [ # # ]: 0 : if (!spd.nr_pages) {
5414 [ # # ][ # # ]: 0 : if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
5415 : : ret = -EAGAIN;
5416 : : goto out;
5417 : : }
5418 : 0 : mutex_unlock(&trace_types_lock);
5419 : 0 : iter->trace->wait_pipe(iter);
5420 : 0 : mutex_lock(&trace_types_lock);
5421 [ # # ]: 0 : if (signal_pending(current)) {
5422 : : ret = -EINTR;
5423 : : goto out;
5424 : : }
5425 : : goto again;
5426 : : }
5427 : :
5428 : 0 : ret = splice_to_pipe(pipe, &spd);
5429 : 0 : splice_shrink_spd(&spd);
5430 : : out:
5431 : 0 : mutex_unlock(&trace_types_lock);
5432 : :
5433 : 0 : return ret;
5434 : : }
5435 : :
5436 : : static const struct file_operations tracing_buffers_fops = {
5437 : : .open = tracing_buffers_open,
5438 : : .read = tracing_buffers_read,
5439 : : .poll = tracing_buffers_poll,
5440 : : .release = tracing_buffers_release,
5441 : : .splice_read = tracing_buffers_splice_read,
5442 : : .llseek = no_llseek,
5443 : : };
5444 : :
5445 : : static ssize_t
5446 : 0 : tracing_stats_read(struct file *filp, char __user *ubuf,
5447 : : size_t count, loff_t *ppos)
5448 : : {
5449 : 0 : struct inode *inode = file_inode(filp);
5450 : 0 : struct trace_array *tr = inode->i_private;
5451 : : struct trace_buffer *trace_buf = &tr->trace_buffer;
5452 : : int cpu = tracing_get_cpu(inode);
5453 : : struct trace_seq *s;
5454 : : unsigned long cnt;
5455 : : unsigned long long t;
5456 : : unsigned long usec_rem;
5457 : :
5458 : : s = kmalloc(sizeof(*s), GFP_KERNEL);
5459 [ # # ]: 0 : if (!s)
5460 : : return -ENOMEM;
5461 : :
5462 : : trace_seq_init(s);
5463 : :
5464 : 0 : cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
5465 : 0 : trace_seq_printf(s, "entries: %ld\n", cnt);
5466 : :
5467 : 0 : cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
5468 : 0 : trace_seq_printf(s, "overrun: %ld\n", cnt);
5469 : :
5470 : 0 : cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
5471 : 0 : trace_seq_printf(s, "commit overrun: %ld\n", cnt);
5472 : :
5473 : 0 : cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
5474 : 0 : trace_seq_printf(s, "bytes: %ld\n", cnt);
5475 : :
5476 [ # # ]: 0 : if (trace_clocks[tr->clock_id].in_ns) {
5477 : : /* local or global for trace_clock */
5478 : 0 : t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5479 : 0 : usec_rem = do_div(t, USEC_PER_SEC);
5480 : 0 : trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
5481 : : t, usec_rem);
5482 : :
5483 : 0 : t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
5484 : 0 : usec_rem = do_div(t, USEC_PER_SEC);
5485 : 0 : trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
5486 : : } else {
5487 : : /* counter or tsc mode for trace_clock */
5488 : 0 : trace_seq_printf(s, "oldest event ts: %llu\n",
5489 : : ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
5490 : :
5491 : 0 : trace_seq_printf(s, "now ts: %llu\n",
5492 : : ring_buffer_time_stamp(trace_buf->buffer, cpu));
5493 : : }
5494 : :
5495 : 0 : cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
5496 : 0 : trace_seq_printf(s, "dropped events: %ld\n", cnt);
5497 : :
5498 : 0 : cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
5499 : 0 : trace_seq_printf(s, "read events: %ld\n", cnt);
5500 : :
5501 : 0 : count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
5502 : :
5503 : 0 : kfree(s);
5504 : :
5505 : 0 : return count;
5506 : : }
5507 : :
5508 : : static const struct file_operations tracing_stats_fops = {
5509 : : .open = tracing_open_generic_tr,
5510 : : .read = tracing_stats_read,
5511 : : .llseek = generic_file_llseek,
5512 : : .release = tracing_release_generic_tr,
5513 : : };
5514 : :
5515 : : #ifdef CONFIG_DYNAMIC_FTRACE
5516 : :
5517 : 0 : int __weak ftrace_arch_read_dyn_info(char *buf, int size)
5518 : : {
5519 : 0 : return 0;
5520 : : }
5521 : :
5522 : : static ssize_t
5523 : 0 : tracing_read_dyn_info(struct file *filp, char __user *ubuf,
5524 : : size_t cnt, loff_t *ppos)
5525 : : {
5526 : : static char ftrace_dyn_info_buffer[1024];
5527 : : static DEFINE_MUTEX(dyn_info_mutex);
5528 : 0 : unsigned long *p = filp->private_data;
5529 : : char *buf = ftrace_dyn_info_buffer;
5530 : : int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
5531 : : int r;
5532 : :
5533 : 0 : mutex_lock(&dyn_info_mutex);
5534 : 0 : r = sprintf(buf, "%ld ", *p);
5535 : :
5536 : 0 : r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
5537 : 0 : buf[r++] = '\n';
5538 : :
5539 : 0 : r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
5540 : :
5541 : 0 : mutex_unlock(&dyn_info_mutex);
5542 : :
5543 : 0 : return r;
5544 : : }
5545 : :
5546 : : static const struct file_operations tracing_dyn_info_fops = {
5547 : : .open = tracing_open_generic,
5548 : : .read = tracing_read_dyn_info,
5549 : : .llseek = generic_file_llseek,
5550 : : };
5551 : : #endif /* CONFIG_DYNAMIC_FTRACE */
5552 : :
5553 : : #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
5554 : : static void
5555 : : ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5556 : : {
5557 : : tracing_snapshot();
5558 : : }
5559 : :
5560 : : static void
5561 : : ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
5562 : : {
5563 : : unsigned long *count = (long *)data;
5564 : :
5565 : : if (!*count)
5566 : : return;
5567 : :
5568 : : if (*count != -1)
5569 : : (*count)--;
5570 : :
5571 : : tracing_snapshot();
5572 : : }
5573 : :
5574 : : static int
5575 : : ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
5576 : : struct ftrace_probe_ops *ops, void *data)
5577 : : {
5578 : : long count = (long)data;
5579 : :
5580 : : seq_printf(m, "%ps:", (void *)ip);
5581 : :
5582 : : seq_printf(m, "snapshot");
5583 : :
5584 : : if (count == -1)
5585 : : seq_printf(m, ":unlimited\n");
5586 : : else
5587 : : seq_printf(m, ":count=%ld\n", count);
5588 : :
5589 : : return 0;
5590 : : }
5591 : :
5592 : : static struct ftrace_probe_ops snapshot_probe_ops = {
5593 : : .func = ftrace_snapshot,
5594 : : .print = ftrace_snapshot_print,
5595 : : };
5596 : :
5597 : : static struct ftrace_probe_ops snapshot_count_probe_ops = {
5598 : : .func = ftrace_count_snapshot,
5599 : : .print = ftrace_snapshot_print,
5600 : : };
5601 : :
5602 : : static int
5603 : : ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
5604 : : char *glob, char *cmd, char *param, int enable)
5605 : : {
5606 : : struct ftrace_probe_ops *ops;
5607 : : void *count = (void *)-1;
5608 : : char *number;
5609 : : int ret;
5610 : :
5611 : : /* hash funcs only work with set_ftrace_filter */
5612 : : if (!enable)
5613 : : return -EINVAL;
5614 : :
5615 : : ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops;
5616 : :
5617 : : if (glob[0] == '!') {
5618 : : unregister_ftrace_function_probe_func(glob+1, ops);
5619 : : return 0;
5620 : : }
5621 : :
5622 : : if (!param)
5623 : : goto out_reg;
5624 : :
5625 : : number = strsep(¶m, ":");
5626 : :
5627 : : if (!strlen(number))
5628 : : goto out_reg;
5629 : :
5630 : : /*
5631 : : * We use the callback data field (which is a pointer)
5632 : : * as our counter.
5633 : : */
5634 : : ret = kstrtoul(number, 0, (unsigned long *)&count);
5635 : : if (ret)
5636 : : return ret;
5637 : :
5638 : : out_reg:
5639 : : ret = register_ftrace_function_probe(glob, ops, count);
5640 : :
5641 : : if (ret >= 0)
5642 : : alloc_snapshot(&global_trace);
5643 : :
5644 : : return ret < 0 ? ret : 0;
5645 : : }
5646 : :
5647 : : static struct ftrace_func_command ftrace_snapshot_cmd = {
5648 : : .name = "snapshot",
5649 : : .func = ftrace_trace_snapshot_callback,
5650 : : };
5651 : :
5652 : : static __init int register_snapshot_cmd(void)
5653 : : {
5654 : : return register_ftrace_command(&ftrace_snapshot_cmd);
5655 : : }
5656 : : #else
5657 : : static inline __init int register_snapshot_cmd(void) { return 0; }
5658 : : #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
5659 : :
5660 : 0 : struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
5661 : : {
5662 [ # # ]: 0 : if (tr->dir)
5663 : : return tr->dir;
5664 : :
5665 [ # # ]: 0 : if (!debugfs_initialized())
5666 : : return NULL;
5667 : :
5668 [ # # ]: 0 : if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
5669 : 0 : tr->dir = debugfs_create_dir("tracing", NULL);
5670 : :
5671 [ # # ]: 0 : if (!tr->dir)
5672 [ # # ]: 0 : pr_warn_once("Could not create debugfs directory 'tracing'\n");
5673 : :
5674 : 0 : return tr->dir;
5675 : : }
5676 : :
5677 : 0 : struct dentry *tracing_init_dentry(void)
5678 : : {
5679 : 0 : return tracing_init_dentry_tr(&global_trace);
5680 : : }
5681 : :
5682 : 0 : static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
5683 : : {
5684 : : struct dentry *d_tracer;
5685 : :
5686 [ # # ]: 0 : if (tr->percpu_dir)
5687 : : return tr->percpu_dir;
5688 : :
5689 : 0 : d_tracer = tracing_init_dentry_tr(tr);
5690 [ # # ]: 0 : if (!d_tracer)
5691 : : return NULL;
5692 : :
5693 : 0 : tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
5694 : :
5695 [ # # ][ # # ]: 0 : WARN_ONCE(!tr->percpu_dir,
[ # # ]
5696 : : "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
5697 : :
5698 : 0 : return tr->percpu_dir;
5699 : : }
5700 : :
5701 : : static struct dentry *
5702 : : trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
5703 : : void *data, long cpu, const struct file_operations *fops)
5704 : : {
5705 : 0 : struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
5706 : :
5707 [ # # # # : 0 : if (ret) /* See tracing_get_cpu() */
# # # # #
# ]
5708 : 0 : ret->d_inode->i_cdev = (void *)(cpu + 1);
5709 : : return ret;
5710 : : }
5711 : :
5712 : : static void
5713 : 0 : tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
5714 : : {
5715 : 0 : struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
5716 : : struct dentry *d_cpu;
5717 : : char cpu_dir[30]; /* 30 characters should be more than enough */
5718 : :
5719 [ # # ]: 0 : if (!d_percpu)
5720 : 0 : return;
5721 : :
5722 : 0 : snprintf(cpu_dir, 30, "cpu%ld", cpu);
5723 : 0 : d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
5724 [ # # ]: 0 : if (!d_cpu) {
5725 : 0 : pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
5726 : 0 : return;
5727 : : }
5728 : :
5729 : : /* per cpu trace_pipe */
5730 : : trace_create_cpu_file("trace_pipe", 0444, d_cpu,
5731 : : tr, cpu, &tracing_pipe_fops);
5732 : :
5733 : : /* per cpu trace */
5734 : : trace_create_cpu_file("trace", 0644, d_cpu,
5735 : : tr, cpu, &tracing_fops);
5736 : :
5737 : : trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
5738 : : tr, cpu, &tracing_buffers_fops);
5739 : :
5740 : : trace_create_cpu_file("stats", 0444, d_cpu,
5741 : : tr, cpu, &tracing_stats_fops);
5742 : :
5743 : : trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
5744 : : tr, cpu, &tracing_entries_fops);
5745 : :
5746 : : #ifdef CONFIG_TRACER_SNAPSHOT
5747 : : trace_create_cpu_file("snapshot", 0644, d_cpu,
5748 : : tr, cpu, &snapshot_fops);
5749 : :
5750 : : trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
5751 : : tr, cpu, &snapshot_raw_fops);
5752 : : #endif
5753 : : }
5754 : :
5755 : : #ifdef CONFIG_FTRACE_SELFTEST
5756 : : /* Let selftest have access to static functions in this file */
5757 : : #include "trace_selftest.c"
5758 : : #endif
5759 : :
5760 : : struct trace_option_dentry {
5761 : : struct tracer_opt *opt;
5762 : : struct tracer_flags *flags;
5763 : : struct trace_array *tr;
5764 : : struct dentry *entry;
5765 : : };
5766 : :
5767 : : static ssize_t
5768 : 0 : trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
5769 : : loff_t *ppos)
5770 : : {
5771 : 0 : struct trace_option_dentry *topt = filp->private_data;
5772 : : char *buf;
5773 : :
5774 [ # # ]: 0 : if (topt->flags->val & topt->opt->bit)
5775 : : buf = "1\n";
5776 : : else
5777 : : buf = "0\n";
5778 : :
5779 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5780 : : }
5781 : :
5782 : : static ssize_t
5783 : 0 : trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
5784 : : loff_t *ppos)
5785 : : {
5786 : 0 : struct trace_option_dentry *topt = filp->private_data;
5787 : : unsigned long val;
5788 : : int ret;
5789 : :
5790 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5791 [ # # ]: 0 : if (ret)
5792 : : return ret;
5793 : :
5794 [ # # ]: 0 : if (val != 0 && val != 1)
5795 : : return -EINVAL;
5796 : :
5797 [ # # ]: 0 : if (!!(topt->flags->val & topt->opt->bit) != val) {
5798 : 0 : mutex_lock(&trace_types_lock);
5799 : 0 : ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
5800 : : topt->opt, !val);
5801 : 0 : mutex_unlock(&trace_types_lock);
5802 [ # # ]: 0 : if (ret)
5803 : : return ret;
5804 : : }
5805 : :
5806 : 0 : *ppos += cnt;
5807 : :
5808 : 0 : return cnt;
5809 : : }
5810 : :
5811 : :
5812 : : static const struct file_operations trace_options_fops = {
5813 : : .open = tracing_open_generic,
5814 : : .read = trace_options_read,
5815 : : .write = trace_options_write,
5816 : : .llseek = generic_file_llseek,
5817 : : };
5818 : :
5819 : : static ssize_t
5820 : 0 : trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
5821 : : loff_t *ppos)
5822 : : {
5823 : 0 : long index = (long)filp->private_data;
5824 : : char *buf;
5825 : :
5826 [ # # ]: 0 : if (trace_flags & (1 << index))
5827 : : buf = "1\n";
5828 : : else
5829 : : buf = "0\n";
5830 : :
5831 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
5832 : : }
5833 : :
5834 : : static ssize_t
5835 : 0 : trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
5836 : : loff_t *ppos)
5837 : : {
5838 : : struct trace_array *tr = &global_trace;
5839 : 0 : long index = (long)filp->private_data;
5840 : : unsigned long val;
5841 : : int ret;
5842 : :
5843 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
5844 [ # # ]: 0 : if (ret)
5845 : : return ret;
5846 : :
5847 [ # # ]: 0 : if (val != 0 && val != 1)
5848 : : return -EINVAL;
5849 : :
5850 : 0 : mutex_lock(&trace_types_lock);
5851 : 0 : ret = set_tracer_flag(tr, 1 << index, val);
5852 : 0 : mutex_unlock(&trace_types_lock);
5853 : :
5854 [ # # ]: 0 : if (ret < 0)
5855 : : return ret;
5856 : :
5857 : 0 : *ppos += cnt;
5858 : :
5859 : 0 : return cnt;
5860 : : }
5861 : :
5862 : : static const struct file_operations trace_options_core_fops = {
5863 : : .open = tracing_open_generic,
5864 : : .read = trace_options_core_read,
5865 : : .write = trace_options_core_write,
5866 : : .llseek = generic_file_llseek,
5867 : : };
5868 : :
5869 : 0 : struct dentry *trace_create_file(const char *name,
5870 : : umode_t mode,
5871 : : struct dentry *parent,
5872 : : void *data,
5873 : : const struct file_operations *fops)
5874 : : {
5875 : : struct dentry *ret;
5876 : :
5877 : 0 : ret = debugfs_create_file(name, mode, parent, data, fops);
5878 [ # # ]: 0 : if (!ret)
5879 : 0 : pr_warning("Could not create debugfs '%s' entry\n", name);
5880 : :
5881 : 0 : return ret;
5882 : : }
5883 : :
5884 : :
5885 : 0 : static struct dentry *trace_options_init_dentry(struct trace_array *tr)
5886 : : {
5887 : : struct dentry *d_tracer;
5888 : :
5889 [ # # ]: 0 : if (tr->options)
5890 : : return tr->options;
5891 : :
5892 : 0 : d_tracer = tracing_init_dentry_tr(tr);
5893 [ # # ]: 0 : if (!d_tracer)
5894 : : return NULL;
5895 : :
5896 : 0 : tr->options = debugfs_create_dir("options", d_tracer);
5897 [ # # ]: 0 : if (!tr->options) {
5898 : 0 : pr_warning("Could not create debugfs directory 'options'\n");
5899 : 0 : return NULL;
5900 : : }
5901 : :
5902 : : return tr->options;
5903 : : }
5904 : :
5905 : : static void
5906 : 0 : create_trace_option_file(struct trace_array *tr,
5907 : : struct trace_option_dentry *topt,
5908 : : struct tracer_flags *flags,
5909 : : struct tracer_opt *opt)
5910 : : {
5911 : : struct dentry *t_options;
5912 : :
5913 : 0 : t_options = trace_options_init_dentry(tr);
5914 [ # # ]: 0 : if (!t_options)
5915 : 0 : return;
5916 : :
5917 : 0 : topt->flags = flags;
5918 : 0 : topt->opt = opt;
5919 : 0 : topt->tr = tr;
5920 : :
5921 : 0 : topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
5922 : : &trace_options_fops);
5923 : :
5924 : : }
5925 : :
5926 : : static struct trace_option_dentry *
5927 : 0 : create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
5928 : : {
5929 : : struct trace_option_dentry *topts;
5930 : : struct tracer_flags *flags;
5931 : : struct tracer_opt *opts;
5932 : : int cnt;
5933 : :
5934 [ # # ]: 0 : if (!tracer)
5935 : : return NULL;
5936 : :
5937 : 0 : flags = tracer->flags;
5938 : :
5939 [ # # ][ # # ]: 0 : if (!flags || !flags->opts)
5940 : : return NULL;
5941 : :
5942 : : opts = flags->opts;
5943 : :
5944 [ # # ]: 0 : for (cnt = 0; opts[cnt].name; cnt++)
5945 : : ;
5946 : :
5947 : 0 : topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
5948 [ # # ]: 0 : if (!topts)
5949 : : return NULL;
5950 : :
5951 [ # # ]: 0 : for (cnt = 0; opts[cnt].name; cnt++)
5952 : 0 : create_trace_option_file(tr, &topts[cnt], flags,
5953 : : &opts[cnt]);
5954 : :
5955 : : return topts;
5956 : : }
5957 : :
5958 : : static void
5959 : 0 : destroy_trace_option_files(struct trace_option_dentry *topts)
5960 : : {
5961 : : int cnt;
5962 : :
5963 [ # # ]: 0 : if (!topts)
5964 : 0 : return;
5965 : :
5966 [ # # ]: 0 : for (cnt = 0; topts[cnt].opt; cnt++) {
5967 [ # # ]: 0 : if (topts[cnt].entry)
5968 : 0 : debugfs_remove(topts[cnt].entry);
5969 : : }
5970 : :
5971 : 0 : kfree(topts);
5972 : : }
5973 : :
5974 : : static struct dentry *
5975 : 0 : create_trace_option_core_file(struct trace_array *tr,
5976 : : const char *option, long index)
5977 : : {
5978 : : struct dentry *t_options;
5979 : :
5980 : 0 : t_options = trace_options_init_dentry(tr);
5981 [ # # ]: 0 : if (!t_options)
5982 : : return NULL;
5983 : :
5984 : 0 : return trace_create_file(option, 0644, t_options, (void *)index,
5985 : : &trace_options_core_fops);
5986 : : }
5987 : :
5988 : 0 : static __init void create_trace_options_dir(struct trace_array *tr)
5989 : : {
5990 : : struct dentry *t_options;
5991 : : int i;
5992 : :
5993 : 0 : t_options = trace_options_init_dentry(tr);
5994 [ # # ]: 0 : if (!t_options)
5995 : 0 : return;
5996 : :
5997 [ # # ]: 0 : for (i = 0; trace_options[i]; i++)
5998 : 0 : create_trace_option_core_file(tr, trace_options[i], i);
5999 : : }
6000 : :
6001 : : static ssize_t
6002 : 0 : rb_simple_read(struct file *filp, char __user *ubuf,
6003 : : size_t cnt, loff_t *ppos)
6004 : : {
6005 : 0 : struct trace_array *tr = filp->private_data;
6006 : : char buf[64];
6007 : : int r;
6008 : :
6009 : : r = tracer_tracing_is_on(tr);
6010 : 0 : r = sprintf(buf, "%d\n", r);
6011 : :
6012 : 0 : return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6013 : : }
6014 : :
6015 : : static ssize_t
6016 : 0 : rb_simple_write(struct file *filp, const char __user *ubuf,
6017 : : size_t cnt, loff_t *ppos)
6018 : : {
6019 : 0 : struct trace_array *tr = filp->private_data;
6020 : 0 : struct ring_buffer *buffer = tr->trace_buffer.buffer;
6021 : : unsigned long val;
6022 : : int ret;
6023 : :
6024 : 0 : ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6025 [ # # ]: 0 : if (ret)
6026 : : return ret;
6027 : :
6028 [ # # ]: 0 : if (buffer) {
6029 : 0 : mutex_lock(&trace_types_lock);
6030 [ # # ]: 0 : if (val) {
6031 : : tracer_tracing_on(tr);
6032 [ # # ]: 0 : if (tr->current_trace->start)
6033 : 0 : tr->current_trace->start(tr);
6034 : : } else {
6035 : : tracer_tracing_off(tr);
6036 [ # # ]: 0 : if (tr->current_trace->stop)
6037 : 0 : tr->current_trace->stop(tr);
6038 : : }
6039 : 0 : mutex_unlock(&trace_types_lock);
6040 : : }
6041 : :
6042 : 0 : (*ppos)++;
6043 : :
6044 : 0 : return cnt;
6045 : : }
6046 : :
6047 : : static const struct file_operations rb_simple_fops = {
6048 : : .open = tracing_open_generic_tr,
6049 : : .read = rb_simple_read,
6050 : : .write = rb_simple_write,
6051 : : .release = tracing_release_generic_tr,
6052 : : .llseek = default_llseek,
6053 : : };
6054 : :
6055 : : struct dentry *trace_instance_dir;
6056 : :
6057 : : static void
6058 : : init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
6059 : :
6060 : : static int
6061 : 0 : allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
6062 : : {
6063 : : enum ring_buffer_flags rb_flags;
6064 : :
6065 : 0 : rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
6066 : :
6067 : 0 : buf->tr = tr;
6068 : :
6069 : 0 : buf->buffer = ring_buffer_alloc(size, rb_flags);
6070 [ # # ]: 0 : if (!buf->buffer)
6071 : : return -ENOMEM;
6072 : :
6073 : 0 : buf->data = alloc_percpu(struct trace_array_cpu);
6074 [ # # ]: 0 : if (!buf->data) {
6075 : 0 : ring_buffer_free(buf->buffer);
6076 : 0 : return -ENOMEM;
6077 : : }
6078 : :
6079 : : /* Allocate the first page for all buffers */
6080 : 0 : set_buffer_entries(&tr->trace_buffer,
6081 : : ring_buffer_size(tr->trace_buffer.buffer, 0));
6082 : :
6083 : 0 : return 0;
6084 : : }
6085 : :
6086 : : static int allocate_trace_buffers(struct trace_array *tr, int size)
6087 : : {
6088 : : int ret;
6089 : :
6090 : 0 : ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
6091 [ # # ][ # # ]: 0 : if (ret)
6092 : : return ret;
6093 : :
6094 : : #ifdef CONFIG_TRACER_MAX_TRACE
6095 : : ret = allocate_trace_buffer(tr, &tr->max_buffer,
6096 : : allocate_snapshot ? size : 1);
6097 : : if (WARN_ON(ret)) {
6098 : : ring_buffer_free(tr->trace_buffer.buffer);
6099 : : free_percpu(tr->trace_buffer.data);
6100 : : return -ENOMEM;
6101 : : }
6102 : : tr->allocated_snapshot = allocate_snapshot;
6103 : :
6104 : : /*
6105 : : * Only the top level trace array gets its snapshot allocated
6106 : : * from the kernel command line.
6107 : : */
6108 : : allocate_snapshot = false;
6109 : : #endif
6110 : : return 0;
6111 : : }
6112 : :
6113 : 0 : static int new_instance_create(const char *name)
6114 : : {
6115 : : struct trace_array *tr;
6116 : : int ret;
6117 : :
6118 : 0 : mutex_lock(&trace_types_lock);
6119 : :
6120 : : ret = -EEXIST;
6121 [ # # ]: 0 : list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6122 [ # # ][ # # ]: 0 : if (tr->name && strcmp(tr->name, name) == 0)
6123 : : goto out_unlock;
6124 : : }
6125 : :
6126 : : ret = -ENOMEM;
6127 : : tr = kzalloc(sizeof(*tr), GFP_KERNEL);
6128 [ # # ]: 0 : if (!tr)
6129 : : goto out_unlock;
6130 : :
6131 : 0 : tr->name = kstrdup(name, GFP_KERNEL);
6132 [ # # ]: 0 : if (!tr->name)
6133 : : goto out_free_tr;
6134 : :
6135 : : if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
6136 : : goto out_free_tr;
6137 : :
6138 : : cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
6139 : :
6140 : 0 : raw_spin_lock_init(&tr->start_lock);
6141 : :
6142 : 0 : tr->current_trace = &nop_trace;
6143 : :
6144 : 0 : INIT_LIST_HEAD(&tr->systems);
6145 : 0 : INIT_LIST_HEAD(&tr->events);
6146 : :
6147 [ # # ]: 0 : if (allocate_trace_buffers(tr, trace_buf_size) < 0)
6148 : : goto out_free_tr;
6149 : :
6150 : 0 : tr->dir = debugfs_create_dir(name, trace_instance_dir);
6151 [ # # ]: 0 : if (!tr->dir)
6152 : : goto out_free_tr;
6153 : :
6154 : 0 : ret = event_trace_add_tracer(tr->dir, tr);
6155 [ # # ]: 0 : if (ret) {
6156 : 0 : debugfs_remove_recursive(tr->dir);
6157 : 0 : goto out_free_tr;
6158 : : }
6159 : :
6160 : 0 : init_tracer_debugfs(tr, tr->dir);
6161 : :
6162 : 0 : list_add(&tr->list, &ftrace_trace_arrays);
6163 : :
6164 : 0 : mutex_unlock(&trace_types_lock);
6165 : :
6166 : 0 : return 0;
6167 : :
6168 : : out_free_tr:
6169 [ # # ]: 0 : if (tr->trace_buffer.buffer)
6170 : 0 : ring_buffer_free(tr->trace_buffer.buffer);
6171 : : free_cpumask_var(tr->tracing_cpumask);
6172 : 0 : kfree(tr->name);
6173 : 0 : kfree(tr);
6174 : :
6175 : : out_unlock:
6176 : 0 : mutex_unlock(&trace_types_lock);
6177 : :
6178 : 0 : return ret;
6179 : :
6180 : : }
6181 : :
6182 : 0 : static int instance_delete(const char *name)
6183 : : {
6184 : : struct trace_array *tr;
6185 : : int found = 0;
6186 : : int ret;
6187 : :
6188 : 0 : mutex_lock(&trace_types_lock);
6189 : :
6190 : : ret = -ENODEV;
6191 [ # # ]: 0 : list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6192 [ # # ][ # # ]: 0 : if (tr->name && strcmp(tr->name, name) == 0) {
6193 : : found = 1;
6194 : : break;
6195 : : }
6196 : : }
6197 [ # # ]: 0 : if (!found)
6198 : : goto out_unlock;
6199 : :
6200 : : ret = -EBUSY;
6201 [ # # ]: 0 : if (tr->ref)
6202 : : goto out_unlock;
6203 : :
6204 : : list_del(&tr->list);
6205 : :
6206 : 0 : event_trace_del_tracer(tr);
6207 : 0 : debugfs_remove_recursive(tr->dir);
6208 : 0 : free_percpu(tr->trace_buffer.data);
6209 : 0 : ring_buffer_free(tr->trace_buffer.buffer);
6210 : :
6211 : 0 : kfree(tr->name);
6212 : 0 : kfree(tr);
6213 : :
6214 : : ret = 0;
6215 : :
6216 : : out_unlock:
6217 : 0 : mutex_unlock(&trace_types_lock);
6218 : :
6219 : 0 : return ret;
6220 : : }
6221 : :
6222 : 0 : static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
6223 : : {
6224 : : struct dentry *parent;
6225 : : int ret;
6226 : :
6227 : : /* Paranoid: Make sure the parent is the "instances" directory */
6228 : 0 : parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6229 [ # # ][ # # ]: 0 : if (WARN_ON_ONCE(parent != trace_instance_dir))
[ # # ][ # # ]
6230 : : return -ENOENT;
6231 : :
6232 : : /*
6233 : : * The inode mutex is locked, but debugfs_create_dir() will also
6234 : : * take the mutex. As the instances directory can not be destroyed
6235 : : * or changed in any other way, it is safe to unlock it, and
6236 : : * let the dentry try. If two users try to make the same dir at
6237 : : * the same time, then the new_instance_create() will determine the
6238 : : * winner.
6239 : : */
6240 : 0 : mutex_unlock(&inode->i_mutex);
6241 : :
6242 : 0 : ret = new_instance_create(dentry->d_iname);
6243 : :
6244 : 0 : mutex_lock(&inode->i_mutex);
6245 : :
6246 : 0 : return ret;
6247 : : }
6248 : :
6249 : 0 : static int instance_rmdir(struct inode *inode, struct dentry *dentry)
6250 : : {
6251 : : struct dentry *parent;
6252 : : int ret;
6253 : :
6254 : : /* Paranoid: Make sure the parent is the "instances" directory */
6255 : 0 : parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
6256 [ # # ][ # # ]: 0 : if (WARN_ON_ONCE(parent != trace_instance_dir))
[ # # ][ # # ]
6257 : : return -ENOENT;
6258 : :
6259 : : /* The caller did a dget() on dentry */
6260 : 0 : mutex_unlock(&dentry->d_inode->i_mutex);
6261 : :
6262 : : /*
6263 : : * The inode mutex is locked, but debugfs_create_dir() will also
6264 : : * take the mutex. As the instances directory can not be destroyed
6265 : : * or changed in any other way, it is safe to unlock it, and
6266 : : * let the dentry try. If two users try to make the same dir at
6267 : : * the same time, then the instance_delete() will determine the
6268 : : * winner.
6269 : : */
6270 : 0 : mutex_unlock(&inode->i_mutex);
6271 : :
6272 : 0 : ret = instance_delete(dentry->d_iname);
6273 : :
6274 : 0 : mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
6275 : 0 : mutex_lock(&dentry->d_inode->i_mutex);
6276 : :
6277 : 0 : return ret;
6278 : : }
6279 : :
6280 : : static const struct inode_operations instance_dir_inode_operations = {
6281 : : .lookup = simple_lookup,
6282 : : .mkdir = instance_mkdir,
6283 : : .rmdir = instance_rmdir,
6284 : : };
6285 : :
6286 : 0 : static __init void create_trace_instances(struct dentry *d_tracer)
6287 : : {
6288 : 0 : trace_instance_dir = debugfs_create_dir("instances", d_tracer);
6289 [ # # ][ # # ]: 0 : if (WARN_ON(!trace_instance_dir))
6290 : 0 : return;
6291 : :
6292 : : /* Hijack the dir inode operations, to allow mkdir */
6293 : 0 : trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
6294 : : }
6295 : :
6296 : : static void
6297 : 0 : init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
6298 : : {
6299 : : int cpu;
6300 : :
6301 : 0 : trace_create_file("tracing_cpumask", 0644, d_tracer,
6302 : : tr, &tracing_cpumask_fops);
6303 : :
6304 : 0 : trace_create_file("trace_options", 0644, d_tracer,
6305 : : tr, &tracing_iter_fops);
6306 : :
6307 : 0 : trace_create_file("trace", 0644, d_tracer,
6308 : : tr, &tracing_fops);
6309 : :
6310 : 0 : trace_create_file("trace_pipe", 0444, d_tracer,
6311 : : tr, &tracing_pipe_fops);
6312 : :
6313 : 0 : trace_create_file("buffer_size_kb", 0644, d_tracer,
6314 : : tr, &tracing_entries_fops);
6315 : :
6316 : 0 : trace_create_file("buffer_total_size_kb", 0444, d_tracer,
6317 : : tr, &tracing_total_entries_fops);
6318 : :
6319 : 0 : trace_create_file("free_buffer", 0200, d_tracer,
6320 : : tr, &tracing_free_buffer_fops);
6321 : :
6322 : 0 : trace_create_file("trace_marker", 0220, d_tracer,
6323 : : tr, &tracing_mark_fops);
6324 : :
6325 : 0 : trace_create_file("saved_tgids", 0444, d_tracer,
6326 : : tr, &tracing_saved_tgids_fops);
6327 : :
6328 : 0 : trace_create_file("trace_clock", 0644, d_tracer, tr,
6329 : : &trace_clock_fops);
6330 : :
6331 : 0 : trace_create_file("tracing_on", 0644, d_tracer,
6332 : : tr, &rb_simple_fops);
6333 : :
6334 : : #ifdef CONFIG_TRACER_SNAPSHOT
6335 : : trace_create_file("snapshot", 0644, d_tracer,
6336 : : tr, &snapshot_fops);
6337 : : #endif
6338 : :
6339 [ # # ]: 0 : for_each_tracing_cpu(cpu)
6340 : 0 : tracing_init_debugfs_percpu(tr, cpu);
6341 : :
6342 : 0 : }
6343 : :
6344 : 0 : static __init int tracer_init_debugfs(void)
6345 : : {
6346 : : struct dentry *d_tracer;
6347 : :
6348 : : trace_access_lock_init();
6349 : :
6350 : : d_tracer = tracing_init_dentry();
6351 [ # # ]: 0 : if (!d_tracer)
6352 : : return 0;
6353 : :
6354 : 0 : init_tracer_debugfs(&global_trace, d_tracer);
6355 : :
6356 : 0 : trace_create_file("available_tracers", 0444, d_tracer,
6357 : : &global_trace, &show_traces_fops);
6358 : :
6359 : 0 : trace_create_file("current_tracer", 0644, d_tracer,
6360 : : &global_trace, &set_tracer_fops);
6361 : :
6362 : : #ifdef CONFIG_TRACER_MAX_TRACE
6363 : : trace_create_file("tracing_max_latency", 0644, d_tracer,
6364 : : &tracing_max_latency, &tracing_max_lat_fops);
6365 : : #endif
6366 : :
6367 : 0 : trace_create_file("tracing_thresh", 0644, d_tracer,
6368 : : &tracing_thresh, &tracing_max_lat_fops);
6369 : :
6370 : 0 : trace_create_file("README", 0444, d_tracer,
6371 : : NULL, &tracing_readme_fops);
6372 : :
6373 : 0 : trace_create_file("saved_cmdlines", 0444, d_tracer,
6374 : : NULL, &tracing_saved_cmdlines_fops);
6375 : :
6376 : : #ifdef CONFIG_DYNAMIC_FTRACE
6377 : 0 : trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
6378 : : &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
6379 : : #endif
6380 : :
6381 : 0 : create_trace_instances(d_tracer);
6382 : :
6383 : 0 : create_trace_options_dir(&global_trace);
6384 : :
6385 : 0 : return 0;
6386 : : }
6387 : :
6388 : 0 : static int trace_panic_handler(struct notifier_block *this,
6389 : : unsigned long event, void *unused)
6390 : : {
6391 [ # # ]: 0 : if (ftrace_dump_on_oops)
6392 : 0 : ftrace_dump(ftrace_dump_on_oops);
6393 : 0 : return NOTIFY_OK;
6394 : : }
6395 : :
6396 : : static struct notifier_block trace_panic_notifier = {
6397 : : .notifier_call = trace_panic_handler,
6398 : : .next = NULL,
6399 : : .priority = 150 /* priority: INT_MAX >= x >= 0 */
6400 : : };
6401 : :
6402 : 0 : static int trace_die_handler(struct notifier_block *self,
6403 : : unsigned long val,
6404 : : void *data)
6405 : : {
6406 [ # # ]: 0 : switch (val) {
6407 : : case DIE_OOPS:
6408 [ # # ]: 0 : if (ftrace_dump_on_oops)
6409 : 0 : ftrace_dump(ftrace_dump_on_oops);
6410 : : break;
6411 : : default:
6412 : : break;
6413 : : }
6414 : 0 : return NOTIFY_OK;
6415 : : }
6416 : :
6417 : : static struct notifier_block trace_die_notifier = {
6418 : : .notifier_call = trace_die_handler,
6419 : : .priority = 200
6420 : : };
6421 : :
6422 : : /*
6423 : : * printk is set to max of 1024, we really don't need it that big.
6424 : : * Nothing should be printing 1000 characters anyway.
6425 : : */
6426 : : #define TRACE_MAX_PRINT 1000
6427 : :
6428 : : /*
6429 : : * Define here KERN_TRACE so that we have one place to modify
6430 : : * it if we decide to change what log level the ftrace dump
6431 : : * should be at.
6432 : : */
6433 : : #define KERN_TRACE KERN_EMERG
6434 : :
6435 : : void
6436 : 0 : trace_printk_seq(struct trace_seq *s)
6437 : : {
6438 : : /* Probably should print a warning here. */
6439 [ # # ]: 0 : if (s->len >= TRACE_MAX_PRINT)
6440 : 0 : s->len = TRACE_MAX_PRINT;
6441 : :
6442 : : /* should be zero ended, but we are paranoid. */
6443 : 0 : s->buffer[s->len] = 0;
6444 : :
6445 : 0 : printk(KERN_TRACE "%s", s->buffer);
6446 : :
6447 : : trace_seq_init(s);
6448 : 0 : }
6449 : :
6450 : 0 : void trace_init_global_iter(struct trace_iterator *iter)
6451 : : {
6452 : 0 : iter->tr = &global_trace;
6453 : 0 : iter->trace = iter->tr->current_trace;
6454 : 0 : iter->cpu_file = RING_BUFFER_ALL_CPUS;
6455 : 0 : iter->trace_buffer = &global_trace.trace_buffer;
6456 : :
6457 [ # # ][ # # ]: 0 : if (iter->trace && iter->trace->open)
6458 : 0 : iter->trace->open(iter);
6459 : :
6460 : : /* Annotate start of buffers if we had overruns */
6461 [ # # ]: 0 : if (ring_buffer_overruns(iter->trace_buffer->buffer))
6462 : 0 : iter->iter_flags |= TRACE_FILE_ANNOTATE;
6463 : :
6464 : : /* Output in nanoseconds only if we are using a clock in nanoseconds. */
6465 [ # # ]: 0 : if (trace_clocks[iter->tr->clock_id].in_ns)
6466 : 0 : iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6467 : 0 : }
6468 : :
6469 : 0 : void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
6470 : : {
6471 : : /* use static because iter can be a bit big for the stack */
6472 : : static struct trace_iterator iter;
6473 : : static atomic_t dump_running;
6474 : : unsigned int old_userobj;
6475 : : unsigned long flags;
6476 : : int cnt = 0, cpu;
6477 : :
6478 : : /* Only allow one dump user at a time. */
6479 [ # # ]: 0 : if (atomic_inc_return(&dump_running) != 1) {
6480 : : atomic_dec(&dump_running);
6481 : 0 : return;
6482 : : }
6483 : :
6484 : : /*
6485 : : * Always turn off tracing when we dump.
6486 : : * We don't need to show trace output of what happens
6487 : : * between multiple crashes.
6488 : : *
6489 : : * If the user does a sysrq-z, then they can re-enable
6490 : : * tracing with echo 1 > tracing_on.
6491 : : */
6492 : 0 : tracing_off();
6493 : :
6494 : : local_irq_save(flags);
6495 : :
6496 : : /* Simulate the iterator */
6497 : 0 : trace_init_global_iter(&iter);
6498 : :
6499 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
6500 : 0 : atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
6501 : : }
6502 : :
6503 : 0 : old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
6504 : :
6505 : : /* don't look at user memory in panic mode */
6506 : 0 : trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
6507 : :
6508 [ # # # # ]: 0 : switch (oops_dump_mode) {
6509 : : case DUMP_ALL:
6510 : 0 : iter.cpu_file = RING_BUFFER_ALL_CPUS;
6511 : 0 : break;
6512 : : case DUMP_ORIG:
6513 : 0 : iter.cpu_file = raw_smp_processor_id();
6514 : 0 : break;
6515 : : case DUMP_NONE:
6516 : : goto out_enable;
6517 : : default:
6518 : 0 : printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
6519 : 0 : iter.cpu_file = RING_BUFFER_ALL_CPUS;
6520 : : }
6521 : :
6522 : 0 : printk(KERN_TRACE "Dumping ftrace buffer:\n");
6523 : :
6524 : : /* Did function tracer already get disabled? */
6525 [ # # ]: 0 : if (ftrace_is_dead()) {
6526 : 0 : printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
6527 : 0 : printk("# MAY BE MISSING FUNCTION EVENTS\n");
6528 : : }
6529 : :
6530 : : /*
6531 : : * We need to stop all tracing on all CPUS to read the
6532 : : * the next buffer. This is a bit expensive, but is
6533 : : * not done often. We fill all what we can read,
6534 : : * and then release the locks again.
6535 : : */
6536 : :
6537 [ # # ]: 0 : while (!trace_empty(&iter)) {
6538 : :
6539 [ # # ]: 0 : if (!cnt)
6540 : 0 : printk(KERN_TRACE "---------------------------------\n");
6541 : :
6542 : 0 : cnt++;
6543 : :
6544 : : /* reset all but tr, trace, and overruns */
6545 : 0 : memset(&iter.seq, 0,
6546 : : sizeof(struct trace_iterator) -
6547 : : offsetof(struct trace_iterator, seq));
6548 : 0 : iter.iter_flags |= TRACE_FILE_LAT_FMT;
6549 : 0 : iter.pos = -1;
6550 : :
6551 [ # # ]: 0 : if (trace_find_next_entry_inc(&iter) != NULL) {
6552 : : int ret;
6553 : :
6554 : 0 : ret = print_trace_line(&iter);
6555 [ # # ]: 0 : if (ret != TRACE_TYPE_NO_CONSUME)
6556 : : trace_consume(&iter);
6557 : : }
6558 : : touch_nmi_watchdog();
6559 : :
6560 : 0 : trace_printk_seq(&iter.seq);
6561 : : }
6562 : :
6563 [ # # ]: 0 : if (!cnt)
6564 : 0 : printk(KERN_TRACE " (ftrace buffer empty)\n");
6565 : : else
6566 : 0 : printk(KERN_TRACE "---------------------------------\n");
6567 : :
6568 : : out_enable:
6569 : 0 : trace_flags |= old_userobj;
6570 : :
6571 [ # # ]: 0 : for_each_tracing_cpu(cpu) {
6572 : 0 : atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
6573 : : }
6574 : : atomic_dec(&dump_running);
6575 [ # # ]: 0 : local_irq_restore(flags);
6576 : : }
6577 : : EXPORT_SYMBOL_GPL(ftrace_dump);
6578 : :
6579 : 0 : __init static int tracer_alloc_buffers(void)
6580 : : {
6581 : : int ring_buf_size;
6582 : : int ret = -ENOMEM;
6583 : :
6584 : :
6585 : : if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
6586 : : goto out;
6587 : :
6588 : : if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
6589 : : goto out_free_buffer_mask;
6590 : :
6591 : : /* Only allocate trace_printk buffers if a trace_printk exists */
6592 [ # # ]: 0 : if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
6593 : : /* Must be called before global_trace.buffer is allocated */
6594 : 0 : trace_printk_init_buffers();
6595 : :
6596 : : /* To save memory, keep the ring buffer size to its minimum */
6597 [ # # ]: 0 : if (ring_buffer_expanded)
6598 : 0 : ring_buf_size = trace_buf_size;
6599 : : else
6600 : : ring_buf_size = 1;
6601 : :
6602 : 0 : cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
6603 : : cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
6604 : :
6605 : 0 : raw_spin_lock_init(&global_trace.start_lock);
6606 : :
6607 : : /* Used for event triggers */
6608 : 0 : temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
6609 [ # # ]: 0 : if (!temp_buffer)
6610 : : goto out_free_cpumask;
6611 : :
6612 : : /* TODO: make the number of buffers hot pluggable with CPUS */
6613 [ # # ]: 0 : if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
6614 : 0 : printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
6615 : 0 : WARN_ON(1);
6616 : : goto out_free_temp_buffer;
6617 : : }
6618 : :
6619 [ # # ]: 0 : if (global_trace.buffer_disabled)
6620 : 0 : tracing_off();
6621 : :
6622 : 0 : trace_init_cmdlines();
6623 : :
6624 : : /*
6625 : : * register_tracer() might reference current_trace, so it
6626 : : * needs to be set before we register anything. This is
6627 : : * just a bootstrap of current_trace anyway.
6628 : : */
6629 : 0 : global_trace.current_trace = &nop_trace;
6630 : :
6631 : 0 : register_tracer(&nop_trace);
6632 : :
6633 : : /* All seems OK, enable tracing */
6634 : 0 : tracing_disabled = 0;
6635 : :
6636 : 0 : atomic_notifier_chain_register(&panic_notifier_list,
6637 : : &trace_panic_notifier);
6638 : :
6639 : 0 : register_die_notifier(&trace_die_notifier);
6640 : :
6641 : 0 : global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
6642 : :
6643 : : INIT_LIST_HEAD(&global_trace.systems);
6644 : : INIT_LIST_HEAD(&global_trace.events);
6645 : : list_add(&global_trace.list, &ftrace_trace_arrays);
6646 : :
6647 [ # # ]: 0 : while (trace_boot_options) {
6648 : : char *option;
6649 : :
6650 : 0 : option = strsep(&trace_boot_options, ",");
6651 : 0 : trace_set_options(&global_trace, option);
6652 : : }
6653 : :
6654 : : register_snapshot_cmd();
6655 : :
6656 : 0 : return 0;
6657 : :
6658 : : out_free_temp_buffer:
6659 : 0 : ring_buffer_free(temp_buffer);
6660 : : out_free_cpumask:
6661 : 0 : free_percpu(global_trace.trace_buffer.data);
6662 : : #ifdef CONFIG_TRACER_MAX_TRACE
6663 : : free_percpu(global_trace.max_buffer.data);
6664 : : #endif
6665 : : free_cpumask_var(global_trace.tracing_cpumask);
6666 : : out_free_buffer_mask:
6667 : : free_cpumask_var(tracing_buffer_mask);
6668 : : out:
6669 : 0 : return ret;
6670 : : }
6671 : :
6672 : 0 : __init static int clear_boot_tracer(void)
6673 : : {
6674 : : /*
6675 : : * The default tracer at boot buffer is an init section.
6676 : : * This function is called in lateinit. If we did not
6677 : : * find the boot tracer, then clear it out, to prevent
6678 : : * later registration from accessing the buffer that is
6679 : : * about to be freed.
6680 : : */
6681 [ # # ]: 0 : if (!default_bootup_tracer)
6682 : : return 0;
6683 : :
6684 : 0 : printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
6685 : : default_bootup_tracer);
6686 : 0 : default_bootup_tracer = NULL;
6687 : :
6688 : 0 : return 0;
6689 : : }
6690 : :
6691 : : early_initcall(tracer_alloc_buffers);
6692 : : fs_initcall(tracer_init_debugfs);
6693 : : late_initcall(clear_boot_tracer);
|