1 /* 2 * ring buffer based function tracer 3 * 4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 6 * 7 * Originally taken from the RT patch by: 8 * Arnaldo Carvalho de Melo <acme@redhat.com> 9 * 10 * Based on code from the latency_tracer, that is: 11 * Copyright (C) 2004-2006 Ingo Molnar 12 * Copyright (C) 2004 William Lee Irwin III 13 */ 14 #include <linux/utsrelease.h> 15 #include <linux/kallsyms.h> 16 #include <linux/seq_file.h> 17 #include <linux/notifier.h> 18 #include <linux/debugfs.h> 19 #include <linux/pagemap.h> 20 #include <linux/hardirq.h> 21 #include <linux/linkage.h> 22 #include <linux/uaccess.h> 23 #include <linux/ftrace.h> 24 #include <linux/module.h> 25 #include <linux/percpu.h> 26 #include <linux/kdebug.h> 27 #include <linux/ctype.h> 28 #include <linux/init.h> 29 #include <linux/poll.h> 30 #include <linux/gfp.h> 31 #include <linux/fs.h> 32 #include <linux/kprobes.h> 33 #include <linux/writeback.h> 34 35 #include <linux/stacktrace.h> 36 #include <linux/ring_buffer.h> 37 #include <linux/irqflags.h> 38 39 #include "trace.h" 40 41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE) 42 43 unsigned long __read_mostly tracing_max_latency; 44 unsigned long __read_mostly tracing_thresh; 45 46 /* 47 * We need to change this state when a selftest is running. 48 * A selftest will lurk into the ring-buffer to count the 49 * entries inserted during the selftest although some concurrent 50 * insertions into the ring-buffer such as ftrace_printk could occurred 51 * at the same time, giving false positive or negative results. 52 */ 53 static bool __read_mostly tracing_selftest_running; 54 55 /* For tracers that don't implement custom flags */ 56 static struct tracer_opt dummy_tracer_opt[] = { 57 { } 58 }; 59 60 static struct tracer_flags dummy_tracer_flags = { 61 .val = 0, 62 .opts = dummy_tracer_opt 63 }; 64 65 static int dummy_set_flag(u32 old_flags, u32 bit, int set) 66 { 67 return 0; 68 } 69 70 /* 71 * Kill all tracing for good (never come back). 72 * It is initialized to 1 but will turn to zero if the initialization 73 * of the tracer is successful. But that is the only place that sets 74 * this back to zero. 75 */ 76 int tracing_disabled = 1; 77 78 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled); 79 80 static inline void ftrace_disable_cpu(void) 81 { 82 preempt_disable(); 83 local_inc(&__get_cpu_var(ftrace_cpu_disabled)); 84 } 85 86 static inline void ftrace_enable_cpu(void) 87 { 88 local_dec(&__get_cpu_var(ftrace_cpu_disabled)); 89 preempt_enable(); 90 } 91 92 static cpumask_var_t __read_mostly tracing_buffer_mask; 93 94 #define for_each_tracing_cpu(cpu) \ 95 for_each_cpu(cpu, tracing_buffer_mask) 96 97 /* 98 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops 99 * 100 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops 101 * is set, then ftrace_dump is called. This will output the contents 102 * of the ftrace buffers to the console. This is very useful for 103 * capturing traces that lead to crashes and outputing it to a 104 * serial console. 105 * 106 * It is default off, but you can enable it with either specifying 107 * "ftrace_dump_on_oops" in the kernel command line, or setting 108 * /proc/sys/kernel/ftrace_dump_on_oops to true. 109 */ 110 int ftrace_dump_on_oops; 111 112 static int tracing_set_tracer(char *buf); 113 114 static int __init set_ftrace(char *str) 115 { 116 tracing_set_tracer(str); 117 return 1; 118 } 119 __setup("ftrace", set_ftrace); 120 121 static int __init set_ftrace_dump_on_oops(char *str) 122 { 123 ftrace_dump_on_oops = 1; 124 return 1; 125 } 126 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); 127 128 long 129 ns2usecs(cycle_t nsec) 130 { 131 nsec += 500; 132 do_div(nsec, 1000); 133 return nsec; 134 } 135 136 cycle_t ftrace_now(int cpu) 137 { 138 u64 ts = ring_buffer_time_stamp(cpu); 139 ring_buffer_normalize_time_stamp(cpu, &ts); 140 return ts; 141 } 142 143 /* 144 * The global_trace is the descriptor that holds the tracing 145 * buffers for the live tracing. For each CPU, it contains 146 * a link list of pages that will store trace entries. The 147 * page descriptor of the pages in the memory is used to hold 148 * the link list by linking the lru item in the page descriptor 149 * to each of the pages in the buffer per CPU. 150 * 151 * For each active CPU there is a data field that holds the 152 * pages for the buffer for that CPU. Each CPU has the same number 153 * of pages allocated for its buffer. 154 */ 155 static struct trace_array global_trace; 156 157 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu); 158 159 /* 160 * The max_tr is used to snapshot the global_trace when a maximum 161 * latency is reached. Some tracers will use this to store a maximum 162 * trace while it continues examining live traces. 163 * 164 * The buffers for the max_tr are set up the same as the global_trace. 165 * When a snapshot is taken, the link list of the max_tr is swapped 166 * with the link list of the global_trace and the buffers are reset for 167 * the global_trace so the tracing can continue. 168 */ 169 static struct trace_array max_tr; 170 171 static DEFINE_PER_CPU(struct trace_array_cpu, max_data); 172 173 /* tracer_enabled is used to toggle activation of a tracer */ 174 static int tracer_enabled = 1; 175 176 /** 177 * tracing_is_enabled - return tracer_enabled status 178 * 179 * This function is used by other tracers to know the status 180 * of the tracer_enabled flag. Tracers may use this function 181 * to know if it should enable their features when starting 182 * up. See irqsoff tracer for an example (start_irqsoff_tracer). 183 */ 184 int tracing_is_enabled(void) 185 { 186 return tracer_enabled; 187 } 188 189 /* function tracing enabled */ 190 int ftrace_function_enabled; 191 192 /* 193 * trace_buf_size is the size in bytes that is allocated 194 * for a buffer. Note, the number of bytes is always rounded 195 * to page size. 196 * 197 * This number is purposely set to a low number of 16384. 198 * If the dump on oops happens, it will be much appreciated 199 * to not have to wait for all that output. Anyway this can be 200 * boot time and run time configurable. 201 */ 202 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ 203 204 static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; 205 206 /* trace_types holds a link list of available tracers. */ 207 static struct tracer *trace_types __read_mostly; 208 209 /* current_trace points to the tracer that is currently active */ 210 static struct tracer *current_trace __read_mostly; 211 212 /* 213 * max_tracer_type_len is used to simplify the allocating of 214 * buffers to read userspace tracer names. We keep track of 215 * the longest tracer name registered. 216 */ 217 static int max_tracer_type_len; 218 219 /* 220 * trace_types_lock is used to protect the trace_types list. 221 * This lock is also used to keep user access serialized. 222 * Accesses from userspace will grab this lock while userspace 223 * activities happen inside the kernel. 224 */ 225 static DEFINE_MUTEX(trace_types_lock); 226 227 /* trace_wait is a waitqueue for tasks blocked on trace_poll */ 228 static DECLARE_WAIT_QUEUE_HEAD(trace_wait); 229 230 /* trace_flags holds trace_options default values */ 231 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | 232 TRACE_ITER_ANNOTATE; 233 234 /** 235 * trace_wake_up - wake up tasks waiting for trace input 236 * 237 * Simply wakes up any task that is blocked on the trace_wait 238 * queue. These is used with trace_poll for tasks polling the trace. 239 */ 240 void trace_wake_up(void) 241 { 242 /* 243 * The runqueue_is_locked() can fail, but this is the best we 244 * have for now: 245 */ 246 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked()) 247 wake_up(&trace_wait); 248 } 249 250 static int __init set_buf_size(char *str) 251 { 252 unsigned long buf_size; 253 int ret; 254 255 if (!str) 256 return 0; 257 ret = strict_strtoul(str, 0, &buf_size); 258 /* nr_entries can not be zero */ 259 if (ret < 0 || buf_size == 0) 260 return 0; 261 trace_buf_size = buf_size; 262 return 1; 263 } 264 __setup("trace_buf_size=", set_buf_size); 265 266 unsigned long nsecs_to_usecs(unsigned long nsecs) 267 { 268 return nsecs / 1000; 269 } 270 271 /* These must match the bit postions in trace_iterator_flags */ 272 static const char *trace_options[] = { 273 "print-parent", 274 "sym-offset", 275 "sym-addr", 276 "verbose", 277 "raw", 278 "hex", 279 "bin", 280 "block", 281 "stacktrace", 282 "sched-tree", 283 "ftrace_printk", 284 "ftrace_preempt", 285 "branch", 286 "annotate", 287 "userstacktrace", 288 "sym-userobj", 289 "printk-msg-only", 290 NULL 291 }; 292 293 /* 294 * ftrace_max_lock is used to protect the swapping of buffers 295 * when taking a max snapshot. The buffers themselves are 296 * protected by per_cpu spinlocks. But the action of the swap 297 * needs its own lock. 298 * 299 * This is defined as a raw_spinlock_t in order to help 300 * with performance when lockdep debugging is enabled. 301 */ 302 static raw_spinlock_t ftrace_max_lock = 303 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; 304 305 /* 306 * Copy the new maximum trace into the separate maximum-trace 307 * structure. (this way the maximum trace is permanently saved, 308 * for later retrieval via /debugfs/tracing/latency_trace) 309 */ 310 static void 311 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) 312 { 313 struct trace_array_cpu *data = tr->data[cpu]; 314 315 max_tr.cpu = cpu; 316 max_tr.time_start = data->preempt_timestamp; 317 318 data = max_tr.data[cpu]; 319 data->saved_latency = tracing_max_latency; 320 321 memcpy(data->comm, tsk->comm, TASK_COMM_LEN); 322 data->pid = tsk->pid; 323 data->uid = task_uid(tsk); 324 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; 325 data->policy = tsk->policy; 326 data->rt_priority = tsk->rt_priority; 327 328 /* record this tasks comm */ 329 tracing_record_cmdline(current); 330 } 331 332 /** 333 * trace_seq_printf - sequence printing of trace information 334 * @s: trace sequence descriptor 335 * @fmt: printf format string 336 * 337 * The tracer may use either sequence operations or its own 338 * copy to user routines. To simplify formating of a trace 339 * trace_seq_printf is used to store strings into a special 340 * buffer (@s). Then the output may be either used by 341 * the sequencer or pulled into another buffer. 342 */ 343 int 344 trace_seq_printf(struct trace_seq *s, const char *fmt, ...) 345 { 346 int len = (PAGE_SIZE - 1) - s->len; 347 va_list ap; 348 int ret; 349 350 if (!len) 351 return 0; 352 353 va_start(ap, fmt); 354 ret = vsnprintf(s->buffer + s->len, len, fmt, ap); 355 va_end(ap); 356 357 /* If we can't write it all, don't bother writing anything */ 358 if (ret >= len) 359 return 0; 360 361 s->len += ret; 362 363 return len; 364 } 365 366 /** 367 * trace_seq_puts - trace sequence printing of simple string 368 * @s: trace sequence descriptor 369 * @str: simple string to record 370 * 371 * The tracer may use either the sequence operations or its own 372 * copy to user routines. This function records a simple string 373 * into a special buffer (@s) for later retrieval by a sequencer 374 * or other mechanism. 375 */ 376 static int 377 trace_seq_puts(struct trace_seq *s, const char *str) 378 { 379 int len = strlen(str); 380 381 if (len > ((PAGE_SIZE - 1) - s->len)) 382 return 0; 383 384 memcpy(s->buffer + s->len, str, len); 385 s->len += len; 386 387 return len; 388 } 389 390 static int 391 trace_seq_putc(struct trace_seq *s, unsigned char c) 392 { 393 if (s->len >= (PAGE_SIZE - 1)) 394 return 0; 395 396 s->buffer[s->len++] = c; 397 398 return 1; 399 } 400 401 static int 402 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len) 403 { 404 if (len > ((PAGE_SIZE - 1) - s->len)) 405 return 0; 406 407 memcpy(s->buffer + s->len, mem, len); 408 s->len += len; 409 410 return len; 411 } 412 413 #define MAX_MEMHEX_BYTES 8 414 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1) 415 416 static int 417 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len) 418 { 419 unsigned char hex[HEX_CHARS]; 420 unsigned char *data = mem; 421 int i, j; 422 423 #ifdef __BIG_ENDIAN 424 for (i = 0, j = 0; i < len; i++) { 425 #else 426 for (i = len-1, j = 0; i >= 0; i--) { 427 #endif 428 hex[j++] = hex_asc_hi(data[i]); 429 hex[j++] = hex_asc_lo(data[i]); 430 } 431 hex[j++] = ' '; 432 433 return trace_seq_putmem(s, hex, j); 434 } 435 436 static int 437 trace_seq_path(struct trace_seq *s, struct path *path) 438 { 439 unsigned char *p; 440 441 if (s->len >= (PAGE_SIZE - 1)) 442 return 0; 443 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len); 444 if (!IS_ERR(p)) { 445 p = mangle_path(s->buffer + s->len, p, "\n"); 446 if (p) { 447 s->len = p - s->buffer; 448 return 1; 449 } 450 } else { 451 s->buffer[s->len++] = '?'; 452 return 1; 453 } 454 455 return 0; 456 } 457 458 static void 459 trace_seq_reset(struct trace_seq *s) 460 { 461 s->len = 0; 462 s->readpos = 0; 463 } 464 465 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt) 466 { 467 int len; 468 int ret; 469 470 if (s->len <= s->readpos) 471 return -EBUSY; 472 473 len = s->len - s->readpos; 474 if (cnt > len) 475 cnt = len; 476 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); 477 if (ret) 478 return -EFAULT; 479 480 s->readpos += len; 481 return cnt; 482 } 483 484 static void 485 trace_print_seq(struct seq_file *m, struct trace_seq *s) 486 { 487 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len; 488 489 s->buffer[len] = 0; 490 seq_puts(m, s->buffer); 491 492 trace_seq_reset(s); 493 } 494 495 /** 496 * update_max_tr - snapshot all trace buffers from global_trace to max_tr 497 * @tr: tracer 498 * @tsk: the task with the latency 499 * @cpu: The cpu that initiated the trace. 500 * 501 * Flip the buffers between the @tr and the max_tr and record information 502 * about which task was the cause of this latency. 503 */ 504 void 505 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) 506 { 507 struct ring_buffer *buf = tr->buffer; 508 509 WARN_ON_ONCE(!irqs_disabled()); 510 __raw_spin_lock(&ftrace_max_lock); 511 512 tr->buffer = max_tr.buffer; 513 max_tr.buffer = buf; 514 515 ftrace_disable_cpu(); 516 ring_buffer_reset(tr->buffer); 517 ftrace_enable_cpu(); 518 519 __update_max_tr(tr, tsk, cpu); 520 __raw_spin_unlock(&ftrace_max_lock); 521 } 522 523 /** 524 * update_max_tr_single - only copy one trace over, and reset the rest 525 * @tr - tracer 526 * @tsk - task with the latency 527 * @cpu - the cpu of the buffer to copy. 528 * 529 * Flip the trace of a single CPU buffer between the @tr and the max_tr. 530 */ 531 void 532 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) 533 { 534 int ret; 535 536 WARN_ON_ONCE(!irqs_disabled()); 537 __raw_spin_lock(&ftrace_max_lock); 538 539 ftrace_disable_cpu(); 540 541 ring_buffer_reset(max_tr.buffer); 542 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu); 543 544 ftrace_enable_cpu(); 545 546 WARN_ON_ONCE(ret); 547 548 __update_max_tr(tr, tsk, cpu); 549 __raw_spin_unlock(&ftrace_max_lock); 550 } 551 552 /** 553 * register_tracer - register a tracer with the ftrace system. 554 * @type - the plugin for the tracer 555 * 556 * Register a new plugin tracer. 557 */ 558 int register_tracer(struct tracer *type) 559 { 560 struct tracer *t; 561 int len; 562 int ret = 0; 563 564 if (!type->name) { 565 pr_info("Tracer must have a name\n"); 566 return -1; 567 } 568 569 /* 570 * When this gets called we hold the BKL which means that 571 * preemption is disabled. Various trace selftests however 572 * need to disable and enable preemption for successful tests. 573 * So we drop the BKL here and grab it after the tests again. 574 */ 575 unlock_kernel(); 576 mutex_lock(&trace_types_lock); 577 578 tracing_selftest_running = true; 579 580 for (t = trace_types; t; t = t->next) { 581 if (strcmp(type->name, t->name) == 0) { 582 /* already found */ 583 pr_info("Trace %s already registered\n", 584 type->name); 585 ret = -1; 586 goto out; 587 } 588 } 589 590 if (!type->set_flag) 591 type->set_flag = &dummy_set_flag; 592 if (!type->flags) 593 type->flags = &dummy_tracer_flags; 594 else 595 if (!type->flags->opts) 596 type->flags->opts = dummy_tracer_opt; 597 598 #ifdef CONFIG_FTRACE_STARTUP_TEST 599 if (type->selftest) { 600 struct tracer *saved_tracer = current_trace; 601 struct trace_array *tr = &global_trace; 602 int i; 603 604 /* 605 * Run a selftest on this tracer. 606 * Here we reset the trace buffer, and set the current 607 * tracer to be this tracer. The tracer can then run some 608 * internal tracing to verify that everything is in order. 609 * If we fail, we do not register this tracer. 610 */ 611 for_each_tracing_cpu(i) 612 tracing_reset(tr, i); 613 614 current_trace = type; 615 /* the test is responsible for initializing and enabling */ 616 pr_info("Testing tracer %s: ", type->name); 617 ret = type->selftest(type, tr); 618 /* the test is responsible for resetting too */ 619 current_trace = saved_tracer; 620 if (ret) { 621 printk(KERN_CONT "FAILED!\n"); 622 goto out; 623 } 624 /* Only reset on passing, to avoid touching corrupted buffers */ 625 for_each_tracing_cpu(i) 626 tracing_reset(tr, i); 627 628 printk(KERN_CONT "PASSED\n"); 629 } 630 #endif 631 632 type->next = trace_types; 633 trace_types = type; 634 len = strlen(type->name); 635 if (len > max_tracer_type_len) 636 max_tracer_type_len = len; 637 638 out: 639 tracing_selftest_running = false; 640 mutex_unlock(&trace_types_lock); 641 lock_kernel(); 642 643 return ret; 644 } 645 646 void unregister_tracer(struct tracer *type) 647 { 648 struct tracer **t; 649 int len; 650 651 mutex_lock(&trace_types_lock); 652 for (t = &trace_types; *t; t = &(*t)->next) { 653 if (*t == type) 654 goto found; 655 } 656 pr_info("Trace %s not registered\n", type->name); 657 goto out; 658 659 found: 660 *t = (*t)->next; 661 if (strlen(type->name) != max_tracer_type_len) 662 goto out; 663 664 max_tracer_type_len = 0; 665 for (t = &trace_types; *t; t = &(*t)->next) { 666 len = strlen((*t)->name); 667 if (len > max_tracer_type_len) 668 max_tracer_type_len = len; 669 } 670 out: 671 mutex_unlock(&trace_types_lock); 672 } 673 674 void tracing_reset(struct trace_array *tr, int cpu) 675 { 676 ftrace_disable_cpu(); 677 ring_buffer_reset_cpu(tr->buffer, cpu); 678 ftrace_enable_cpu(); 679 } 680 681 void tracing_reset_online_cpus(struct trace_array *tr) 682 { 683 int cpu; 684 685 tr->time_start = ftrace_now(tr->cpu); 686 687 for_each_online_cpu(cpu) 688 tracing_reset(tr, cpu); 689 } 690 691 #define SAVED_CMDLINES 128 692 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; 693 static unsigned map_cmdline_to_pid[SAVED_CMDLINES]; 694 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN]; 695 static int cmdline_idx; 696 static DEFINE_SPINLOCK(trace_cmdline_lock); 697 698 /* temporary disable recording */ 699 atomic_t trace_record_cmdline_disabled __read_mostly; 700 701 static void trace_init_cmdlines(void) 702 { 703 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline)); 704 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid)); 705 cmdline_idx = 0; 706 } 707 708 static int trace_stop_count; 709 static DEFINE_SPINLOCK(tracing_start_lock); 710 711 /** 712 * ftrace_off_permanent - disable all ftrace code permanently 713 * 714 * This should only be called when a serious anomally has 715 * been detected. This will turn off the function tracing, 716 * ring buffers, and other tracing utilites. It takes no 717 * locks and can be called from any context. 718 */ 719 void ftrace_off_permanent(void) 720 { 721 tracing_disabled = 1; 722 ftrace_stop(); 723 tracing_off_permanent(); 724 } 725 726 /** 727 * tracing_start - quick start of the tracer 728 * 729 * If tracing is enabled but was stopped by tracing_stop, 730 * this will start the tracer back up. 731 */ 732 void tracing_start(void) 733 { 734 struct ring_buffer *buffer; 735 unsigned long flags; 736 737 if (tracing_disabled) 738 return; 739 740 spin_lock_irqsave(&tracing_start_lock, flags); 741 if (--trace_stop_count) 742 goto out; 743 744 if (trace_stop_count < 0) { 745 /* Someone screwed up their debugging */ 746 WARN_ON_ONCE(1); 747 trace_stop_count = 0; 748 goto out; 749 } 750 751 752 buffer = global_trace.buffer; 753 if (buffer) 754 ring_buffer_record_enable(buffer); 755 756 buffer = max_tr.buffer; 757 if (buffer) 758 ring_buffer_record_enable(buffer); 759 760 ftrace_start(); 761 out: 762 spin_unlock_irqrestore(&tracing_start_lock, flags); 763 } 764 765 /** 766 * tracing_stop - quick stop of the tracer 767 * 768 * Light weight way to stop tracing. Use in conjunction with 769 * tracing_start. 770 */ 771 void tracing_stop(void) 772 { 773 struct ring_buffer *buffer; 774 unsigned long flags; 775 776 ftrace_stop(); 777 spin_lock_irqsave(&tracing_start_lock, flags); 778 if (trace_stop_count++) 779 goto out; 780 781 buffer = global_trace.buffer; 782 if (buffer) 783 ring_buffer_record_disable(buffer); 784 785 buffer = max_tr.buffer; 786 if (buffer) 787 ring_buffer_record_disable(buffer); 788 789 out: 790 spin_unlock_irqrestore(&tracing_start_lock, flags); 791 } 792 793 void trace_stop_cmdline_recording(void); 794 795 static void trace_save_cmdline(struct task_struct *tsk) 796 { 797 unsigned map; 798 unsigned idx; 799 800 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) 801 return; 802 803 /* 804 * It's not the end of the world if we don't get 805 * the lock, but we also don't want to spin 806 * nor do we want to disable interrupts, 807 * so if we miss here, then better luck next time. 808 */ 809 if (!spin_trylock(&trace_cmdline_lock)) 810 return; 811 812 idx = map_pid_to_cmdline[tsk->pid]; 813 if (idx >= SAVED_CMDLINES) { 814 idx = (cmdline_idx + 1) % SAVED_CMDLINES; 815 816 map = map_cmdline_to_pid[idx]; 817 if (map <= PID_MAX_DEFAULT) 818 map_pid_to_cmdline[map] = (unsigned)-1; 819 820 map_pid_to_cmdline[tsk->pid] = idx; 821 822 cmdline_idx = idx; 823 } 824 825 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN); 826 827 spin_unlock(&trace_cmdline_lock); 828 } 829 830 char *trace_find_cmdline(int pid) 831 { 832 char *cmdline = "<...>"; 833 unsigned map; 834 835 if (!pid) 836 return "<idle>"; 837 838 if (pid > PID_MAX_DEFAULT) 839 goto out; 840 841 map = map_pid_to_cmdline[pid]; 842 if (map >= SAVED_CMDLINES) 843 goto out; 844 845 cmdline = saved_cmdlines[map]; 846 847 out: 848 return cmdline; 849 } 850 851 void tracing_record_cmdline(struct task_struct *tsk) 852 { 853 if (atomic_read(&trace_record_cmdline_disabled)) 854 return; 855 856 trace_save_cmdline(tsk); 857 } 858 859 void 860 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags, 861 int pc) 862 { 863 struct task_struct *tsk = current; 864 865 entry->preempt_count = pc & 0xff; 866 entry->pid = (tsk) ? tsk->pid : 0; 867 entry->tgid = (tsk) ? tsk->tgid : 0; 868 entry->flags = 869 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT 870 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | 871 #else 872 TRACE_FLAG_IRQS_NOSUPPORT | 873 #endif 874 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | 875 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | 876 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0); 877 } 878 879 void 880 trace_function(struct trace_array *tr, struct trace_array_cpu *data, 881 unsigned long ip, unsigned long parent_ip, unsigned long flags, 882 int pc) 883 { 884 struct ring_buffer_event *event; 885 struct ftrace_entry *entry; 886 unsigned long irq_flags; 887 888 /* If we are reading the ring buffer, don't trace */ 889 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled)))) 890 return; 891 892 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), 893 &irq_flags); 894 if (!event) 895 return; 896 entry = ring_buffer_event_data(event); 897 tracing_generic_entry_update(&entry->ent, flags, pc); 898 entry->ent.type = TRACE_FN; 899 entry->ip = ip; 900 entry->parent_ip = parent_ip; 901 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 902 } 903 904 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 905 static void __trace_graph_entry(struct trace_array *tr, 906 struct trace_array_cpu *data, 907 struct ftrace_graph_ent *trace, 908 unsigned long flags, 909 int pc) 910 { 911 struct ring_buffer_event *event; 912 struct ftrace_graph_ent_entry *entry; 913 unsigned long irq_flags; 914 915 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled)))) 916 return; 917 918 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry), 919 &irq_flags); 920 if (!event) 921 return; 922 entry = ring_buffer_event_data(event); 923 tracing_generic_entry_update(&entry->ent, flags, pc); 924 entry->ent.type = TRACE_GRAPH_ENT; 925 entry->graph_ent = *trace; 926 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags); 927 } 928 929 static void __trace_graph_return(struct trace_array *tr, 930 struct trace_array_cpu *data, 931 struct ftrace_graph_ret *trace, 932 unsigned long flags, 933 int pc) 934 { 935 struct ring_buffer_event *event; 936 struct ftrace_graph_ret_entry *entry; 937 unsigned long irq_flags; 938 939 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled)))) 940 return; 941 942 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry), 943 &irq_flags); 944 if (!event) 945 return; 946 entry = ring_buffer_event_data(event); 947 tracing_generic_entry_update(&entry->ent, flags, pc); 948 entry->ent.type = TRACE_GRAPH_RET; 949 entry->ret = *trace; 950 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags); 951 } 952 #endif 953 954 void 955 ftrace(struct trace_array *tr, struct trace_array_cpu *data, 956 unsigned long ip, unsigned long parent_ip, unsigned long flags, 957 int pc) 958 { 959 if (likely(!atomic_read(&data->disabled))) 960 trace_function(tr, data, ip, parent_ip, flags, pc); 961 } 962 963 static void ftrace_trace_stack(struct trace_array *tr, 964 struct trace_array_cpu *data, 965 unsigned long flags, 966 int skip, int pc) 967 { 968 #ifdef CONFIG_STACKTRACE 969 struct ring_buffer_event *event; 970 struct stack_entry *entry; 971 struct stack_trace trace; 972 unsigned long irq_flags; 973 974 if (!(trace_flags & TRACE_ITER_STACKTRACE)) 975 return; 976 977 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), 978 &irq_flags); 979 if (!event) 980 return; 981 entry = ring_buffer_event_data(event); 982 tracing_generic_entry_update(&entry->ent, flags, pc); 983 entry->ent.type = TRACE_STACK; 984 985 memset(&entry->caller, 0, sizeof(entry->caller)); 986 987 trace.nr_entries = 0; 988 trace.max_entries = FTRACE_STACK_ENTRIES; 989 trace.skip = skip; 990 trace.entries = entry->caller; 991 992 save_stack_trace(&trace); 993 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 994 #endif 995 } 996 997 void __trace_stack(struct trace_array *tr, 998 struct trace_array_cpu *data, 999 unsigned long flags, 1000 int skip) 1001 { 1002 ftrace_trace_stack(tr, data, flags, skip, preempt_count()); 1003 } 1004 1005 static void ftrace_trace_userstack(struct trace_array *tr, 1006 struct trace_array_cpu *data, 1007 unsigned long flags, int pc) 1008 { 1009 #ifdef CONFIG_STACKTRACE 1010 struct ring_buffer_event *event; 1011 struct userstack_entry *entry; 1012 struct stack_trace trace; 1013 unsigned long irq_flags; 1014 1015 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE)) 1016 return; 1017 1018 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), 1019 &irq_flags); 1020 if (!event) 1021 return; 1022 entry = ring_buffer_event_data(event); 1023 tracing_generic_entry_update(&entry->ent, flags, pc); 1024 entry->ent.type = TRACE_USER_STACK; 1025 1026 memset(&entry->caller, 0, sizeof(entry->caller)); 1027 1028 trace.nr_entries = 0; 1029 trace.max_entries = FTRACE_STACK_ENTRIES; 1030 trace.skip = 0; 1031 trace.entries = entry->caller; 1032 1033 save_stack_trace_user(&trace); 1034 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 1035 #endif 1036 } 1037 1038 void __trace_userstack(struct trace_array *tr, 1039 struct trace_array_cpu *data, 1040 unsigned long flags) 1041 { 1042 ftrace_trace_userstack(tr, data, flags, preempt_count()); 1043 } 1044 1045 static void 1046 ftrace_trace_special(void *__tr, void *__data, 1047 unsigned long arg1, unsigned long arg2, unsigned long arg3, 1048 int pc) 1049 { 1050 struct ring_buffer_event *event; 1051 struct trace_array_cpu *data = __data; 1052 struct trace_array *tr = __tr; 1053 struct special_entry *entry; 1054 unsigned long irq_flags; 1055 1056 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), 1057 &irq_flags); 1058 if (!event) 1059 return; 1060 entry = ring_buffer_event_data(event); 1061 tracing_generic_entry_update(&entry->ent, 0, pc); 1062 entry->ent.type = TRACE_SPECIAL; 1063 entry->arg1 = arg1; 1064 entry->arg2 = arg2; 1065 entry->arg3 = arg3; 1066 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 1067 ftrace_trace_stack(tr, data, irq_flags, 4, pc); 1068 ftrace_trace_userstack(tr, data, irq_flags, pc); 1069 1070 trace_wake_up(); 1071 } 1072 1073 void 1074 __trace_special(void *__tr, void *__data, 1075 unsigned long arg1, unsigned long arg2, unsigned long arg3) 1076 { 1077 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count()); 1078 } 1079 1080 void 1081 tracing_sched_switch_trace(struct trace_array *tr, 1082 struct trace_array_cpu *data, 1083 struct task_struct *prev, 1084 struct task_struct *next, 1085 unsigned long flags, int pc) 1086 { 1087 struct ring_buffer_event *event; 1088 struct ctx_switch_entry *entry; 1089 unsigned long irq_flags; 1090 1091 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), 1092 &irq_flags); 1093 if (!event) 1094 return; 1095 entry = ring_buffer_event_data(event); 1096 tracing_generic_entry_update(&entry->ent, flags, pc); 1097 entry->ent.type = TRACE_CTX; 1098 entry->prev_pid = prev->pid; 1099 entry->prev_prio = prev->prio; 1100 entry->prev_state = prev->state; 1101 entry->next_pid = next->pid; 1102 entry->next_prio = next->prio; 1103 entry->next_state = next->state; 1104 entry->next_cpu = task_cpu(next); 1105 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 1106 ftrace_trace_stack(tr, data, flags, 5, pc); 1107 ftrace_trace_userstack(tr, data, flags, pc); 1108 } 1109 1110 void 1111 tracing_sched_wakeup_trace(struct trace_array *tr, 1112 struct trace_array_cpu *data, 1113 struct task_struct *wakee, 1114 struct task_struct *curr, 1115 unsigned long flags, int pc) 1116 { 1117 struct ring_buffer_event *event; 1118 struct ctx_switch_entry *entry; 1119 unsigned long irq_flags; 1120 1121 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), 1122 &irq_flags); 1123 if (!event) 1124 return; 1125 entry = ring_buffer_event_data(event); 1126 tracing_generic_entry_update(&entry->ent, flags, pc); 1127 entry->ent.type = TRACE_WAKE; 1128 entry->prev_pid = curr->pid; 1129 entry->prev_prio = curr->prio; 1130 entry->prev_state = curr->state; 1131 entry->next_pid = wakee->pid; 1132 entry->next_prio = wakee->prio; 1133 entry->next_state = wakee->state; 1134 entry->next_cpu = task_cpu(wakee); 1135 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 1136 ftrace_trace_stack(tr, data, flags, 6, pc); 1137 ftrace_trace_userstack(tr, data, flags, pc); 1138 1139 trace_wake_up(); 1140 } 1141 1142 void 1143 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) 1144 { 1145 struct trace_array *tr = &global_trace; 1146 struct trace_array_cpu *data; 1147 unsigned long flags; 1148 int cpu; 1149 int pc; 1150 1151 if (tracing_disabled) 1152 return; 1153 1154 pc = preempt_count(); 1155 local_irq_save(flags); 1156 cpu = raw_smp_processor_id(); 1157 data = tr->data[cpu]; 1158 1159 if (likely(atomic_inc_return(&data->disabled) == 1)) 1160 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc); 1161 1162 atomic_dec(&data->disabled); 1163 local_irq_restore(flags); 1164 } 1165 1166 #ifdef CONFIG_FUNCTION_TRACER 1167 static void 1168 function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip) 1169 { 1170 struct trace_array *tr = &global_trace; 1171 struct trace_array_cpu *data; 1172 unsigned long flags; 1173 long disabled; 1174 int cpu, resched; 1175 int pc; 1176 1177 if (unlikely(!ftrace_function_enabled)) 1178 return; 1179 1180 pc = preempt_count(); 1181 resched = ftrace_preempt_disable(); 1182 local_save_flags(flags); 1183 cpu = raw_smp_processor_id(); 1184 data = tr->data[cpu]; 1185 disabled = atomic_inc_return(&data->disabled); 1186 1187 if (likely(disabled == 1)) 1188 trace_function(tr, data, ip, parent_ip, flags, pc); 1189 1190 atomic_dec(&data->disabled); 1191 ftrace_preempt_enable(resched); 1192 } 1193 1194 static void 1195 function_trace_call(unsigned long ip, unsigned long parent_ip) 1196 { 1197 struct trace_array *tr = &global_trace; 1198 struct trace_array_cpu *data; 1199 unsigned long flags; 1200 long disabled; 1201 int cpu; 1202 int pc; 1203 1204 if (unlikely(!ftrace_function_enabled)) 1205 return; 1206 1207 /* 1208 * Need to use raw, since this must be called before the 1209 * recursive protection is performed. 1210 */ 1211 local_irq_save(flags); 1212 cpu = raw_smp_processor_id(); 1213 data = tr->data[cpu]; 1214 disabled = atomic_inc_return(&data->disabled); 1215 1216 if (likely(disabled == 1)) { 1217 pc = preempt_count(); 1218 trace_function(tr, data, ip, parent_ip, flags, pc); 1219 } 1220 1221 atomic_dec(&data->disabled); 1222 local_irq_restore(flags); 1223 } 1224 1225 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 1226 int trace_graph_entry(struct ftrace_graph_ent *trace) 1227 { 1228 struct trace_array *tr = &global_trace; 1229 struct trace_array_cpu *data; 1230 unsigned long flags; 1231 long disabled; 1232 int cpu; 1233 int pc; 1234 1235 if (!ftrace_trace_task(current)) 1236 return 0; 1237 1238 if (!ftrace_graph_addr(trace->func)) 1239 return 0; 1240 1241 local_irq_save(flags); 1242 cpu = raw_smp_processor_id(); 1243 data = tr->data[cpu]; 1244 disabled = atomic_inc_return(&data->disabled); 1245 if (likely(disabled == 1)) { 1246 pc = preempt_count(); 1247 __trace_graph_entry(tr, data, trace, flags, pc); 1248 } 1249 /* Only do the atomic if it is not already set */ 1250 if (!test_tsk_trace_graph(current)) 1251 set_tsk_trace_graph(current); 1252 atomic_dec(&data->disabled); 1253 local_irq_restore(flags); 1254 1255 return 1; 1256 } 1257 1258 void trace_graph_return(struct ftrace_graph_ret *trace) 1259 { 1260 struct trace_array *tr = &global_trace; 1261 struct trace_array_cpu *data; 1262 unsigned long flags; 1263 long disabled; 1264 int cpu; 1265 int pc; 1266 1267 local_irq_save(flags); 1268 cpu = raw_smp_processor_id(); 1269 data = tr->data[cpu]; 1270 disabled = atomic_inc_return(&data->disabled); 1271 if (likely(disabled == 1)) { 1272 pc = preempt_count(); 1273 __trace_graph_return(tr, data, trace, flags, pc); 1274 } 1275 if (!trace->depth) 1276 clear_tsk_trace_graph(current); 1277 atomic_dec(&data->disabled); 1278 local_irq_restore(flags); 1279 } 1280 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 1281 1282 static struct ftrace_ops trace_ops __read_mostly = 1283 { 1284 .func = function_trace_call, 1285 }; 1286 1287 void tracing_start_function_trace(void) 1288 { 1289 ftrace_function_enabled = 0; 1290 1291 if (trace_flags & TRACE_ITER_PREEMPTONLY) 1292 trace_ops.func = function_trace_call_preempt_only; 1293 else 1294 trace_ops.func = function_trace_call; 1295 1296 register_ftrace_function(&trace_ops); 1297 ftrace_function_enabled = 1; 1298 } 1299 1300 void tracing_stop_function_trace(void) 1301 { 1302 ftrace_function_enabled = 0; 1303 unregister_ftrace_function(&trace_ops); 1304 } 1305 #endif 1306 1307 enum trace_file_type { 1308 TRACE_FILE_LAT_FMT = 1, 1309 TRACE_FILE_ANNOTATE = 2, 1310 }; 1311 1312 static void trace_iterator_increment(struct trace_iterator *iter) 1313 { 1314 /* Don't allow ftrace to trace into the ring buffers */ 1315 ftrace_disable_cpu(); 1316 1317 iter->idx++; 1318 if (iter->buffer_iter[iter->cpu]) 1319 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL); 1320 1321 ftrace_enable_cpu(); 1322 } 1323 1324 static struct trace_entry * 1325 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts) 1326 { 1327 struct ring_buffer_event *event; 1328 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu]; 1329 1330 /* Don't allow ftrace to trace into the ring buffers */ 1331 ftrace_disable_cpu(); 1332 1333 if (buf_iter) 1334 event = ring_buffer_iter_peek(buf_iter, ts); 1335 else 1336 event = ring_buffer_peek(iter->tr->buffer, cpu, ts); 1337 1338 ftrace_enable_cpu(); 1339 1340 return event ? ring_buffer_event_data(event) : NULL; 1341 } 1342 1343 static struct trace_entry * 1344 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts) 1345 { 1346 struct ring_buffer *buffer = iter->tr->buffer; 1347 struct trace_entry *ent, *next = NULL; 1348 u64 next_ts = 0, ts; 1349 int next_cpu = -1; 1350 int cpu; 1351 1352 for_each_tracing_cpu(cpu) { 1353 1354 if (ring_buffer_empty_cpu(buffer, cpu)) 1355 continue; 1356 1357 ent = peek_next_entry(iter, cpu, &ts); 1358 1359 /* 1360 * Pick the entry with the smallest timestamp: 1361 */ 1362 if (ent && (!next || ts < next_ts)) { 1363 next = ent; 1364 next_cpu = cpu; 1365 next_ts = ts; 1366 } 1367 } 1368 1369 if (ent_cpu) 1370 *ent_cpu = next_cpu; 1371 1372 if (ent_ts) 1373 *ent_ts = next_ts; 1374 1375 return next; 1376 } 1377 1378 /* Find the next real entry, without updating the iterator itself */ 1379 static struct trace_entry * 1380 find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts) 1381 { 1382 return __find_next_entry(iter, ent_cpu, ent_ts); 1383 } 1384 1385 /* Find the next real entry, and increment the iterator to the next entry */ 1386 static void *find_next_entry_inc(struct trace_iterator *iter) 1387 { 1388 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts); 1389 1390 if (iter->ent) 1391 trace_iterator_increment(iter); 1392 1393 return iter->ent ? iter : NULL; 1394 } 1395 1396 static void trace_consume(struct trace_iterator *iter) 1397 { 1398 /* Don't allow ftrace to trace into the ring buffers */ 1399 ftrace_disable_cpu(); 1400 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts); 1401 ftrace_enable_cpu(); 1402 } 1403 1404 static void *s_next(struct seq_file *m, void *v, loff_t *pos) 1405 { 1406 struct trace_iterator *iter = m->private; 1407 int i = (int)*pos; 1408 void *ent; 1409 1410 (*pos)++; 1411 1412 /* can't go backwards */ 1413 if (iter->idx > i) 1414 return NULL; 1415 1416 if (iter->idx < 0) 1417 ent = find_next_entry_inc(iter); 1418 else 1419 ent = iter; 1420 1421 while (ent && iter->idx < i) 1422 ent = find_next_entry_inc(iter); 1423 1424 iter->pos = *pos; 1425 1426 return ent; 1427 } 1428 1429 static void *s_start(struct seq_file *m, loff_t *pos) 1430 { 1431 struct trace_iterator *iter = m->private; 1432 void *p = NULL; 1433 loff_t l = 0; 1434 int cpu; 1435 1436 mutex_lock(&trace_types_lock); 1437 1438 if (!current_trace || current_trace != iter->trace) { 1439 mutex_unlock(&trace_types_lock); 1440 return NULL; 1441 } 1442 1443 atomic_inc(&trace_record_cmdline_disabled); 1444 1445 if (*pos != iter->pos) { 1446 iter->ent = NULL; 1447 iter->cpu = 0; 1448 iter->idx = -1; 1449 1450 ftrace_disable_cpu(); 1451 1452 for_each_tracing_cpu(cpu) { 1453 ring_buffer_iter_reset(iter->buffer_iter[cpu]); 1454 } 1455 1456 ftrace_enable_cpu(); 1457 1458 for (p = iter; p && l < *pos; p = s_next(m, p, &l)) 1459 ; 1460 1461 } else { 1462 l = *pos - 1; 1463 p = s_next(m, p, &l); 1464 } 1465 1466 return p; 1467 } 1468 1469 static void s_stop(struct seq_file *m, void *p) 1470 { 1471 atomic_dec(&trace_record_cmdline_disabled); 1472 mutex_unlock(&trace_types_lock); 1473 } 1474 1475 #ifdef CONFIG_KRETPROBES 1476 static inline const char *kretprobed(const char *name) 1477 { 1478 static const char tramp_name[] = "kretprobe_trampoline"; 1479 int size = sizeof(tramp_name); 1480 1481 if (strncmp(tramp_name, name, size) == 0) 1482 return "[unknown/kretprobe'd]"; 1483 return name; 1484 } 1485 #else 1486 static inline const char *kretprobed(const char *name) 1487 { 1488 return name; 1489 } 1490 #endif /* CONFIG_KRETPROBES */ 1491 1492 static int 1493 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address) 1494 { 1495 #ifdef CONFIG_KALLSYMS 1496 char str[KSYM_SYMBOL_LEN]; 1497 const char *name; 1498 1499 kallsyms_lookup(address, NULL, NULL, NULL, str); 1500 1501 name = kretprobed(str); 1502 1503 return trace_seq_printf(s, fmt, name); 1504 #endif 1505 return 1; 1506 } 1507 1508 static int 1509 seq_print_sym_offset(struct trace_seq *s, const char *fmt, 1510 unsigned long address) 1511 { 1512 #ifdef CONFIG_KALLSYMS 1513 char str[KSYM_SYMBOL_LEN]; 1514 const char *name; 1515 1516 sprint_symbol(str, address); 1517 name = kretprobed(str); 1518 1519 return trace_seq_printf(s, fmt, name); 1520 #endif 1521 return 1; 1522 } 1523 1524 #ifndef CONFIG_64BIT 1525 # define IP_FMT "%08lx" 1526 #else 1527 # define IP_FMT "%016lx" 1528 #endif 1529 1530 int 1531 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags) 1532 { 1533 int ret; 1534 1535 if (!ip) 1536 return trace_seq_printf(s, "0"); 1537 1538 if (sym_flags & TRACE_ITER_SYM_OFFSET) 1539 ret = seq_print_sym_offset(s, "%s", ip); 1540 else 1541 ret = seq_print_sym_short(s, "%s", ip); 1542 1543 if (!ret) 1544 return 0; 1545 1546 if (sym_flags & TRACE_ITER_SYM_ADDR) 1547 ret = trace_seq_printf(s, " <" IP_FMT ">", ip); 1548 return ret; 1549 } 1550 1551 static inline int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm, 1552 unsigned long ip, unsigned long sym_flags) 1553 { 1554 struct file *file = NULL; 1555 unsigned long vmstart = 0; 1556 int ret = 1; 1557 1558 if (mm) { 1559 const struct vm_area_struct *vma; 1560 1561 down_read(&mm->mmap_sem); 1562 vma = find_vma(mm, ip); 1563 if (vma) { 1564 file = vma->vm_file; 1565 vmstart = vma->vm_start; 1566 } 1567 if (file) { 1568 ret = trace_seq_path(s, &file->f_path); 1569 if (ret) 1570 ret = trace_seq_printf(s, "[+0x%lx]", ip - vmstart); 1571 } 1572 up_read(&mm->mmap_sem); 1573 } 1574 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file)) 1575 ret = trace_seq_printf(s, " <" IP_FMT ">", ip); 1576 return ret; 1577 } 1578 1579 static int 1580 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s, 1581 unsigned long sym_flags) 1582 { 1583 struct mm_struct *mm = NULL; 1584 int ret = 1; 1585 unsigned int i; 1586 1587 if (trace_flags & TRACE_ITER_SYM_USEROBJ) { 1588 struct task_struct *task; 1589 /* 1590 * we do the lookup on the thread group leader, 1591 * since individual threads might have already quit! 1592 */ 1593 rcu_read_lock(); 1594 task = find_task_by_vpid(entry->ent.tgid); 1595 if (task) 1596 mm = get_task_mm(task); 1597 rcu_read_unlock(); 1598 } 1599 1600 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { 1601 unsigned long ip = entry->caller[i]; 1602 1603 if (ip == ULONG_MAX || !ret) 1604 break; 1605 if (i && ret) 1606 ret = trace_seq_puts(s, " <- "); 1607 if (!ip) { 1608 if (ret) 1609 ret = trace_seq_puts(s, "??"); 1610 continue; 1611 } 1612 if (!ret) 1613 break; 1614 if (ret) 1615 ret = seq_print_user_ip(s, mm, ip, sym_flags); 1616 } 1617 1618 if (mm) 1619 mmput(mm); 1620 return ret; 1621 } 1622 1623 static void print_lat_help_header(struct seq_file *m) 1624 { 1625 seq_puts(m, "# _------=> CPU# \n"); 1626 seq_puts(m, "# / _-----=> irqs-off \n"); 1627 seq_puts(m, "# | / _----=> need-resched \n"); 1628 seq_puts(m, "# || / _---=> hardirq/softirq \n"); 1629 seq_puts(m, "# ||| / _--=> preempt-depth \n"); 1630 seq_puts(m, "# |||| / \n"); 1631 seq_puts(m, "# ||||| delay \n"); 1632 seq_puts(m, "# cmd pid ||||| time | caller \n"); 1633 seq_puts(m, "# \\ / ||||| \\ | / \n"); 1634 } 1635 1636 static void print_func_help_header(struct seq_file *m) 1637 { 1638 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"); 1639 seq_puts(m, "# | | | | |\n"); 1640 } 1641 1642 1643 static void 1644 print_trace_header(struct seq_file *m, struct trace_iterator *iter) 1645 { 1646 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); 1647 struct trace_array *tr = iter->tr; 1648 struct trace_array_cpu *data = tr->data[tr->cpu]; 1649 struct tracer *type = current_trace; 1650 unsigned long total; 1651 unsigned long entries; 1652 const char *name = "preemption"; 1653 1654 if (type) 1655 name = type->name; 1656 1657 entries = ring_buffer_entries(iter->tr->buffer); 1658 total = entries + 1659 ring_buffer_overruns(iter->tr->buffer); 1660 1661 seq_printf(m, "%s latency trace v1.1.5 on %s\n", 1662 name, UTS_RELEASE); 1663 seq_puts(m, "-----------------------------------" 1664 "---------------------------------\n"); 1665 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |" 1666 " (M:%s VP:%d, KP:%d, SP:%d HP:%d", 1667 nsecs_to_usecs(data->saved_latency), 1668 entries, 1669 total, 1670 tr->cpu, 1671 #if defined(CONFIG_PREEMPT_NONE) 1672 "server", 1673 #elif defined(CONFIG_PREEMPT_VOLUNTARY) 1674 "desktop", 1675 #elif defined(CONFIG_PREEMPT) 1676 "preempt", 1677 #else 1678 "unknown", 1679 #endif 1680 /* These are reserved for later use */ 1681 0, 0, 0, 0); 1682 #ifdef CONFIG_SMP 1683 seq_printf(m, " #P:%d)\n", num_online_cpus()); 1684 #else 1685 seq_puts(m, ")\n"); 1686 #endif 1687 seq_puts(m, " -----------------\n"); 1688 seq_printf(m, " | task: %.16s-%d " 1689 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", 1690 data->comm, data->pid, data->uid, data->nice, 1691 data->policy, data->rt_priority); 1692 seq_puts(m, " -----------------\n"); 1693 1694 if (data->critical_start) { 1695 seq_puts(m, " => started at: "); 1696 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); 1697 trace_print_seq(m, &iter->seq); 1698 seq_puts(m, "\n => ended at: "); 1699 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); 1700 trace_print_seq(m, &iter->seq); 1701 seq_puts(m, "\n"); 1702 } 1703 1704 seq_puts(m, "\n"); 1705 } 1706 1707 static void 1708 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu) 1709 { 1710 int hardirq, softirq; 1711 char *comm; 1712 1713 comm = trace_find_cmdline(entry->pid); 1714 1715 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid); 1716 trace_seq_printf(s, "%3d", cpu); 1717 trace_seq_printf(s, "%c%c", 1718 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : 1719 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.', 1720 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.')); 1721 1722 hardirq = entry->flags & TRACE_FLAG_HARDIRQ; 1723 softirq = entry->flags & TRACE_FLAG_SOFTIRQ; 1724 if (hardirq && softirq) { 1725 trace_seq_putc(s, 'H'); 1726 } else { 1727 if (hardirq) { 1728 trace_seq_putc(s, 'h'); 1729 } else { 1730 if (softirq) 1731 trace_seq_putc(s, 's'); 1732 else 1733 trace_seq_putc(s, '.'); 1734 } 1735 } 1736 1737 if (entry->preempt_count) 1738 trace_seq_printf(s, "%x", entry->preempt_count); 1739 else 1740 trace_seq_puts(s, "."); 1741 } 1742 1743 unsigned long preempt_mark_thresh = 100; 1744 1745 static void 1746 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs, 1747 unsigned long rel_usecs) 1748 { 1749 trace_seq_printf(s, " %4lldus", abs_usecs); 1750 if (rel_usecs > preempt_mark_thresh) 1751 trace_seq_puts(s, "!: "); 1752 else if (rel_usecs > 1) 1753 trace_seq_puts(s, "+: "); 1754 else 1755 trace_seq_puts(s, " : "); 1756 } 1757 1758 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR; 1759 1760 static int task_state_char(unsigned long state) 1761 { 1762 int bit = state ? __ffs(state) + 1 : 0; 1763 1764 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?'; 1765 } 1766 1767 /* 1768 * The message is supposed to contain an ending newline. 1769 * If the printing stops prematurely, try to add a newline of our own. 1770 */ 1771 void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter) 1772 { 1773 struct trace_entry *ent; 1774 struct trace_field_cont *cont; 1775 bool ok = true; 1776 1777 ent = peek_next_entry(iter, iter->cpu, NULL); 1778 if (!ent || ent->type != TRACE_CONT) { 1779 trace_seq_putc(s, '\n'); 1780 return; 1781 } 1782 1783 do { 1784 cont = (struct trace_field_cont *)ent; 1785 if (ok) 1786 ok = (trace_seq_printf(s, "%s", cont->buf) > 0); 1787 1788 ftrace_disable_cpu(); 1789 1790 if (iter->buffer_iter[iter->cpu]) 1791 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL); 1792 else 1793 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL); 1794 1795 ftrace_enable_cpu(); 1796 1797 ent = peek_next_entry(iter, iter->cpu, NULL); 1798 } while (ent && ent->type == TRACE_CONT); 1799 1800 if (!ok) 1801 trace_seq_putc(s, '\n'); 1802 } 1803 1804 static void test_cpu_buff_start(struct trace_iterator *iter) 1805 { 1806 struct trace_seq *s = &iter->seq; 1807 1808 if (!(trace_flags & TRACE_ITER_ANNOTATE)) 1809 return; 1810 1811 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE)) 1812 return; 1813 1814 if (cpumask_test_cpu(iter->cpu, iter->started)) 1815 return; 1816 1817 cpumask_set_cpu(iter->cpu, iter->started); 1818 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu); 1819 } 1820 1821 static enum print_line_t 1822 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) 1823 { 1824 struct trace_seq *s = &iter->seq; 1825 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); 1826 struct trace_entry *next_entry; 1827 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE); 1828 struct trace_entry *entry = iter->ent; 1829 unsigned long abs_usecs; 1830 unsigned long rel_usecs; 1831 u64 next_ts; 1832 char *comm; 1833 int S, T; 1834 int i; 1835 1836 if (entry->type == TRACE_CONT) 1837 return TRACE_TYPE_HANDLED; 1838 1839 test_cpu_buff_start(iter); 1840 1841 next_entry = find_next_entry(iter, NULL, &next_ts); 1842 if (!next_entry) 1843 next_ts = iter->ts; 1844 rel_usecs = ns2usecs(next_ts - iter->ts); 1845 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start); 1846 1847 if (verbose) { 1848 comm = trace_find_cmdline(entry->pid); 1849 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]" 1850 " %ld.%03ldms (+%ld.%03ldms): ", 1851 comm, 1852 entry->pid, cpu, entry->flags, 1853 entry->preempt_count, trace_idx, 1854 ns2usecs(iter->ts), 1855 abs_usecs/1000, 1856 abs_usecs % 1000, rel_usecs/1000, 1857 rel_usecs % 1000); 1858 } else { 1859 lat_print_generic(s, entry, cpu); 1860 lat_print_timestamp(s, abs_usecs, rel_usecs); 1861 } 1862 switch (entry->type) { 1863 case TRACE_FN: { 1864 struct ftrace_entry *field; 1865 1866 trace_assign_type(field, entry); 1867 1868 seq_print_ip_sym(s, field->ip, sym_flags); 1869 trace_seq_puts(s, " ("); 1870 seq_print_ip_sym(s, field->parent_ip, sym_flags); 1871 trace_seq_puts(s, ")\n"); 1872 break; 1873 } 1874 case TRACE_CTX: 1875 case TRACE_WAKE: { 1876 struct ctx_switch_entry *field; 1877 1878 trace_assign_type(field, entry); 1879 1880 T = task_state_char(field->next_state); 1881 S = task_state_char(field->prev_state); 1882 comm = trace_find_cmdline(field->next_pid); 1883 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n", 1884 field->prev_pid, 1885 field->prev_prio, 1886 S, entry->type == TRACE_CTX ? "==>" : " +", 1887 field->next_cpu, 1888 field->next_pid, 1889 field->next_prio, 1890 T, comm); 1891 break; 1892 } 1893 case TRACE_SPECIAL: { 1894 struct special_entry *field; 1895 1896 trace_assign_type(field, entry); 1897 1898 trace_seq_printf(s, "# %ld %ld %ld\n", 1899 field->arg1, 1900 field->arg2, 1901 field->arg3); 1902 break; 1903 } 1904 case TRACE_STACK: { 1905 struct stack_entry *field; 1906 1907 trace_assign_type(field, entry); 1908 1909 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { 1910 if (i) 1911 trace_seq_puts(s, " <= "); 1912 seq_print_ip_sym(s, field->caller[i], sym_flags); 1913 } 1914 trace_seq_puts(s, "\n"); 1915 break; 1916 } 1917 case TRACE_PRINT: { 1918 struct print_entry *field; 1919 1920 trace_assign_type(field, entry); 1921 1922 seq_print_ip_sym(s, field->ip, sym_flags); 1923 trace_seq_printf(s, ": %s", field->buf); 1924 if (entry->flags & TRACE_FLAG_CONT) 1925 trace_seq_print_cont(s, iter); 1926 break; 1927 } 1928 case TRACE_BRANCH: { 1929 struct trace_branch *field; 1930 1931 trace_assign_type(field, entry); 1932 1933 trace_seq_printf(s, "[%s] %s:%s:%d\n", 1934 field->correct ? " ok " : " MISS ", 1935 field->func, 1936 field->file, 1937 field->line); 1938 break; 1939 } 1940 case TRACE_USER_STACK: { 1941 struct userstack_entry *field; 1942 1943 trace_assign_type(field, entry); 1944 1945 seq_print_userip_objs(field, s, sym_flags); 1946 trace_seq_putc(s, '\n'); 1947 break; 1948 } 1949 default: 1950 trace_seq_printf(s, "Unknown type %d\n", entry->type); 1951 } 1952 return TRACE_TYPE_HANDLED; 1953 } 1954 1955 static enum print_line_t print_trace_fmt(struct trace_iterator *iter) 1956 { 1957 struct trace_seq *s = &iter->seq; 1958 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); 1959 struct trace_entry *entry; 1960 unsigned long usec_rem; 1961 unsigned long long t; 1962 unsigned long secs; 1963 char *comm; 1964 int ret; 1965 int S, T; 1966 int i; 1967 1968 entry = iter->ent; 1969 1970 if (entry->type == TRACE_CONT) 1971 return TRACE_TYPE_HANDLED; 1972 1973 test_cpu_buff_start(iter); 1974 1975 comm = trace_find_cmdline(iter->ent->pid); 1976 1977 t = ns2usecs(iter->ts); 1978 usec_rem = do_div(t, 1000000ULL); 1979 secs = (unsigned long)t; 1980 1981 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid); 1982 if (!ret) 1983 return TRACE_TYPE_PARTIAL_LINE; 1984 ret = trace_seq_printf(s, "[%03d] ", iter->cpu); 1985 if (!ret) 1986 return TRACE_TYPE_PARTIAL_LINE; 1987 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem); 1988 if (!ret) 1989 return TRACE_TYPE_PARTIAL_LINE; 1990 1991 switch (entry->type) { 1992 case TRACE_FN: { 1993 struct ftrace_entry *field; 1994 1995 trace_assign_type(field, entry); 1996 1997 ret = seq_print_ip_sym(s, field->ip, sym_flags); 1998 if (!ret) 1999 return TRACE_TYPE_PARTIAL_LINE; 2000 if ((sym_flags & TRACE_ITER_PRINT_PARENT) && 2001 field->parent_ip) { 2002 ret = trace_seq_printf(s, " <-"); 2003 if (!ret) 2004 return TRACE_TYPE_PARTIAL_LINE; 2005 ret = seq_print_ip_sym(s, 2006 field->parent_ip, 2007 sym_flags); 2008 if (!ret) 2009 return TRACE_TYPE_PARTIAL_LINE; 2010 } 2011 ret = trace_seq_printf(s, "\n"); 2012 if (!ret) 2013 return TRACE_TYPE_PARTIAL_LINE; 2014 break; 2015 } 2016 case TRACE_CTX: 2017 case TRACE_WAKE: { 2018 struct ctx_switch_entry *field; 2019 2020 trace_assign_type(field, entry); 2021 2022 T = task_state_char(field->next_state); 2023 S = task_state_char(field->prev_state); 2024 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n", 2025 field->prev_pid, 2026 field->prev_prio, 2027 S, 2028 entry->type == TRACE_CTX ? "==>" : " +", 2029 field->next_cpu, 2030 field->next_pid, 2031 field->next_prio, 2032 T); 2033 if (!ret) 2034 return TRACE_TYPE_PARTIAL_LINE; 2035 break; 2036 } 2037 case TRACE_SPECIAL: { 2038 struct special_entry *field; 2039 2040 trace_assign_type(field, entry); 2041 2042 ret = trace_seq_printf(s, "# %ld %ld %ld\n", 2043 field->arg1, 2044 field->arg2, 2045 field->arg3); 2046 if (!ret) 2047 return TRACE_TYPE_PARTIAL_LINE; 2048 break; 2049 } 2050 case TRACE_STACK: { 2051 struct stack_entry *field; 2052 2053 trace_assign_type(field, entry); 2054 2055 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { 2056 if (i) { 2057 ret = trace_seq_puts(s, " <= "); 2058 if (!ret) 2059 return TRACE_TYPE_PARTIAL_LINE; 2060 } 2061 ret = seq_print_ip_sym(s, field->caller[i], 2062 sym_flags); 2063 if (!ret) 2064 return TRACE_TYPE_PARTIAL_LINE; 2065 } 2066 ret = trace_seq_puts(s, "\n"); 2067 if (!ret) 2068 return TRACE_TYPE_PARTIAL_LINE; 2069 break; 2070 } 2071 case TRACE_PRINT: { 2072 struct print_entry *field; 2073 2074 trace_assign_type(field, entry); 2075 2076 seq_print_ip_sym(s, field->ip, sym_flags); 2077 trace_seq_printf(s, ": %s", field->buf); 2078 if (entry->flags & TRACE_FLAG_CONT) 2079 trace_seq_print_cont(s, iter); 2080 break; 2081 } 2082 case TRACE_GRAPH_RET: { 2083 return print_graph_function(iter); 2084 } 2085 case TRACE_GRAPH_ENT: { 2086 return print_graph_function(iter); 2087 } 2088 case TRACE_BRANCH: { 2089 struct trace_branch *field; 2090 2091 trace_assign_type(field, entry); 2092 2093 trace_seq_printf(s, "[%s] %s:%s:%d\n", 2094 field->correct ? " ok " : " MISS ", 2095 field->func, 2096 field->file, 2097 field->line); 2098 break; 2099 } 2100 case TRACE_USER_STACK: { 2101 struct userstack_entry *field; 2102 2103 trace_assign_type(field, entry); 2104 2105 ret = seq_print_userip_objs(field, s, sym_flags); 2106 if (!ret) 2107 return TRACE_TYPE_PARTIAL_LINE; 2108 ret = trace_seq_putc(s, '\n'); 2109 if (!ret) 2110 return TRACE_TYPE_PARTIAL_LINE; 2111 break; 2112 } 2113 } 2114 return TRACE_TYPE_HANDLED; 2115 } 2116 2117 static enum print_line_t print_raw_fmt(struct trace_iterator *iter) 2118 { 2119 struct trace_seq *s = &iter->seq; 2120 struct trace_entry *entry; 2121 int ret; 2122 int S, T; 2123 2124 entry = iter->ent; 2125 2126 if (entry->type == TRACE_CONT) 2127 return TRACE_TYPE_HANDLED; 2128 2129 ret = trace_seq_printf(s, "%d %d %llu ", 2130 entry->pid, iter->cpu, iter->ts); 2131 if (!ret) 2132 return TRACE_TYPE_PARTIAL_LINE; 2133 2134 switch (entry->type) { 2135 case TRACE_FN: { 2136 struct ftrace_entry *field; 2137 2138 trace_assign_type(field, entry); 2139 2140 ret = trace_seq_printf(s, "%x %x\n", 2141 field->ip, 2142 field->parent_ip); 2143 if (!ret) 2144 return TRACE_TYPE_PARTIAL_LINE; 2145 break; 2146 } 2147 case TRACE_CTX: 2148 case TRACE_WAKE: { 2149 struct ctx_switch_entry *field; 2150 2151 trace_assign_type(field, entry); 2152 2153 T = task_state_char(field->next_state); 2154 S = entry->type == TRACE_WAKE ? '+' : 2155 task_state_char(field->prev_state); 2156 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n", 2157 field->prev_pid, 2158 field->prev_prio, 2159 S, 2160 field->next_cpu, 2161 field->next_pid, 2162 field->next_prio, 2163 T); 2164 if (!ret) 2165 return TRACE_TYPE_PARTIAL_LINE; 2166 break; 2167 } 2168 case TRACE_SPECIAL: 2169 case TRACE_USER_STACK: 2170 case TRACE_STACK: { 2171 struct special_entry *field; 2172 2173 trace_assign_type(field, entry); 2174 2175 ret = trace_seq_printf(s, "# %ld %ld %ld\n", 2176 field->arg1, 2177 field->arg2, 2178 field->arg3); 2179 if (!ret) 2180 return TRACE_TYPE_PARTIAL_LINE; 2181 break; 2182 } 2183 case TRACE_PRINT: { 2184 struct print_entry *field; 2185 2186 trace_assign_type(field, entry); 2187 2188 trace_seq_printf(s, "# %lx %s", field->ip, field->buf); 2189 if (entry->flags & TRACE_FLAG_CONT) 2190 trace_seq_print_cont(s, iter); 2191 break; 2192 } 2193 } 2194 return TRACE_TYPE_HANDLED; 2195 } 2196 2197 #define SEQ_PUT_FIELD_RET(s, x) \ 2198 do { \ 2199 if (!trace_seq_putmem(s, &(x), sizeof(x))) \ 2200 return 0; \ 2201 } while (0) 2202 2203 #define SEQ_PUT_HEX_FIELD_RET(s, x) \ 2204 do { \ 2205 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \ 2206 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \ 2207 return 0; \ 2208 } while (0) 2209 2210 static enum print_line_t print_hex_fmt(struct trace_iterator *iter) 2211 { 2212 struct trace_seq *s = &iter->seq; 2213 unsigned char newline = '\n'; 2214 struct trace_entry *entry; 2215 int S, T; 2216 2217 entry = iter->ent; 2218 2219 if (entry->type == TRACE_CONT) 2220 return TRACE_TYPE_HANDLED; 2221 2222 SEQ_PUT_HEX_FIELD_RET(s, entry->pid); 2223 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu); 2224 SEQ_PUT_HEX_FIELD_RET(s, iter->ts); 2225 2226 switch (entry->type) { 2227 case TRACE_FN: { 2228 struct ftrace_entry *field; 2229 2230 trace_assign_type(field, entry); 2231 2232 SEQ_PUT_HEX_FIELD_RET(s, field->ip); 2233 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip); 2234 break; 2235 } 2236 case TRACE_CTX: 2237 case TRACE_WAKE: { 2238 struct ctx_switch_entry *field; 2239 2240 trace_assign_type(field, entry); 2241 2242 T = task_state_char(field->next_state); 2243 S = entry->type == TRACE_WAKE ? '+' : 2244 task_state_char(field->prev_state); 2245 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid); 2246 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio); 2247 SEQ_PUT_HEX_FIELD_RET(s, S); 2248 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu); 2249 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid); 2250 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio); 2251 SEQ_PUT_HEX_FIELD_RET(s, T); 2252 break; 2253 } 2254 case TRACE_SPECIAL: 2255 case TRACE_USER_STACK: 2256 case TRACE_STACK: { 2257 struct special_entry *field; 2258 2259 trace_assign_type(field, entry); 2260 2261 SEQ_PUT_HEX_FIELD_RET(s, field->arg1); 2262 SEQ_PUT_HEX_FIELD_RET(s, field->arg2); 2263 SEQ_PUT_HEX_FIELD_RET(s, field->arg3); 2264 break; 2265 } 2266 } 2267 SEQ_PUT_FIELD_RET(s, newline); 2268 2269 return TRACE_TYPE_HANDLED; 2270 } 2271 2272 static enum print_line_t print_printk_msg_only(struct trace_iterator *iter) 2273 { 2274 struct trace_seq *s = &iter->seq; 2275 struct trace_entry *entry = iter->ent; 2276 struct print_entry *field; 2277 int ret; 2278 2279 trace_assign_type(field, entry); 2280 2281 ret = trace_seq_printf(s, field->buf); 2282 if (!ret) 2283 return TRACE_TYPE_PARTIAL_LINE; 2284 2285 if (entry->flags & TRACE_FLAG_CONT) 2286 trace_seq_print_cont(s, iter); 2287 2288 return TRACE_TYPE_HANDLED; 2289 } 2290 2291 static enum print_line_t print_bin_fmt(struct trace_iterator *iter) 2292 { 2293 struct trace_seq *s = &iter->seq; 2294 struct trace_entry *entry; 2295 2296 entry = iter->ent; 2297 2298 if (entry->type == TRACE_CONT) 2299 return TRACE_TYPE_HANDLED; 2300 2301 SEQ_PUT_FIELD_RET(s, entry->pid); 2302 SEQ_PUT_FIELD_RET(s, entry->cpu); 2303 SEQ_PUT_FIELD_RET(s, iter->ts); 2304 2305 switch (entry->type) { 2306 case TRACE_FN: { 2307 struct ftrace_entry *field; 2308 2309 trace_assign_type(field, entry); 2310 2311 SEQ_PUT_FIELD_RET(s, field->ip); 2312 SEQ_PUT_FIELD_RET(s, field->parent_ip); 2313 break; 2314 } 2315 case TRACE_CTX: { 2316 struct ctx_switch_entry *field; 2317 2318 trace_assign_type(field, entry); 2319 2320 SEQ_PUT_FIELD_RET(s, field->prev_pid); 2321 SEQ_PUT_FIELD_RET(s, field->prev_prio); 2322 SEQ_PUT_FIELD_RET(s, field->prev_state); 2323 SEQ_PUT_FIELD_RET(s, field->next_pid); 2324 SEQ_PUT_FIELD_RET(s, field->next_prio); 2325 SEQ_PUT_FIELD_RET(s, field->next_state); 2326 break; 2327 } 2328 case TRACE_SPECIAL: 2329 case TRACE_USER_STACK: 2330 case TRACE_STACK: { 2331 struct special_entry *field; 2332 2333 trace_assign_type(field, entry); 2334 2335 SEQ_PUT_FIELD_RET(s, field->arg1); 2336 SEQ_PUT_FIELD_RET(s, field->arg2); 2337 SEQ_PUT_FIELD_RET(s, field->arg3); 2338 break; 2339 } 2340 } 2341 return 1; 2342 } 2343 2344 static int trace_empty(struct trace_iterator *iter) 2345 { 2346 int cpu; 2347 2348 for_each_tracing_cpu(cpu) { 2349 if (iter->buffer_iter[cpu]) { 2350 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu])) 2351 return 0; 2352 } else { 2353 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu)) 2354 return 0; 2355 } 2356 } 2357 2358 return 1; 2359 } 2360 2361 static enum print_line_t print_trace_line(struct trace_iterator *iter) 2362 { 2363 enum print_line_t ret; 2364 2365 if (iter->trace && iter->trace->print_line) { 2366 ret = iter->trace->print_line(iter); 2367 if (ret != TRACE_TYPE_UNHANDLED) 2368 return ret; 2369 } 2370 2371 if (iter->ent->type == TRACE_PRINT && 2372 trace_flags & TRACE_ITER_PRINTK && 2373 trace_flags & TRACE_ITER_PRINTK_MSGONLY) 2374 return print_printk_msg_only(iter); 2375 2376 if (trace_flags & TRACE_ITER_BIN) 2377 return print_bin_fmt(iter); 2378 2379 if (trace_flags & TRACE_ITER_HEX) 2380 return print_hex_fmt(iter); 2381 2382 if (trace_flags & TRACE_ITER_RAW) 2383 return print_raw_fmt(iter); 2384 2385 if (iter->iter_flags & TRACE_FILE_LAT_FMT) 2386 return print_lat_fmt(iter, iter->idx, iter->cpu); 2387 2388 return print_trace_fmt(iter); 2389 } 2390 2391 static int s_show(struct seq_file *m, void *v) 2392 { 2393 struct trace_iterator *iter = v; 2394 2395 if (iter->ent == NULL) { 2396 if (iter->tr) { 2397 seq_printf(m, "# tracer: %s\n", iter->trace->name); 2398 seq_puts(m, "#\n"); 2399 } 2400 if (iter->trace && iter->trace->print_header) 2401 iter->trace->print_header(m); 2402 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) { 2403 /* print nothing if the buffers are empty */ 2404 if (trace_empty(iter)) 2405 return 0; 2406 print_trace_header(m, iter); 2407 if (!(trace_flags & TRACE_ITER_VERBOSE)) 2408 print_lat_help_header(m); 2409 } else { 2410 if (!(trace_flags & TRACE_ITER_VERBOSE)) 2411 print_func_help_header(m); 2412 } 2413 } else { 2414 print_trace_line(iter); 2415 trace_print_seq(m, &iter->seq); 2416 } 2417 2418 return 0; 2419 } 2420 2421 static struct seq_operations tracer_seq_ops = { 2422 .start = s_start, 2423 .next = s_next, 2424 .stop = s_stop, 2425 .show = s_show, 2426 }; 2427 2428 static struct trace_iterator * 2429 __tracing_open(struct inode *inode, struct file *file, int *ret) 2430 { 2431 struct trace_iterator *iter; 2432 struct seq_file *m; 2433 int cpu; 2434 2435 if (tracing_disabled) { 2436 *ret = -ENODEV; 2437 return NULL; 2438 } 2439 2440 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 2441 if (!iter) { 2442 *ret = -ENOMEM; 2443 goto out; 2444 } 2445 2446 mutex_lock(&trace_types_lock); 2447 if (current_trace && current_trace->print_max) 2448 iter->tr = &max_tr; 2449 else 2450 iter->tr = inode->i_private; 2451 iter->trace = current_trace; 2452 iter->pos = -1; 2453 2454 /* Notify the tracer early; before we stop tracing. */ 2455 if (iter->trace && iter->trace->open) 2456 iter->trace->open(iter); 2457 2458 /* Annotate start of buffers if we had overruns */ 2459 if (ring_buffer_overruns(iter->tr->buffer)) 2460 iter->iter_flags |= TRACE_FILE_ANNOTATE; 2461 2462 2463 for_each_tracing_cpu(cpu) { 2464 2465 iter->buffer_iter[cpu] = 2466 ring_buffer_read_start(iter->tr->buffer, cpu); 2467 2468 if (!iter->buffer_iter[cpu]) 2469 goto fail_buffer; 2470 } 2471 2472 /* TODO stop tracer */ 2473 *ret = seq_open(file, &tracer_seq_ops); 2474 if (*ret) 2475 goto fail_buffer; 2476 2477 m = file->private_data; 2478 m->private = iter; 2479 2480 /* stop the trace while dumping */ 2481 tracing_stop(); 2482 2483 mutex_unlock(&trace_types_lock); 2484 2485 out: 2486 return iter; 2487 2488 fail_buffer: 2489 for_each_tracing_cpu(cpu) { 2490 if (iter->buffer_iter[cpu]) 2491 ring_buffer_read_finish(iter->buffer_iter[cpu]); 2492 } 2493 mutex_unlock(&trace_types_lock); 2494 kfree(iter); 2495 2496 return ERR_PTR(-ENOMEM); 2497 } 2498 2499 int tracing_open_generic(struct inode *inode, struct file *filp) 2500 { 2501 if (tracing_disabled) 2502 return -ENODEV; 2503 2504 filp->private_data = inode->i_private; 2505 return 0; 2506 } 2507 2508 int tracing_release(struct inode *inode, struct file *file) 2509 { 2510 struct seq_file *m = (struct seq_file *)file->private_data; 2511 struct trace_iterator *iter = m->private; 2512 int cpu; 2513 2514 mutex_lock(&trace_types_lock); 2515 for_each_tracing_cpu(cpu) { 2516 if (iter->buffer_iter[cpu]) 2517 ring_buffer_read_finish(iter->buffer_iter[cpu]); 2518 } 2519 2520 if (iter->trace && iter->trace->close) 2521 iter->trace->close(iter); 2522 2523 /* reenable tracing if it was previously enabled */ 2524 tracing_start(); 2525 mutex_unlock(&trace_types_lock); 2526 2527 seq_release(inode, file); 2528 kfree(iter); 2529 return 0; 2530 } 2531 2532 static int tracing_open(struct inode *inode, struct file *file) 2533 { 2534 int ret; 2535 2536 __tracing_open(inode, file, &ret); 2537 2538 return ret; 2539 } 2540 2541 static int tracing_lt_open(struct inode *inode, struct file *file) 2542 { 2543 struct trace_iterator *iter; 2544 int ret; 2545 2546 iter = __tracing_open(inode, file, &ret); 2547 2548 if (!ret) 2549 iter->iter_flags |= TRACE_FILE_LAT_FMT; 2550 2551 return ret; 2552 } 2553 2554 2555 static void * 2556 t_next(struct seq_file *m, void *v, loff_t *pos) 2557 { 2558 struct tracer *t = m->private; 2559 2560 (*pos)++; 2561 2562 if (t) 2563 t = t->next; 2564 2565 m->private = t; 2566 2567 return t; 2568 } 2569 2570 static void *t_start(struct seq_file *m, loff_t *pos) 2571 { 2572 struct tracer *t = m->private; 2573 loff_t l = 0; 2574 2575 mutex_lock(&trace_types_lock); 2576 for (; t && l < *pos; t = t_next(m, t, &l)) 2577 ; 2578 2579 return t; 2580 } 2581 2582 static void t_stop(struct seq_file *m, void *p) 2583 { 2584 mutex_unlock(&trace_types_lock); 2585 } 2586 2587 static int t_show(struct seq_file *m, void *v) 2588 { 2589 struct tracer *t = v; 2590 2591 if (!t) 2592 return 0; 2593 2594 seq_printf(m, "%s", t->name); 2595 if (t->next) 2596 seq_putc(m, ' '); 2597 else 2598 seq_putc(m, '\n'); 2599 2600 return 0; 2601 } 2602 2603 static struct seq_operations show_traces_seq_ops = { 2604 .start = t_start, 2605 .next = t_next, 2606 .stop = t_stop, 2607 .show = t_show, 2608 }; 2609 2610 static int show_traces_open(struct inode *inode, struct file *file) 2611 { 2612 int ret; 2613 2614 if (tracing_disabled) 2615 return -ENODEV; 2616 2617 ret = seq_open(file, &show_traces_seq_ops); 2618 if (!ret) { 2619 struct seq_file *m = file->private_data; 2620 m->private = trace_types; 2621 } 2622 2623 return ret; 2624 } 2625 2626 static struct file_operations tracing_fops = { 2627 .open = tracing_open, 2628 .read = seq_read, 2629 .llseek = seq_lseek, 2630 .release = tracing_release, 2631 }; 2632 2633 static struct file_operations tracing_lt_fops = { 2634 .open = tracing_lt_open, 2635 .read = seq_read, 2636 .llseek = seq_lseek, 2637 .release = tracing_release, 2638 }; 2639 2640 static struct file_operations show_traces_fops = { 2641 .open = show_traces_open, 2642 .read = seq_read, 2643 .release = seq_release, 2644 }; 2645 2646 /* 2647 * Only trace on a CPU if the bitmask is set: 2648 */ 2649 static cpumask_var_t tracing_cpumask; 2650 2651 /* 2652 * The tracer itself will not take this lock, but still we want 2653 * to provide a consistent cpumask to user-space: 2654 */ 2655 static DEFINE_MUTEX(tracing_cpumask_update_lock); 2656 2657 /* 2658 * Temporary storage for the character representation of the 2659 * CPU bitmask (and one more byte for the newline): 2660 */ 2661 static char mask_str[NR_CPUS + 1]; 2662 2663 static ssize_t 2664 tracing_cpumask_read(struct file *filp, char __user *ubuf, 2665 size_t count, loff_t *ppos) 2666 { 2667 int len; 2668 2669 mutex_lock(&tracing_cpumask_update_lock); 2670 2671 len = cpumask_scnprintf(mask_str, count, tracing_cpumask); 2672 if (count - len < 2) { 2673 count = -EINVAL; 2674 goto out_err; 2675 } 2676 len += sprintf(mask_str + len, "\n"); 2677 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); 2678 2679 out_err: 2680 mutex_unlock(&tracing_cpumask_update_lock); 2681 2682 return count; 2683 } 2684 2685 static ssize_t 2686 tracing_cpumask_write(struct file *filp, const char __user *ubuf, 2687 size_t count, loff_t *ppos) 2688 { 2689 int err, cpu; 2690 cpumask_var_t tracing_cpumask_new; 2691 2692 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL)) 2693 return -ENOMEM; 2694 2695 mutex_lock(&tracing_cpumask_update_lock); 2696 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); 2697 if (err) 2698 goto err_unlock; 2699 2700 local_irq_disable(); 2701 __raw_spin_lock(&ftrace_max_lock); 2702 for_each_tracing_cpu(cpu) { 2703 /* 2704 * Increase/decrease the disabled counter if we are 2705 * about to flip a bit in the cpumask: 2706 */ 2707 if (cpumask_test_cpu(cpu, tracing_cpumask) && 2708 !cpumask_test_cpu(cpu, tracing_cpumask_new)) { 2709 atomic_inc(&global_trace.data[cpu]->disabled); 2710 } 2711 if (!cpumask_test_cpu(cpu, tracing_cpumask) && 2712 cpumask_test_cpu(cpu, tracing_cpumask_new)) { 2713 atomic_dec(&global_trace.data[cpu]->disabled); 2714 } 2715 } 2716 __raw_spin_unlock(&ftrace_max_lock); 2717 local_irq_enable(); 2718 2719 cpumask_copy(tracing_cpumask, tracing_cpumask_new); 2720 2721 mutex_unlock(&tracing_cpumask_update_lock); 2722 free_cpumask_var(tracing_cpumask_new); 2723 2724 return count; 2725 2726 err_unlock: 2727 mutex_unlock(&tracing_cpumask_update_lock); 2728 free_cpumask_var(tracing_cpumask); 2729 2730 return err; 2731 } 2732 2733 static struct file_operations tracing_cpumask_fops = { 2734 .open = tracing_open_generic, 2735 .read = tracing_cpumask_read, 2736 .write = tracing_cpumask_write, 2737 }; 2738 2739 static ssize_t 2740 tracing_trace_options_read(struct file *filp, char __user *ubuf, 2741 size_t cnt, loff_t *ppos) 2742 { 2743 int i; 2744 char *buf; 2745 int r = 0; 2746 int len = 0; 2747 u32 tracer_flags = current_trace->flags->val; 2748 struct tracer_opt *trace_opts = current_trace->flags->opts; 2749 2750 2751 /* calulate max size */ 2752 for (i = 0; trace_options[i]; i++) { 2753 len += strlen(trace_options[i]); 2754 len += 3; /* "no" and space */ 2755 } 2756 2757 /* 2758 * Increase the size with names of options specific 2759 * of the current tracer. 2760 */ 2761 for (i = 0; trace_opts[i].name; i++) { 2762 len += strlen(trace_opts[i].name); 2763 len += 3; /* "no" and space */ 2764 } 2765 2766 /* +2 for \n and \0 */ 2767 buf = kmalloc(len + 2, GFP_KERNEL); 2768 if (!buf) 2769 return -ENOMEM; 2770 2771 for (i = 0; trace_options[i]; i++) { 2772 if (trace_flags & (1 << i)) 2773 r += sprintf(buf + r, "%s ", trace_options[i]); 2774 else 2775 r += sprintf(buf + r, "no%s ", trace_options[i]); 2776 } 2777 2778 for (i = 0; trace_opts[i].name; i++) { 2779 if (tracer_flags & trace_opts[i].bit) 2780 r += sprintf(buf + r, "%s ", 2781 trace_opts[i].name); 2782 else 2783 r += sprintf(buf + r, "no%s ", 2784 trace_opts[i].name); 2785 } 2786 2787 r += sprintf(buf + r, "\n"); 2788 WARN_ON(r >= len + 2); 2789 2790 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 2791 2792 kfree(buf); 2793 2794 return r; 2795 } 2796 2797 /* Try to assign a tracer specific option */ 2798 static int set_tracer_option(struct tracer *trace, char *cmp, int neg) 2799 { 2800 struct tracer_flags *trace_flags = trace->flags; 2801 struct tracer_opt *opts = NULL; 2802 int ret = 0, i = 0; 2803 int len; 2804 2805 for (i = 0; trace_flags->opts[i].name; i++) { 2806 opts = &trace_flags->opts[i]; 2807 len = strlen(opts->name); 2808 2809 if (strncmp(cmp, opts->name, len) == 0) { 2810 ret = trace->set_flag(trace_flags->val, 2811 opts->bit, !neg); 2812 break; 2813 } 2814 } 2815 /* Not found */ 2816 if (!trace_flags->opts[i].name) 2817 return -EINVAL; 2818 2819 /* Refused to handle */ 2820 if (ret) 2821 return ret; 2822 2823 if (neg) 2824 trace_flags->val &= ~opts->bit; 2825 else 2826 trace_flags->val |= opts->bit; 2827 2828 return 0; 2829 } 2830 2831 static ssize_t 2832 tracing_trace_options_write(struct file *filp, const char __user *ubuf, 2833 size_t cnt, loff_t *ppos) 2834 { 2835 char buf[64]; 2836 char *cmp = buf; 2837 int neg = 0; 2838 int ret; 2839 int i; 2840 2841 if (cnt >= sizeof(buf)) 2842 return -EINVAL; 2843 2844 if (copy_from_user(&buf, ubuf, cnt)) 2845 return -EFAULT; 2846 2847 buf[cnt] = 0; 2848 2849 if (strncmp(buf, "no", 2) == 0) { 2850 neg = 1; 2851 cmp += 2; 2852 } 2853 2854 for (i = 0; trace_options[i]; i++) { 2855 int len = strlen(trace_options[i]); 2856 2857 if (strncmp(cmp, trace_options[i], len) == 0) { 2858 if (neg) 2859 trace_flags &= ~(1 << i); 2860 else 2861 trace_flags |= (1 << i); 2862 break; 2863 } 2864 } 2865 2866 /* If no option could be set, test the specific tracer options */ 2867 if (!trace_options[i]) { 2868 ret = set_tracer_option(current_trace, cmp, neg); 2869 if (ret) 2870 return ret; 2871 } 2872 2873 filp->f_pos += cnt; 2874 2875 return cnt; 2876 } 2877 2878 static struct file_operations tracing_iter_fops = { 2879 .open = tracing_open_generic, 2880 .read = tracing_trace_options_read, 2881 .write = tracing_trace_options_write, 2882 }; 2883 2884 static const char readme_msg[] = 2885 "tracing mini-HOWTO:\n\n" 2886 "# mkdir /debug\n" 2887 "# mount -t debugfs nodev /debug\n\n" 2888 "# cat /debug/tracing/available_tracers\n" 2889 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n" 2890 "# cat /debug/tracing/current_tracer\n" 2891 "none\n" 2892 "# echo sched_switch > /debug/tracing/current_tracer\n" 2893 "# cat /debug/tracing/current_tracer\n" 2894 "sched_switch\n" 2895 "# cat /debug/tracing/trace_options\n" 2896 "noprint-parent nosym-offset nosym-addr noverbose\n" 2897 "# echo print-parent > /debug/tracing/trace_options\n" 2898 "# echo 1 > /debug/tracing/tracing_enabled\n" 2899 "# cat /debug/tracing/trace > /tmp/trace.txt\n" 2900 "echo 0 > /debug/tracing/tracing_enabled\n" 2901 ; 2902 2903 static ssize_t 2904 tracing_readme_read(struct file *filp, char __user *ubuf, 2905 size_t cnt, loff_t *ppos) 2906 { 2907 return simple_read_from_buffer(ubuf, cnt, ppos, 2908 readme_msg, strlen(readme_msg)); 2909 } 2910 2911 static struct file_operations tracing_readme_fops = { 2912 .open = tracing_open_generic, 2913 .read = tracing_readme_read, 2914 }; 2915 2916 static ssize_t 2917 tracing_ctrl_read(struct file *filp, char __user *ubuf, 2918 size_t cnt, loff_t *ppos) 2919 { 2920 char buf[64]; 2921 int r; 2922 2923 r = sprintf(buf, "%u\n", tracer_enabled); 2924 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 2925 } 2926 2927 static ssize_t 2928 tracing_ctrl_write(struct file *filp, const char __user *ubuf, 2929 size_t cnt, loff_t *ppos) 2930 { 2931 struct trace_array *tr = filp->private_data; 2932 char buf[64]; 2933 long val; 2934 int ret; 2935 2936 if (cnt >= sizeof(buf)) 2937 return -EINVAL; 2938 2939 if (copy_from_user(&buf, ubuf, cnt)) 2940 return -EFAULT; 2941 2942 buf[cnt] = 0; 2943 2944 ret = strict_strtoul(buf, 10, &val); 2945 if (ret < 0) 2946 return ret; 2947 2948 val = !!val; 2949 2950 mutex_lock(&trace_types_lock); 2951 if (tracer_enabled ^ val) { 2952 if (val) { 2953 tracer_enabled = 1; 2954 if (current_trace->start) 2955 current_trace->start(tr); 2956 tracing_start(); 2957 } else { 2958 tracer_enabled = 0; 2959 tracing_stop(); 2960 if (current_trace->stop) 2961 current_trace->stop(tr); 2962 } 2963 } 2964 mutex_unlock(&trace_types_lock); 2965 2966 filp->f_pos += cnt; 2967 2968 return cnt; 2969 } 2970 2971 static ssize_t 2972 tracing_set_trace_read(struct file *filp, char __user *ubuf, 2973 size_t cnt, loff_t *ppos) 2974 { 2975 char buf[max_tracer_type_len+2]; 2976 int r; 2977 2978 mutex_lock(&trace_types_lock); 2979 if (current_trace) 2980 r = sprintf(buf, "%s\n", current_trace->name); 2981 else 2982 r = sprintf(buf, "\n"); 2983 mutex_unlock(&trace_types_lock); 2984 2985 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 2986 } 2987 2988 static int tracing_set_tracer(char *buf) 2989 { 2990 struct trace_array *tr = &global_trace; 2991 struct tracer *t; 2992 int ret = 0; 2993 2994 mutex_lock(&trace_types_lock); 2995 for (t = trace_types; t; t = t->next) { 2996 if (strcmp(t->name, buf) == 0) 2997 break; 2998 } 2999 if (!t) { 3000 ret = -EINVAL; 3001 goto out; 3002 } 3003 if (t == current_trace) 3004 goto out; 3005 3006 trace_branch_disable(); 3007 if (current_trace && current_trace->reset) 3008 current_trace->reset(tr); 3009 3010 current_trace = t; 3011 if (t->init) { 3012 ret = t->init(tr); 3013 if (ret) 3014 goto out; 3015 } 3016 3017 trace_branch_enable(tr); 3018 out: 3019 mutex_unlock(&trace_types_lock); 3020 3021 return ret; 3022 } 3023 3024 static ssize_t 3025 tracing_set_trace_write(struct file *filp, const char __user *ubuf, 3026 size_t cnt, loff_t *ppos) 3027 { 3028 char buf[max_tracer_type_len+1]; 3029 int i; 3030 size_t ret; 3031 int err; 3032 3033 ret = cnt; 3034 3035 if (cnt > max_tracer_type_len) 3036 cnt = max_tracer_type_len; 3037 3038 if (copy_from_user(&buf, ubuf, cnt)) 3039 return -EFAULT; 3040 3041 buf[cnt] = 0; 3042 3043 /* strip ending whitespace. */ 3044 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) 3045 buf[i] = 0; 3046 3047 err = tracing_set_tracer(buf); 3048 if (err) 3049 return err; 3050 3051 filp->f_pos += ret; 3052 3053 return ret; 3054 } 3055 3056 static ssize_t 3057 tracing_max_lat_read(struct file *filp, char __user *ubuf, 3058 size_t cnt, loff_t *ppos) 3059 { 3060 unsigned long *ptr = filp->private_data; 3061 char buf[64]; 3062 int r; 3063 3064 r = snprintf(buf, sizeof(buf), "%ld\n", 3065 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); 3066 if (r > sizeof(buf)) 3067 r = sizeof(buf); 3068 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 3069 } 3070 3071 static ssize_t 3072 tracing_max_lat_write(struct file *filp, const char __user *ubuf, 3073 size_t cnt, loff_t *ppos) 3074 { 3075 long *ptr = filp->private_data; 3076 char buf[64]; 3077 long val; 3078 int ret; 3079 3080 if (cnt >= sizeof(buf)) 3081 return -EINVAL; 3082 3083 if (copy_from_user(&buf, ubuf, cnt)) 3084 return -EFAULT; 3085 3086 buf[cnt] = 0; 3087 3088 ret = strict_strtoul(buf, 10, &val); 3089 if (ret < 0) 3090 return ret; 3091 3092 *ptr = val * 1000; 3093 3094 return cnt; 3095 } 3096 3097 static atomic_t tracing_reader; 3098 3099 static int tracing_open_pipe(struct inode *inode, struct file *filp) 3100 { 3101 struct trace_iterator *iter; 3102 3103 if (tracing_disabled) 3104 return -ENODEV; 3105 3106 /* We only allow for reader of the pipe */ 3107 if (atomic_inc_return(&tracing_reader) != 1) { 3108 atomic_dec(&tracing_reader); 3109 return -EBUSY; 3110 } 3111 3112 /* create a buffer to store the information to pass to userspace */ 3113 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 3114 if (!iter) 3115 return -ENOMEM; 3116 3117 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) { 3118 kfree(iter); 3119 return -ENOMEM; 3120 } 3121 3122 mutex_lock(&trace_types_lock); 3123 3124 /* trace pipe does not show start of buffer */ 3125 cpumask_setall(iter->started); 3126 3127 iter->tr = &global_trace; 3128 iter->trace = current_trace; 3129 filp->private_data = iter; 3130 3131 if (iter->trace->pipe_open) 3132 iter->trace->pipe_open(iter); 3133 mutex_unlock(&trace_types_lock); 3134 3135 return 0; 3136 } 3137 3138 static int tracing_release_pipe(struct inode *inode, struct file *file) 3139 { 3140 struct trace_iterator *iter = file->private_data; 3141 3142 free_cpumask_var(iter->started); 3143 kfree(iter); 3144 atomic_dec(&tracing_reader); 3145 3146 return 0; 3147 } 3148 3149 static unsigned int 3150 tracing_poll_pipe(struct file *filp, poll_table *poll_table) 3151 { 3152 struct trace_iterator *iter = filp->private_data; 3153 3154 if (trace_flags & TRACE_ITER_BLOCK) { 3155 /* 3156 * Always select as readable when in blocking mode 3157 */ 3158 return POLLIN | POLLRDNORM; 3159 } else { 3160 if (!trace_empty(iter)) 3161 return POLLIN | POLLRDNORM; 3162 poll_wait(filp, &trace_wait, poll_table); 3163 if (!trace_empty(iter)) 3164 return POLLIN | POLLRDNORM; 3165 3166 return 0; 3167 } 3168 } 3169 3170 /* 3171 * Consumer reader. 3172 */ 3173 static ssize_t 3174 tracing_read_pipe(struct file *filp, char __user *ubuf, 3175 size_t cnt, loff_t *ppos) 3176 { 3177 struct trace_iterator *iter = filp->private_data; 3178 ssize_t sret; 3179 3180 /* return any leftover data */ 3181 sret = trace_seq_to_user(&iter->seq, ubuf, cnt); 3182 if (sret != -EBUSY) 3183 return sret; 3184 3185 trace_seq_reset(&iter->seq); 3186 3187 mutex_lock(&trace_types_lock); 3188 if (iter->trace->read) { 3189 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); 3190 if (sret) 3191 goto out; 3192 } 3193 3194 waitagain: 3195 sret = 0; 3196 while (trace_empty(iter)) { 3197 3198 if ((filp->f_flags & O_NONBLOCK)) { 3199 sret = -EAGAIN; 3200 goto out; 3201 } 3202 3203 /* 3204 * This is a make-shift waitqueue. The reason we don't use 3205 * an actual wait queue is because: 3206 * 1) we only ever have one waiter 3207 * 2) the tracing, traces all functions, we don't want 3208 * the overhead of calling wake_up and friends 3209 * (and tracing them too) 3210 * Anyway, this is really very primitive wakeup. 3211 */ 3212 set_current_state(TASK_INTERRUPTIBLE); 3213 iter->tr->waiter = current; 3214 3215 mutex_unlock(&trace_types_lock); 3216 3217 /* sleep for 100 msecs, and try again. */ 3218 schedule_timeout(HZ/10); 3219 3220 mutex_lock(&trace_types_lock); 3221 3222 iter->tr->waiter = NULL; 3223 3224 if (signal_pending(current)) { 3225 sret = -EINTR; 3226 goto out; 3227 } 3228 3229 if (iter->trace != current_trace) 3230 goto out; 3231 3232 /* 3233 * We block until we read something and tracing is disabled. 3234 * We still block if tracing is disabled, but we have never 3235 * read anything. This allows a user to cat this file, and 3236 * then enable tracing. But after we have read something, 3237 * we give an EOF when tracing is again disabled. 3238 * 3239 * iter->pos will be 0 if we haven't read anything. 3240 */ 3241 if (!tracer_enabled && iter->pos) 3242 break; 3243 3244 continue; 3245 } 3246 3247 /* stop when tracing is finished */ 3248 if (trace_empty(iter)) 3249 goto out; 3250 3251 if (cnt >= PAGE_SIZE) 3252 cnt = PAGE_SIZE - 1; 3253 3254 /* reset all but tr, trace, and overruns */ 3255 memset(&iter->seq, 0, 3256 sizeof(struct trace_iterator) - 3257 offsetof(struct trace_iterator, seq)); 3258 iter->pos = -1; 3259 3260 while (find_next_entry_inc(iter) != NULL) { 3261 enum print_line_t ret; 3262 int len = iter->seq.len; 3263 3264 ret = print_trace_line(iter); 3265 if (ret == TRACE_TYPE_PARTIAL_LINE) { 3266 /* don't print partial lines */ 3267 iter->seq.len = len; 3268 break; 3269 } 3270 3271 trace_consume(iter); 3272 3273 if (iter->seq.len >= cnt) 3274 break; 3275 } 3276 3277 /* Now copy what we have to the user */ 3278 sret = trace_seq_to_user(&iter->seq, ubuf, cnt); 3279 if (iter->seq.readpos >= iter->seq.len) 3280 trace_seq_reset(&iter->seq); 3281 3282 /* 3283 * If there was nothing to send to user, inspite of consuming trace 3284 * entries, go back to wait for more entries. 3285 */ 3286 if (sret == -EBUSY) 3287 goto waitagain; 3288 3289 out: 3290 mutex_unlock(&trace_types_lock); 3291 3292 return sret; 3293 } 3294 3295 static ssize_t 3296 tracing_entries_read(struct file *filp, char __user *ubuf, 3297 size_t cnt, loff_t *ppos) 3298 { 3299 struct trace_array *tr = filp->private_data; 3300 char buf[64]; 3301 int r; 3302 3303 r = sprintf(buf, "%lu\n", tr->entries >> 10); 3304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 3305 } 3306 3307 static ssize_t 3308 tracing_entries_write(struct file *filp, const char __user *ubuf, 3309 size_t cnt, loff_t *ppos) 3310 { 3311 unsigned long val; 3312 char buf[64]; 3313 int ret, cpu; 3314 3315 if (cnt >= sizeof(buf)) 3316 return -EINVAL; 3317 3318 if (copy_from_user(&buf, ubuf, cnt)) 3319 return -EFAULT; 3320 3321 buf[cnt] = 0; 3322 3323 ret = strict_strtoul(buf, 10, &val); 3324 if (ret < 0) 3325 return ret; 3326 3327 /* must have at least 1 entry */ 3328 if (!val) 3329 return -EINVAL; 3330 3331 mutex_lock(&trace_types_lock); 3332 3333 tracing_stop(); 3334 3335 /* disable all cpu buffers */ 3336 for_each_tracing_cpu(cpu) { 3337 if (global_trace.data[cpu]) 3338 atomic_inc(&global_trace.data[cpu]->disabled); 3339 if (max_tr.data[cpu]) 3340 atomic_inc(&max_tr.data[cpu]->disabled); 3341 } 3342 3343 /* value is in KB */ 3344 val <<= 10; 3345 3346 if (val != global_trace.entries) { 3347 ret = ring_buffer_resize(global_trace.buffer, val); 3348 if (ret < 0) { 3349 cnt = ret; 3350 goto out; 3351 } 3352 3353 ret = ring_buffer_resize(max_tr.buffer, val); 3354 if (ret < 0) { 3355 int r; 3356 cnt = ret; 3357 r = ring_buffer_resize(global_trace.buffer, 3358 global_trace.entries); 3359 if (r < 0) { 3360 /* AARGH! We are left with different 3361 * size max buffer!!!! */ 3362 WARN_ON(1); 3363 tracing_disabled = 1; 3364 } 3365 goto out; 3366 } 3367 3368 global_trace.entries = val; 3369 } 3370 3371 filp->f_pos += cnt; 3372 3373 /* If check pages failed, return ENOMEM */ 3374 if (tracing_disabled) 3375 cnt = -ENOMEM; 3376 out: 3377 for_each_tracing_cpu(cpu) { 3378 if (global_trace.data[cpu]) 3379 atomic_dec(&global_trace.data[cpu]->disabled); 3380 if (max_tr.data[cpu]) 3381 atomic_dec(&max_tr.data[cpu]->disabled); 3382 } 3383 3384 tracing_start(); 3385 max_tr.entries = global_trace.entries; 3386 mutex_unlock(&trace_types_lock); 3387 3388 return cnt; 3389 } 3390 3391 static int mark_printk(const char *fmt, ...) 3392 { 3393 int ret; 3394 va_list args; 3395 va_start(args, fmt); 3396 ret = trace_vprintk(0, -1, fmt, args); 3397 va_end(args); 3398 return ret; 3399 } 3400 3401 static ssize_t 3402 tracing_mark_write(struct file *filp, const char __user *ubuf, 3403 size_t cnt, loff_t *fpos) 3404 { 3405 char *buf; 3406 char *end; 3407 3408 if (tracing_disabled) 3409 return -EINVAL; 3410 3411 if (cnt > TRACE_BUF_SIZE) 3412 cnt = TRACE_BUF_SIZE; 3413 3414 buf = kmalloc(cnt + 1, GFP_KERNEL); 3415 if (buf == NULL) 3416 return -ENOMEM; 3417 3418 if (copy_from_user(buf, ubuf, cnt)) { 3419 kfree(buf); 3420 return -EFAULT; 3421 } 3422 3423 /* Cut from the first nil or newline. */ 3424 buf[cnt] = '\0'; 3425 end = strchr(buf, '\n'); 3426 if (end) 3427 *end = '\0'; 3428 3429 cnt = mark_printk("%s\n", buf); 3430 kfree(buf); 3431 *fpos += cnt; 3432 3433 return cnt; 3434 } 3435 3436 static struct file_operations tracing_max_lat_fops = { 3437 .open = tracing_open_generic, 3438 .read = tracing_max_lat_read, 3439 .write = tracing_max_lat_write, 3440 }; 3441 3442 static struct file_operations tracing_ctrl_fops = { 3443 .open = tracing_open_generic, 3444 .read = tracing_ctrl_read, 3445 .write = tracing_ctrl_write, 3446 }; 3447 3448 static struct file_operations set_tracer_fops = { 3449 .open = tracing_open_generic, 3450 .read = tracing_set_trace_read, 3451 .write = tracing_set_trace_write, 3452 }; 3453 3454 static struct file_operations tracing_pipe_fops = { 3455 .open = tracing_open_pipe, 3456 .poll = tracing_poll_pipe, 3457 .read = tracing_read_pipe, 3458 .release = tracing_release_pipe, 3459 }; 3460 3461 static struct file_operations tracing_entries_fops = { 3462 .open = tracing_open_generic, 3463 .read = tracing_entries_read, 3464 .write = tracing_entries_write, 3465 }; 3466 3467 static struct file_operations tracing_mark_fops = { 3468 .open = tracing_open_generic, 3469 .write = tracing_mark_write, 3470 }; 3471 3472 #ifdef CONFIG_DYNAMIC_FTRACE 3473 3474 int __weak ftrace_arch_read_dyn_info(char *buf, int size) 3475 { 3476 return 0; 3477 } 3478 3479 static ssize_t 3480 tracing_read_dyn_info(struct file *filp, char __user *ubuf, 3481 size_t cnt, loff_t *ppos) 3482 { 3483 static char ftrace_dyn_info_buffer[1024]; 3484 static DEFINE_MUTEX(dyn_info_mutex); 3485 unsigned long *p = filp->private_data; 3486 char *buf = ftrace_dyn_info_buffer; 3487 int size = ARRAY_SIZE(ftrace_dyn_info_buffer); 3488 int r; 3489 3490 mutex_lock(&dyn_info_mutex); 3491 r = sprintf(buf, "%ld ", *p); 3492 3493 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r); 3494 buf[r++] = '\n'; 3495 3496 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 3497 3498 mutex_unlock(&dyn_info_mutex); 3499 3500 return r; 3501 } 3502 3503 static struct file_operations tracing_dyn_info_fops = { 3504 .open = tracing_open_generic, 3505 .read = tracing_read_dyn_info, 3506 }; 3507 #endif 3508 3509 static struct dentry *d_tracer; 3510 3511 struct dentry *tracing_init_dentry(void) 3512 { 3513 static int once; 3514 3515 if (d_tracer) 3516 return d_tracer; 3517 3518 d_tracer = debugfs_create_dir("tracing", NULL); 3519 3520 if (!d_tracer && !once) { 3521 once = 1; 3522 pr_warning("Could not create debugfs directory 'tracing'\n"); 3523 return NULL; 3524 } 3525 3526 return d_tracer; 3527 } 3528 3529 #ifdef CONFIG_FTRACE_SELFTEST 3530 /* Let selftest have access to static functions in this file */ 3531 #include "trace_selftest.c" 3532 #endif 3533 3534 static __init int tracer_init_debugfs(void) 3535 { 3536 struct dentry *d_tracer; 3537 struct dentry *entry; 3538 3539 d_tracer = tracing_init_dentry(); 3540 3541 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer, 3542 &global_trace, &tracing_ctrl_fops); 3543 if (!entry) 3544 pr_warning("Could not create debugfs 'tracing_enabled' entry\n"); 3545 3546 entry = debugfs_create_file("trace_options", 0644, d_tracer, 3547 NULL, &tracing_iter_fops); 3548 if (!entry) 3549 pr_warning("Could not create debugfs 'trace_options' entry\n"); 3550 3551 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer, 3552 NULL, &tracing_cpumask_fops); 3553 if (!entry) 3554 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n"); 3555 3556 entry = debugfs_create_file("latency_trace", 0444, d_tracer, 3557 &global_trace, &tracing_lt_fops); 3558 if (!entry) 3559 pr_warning("Could not create debugfs 'latency_trace' entry\n"); 3560 3561 entry = debugfs_create_file("trace", 0444, d_tracer, 3562 &global_trace, &tracing_fops); 3563 if (!entry) 3564 pr_warning("Could not create debugfs 'trace' entry\n"); 3565 3566 entry = debugfs_create_file("available_tracers", 0444, d_tracer, 3567 &global_trace, &show_traces_fops); 3568 if (!entry) 3569 pr_warning("Could not create debugfs 'available_tracers' entry\n"); 3570 3571 entry = debugfs_create_file("current_tracer", 0444, d_tracer, 3572 &global_trace, &set_tracer_fops); 3573 if (!entry) 3574 pr_warning("Could not create debugfs 'current_tracer' entry\n"); 3575 3576 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer, 3577 &tracing_max_latency, 3578 &tracing_max_lat_fops); 3579 if (!entry) 3580 pr_warning("Could not create debugfs " 3581 "'tracing_max_latency' entry\n"); 3582 3583 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer, 3584 &tracing_thresh, &tracing_max_lat_fops); 3585 if (!entry) 3586 pr_warning("Could not create debugfs " 3587 "'tracing_thresh' entry\n"); 3588 entry = debugfs_create_file("README", 0644, d_tracer, 3589 NULL, &tracing_readme_fops); 3590 if (!entry) 3591 pr_warning("Could not create debugfs 'README' entry\n"); 3592 3593 entry = debugfs_create_file("trace_pipe", 0644, d_tracer, 3594 NULL, &tracing_pipe_fops); 3595 if (!entry) 3596 pr_warning("Could not create debugfs " 3597 "'trace_pipe' entry\n"); 3598 3599 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer, 3600 &global_trace, &tracing_entries_fops); 3601 if (!entry) 3602 pr_warning("Could not create debugfs " 3603 "'buffer_size_kb' entry\n"); 3604 3605 entry = debugfs_create_file("trace_marker", 0220, d_tracer, 3606 NULL, &tracing_mark_fops); 3607 if (!entry) 3608 pr_warning("Could not create debugfs " 3609 "'trace_marker' entry\n"); 3610 3611 #ifdef CONFIG_DYNAMIC_FTRACE 3612 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer, 3613 &ftrace_update_tot_cnt, 3614 &tracing_dyn_info_fops); 3615 if (!entry) 3616 pr_warning("Could not create debugfs " 3617 "'dyn_ftrace_total_info' entry\n"); 3618 #endif 3619 #ifdef CONFIG_SYSPROF_TRACER 3620 init_tracer_sysprof_debugfs(d_tracer); 3621 #endif 3622 return 0; 3623 } 3624 3625 int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args) 3626 { 3627 static DEFINE_SPINLOCK(trace_buf_lock); 3628 static char trace_buf[TRACE_BUF_SIZE]; 3629 3630 struct ring_buffer_event *event; 3631 struct trace_array *tr = &global_trace; 3632 struct trace_array_cpu *data; 3633 int cpu, len = 0, size, pc; 3634 struct print_entry *entry; 3635 unsigned long irq_flags; 3636 3637 if (tracing_disabled || tracing_selftest_running) 3638 return 0; 3639 3640 pc = preempt_count(); 3641 preempt_disable_notrace(); 3642 cpu = raw_smp_processor_id(); 3643 data = tr->data[cpu]; 3644 3645 if (unlikely(atomic_read(&data->disabled))) 3646 goto out; 3647 3648 pause_graph_tracing(); 3649 spin_lock_irqsave(&trace_buf_lock, irq_flags); 3650 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args); 3651 3652 len = min(len, TRACE_BUF_SIZE-1); 3653 trace_buf[len] = 0; 3654 3655 size = sizeof(*entry) + len + 1; 3656 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags); 3657 if (!event) 3658 goto out_unlock; 3659 entry = ring_buffer_event_data(event); 3660 tracing_generic_entry_update(&entry->ent, irq_flags, pc); 3661 entry->ent.type = TRACE_PRINT; 3662 entry->ip = ip; 3663 entry->depth = depth; 3664 3665 memcpy(&entry->buf, trace_buf, len); 3666 entry->buf[len] = 0; 3667 ring_buffer_unlock_commit(tr->buffer, event, irq_flags); 3668 3669 out_unlock: 3670 spin_unlock_irqrestore(&trace_buf_lock, irq_flags); 3671 unpause_graph_tracing(); 3672 out: 3673 preempt_enable_notrace(); 3674 3675 return len; 3676 } 3677 EXPORT_SYMBOL_GPL(trace_vprintk); 3678 3679 int __ftrace_printk(unsigned long ip, const char *fmt, ...) 3680 { 3681 int ret; 3682 va_list ap; 3683 3684 if (!(trace_flags & TRACE_ITER_PRINTK)) 3685 return 0; 3686 3687 va_start(ap, fmt); 3688 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap); 3689 va_end(ap); 3690 return ret; 3691 } 3692 EXPORT_SYMBOL_GPL(__ftrace_printk); 3693 3694 static int trace_panic_handler(struct notifier_block *this, 3695 unsigned long event, void *unused) 3696 { 3697 if (ftrace_dump_on_oops) 3698 ftrace_dump(); 3699 return NOTIFY_OK; 3700 } 3701 3702 static struct notifier_block trace_panic_notifier = { 3703 .notifier_call = trace_panic_handler, 3704 .next = NULL, 3705 .priority = 150 /* priority: INT_MAX >= x >= 0 */ 3706 }; 3707 3708 static int trace_die_handler(struct notifier_block *self, 3709 unsigned long val, 3710 void *data) 3711 { 3712 switch (val) { 3713 case DIE_OOPS: 3714 if (ftrace_dump_on_oops) 3715 ftrace_dump(); 3716 break; 3717 default: 3718 break; 3719 } 3720 return NOTIFY_OK; 3721 } 3722 3723 static struct notifier_block trace_die_notifier = { 3724 .notifier_call = trace_die_handler, 3725 .priority = 200 3726 }; 3727 3728 /* 3729 * printk is set to max of 1024, we really don't need it that big. 3730 * Nothing should be printing 1000 characters anyway. 3731 */ 3732 #define TRACE_MAX_PRINT 1000 3733 3734 /* 3735 * Define here KERN_TRACE so that we have one place to modify 3736 * it if we decide to change what log level the ftrace dump 3737 * should be at. 3738 */ 3739 #define KERN_TRACE KERN_EMERG 3740 3741 static void 3742 trace_printk_seq(struct trace_seq *s) 3743 { 3744 /* Probably should print a warning here. */ 3745 if (s->len >= 1000) 3746 s->len = 1000; 3747 3748 /* should be zero ended, but we are paranoid. */ 3749 s->buffer[s->len] = 0; 3750 3751 printk(KERN_TRACE "%s", s->buffer); 3752 3753 trace_seq_reset(s); 3754 } 3755 3756 void ftrace_dump(void) 3757 { 3758 static DEFINE_SPINLOCK(ftrace_dump_lock); 3759 /* use static because iter can be a bit big for the stack */ 3760 static struct trace_iterator iter; 3761 static int dump_ran; 3762 unsigned long flags; 3763 int cnt = 0, cpu; 3764 3765 /* only one dump */ 3766 spin_lock_irqsave(&ftrace_dump_lock, flags); 3767 if (dump_ran) 3768 goto out; 3769 3770 dump_ran = 1; 3771 3772 /* No turning back! */ 3773 tracing_off(); 3774 ftrace_kill(); 3775 3776 for_each_tracing_cpu(cpu) { 3777 atomic_inc(&global_trace.data[cpu]->disabled); 3778 } 3779 3780 /* don't look at user memory in panic mode */ 3781 trace_flags &= ~TRACE_ITER_SYM_USEROBJ; 3782 3783 printk(KERN_TRACE "Dumping ftrace buffer:\n"); 3784 3785 iter.tr = &global_trace; 3786 iter.trace = current_trace; 3787 3788 /* 3789 * We need to stop all tracing on all CPUS to read the 3790 * the next buffer. This is a bit expensive, but is 3791 * not done often. We fill all what we can read, 3792 * and then release the locks again. 3793 */ 3794 3795 while (!trace_empty(&iter)) { 3796 3797 if (!cnt) 3798 printk(KERN_TRACE "---------------------------------\n"); 3799 3800 cnt++; 3801 3802 /* reset all but tr, trace, and overruns */ 3803 memset(&iter.seq, 0, 3804 sizeof(struct trace_iterator) - 3805 offsetof(struct trace_iterator, seq)); 3806 iter.iter_flags |= TRACE_FILE_LAT_FMT; 3807 iter.pos = -1; 3808 3809 if (find_next_entry_inc(&iter) != NULL) { 3810 print_trace_line(&iter); 3811 trace_consume(&iter); 3812 } 3813 3814 trace_printk_seq(&iter.seq); 3815 } 3816 3817 if (!cnt) 3818 printk(KERN_TRACE " (ftrace buffer empty)\n"); 3819 else 3820 printk(KERN_TRACE "---------------------------------\n"); 3821 3822 out: 3823 spin_unlock_irqrestore(&ftrace_dump_lock, flags); 3824 } 3825 3826 __init static int tracer_alloc_buffers(void) 3827 { 3828 struct trace_array_cpu *data; 3829 int i; 3830 int ret = -ENOMEM; 3831 3832 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL)) 3833 goto out; 3834 3835 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL)) 3836 goto out_free_buffer_mask; 3837 3838 cpumask_copy(tracing_buffer_mask, cpu_possible_mask); 3839 cpumask_copy(tracing_cpumask, cpu_all_mask); 3840 3841 /* TODO: make the number of buffers hot pluggable with CPUS */ 3842 global_trace.buffer = ring_buffer_alloc(trace_buf_size, 3843 TRACE_BUFFER_FLAGS); 3844 if (!global_trace.buffer) { 3845 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n"); 3846 WARN_ON(1); 3847 goto out_free_cpumask; 3848 } 3849 global_trace.entries = ring_buffer_size(global_trace.buffer); 3850 3851 3852 #ifdef CONFIG_TRACER_MAX_TRACE 3853 max_tr.buffer = ring_buffer_alloc(trace_buf_size, 3854 TRACE_BUFFER_FLAGS); 3855 if (!max_tr.buffer) { 3856 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n"); 3857 WARN_ON(1); 3858 ring_buffer_free(global_trace.buffer); 3859 goto out_free_cpumask; 3860 } 3861 max_tr.entries = ring_buffer_size(max_tr.buffer); 3862 WARN_ON(max_tr.entries != global_trace.entries); 3863 #endif 3864 3865 /* Allocate the first page for all buffers */ 3866 for_each_tracing_cpu(i) { 3867 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i); 3868 max_tr.data[i] = &per_cpu(max_data, i); 3869 } 3870 3871 trace_init_cmdlines(); 3872 3873 register_tracer(&nop_trace); 3874 #ifdef CONFIG_BOOT_TRACER 3875 register_tracer(&boot_tracer); 3876 current_trace = &boot_tracer; 3877 current_trace->init(&global_trace); 3878 #else 3879 current_trace = &nop_trace; 3880 #endif 3881 3882 /* All seems OK, enable tracing */ 3883 tracing_disabled = 0; 3884 3885 atomic_notifier_chain_register(&panic_notifier_list, 3886 &trace_panic_notifier); 3887 3888 register_die_notifier(&trace_die_notifier); 3889 ret = 0; 3890 3891 out_free_cpumask: 3892 free_cpumask_var(tracing_cpumask); 3893 out_free_buffer_mask: 3894 free_cpumask_var(tracing_buffer_mask); 3895 out: 3896 return ret; 3897 } 3898 early_initcall(tracer_alloc_buffers); 3899 fs_initcall(tracer_init_debugfs); 3900