1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
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>
16 #include "trace_output.h"
18 /* must be a power of 2 */
19 #define EVENT_HASHSIZE 128
21 DECLARE_RWSEM(trace_event_sem);
23 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
25 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
27 struct trace_seq *s = &iter->seq;
28 struct trace_entry *entry = iter->ent;
29 struct bputs_entry *field;
31 trace_assign_type(field, entry);
33 trace_seq_puts(s, field->str);
35 return trace_handle_return(s);
38 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
40 struct trace_seq *s = &iter->seq;
41 struct trace_entry *entry = iter->ent;
42 struct bprint_entry *field;
44 trace_assign_type(field, entry);
46 trace_seq_bprintf(s, field->fmt, field->buf);
48 return trace_handle_return(s);
51 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
53 struct trace_seq *s = &iter->seq;
54 struct trace_entry *entry = iter->ent;
55 struct print_entry *field;
57 trace_assign_type(field, entry);
59 trace_seq_puts(s, field->buf);
61 return trace_handle_return(s);
65 trace_print_flags_seq(struct trace_seq *p, const char *delim,
67 const struct trace_print_flags *flag_array)
71 const char *ret = trace_seq_buffer_ptr(p);
74 for (i = 0; flag_array[i].name && flags; i++) {
76 mask = flag_array[i].mask;
77 if ((flags & mask) != mask)
80 str = flag_array[i].name;
83 trace_seq_puts(p, delim);
86 trace_seq_puts(p, str);
89 /* check for left over flags */
92 trace_seq_puts(p, delim);
93 trace_seq_printf(p, "0x%lx", flags);
100 EXPORT_SYMBOL(trace_print_flags_seq);
103 trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
104 const struct trace_print_flags *symbol_array)
107 const char *ret = trace_seq_buffer_ptr(p);
109 for (i = 0; symbol_array[i].name; i++) {
111 if (val != symbol_array[i].mask)
114 trace_seq_puts(p, symbol_array[i].name);
118 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
119 trace_seq_printf(p, "0x%lx", val);
121 trace_seq_putc(p, 0);
125 EXPORT_SYMBOL(trace_print_symbols_seq);
127 #if BITS_PER_LONG == 32
129 trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
130 unsigned long long flags,
131 const struct trace_print_flags_u64 *flag_array)
133 unsigned long long mask;
135 const char *ret = trace_seq_buffer_ptr(p);
138 for (i = 0; flag_array[i].name && flags; i++) {
140 mask = flag_array[i].mask;
141 if ((flags & mask) != mask)
144 str = flag_array[i].name;
147 trace_seq_puts(p, delim);
150 trace_seq_puts(p, str);
153 /* check for left over flags */
156 trace_seq_puts(p, delim);
157 trace_seq_printf(p, "0x%llx", flags);
160 trace_seq_putc(p, 0);
164 EXPORT_SYMBOL(trace_print_flags_seq_u64);
167 trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
168 const struct trace_print_flags_u64 *symbol_array)
171 const char *ret = trace_seq_buffer_ptr(p);
173 for (i = 0; symbol_array[i].name; i++) {
175 if (val != symbol_array[i].mask)
178 trace_seq_puts(p, symbol_array[i].name);
182 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
183 trace_seq_printf(p, "0x%llx", val);
185 trace_seq_putc(p, 0);
189 EXPORT_SYMBOL(trace_print_symbols_seq_u64);
193 trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
194 unsigned int bitmask_size)
196 const char *ret = trace_seq_buffer_ptr(p);
198 trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
199 trace_seq_putc(p, 0);
203 EXPORT_SYMBOL_GPL(trace_print_bitmask_seq);
206 * trace_print_hex_seq - print buffer as hex sequence
207 * @p: trace seq struct to write to
208 * @buf: The buffer to print
209 * @buf_len: Length of @buf in bytes
210 * @concatenate: Print @buf as single hex string or with spacing
212 * Prints the passed buffer as a hex sequence either as a whole,
213 * single hex string if @concatenate is true or with spacing after
214 * each byte in case @concatenate is false.
217 trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
221 const char *ret = trace_seq_buffer_ptr(p);
222 const char *fmt = concatenate ? "%*phN" : "%*ph";
224 for (i = 0; i < buf_len; i += 16)
225 trace_seq_printf(p, fmt, min(buf_len - i, 16), &buf[i]);
226 trace_seq_putc(p, 0);
230 EXPORT_SYMBOL(trace_print_hex_seq);
233 trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
236 const char *ret = trace_seq_buffer_ptr(p);
237 const char *prefix = "";
238 void *ptr = (void *)buf;
239 size_t buf_len = count * el_size;
241 trace_seq_putc(p, '{');
243 while (ptr < buf + buf_len) {
246 trace_seq_printf(p, "%s0x%x", prefix,
250 trace_seq_printf(p, "%s0x%x", prefix,
254 trace_seq_printf(p, "%s0x%x", prefix,
258 trace_seq_printf(p, "%s0x%llx", prefix,
262 trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
270 trace_seq_putc(p, '}');
271 trace_seq_putc(p, 0);
275 EXPORT_SYMBOL(trace_print_array_seq);
278 trace_print_hex_dump_seq(struct trace_seq *p, const char *prefix_str,
279 int prefix_type, int rowsize, int groupsize,
280 const void *buf, size_t len, bool ascii)
282 const char *ret = trace_seq_buffer_ptr(p);
284 trace_seq_putc(p, '\n');
285 trace_seq_hex_dump(p, prefix_str, prefix_type,
286 rowsize, groupsize, buf, len, ascii);
287 trace_seq_putc(p, 0);
290 EXPORT_SYMBOL(trace_print_hex_dump_seq);
292 int trace_raw_output_prep(struct trace_iterator *iter,
293 struct trace_event *trace_event)
295 struct trace_event_call *event;
296 struct trace_seq *s = &iter->seq;
297 struct trace_seq *p = &iter->tmp_seq;
298 struct trace_entry *entry;
300 event = container_of(trace_event, struct trace_event_call, event);
303 if (entry->type != event->event.type) {
305 return TRACE_TYPE_UNHANDLED;
309 trace_seq_printf(s, "%s: ", trace_event_name(event));
311 return trace_handle_return(s);
313 EXPORT_SYMBOL(trace_raw_output_prep);
315 void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...)
320 trace_check_vprintf(iter, trace_event_format(iter, fmt), ap);
323 EXPORT_SYMBOL(trace_event_printf);
325 static int trace_output_raw(struct trace_iterator *iter, char *name,
326 char *fmt, va_list ap)
328 struct trace_seq *s = &iter->seq;
330 trace_seq_printf(s, "%s: ", name);
331 trace_seq_vprintf(s, trace_event_format(iter, fmt), ap);
333 return trace_handle_return(s);
336 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
342 ret = trace_output_raw(iter, name, fmt, ap);
347 EXPORT_SYMBOL_GPL(trace_output_call);
349 static inline const char *kretprobed(const char *name, unsigned long addr)
351 if (is_kretprobe_trampoline(addr))
352 return "[unknown/kretprobe'd]";
357 trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
359 #ifdef CONFIG_KALLSYMS
360 char str[KSYM_SYMBOL_LEN];
364 sprint_symbol(str, address);
366 kallsyms_lookup(address, NULL, NULL, NULL, str);
367 name = kretprobed(str, address);
369 if (name && strlen(name)) {
370 trace_seq_puts(s, name);
374 trace_seq_printf(s, "0x%08lx", address);
378 # define IP_FMT "%08lx"
380 # define IP_FMT "%016lx"
383 static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
384 unsigned long ip, unsigned long sym_flags)
386 struct file *file = NULL;
387 unsigned long vmstart = 0;
394 const struct vm_area_struct *vma;
397 vma = find_vma(mm, ip);
400 vmstart = vma->vm_start;
403 ret = trace_seq_path(s, &file->f_path);
405 trace_seq_printf(s, "[+0x%lx]",
408 mmap_read_unlock(mm);
410 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
411 trace_seq_printf(s, " <" IP_FMT ">", ip);
412 return !trace_seq_has_overflowed(s);
416 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
419 trace_seq_putc(s, '0');
423 trace_seq_print_sym(s, ip, sym_flags & TRACE_ITER_SYM_OFFSET);
425 if (sym_flags & TRACE_ITER_SYM_ADDR)
426 trace_seq_printf(s, " <" IP_FMT ">", ip);
429 return !trace_seq_has_overflowed(s);
433 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
434 * @s: trace seq struct to write to
435 * @entry: The trace entry field from the ring buffer
437 * Prints the generic fields of irqs off, in hard or softirq, preempt
440 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
450 nmi = entry->flags & TRACE_FLAG_NMI;
451 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
452 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
453 bh_off = entry->flags & TRACE_FLAG_BH_OFF;
456 (entry->flags & TRACE_FLAG_IRQS_OFF && bh_off) ? 'D' :
457 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
459 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
462 switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
463 TRACE_FLAG_PREEMPT_RESCHED)) {
464 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
467 case TRACE_FLAG_NEED_RESCHED:
470 case TRACE_FLAG_PREEMPT_RESCHED:
479 (nmi && hardirq) ? 'Z' :
481 (hardirq && softirq) ? 'H' :
486 trace_seq_printf(s, "%c%c%c",
487 irqs_off, need_resched, hardsoft_irq);
489 if (entry->preempt_count & 0xf)
490 trace_seq_printf(s, "%x", entry->preempt_count & 0xf);
492 trace_seq_putc(s, '.');
494 if (entry->preempt_count & 0xf0)
495 trace_seq_printf(s, "%x", entry->preempt_count >> 4);
497 trace_seq_putc(s, '.');
499 return !trace_seq_has_overflowed(s);
503 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
505 char comm[TASK_COMM_LEN];
507 trace_find_cmdline(entry->pid, comm);
509 trace_seq_printf(s, "%8.8s-%-7d %3d",
510 comm, entry->pid, cpu);
512 return trace_print_lat_fmt(s, entry);
516 #define MARK(v, s) {.val = v, .sym = s}
517 /* trace overhead mark */
518 static const struct trace_mark {
519 unsigned long long val; /* unit: nsec */
522 MARK(1000000000ULL , '$'), /* 1 sec */
523 MARK(100000000ULL , '@'), /* 100 msec */
524 MARK(10000000ULL , '*'), /* 10 msec */
525 MARK(1000000ULL , '#'), /* 1000 usecs */
526 MARK(100000ULL , '!'), /* 100 usecs */
527 MARK(10000ULL , '+'), /* 10 usecs */
531 char trace_find_mark(unsigned long long d)
534 int size = ARRAY_SIZE(mark);
536 for (i = 0; i < size; i++) {
541 return (i == size) ? ' ' : mark[i].sym;
545 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
547 struct trace_array *tr = iter->tr;
548 unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
549 unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
550 unsigned long long abs_ts = iter->ts - iter->array_buffer->time_start;
551 unsigned long long rel_ts = next_ts - iter->ts;
552 struct trace_seq *s = &iter->seq;
555 abs_ts = ns2usecs(abs_ts);
556 rel_ts = ns2usecs(rel_ts);
559 if (verbose && in_ns) {
560 unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
561 unsigned long abs_msec = (unsigned long)abs_ts;
562 unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
563 unsigned long rel_msec = (unsigned long)rel_ts;
566 s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
571 } else if (verbose && !in_ns) {
573 s, "[%016llx] %lld (+%lld): ",
574 iter->ts, abs_ts, rel_ts);
576 } else if (!verbose && in_ns) {
580 trace_find_mark(rel_ts * NSEC_PER_USEC));
582 } else { /* !verbose && !in_ns */
583 trace_seq_printf(s, " %4lld: ", abs_ts);
586 return !trace_seq_has_overflowed(s);
589 static void trace_print_time(struct trace_seq *s, struct trace_iterator *iter,
590 unsigned long long ts)
592 unsigned long secs, usec_rem;
593 unsigned long long t;
595 if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
597 usec_rem = do_div(t, USEC_PER_SEC);
598 secs = (unsigned long)t;
599 trace_seq_printf(s, " %5lu.%06lu", secs, usec_rem);
601 trace_seq_printf(s, " %12llu", ts);
604 int trace_print_context(struct trace_iterator *iter)
606 struct trace_array *tr = iter->tr;
607 struct trace_seq *s = &iter->seq;
608 struct trace_entry *entry = iter->ent;
609 char comm[TASK_COMM_LEN];
611 trace_find_cmdline(entry->pid, comm);
613 trace_seq_printf(s, "%16s-%-7d ", comm, entry->pid);
615 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
616 unsigned int tgid = trace_find_tgid(entry->pid);
619 trace_seq_printf(s, "(-------) ");
621 trace_seq_printf(s, "(%7d) ", tgid);
624 trace_seq_printf(s, "[%03d] ", iter->cpu);
626 if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
627 trace_print_lat_fmt(s, entry);
629 trace_print_time(s, iter, iter->ts);
630 trace_seq_puts(s, ": ");
632 return !trace_seq_has_overflowed(s);
635 int trace_print_lat_context(struct trace_iterator *iter)
637 struct trace_entry *entry, *next_entry;
638 struct trace_array *tr = iter->tr;
639 struct trace_seq *s = &iter->seq;
640 unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
643 next_entry = trace_find_next_entry(iter, NULL, &next_ts);
647 /* trace_find_next_entry() may change iter->ent */
651 char comm[TASK_COMM_LEN];
653 trace_find_cmdline(entry->pid, comm);
656 s, "%16s %7d %3d %d %08x %08lx ",
657 comm, entry->pid, iter->cpu, entry->flags,
658 entry->preempt_count & 0xf, iter->idx);
660 lat_print_generic(s, entry, iter->cpu);
663 lat_print_timestamp(iter, next_ts);
665 return !trace_seq_has_overflowed(s);
669 * ftrace_find_event - find a registered event
670 * @type: the type of event to look for
672 * Returns an event of type @type otherwise NULL
673 * Called with trace_event_read_lock() held.
675 struct trace_event *ftrace_find_event(int type)
677 struct trace_event *event;
680 key = type & (EVENT_HASHSIZE - 1);
682 hlist_for_each_entry(event, &event_hash[key], node) {
683 if (event->type == type)
690 static DEFINE_IDA(trace_event_ida);
692 static void free_trace_event_type(int type)
694 if (type >= __TRACE_LAST_TYPE)
695 ida_free(&trace_event_ida, type);
698 static int alloc_trace_event_type(void)
702 /* Skip static defined type numbers */
703 next = ida_alloc_range(&trace_event_ida, __TRACE_LAST_TYPE,
704 TRACE_EVENT_TYPE_MAX, GFP_KERNEL);
710 void trace_event_read_lock(void)
712 down_read(&trace_event_sem);
715 void trace_event_read_unlock(void)
717 up_read(&trace_event_sem);
721 * register_trace_event - register output for an event type
722 * @event: the event type to register
724 * Event types are stored in a hash and this hash is used to
725 * find a way to print an event. If the @event->type is set
726 * then it will use that type, otherwise it will assign a
729 * If you assign your own type, please make sure it is added
730 * to the trace_type enum in trace.h, to avoid collisions
731 * with the dynamic types.
733 * Returns the event type number or zero on error.
735 int register_trace_event(struct trace_event *event)
740 down_write(&trace_event_sem);
745 if (WARN_ON(!event->funcs))
749 event->type = alloc_trace_event_type();
752 } else if (WARN(event->type > __TRACE_LAST_TYPE,
753 "Need to add type to trace.h")) {
756 /* Is this event already used */
757 if (ftrace_find_event(event->type))
761 if (event->funcs->trace == NULL)
762 event->funcs->trace = trace_nop_print;
763 if (event->funcs->raw == NULL)
764 event->funcs->raw = trace_nop_print;
765 if (event->funcs->hex == NULL)
766 event->funcs->hex = trace_nop_print;
767 if (event->funcs->binary == NULL)
768 event->funcs->binary = trace_nop_print;
770 key = event->type & (EVENT_HASHSIZE - 1);
772 hlist_add_head(&event->node, &event_hash[key]);
776 up_write(&trace_event_sem);
780 EXPORT_SYMBOL_GPL(register_trace_event);
783 * Used by module code with the trace_event_sem held for write.
785 int __unregister_trace_event(struct trace_event *event)
787 hlist_del(&event->node);
788 free_trace_event_type(event->type);
793 * unregister_trace_event - remove a no longer used event
794 * @event: the event to remove
796 int unregister_trace_event(struct trace_event *event)
798 down_write(&trace_event_sem);
799 __unregister_trace_event(event);
800 up_write(&trace_event_sem);
804 EXPORT_SYMBOL_GPL(unregister_trace_event);
810 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
811 struct trace_event *event)
813 trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
815 return trace_handle_return(&iter->seq);
818 static void print_fn_trace(struct trace_seq *s, unsigned long ip,
819 unsigned long parent_ip, int flags)
821 seq_print_ip_sym(s, ip, flags);
823 if ((flags & TRACE_ITER_PRINT_PARENT) && parent_ip) {
824 trace_seq_puts(s, " <-");
825 seq_print_ip_sym(s, parent_ip, flags);
830 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
831 struct trace_event *event)
833 struct ftrace_entry *field;
834 struct trace_seq *s = &iter->seq;
836 trace_assign_type(field, iter->ent);
838 print_fn_trace(s, field->ip, field->parent_ip, flags);
839 trace_seq_putc(s, '\n');
841 return trace_handle_return(s);
844 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
845 struct trace_event *event)
847 struct ftrace_entry *field;
849 trace_assign_type(field, iter->ent);
851 trace_seq_printf(&iter->seq, "%lx %lx\n",
855 return trace_handle_return(&iter->seq);
858 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
859 struct trace_event *event)
861 struct ftrace_entry *field;
862 struct trace_seq *s = &iter->seq;
864 trace_assign_type(field, iter->ent);
866 SEQ_PUT_HEX_FIELD(s, field->ip);
867 SEQ_PUT_HEX_FIELD(s, field->parent_ip);
869 return trace_handle_return(s);
872 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
873 struct trace_event *event)
875 struct ftrace_entry *field;
876 struct trace_seq *s = &iter->seq;
878 trace_assign_type(field, iter->ent);
880 SEQ_PUT_FIELD(s, field->ip);
881 SEQ_PUT_FIELD(s, field->parent_ip);
883 return trace_handle_return(s);
886 static struct trace_event_functions trace_fn_funcs = {
887 .trace = trace_fn_trace,
890 .binary = trace_fn_bin,
893 static struct trace_event trace_fn_event = {
895 .funcs = &trace_fn_funcs,
898 /* TRACE_CTX an TRACE_WAKE */
899 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
902 struct ctx_switch_entry *field;
903 char comm[TASK_COMM_LEN];
907 trace_assign_type(field, iter->ent);
909 T = task_index_to_char(field->next_state);
910 S = task_index_to_char(field->prev_state);
911 trace_find_cmdline(field->next_pid, comm);
912 trace_seq_printf(&iter->seq,
913 " %7d:%3d:%c %s [%03d] %7d:%3d:%c %s\n",
922 return trace_handle_return(&iter->seq);
925 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
926 struct trace_event *event)
928 return trace_ctxwake_print(iter, "==>");
931 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
932 int flags, struct trace_event *event)
934 return trace_ctxwake_print(iter, " +");
937 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
939 struct ctx_switch_entry *field;
942 trace_assign_type(field, iter->ent);
945 S = task_index_to_char(field->prev_state);
946 T = task_index_to_char(field->next_state);
947 trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
956 return trace_handle_return(&iter->seq);
959 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
960 struct trace_event *event)
962 return trace_ctxwake_raw(iter, 0);
965 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
966 struct trace_event *event)
968 return trace_ctxwake_raw(iter, '+');
972 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
974 struct ctx_switch_entry *field;
975 struct trace_seq *s = &iter->seq;
978 trace_assign_type(field, iter->ent);
981 S = task_index_to_char(field->prev_state);
982 T = task_index_to_char(field->next_state);
984 SEQ_PUT_HEX_FIELD(s, field->prev_pid);
985 SEQ_PUT_HEX_FIELD(s, field->prev_prio);
986 SEQ_PUT_HEX_FIELD(s, S);
987 SEQ_PUT_HEX_FIELD(s, field->next_cpu);
988 SEQ_PUT_HEX_FIELD(s, field->next_pid);
989 SEQ_PUT_HEX_FIELD(s, field->next_prio);
990 SEQ_PUT_HEX_FIELD(s, T);
992 return trace_handle_return(s);
995 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
996 struct trace_event *event)
998 return trace_ctxwake_hex(iter, 0);
1001 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1002 struct trace_event *event)
1004 return trace_ctxwake_hex(iter, '+');
1007 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1008 int flags, struct trace_event *event)
1010 struct ctx_switch_entry *field;
1011 struct trace_seq *s = &iter->seq;
1013 trace_assign_type(field, iter->ent);
1015 SEQ_PUT_FIELD(s, field->prev_pid);
1016 SEQ_PUT_FIELD(s, field->prev_prio);
1017 SEQ_PUT_FIELD(s, field->prev_state);
1018 SEQ_PUT_FIELD(s, field->next_cpu);
1019 SEQ_PUT_FIELD(s, field->next_pid);
1020 SEQ_PUT_FIELD(s, field->next_prio);
1021 SEQ_PUT_FIELD(s, field->next_state);
1023 return trace_handle_return(s);
1026 static struct trace_event_functions trace_ctx_funcs = {
1027 .trace = trace_ctx_print,
1028 .raw = trace_ctx_raw,
1029 .hex = trace_ctx_hex,
1030 .binary = trace_ctxwake_bin,
1033 static struct trace_event trace_ctx_event = {
1035 .funcs = &trace_ctx_funcs,
1038 static struct trace_event_functions trace_wake_funcs = {
1039 .trace = trace_wake_print,
1040 .raw = trace_wake_raw,
1041 .hex = trace_wake_hex,
1042 .binary = trace_ctxwake_bin,
1045 static struct trace_event trace_wake_event = {
1047 .funcs = &trace_wake_funcs,
1052 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1053 int flags, struct trace_event *event)
1055 struct stack_entry *field;
1056 struct trace_seq *s = &iter->seq;
1060 trace_assign_type(field, iter->ent);
1061 end = (unsigned long *)((long)iter->ent + iter->ent_size);
1063 trace_seq_puts(s, "<stack trace>\n");
1065 for (p = field->caller; p && p < end && *p != ULONG_MAX; p++) {
1067 if (trace_seq_has_overflowed(s))
1070 trace_seq_puts(s, " => ");
1071 seq_print_ip_sym(s, *p, flags);
1072 trace_seq_putc(s, '\n');
1075 return trace_handle_return(s);
1078 static struct trace_event_functions trace_stack_funcs = {
1079 .trace = trace_stack_print,
1082 static struct trace_event trace_stack_event = {
1083 .type = TRACE_STACK,
1084 .funcs = &trace_stack_funcs,
1087 /* TRACE_USER_STACK */
1088 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1089 int flags, struct trace_event *event)
1091 struct trace_array *tr = iter->tr;
1092 struct userstack_entry *field;
1093 struct trace_seq *s = &iter->seq;
1094 struct mm_struct *mm = NULL;
1097 trace_assign_type(field, iter->ent);
1099 trace_seq_puts(s, "<user stack trace>\n");
1101 if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1102 struct task_struct *task;
1104 * we do the lookup on the thread group leader,
1105 * since individual threads might have already quit!
1108 task = find_task_by_vpid(field->tgid);
1110 mm = get_task_mm(task);
1114 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1115 unsigned long ip = field->caller[i];
1117 if (!ip || trace_seq_has_overflowed(s))
1120 trace_seq_puts(s, " => ");
1121 seq_print_user_ip(s, mm, ip, flags);
1122 trace_seq_putc(s, '\n');
1128 return trace_handle_return(s);
1131 static struct trace_event_functions trace_user_stack_funcs = {
1132 .trace = trace_user_stack_print,
1135 static struct trace_event trace_user_stack_event = {
1136 .type = TRACE_USER_STACK,
1137 .funcs = &trace_user_stack_funcs,
1141 static enum print_line_t
1142 trace_hwlat_print(struct trace_iterator *iter, int flags,
1143 struct trace_event *event)
1145 struct trace_entry *entry = iter->ent;
1146 struct trace_seq *s = &iter->seq;
1147 struct hwlat_entry *field;
1149 trace_assign_type(field, entry);
1151 trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%lld.%09ld count:%d",
1154 field->outer_duration,
1155 (long long)field->timestamp.tv_sec,
1156 field->timestamp.tv_nsec, field->count);
1158 if (field->nmi_count) {
1160 * The generic sched_clock() is not NMI safe, thus
1161 * we only record the count and not the time.
1163 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
1164 trace_seq_printf(s, " nmi-total:%llu",
1165 field->nmi_total_ts);
1166 trace_seq_printf(s, " nmi-count:%u",
1170 trace_seq_putc(s, '\n');
1172 return trace_handle_return(s);
1175 static enum print_line_t
1176 trace_hwlat_raw(struct trace_iterator *iter, int flags,
1177 struct trace_event *event)
1179 struct hwlat_entry *field;
1180 struct trace_seq *s = &iter->seq;
1182 trace_assign_type(field, iter->ent);
1184 trace_seq_printf(s, "%llu %lld %lld %09ld %u\n",
1186 field->outer_duration,
1187 (long long)field->timestamp.tv_sec,
1188 field->timestamp.tv_nsec,
1191 return trace_handle_return(s);
1194 static struct trace_event_functions trace_hwlat_funcs = {
1195 .trace = trace_hwlat_print,
1196 .raw = trace_hwlat_raw,
1199 static struct trace_event trace_hwlat_event = {
1200 .type = TRACE_HWLAT,
1201 .funcs = &trace_hwlat_funcs,
1205 static enum print_line_t
1206 trace_osnoise_print(struct trace_iterator *iter, int flags,
1207 struct trace_event *event)
1209 struct trace_entry *entry = iter->ent;
1210 struct trace_seq *s = &iter->seq;
1211 struct osnoise_entry *field;
1212 u64 ratio, ratio_dec;
1215 trace_assign_type(field, entry);
1218 * compute the available % of cpu time.
1220 net_runtime = field->runtime - field->noise;
1221 ratio = net_runtime * 10000000;
1222 do_div(ratio, field->runtime);
1223 ratio_dec = do_div(ratio, 100000);
1225 trace_seq_printf(s, "%llu %10llu %3llu.%05llu %7llu",
1231 trace_seq_printf(s, " %6u", field->hw_count);
1232 trace_seq_printf(s, " %6u", field->nmi_count);
1233 trace_seq_printf(s, " %6u", field->irq_count);
1234 trace_seq_printf(s, " %6u", field->softirq_count);
1235 trace_seq_printf(s, " %6u", field->thread_count);
1237 trace_seq_putc(s, '\n');
1239 return trace_handle_return(s);
1242 static enum print_line_t
1243 trace_osnoise_raw(struct trace_iterator *iter, int flags,
1244 struct trace_event *event)
1246 struct osnoise_entry *field;
1247 struct trace_seq *s = &iter->seq;
1249 trace_assign_type(field, iter->ent);
1251 trace_seq_printf(s, "%lld %llu %llu %u %u %u %u %u\n",
1258 field->softirq_count,
1259 field->thread_count);
1261 return trace_handle_return(s);
1264 static struct trace_event_functions trace_osnoise_funcs = {
1265 .trace = trace_osnoise_print,
1266 .raw = trace_osnoise_raw,
1269 static struct trace_event trace_osnoise_event = {
1270 .type = TRACE_OSNOISE,
1271 .funcs = &trace_osnoise_funcs,
1274 /* TRACE_TIMERLAT */
1275 static enum print_line_t
1276 trace_timerlat_print(struct trace_iterator *iter, int flags,
1277 struct trace_event *event)
1279 struct trace_entry *entry = iter->ent;
1280 struct trace_seq *s = &iter->seq;
1281 struct timerlat_entry *field;
1283 trace_assign_type(field, entry);
1285 trace_seq_printf(s, "#%-5u context %6s timer_latency %9llu ns\n",
1287 field->context ? "thread" : "irq",
1288 field->timer_latency);
1290 return trace_handle_return(s);
1293 static enum print_line_t
1294 trace_timerlat_raw(struct trace_iterator *iter, int flags,
1295 struct trace_event *event)
1297 struct timerlat_entry *field;
1298 struct trace_seq *s = &iter->seq;
1300 trace_assign_type(field, iter->ent);
1302 trace_seq_printf(s, "%u %d %llu\n",
1305 field->timer_latency);
1307 return trace_handle_return(s);
1310 static struct trace_event_functions trace_timerlat_funcs = {
1311 .trace = trace_timerlat_print,
1312 .raw = trace_timerlat_raw,
1315 static struct trace_event trace_timerlat_event = {
1316 .type = TRACE_TIMERLAT,
1317 .funcs = &trace_timerlat_funcs,
1321 static enum print_line_t
1322 trace_bputs_print(struct trace_iterator *iter, int flags,
1323 struct trace_event *event)
1325 struct trace_entry *entry = iter->ent;
1326 struct trace_seq *s = &iter->seq;
1327 struct bputs_entry *field;
1329 trace_assign_type(field, entry);
1331 seq_print_ip_sym(s, field->ip, flags);
1332 trace_seq_puts(s, ": ");
1333 trace_seq_puts(s, field->str);
1335 return trace_handle_return(s);
1339 static enum print_line_t
1340 trace_bputs_raw(struct trace_iterator *iter, int flags,
1341 struct trace_event *event)
1343 struct bputs_entry *field;
1344 struct trace_seq *s = &iter->seq;
1346 trace_assign_type(field, iter->ent);
1348 trace_seq_printf(s, ": %lx : ", field->ip);
1349 trace_seq_puts(s, field->str);
1351 return trace_handle_return(s);
1354 static struct trace_event_functions trace_bputs_funcs = {
1355 .trace = trace_bputs_print,
1356 .raw = trace_bputs_raw,
1359 static struct trace_event trace_bputs_event = {
1360 .type = TRACE_BPUTS,
1361 .funcs = &trace_bputs_funcs,
1365 static enum print_line_t
1366 trace_bprint_print(struct trace_iterator *iter, int flags,
1367 struct trace_event *event)
1369 struct trace_entry *entry = iter->ent;
1370 struct trace_seq *s = &iter->seq;
1371 struct bprint_entry *field;
1373 trace_assign_type(field, entry);
1375 seq_print_ip_sym(s, field->ip, flags);
1376 trace_seq_puts(s, ": ");
1377 trace_seq_bprintf(s, field->fmt, field->buf);
1379 return trace_handle_return(s);
1383 static enum print_line_t
1384 trace_bprint_raw(struct trace_iterator *iter, int flags,
1385 struct trace_event *event)
1387 struct bprint_entry *field;
1388 struct trace_seq *s = &iter->seq;
1390 trace_assign_type(field, iter->ent);
1392 trace_seq_printf(s, ": %lx : ", field->ip);
1393 trace_seq_bprintf(s, field->fmt, field->buf);
1395 return trace_handle_return(s);
1398 static struct trace_event_functions trace_bprint_funcs = {
1399 .trace = trace_bprint_print,
1400 .raw = trace_bprint_raw,
1403 static struct trace_event trace_bprint_event = {
1404 .type = TRACE_BPRINT,
1405 .funcs = &trace_bprint_funcs,
1409 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1410 int flags, struct trace_event *event)
1412 struct print_entry *field;
1413 struct trace_seq *s = &iter->seq;
1415 trace_assign_type(field, iter->ent);
1417 seq_print_ip_sym(s, field->ip, flags);
1418 trace_seq_printf(s, ": %s", field->buf);
1420 return trace_handle_return(s);
1423 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1424 struct trace_event *event)
1426 struct print_entry *field;
1428 trace_assign_type(field, iter->ent);
1430 trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
1432 return trace_handle_return(&iter->seq);
1435 static struct trace_event_functions trace_print_funcs = {
1436 .trace = trace_print_print,
1437 .raw = trace_print_raw,
1440 static struct trace_event trace_print_event = {
1441 .type = TRACE_PRINT,
1442 .funcs = &trace_print_funcs,
1445 static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
1446 struct trace_event *event)
1448 struct raw_data_entry *field;
1451 trace_assign_type(field, iter->ent);
1453 trace_seq_printf(&iter->seq, "# %x buf:", field->id);
1455 for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
1456 trace_seq_printf(&iter->seq, " %02x",
1457 (unsigned char)field->buf[i]);
1459 trace_seq_putc(&iter->seq, '\n');
1461 return trace_handle_return(&iter->seq);
1464 static struct trace_event_functions trace_raw_data_funcs = {
1465 .trace = trace_raw_data,
1466 .raw = trace_raw_data,
1469 static struct trace_event trace_raw_data_event = {
1470 .type = TRACE_RAW_DATA,
1471 .funcs = &trace_raw_data_funcs,
1474 static enum print_line_t
1475 trace_func_repeats_raw(struct trace_iterator *iter, int flags,
1476 struct trace_event *event)
1478 struct func_repeats_entry *field;
1479 struct trace_seq *s = &iter->seq;
1481 trace_assign_type(field, iter->ent);
1483 trace_seq_printf(s, "%lu %lu %u %llu\n",
1487 FUNC_REPEATS_GET_DELTA_TS(field));
1489 return trace_handle_return(s);
1492 static enum print_line_t
1493 trace_func_repeats_print(struct trace_iterator *iter, int flags,
1494 struct trace_event *event)
1496 struct func_repeats_entry *field;
1497 struct trace_seq *s = &iter->seq;
1499 trace_assign_type(field, iter->ent);
1501 print_fn_trace(s, field->ip, field->parent_ip, flags);
1502 trace_seq_printf(s, " (repeats: %u, last_ts:", field->count);
1503 trace_print_time(s, iter,
1504 iter->ts - FUNC_REPEATS_GET_DELTA_TS(field));
1505 trace_seq_puts(s, ")\n");
1507 return trace_handle_return(s);
1510 static struct trace_event_functions trace_func_repeats_funcs = {
1511 .trace = trace_func_repeats_print,
1512 .raw = trace_func_repeats_raw,
1515 static struct trace_event trace_func_repeats_event = {
1516 .type = TRACE_FUNC_REPEATS,
1517 .funcs = &trace_func_repeats_funcs,
1520 static struct trace_event *events[] __initdata = {
1525 &trace_user_stack_event,
1527 &trace_bprint_event,
1530 &trace_osnoise_event,
1531 &trace_timerlat_event,
1532 &trace_raw_data_event,
1533 &trace_func_repeats_event,
1537 __init static int init_events(void)
1539 struct trace_event *event;
1542 for (i = 0; events[i]; i++) {
1544 ret = register_trace_event(event);
1545 WARN_ONCE(!ret, "event %d failed to register", event->type);
1550 early_initcall(init_events);