1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace_output.c
4 *
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 *
7 */
8 #include "trace.h"
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/ftrace.h>
12 #include <linux/kprobes.h>
13 #include <linux/sched/clock.h>
14 #include <linux/sched/mm.h>
15 #include <linux/idr.h>
16 #include <linux/btf.h>
17 #include <linux/bpf.h>
18 #include <linux/hashtable.h>
19
20 #include "trace_output.h"
21 #include "trace_btf.h"
22
23 /* 2^7 = 128 */
24 #define EVENT_HASH_BITS 7
25
26 DECLARE_RWSEM(trace_event_sem);
27
28 static DEFINE_HASHTABLE(event_hash, EVENT_HASH_BITS);
29
trace_print_bputs_msg_only(struct trace_iterator * iter)30 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
31 {
32 struct trace_seq *s = &iter->seq;
33 struct trace_entry *entry = iter->ent;
34 struct bputs_entry *field;
35
36 trace_assign_type(field, entry);
37
38 trace_seq_puts(s, field->str);
39
40 return trace_handle_return(s);
41 }
42
trace_print_bprintk_msg_only(struct trace_iterator * iter)43 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
44 {
45 struct trace_seq *s = &iter->seq;
46 struct trace_entry *entry = iter->ent;
47 struct bprint_entry *field;
48
49 trace_assign_type(field, entry);
50
51 trace_seq_bprintf(s, field->fmt, field->buf);
52
53 return trace_handle_return(s);
54 }
55
trace_print_printk_msg_only(struct trace_iterator * iter)56 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
57 {
58 struct trace_seq *s = &iter->seq;
59 struct trace_entry *entry = iter->ent;
60 struct print_entry *field;
61
62 trace_assign_type(field, entry);
63
64 trace_seq_puts(s, field->buf);
65
66 return trace_handle_return(s);
67 }
68
69 const char *
trace_print_flags_seq(struct trace_seq * p,const char * delim,unsigned long flags,const struct trace_print_flags * flag_array)70 trace_print_flags_seq(struct trace_seq *p, const char *delim,
71 unsigned long flags,
72 const struct trace_print_flags *flag_array)
73 {
74 unsigned long mask;
75 const char *str;
76 const char *ret = trace_seq_buffer_ptr(p);
77 int i, first = 1;
78
79 for (i = 0; flag_array[i].name && flags; i++) {
80
81 mask = flag_array[i].mask;
82 if ((flags & mask) != mask)
83 continue;
84
85 str = flag_array[i].name;
86 flags &= ~mask;
87 if (!first && delim)
88 trace_seq_puts(p, delim);
89 else
90 first = 0;
91 trace_seq_puts(p, str);
92 }
93
94 /* check for left over flags */
95 if (flags) {
96 if (!first && delim)
97 trace_seq_puts(p, delim);
98 trace_seq_printf(p, "0x%lx", flags);
99 }
100
101 trace_seq_putc(p, 0);
102
103 return ret;
104 }
105 EXPORT_SYMBOL(trace_print_flags_seq);
106
107 const char *
trace_print_symbols_seq(struct trace_seq * p,unsigned long val,const struct trace_print_flags * symbol_array)108 trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
109 const struct trace_print_flags *symbol_array)
110 {
111 int i;
112 const char *ret = trace_seq_buffer_ptr(p);
113
114 for (i = 0; symbol_array[i].name; i++) {
115
116 if (val != symbol_array[i].mask)
117 continue;
118
119 trace_seq_puts(p, symbol_array[i].name);
120 break;
121 }
122
123 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
124 trace_seq_printf(p, "0x%lx", val);
125
126 trace_seq_putc(p, 0);
127
128 return ret;
129 }
130 EXPORT_SYMBOL(trace_print_symbols_seq);
131
132 #if BITS_PER_LONG == 32
133 const char *
trace_print_flags_seq_u64(struct trace_seq * p,const char * delim,unsigned long long flags,const struct trace_print_flags_u64 * flag_array)134 trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
135 unsigned long long flags,
136 const struct trace_print_flags_u64 *flag_array)
137 {
138 unsigned long long mask;
139 const char *str;
140 const char *ret = trace_seq_buffer_ptr(p);
141 int i, first = 1;
142
143 for (i = 0; flag_array[i].name && flags; i++) {
144
145 mask = flag_array[i].mask;
146 if ((flags & mask) != mask)
147 continue;
148
149 str = flag_array[i].name;
150 flags &= ~mask;
151 if (!first && delim)
152 trace_seq_puts(p, delim);
153 else
154 first = 0;
155 trace_seq_puts(p, str);
156 }
157
158 /* check for left over flags */
159 if (flags) {
160 if (!first && delim)
161 trace_seq_puts(p, delim);
162 trace_seq_printf(p, "0x%llx", flags);
163 }
164
165 trace_seq_putc(p, 0);
166
167 return ret;
168 }
169 EXPORT_SYMBOL(trace_print_flags_seq_u64);
170
171 const char *
trace_print_symbols_seq_u64(struct trace_seq * p,unsigned long long val,const struct trace_print_flags_u64 * symbol_array)172 trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
173 const struct trace_print_flags_u64 *symbol_array)
174 {
175 int i;
176 const char *ret = trace_seq_buffer_ptr(p);
177
178 for (i = 0; symbol_array[i].name; i++) {
179
180 if (val != symbol_array[i].mask)
181 continue;
182
183 trace_seq_puts(p, symbol_array[i].name);
184 break;
185 }
186
187 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
188 trace_seq_printf(p, "0x%llx", val);
189
190 trace_seq_putc(p, 0);
191
192 return ret;
193 }
194 EXPORT_SYMBOL(trace_print_symbols_seq_u64);
195 #endif
196
197 const char *
trace_print_bitmask_seq(struct trace_seq * p,void * bitmask_ptr,unsigned int bitmask_size)198 trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
199 unsigned int bitmask_size)
200 {
201 const char *ret = trace_seq_buffer_ptr(p);
202
203 trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
204 trace_seq_putc(p, 0);
205
206 return ret;
207 }
208 EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
209
210 /**
211 * trace_print_hex_seq - print buffer as hex sequence
212 * @p: trace seq struct to write to
213 * @buf: The buffer to print
214 * @buf_len: Length of @buf in bytes
215 * @concatenate: Print @buf as single hex string or with spacing
216 *
217 * Prints the passed buffer as a hex sequence either as a whole,
218 * single hex string if @concatenate is true or with spacing after
219 * each byte in case @concatenate is false.
220 */
221 const char *
trace_print_hex_seq(struct trace_seq * p,const unsigned char * buf,int buf_len,bool concatenate)222 trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
223 bool concatenate)
224 {
225 int i;
226 const char *ret = trace_seq_buffer_ptr(p);
227 const char *fmt = concatenate ? "%*phN" : "%*ph";
228
229 for (i = 0; i < buf_len; i += 16) {
230 if (!concatenate && i != 0)
231 trace_seq_putc(p, ' ');
232 trace_seq_printf(p, fmt, min(buf_len - i, 16), &buf[i]);
233 }
234 trace_seq_putc(p, 0);
235
236 return ret;
237 }
238 EXPORT_SYMBOL(trace_print_hex_seq);
239
240 const char *
trace_print_array_seq(struct trace_seq * p,const void * buf,int count,size_t el_size)241 trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
242 size_t el_size)
243 {
244 const char *ret = trace_seq_buffer_ptr(p);
245 const char *prefix = "";
246 void *ptr = (void *)buf;
247 size_t buf_len = count * el_size;
248
249 trace_seq_putc(p, '{');
250
251 while (ptr < buf + buf_len) {
252 switch (el_size) {
253 case 1:
254 trace_seq_printf(p, "%s0x%x", prefix,
255 *(u8 *)ptr);
256 break;
257 case 2:
258 trace_seq_printf(p, "%s0x%x", prefix,
259 *(u16 *)ptr);
260 break;
261 case 4:
262 trace_seq_printf(p, "%s0x%x", prefix,
263 *(u32 *)ptr);
264 break;
265 case 8:
266 trace_seq_printf(p, "%s0x%llx", prefix,
267 *(u64 *)ptr);
268 break;
269 default:
270 trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
271 *(u8 *)ptr);
272 el_size = 1;
273 }
274 prefix = ",";
275 ptr += el_size;
276 }
277
278 trace_seq_putc(p, '}');
279 trace_seq_putc(p, 0);
280
281 return ret;
282 }
283 EXPORT_SYMBOL(trace_print_array_seq);
284
285 const char *
trace_print_hex_dump_seq(struct trace_seq * p,const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)286 trace_print_hex_dump_seq(struct trace_seq *p, const char *prefix_str,
287 int prefix_type, int rowsize, int groupsize,
288 const void *buf, size_t len, bool ascii)
289 {
290 const char *ret = trace_seq_buffer_ptr(p);
291
292 trace_seq_putc(p, '\n');
293 trace_seq_hex_dump(p, prefix_str, prefix_type,
294 rowsize, groupsize, buf, len, ascii);
295 trace_seq_putc(p, 0);
296 return ret;
297 }
298 EXPORT_SYMBOL(trace_print_hex_dump_seq);
299
trace_raw_output_prep(struct trace_iterator * iter,struct trace_event * trace_event)300 int trace_raw_output_prep(struct trace_iterator *iter,
301 struct trace_event *trace_event)
302 {
303 struct trace_event_call *event;
304 struct trace_seq *s = &iter->seq;
305 struct trace_seq *p = &iter->tmp_seq;
306 struct trace_entry *entry;
307
308 event = container_of(trace_event, struct trace_event_call, event);
309 entry = iter->ent;
310
311 if (entry->type != event->event.type) {
312 WARN_ON_ONCE(1);
313 return TRACE_TYPE_UNHANDLED;
314 }
315
316 trace_seq_init(p);
317 trace_seq_printf(s, "%s: ", trace_event_name(event));
318
319 return trace_handle_return(s);
320 }
321 EXPORT_SYMBOL(trace_raw_output_prep);
322
trace_event_printf(struct trace_iterator * iter,const char * fmt,...)323 void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...)
324 {
325 struct trace_seq *s = &iter->seq;
326 va_list ap;
327
328 if (ignore_event(iter))
329 return;
330
331 va_start(ap, fmt);
332 trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
333 va_end(ap);
334 }
335 EXPORT_SYMBOL(trace_event_printf);
336
337 static __printf(3, 0)
trace_output_raw(struct trace_iterator * iter,char * name,char * fmt,va_list ap)338 int trace_output_raw(struct trace_iterator *iter, char *name,
339 char *fmt, va_list ap)
340 {
341 struct trace_seq *s = &iter->seq;
342
343 trace_seq_printf(s, "%s: ", name);
344 trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
345
346 return trace_handle_return(s);
347 }
348
trace_output_call(struct trace_iterator * iter,char * name,char * fmt,...)349 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
350 {
351 va_list ap;
352 int ret;
353
354 va_start(ap, fmt);
355 ret = trace_output_raw(iter, name, fmt, ap);
356 va_end(ap);
357
358 return ret;
359 }
360 EXPORT_SYMBOL_GPL(trace_output_call);
361
kretprobed(const char * name,unsigned long addr)362 static inline const char *kretprobed(const char *name, unsigned long addr)
363 {
364 if (is_kretprobe_trampoline(addr))
365 return "[unknown/kretprobe'd]";
366 return name;
367 }
368
369 void
trace_seq_print_sym(struct trace_seq * s,unsigned long address,bool offset)370 trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
371 {
372 #ifdef CONFIG_KALLSYMS
373 char str[KSYM_SYMBOL_LEN];
374 const char *name;
375
376 if (offset)
377 sprint_symbol(str, address);
378 else
379 kallsyms_lookup(address, NULL, NULL, NULL, str);
380 name = kretprobed(str, address);
381
382 if (name && strlen(name)) {
383 trace_seq_puts(s, name);
384 return;
385 }
386 #endif
387 trace_seq_printf(s, "0x%08lx", address);
388 }
389
390 #ifndef CONFIG_64BIT
391 # define IP_FMT "%08lx"
392 #else
393 # define IP_FMT "%016lx"
394 #endif
395
seq_print_user_ip(struct trace_seq * s,struct mm_struct * mm,unsigned long ip,unsigned long sym_flags)396 static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
397 unsigned long ip, unsigned long sym_flags)
398 {
399 struct file *file = NULL;
400 unsigned long vmstart = 0;
401 int ret = 1;
402
403 if (s->full)
404 return 0;
405
406 if (mm) {
407 const struct vm_area_struct *vma;
408
409 mmap_read_lock(mm);
410 vma = find_vma(mm, ip);
411 if (vma) {
412 file = vma->vm_file;
413 vmstart = vma->vm_start;
414 }
415 if (file) {
416 ret = trace_seq_path(s, file_user_path(file));
417 if (ret)
418 trace_seq_printf(s, "[+0x%lx]",
419 ip - vmstart);
420 }
421 mmap_read_unlock(mm);
422 }
423 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
424 trace_seq_printf(s, " <" IP_FMT ">", ip);
425 return !trace_seq_has_overflowed(s);
426 }
427
428 int
seq_print_ip_sym(struct trace_seq * s,unsigned long ip,unsigned long sym_flags)429 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
430 {
431 if (!ip) {
432 trace_seq_putc(s, '0');
433 goto out;
434 }
435
436 trace_seq_print_sym(s, ip, sym_flags & TRACE_ITER_SYM_OFFSET);
437
438 if (sym_flags & TRACE_ITER_SYM_ADDR)
439 trace_seq_printf(s, " <" IP_FMT ">", ip);
440
441 out:
442 return !trace_seq_has_overflowed(s);
443 }
444
445 /**
446 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
447 * @s: trace seq struct to write to
448 * @entry: The trace entry field from the ring buffer
449 *
450 * Prints the generic fields of irqs off, in hard or softirq, preempt
451 * count.
452 */
trace_print_lat_fmt(struct trace_seq * s,struct trace_entry * entry)453 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
454 {
455 char hardsoft_irq;
456 char need_resched;
457 char irqs_off;
458 int hardirq;
459 int softirq;
460 int bh_off;
461 int nmi;
462
463 nmi = entry->flags & TRACE_FLAG_NMI;
464 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
465 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
466 bh_off = entry->flags & TRACE_FLAG_BH_OFF;
467
468 irqs_off =
469 (entry->flags & TRACE_FLAG_IRQS_OFF && bh_off) ? 'D' :
470 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
471 bh_off ? 'b' :
472 '.';
473
474 switch (entry->flags & (TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY |
475 TRACE_FLAG_PREEMPT_RESCHED)) {
476 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY | TRACE_FLAG_PREEMPT_RESCHED:
477 need_resched = 'B';
478 break;
479 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
480 need_resched = 'N';
481 break;
482 case TRACE_FLAG_NEED_RESCHED_LAZY | TRACE_FLAG_PREEMPT_RESCHED:
483 need_resched = 'L';
484 break;
485 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY:
486 need_resched = 'b';
487 break;
488 case TRACE_FLAG_NEED_RESCHED:
489 need_resched = 'n';
490 break;
491 case TRACE_FLAG_PREEMPT_RESCHED:
492 need_resched = 'p';
493 break;
494 case TRACE_FLAG_NEED_RESCHED_LAZY:
495 need_resched = 'l';
496 break;
497 default:
498 need_resched = '.';
499 break;
500 }
501
502 hardsoft_irq =
503 (nmi && hardirq) ? 'Z' :
504 nmi ? 'z' :
505 (hardirq && softirq) ? 'H' :
506 hardirq ? 'h' :
507 softirq ? 's' :
508 '.' ;
509
510 trace_seq_printf(s, "%c%c%c",
511 irqs_off, need_resched, hardsoft_irq);
512
513 if (entry->preempt_count & 0xf)
514 trace_seq_printf(s, "%x", entry->preempt_count & 0xf);
515 else
516 trace_seq_putc(s, '.');
517
518 if (entry->preempt_count & 0xf0)
519 trace_seq_printf(s, "%x", entry->preempt_count >> 4);
520 else
521 trace_seq_putc(s, '.');
522
523 return !trace_seq_has_overflowed(s);
524 }
525
526 static int
lat_print_generic(struct trace_seq * s,struct trace_entry * entry,int cpu)527 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
528 {
529 char comm[TASK_COMM_LEN];
530
531 trace_find_cmdline(entry->pid, comm);
532
533 trace_seq_printf(s, "%8.8s-%-7d %3d",
534 comm, entry->pid, cpu);
535
536 return trace_print_lat_fmt(s, entry);
537 }
538
539 #undef MARK
540 #define MARK(v, s) {.val = v, .sym = s}
541 /* trace overhead mark */
542 static const struct trace_mark {
543 unsigned long long val; /* unit: nsec */
544 char sym;
545 } mark[] = {
546 MARK(1000000000ULL , '$'), /* 1 sec */
547 MARK(100000000ULL , '@'), /* 100 msec */
548 MARK(10000000ULL , '*'), /* 10 msec */
549 MARK(1000000ULL , '#'), /* 1000 usecs */
550 MARK(100000ULL , '!'), /* 100 usecs */
551 MARK(10000ULL , '+'), /* 10 usecs */
552 };
553 #undef MARK
554
trace_find_mark(unsigned long long d)555 char trace_find_mark(unsigned long long d)
556 {
557 int i;
558 int size = ARRAY_SIZE(mark);
559
560 for (i = 0; i < size; i++) {
561 if (d > mark[i].val)
562 break;
563 }
564
565 return (i == size) ? ' ' : mark[i].sym;
566 }
567
568 static int
lat_print_timestamp(struct trace_iterator * iter,u64 next_ts)569 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
570 {
571 struct trace_array *tr = iter->tr;
572 unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
573 unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
574 unsigned long long abs_ts = iter->ts - iter->array_buffer->time_start;
575 unsigned long long rel_ts = next_ts - iter->ts;
576 struct trace_seq *s = &iter->seq;
577
578 if (in_ns) {
579 abs_ts = ns2usecs(abs_ts);
580 rel_ts = ns2usecs(rel_ts);
581 }
582
583 if (verbose && in_ns) {
584 unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
585 unsigned long abs_msec = (unsigned long)abs_ts;
586 unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
587 unsigned long rel_msec = (unsigned long)rel_ts;
588
589 trace_seq_printf(
590 s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
591 ns2usecs(iter->ts),
592 abs_msec, abs_usec,
593 rel_msec, rel_usec);
594
595 } else if (verbose && !in_ns) {
596 trace_seq_printf(
597 s, "[%016llx] %lld (+%lld): ",
598 iter->ts, abs_ts, rel_ts);
599
600 } else if (!verbose && in_ns) {
601 trace_seq_printf(
602 s, " %4lldus%c: ",
603 abs_ts,
604 trace_find_mark(rel_ts * NSEC_PER_USEC));
605
606 } else { /* !verbose && !in_ns */
607 trace_seq_printf(s, " %4lld: ", abs_ts);
608 }
609
610 return !trace_seq_has_overflowed(s);
611 }
612
trace_print_time(struct trace_seq * s,struct trace_iterator * iter,unsigned long long ts)613 static void trace_print_time(struct trace_seq *s, struct trace_iterator *iter,
614 unsigned long long ts)
615 {
616 unsigned long secs, usec_rem;
617 unsigned long long t;
618
619 if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
620 t = ns2usecs(ts);
621 usec_rem = do_div(t, USEC_PER_SEC);
622 secs = (unsigned long)t;
623 trace_seq_printf(s, " %5lu.%06lu", secs, usec_rem);
624 } else
625 trace_seq_printf(s, " %12llu", ts);
626 }
627
trace_print_context(struct trace_iterator * iter)628 int trace_print_context(struct trace_iterator *iter)
629 {
630 struct trace_array *tr = iter->tr;
631 struct trace_seq *s = &iter->seq;
632 struct trace_entry *entry = iter->ent;
633 char comm[TASK_COMM_LEN];
634
635 trace_find_cmdline(entry->pid, comm);
636
637 trace_seq_printf(s, "%16s-%-7d ", comm, entry->pid);
638
639 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
640 unsigned int tgid = trace_find_tgid(entry->pid);
641
642 if (!tgid)
643 trace_seq_printf(s, "(-------) ");
644 else
645 trace_seq_printf(s, "(%7d) ", tgid);
646 }
647
648 trace_seq_printf(s, "[%03d] ", iter->cpu);
649
650 if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
651 trace_print_lat_fmt(s, entry);
652
653 trace_print_time(s, iter, iter->ts);
654 trace_seq_puts(s, ": ");
655
656 return !trace_seq_has_overflowed(s);
657 }
658
trace_print_lat_context(struct trace_iterator * iter)659 int trace_print_lat_context(struct trace_iterator *iter)
660 {
661 struct trace_entry *entry, *next_entry;
662 struct trace_array *tr = iter->tr;
663 struct trace_seq *s = &iter->seq;
664 unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
665 u64 next_ts;
666
667 next_entry = trace_find_next_entry(iter, NULL, &next_ts);
668 if (!next_entry)
669 next_ts = iter->ts;
670
671 /* trace_find_next_entry() may change iter->ent */
672 entry = iter->ent;
673
674 if (verbose) {
675 char comm[TASK_COMM_LEN];
676
677 trace_find_cmdline(entry->pid, comm);
678
679 trace_seq_printf(
680 s, "%16s %7d %3d %d %08x %08lx ",
681 comm, entry->pid, iter->cpu, entry->flags,
682 entry->preempt_count & 0xf, iter->idx);
683 } else {
684 lat_print_generic(s, entry, iter->cpu);
685 }
686
687 lat_print_timestamp(iter, next_ts);
688
689 return !trace_seq_has_overflowed(s);
690 }
691
692 #ifdef CONFIG_FUNCTION_TRACE_ARGS
print_function_args(struct trace_seq * s,unsigned long * args,unsigned long func)693 void print_function_args(struct trace_seq *s, unsigned long *args,
694 unsigned long func)
695 {
696 const struct btf_param *param;
697 const struct btf_type *t;
698 const char *param_name;
699 char name[KSYM_NAME_LEN];
700 unsigned long arg;
701 struct btf *btf;
702 s32 tid, nr = 0;
703 int a, p, x;
704
705 trace_seq_printf(s, "(");
706
707 if (!args)
708 goto out;
709 if (lookup_symbol_name(func, name))
710 goto out;
711
712 /* TODO: Pass module name here too */
713 t = btf_find_func_proto(name, &btf);
714 if (IS_ERR_OR_NULL(t))
715 goto out;
716
717 param = btf_get_func_param(t, &nr);
718 if (!param)
719 goto out_put;
720
721 for (a = 0, p = 0; p < nr; a++, p++) {
722 if (p)
723 trace_seq_puts(s, ", ");
724
725 /* This only prints what the arch allows (6 args by default) */
726 if (a == FTRACE_REGS_MAX_ARGS) {
727 trace_seq_puts(s, "...");
728 break;
729 }
730
731 arg = args[a];
732
733 param_name = btf_name_by_offset(btf, param[p].name_off);
734 if (param_name)
735 trace_seq_printf(s, "%s=", param_name);
736 t = btf_type_skip_modifiers(btf, param[p].type, &tid);
737
738 switch (t ? BTF_INFO_KIND(t->info) : BTF_KIND_UNKN) {
739 case BTF_KIND_UNKN:
740 trace_seq_putc(s, '?');
741 /* Still print unknown type values */
742 fallthrough;
743 case BTF_KIND_PTR:
744 trace_seq_printf(s, "0x%lx", arg);
745 break;
746 case BTF_KIND_INT:
747 trace_seq_printf(s, "%ld", arg);
748 break;
749 case BTF_KIND_ENUM:
750 trace_seq_printf(s, "%ld", arg);
751 break;
752 default:
753 /* This does not handle complex arguments */
754 trace_seq_printf(s, "(%s)[0x%lx", btf_type_str(t), arg);
755 for (x = sizeof(long); x < t->size; x += sizeof(long)) {
756 trace_seq_putc(s, ':');
757 if (++a == FTRACE_REGS_MAX_ARGS) {
758 trace_seq_puts(s, "...]");
759 goto out_put;
760 }
761 trace_seq_printf(s, "0x%lx", args[a]);
762 }
763 trace_seq_putc(s, ']');
764 break;
765 }
766 }
767 out_put:
768 btf_put(btf);
769 out:
770 trace_seq_printf(s, ")");
771 }
772 #endif
773
774 /**
775 * ftrace_find_event - find a registered event
776 * @type: the type of event to look for
777 *
778 * Returns an event of type @type otherwise NULL
779 * Called with trace_event_read_lock() held.
780 */
ftrace_find_event(int type)781 struct trace_event *ftrace_find_event(int type)
782 {
783 struct trace_event *event;
784
785 hash_for_each_possible(event_hash, event, node, type) {
786 if (event->type == type)
787 return event;
788 }
789
790 return NULL;
791 }
792
793 static DEFINE_IDA(trace_event_ida);
794
free_trace_event_type(int type)795 static void free_trace_event_type(int type)
796 {
797 if (type >= __TRACE_LAST_TYPE)
798 ida_free(&trace_event_ida, type);
799 }
800
alloc_trace_event_type(void)801 static int alloc_trace_event_type(void)
802 {
803 int next;
804
805 /* Skip static defined type numbers */
806 next = ida_alloc_range(&trace_event_ida, __TRACE_LAST_TYPE,
807 TRACE_EVENT_TYPE_MAX, GFP_KERNEL);
808 if (next < 0)
809 return 0;
810 return next;
811 }
812
trace_event_read_lock(void)813 void trace_event_read_lock(void)
814 {
815 down_read(&trace_event_sem);
816 }
817
trace_event_read_unlock(void)818 void trace_event_read_unlock(void)
819 {
820 up_read(&trace_event_sem);
821 }
822
823 /**
824 * register_trace_event - register output for an event type
825 * @event: the event type to register
826 *
827 * Event types are stored in a hash and this hash is used to
828 * find a way to print an event. If the @event->type is set
829 * then it will use that type, otherwise it will assign a
830 * type to use.
831 *
832 * If you assign your own type, please make sure it is added
833 * to the trace_type enum in trace.h, to avoid collisions
834 * with the dynamic types.
835 *
836 * Returns the event type number or zero on error.
837 */
register_trace_event(struct trace_event * event)838 int register_trace_event(struct trace_event *event)
839 {
840 int ret = 0;
841
842 down_write(&trace_event_sem);
843
844 if (WARN_ON(!event))
845 goto out;
846
847 if (WARN_ON(!event->funcs))
848 goto out;
849
850 if (!event->type) {
851 event->type = alloc_trace_event_type();
852 if (!event->type)
853 goto out;
854 } else if (WARN(event->type > __TRACE_LAST_TYPE,
855 "Need to add type to trace.h")) {
856 goto out;
857 } else {
858 /* Is this event already used */
859 if (ftrace_find_event(event->type))
860 goto out;
861 }
862
863 if (event->funcs->trace == NULL)
864 event->funcs->trace = trace_nop_print;
865 if (event->funcs->raw == NULL)
866 event->funcs->raw = trace_nop_print;
867 if (event->funcs->hex == NULL)
868 event->funcs->hex = trace_nop_print;
869 if (event->funcs->binary == NULL)
870 event->funcs->binary = trace_nop_print;
871
872 hash_add(event_hash, &event->node, event->type);
873
874 ret = event->type;
875 out:
876 up_write(&trace_event_sem);
877
878 return ret;
879 }
880 EXPORT_SYMBOL_GPL(register_trace_event);
881
882 /*
883 * Used by module code with the trace_event_sem held for write.
884 */
__unregister_trace_event(struct trace_event * event)885 int __unregister_trace_event(struct trace_event *event)
886 {
887 hash_del(&event->node);
888 free_trace_event_type(event->type);
889 return 0;
890 }
891
892 /**
893 * unregister_trace_event - remove a no longer used event
894 * @event: the event to remove
895 */
unregister_trace_event(struct trace_event * event)896 int unregister_trace_event(struct trace_event *event)
897 {
898 down_write(&trace_event_sem);
899 __unregister_trace_event(event);
900 up_write(&trace_event_sem);
901
902 return 0;
903 }
904 EXPORT_SYMBOL_GPL(unregister_trace_event);
905
906 /*
907 * Standard events
908 */
909
print_array(struct trace_iterator * iter,void * pos,struct ftrace_event_field * field)910 static void print_array(struct trace_iterator *iter, void *pos,
911 struct ftrace_event_field *field)
912 {
913 int offset;
914 int len;
915 int i;
916
917 offset = *(int *)pos & 0xffff;
918 len = *(int *)pos >> 16;
919
920 if (field)
921 offset += field->offset + sizeof(int);
922
923 if (offset + len > iter->ent_size) {
924 trace_seq_puts(&iter->seq, "<OVERFLOW>");
925 return;
926 }
927
928 pos = (void *)iter->ent + offset;
929
930 for (i = 0; i < len; i++, pos++) {
931 if (i)
932 trace_seq_putc(&iter->seq, ',');
933 trace_seq_printf(&iter->seq, "%02x", *(unsigned char *)pos);
934 }
935 }
936
print_fields(struct trace_iterator * iter,struct trace_event_call * call,struct list_head * head)937 static void print_fields(struct trace_iterator *iter, struct trace_event_call *call,
938 struct list_head *head)
939 {
940 struct ftrace_event_field *field;
941 int offset;
942 int len;
943 int ret;
944 void *pos;
945
946 list_for_each_entry_reverse(field, head, link) {
947 trace_seq_printf(&iter->seq, " %s=", field->name);
948 if (field->offset + field->size > iter->ent_size) {
949 trace_seq_puts(&iter->seq, "<OVERFLOW>");
950 continue;
951 }
952 pos = (void *)iter->ent + field->offset;
953
954 switch (field->filter_type) {
955 case FILTER_COMM:
956 case FILTER_STATIC_STRING:
957 trace_seq_printf(&iter->seq, "%.*s", field->size, (char *)pos);
958 break;
959 case FILTER_RDYN_STRING:
960 case FILTER_DYN_STRING:
961 offset = *(int *)pos & 0xffff;
962 len = *(int *)pos >> 16;
963
964 if (field->filter_type == FILTER_RDYN_STRING)
965 offset += field->offset + sizeof(int);
966
967 if (offset + len > iter->ent_size) {
968 trace_seq_puts(&iter->seq, "<OVERFLOW>");
969 break;
970 }
971 pos = (void *)iter->ent + offset;
972 trace_seq_printf(&iter->seq, "%.*s", len, (char *)pos);
973 break;
974 case FILTER_PTR_STRING:
975 if (!iter->fmt_size)
976 trace_iter_expand_format(iter);
977 pos = *(void **)pos;
978 ret = strncpy_from_kernel_nofault(iter->fmt, pos,
979 iter->fmt_size);
980 if (ret < 0)
981 trace_seq_printf(&iter->seq, "(0x%px)", pos);
982 else
983 trace_seq_printf(&iter->seq, "(0x%px:%s)",
984 pos, iter->fmt);
985 break;
986 case FILTER_TRACE_FN:
987 pos = *(void **)pos;
988 trace_seq_printf(&iter->seq, "%pS", pos);
989 break;
990 case FILTER_CPU:
991 case FILTER_OTHER:
992 switch (field->size) {
993 case 1:
994 if (isprint(*(char *)pos)) {
995 trace_seq_printf(&iter->seq, "'%c'",
996 *(unsigned char *)pos);
997 }
998 trace_seq_printf(&iter->seq, "(%d)",
999 *(unsigned char *)pos);
1000 break;
1001 case 2:
1002 trace_seq_printf(&iter->seq, "0x%x (%d)",
1003 *(unsigned short *)pos,
1004 *(unsigned short *)pos);
1005 break;
1006 case 4:
1007 /* dynamic array info is 4 bytes */
1008 if (strstr(field->type, "__data_loc")) {
1009 print_array(iter, pos, NULL);
1010 break;
1011 }
1012
1013 if (strstr(field->type, "__rel_loc")) {
1014 print_array(iter, pos, field);
1015 break;
1016 }
1017
1018 trace_seq_printf(&iter->seq, "0x%x (%d)",
1019 *(unsigned int *)pos,
1020 *(unsigned int *)pos);
1021 break;
1022 case 8:
1023 trace_seq_printf(&iter->seq, "0x%llx (%lld)",
1024 *(unsigned long long *)pos,
1025 *(unsigned long long *)pos);
1026 break;
1027 default:
1028 trace_seq_puts(&iter->seq, "<INVALID-SIZE>");
1029 break;
1030 }
1031 break;
1032 default:
1033 trace_seq_puts(&iter->seq, "<INVALID-TYPE>");
1034 }
1035 }
1036 trace_seq_putc(&iter->seq, '\n');
1037 }
1038
print_event_fields(struct trace_iterator * iter,struct trace_event * event)1039 enum print_line_t print_event_fields(struct trace_iterator *iter,
1040 struct trace_event *event)
1041 {
1042 struct trace_event_call *call;
1043 struct list_head *head;
1044
1045 lockdep_assert_held_read(&trace_event_sem);
1046
1047 /* ftrace defined events have separate call structures */
1048 if (event->type <= __TRACE_LAST_TYPE) {
1049 bool found = false;
1050
1051 list_for_each_entry(call, &ftrace_events, list) {
1052 if (call->event.type == event->type) {
1053 found = true;
1054 break;
1055 }
1056 /* No need to search all events */
1057 if (call->event.type > __TRACE_LAST_TYPE)
1058 break;
1059 }
1060 if (!found) {
1061 trace_seq_printf(&iter->seq, "UNKNOWN TYPE %d\n", event->type);
1062 goto out;
1063 }
1064 } else {
1065 call = container_of(event, struct trace_event_call, event);
1066 }
1067 head = trace_get_fields(call);
1068
1069 trace_seq_printf(&iter->seq, "%s:", trace_event_name(call));
1070
1071 if (head && !list_empty(head))
1072 print_fields(iter, call, head);
1073 else
1074 trace_seq_puts(&iter->seq, "No fields found\n");
1075
1076 out:
1077 return trace_handle_return(&iter->seq);
1078 }
1079
trace_nop_print(struct trace_iterator * iter,int flags,struct trace_event * event)1080 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
1081 struct trace_event *event)
1082 {
1083 trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
1084
1085 return trace_handle_return(&iter->seq);
1086 }
1087
print_fn_trace(struct trace_seq * s,unsigned long ip,unsigned long parent_ip,long delta,unsigned long * args,int flags)1088 static void print_fn_trace(struct trace_seq *s, unsigned long ip,
1089 unsigned long parent_ip, long delta,
1090 unsigned long *args, int flags)
1091 {
1092 ip += delta;
1093 parent_ip += delta;
1094
1095 seq_print_ip_sym(s, ip, flags);
1096 if (args)
1097 print_function_args(s, args, ip);
1098
1099 if ((flags & TRACE_ITER_PRINT_PARENT) && parent_ip) {
1100 trace_seq_puts(s, " <-");
1101 seq_print_ip_sym(s, parent_ip, flags);
1102 }
1103 }
1104
1105 /* TRACE_FN */
trace_fn_trace(struct trace_iterator * iter,int flags,struct trace_event * event)1106 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
1107 struct trace_event *event)
1108 {
1109 struct ftrace_entry *field;
1110 struct trace_seq *s = &iter->seq;
1111 unsigned long *args;
1112 int args_size;
1113
1114 trace_assign_type(field, iter->ent);
1115
1116 args_size = iter->ent_size - offsetof(struct ftrace_entry, args);
1117 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long))
1118 args = field->args;
1119 else
1120 args = NULL;
1121
1122 print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta,
1123 args, flags);
1124 trace_seq_putc(s, '\n');
1125
1126 return trace_handle_return(s);
1127 }
1128
trace_fn_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1129 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
1130 struct trace_event *event)
1131 {
1132 struct ftrace_entry *field;
1133
1134 trace_assign_type(field, iter->ent);
1135
1136 trace_seq_printf(&iter->seq, "%lx %lx\n",
1137 field->ip,
1138 field->parent_ip);
1139
1140 return trace_handle_return(&iter->seq);
1141 }
1142
trace_fn_hex(struct trace_iterator * iter,int flags,struct trace_event * event)1143 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
1144 struct trace_event *event)
1145 {
1146 struct ftrace_entry *field;
1147 struct trace_seq *s = &iter->seq;
1148
1149 trace_assign_type(field, iter->ent);
1150
1151 SEQ_PUT_HEX_FIELD(s, field->ip);
1152 SEQ_PUT_HEX_FIELD(s, field->parent_ip);
1153
1154 return trace_handle_return(s);
1155 }
1156
trace_fn_bin(struct trace_iterator * iter,int flags,struct trace_event * event)1157 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
1158 struct trace_event *event)
1159 {
1160 struct ftrace_entry *field;
1161 struct trace_seq *s = &iter->seq;
1162
1163 trace_assign_type(field, iter->ent);
1164
1165 SEQ_PUT_FIELD(s, field->ip);
1166 SEQ_PUT_FIELD(s, field->parent_ip);
1167
1168 return trace_handle_return(s);
1169 }
1170
1171 static struct trace_event_functions trace_fn_funcs = {
1172 .trace = trace_fn_trace,
1173 .raw = trace_fn_raw,
1174 .hex = trace_fn_hex,
1175 .binary = trace_fn_bin,
1176 };
1177
1178 static struct trace_event trace_fn_event = {
1179 .type = TRACE_FN,
1180 .funcs = &trace_fn_funcs,
1181 };
1182
1183 /* TRACE_CTX an TRACE_WAKE */
trace_ctxwake_print(struct trace_iterator * iter,char * delim)1184 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
1185 char *delim)
1186 {
1187 struct ctx_switch_entry *field;
1188 char comm[TASK_COMM_LEN];
1189 int S, T;
1190
1191
1192 trace_assign_type(field, iter->ent);
1193
1194 T = task_index_to_char(field->next_state);
1195 S = task_index_to_char(field->prev_state);
1196 trace_find_cmdline(field->next_pid, comm);
1197 trace_seq_printf(&iter->seq,
1198 " %7d:%3d:%c %s [%03d] %7d:%3d:%c %s\n",
1199 field->prev_pid,
1200 field->prev_prio,
1201 S, delim,
1202 field->next_cpu,
1203 field->next_pid,
1204 field->next_prio,
1205 T, comm);
1206
1207 return trace_handle_return(&iter->seq);
1208 }
1209
trace_ctx_print(struct trace_iterator * iter,int flags,struct trace_event * event)1210 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
1211 struct trace_event *event)
1212 {
1213 return trace_ctxwake_print(iter, "==>");
1214 }
1215
trace_wake_print(struct trace_iterator * iter,int flags,struct trace_event * event)1216 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1217 int flags, struct trace_event *event)
1218 {
1219 return trace_ctxwake_print(iter, " +");
1220 }
1221
trace_ctxwake_raw(struct trace_iterator * iter,char S)1222 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1223 {
1224 struct ctx_switch_entry *field;
1225 int T;
1226
1227 trace_assign_type(field, iter->ent);
1228
1229 if (!S)
1230 S = task_index_to_char(field->prev_state);
1231 T = task_index_to_char(field->next_state);
1232 trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
1233 field->prev_pid,
1234 field->prev_prio,
1235 S,
1236 field->next_cpu,
1237 field->next_pid,
1238 field->next_prio,
1239 T);
1240
1241 return trace_handle_return(&iter->seq);
1242 }
1243
trace_ctx_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1244 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
1245 struct trace_event *event)
1246 {
1247 return trace_ctxwake_raw(iter, 0);
1248 }
1249
trace_wake_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1250 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
1251 struct trace_event *event)
1252 {
1253 return trace_ctxwake_raw(iter, '+');
1254 }
1255
1256
trace_ctxwake_hex(struct trace_iterator * iter,char S)1257 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1258 {
1259 struct ctx_switch_entry *field;
1260 struct trace_seq *s = &iter->seq;
1261 int T;
1262
1263 trace_assign_type(field, iter->ent);
1264
1265 if (!S)
1266 S = task_index_to_char(field->prev_state);
1267 T = task_index_to_char(field->next_state);
1268
1269 SEQ_PUT_HEX_FIELD(s, field->prev_pid);
1270 SEQ_PUT_HEX_FIELD(s, field->prev_prio);
1271 SEQ_PUT_HEX_FIELD(s, S);
1272 SEQ_PUT_HEX_FIELD(s, field->next_cpu);
1273 SEQ_PUT_HEX_FIELD(s, field->next_pid);
1274 SEQ_PUT_HEX_FIELD(s, field->next_prio);
1275 SEQ_PUT_HEX_FIELD(s, T);
1276
1277 return trace_handle_return(s);
1278 }
1279
trace_ctx_hex(struct trace_iterator * iter,int flags,struct trace_event * event)1280 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1281 struct trace_event *event)
1282 {
1283 return trace_ctxwake_hex(iter, 0);
1284 }
1285
trace_wake_hex(struct trace_iterator * iter,int flags,struct trace_event * event)1286 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1287 struct trace_event *event)
1288 {
1289 return trace_ctxwake_hex(iter, '+');
1290 }
1291
trace_ctxwake_bin(struct trace_iterator * iter,int flags,struct trace_event * event)1292 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1293 int flags, struct trace_event *event)
1294 {
1295 struct ctx_switch_entry *field;
1296 struct trace_seq *s = &iter->seq;
1297
1298 trace_assign_type(field, iter->ent);
1299
1300 SEQ_PUT_FIELD(s, field->prev_pid);
1301 SEQ_PUT_FIELD(s, field->prev_prio);
1302 SEQ_PUT_FIELD(s, field->prev_state);
1303 SEQ_PUT_FIELD(s, field->next_cpu);
1304 SEQ_PUT_FIELD(s, field->next_pid);
1305 SEQ_PUT_FIELD(s, field->next_prio);
1306 SEQ_PUT_FIELD(s, field->next_state);
1307
1308 return trace_handle_return(s);
1309 }
1310
1311 static struct trace_event_functions trace_ctx_funcs = {
1312 .trace = trace_ctx_print,
1313 .raw = trace_ctx_raw,
1314 .hex = trace_ctx_hex,
1315 .binary = trace_ctxwake_bin,
1316 };
1317
1318 static struct trace_event trace_ctx_event = {
1319 .type = TRACE_CTX,
1320 .funcs = &trace_ctx_funcs,
1321 };
1322
1323 static struct trace_event_functions trace_wake_funcs = {
1324 .trace = trace_wake_print,
1325 .raw = trace_wake_raw,
1326 .hex = trace_wake_hex,
1327 .binary = trace_ctxwake_bin,
1328 };
1329
1330 static struct trace_event trace_wake_event = {
1331 .type = TRACE_WAKE,
1332 .funcs = &trace_wake_funcs,
1333 };
1334
1335 /* TRACE_STACK */
1336
trace_stack_print(struct trace_iterator * iter,int flags,struct trace_event * event)1337 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1338 int flags, struct trace_event *event)
1339 {
1340 struct stack_entry *field;
1341 struct trace_seq *s = &iter->seq;
1342 unsigned long *p;
1343 unsigned long *end;
1344
1345 trace_assign_type(field, iter->ent);
1346 end = (unsigned long *)((long)iter->ent + iter->ent_size);
1347
1348 trace_seq_puts(s, "<stack trace>\n");
1349
1350 for (p = field->caller; p && p < end && *p != ULONG_MAX; p++) {
1351
1352 if (trace_seq_has_overflowed(s))
1353 break;
1354
1355 trace_seq_puts(s, " => ");
1356 if ((*p) == FTRACE_TRAMPOLINE_MARKER) {
1357 trace_seq_puts(s, "[FTRACE TRAMPOLINE]\n");
1358 continue;
1359 }
1360 seq_print_ip_sym(s, trace_adjust_address(iter->tr, *p), flags);
1361 trace_seq_putc(s, '\n');
1362 }
1363
1364 return trace_handle_return(s);
1365 }
1366
1367 static struct trace_event_functions trace_stack_funcs = {
1368 .trace = trace_stack_print,
1369 };
1370
1371 static struct trace_event trace_stack_event = {
1372 .type = TRACE_STACK,
1373 .funcs = &trace_stack_funcs,
1374 };
1375
1376 /* TRACE_USER_STACK */
trace_user_stack_print(struct trace_iterator * iter,int flags,struct trace_event * event)1377 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1378 int flags, struct trace_event *event)
1379 {
1380 struct trace_array *tr = iter->tr;
1381 struct userstack_entry *field;
1382 struct trace_seq *s = &iter->seq;
1383 struct mm_struct *mm = NULL;
1384 unsigned int i;
1385
1386 trace_assign_type(field, iter->ent);
1387
1388 trace_seq_puts(s, "<user stack trace>\n");
1389
1390 if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1391 struct task_struct *task;
1392 /*
1393 * we do the lookup on the thread group leader,
1394 * since individual threads might have already quit!
1395 */
1396 rcu_read_lock();
1397 task = find_task_by_vpid(field->tgid);
1398 if (task)
1399 mm = get_task_mm(task);
1400 rcu_read_unlock();
1401 }
1402
1403 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1404 unsigned long ip = field->caller[i];
1405
1406 if (!ip || trace_seq_has_overflowed(s))
1407 break;
1408
1409 trace_seq_puts(s, " => ");
1410 seq_print_user_ip(s, mm, ip, flags);
1411 trace_seq_putc(s, '\n');
1412 }
1413
1414 if (mm)
1415 mmput(mm);
1416
1417 return trace_handle_return(s);
1418 }
1419
1420 static struct trace_event_functions trace_user_stack_funcs = {
1421 .trace = trace_user_stack_print,
1422 };
1423
1424 static struct trace_event trace_user_stack_event = {
1425 .type = TRACE_USER_STACK,
1426 .funcs = &trace_user_stack_funcs,
1427 };
1428
1429 /* TRACE_HWLAT */
1430 static enum print_line_t
trace_hwlat_print(struct trace_iterator * iter,int flags,struct trace_event * event)1431 trace_hwlat_print(struct trace_iterator *iter, int flags,
1432 struct trace_event *event)
1433 {
1434 struct trace_entry *entry = iter->ent;
1435 struct trace_seq *s = &iter->seq;
1436 struct hwlat_entry *field;
1437
1438 trace_assign_type(field, entry);
1439
1440 trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%lld.%09ld count:%d",
1441 field->seqnum,
1442 field->duration,
1443 field->outer_duration,
1444 (long long)field->timestamp.tv_sec,
1445 field->timestamp.tv_nsec, field->count);
1446
1447 if (field->nmi_count) {
1448 /*
1449 * The generic sched_clock() is not NMI safe, thus
1450 * we only record the count and not the time.
1451 */
1452 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
1453 trace_seq_printf(s, " nmi-total:%llu",
1454 field->nmi_total_ts);
1455 trace_seq_printf(s, " nmi-count:%u",
1456 field->nmi_count);
1457 }
1458
1459 trace_seq_putc(s, '\n');
1460
1461 return trace_handle_return(s);
1462 }
1463
1464 static enum print_line_t
trace_hwlat_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1465 trace_hwlat_raw(struct trace_iterator *iter, int flags,
1466 struct trace_event *event)
1467 {
1468 struct hwlat_entry *field;
1469 struct trace_seq *s = &iter->seq;
1470
1471 trace_assign_type(field, iter->ent);
1472
1473 trace_seq_printf(s, "%llu %lld %lld %09ld %u\n",
1474 field->duration,
1475 field->outer_duration,
1476 (long long)field->timestamp.tv_sec,
1477 field->timestamp.tv_nsec,
1478 field->seqnum);
1479
1480 return trace_handle_return(s);
1481 }
1482
1483 static struct trace_event_functions trace_hwlat_funcs = {
1484 .trace = trace_hwlat_print,
1485 .raw = trace_hwlat_raw,
1486 };
1487
1488 static struct trace_event trace_hwlat_event = {
1489 .type = TRACE_HWLAT,
1490 .funcs = &trace_hwlat_funcs,
1491 };
1492
1493 /* TRACE_OSNOISE */
1494 static enum print_line_t
trace_osnoise_print(struct trace_iterator * iter,int flags,struct trace_event * event)1495 trace_osnoise_print(struct trace_iterator *iter, int flags,
1496 struct trace_event *event)
1497 {
1498 struct trace_entry *entry = iter->ent;
1499 struct trace_seq *s = &iter->seq;
1500 struct osnoise_entry *field;
1501 u64 ratio, ratio_dec;
1502 u64 net_runtime;
1503
1504 trace_assign_type(field, entry);
1505
1506 /*
1507 * compute the available % of cpu time.
1508 */
1509 net_runtime = field->runtime - field->noise;
1510 ratio = net_runtime * 10000000;
1511 do_div(ratio, field->runtime);
1512 ratio_dec = do_div(ratio, 100000);
1513
1514 trace_seq_printf(s, "%llu %10llu %3llu.%05llu %7llu",
1515 field->runtime,
1516 field->noise,
1517 ratio, ratio_dec,
1518 field->max_sample);
1519
1520 trace_seq_printf(s, " %6u", field->hw_count);
1521 trace_seq_printf(s, " %6u", field->nmi_count);
1522 trace_seq_printf(s, " %6u", field->irq_count);
1523 trace_seq_printf(s, " %6u", field->softirq_count);
1524 trace_seq_printf(s, " %6u", field->thread_count);
1525
1526 trace_seq_putc(s, '\n');
1527
1528 return trace_handle_return(s);
1529 }
1530
1531 static enum print_line_t
trace_osnoise_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1532 trace_osnoise_raw(struct trace_iterator *iter, int flags,
1533 struct trace_event *event)
1534 {
1535 struct osnoise_entry *field;
1536 struct trace_seq *s = &iter->seq;
1537
1538 trace_assign_type(field, iter->ent);
1539
1540 trace_seq_printf(s, "%lld %llu %llu %u %u %u %u %u\n",
1541 field->runtime,
1542 field->noise,
1543 field->max_sample,
1544 field->hw_count,
1545 field->nmi_count,
1546 field->irq_count,
1547 field->softirq_count,
1548 field->thread_count);
1549
1550 return trace_handle_return(s);
1551 }
1552
1553 static struct trace_event_functions trace_osnoise_funcs = {
1554 .trace = trace_osnoise_print,
1555 .raw = trace_osnoise_raw,
1556 };
1557
1558 static struct trace_event trace_osnoise_event = {
1559 .type = TRACE_OSNOISE,
1560 .funcs = &trace_osnoise_funcs,
1561 };
1562
1563 /* TRACE_TIMERLAT */
1564
1565 static char *timerlat_lat_context[] = {"irq", "thread", "user-ret"};
1566 static enum print_line_t
trace_timerlat_print(struct trace_iterator * iter,int flags,struct trace_event * event)1567 trace_timerlat_print(struct trace_iterator *iter, int flags,
1568 struct trace_event *event)
1569 {
1570 struct trace_entry *entry = iter->ent;
1571 struct trace_seq *s = &iter->seq;
1572 struct timerlat_entry *field;
1573
1574 trace_assign_type(field, entry);
1575
1576 trace_seq_printf(s, "#%-5u context %6s timer_latency %9llu ns\n",
1577 field->seqnum,
1578 timerlat_lat_context[field->context],
1579 field->timer_latency);
1580
1581 return trace_handle_return(s);
1582 }
1583
1584 static enum print_line_t
trace_timerlat_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1585 trace_timerlat_raw(struct trace_iterator *iter, int flags,
1586 struct trace_event *event)
1587 {
1588 struct timerlat_entry *field;
1589 struct trace_seq *s = &iter->seq;
1590
1591 trace_assign_type(field, iter->ent);
1592
1593 trace_seq_printf(s, "%u %d %llu\n",
1594 field->seqnum,
1595 field->context,
1596 field->timer_latency);
1597
1598 return trace_handle_return(s);
1599 }
1600
1601 static struct trace_event_functions trace_timerlat_funcs = {
1602 .trace = trace_timerlat_print,
1603 .raw = trace_timerlat_raw,
1604 };
1605
1606 static struct trace_event trace_timerlat_event = {
1607 .type = TRACE_TIMERLAT,
1608 .funcs = &trace_timerlat_funcs,
1609 };
1610
1611 /* TRACE_BPUTS */
1612 static enum print_line_t
trace_bputs_print(struct trace_iterator * iter,int flags,struct trace_event * event)1613 trace_bputs_print(struct trace_iterator *iter, int flags,
1614 struct trace_event *event)
1615 {
1616 struct trace_entry *entry = iter->ent;
1617 struct trace_seq *s = &iter->seq;
1618 struct bputs_entry *field;
1619
1620 trace_assign_type(field, entry);
1621
1622 seq_print_ip_sym(s, field->ip, flags);
1623 trace_seq_puts(s, ": ");
1624 trace_seq_puts(s, field->str);
1625
1626 return trace_handle_return(s);
1627 }
1628
1629
1630 static enum print_line_t
trace_bputs_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1631 trace_bputs_raw(struct trace_iterator *iter, int flags,
1632 struct trace_event *event)
1633 {
1634 struct bputs_entry *field;
1635 struct trace_seq *s = &iter->seq;
1636
1637 trace_assign_type(field, iter->ent);
1638
1639 trace_seq_printf(s, ": %lx : ", field->ip);
1640 trace_seq_puts(s, field->str);
1641
1642 return trace_handle_return(s);
1643 }
1644
1645 static struct trace_event_functions trace_bputs_funcs = {
1646 .trace = trace_bputs_print,
1647 .raw = trace_bputs_raw,
1648 };
1649
1650 static struct trace_event trace_bputs_event = {
1651 .type = TRACE_BPUTS,
1652 .funcs = &trace_bputs_funcs,
1653 };
1654
1655 /* TRACE_BPRINT */
1656 static enum print_line_t
trace_bprint_print(struct trace_iterator * iter,int flags,struct trace_event * event)1657 trace_bprint_print(struct trace_iterator *iter, int flags,
1658 struct trace_event *event)
1659 {
1660 struct trace_entry *entry = iter->ent;
1661 struct trace_seq *s = &iter->seq;
1662 struct bprint_entry *field;
1663
1664 trace_assign_type(field, entry);
1665
1666 seq_print_ip_sym(s, field->ip, flags);
1667 trace_seq_puts(s, ": ");
1668 trace_seq_bprintf(s, field->fmt, field->buf);
1669
1670 return trace_handle_return(s);
1671 }
1672
1673
1674 static enum print_line_t
trace_bprint_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1675 trace_bprint_raw(struct trace_iterator *iter, int flags,
1676 struct trace_event *event)
1677 {
1678 struct bprint_entry *field;
1679 struct trace_seq *s = &iter->seq;
1680
1681 trace_assign_type(field, iter->ent);
1682
1683 trace_seq_printf(s, ": %lx : ", field->ip);
1684 trace_seq_bprintf(s, field->fmt, field->buf);
1685
1686 return trace_handle_return(s);
1687 }
1688
1689 static struct trace_event_functions trace_bprint_funcs = {
1690 .trace = trace_bprint_print,
1691 .raw = trace_bprint_raw,
1692 };
1693
1694 static struct trace_event trace_bprint_event = {
1695 .type = TRACE_BPRINT,
1696 .funcs = &trace_bprint_funcs,
1697 };
1698
1699 /* TRACE_PRINT */
trace_print_print(struct trace_iterator * iter,int flags,struct trace_event * event)1700 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1701 int flags, struct trace_event *event)
1702 {
1703 struct print_entry *field;
1704 struct trace_seq *s = &iter->seq;
1705 unsigned long ip;
1706
1707 trace_assign_type(field, iter->ent);
1708
1709 ip = field->ip + iter->tr->text_delta;
1710
1711 seq_print_ip_sym(s, ip, flags);
1712 trace_seq_printf(s, ": %s", field->buf);
1713
1714 return trace_handle_return(s);
1715 }
1716
trace_print_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1717 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1718 struct trace_event *event)
1719 {
1720 struct print_entry *field;
1721
1722 trace_assign_type(field, iter->ent);
1723
1724 trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
1725
1726 return trace_handle_return(&iter->seq);
1727 }
1728
1729 static struct trace_event_functions trace_print_funcs = {
1730 .trace = trace_print_print,
1731 .raw = trace_print_raw,
1732 };
1733
1734 static struct trace_event trace_print_event = {
1735 .type = TRACE_PRINT,
1736 .funcs = &trace_print_funcs,
1737 };
1738
trace_raw_data(struct trace_iterator * iter,int flags,struct trace_event * event)1739 static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
1740 struct trace_event *event)
1741 {
1742 struct raw_data_entry *field;
1743 int i;
1744
1745 trace_assign_type(field, iter->ent);
1746
1747 trace_seq_printf(&iter->seq, "# %x buf:", field->id);
1748
1749 for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
1750 trace_seq_printf(&iter->seq, " %02x",
1751 (unsigned char)field->buf[i]);
1752
1753 trace_seq_putc(&iter->seq, '\n');
1754
1755 return trace_handle_return(&iter->seq);
1756 }
1757
1758 static struct trace_event_functions trace_raw_data_funcs = {
1759 .trace = trace_raw_data,
1760 .raw = trace_raw_data,
1761 };
1762
1763 static struct trace_event trace_raw_data_event = {
1764 .type = TRACE_RAW_DATA,
1765 .funcs = &trace_raw_data_funcs,
1766 };
1767
1768 static enum print_line_t
trace_func_repeats_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1769 trace_func_repeats_raw(struct trace_iterator *iter, int flags,
1770 struct trace_event *event)
1771 {
1772 struct func_repeats_entry *field;
1773 struct trace_seq *s = &iter->seq;
1774
1775 trace_assign_type(field, iter->ent);
1776
1777 trace_seq_printf(s, "%lu %lu %u %llu\n",
1778 field->ip,
1779 field->parent_ip,
1780 field->count,
1781 FUNC_REPEATS_GET_DELTA_TS(field));
1782
1783 return trace_handle_return(s);
1784 }
1785
1786 static enum print_line_t
trace_func_repeats_print(struct trace_iterator * iter,int flags,struct trace_event * event)1787 trace_func_repeats_print(struct trace_iterator *iter, int flags,
1788 struct trace_event *event)
1789 {
1790 struct func_repeats_entry *field;
1791 struct trace_seq *s = &iter->seq;
1792
1793 trace_assign_type(field, iter->ent);
1794
1795 print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta, NULL, flags);
1796 trace_seq_printf(s, " (repeats: %u, last_ts:", field->count);
1797 trace_print_time(s, iter,
1798 iter->ts - FUNC_REPEATS_GET_DELTA_TS(field));
1799 trace_seq_puts(s, ")\n");
1800
1801 return trace_handle_return(s);
1802 }
1803
1804 static struct trace_event_functions trace_func_repeats_funcs = {
1805 .trace = trace_func_repeats_print,
1806 .raw = trace_func_repeats_raw,
1807 };
1808
1809 static struct trace_event trace_func_repeats_event = {
1810 .type = TRACE_FUNC_REPEATS,
1811 .funcs = &trace_func_repeats_funcs,
1812 };
1813
1814 static struct trace_event *events[] __initdata = {
1815 &trace_fn_event,
1816 &trace_ctx_event,
1817 &trace_wake_event,
1818 &trace_stack_event,
1819 &trace_user_stack_event,
1820 &trace_bputs_event,
1821 &trace_bprint_event,
1822 &trace_print_event,
1823 &trace_hwlat_event,
1824 &trace_osnoise_event,
1825 &trace_timerlat_event,
1826 &trace_raw_data_event,
1827 &trace_func_repeats_event,
1828 NULL
1829 };
1830
init_events(void)1831 __init int init_events(void)
1832 {
1833 struct trace_event *event;
1834 int i, ret;
1835
1836 for (i = 0; events[i]; i++) {
1837 event = events[i];
1838 ret = register_trace_event(event);
1839 WARN_ONCE(!ret, "event %d failed to register", event->type);
1840 }
1841
1842 return 0;
1843 }
1844