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 <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/ftrace.h>
11 #include <linux/kprobes.h>
12 #include <linux/sched/clock.h>
13 #include <linux/sched/mm.h>
14 #include <linux/idr.h>
15 #include <linux/btf.h>
16 #include <linux/bpf.h>
17 #include <linux/hashtable.h>
18
19 #include "trace_output.h"
20 #include "trace_btf.h"
21
22 /* 2^7 = 128 */
23 #define EVENT_HASH_BITS 7
24
25 DECLARE_RWSEM(trace_event_sem);
26
27 static DEFINE_HASHTABLE(event_hash, EVENT_HASH_BITS);
28
trace_print_bputs_msg_only(struct trace_iterator * iter)29 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
30 {
31 struct trace_seq *s = &iter->seq;
32 struct trace_entry *entry = iter->ent;
33 struct bputs_entry *field;
34
35 trace_assign_type(field, entry);
36
37 trace_seq_puts(s, field->str);
38
39 return trace_handle_return(s);
40 }
41
trace_print_bprintk_msg_only(struct trace_iterator * iter)42 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
43 {
44 struct trace_seq *s = &iter->seq;
45 struct trace_entry *entry = iter->ent;
46 struct bprint_entry *field;
47
48 trace_assign_type(field, entry);
49
50 trace_seq_bprintf(s, field->fmt, field->buf);
51
52 return trace_handle_return(s);
53 }
54
trace_print_printk_msg_only(struct trace_iterator * iter)55 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
56 {
57 struct trace_seq *s = &iter->seq;
58 struct trace_entry *entry = iter->ent;
59 struct print_entry *field;
60
61 trace_assign_type(field, entry);
62
63 trace_seq_puts(s, field->buf);
64
65 return trace_handle_return(s);
66 }
67
68 const char *
trace_print_flags_seq(struct trace_seq * p,const char * delim,unsigned long flags,const struct trace_print_flags * flag_array)69 trace_print_flags_seq(struct trace_seq *p, const char *delim,
70 unsigned long flags,
71 const struct trace_print_flags *flag_array)
72 {
73 unsigned long mask;
74 const char *str;
75 const char *ret = trace_seq_buffer_ptr(p);
76 int i, first = 1;
77
78 for (i = 0; flag_array[i].name && flags; i++) {
79
80 mask = flag_array[i].mask;
81 if ((flags & mask) != mask)
82 continue;
83
84 str = flag_array[i].name;
85 flags &= ~mask;
86 if (!first && delim)
87 trace_seq_puts(p, delim);
88 else
89 first = 0;
90 trace_seq_puts(p, str);
91 }
92
93 /* check for left over flags */
94 if (flags) {
95 if (!first && delim)
96 trace_seq_puts(p, delim);
97 trace_seq_printf(p, "0x%lx", flags);
98 }
99
100 trace_seq_putc(p, 0);
101
102 return ret;
103 }
104 EXPORT_SYMBOL(trace_print_flags_seq);
105
106 const char *
trace_print_symbols_seq(struct trace_seq * p,unsigned long val,const struct trace_print_flags * symbol_array)107 trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
108 const struct trace_print_flags *symbol_array)
109 {
110 int i;
111 const char *ret = trace_seq_buffer_ptr(p);
112
113 for (i = 0; symbol_array[i].name; i++) {
114
115 if (val != symbol_array[i].mask)
116 continue;
117
118 trace_seq_puts(p, symbol_array[i].name);
119 break;
120 }
121
122 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
123 trace_seq_printf(p, "0x%lx", val);
124
125 trace_seq_putc(p, 0);
126
127 return ret;
128 }
129 EXPORT_SYMBOL(trace_print_symbols_seq);
130
131 #if BITS_PER_LONG == 32
132 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)133 trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
134 unsigned long long flags,
135 const struct trace_print_flags_u64 *flag_array)
136 {
137 unsigned long long mask;
138 const char *str;
139 const char *ret = trace_seq_buffer_ptr(p);
140 int i, first = 1;
141
142 for (i = 0; flag_array[i].name && flags; i++) {
143
144 mask = flag_array[i].mask;
145 if ((flags & mask) != mask)
146 continue;
147
148 str = flag_array[i].name;
149 flags &= ~mask;
150 if (!first && delim)
151 trace_seq_puts(p, delim);
152 else
153 first = 0;
154 trace_seq_puts(p, str);
155 }
156
157 /* check for left over flags */
158 if (flags) {
159 if (!first && delim)
160 trace_seq_puts(p, delim);
161 trace_seq_printf(p, "0x%llx", flags);
162 }
163
164 trace_seq_putc(p, 0);
165
166 return ret;
167 }
168 EXPORT_SYMBOL(trace_print_flags_seq_u64);
169
170 const char *
trace_print_symbols_seq_u64(struct trace_seq * p,unsigned long long val,const struct trace_print_flags_u64 * symbol_array)171 trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
172 const struct trace_print_flags_u64 *symbol_array)
173 {
174 int i;
175 const char *ret = trace_seq_buffer_ptr(p);
176
177 for (i = 0; symbol_array[i].name; i++) {
178
179 if (val != symbol_array[i].mask)
180 continue;
181
182 trace_seq_puts(p, symbol_array[i].name);
183 break;
184 }
185
186 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
187 trace_seq_printf(p, "0x%llx", val);
188
189 trace_seq_putc(p, 0);
190
191 return ret;
192 }
193 EXPORT_SYMBOL(trace_print_symbols_seq_u64);
194 #endif
195
196 const char *
trace_print_bitmask_seq(struct trace_seq * p,void * bitmask_ptr,unsigned int bitmask_size)197 trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
198 unsigned int bitmask_size)
199 {
200 const char *ret = trace_seq_buffer_ptr(p);
201
202 trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
203 trace_seq_putc(p, 0);
204
205 return ret;
206 }
207 EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
208
209 /**
210 * trace_print_hex_seq - print buffer as hex sequence
211 * @p: trace seq struct to write to
212 * @buf: The buffer to print
213 * @buf_len: Length of @buf in bytes
214 * @concatenate: Print @buf as single hex string or with spacing
215 *
216 * Prints the passed buffer as a hex sequence either as a whole,
217 * single hex string if @concatenate is true or with spacing after
218 * each byte in case @concatenate is false.
219 */
220 const char *
trace_print_hex_seq(struct trace_seq * p,const unsigned char * buf,int buf_len,bool concatenate)221 trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
222 bool concatenate)
223 {
224 int i;
225 const char *ret = trace_seq_buffer_ptr(p);
226 const char *fmt = concatenate ? "%*phN" : "%*ph";
227
228 for (i = 0; i < buf_len; i += 16) {
229 if (!concatenate && i != 0)
230 trace_seq_putc(p, ' ');
231 trace_seq_printf(p, fmt, min(buf_len - i, 16), &buf[i]);
232 }
233 trace_seq_putc(p, 0);
234
235 return ret;
236 }
237 EXPORT_SYMBOL(trace_print_hex_seq);
238
239 const char *
trace_print_array_seq(struct trace_seq * p,const void * buf,int count,size_t el_size)240 trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
241 size_t el_size)
242 {
243 const char *ret = trace_seq_buffer_ptr(p);
244 const char *prefix = "";
245 void *ptr = (void *)buf;
246 size_t buf_len = count * el_size;
247
248 trace_seq_putc(p, '{');
249
250 while (ptr < buf + buf_len) {
251 switch (el_size) {
252 case 1:
253 trace_seq_printf(p, "%s0x%x", prefix,
254 *(u8 *)ptr);
255 break;
256 case 2:
257 trace_seq_printf(p, "%s0x%x", prefix,
258 *(u16 *)ptr);
259 break;
260 case 4:
261 trace_seq_printf(p, "%s0x%x", prefix,
262 *(u32 *)ptr);
263 break;
264 case 8:
265 trace_seq_printf(p, "%s0x%llx", prefix,
266 *(u64 *)ptr);
267 break;
268 default:
269 trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
270 *(u8 *)ptr);
271 el_size = 1;
272 }
273 prefix = ",";
274 ptr += el_size;
275 }
276
277 trace_seq_putc(p, '}');
278 trace_seq_putc(p, 0);
279
280 return ret;
281 }
282 EXPORT_SYMBOL(trace_print_array_seq);
283
284 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)285 trace_print_hex_dump_seq(struct trace_seq *p, const char *prefix_str,
286 int prefix_type, int rowsize, int groupsize,
287 const void *buf, size_t len, bool ascii)
288 {
289 const char *ret = trace_seq_buffer_ptr(p);
290
291 trace_seq_putc(p, '\n');
292 trace_seq_hex_dump(p, prefix_str, prefix_type,
293 rowsize, groupsize, buf, len, ascii);
294 trace_seq_putc(p, 0);
295 return ret;
296 }
297 EXPORT_SYMBOL(trace_print_hex_dump_seq);
298
trace_raw_output_prep(struct trace_iterator * iter,struct trace_event * trace_event)299 int trace_raw_output_prep(struct trace_iterator *iter,
300 struct trace_event *trace_event)
301 {
302 struct trace_event_call *event;
303 struct trace_seq *s = &iter->seq;
304 struct trace_seq *p = &iter->tmp_seq;
305 struct trace_entry *entry;
306
307 event = container_of(trace_event, struct trace_event_call, event);
308 entry = iter->ent;
309
310 if (entry->type != event->event.type) {
311 WARN_ON_ONCE(1);
312 return TRACE_TYPE_UNHANDLED;
313 }
314
315 trace_seq_init(p);
316 trace_seq_printf(s, "%s: ", trace_event_name(event));
317
318 return trace_handle_return(s);
319 }
320 EXPORT_SYMBOL(trace_raw_output_prep);
321
trace_event_printf(struct trace_iterator * iter,const char * fmt,...)322 void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...)
323 {
324 struct trace_seq *s = &iter->seq;
325 va_list ap;
326
327 if (ignore_event(iter))
328 return;
329
330 va_start(ap, fmt);
331 trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
332 va_end(ap);
333 }
334 EXPORT_SYMBOL(trace_event_printf);
335
336 static __printf(3, 0)
trace_output_raw(struct trace_iterator * iter,char * name,char * fmt,va_list ap)337 int trace_output_raw(struct trace_iterator *iter, char *name,
338 char *fmt, va_list ap)
339 {
340 struct trace_seq *s = &iter->seq;
341
342 trace_seq_printf(s, "%s: ", name);
343 trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
344
345 return trace_handle_return(s);
346 }
347
trace_output_call(struct trace_iterator * iter,char * name,char * fmt,...)348 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
349 {
350 va_list ap;
351 int ret;
352
353 va_start(ap, fmt);
354 ret = trace_output_raw(iter, name, fmt, ap);
355 va_end(ap);
356
357 return ret;
358 }
359 EXPORT_SYMBOL_GPL(trace_output_call);
360
kretprobed(const char * name,unsigned long addr)361 static inline const char *kretprobed(const char *name, unsigned long addr)
362 {
363 if (is_kretprobe_trampoline(addr))
364 return "[unknown/kretprobe'd]";
365 return name;
366 }
367
368 void
trace_seq_print_sym(struct trace_seq * s,unsigned long address,bool offset)369 trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
370 {
371 #ifdef CONFIG_KALLSYMS
372 char str[KSYM_SYMBOL_LEN];
373 const char *name;
374
375 if (offset)
376 sprint_symbol(str, address);
377 else
378 kallsyms_lookup(address, NULL, NULL, NULL, str);
379 name = kretprobed(str, address);
380
381 if (name && strlen(name)) {
382 trace_seq_puts(s, name);
383 return;
384 }
385 #endif
386 trace_seq_printf(s, "0x%08lx", address);
387 }
388
389 #ifndef CONFIG_64BIT
390 # define IP_FMT "%08lx"
391 #else
392 # define IP_FMT "%016lx"
393 #endif
394
seq_print_user_ip(struct trace_seq * s,struct mm_struct * mm,unsigned long ip,unsigned long sym_flags)395 static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
396 unsigned long ip, unsigned long sym_flags)
397 {
398 struct file *file = NULL;
399 unsigned long vmstart = 0;
400 int ret = 1;
401
402 if (s->full)
403 return 0;
404
405 if (mm) {
406 const struct vm_area_struct *vma;
407
408 mmap_read_lock(mm);
409 vma = find_vma(mm, ip);
410 if (vma) {
411 file = vma->vm_file;
412 vmstart = vma->vm_start;
413 }
414 if (file) {
415 ret = trace_seq_path(s, file_user_path(file));
416 if (ret)
417 trace_seq_printf(s, "[+0x%lx]",
418 ip - vmstart);
419 }
420 mmap_read_unlock(mm);
421 }
422 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
423 trace_seq_printf(s, " <" IP_FMT ">", ip);
424 return !trace_seq_has_overflowed(s);
425 }
426
427 int
seq_print_ip_sym(struct trace_seq * s,unsigned long ip,unsigned long sym_flags)428 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
429 {
430 if (!ip) {
431 trace_seq_putc(s, '0');
432 goto out;
433 }
434
435 trace_seq_print_sym(s, ip, sym_flags & TRACE_ITER_SYM_OFFSET);
436
437 if (sym_flags & TRACE_ITER_SYM_ADDR)
438 trace_seq_printf(s, " <" IP_FMT ">", ip);
439
440 out:
441 return !trace_seq_has_overflowed(s);
442 }
443
444 /**
445 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
446 * @s: trace seq struct to write to
447 * @entry: The trace entry field from the ring buffer
448 *
449 * Prints the generic fields of irqs off, in hard or softirq, preempt
450 * count.
451 */
trace_print_lat_fmt(struct trace_seq * s,struct trace_entry * entry)452 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
453 {
454 char hardsoft_irq;
455 char need_resched;
456 char irqs_off;
457 int hardirq;
458 int softirq;
459 int bh_off;
460 int nmi;
461
462 nmi = entry->flags & TRACE_FLAG_NMI;
463 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
464 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
465 bh_off = entry->flags & TRACE_FLAG_BH_OFF;
466
467 irqs_off =
468 (entry->flags & TRACE_FLAG_IRQS_OFF && bh_off) ? 'D' :
469 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
470 bh_off ? 'b' :
471 '.';
472
473 switch (entry->flags & (TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY |
474 TRACE_FLAG_PREEMPT_RESCHED)) {
475 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY | TRACE_FLAG_PREEMPT_RESCHED:
476 need_resched = 'B';
477 break;
478 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
479 need_resched = 'N';
480 break;
481 case TRACE_FLAG_NEED_RESCHED_LAZY | TRACE_FLAG_PREEMPT_RESCHED:
482 need_resched = 'L';
483 break;
484 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_NEED_RESCHED_LAZY:
485 need_resched = 'b';
486 break;
487 case TRACE_FLAG_NEED_RESCHED:
488 need_resched = 'n';
489 break;
490 case TRACE_FLAG_PREEMPT_RESCHED:
491 need_resched = 'p';
492 break;
493 case TRACE_FLAG_NEED_RESCHED_LAZY:
494 need_resched = 'l';
495 break;
496 default:
497 need_resched = '.';
498 break;
499 }
500
501 hardsoft_irq =
502 (nmi && hardirq) ? 'Z' :
503 nmi ? 'z' :
504 (hardirq && softirq) ? 'H' :
505 hardirq ? 'h' :
506 softirq ? 's' :
507 '.' ;
508
509 trace_seq_printf(s, "%c%c%c",
510 irqs_off, need_resched, hardsoft_irq);
511
512 if (entry->preempt_count & 0xf)
513 trace_seq_printf(s, "%x", entry->preempt_count & 0xf);
514 else
515 trace_seq_putc(s, '.');
516
517 if (entry->preempt_count & 0xf0)
518 trace_seq_printf(s, "%x", entry->preempt_count >> 4);
519 else
520 trace_seq_putc(s, '.');
521
522 return !trace_seq_has_overflowed(s);
523 }
524
525 static int
lat_print_generic(struct trace_seq * s,struct trace_entry * entry,int cpu)526 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
527 {
528 char comm[TASK_COMM_LEN];
529
530 trace_find_cmdline(entry->pid, comm);
531
532 trace_seq_printf(s, "%8.8s-%-7d %3d",
533 comm, entry->pid, cpu);
534
535 return trace_print_lat_fmt(s, entry);
536 }
537
538 #undef MARK
539 #define MARK(v, s) {.val = v, .sym = s}
540 /* trace overhead mark */
541 static const struct trace_mark {
542 unsigned long long val; /* unit: nsec */
543 char sym;
544 } mark[] = {
545 MARK(1000000000ULL , '$'), /* 1 sec */
546 MARK(100000000ULL , '@'), /* 100 msec */
547 MARK(10000000ULL , '*'), /* 10 msec */
548 MARK(1000000ULL , '#'), /* 1000 usecs */
549 MARK(100000ULL , '!'), /* 100 usecs */
550 MARK(10000ULL , '+'), /* 10 usecs */
551 };
552 #undef MARK
553
trace_find_mark(unsigned long long d)554 char trace_find_mark(unsigned long long d)
555 {
556 int i;
557 int size = ARRAY_SIZE(mark);
558
559 for (i = 0; i < size; i++) {
560 if (d > mark[i].val)
561 break;
562 }
563
564 return (i == size) ? ' ' : mark[i].sym;
565 }
566
567 static int
lat_print_timestamp(struct trace_iterator * iter,u64 next_ts)568 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
569 {
570 struct trace_array *tr = iter->tr;
571 unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
572 unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
573 unsigned long long abs_ts = iter->ts - iter->array_buffer->time_start;
574 unsigned long long rel_ts = next_ts - iter->ts;
575 struct trace_seq *s = &iter->seq;
576
577 if (in_ns) {
578 abs_ts = ns2usecs(abs_ts);
579 rel_ts = ns2usecs(rel_ts);
580 }
581
582 if (verbose && in_ns) {
583 unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
584 unsigned long abs_msec = (unsigned long)abs_ts;
585 unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
586 unsigned long rel_msec = (unsigned long)rel_ts;
587
588 trace_seq_printf(
589 s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
590 ns2usecs(iter->ts),
591 abs_msec, abs_usec,
592 rel_msec, rel_usec);
593
594 } else if (verbose && !in_ns) {
595 trace_seq_printf(
596 s, "[%016llx] %lld (+%lld): ",
597 iter->ts, abs_ts, rel_ts);
598
599 } else if (!verbose && in_ns) {
600 trace_seq_printf(
601 s, " %4lldus%c: ",
602 abs_ts,
603 trace_find_mark(rel_ts * NSEC_PER_USEC));
604
605 } else { /* !verbose && !in_ns */
606 trace_seq_printf(s, " %4lld: ", abs_ts);
607 }
608
609 return !trace_seq_has_overflowed(s);
610 }
611
trace_print_time(struct trace_seq * s,struct trace_iterator * iter,unsigned long long ts)612 static void trace_print_time(struct trace_seq *s, struct trace_iterator *iter,
613 unsigned long long ts)
614 {
615 unsigned long secs, usec_rem;
616 unsigned long long t;
617
618 if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
619 t = ns2usecs(ts);
620 usec_rem = do_div(t, USEC_PER_SEC);
621 secs = (unsigned long)t;
622 trace_seq_printf(s, " %5lu.%06lu", secs, usec_rem);
623 } else
624 trace_seq_printf(s, " %12llu", ts);
625 }
626
trace_print_context(struct trace_iterator * iter)627 int trace_print_context(struct trace_iterator *iter)
628 {
629 struct trace_array *tr = iter->tr;
630 struct trace_seq *s = &iter->seq;
631 struct trace_entry *entry = iter->ent;
632 char comm[TASK_COMM_LEN];
633
634 trace_find_cmdline(entry->pid, comm);
635
636 trace_seq_printf(s, "%16s-%-7d ", comm, entry->pid);
637
638 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
639 unsigned int tgid = trace_find_tgid(entry->pid);
640
641 if (!tgid)
642 trace_seq_printf(s, "(-------) ");
643 else
644 trace_seq_printf(s, "(%7d) ", tgid);
645 }
646
647 trace_seq_printf(s, "[%03d] ", iter->cpu);
648
649 if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
650 trace_print_lat_fmt(s, entry);
651
652 trace_print_time(s, iter, iter->ts);
653 trace_seq_puts(s, ": ");
654
655 return !trace_seq_has_overflowed(s);
656 }
657
trace_print_lat_context(struct trace_iterator * iter)658 int trace_print_lat_context(struct trace_iterator *iter)
659 {
660 struct trace_entry *entry, *next_entry;
661 struct trace_array *tr = iter->tr;
662 struct trace_seq *s = &iter->seq;
663 unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
664 u64 next_ts;
665
666 next_entry = trace_find_next_entry(iter, NULL, &next_ts);
667 if (!next_entry)
668 next_ts = iter->ts;
669
670 /* trace_find_next_entry() may change iter->ent */
671 entry = iter->ent;
672
673 if (verbose) {
674 char comm[TASK_COMM_LEN];
675
676 trace_find_cmdline(entry->pid, comm);
677
678 trace_seq_printf(
679 s, "%16s %7d %3d %d %08x %08lx ",
680 comm, entry->pid, iter->cpu, entry->flags,
681 entry->preempt_count & 0xf, iter->idx);
682 } else {
683 lat_print_generic(s, entry, iter->cpu);
684 }
685
686 lat_print_timestamp(iter, next_ts);
687
688 return !trace_seq_has_overflowed(s);
689 }
690
691 #ifdef CONFIG_FUNCTION_TRACE_ARGS
print_function_args(struct trace_seq * s,unsigned long * args,unsigned long func)692 void print_function_args(struct trace_seq *s, unsigned long *args,
693 unsigned long func)
694 {
695 const struct btf_param *param;
696 const struct btf_type *t;
697 const char *param_name;
698 char name[KSYM_NAME_LEN];
699 unsigned long arg;
700 struct btf *btf;
701 s32 tid, nr = 0;
702 int a, p, x;
703
704 trace_seq_printf(s, "(");
705
706 if (!args)
707 goto out;
708 if (lookup_symbol_name(func, name))
709 goto out;
710
711 /* TODO: Pass module name here too */
712 t = btf_find_func_proto(name, &btf);
713 if (IS_ERR_OR_NULL(t))
714 goto out;
715
716 param = btf_get_func_param(t, &nr);
717 if (!param)
718 goto out_put;
719
720 for (a = 0, p = 0; p < nr; a++, p++) {
721 if (p)
722 trace_seq_puts(s, ", ");
723
724 /* This only prints what the arch allows (6 args by default) */
725 if (a == FTRACE_REGS_MAX_ARGS) {
726 trace_seq_puts(s, "...");
727 break;
728 }
729
730 arg = args[a];
731
732 param_name = btf_name_by_offset(btf, param[p].name_off);
733 if (param_name)
734 trace_seq_printf(s, "%s=", param_name);
735 t = btf_type_skip_modifiers(btf, param[p].type, &tid);
736
737 switch (t ? BTF_INFO_KIND(t->info) : BTF_KIND_UNKN) {
738 case BTF_KIND_UNKN:
739 trace_seq_putc(s, '?');
740 /* Still print unknown type values */
741 fallthrough;
742 case BTF_KIND_PTR:
743 trace_seq_printf(s, "0x%lx", arg);
744 break;
745 case BTF_KIND_INT:
746 trace_seq_printf(s, "%ld", arg);
747 break;
748 case BTF_KIND_ENUM:
749 trace_seq_printf(s, "%ld", arg);
750 break;
751 default:
752 /* This does not handle complex arguments */
753 trace_seq_printf(s, "(%s)[0x%lx", btf_type_str(t), arg);
754 for (x = sizeof(long); x < t->size; x += sizeof(long)) {
755 trace_seq_putc(s, ':');
756 if (++a == FTRACE_REGS_MAX_ARGS) {
757 trace_seq_puts(s, "...]");
758 goto out_put;
759 }
760 trace_seq_printf(s, "0x%lx", args[a]);
761 }
762 trace_seq_putc(s, ']');
763 break;
764 }
765 }
766 out_put:
767 btf_put(btf);
768 out:
769 trace_seq_printf(s, ")");
770 }
771 #endif
772
773 /**
774 * ftrace_find_event - find a registered event
775 * @type: the type of event to look for
776 *
777 * Returns an event of type @type otherwise NULL
778 * Called with trace_event_read_lock() held.
779 */
ftrace_find_event(int type)780 struct trace_event *ftrace_find_event(int type)
781 {
782 struct trace_event *event;
783
784 hash_for_each_possible(event_hash, event, node, type) {
785 if (event->type == type)
786 return event;
787 }
788
789 return NULL;
790 }
791
792 static DEFINE_IDA(trace_event_ida);
793
free_trace_event_type(int type)794 static void free_trace_event_type(int type)
795 {
796 if (type >= __TRACE_LAST_TYPE)
797 ida_free(&trace_event_ida, type);
798 }
799
alloc_trace_event_type(void)800 static int alloc_trace_event_type(void)
801 {
802 int next;
803
804 /* Skip static defined type numbers */
805 next = ida_alloc_range(&trace_event_ida, __TRACE_LAST_TYPE,
806 TRACE_EVENT_TYPE_MAX, GFP_KERNEL);
807 if (next < 0)
808 return 0;
809 return next;
810 }
811
trace_event_read_lock(void)812 void trace_event_read_lock(void)
813 {
814 down_read(&trace_event_sem);
815 }
816
trace_event_read_unlock(void)817 void trace_event_read_unlock(void)
818 {
819 up_read(&trace_event_sem);
820 }
821
822 /**
823 * register_trace_event - register output for an event type
824 * @event: the event type to register
825 *
826 * Event types are stored in a hash and this hash is used to
827 * find a way to print an event. If the @event->type is set
828 * then it will use that type, otherwise it will assign a
829 * type to use.
830 *
831 * If you assign your own type, please make sure it is added
832 * to the trace_type enum in trace.h, to avoid collisions
833 * with the dynamic types.
834 *
835 * Returns the event type number or zero on error.
836 */
register_trace_event(struct trace_event * event)837 int register_trace_event(struct trace_event *event)
838 {
839 int ret = 0;
840
841 down_write(&trace_event_sem);
842
843 if (WARN_ON(!event))
844 goto out;
845
846 if (WARN_ON(!event->funcs))
847 goto out;
848
849 if (!event->type) {
850 event->type = alloc_trace_event_type();
851 if (!event->type)
852 goto out;
853 } else if (WARN(event->type > __TRACE_LAST_TYPE,
854 "Need to add type to trace.h")) {
855 goto out;
856 } else {
857 /* Is this event already used */
858 if (ftrace_find_event(event->type))
859 goto out;
860 }
861
862 if (event->funcs->trace == NULL)
863 event->funcs->trace = trace_nop_print;
864 if (event->funcs->raw == NULL)
865 event->funcs->raw = trace_nop_print;
866 if (event->funcs->hex == NULL)
867 event->funcs->hex = trace_nop_print;
868 if (event->funcs->binary == NULL)
869 event->funcs->binary = trace_nop_print;
870
871 hash_add(event_hash, &event->node, event->type);
872
873 ret = event->type;
874 out:
875 up_write(&trace_event_sem);
876
877 return ret;
878 }
879 EXPORT_SYMBOL_GPL(register_trace_event);
880
881 /*
882 * Used by module code with the trace_event_sem held for write.
883 */
__unregister_trace_event(struct trace_event * event)884 int __unregister_trace_event(struct trace_event *event)
885 {
886 hash_del(&event->node);
887 free_trace_event_type(event->type);
888 return 0;
889 }
890
891 /**
892 * unregister_trace_event - remove a no longer used event
893 * @event: the event to remove
894 */
unregister_trace_event(struct trace_event * event)895 int unregister_trace_event(struct trace_event *event)
896 {
897 down_write(&trace_event_sem);
898 __unregister_trace_event(event);
899 up_write(&trace_event_sem);
900
901 return 0;
902 }
903 EXPORT_SYMBOL_GPL(unregister_trace_event);
904
905 /*
906 * Standard events
907 */
908
print_array(struct trace_iterator * iter,void * pos,struct ftrace_event_field * field)909 static void print_array(struct trace_iterator *iter, void *pos,
910 struct ftrace_event_field *field)
911 {
912 int offset;
913 int len;
914 int i;
915
916 offset = *(int *)pos & 0xffff;
917 len = *(int *)pos >> 16;
918
919 if (field)
920 offset += field->offset + sizeof(int);
921
922 if (offset + len > iter->ent_size) {
923 trace_seq_puts(&iter->seq, "<OVERFLOW>");
924 return;
925 }
926
927 pos = (void *)iter->ent + offset;
928
929 for (i = 0; i < len; i++, pos++) {
930 if (i)
931 trace_seq_putc(&iter->seq, ',');
932 trace_seq_printf(&iter->seq, "%02x", *(unsigned char *)pos);
933 }
934 }
935
print_fields(struct trace_iterator * iter,struct trace_event_call * call,struct list_head * head)936 static void print_fields(struct trace_iterator *iter, struct trace_event_call *call,
937 struct list_head *head)
938 {
939 struct ftrace_event_field *field;
940 int offset;
941 int len;
942 int ret;
943 void *pos;
944
945 list_for_each_entry_reverse(field, head, link) {
946 trace_seq_printf(&iter->seq, " %s=", field->name);
947 if (field->offset + field->size > iter->ent_size) {
948 trace_seq_puts(&iter->seq, "<OVERFLOW>");
949 continue;
950 }
951 pos = (void *)iter->ent + field->offset;
952
953 switch (field->filter_type) {
954 case FILTER_COMM:
955 case FILTER_STATIC_STRING:
956 trace_seq_printf(&iter->seq, "%.*s", field->size, (char *)pos);
957 break;
958 case FILTER_RDYN_STRING:
959 case FILTER_DYN_STRING:
960 offset = *(int *)pos & 0xffff;
961 len = *(int *)pos >> 16;
962
963 if (field->filter_type == FILTER_RDYN_STRING)
964 offset += field->offset + sizeof(int);
965
966 if (offset + len > iter->ent_size) {
967 trace_seq_puts(&iter->seq, "<OVERFLOW>");
968 break;
969 }
970 pos = (void *)iter->ent + offset;
971 trace_seq_printf(&iter->seq, "%.*s", len, (char *)pos);
972 break;
973 case FILTER_PTR_STRING:
974 if (!iter->fmt_size)
975 trace_iter_expand_format(iter);
976 pos = *(void **)pos;
977 ret = strncpy_from_kernel_nofault(iter->fmt, pos,
978 iter->fmt_size);
979 if (ret < 0)
980 trace_seq_printf(&iter->seq, "(0x%px)", pos);
981 else
982 trace_seq_printf(&iter->seq, "(0x%px:%s)",
983 pos, iter->fmt);
984 break;
985 case FILTER_TRACE_FN:
986 pos = *(void **)pos;
987 trace_seq_printf(&iter->seq, "%pS", pos);
988 break;
989 case FILTER_CPU:
990 case FILTER_OTHER:
991 switch (field->size) {
992 case 1:
993 if (isprint(*(char *)pos)) {
994 trace_seq_printf(&iter->seq, "'%c'",
995 *(unsigned char *)pos);
996 }
997 trace_seq_printf(&iter->seq, "(%d)",
998 *(unsigned char *)pos);
999 break;
1000 case 2:
1001 trace_seq_printf(&iter->seq, "0x%x (%d)",
1002 *(unsigned short *)pos,
1003 *(unsigned short *)pos);
1004 break;
1005 case 4:
1006 /* dynamic array info is 4 bytes */
1007 if (strstr(field->type, "__data_loc")) {
1008 print_array(iter, pos, NULL);
1009 break;
1010 }
1011
1012 if (strstr(field->type, "__rel_loc")) {
1013 print_array(iter, pos, field);
1014 break;
1015 }
1016
1017 trace_seq_printf(&iter->seq, "0x%x (%d)",
1018 *(unsigned int *)pos,
1019 *(unsigned int *)pos);
1020 break;
1021 case 8:
1022 trace_seq_printf(&iter->seq, "0x%llx (%lld)",
1023 *(unsigned long long *)pos,
1024 *(unsigned long long *)pos);
1025 break;
1026 default:
1027 trace_seq_puts(&iter->seq, "<INVALID-SIZE>");
1028 break;
1029 }
1030 break;
1031 default:
1032 trace_seq_puts(&iter->seq, "<INVALID-TYPE>");
1033 }
1034 }
1035 trace_seq_putc(&iter->seq, '\n');
1036 }
1037
print_event_fields(struct trace_iterator * iter,struct trace_event * event)1038 enum print_line_t print_event_fields(struct trace_iterator *iter,
1039 struct trace_event *event)
1040 {
1041 struct trace_event_call *call;
1042 struct list_head *head;
1043
1044 /* ftrace defined events have separate call structures */
1045 if (event->type <= __TRACE_LAST_TYPE) {
1046 bool found = false;
1047
1048 down_read(&trace_event_sem);
1049 list_for_each_entry(call, &ftrace_events, list) {
1050 if (call->event.type == event->type) {
1051 found = true;
1052 break;
1053 }
1054 /* No need to search all events */
1055 if (call->event.type > __TRACE_LAST_TYPE)
1056 break;
1057 }
1058 up_read(&trace_event_sem);
1059 if (!found) {
1060 trace_seq_printf(&iter->seq, "UNKNOWN TYPE %d\n", event->type);
1061 goto out;
1062 }
1063 } else {
1064 call = container_of(event, struct trace_event_call, event);
1065 }
1066 head = trace_get_fields(call);
1067
1068 trace_seq_printf(&iter->seq, "%s:", trace_event_name(call));
1069
1070 if (head && !list_empty(head))
1071 print_fields(iter, call, head);
1072 else
1073 trace_seq_puts(&iter->seq, "No fields found\n");
1074
1075 out:
1076 return trace_handle_return(&iter->seq);
1077 }
1078
trace_nop_print(struct trace_iterator * iter,int flags,struct trace_event * event)1079 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
1080 struct trace_event *event)
1081 {
1082 trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
1083
1084 return trace_handle_return(&iter->seq);
1085 }
1086
print_fn_trace(struct trace_seq * s,unsigned long ip,unsigned long parent_ip,long delta,unsigned long * args,int flags)1087 static void print_fn_trace(struct trace_seq *s, unsigned long ip,
1088 unsigned long parent_ip, long delta,
1089 unsigned long *args, int flags)
1090 {
1091 ip += delta;
1092 parent_ip += delta;
1093
1094 seq_print_ip_sym(s, ip, flags);
1095 if (args)
1096 print_function_args(s, args, ip);
1097
1098 if ((flags & TRACE_ITER_PRINT_PARENT) && parent_ip) {
1099 trace_seq_puts(s, " <-");
1100 seq_print_ip_sym(s, parent_ip, flags);
1101 }
1102 }
1103
1104 /* TRACE_FN */
trace_fn_trace(struct trace_iterator * iter,int flags,struct trace_event * event)1105 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
1106 struct trace_event *event)
1107 {
1108 struct ftrace_entry *field;
1109 struct trace_seq *s = &iter->seq;
1110 unsigned long *args;
1111 int args_size;
1112
1113 trace_assign_type(field, iter->ent);
1114
1115 args_size = iter->ent_size - offsetof(struct ftrace_entry, args);
1116 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long))
1117 args = field->args;
1118 else
1119 args = NULL;
1120
1121 print_fn_trace(s, field->ip, field->parent_ip, iter->tr->text_delta,
1122 args, flags);
1123 trace_seq_putc(s, '\n');
1124
1125 return trace_handle_return(s);
1126 }
1127
trace_fn_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1128 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
1129 struct trace_event *event)
1130 {
1131 struct ftrace_entry *field;
1132
1133 trace_assign_type(field, iter->ent);
1134
1135 trace_seq_printf(&iter->seq, "%lx %lx\n",
1136 field->ip,
1137 field->parent_ip);
1138
1139 return trace_handle_return(&iter->seq);
1140 }
1141
trace_fn_hex(struct trace_iterator * iter,int flags,struct trace_event * event)1142 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
1143 struct trace_event *event)
1144 {
1145 struct ftrace_entry *field;
1146 struct trace_seq *s = &iter->seq;
1147
1148 trace_assign_type(field, iter->ent);
1149
1150 SEQ_PUT_HEX_FIELD(s, field->ip);
1151 SEQ_PUT_HEX_FIELD(s, field->parent_ip);
1152
1153 return trace_handle_return(s);
1154 }
1155
trace_fn_bin(struct trace_iterator * iter,int flags,struct trace_event * event)1156 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
1157 struct trace_event *event)
1158 {
1159 struct ftrace_entry *field;
1160 struct trace_seq *s = &iter->seq;
1161
1162 trace_assign_type(field, iter->ent);
1163
1164 SEQ_PUT_FIELD(s, field->ip);
1165 SEQ_PUT_FIELD(s, field->parent_ip);
1166
1167 return trace_handle_return(s);
1168 }
1169
1170 static struct trace_event_functions trace_fn_funcs = {
1171 .trace = trace_fn_trace,
1172 .raw = trace_fn_raw,
1173 .hex = trace_fn_hex,
1174 .binary = trace_fn_bin,
1175 };
1176
1177 static struct trace_event trace_fn_event = {
1178 .type = TRACE_FN,
1179 .funcs = &trace_fn_funcs,
1180 };
1181
1182 /* TRACE_CTX an TRACE_WAKE */
trace_ctxwake_print(struct trace_iterator * iter,char * delim)1183 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
1184 char *delim)
1185 {
1186 struct ctx_switch_entry *field;
1187 char comm[TASK_COMM_LEN];
1188 int S, T;
1189
1190
1191 trace_assign_type(field, iter->ent);
1192
1193 T = task_index_to_char(field->next_state);
1194 S = task_index_to_char(field->prev_state);
1195 trace_find_cmdline(field->next_pid, comm);
1196 trace_seq_printf(&iter->seq,
1197 " %7d:%3d:%c %s [%03d] %7d:%3d:%c %s\n",
1198 field->prev_pid,
1199 field->prev_prio,
1200 S, delim,
1201 field->next_cpu,
1202 field->next_pid,
1203 field->next_prio,
1204 T, comm);
1205
1206 return trace_handle_return(&iter->seq);
1207 }
1208
trace_ctx_print(struct trace_iterator * iter,int flags,struct trace_event * event)1209 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
1210 struct trace_event *event)
1211 {
1212 return trace_ctxwake_print(iter, "==>");
1213 }
1214
trace_wake_print(struct trace_iterator * iter,int flags,struct trace_event * event)1215 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1216 int flags, struct trace_event *event)
1217 {
1218 return trace_ctxwake_print(iter, " +");
1219 }
1220
trace_ctxwake_raw(struct trace_iterator * iter,char S)1221 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1222 {
1223 struct ctx_switch_entry *field;
1224 int T;
1225
1226 trace_assign_type(field, iter->ent);
1227
1228 if (!S)
1229 S = task_index_to_char(field->prev_state);
1230 T = task_index_to_char(field->next_state);
1231 trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
1232 field->prev_pid,
1233 field->prev_prio,
1234 S,
1235 field->next_cpu,
1236 field->next_pid,
1237 field->next_prio,
1238 T);
1239
1240 return trace_handle_return(&iter->seq);
1241 }
1242
trace_ctx_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1243 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
1244 struct trace_event *event)
1245 {
1246 return trace_ctxwake_raw(iter, 0);
1247 }
1248
trace_wake_raw(struct trace_iterator * iter,int flags,struct trace_event * event)1249 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
1250 struct trace_event *event)
1251 {
1252 return trace_ctxwake_raw(iter, '+');
1253 }
1254
1255
trace_ctxwake_hex(struct trace_iterator * iter,char S)1256 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1257 {
1258 struct ctx_switch_entry *field;
1259 struct trace_seq *s = &iter->seq;
1260 int T;
1261
1262 trace_assign_type(field, iter->ent);
1263
1264 if (!S)
1265 S = task_index_to_char(field->prev_state);
1266 T = task_index_to_char(field->next_state);
1267
1268 SEQ_PUT_HEX_FIELD(s, field->prev_pid);
1269 SEQ_PUT_HEX_FIELD(s, field->prev_prio);
1270 SEQ_PUT_HEX_FIELD(s, S);
1271 SEQ_PUT_HEX_FIELD(s, field->next_cpu);
1272 SEQ_PUT_HEX_FIELD(s, field->next_pid);
1273 SEQ_PUT_HEX_FIELD(s, field->next_prio);
1274 SEQ_PUT_HEX_FIELD(s, T);
1275
1276 return trace_handle_return(s);
1277 }
1278
trace_ctx_hex(struct trace_iterator * iter,int flags,struct trace_event * event)1279 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1280 struct trace_event *event)
1281 {
1282 return trace_ctxwake_hex(iter, 0);
1283 }
1284
trace_wake_hex(struct trace_iterator * iter,int flags,struct trace_event * event)1285 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1286 struct trace_event *event)
1287 {
1288 return trace_ctxwake_hex(iter, '+');
1289 }
1290
trace_ctxwake_bin(struct trace_iterator * iter,int flags,struct trace_event * event)1291 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1292 int flags, struct trace_event *event)
1293 {
1294 struct ctx_switch_entry *field;
1295 struct trace_seq *s = &iter->seq;
1296
1297 trace_assign_type(field, iter->ent);
1298
1299 SEQ_PUT_FIELD(s, field->prev_pid);
1300 SEQ_PUT_FIELD(s, field->prev_prio);
1301 SEQ_PUT_FIELD(s, field->prev_state);
1302 SEQ_PUT_FIELD(s, field->next_cpu);
1303 SEQ_PUT_FIELD(s, field->next_pid);
1304 SEQ_PUT_FIELD(s, field->next_prio);
1305 SEQ_PUT_FIELD(s, field->next_state);
1306
1307 return trace_handle_return(s);
1308 }
1309
1310 static struct trace_event_functions trace_ctx_funcs = {
1311 .trace = trace_ctx_print,
1312 .raw = trace_ctx_raw,
1313 .hex = trace_ctx_hex,
1314 .binary = trace_ctxwake_bin,
1315 };
1316
1317 static struct trace_event trace_ctx_event = {
1318 .type = TRACE_CTX,
1319 .funcs = &trace_ctx_funcs,
1320 };
1321
1322 static struct trace_event_functions trace_wake_funcs = {
1323 .trace = trace_wake_print,
1324 .raw = trace_wake_raw,
1325 .hex = trace_wake_hex,
1326 .binary = trace_ctxwake_bin,
1327 };
1328
1329 static struct trace_event trace_wake_event = {
1330 .type = TRACE_WAKE,
1331 .funcs = &trace_wake_funcs,
1332 };
1333
1334 /* TRACE_STACK */
1335
trace_stack_print(struct trace_iterator * iter,int flags,struct trace_event * event)1336 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1337 int flags, struct trace_event *event)
1338 {
1339 struct stack_entry *field;
1340 struct trace_seq *s = &iter->seq;
1341 unsigned long *p;
1342 unsigned long *end;
1343 long delta = iter->tr->text_delta;
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, (*p) + delta, 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