Merge branch 'x86/microcode' into x86/urgent, to pick up cleanup
[linux/fpc-iii.git] / kernel / trace / trace_output.c
blob02a4aeb22c4785cc808a7906c970dfa420796cbe
1 /*
2 * trace_output.c
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 */
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/ftrace.h>
11 #include <linux/sched/clock.h>
12 #include <linux/sched/mm.h>
14 #include "trace_output.h"
16 /* must be a power of 2 */
17 #define EVENT_HASHSIZE 128
19 DECLARE_RWSEM(trace_event_sem);
21 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
23 static int next_event_type = __TRACE_LAST_TYPE + 1;
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);
64 const char *
65 trace_print_flags_seq(struct trace_seq *p, const char *delim,
66 unsigned long flags,
67 const struct trace_print_flags *flag_array)
69 unsigned long mask;
70 const char *str;
71 const char *ret = trace_seq_buffer_ptr(p);
72 int i, first = 1;
74 for (i = 0; flag_array[i].name && flags; i++) {
76 mask = flag_array[i].mask;
77 if ((flags & mask) != mask)
78 continue;
80 str = flag_array[i].name;
81 flags &= ~mask;
82 if (!first && delim)
83 trace_seq_puts(p, delim);
84 else
85 first = 0;
86 trace_seq_puts(p, str);
89 /* check for left over flags */
90 if (flags) {
91 if (!first && delim)
92 trace_seq_puts(p, delim);
93 trace_seq_printf(p, "0x%lx", flags);
96 trace_seq_putc(p, 0);
98 return ret;
100 EXPORT_SYMBOL(trace_print_flags_seq);
102 const char *
103 trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
104 const struct trace_print_flags *symbol_array)
106 int i;
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)
112 continue;
114 trace_seq_puts(p, symbol_array[i].name);
115 break;
118 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
119 trace_seq_printf(p, "0x%lx", val);
121 trace_seq_putc(p, 0);
123 return ret;
125 EXPORT_SYMBOL(trace_print_symbols_seq);
127 #if BITS_PER_LONG == 32
128 const char *
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;
134 const char *str;
135 const char *ret = trace_seq_buffer_ptr(p);
136 int i, first = 1;
138 for (i = 0; flag_array[i].name && flags; i++) {
140 mask = flag_array[i].mask;
141 if ((flags & mask) != mask)
142 continue;
144 str = flag_array[i].name;
145 flags &= ~mask;
146 if (!first && delim)
147 trace_seq_puts(p, delim);
148 else
149 first = 0;
150 trace_seq_puts(p, str);
153 /* check for left over flags */
154 if (flags) {
155 if (!first && delim)
156 trace_seq_puts(p, delim);
157 trace_seq_printf(p, "0x%llx", flags);
160 trace_seq_putc(p, 0);
162 return ret;
164 EXPORT_SYMBOL(trace_print_flags_seq_u64);
166 const char *
167 trace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
168 const struct trace_print_flags_u64 *symbol_array)
170 int i;
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)
176 continue;
178 trace_seq_puts(p, symbol_array[i].name);
179 break;
182 if (ret == (const char *)(trace_seq_buffer_ptr(p)))
183 trace_seq_printf(p, "0x%llx", val);
185 trace_seq_putc(p, 0);
187 return ret;
189 EXPORT_SYMBOL(trace_print_symbols_seq_u64);
190 #endif
192 const char *
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);
201 return ret;
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.
216 const char *
217 trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len,
218 bool concatenate)
220 int i;
221 const char *ret = trace_seq_buffer_ptr(p);
223 for (i = 0; i < buf_len; i++)
224 trace_seq_printf(p, "%s%2.2x", concatenate || i == 0 ? "" : " ",
225 buf[i]);
226 trace_seq_putc(p, 0);
228 return ret;
230 EXPORT_SYMBOL(trace_print_hex_seq);
232 const char *
233 trace_print_array_seq(struct trace_seq *p, const void *buf, int count,
234 size_t el_size)
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) {
244 switch (el_size) {
245 case 1:
246 trace_seq_printf(p, "%s0x%x", prefix,
247 *(u8 *)ptr);
248 break;
249 case 2:
250 trace_seq_printf(p, "%s0x%x", prefix,
251 *(u16 *)ptr);
252 break;
253 case 4:
254 trace_seq_printf(p, "%s0x%x", prefix,
255 *(u32 *)ptr);
256 break;
257 case 8:
258 trace_seq_printf(p, "%s0x%llx", prefix,
259 *(u64 *)ptr);
260 break;
261 default:
262 trace_seq_printf(p, "BAD SIZE:%zu 0x%x", el_size,
263 *(u8 *)ptr);
264 el_size = 1;
266 prefix = ",";
267 ptr += el_size;
270 trace_seq_putc(p, '}');
271 trace_seq_putc(p, 0);
273 return ret;
275 EXPORT_SYMBOL(trace_print_array_seq);
277 int trace_raw_output_prep(struct trace_iterator *iter,
278 struct trace_event *trace_event)
280 struct trace_event_call *event;
281 struct trace_seq *s = &iter->seq;
282 struct trace_seq *p = &iter->tmp_seq;
283 struct trace_entry *entry;
285 event = container_of(trace_event, struct trace_event_call, event);
286 entry = iter->ent;
288 if (entry->type != event->event.type) {
289 WARN_ON_ONCE(1);
290 return TRACE_TYPE_UNHANDLED;
293 trace_seq_init(p);
294 trace_seq_printf(s, "%s: ", trace_event_name(event));
296 return trace_handle_return(s);
298 EXPORT_SYMBOL(trace_raw_output_prep);
300 static int trace_output_raw(struct trace_iterator *iter, char *name,
301 char *fmt, va_list ap)
303 struct trace_seq *s = &iter->seq;
305 trace_seq_printf(s, "%s: ", name);
306 trace_seq_vprintf(s, fmt, ap);
308 return trace_handle_return(s);
311 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...)
313 va_list ap;
314 int ret;
316 va_start(ap, fmt);
317 ret = trace_output_raw(iter, name, fmt, ap);
318 va_end(ap);
320 return ret;
322 EXPORT_SYMBOL_GPL(trace_output_call);
324 #ifdef CONFIG_KRETPROBES
325 static inline const char *kretprobed(const char *name)
327 static const char tramp_name[] = "kretprobe_trampoline";
328 int size = sizeof(tramp_name);
330 if (strncmp(tramp_name, name, size) == 0)
331 return "[unknown/kretprobe'd]";
332 return name;
334 #else
335 static inline const char *kretprobed(const char *name)
337 return name;
339 #endif /* CONFIG_KRETPROBES */
341 static void
342 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
344 #ifdef CONFIG_KALLSYMS
345 char str[KSYM_SYMBOL_LEN];
346 const char *name;
348 kallsyms_lookup(address, NULL, NULL, NULL, str);
350 name = kretprobed(str);
352 trace_seq_printf(s, fmt, name);
353 #endif
356 static void
357 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
358 unsigned long address)
360 #ifdef CONFIG_KALLSYMS
361 char str[KSYM_SYMBOL_LEN];
362 const char *name;
364 sprint_symbol(str, address);
365 name = kretprobed(str);
367 trace_seq_printf(s, fmt, name);
368 #endif
371 #ifndef CONFIG_64BIT
372 # define IP_FMT "%08lx"
373 #else
374 # define IP_FMT "%016lx"
375 #endif
377 static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
378 unsigned long ip, unsigned long sym_flags)
380 struct file *file = NULL;
381 unsigned long vmstart = 0;
382 int ret = 1;
384 if (s->full)
385 return 0;
387 if (mm) {
388 const struct vm_area_struct *vma;
390 down_read(&mm->mmap_sem);
391 vma = find_vma(mm, ip);
392 if (vma) {
393 file = vma->vm_file;
394 vmstart = vma->vm_start;
396 if (file) {
397 ret = trace_seq_path(s, &file->f_path);
398 if (ret)
399 trace_seq_printf(s, "[+0x%lx]",
400 ip - vmstart);
402 up_read(&mm->mmap_sem);
404 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
405 trace_seq_printf(s, " <" IP_FMT ">", ip);
406 return !trace_seq_has_overflowed(s);
410 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
412 if (!ip) {
413 trace_seq_putc(s, '0');
414 goto out;
417 if (sym_flags & TRACE_ITER_SYM_OFFSET)
418 seq_print_sym_offset(s, "%s", ip);
419 else
420 seq_print_sym_short(s, "%s", ip);
422 if (sym_flags & TRACE_ITER_SYM_ADDR)
423 trace_seq_printf(s, " <" IP_FMT ">", ip);
425 out:
426 return !trace_seq_has_overflowed(s);
430 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
431 * @s: trace seq struct to write to
432 * @entry: The trace entry field from the ring buffer
434 * Prints the generic fields of irqs off, in hard or softirq, preempt
435 * count.
437 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
439 char hardsoft_irq;
440 char need_resched;
441 char irqs_off;
442 int hardirq;
443 int softirq;
444 int nmi;
446 nmi = entry->flags & TRACE_FLAG_NMI;
447 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
448 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
450 irqs_off =
451 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
452 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
453 '.';
455 switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
456 TRACE_FLAG_PREEMPT_RESCHED)) {
457 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
458 need_resched = 'N';
459 break;
460 case TRACE_FLAG_NEED_RESCHED:
461 need_resched = 'n';
462 break;
463 case TRACE_FLAG_PREEMPT_RESCHED:
464 need_resched = 'p';
465 break;
466 default:
467 need_resched = '.';
468 break;
471 hardsoft_irq =
472 (nmi && hardirq) ? 'Z' :
473 nmi ? 'z' :
474 (hardirq && softirq) ? 'H' :
475 hardirq ? 'h' :
476 softirq ? 's' :
477 '.' ;
479 trace_seq_printf(s, "%c%c%c",
480 irqs_off, need_resched, hardsoft_irq);
482 if (entry->preempt_count)
483 trace_seq_printf(s, "%x", entry->preempt_count);
484 else
485 trace_seq_putc(s, '.');
487 return !trace_seq_has_overflowed(s);
490 static int
491 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
493 char comm[TASK_COMM_LEN];
495 trace_find_cmdline(entry->pid, comm);
497 trace_seq_printf(s, "%8.8s-%-5d %3d",
498 comm, entry->pid, cpu);
500 return trace_print_lat_fmt(s, entry);
503 #undef MARK
504 #define MARK(v, s) {.val = v, .sym = s}
505 /* trace overhead mark */
506 static const struct trace_mark {
507 unsigned long long val; /* unit: nsec */
508 char sym;
509 } mark[] = {
510 MARK(1000000000ULL , '$'), /* 1 sec */
511 MARK(100000000ULL , '@'), /* 100 msec */
512 MARK(10000000ULL , '*'), /* 10 msec */
513 MARK(1000000ULL , '#'), /* 1000 usecs */
514 MARK(100000ULL , '!'), /* 100 usecs */
515 MARK(10000ULL , '+'), /* 10 usecs */
517 #undef MARK
519 char trace_find_mark(unsigned long long d)
521 int i;
522 int size = ARRAY_SIZE(mark);
524 for (i = 0; i < size; i++) {
525 if (d > mark[i].val)
526 break;
529 return (i == size) ? ' ' : mark[i].sym;
532 static int
533 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
535 struct trace_array *tr = iter->tr;
536 unsigned long verbose = tr->trace_flags & TRACE_ITER_VERBOSE;
537 unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
538 unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
539 unsigned long long rel_ts = next_ts - iter->ts;
540 struct trace_seq *s = &iter->seq;
542 if (in_ns) {
543 abs_ts = ns2usecs(abs_ts);
544 rel_ts = ns2usecs(rel_ts);
547 if (verbose && in_ns) {
548 unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
549 unsigned long abs_msec = (unsigned long)abs_ts;
550 unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
551 unsigned long rel_msec = (unsigned long)rel_ts;
553 trace_seq_printf(
554 s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
555 ns2usecs(iter->ts),
556 abs_msec, abs_usec,
557 rel_msec, rel_usec);
559 } else if (verbose && !in_ns) {
560 trace_seq_printf(
561 s, "[%016llx] %lld (+%lld): ",
562 iter->ts, abs_ts, rel_ts);
564 } else if (!verbose && in_ns) {
565 trace_seq_printf(
566 s, " %4lldus%c: ",
567 abs_ts,
568 trace_find_mark(rel_ts * NSEC_PER_USEC));
570 } else { /* !verbose && !in_ns */
571 trace_seq_printf(s, " %4lld: ", abs_ts);
574 return !trace_seq_has_overflowed(s);
577 int trace_print_context(struct trace_iterator *iter)
579 struct trace_array *tr = iter->tr;
580 struct trace_seq *s = &iter->seq;
581 struct trace_entry *entry = iter->ent;
582 unsigned long long t;
583 unsigned long secs, usec_rem;
584 char comm[TASK_COMM_LEN];
586 trace_find_cmdline(entry->pid, comm);
588 trace_seq_printf(s, "%16s-%-5d [%03d] ",
589 comm, entry->pid, iter->cpu);
591 if (tr->trace_flags & TRACE_ITER_IRQ_INFO)
592 trace_print_lat_fmt(s, entry);
594 if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
595 t = ns2usecs(iter->ts);
596 usec_rem = do_div(t, USEC_PER_SEC);
597 secs = (unsigned long)t;
598 trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
599 } else
600 trace_seq_printf(s, " %12llu: ", iter->ts);
602 return !trace_seq_has_overflowed(s);
605 int trace_print_lat_context(struct trace_iterator *iter)
607 struct trace_array *tr = iter->tr;
608 /* trace_find_next_entry will reset ent_size */
609 int ent_size = iter->ent_size;
610 struct trace_seq *s = &iter->seq;
611 u64 next_ts;
612 struct trace_entry *entry = iter->ent,
613 *next_entry = trace_find_next_entry(iter, NULL,
614 &next_ts);
615 unsigned long verbose = (tr->trace_flags & TRACE_ITER_VERBOSE);
617 /* Restore the original ent_size */
618 iter->ent_size = ent_size;
620 if (!next_entry)
621 next_ts = iter->ts;
623 if (verbose) {
624 char comm[TASK_COMM_LEN];
626 trace_find_cmdline(entry->pid, comm);
628 trace_seq_printf(
629 s, "%16s %5d %3d %d %08x %08lx ",
630 comm, entry->pid, iter->cpu, entry->flags,
631 entry->preempt_count, iter->idx);
632 } else {
633 lat_print_generic(s, entry, iter->cpu);
636 lat_print_timestamp(iter, next_ts);
638 return !trace_seq_has_overflowed(s);
641 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
643 static int task_state_char(unsigned long state)
645 int bit = state ? __ffs(state) + 1 : 0;
647 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
651 * ftrace_find_event - find a registered event
652 * @type: the type of event to look for
654 * Returns an event of type @type otherwise NULL
655 * Called with trace_event_read_lock() held.
657 struct trace_event *ftrace_find_event(int type)
659 struct trace_event *event;
660 unsigned key;
662 key = type & (EVENT_HASHSIZE - 1);
664 hlist_for_each_entry(event, &event_hash[key], node) {
665 if (event->type == type)
666 return event;
669 return NULL;
672 static LIST_HEAD(ftrace_event_list);
674 static int trace_search_list(struct list_head **list)
676 struct trace_event *e;
677 int last = __TRACE_LAST_TYPE;
679 if (list_empty(&ftrace_event_list)) {
680 *list = &ftrace_event_list;
681 return last + 1;
685 * We used up all possible max events,
686 * lets see if somebody freed one.
688 list_for_each_entry(e, &ftrace_event_list, list) {
689 if (e->type != last + 1)
690 break;
691 last++;
694 /* Did we used up all 65 thousand events??? */
695 if ((last + 1) > TRACE_EVENT_TYPE_MAX)
696 return 0;
698 *list = &e->list;
699 return last + 1;
702 void trace_event_read_lock(void)
704 down_read(&trace_event_sem);
707 void trace_event_read_unlock(void)
709 up_read(&trace_event_sem);
713 * register_trace_event - register output for an event type
714 * @event: the event type to register
716 * Event types are stored in a hash and this hash is used to
717 * find a way to print an event. If the @event->type is set
718 * then it will use that type, otherwise it will assign a
719 * type to use.
721 * If you assign your own type, please make sure it is added
722 * to the trace_type enum in trace.h, to avoid collisions
723 * with the dynamic types.
725 * Returns the event type number or zero on error.
727 int register_trace_event(struct trace_event *event)
729 unsigned key;
730 int ret = 0;
732 down_write(&trace_event_sem);
734 if (WARN_ON(!event))
735 goto out;
737 if (WARN_ON(!event->funcs))
738 goto out;
740 INIT_LIST_HEAD(&event->list);
742 if (!event->type) {
743 struct list_head *list = NULL;
745 if (next_event_type > TRACE_EVENT_TYPE_MAX) {
747 event->type = trace_search_list(&list);
748 if (!event->type)
749 goto out;
751 } else {
753 event->type = next_event_type++;
754 list = &ftrace_event_list;
757 if (WARN_ON(ftrace_find_event(event->type)))
758 goto out;
760 list_add_tail(&event->list, list);
762 } else if (event->type > __TRACE_LAST_TYPE) {
763 printk(KERN_WARNING "Need to add type to trace.h\n");
764 WARN_ON(1);
765 goto out;
766 } else {
767 /* Is this event already used */
768 if (ftrace_find_event(event->type))
769 goto out;
772 if (event->funcs->trace == NULL)
773 event->funcs->trace = trace_nop_print;
774 if (event->funcs->raw == NULL)
775 event->funcs->raw = trace_nop_print;
776 if (event->funcs->hex == NULL)
777 event->funcs->hex = trace_nop_print;
778 if (event->funcs->binary == NULL)
779 event->funcs->binary = trace_nop_print;
781 key = event->type & (EVENT_HASHSIZE - 1);
783 hlist_add_head(&event->node, &event_hash[key]);
785 ret = event->type;
786 out:
787 up_write(&trace_event_sem);
789 return ret;
791 EXPORT_SYMBOL_GPL(register_trace_event);
794 * Used by module code with the trace_event_sem held for write.
796 int __unregister_trace_event(struct trace_event *event)
798 hlist_del(&event->node);
799 list_del(&event->list);
800 return 0;
804 * unregister_trace_event - remove a no longer used event
805 * @event: the event to remove
807 int unregister_trace_event(struct trace_event *event)
809 down_write(&trace_event_sem);
810 __unregister_trace_event(event);
811 up_write(&trace_event_sem);
813 return 0;
815 EXPORT_SYMBOL_GPL(unregister_trace_event);
818 * Standard events
821 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
822 struct trace_event *event)
824 trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type);
826 return trace_handle_return(&iter->seq);
829 /* TRACE_FN */
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 seq_print_ip_sym(s, field->ip, flags);
840 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
841 trace_seq_puts(s, " <-");
842 seq_print_ip_sym(s, field->parent_ip, flags);
845 trace_seq_putc(s, '\n');
847 return trace_handle_return(s);
850 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
851 struct trace_event *event)
853 struct ftrace_entry *field;
855 trace_assign_type(field, iter->ent);
857 trace_seq_printf(&iter->seq, "%lx %lx\n",
858 field->ip,
859 field->parent_ip);
861 return trace_handle_return(&iter->seq);
864 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
865 struct trace_event *event)
867 struct ftrace_entry *field;
868 struct trace_seq *s = &iter->seq;
870 trace_assign_type(field, iter->ent);
872 SEQ_PUT_HEX_FIELD(s, field->ip);
873 SEQ_PUT_HEX_FIELD(s, field->parent_ip);
875 return trace_handle_return(s);
878 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
879 struct trace_event *event)
881 struct ftrace_entry *field;
882 struct trace_seq *s = &iter->seq;
884 trace_assign_type(field, iter->ent);
886 SEQ_PUT_FIELD(s, field->ip);
887 SEQ_PUT_FIELD(s, field->parent_ip);
889 return trace_handle_return(s);
892 static struct trace_event_functions trace_fn_funcs = {
893 .trace = trace_fn_trace,
894 .raw = trace_fn_raw,
895 .hex = trace_fn_hex,
896 .binary = trace_fn_bin,
899 static struct trace_event trace_fn_event = {
900 .type = TRACE_FN,
901 .funcs = &trace_fn_funcs,
904 /* TRACE_CTX an TRACE_WAKE */
905 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
906 char *delim)
908 struct ctx_switch_entry *field;
909 char comm[TASK_COMM_LEN];
910 int S, T;
913 trace_assign_type(field, iter->ent);
915 T = task_state_char(field->next_state);
916 S = task_state_char(field->prev_state);
917 trace_find_cmdline(field->next_pid, comm);
918 trace_seq_printf(&iter->seq,
919 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
920 field->prev_pid,
921 field->prev_prio,
922 S, delim,
923 field->next_cpu,
924 field->next_pid,
925 field->next_prio,
926 T, comm);
928 return trace_handle_return(&iter->seq);
931 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
932 struct trace_event *event)
934 return trace_ctxwake_print(iter, "==>");
937 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
938 int flags, struct trace_event *event)
940 return trace_ctxwake_print(iter, " +");
943 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
945 struct ctx_switch_entry *field;
946 int T;
948 trace_assign_type(field, iter->ent);
950 if (!S)
951 S = task_state_char(field->prev_state);
952 T = task_state_char(field->next_state);
953 trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
954 field->prev_pid,
955 field->prev_prio,
957 field->next_cpu,
958 field->next_pid,
959 field->next_prio,
962 return trace_handle_return(&iter->seq);
965 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
966 struct trace_event *event)
968 return trace_ctxwake_raw(iter, 0);
971 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
972 struct trace_event *event)
974 return trace_ctxwake_raw(iter, '+');
978 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
980 struct ctx_switch_entry *field;
981 struct trace_seq *s = &iter->seq;
982 int T;
984 trace_assign_type(field, iter->ent);
986 if (!S)
987 S = task_state_char(field->prev_state);
988 T = task_state_char(field->next_state);
990 SEQ_PUT_HEX_FIELD(s, field->prev_pid);
991 SEQ_PUT_HEX_FIELD(s, field->prev_prio);
992 SEQ_PUT_HEX_FIELD(s, S);
993 SEQ_PUT_HEX_FIELD(s, field->next_cpu);
994 SEQ_PUT_HEX_FIELD(s, field->next_pid);
995 SEQ_PUT_HEX_FIELD(s, field->next_prio);
996 SEQ_PUT_HEX_FIELD(s, T);
998 return trace_handle_return(s);
1001 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1002 struct trace_event *event)
1004 return trace_ctxwake_hex(iter, 0);
1007 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1008 struct trace_event *event)
1010 return trace_ctxwake_hex(iter, '+');
1013 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1014 int flags, struct trace_event *event)
1016 struct ctx_switch_entry *field;
1017 struct trace_seq *s = &iter->seq;
1019 trace_assign_type(field, iter->ent);
1021 SEQ_PUT_FIELD(s, field->prev_pid);
1022 SEQ_PUT_FIELD(s, field->prev_prio);
1023 SEQ_PUT_FIELD(s, field->prev_state);
1024 SEQ_PUT_FIELD(s, field->next_cpu);
1025 SEQ_PUT_FIELD(s, field->next_pid);
1026 SEQ_PUT_FIELD(s, field->next_prio);
1027 SEQ_PUT_FIELD(s, field->next_state);
1029 return trace_handle_return(s);
1032 static struct trace_event_functions trace_ctx_funcs = {
1033 .trace = trace_ctx_print,
1034 .raw = trace_ctx_raw,
1035 .hex = trace_ctx_hex,
1036 .binary = trace_ctxwake_bin,
1039 static struct trace_event trace_ctx_event = {
1040 .type = TRACE_CTX,
1041 .funcs = &trace_ctx_funcs,
1044 static struct trace_event_functions trace_wake_funcs = {
1045 .trace = trace_wake_print,
1046 .raw = trace_wake_raw,
1047 .hex = trace_wake_hex,
1048 .binary = trace_ctxwake_bin,
1051 static struct trace_event trace_wake_event = {
1052 .type = TRACE_WAKE,
1053 .funcs = &trace_wake_funcs,
1056 /* TRACE_STACK */
1058 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1059 int flags, struct trace_event *event)
1061 struct stack_entry *field;
1062 struct trace_seq *s = &iter->seq;
1063 unsigned long *p;
1064 unsigned long *end;
1066 trace_assign_type(field, iter->ent);
1067 end = (unsigned long *)((long)iter->ent + iter->ent_size);
1069 trace_seq_puts(s, "<stack trace>\n");
1071 for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
1073 if (trace_seq_has_overflowed(s))
1074 break;
1076 trace_seq_puts(s, " => ");
1077 seq_print_ip_sym(s, *p, flags);
1078 trace_seq_putc(s, '\n');
1081 return trace_handle_return(s);
1084 static struct trace_event_functions trace_stack_funcs = {
1085 .trace = trace_stack_print,
1088 static struct trace_event trace_stack_event = {
1089 .type = TRACE_STACK,
1090 .funcs = &trace_stack_funcs,
1093 /* TRACE_USER_STACK */
1094 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1095 int flags, struct trace_event *event)
1097 struct trace_array *tr = iter->tr;
1098 struct userstack_entry *field;
1099 struct trace_seq *s = &iter->seq;
1100 struct mm_struct *mm = NULL;
1101 unsigned int i;
1103 trace_assign_type(field, iter->ent);
1105 trace_seq_puts(s, "<user stack trace>\n");
1107 if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) {
1108 struct task_struct *task;
1110 * we do the lookup on the thread group leader,
1111 * since individual threads might have already quit!
1113 rcu_read_lock();
1114 task = find_task_by_vpid(field->tgid);
1115 if (task)
1116 mm = get_task_mm(task);
1117 rcu_read_unlock();
1120 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1121 unsigned long ip = field->caller[i];
1123 if (ip == ULONG_MAX || trace_seq_has_overflowed(s))
1124 break;
1126 trace_seq_puts(s, " => ");
1128 if (!ip) {
1129 trace_seq_puts(s, "??");
1130 trace_seq_putc(s, '\n');
1131 continue;
1134 seq_print_user_ip(s, mm, ip, flags);
1135 trace_seq_putc(s, '\n');
1138 if (mm)
1139 mmput(mm);
1141 return trace_handle_return(s);
1144 static struct trace_event_functions trace_user_stack_funcs = {
1145 .trace = trace_user_stack_print,
1148 static struct trace_event trace_user_stack_event = {
1149 .type = TRACE_USER_STACK,
1150 .funcs = &trace_user_stack_funcs,
1153 /* TRACE_HWLAT */
1154 static enum print_line_t
1155 trace_hwlat_print(struct trace_iterator *iter, int flags,
1156 struct trace_event *event)
1158 struct trace_entry *entry = iter->ent;
1159 struct trace_seq *s = &iter->seq;
1160 struct hwlat_entry *field;
1162 trace_assign_type(field, entry);
1164 trace_seq_printf(s, "#%-5u inner/outer(us): %4llu/%-5llu ts:%ld.%09ld",
1165 field->seqnum,
1166 field->duration,
1167 field->outer_duration,
1168 field->timestamp.tv_sec,
1169 field->timestamp.tv_nsec);
1171 if (field->nmi_count) {
1173 * The generic sched_clock() is not NMI safe, thus
1174 * we only record the count and not the time.
1176 if (!IS_ENABLED(CONFIG_GENERIC_SCHED_CLOCK))
1177 trace_seq_printf(s, " nmi-total:%llu",
1178 field->nmi_total_ts);
1179 trace_seq_printf(s, " nmi-count:%u",
1180 field->nmi_count);
1183 trace_seq_putc(s, '\n');
1185 return trace_handle_return(s);
1189 static enum print_line_t
1190 trace_hwlat_raw(struct trace_iterator *iter, int flags,
1191 struct trace_event *event)
1193 struct hwlat_entry *field;
1194 struct trace_seq *s = &iter->seq;
1196 trace_assign_type(field, iter->ent);
1198 trace_seq_printf(s, "%llu %lld %ld %09ld %u\n",
1199 field->duration,
1200 field->outer_duration,
1201 field->timestamp.tv_sec,
1202 field->timestamp.tv_nsec,
1203 field->seqnum);
1205 return trace_handle_return(s);
1208 static struct trace_event_functions trace_hwlat_funcs = {
1209 .trace = trace_hwlat_print,
1210 .raw = trace_hwlat_raw,
1213 static struct trace_event trace_hwlat_event = {
1214 .type = TRACE_HWLAT,
1215 .funcs = &trace_hwlat_funcs,
1218 /* TRACE_BPUTS */
1219 static enum print_line_t
1220 trace_bputs_print(struct trace_iterator *iter, int flags,
1221 struct trace_event *event)
1223 struct trace_entry *entry = iter->ent;
1224 struct trace_seq *s = &iter->seq;
1225 struct bputs_entry *field;
1227 trace_assign_type(field, entry);
1229 seq_print_ip_sym(s, field->ip, flags);
1230 trace_seq_puts(s, ": ");
1231 trace_seq_puts(s, field->str);
1233 return trace_handle_return(s);
1237 static enum print_line_t
1238 trace_bputs_raw(struct trace_iterator *iter, int flags,
1239 struct trace_event *event)
1241 struct bputs_entry *field;
1242 struct trace_seq *s = &iter->seq;
1244 trace_assign_type(field, iter->ent);
1246 trace_seq_printf(s, ": %lx : ", field->ip);
1247 trace_seq_puts(s, field->str);
1249 return trace_handle_return(s);
1252 static struct trace_event_functions trace_bputs_funcs = {
1253 .trace = trace_bputs_print,
1254 .raw = trace_bputs_raw,
1257 static struct trace_event trace_bputs_event = {
1258 .type = TRACE_BPUTS,
1259 .funcs = &trace_bputs_funcs,
1262 /* TRACE_BPRINT */
1263 static enum print_line_t
1264 trace_bprint_print(struct trace_iterator *iter, int flags,
1265 struct trace_event *event)
1267 struct trace_entry *entry = iter->ent;
1268 struct trace_seq *s = &iter->seq;
1269 struct bprint_entry *field;
1271 trace_assign_type(field, entry);
1273 seq_print_ip_sym(s, field->ip, flags);
1274 trace_seq_puts(s, ": ");
1275 trace_seq_bprintf(s, field->fmt, field->buf);
1277 return trace_handle_return(s);
1281 static enum print_line_t
1282 trace_bprint_raw(struct trace_iterator *iter, int flags,
1283 struct trace_event *event)
1285 struct bprint_entry *field;
1286 struct trace_seq *s = &iter->seq;
1288 trace_assign_type(field, iter->ent);
1290 trace_seq_printf(s, ": %lx : ", field->ip);
1291 trace_seq_bprintf(s, field->fmt, field->buf);
1293 return trace_handle_return(s);
1296 static struct trace_event_functions trace_bprint_funcs = {
1297 .trace = trace_bprint_print,
1298 .raw = trace_bprint_raw,
1301 static struct trace_event trace_bprint_event = {
1302 .type = TRACE_BPRINT,
1303 .funcs = &trace_bprint_funcs,
1306 /* TRACE_PRINT */
1307 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1308 int flags, struct trace_event *event)
1310 struct print_entry *field;
1311 struct trace_seq *s = &iter->seq;
1313 trace_assign_type(field, iter->ent);
1315 seq_print_ip_sym(s, field->ip, flags);
1316 trace_seq_printf(s, ": %s", field->buf);
1318 return trace_handle_return(s);
1321 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1322 struct trace_event *event)
1324 struct print_entry *field;
1326 trace_assign_type(field, iter->ent);
1328 trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf);
1330 return trace_handle_return(&iter->seq);
1333 static struct trace_event_functions trace_print_funcs = {
1334 .trace = trace_print_print,
1335 .raw = trace_print_raw,
1338 static struct trace_event trace_print_event = {
1339 .type = TRACE_PRINT,
1340 .funcs = &trace_print_funcs,
1343 static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags,
1344 struct trace_event *event)
1346 struct raw_data_entry *field;
1347 int i;
1349 trace_assign_type(field, iter->ent);
1351 trace_seq_printf(&iter->seq, "# %x buf:", field->id);
1353 for (i = 0; i < iter->ent_size - offsetof(struct raw_data_entry, buf); i++)
1354 trace_seq_printf(&iter->seq, " %02x",
1355 (unsigned char)field->buf[i]);
1357 trace_seq_putc(&iter->seq, '\n');
1359 return trace_handle_return(&iter->seq);
1362 static struct trace_event_functions trace_raw_data_funcs = {
1363 .trace = trace_raw_data,
1364 .raw = trace_raw_data,
1367 static struct trace_event trace_raw_data_event = {
1368 .type = TRACE_RAW_DATA,
1369 .funcs = &trace_raw_data_funcs,
1373 static struct trace_event *events[] __initdata = {
1374 &trace_fn_event,
1375 &trace_ctx_event,
1376 &trace_wake_event,
1377 &trace_stack_event,
1378 &trace_user_stack_event,
1379 &trace_bputs_event,
1380 &trace_bprint_event,
1381 &trace_print_event,
1382 &trace_hwlat_event,
1383 &trace_raw_data_event,
1384 NULL
1387 __init static int init_events(void)
1389 struct trace_event *event;
1390 int i, ret;
1392 for (i = 0; events[i]; i++) {
1393 event = events[i];
1395 ret = register_trace_event(event);
1396 if (!ret) {
1397 printk(KERN_WARNING "event %d failed to register\n",
1398 event->type);
1399 WARN_ON_ONCE(1);
1403 return 0;
1405 early_initcall(init_events);