1 /* 2 * trace_output.c 3 * 4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> 5 * 6 */ 7 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> 13 14 #include "trace_output.h" 15 16 /* must be a power of 2 */ 17 #define EVENT_HASHSIZE 128 18 19 DECLARE_RWSEM(trace_event_sem); 20 21 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly; 22 23 static int next_event_type = __TRACE_LAST_TYPE + 1; 24 25 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter) 26 { 27 struct trace_seq *s = &iter->seq; 28 struct trace_entry *entry = iter->ent; 29 struct bputs_entry *field; 30 31 trace_assign_type(field, entry); 32 33 trace_seq_puts(s, field->str); 34 35 return trace_handle_return(s); 36 } 37 38 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter) 39 { 40 struct trace_seq *s = &iter->seq; 41 struct trace_entry *entry = iter->ent; 42 struct bprint_entry *field; 43 44 trace_assign_type(field, entry); 45 46 trace_seq_bprintf(s, field->fmt, field->buf); 47 48 return trace_handle_return(s); 49 } 50 51 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter) 52 { 53 struct trace_seq *s = &iter->seq; 54 struct trace_entry *entry = iter->ent; 55 struct print_entry *field; 56 57 trace_assign_type(field, entry); 58 59 trace_seq_puts(s, field->buf); 60 61 return trace_handle_return(s); 62 } 63 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) 68 { 69 unsigned long mask; 70 const char *str; 71 const char *ret = trace_seq_buffer_ptr(p); 72 int i, first = 1; 73 74 for (i = 0; flag_array[i].name && flags; i++) { 75 76 mask = flag_array[i].mask; 77 if ((flags & mask) != mask) 78 continue; 79 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); 87 } 88 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); 94 } 95 96 trace_seq_putc(p, 0); 97 98 return ret; 99 } 100 EXPORT_SYMBOL(trace_print_flags_seq); 101 102 const char * 103 trace_print_symbols_seq(struct trace_seq *p, unsigned long val, 104 const struct trace_print_flags *symbol_array) 105 { 106 int i; 107 const char *ret = trace_seq_buffer_ptr(p); 108 109 for (i = 0; symbol_array[i].name; i++) { 110 111 if (val != symbol_array[i].mask) 112 continue; 113 114 trace_seq_puts(p, symbol_array[i].name); 115 break; 116 } 117 118 if (ret == (const char *)(trace_seq_buffer_ptr(p))) 119 trace_seq_printf(p, "0x%lx", val); 120 121 trace_seq_putc(p, 0); 122 123 return ret; 124 } 125 EXPORT_SYMBOL(trace_print_symbols_seq); 126 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) 132 { 133 unsigned long long mask; 134 const char *str; 135 const char *ret = trace_seq_buffer_ptr(p); 136 int i, first = 1; 137 138 for (i = 0; flag_array[i].name && flags; i++) { 139 140 mask = flag_array[i].mask; 141 if ((flags & mask) != mask) 142 continue; 143 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); 151 } 152 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); 158 } 159 160 trace_seq_putc(p, 0); 161 162 return ret; 163 } 164 EXPORT_SYMBOL(trace_print_flags_seq_u64); 165 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) 169 { 170 int i; 171 const char *ret = trace_seq_buffer_ptr(p); 172 173 for (i = 0; symbol_array[i].name; i++) { 174 175 if (val != symbol_array[i].mask) 176 continue; 177 178 trace_seq_puts(p, symbol_array[i].name); 179 break; 180 } 181 182 if (ret == (const char *)(trace_seq_buffer_ptr(p))) 183 trace_seq_printf(p, "0x%llx", val); 184 185 trace_seq_putc(p, 0); 186 187 return ret; 188 } 189 EXPORT_SYMBOL(trace_print_symbols_seq_u64); 190 #endif 191 192 const char * 193 trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr, 194 unsigned int bitmask_size) 195 { 196 const char *ret = trace_seq_buffer_ptr(p); 197 198 trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8); 199 trace_seq_putc(p, 0); 200 201 return ret; 202 } 203 EXPORT_SYMBOL_GPL(trace_print_bitmask_seq); 204 205 /** 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 211 * 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. 215 */ 216 const char * 217 trace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len, 218 bool concatenate) 219 { 220 int i; 221 const char *ret = trace_seq_buffer_ptr(p); 222 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); 227 228 return ret; 229 } 230 EXPORT_SYMBOL(trace_print_hex_seq); 231 232 const char * 233 trace_print_array_seq(struct trace_seq *p, const void *buf, int count, 234 size_t el_size) 235 { 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; 240 241 trace_seq_putc(p, '{'); 242 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; 265 } 266 prefix = ","; 267 ptr += el_size; 268 } 269 270 trace_seq_putc(p, '}'); 271 trace_seq_putc(p, 0); 272 273 return ret; 274 } 275 EXPORT_SYMBOL(trace_print_array_seq); 276 277 int trace_raw_output_prep(struct trace_iterator *iter, 278 struct trace_event *trace_event) 279 { 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; 284 285 event = container_of(trace_event, struct trace_event_call, event); 286 entry = iter->ent; 287 288 if (entry->type != event->event.type) { 289 WARN_ON_ONCE(1); 290 return TRACE_TYPE_UNHANDLED; 291 } 292 293 trace_seq_init(p); 294 trace_seq_printf(s, "%s: ", trace_event_name(event)); 295 296 return trace_handle_return(s); 297 } 298 EXPORT_SYMBOL(trace_raw_output_prep); 299 300 static int trace_output_raw(struct trace_iterator *iter, char *name, 301 char *fmt, va_list ap) 302 { 303 struct trace_seq *s = &iter->seq; 304 305 trace_seq_printf(s, "%s: ", name); 306 trace_seq_vprintf(s, fmt, ap); 307 308 return trace_handle_return(s); 309 } 310 311 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...) 312 { 313 va_list ap; 314 int ret; 315 316 va_start(ap, fmt); 317 ret = trace_output_raw(iter, name, fmt, ap); 318 va_end(ap); 319 320 return ret; 321 } 322 EXPORT_SYMBOL_GPL(trace_output_call); 323 324 #ifdef CONFIG_KRETPROBES 325 static inline const char *kretprobed(const char *name) 326 { 327 static const char tramp_name[] = "kretprobe_trampoline"; 328 int size = sizeof(tramp_name); 329 330 if (strncmp(tramp_name, name, size) == 0) 331 return "[unknown/kretprobe'd]"; 332 return name; 333 } 334 #else 335 static inline const char *kretprobed(const char *name) 336 { 337 return name; 338 } 339 #endif /* CONFIG_KRETPROBES */ 340 341 static void 342 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address) 343 { 344 #ifdef CONFIG_KALLSYMS 345 char str[KSYM_SYMBOL_LEN]; 346 const char *name; 347 348 kallsyms_lookup(address, NULL, NULL, NULL, str); 349 350 name = kretprobed(str); 351 352 trace_seq_printf(s, fmt, name); 353 #endif 354 } 355 356 static void 357 seq_print_sym_offset(struct trace_seq *s, const char *fmt, 358 unsigned long address) 359 { 360 #ifdef CONFIG_KALLSYMS 361 char str[KSYM_SYMBOL_LEN]; 362 const char *name; 363 364 sprint_symbol(str, address); 365 name = kretprobed(str); 366 367 trace_seq_printf(s, fmt, name); 368 #endif 369 } 370 371 #ifndef CONFIG_64BIT 372 # define IP_FMT "%08lx" 373 #else 374 # define IP_FMT "%016lx" 375 #endif 376 377 static int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm, 378 unsigned long ip, unsigned long sym_flags) 379 { 380 struct file *file = NULL; 381 unsigned long vmstart = 0; 382 int ret = 1; 383 384 if (s->full) 385 return 0; 386 387 if (mm) { 388 const struct vm_area_struct *vma; 389 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; 395 } 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); 401 } 402 up_read(&mm->mmap_sem); 403 } 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); 407 } 408 409 int 410 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags) 411 { 412 if (!ip) { 413 trace_seq_putc(s, '0'); 414 goto out; 415 } 416 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); 421 422 if (sym_flags & TRACE_ITER_SYM_ADDR) 423 trace_seq_printf(s, " <" IP_FMT ">", ip); 424 425 out: 426 return !trace_seq_has_overflowed(s); 427 } 428 429 /** 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 433 * 434 * Prints the generic fields of irqs off, in hard or softirq, preempt 435 * count. 436 */ 437 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry) 438 { 439 char hardsoft_irq; 440 char need_resched; 441 char irqs_off; 442 int hardirq; 443 int softirq; 444 int nmi; 445 446 nmi = entry->flags & TRACE_FLAG_NMI; 447 hardirq = entry->flags & TRACE_FLAG_HARDIRQ; 448 softirq = entry->flags & TRACE_FLAG_SOFTIRQ; 449 450 irqs_off = 451 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : 452 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : 453 '.'; 454 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; 469 } 470 471 hardsoft_irq = 472 (nmi && hardirq) ? 'Z' : 473 nmi ? 'z' : 474 (hardirq && softirq) ? 'H' : 475 hardirq ? 'h' : 476 softirq ? 's' : 477 '.' ; 478 479 trace_seq_printf(s, "%c%c%c", 480 irqs_off, need_resched, hardsoft_irq); 481 482 if (entry->preempt_count) 483 trace_seq_printf(s, "%x", entry->preempt_count); 484 else 485 trace_seq_putc(s, '.'); 486 487 return !trace_seq_has_overflowed(s); 488 } 489 490 static int 491 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu) 492 { 493 char comm[TASK_COMM_LEN]; 494 495 trace_find_cmdline(entry->pid, comm); 496 497 trace_seq_printf(s, "%8.8s-%-5d %3d", 498 comm, entry->pid, cpu); 499 500 return trace_print_lat_fmt(s, entry); 501 } 502 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 */ 516 }; 517 #undef MARK 518 519 char trace_find_mark(unsigned long long d) 520 { 521 int i; 522 int size = ARRAY_SIZE(mark); 523 524 for (i = 0; i < size; i++) { 525 if (d > mark[i].val) 526 break; 527 } 528 529 return (i == size) ? ' ' : mark[i].sym; 530 } 531 532 static int 533 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts) 534 { 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; 541 542 if (in_ns) { 543 abs_ts = ns2usecs(abs_ts); 544 rel_ts = ns2usecs(rel_ts); 545 } 546 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; 552 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); 558 559 } else if (verbose && !in_ns) { 560 trace_seq_printf( 561 s, "[%016llx] %lld (+%lld): ", 562 iter->ts, abs_ts, rel_ts); 563 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)); 569 570 } else { /* !verbose && !in_ns */ 571 trace_seq_printf(s, " %4lld: ", abs_ts); 572 } 573 574 return !trace_seq_has_overflowed(s); 575 } 576 577 int trace_print_context(struct trace_iterator *iter) 578 { 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]; 585 586 trace_find_cmdline(entry->pid, comm); 587 588 trace_seq_printf(s, "%16s-%-5d [%03d] ", 589 comm, entry->pid, iter->cpu); 590 591 if (tr->trace_flags & TRACE_ITER_IRQ_INFO) 592 trace_print_lat_fmt(s, entry); 593 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); 601 602 return !trace_seq_has_overflowed(s); 603 } 604 605 int trace_print_lat_context(struct trace_iterator *iter) 606 { 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); 616 617 /* Restore the original ent_size */ 618 iter->ent_size = ent_size; 619 620 if (!next_entry) 621 next_ts = iter->ts; 622 623 if (verbose) { 624 char comm[TASK_COMM_LEN]; 625 626 trace_find_cmdline(entry->pid, comm); 627 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); 634 } 635 636 lat_print_timestamp(iter, next_ts); 637 638 return !trace_seq_has_overflowed(s); 639 } 640 641 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR; 642 643 static int task_state_char(unsigned long state) 644 { 645 int bit = state ? __ffs(state) + 1 : 0; 646 647 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; 648 } 649 650 /** 651 * ftrace_find_event - find a registered event 652 * @type: the type of event to look for 653 * 654 * Returns an event of type @type otherwise NULL 655 * Called with trace_event_read_lock() held. 656 */ 657 struct trace_event *ftrace_find_event(int type) 658 { 659 struct trace_event *event; 660 unsigned key; 661 662 key = type & (EVENT_HASHSIZE - 1); 663 664 hlist_for_each_entry(event, &event_hash[key], node) { 665 if (event->type == type) 666 return event; 667 } 668 669 return NULL; 670 } 671 672 static LIST_HEAD(ftrace_event_list); 673 674 static int trace_search_list(struct list_head **list) 675 { 676 struct trace_event *e; 677 int last = __TRACE_LAST_TYPE; 678 679 if (list_empty(&ftrace_event_list)) { 680 *list = &ftrace_event_list; 681 return last + 1; 682 } 683 684 /* 685 * We used up all possible max events, 686 * lets see if somebody freed one. 687 */ 688 list_for_each_entry(e, &ftrace_event_list, list) { 689 if (e->type != last + 1) 690 break; 691 last++; 692 } 693 694 /* Did we used up all 65 thousand events??? */ 695 if ((last + 1) > TRACE_EVENT_TYPE_MAX) 696 return 0; 697 698 *list = &e->list; 699 return last + 1; 700 } 701 702 void trace_event_read_lock(void) 703 { 704 down_read(&trace_event_sem); 705 } 706 707 void trace_event_read_unlock(void) 708 { 709 up_read(&trace_event_sem); 710 } 711 712 /** 713 * register_trace_event - register output for an event type 714 * @event: the event type to register 715 * 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. 720 * 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. 724 * 725 * Returns the event type number or zero on error. 726 */ 727 int register_trace_event(struct trace_event *event) 728 { 729 unsigned key; 730 int ret = 0; 731 732 down_write(&trace_event_sem); 733 734 if (WARN_ON(!event)) 735 goto out; 736 737 if (WARN_ON(!event->funcs)) 738 goto out; 739 740 INIT_LIST_HEAD(&event->list); 741 742 if (!event->type) { 743 struct list_head *list = NULL; 744 745 if (next_event_type > TRACE_EVENT_TYPE_MAX) { 746 747 event->type = trace_search_list(&list); 748 if (!event->type) 749 goto out; 750 751 } else { 752 753 event->type = next_event_type++; 754 list = &ftrace_event_list; 755 } 756 757 if (WARN_ON(ftrace_find_event(event->type))) 758 goto out; 759 760 list_add_tail(&event->list, list); 761 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; 770 } 771 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; 780 781 key = event->type & (EVENT_HASHSIZE - 1); 782 783 hlist_add_head(&event->node, &event_hash[key]); 784 785 ret = event->type; 786 out: 787 up_write(&trace_event_sem); 788 789 return ret; 790 } 791 EXPORT_SYMBOL_GPL(register_trace_event); 792 793 /* 794 * Used by module code with the trace_event_sem held for write. 795 */ 796 int __unregister_trace_event(struct trace_event *event) 797 { 798 hlist_del(&event->node); 799 list_del(&event->list); 800 return 0; 801 } 802 803 /** 804 * unregister_trace_event - remove a no longer used event 805 * @event: the event to remove 806 */ 807 int unregister_trace_event(struct trace_event *event) 808 { 809 down_write(&trace_event_sem); 810 __unregister_trace_event(event); 811 up_write(&trace_event_sem); 812 813 return 0; 814 } 815 EXPORT_SYMBOL_GPL(unregister_trace_event); 816 817 /* 818 * Standard events 819 */ 820 821 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags, 822 struct trace_event *event) 823 { 824 trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type); 825 826 return trace_handle_return(&iter->seq); 827 } 828 829 /* TRACE_FN */ 830 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags, 831 struct trace_event *event) 832 { 833 struct ftrace_entry *field; 834 struct trace_seq *s = &iter->seq; 835 836 trace_assign_type(field, iter->ent); 837 838 seq_print_ip_sym(s, field->ip, flags); 839 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); 843 } 844 845 trace_seq_putc(s, '\n'); 846 847 return trace_handle_return(s); 848 } 849 850 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags, 851 struct trace_event *event) 852 { 853 struct ftrace_entry *field; 854 855 trace_assign_type(field, iter->ent); 856 857 trace_seq_printf(&iter->seq, "%lx %lx\n", 858 field->ip, 859 field->parent_ip); 860 861 return trace_handle_return(&iter->seq); 862 } 863 864 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags, 865 struct trace_event *event) 866 { 867 struct ftrace_entry *field; 868 struct trace_seq *s = &iter->seq; 869 870 trace_assign_type(field, iter->ent); 871 872 SEQ_PUT_HEX_FIELD(s, field->ip); 873 SEQ_PUT_HEX_FIELD(s, field->parent_ip); 874 875 return trace_handle_return(s); 876 } 877 878 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags, 879 struct trace_event *event) 880 { 881 struct ftrace_entry *field; 882 struct trace_seq *s = &iter->seq; 883 884 trace_assign_type(field, iter->ent); 885 886 SEQ_PUT_FIELD(s, field->ip); 887 SEQ_PUT_FIELD(s, field->parent_ip); 888 889 return trace_handle_return(s); 890 } 891 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, 897 }; 898 899 static struct trace_event trace_fn_event = { 900 .type = TRACE_FN, 901 .funcs = &trace_fn_funcs, 902 }; 903 904 /* TRACE_CTX an TRACE_WAKE */ 905 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter, 906 char *delim) 907 { 908 struct ctx_switch_entry *field; 909 char comm[TASK_COMM_LEN]; 910 int S, T; 911 912 913 trace_assign_type(field, iter->ent); 914 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); 927 928 return trace_handle_return(&iter->seq); 929 } 930 931 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags, 932 struct trace_event *event) 933 { 934 return trace_ctxwake_print(iter, "==>"); 935 } 936 937 static enum print_line_t trace_wake_print(struct trace_iterator *iter, 938 int flags, struct trace_event *event) 939 { 940 return trace_ctxwake_print(iter, " +"); 941 } 942 943 static int trace_ctxwake_raw(struct trace_iterator *iter, char S) 944 { 945 struct ctx_switch_entry *field; 946 int T; 947 948 trace_assign_type(field, iter->ent); 949 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, 956 S, 957 field->next_cpu, 958 field->next_pid, 959 field->next_prio, 960 T); 961 962 return trace_handle_return(&iter->seq); 963 } 964 965 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags, 966 struct trace_event *event) 967 { 968 return trace_ctxwake_raw(iter, 0); 969 } 970 971 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags, 972 struct trace_event *event) 973 { 974 return trace_ctxwake_raw(iter, '+'); 975 } 976 977 978 static int trace_ctxwake_hex(struct trace_iterator *iter, char S) 979 { 980 struct ctx_switch_entry *field; 981 struct trace_seq *s = &iter->seq; 982 int T; 983 984 trace_assign_type(field, iter->ent); 985 986 if (!S) 987 S = task_state_char(field->prev_state); 988 T = task_state_char(field->next_state); 989 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); 997 998 return trace_handle_return(s); 999 } 1000 1001 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags, 1002 struct trace_event *event) 1003 { 1004 return trace_ctxwake_hex(iter, 0); 1005 } 1006 1007 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags, 1008 struct trace_event *event) 1009 { 1010 return trace_ctxwake_hex(iter, '+'); 1011 } 1012 1013 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter, 1014 int flags, struct trace_event *event) 1015 { 1016 struct ctx_switch_entry *field; 1017 struct trace_seq *s = &iter->seq; 1018 1019 trace_assign_type(field, iter->ent); 1020 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); 1028 1029 return trace_handle_return(s); 1030 } 1031 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, 1037 }; 1038 1039 static struct trace_event trace_ctx_event = { 1040 .type = TRACE_CTX, 1041 .funcs = &trace_ctx_funcs, 1042 }; 1043 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, 1049 }; 1050 1051 static struct trace_event trace_wake_event = { 1052 .type = TRACE_WAKE, 1053 .funcs = &trace_wake_funcs, 1054 }; 1055 1056 /* TRACE_STACK */ 1057 1058 static enum print_line_t trace_stack_print(struct trace_iterator *iter, 1059 int flags, struct trace_event *event) 1060 { 1061 struct stack_entry *field; 1062 struct trace_seq *s = &iter->seq; 1063 unsigned long *p; 1064 unsigned long *end; 1065 1066 trace_assign_type(field, iter->ent); 1067 end = (unsigned long *)((long)iter->ent + iter->ent_size); 1068 1069 trace_seq_puts(s, "<stack trace>\n"); 1070 1071 for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) { 1072 1073 if (trace_seq_has_overflowed(s)) 1074 break; 1075 1076 trace_seq_puts(s, " => "); 1077 seq_print_ip_sym(s, *p, flags); 1078 trace_seq_putc(s, '\n'); 1079 } 1080 1081 return trace_handle_return(s); 1082 } 1083 1084 static struct trace_event_functions trace_stack_funcs = { 1085 .trace = trace_stack_print, 1086 }; 1087 1088 static struct trace_event trace_stack_event = { 1089 .type = TRACE_STACK, 1090 .funcs = &trace_stack_funcs, 1091 }; 1092 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) 1096 { 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; 1102 1103 trace_assign_type(field, iter->ent); 1104 1105 trace_seq_puts(s, "<user stack trace>\n"); 1106 1107 if (tr->trace_flags & TRACE_ITER_SYM_USEROBJ) { 1108 struct task_struct *task; 1109 /* 1110 * we do the lookup on the thread group leader, 1111 * since individual threads might have already quit! 1112 */ 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(); 1118 } 1119 1120 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { 1121 unsigned long ip = field->caller[i]; 1122 1123 if (ip == ULONG_MAX || trace_seq_has_overflowed(s)) 1124 break; 1125 1126 trace_seq_puts(s, " => "); 1127 1128 if (!ip) { 1129 trace_seq_puts(s, "??"); 1130 trace_seq_putc(s, '\n'); 1131 continue; 1132 } 1133 1134 seq_print_user_ip(s, mm, ip, flags); 1135 trace_seq_putc(s, '\n'); 1136 } 1137 1138 if (mm) 1139 mmput(mm); 1140 1141 return trace_handle_return(s); 1142 } 1143 1144 static struct trace_event_functions trace_user_stack_funcs = { 1145 .trace = trace_user_stack_print, 1146 }; 1147 1148 static struct trace_event trace_user_stack_event = { 1149 .type = TRACE_USER_STACK, 1150 .funcs = &trace_user_stack_funcs, 1151 }; 1152 1153 /* TRACE_HWLAT */ 1154 static enum print_line_t 1155 trace_hwlat_print(struct trace_iterator *iter, int flags, 1156 struct trace_event *event) 1157 { 1158 struct trace_entry *entry = iter->ent; 1159 struct trace_seq *s = &iter->seq; 1160 struct hwlat_entry *field; 1161 1162 trace_assign_type(field, entry); 1163 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); 1170 1171 if (field->nmi_count) { 1172 /* 1173 * The generic sched_clock() is not NMI safe, thus 1174 * we only record the count and not the time. 1175 */ 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); 1181 } 1182 1183 trace_seq_putc(s, '\n'); 1184 1185 return trace_handle_return(s); 1186 } 1187 1188 1189 static enum print_line_t 1190 trace_hwlat_raw(struct trace_iterator *iter, int flags, 1191 struct trace_event *event) 1192 { 1193 struct hwlat_entry *field; 1194 struct trace_seq *s = &iter->seq; 1195 1196 trace_assign_type(field, iter->ent); 1197 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); 1204 1205 return trace_handle_return(s); 1206 } 1207 1208 static struct trace_event_functions trace_hwlat_funcs = { 1209 .trace = trace_hwlat_print, 1210 .raw = trace_hwlat_raw, 1211 }; 1212 1213 static struct trace_event trace_hwlat_event = { 1214 .type = TRACE_HWLAT, 1215 .funcs = &trace_hwlat_funcs, 1216 }; 1217 1218 /* TRACE_BPUTS */ 1219 static enum print_line_t 1220 trace_bputs_print(struct trace_iterator *iter, int flags, 1221 struct trace_event *event) 1222 { 1223 struct trace_entry *entry = iter->ent; 1224 struct trace_seq *s = &iter->seq; 1225 struct bputs_entry *field; 1226 1227 trace_assign_type(field, entry); 1228 1229 seq_print_ip_sym(s, field->ip, flags); 1230 trace_seq_puts(s, ": "); 1231 trace_seq_puts(s, field->str); 1232 1233 return trace_handle_return(s); 1234 } 1235 1236 1237 static enum print_line_t 1238 trace_bputs_raw(struct trace_iterator *iter, int flags, 1239 struct trace_event *event) 1240 { 1241 struct bputs_entry *field; 1242 struct trace_seq *s = &iter->seq; 1243 1244 trace_assign_type(field, iter->ent); 1245 1246 trace_seq_printf(s, ": %lx : ", field->ip); 1247 trace_seq_puts(s, field->str); 1248 1249 return trace_handle_return(s); 1250 } 1251 1252 static struct trace_event_functions trace_bputs_funcs = { 1253 .trace = trace_bputs_print, 1254 .raw = trace_bputs_raw, 1255 }; 1256 1257 static struct trace_event trace_bputs_event = { 1258 .type = TRACE_BPUTS, 1259 .funcs = &trace_bputs_funcs, 1260 }; 1261 1262 /* TRACE_BPRINT */ 1263 static enum print_line_t 1264 trace_bprint_print(struct trace_iterator *iter, int flags, 1265 struct trace_event *event) 1266 { 1267 struct trace_entry *entry = iter->ent; 1268 struct trace_seq *s = &iter->seq; 1269 struct bprint_entry *field; 1270 1271 trace_assign_type(field, entry); 1272 1273 seq_print_ip_sym(s, field->ip, flags); 1274 trace_seq_puts(s, ": "); 1275 trace_seq_bprintf(s, field->fmt, field->buf); 1276 1277 return trace_handle_return(s); 1278 } 1279 1280 1281 static enum print_line_t 1282 trace_bprint_raw(struct trace_iterator *iter, int flags, 1283 struct trace_event *event) 1284 { 1285 struct bprint_entry *field; 1286 struct trace_seq *s = &iter->seq; 1287 1288 trace_assign_type(field, iter->ent); 1289 1290 trace_seq_printf(s, ": %lx : ", field->ip); 1291 trace_seq_bprintf(s, field->fmt, field->buf); 1292 1293 return trace_handle_return(s); 1294 } 1295 1296 static struct trace_event_functions trace_bprint_funcs = { 1297 .trace = trace_bprint_print, 1298 .raw = trace_bprint_raw, 1299 }; 1300 1301 static struct trace_event trace_bprint_event = { 1302 .type = TRACE_BPRINT, 1303 .funcs = &trace_bprint_funcs, 1304 }; 1305 1306 /* TRACE_PRINT */ 1307 static enum print_line_t trace_print_print(struct trace_iterator *iter, 1308 int flags, struct trace_event *event) 1309 { 1310 struct print_entry *field; 1311 struct trace_seq *s = &iter->seq; 1312 1313 trace_assign_type(field, iter->ent); 1314 1315 seq_print_ip_sym(s, field->ip, flags); 1316 trace_seq_printf(s, ": %s", field->buf); 1317 1318 return trace_handle_return(s); 1319 } 1320 1321 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags, 1322 struct trace_event *event) 1323 { 1324 struct print_entry *field; 1325 1326 trace_assign_type(field, iter->ent); 1327 1328 trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf); 1329 1330 return trace_handle_return(&iter->seq); 1331 } 1332 1333 static struct trace_event_functions trace_print_funcs = { 1334 .trace = trace_print_print, 1335 .raw = trace_print_raw, 1336 }; 1337 1338 static struct trace_event trace_print_event = { 1339 .type = TRACE_PRINT, 1340 .funcs = &trace_print_funcs, 1341 }; 1342 1343 static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags, 1344 struct trace_event *event) 1345 { 1346 struct raw_data_entry *field; 1347 int i; 1348 1349 trace_assign_type(field, iter->ent); 1350 1351 trace_seq_printf(&iter->seq, "# %x buf:", field->id); 1352 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]); 1356 1357 trace_seq_putc(&iter->seq, '\n'); 1358 1359 return trace_handle_return(&iter->seq); 1360 } 1361 1362 static struct trace_event_functions trace_raw_data_funcs = { 1363 .trace = trace_raw_data, 1364 .raw = trace_raw_data, 1365 }; 1366 1367 static struct trace_event trace_raw_data_event = { 1368 .type = TRACE_RAW_DATA, 1369 .funcs = &trace_raw_data_funcs, 1370 }; 1371 1372 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 1385 }; 1386 1387 __init static int init_events(void) 1388 { 1389 struct trace_event *event; 1390 int i, ret; 1391 1392 for (i = 0; events[i]; i++) { 1393 event = events[i]; 1394 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); 1400 } 1401 } 1402 1403 return 0; 1404 } 1405 early_initcall(init_events); 1406