| 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/debugfs.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/hardirq.h> |
| 20 | #include <linux/linkage.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/ftrace.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/percpu.h> |
| 25 | #include <linux/ctype.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/poll.h> |
| 28 | #include <linux/gfp.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/kprobes.h> |
| 31 | #include <linux/writeback.h> |
| 32 | |
| 33 | #include <linux/stacktrace.h> |
| 34 | |
| 35 | #include "trace.h" |
| 36 | |
| 37 | unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX; |
| 38 | unsigned long __read_mostly tracing_thresh; |
| 39 | |
| 40 | static unsigned long __read_mostly tracing_nr_buffers; |
| 41 | static cpumask_t __read_mostly tracing_buffer_mask; |
| 42 | |
| 43 | #define for_each_tracing_cpu(cpu) \ |
| 44 | for_each_cpu_mask(cpu, tracing_buffer_mask) |
| 45 | |
| 46 | static int trace_alloc_page(void); |
| 47 | static int trace_free_page(void); |
| 48 | |
| 49 | static int tracing_disabled = 1; |
| 50 | |
| 51 | static unsigned long tracing_pages_allocated; |
| 52 | |
| 53 | long |
| 54 | ns2usecs(cycle_t nsec) |
| 55 | { |
| 56 | nsec += 500; |
| 57 | do_div(nsec, 1000); |
| 58 | return nsec; |
| 59 | } |
| 60 | |
| 61 | cycle_t ftrace_now(int cpu) |
| 62 | { |
| 63 | return cpu_clock(cpu); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * The global_trace is the descriptor that holds the tracing |
| 68 | * buffers for the live tracing. For each CPU, it contains |
| 69 | * a link list of pages that will store trace entries. The |
| 70 | * page descriptor of the pages in the memory is used to hold |
| 71 | * the link list by linking the lru item in the page descriptor |
| 72 | * to each of the pages in the buffer per CPU. |
| 73 | * |
| 74 | * For each active CPU there is a data field that holds the |
| 75 | * pages for the buffer for that CPU. Each CPU has the same number |
| 76 | * of pages allocated for its buffer. |
| 77 | */ |
| 78 | static struct trace_array global_trace; |
| 79 | |
| 80 | static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu); |
| 81 | |
| 82 | /* |
| 83 | * The max_tr is used to snapshot the global_trace when a maximum |
| 84 | * latency is reached. Some tracers will use this to store a maximum |
| 85 | * trace while it continues examining live traces. |
| 86 | * |
| 87 | * The buffers for the max_tr are set up the same as the global_trace. |
| 88 | * When a snapshot is taken, the link list of the max_tr is swapped |
| 89 | * with the link list of the global_trace and the buffers are reset for |
| 90 | * the global_trace so the tracing can continue. |
| 91 | */ |
| 92 | static struct trace_array max_tr; |
| 93 | |
| 94 | static DEFINE_PER_CPU(struct trace_array_cpu, max_data); |
| 95 | |
| 96 | /* tracer_enabled is used to toggle activation of a tracer */ |
| 97 | static int tracer_enabled = 1; |
| 98 | |
| 99 | /* function tracing enabled */ |
| 100 | int ftrace_function_enabled; |
| 101 | |
| 102 | /* |
| 103 | * trace_nr_entries is the number of entries that is allocated |
| 104 | * for a buffer. Note, the number of entries is always rounded |
| 105 | * to ENTRIES_PER_PAGE. |
| 106 | */ |
| 107 | static unsigned long trace_nr_entries = 65536UL; |
| 108 | |
| 109 | /* trace_types holds a link list of available tracers. */ |
| 110 | static struct tracer *trace_types __read_mostly; |
| 111 | |
| 112 | /* current_trace points to the tracer that is currently active */ |
| 113 | static struct tracer *current_trace __read_mostly; |
| 114 | |
| 115 | /* |
| 116 | * max_tracer_type_len is used to simplify the allocating of |
| 117 | * buffers to read userspace tracer names. We keep track of |
| 118 | * the longest tracer name registered. |
| 119 | */ |
| 120 | static int max_tracer_type_len; |
| 121 | |
| 122 | /* |
| 123 | * trace_types_lock is used to protect the trace_types list. |
| 124 | * This lock is also used to keep user access serialized. |
| 125 | * Accesses from userspace will grab this lock while userspace |
| 126 | * activities happen inside the kernel. |
| 127 | */ |
| 128 | static DEFINE_MUTEX(trace_types_lock); |
| 129 | |
| 130 | /* trace_wait is a waitqueue for tasks blocked on trace_poll */ |
| 131 | static DECLARE_WAIT_QUEUE_HEAD(trace_wait); |
| 132 | |
| 133 | /* trace_flags holds iter_ctrl options */ |
| 134 | unsigned long trace_flags = TRACE_ITER_PRINT_PARENT; |
| 135 | |
| 136 | static notrace void no_trace_init(struct trace_array *tr) |
| 137 | { |
| 138 | int cpu; |
| 139 | |
| 140 | ftrace_function_enabled = 0; |
| 141 | if(tr->ctrl) |
| 142 | for_each_online_cpu(cpu) |
| 143 | tracing_reset(tr->data[cpu]); |
| 144 | tracer_enabled = 0; |
| 145 | } |
| 146 | |
| 147 | /* dummy trace to disable tracing */ |
| 148 | static struct tracer no_tracer __read_mostly = { |
| 149 | .name = "none", |
| 150 | .init = no_trace_init |
| 151 | }; |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * trace_wake_up - wake up tasks waiting for trace input |
| 156 | * |
| 157 | * Simply wakes up any task that is blocked on the trace_wait |
| 158 | * queue. These is used with trace_poll for tasks polling the trace. |
| 159 | */ |
| 160 | void trace_wake_up(void) |
| 161 | { |
| 162 | /* |
| 163 | * The runqueue_is_locked() can fail, but this is the best we |
| 164 | * have for now: |
| 165 | */ |
| 166 | if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked()) |
| 167 | wake_up(&trace_wait); |
| 168 | } |
| 169 | |
| 170 | #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry)) |
| 171 | |
| 172 | static int __init set_nr_entries(char *str) |
| 173 | { |
| 174 | unsigned long nr_entries; |
| 175 | int ret; |
| 176 | |
| 177 | if (!str) |
| 178 | return 0; |
| 179 | ret = strict_strtoul(str, 0, &nr_entries); |
| 180 | /* nr_entries can not be zero */ |
| 181 | if (ret < 0 || nr_entries == 0) |
| 182 | return 0; |
| 183 | trace_nr_entries = nr_entries; |
| 184 | return 1; |
| 185 | } |
| 186 | __setup("trace_entries=", set_nr_entries); |
| 187 | |
| 188 | unsigned long nsecs_to_usecs(unsigned long nsecs) |
| 189 | { |
| 190 | return nsecs / 1000; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * trace_flag_type is an enumeration that holds different |
| 195 | * states when a trace occurs. These are: |
| 196 | * IRQS_OFF - interrupts were disabled |
| 197 | * NEED_RESCED - reschedule is requested |
| 198 | * HARDIRQ - inside an interrupt handler |
| 199 | * SOFTIRQ - inside a softirq handler |
| 200 | * CONT - multiple entries hold the trace item |
| 201 | */ |
| 202 | enum trace_flag_type { |
| 203 | TRACE_FLAG_IRQS_OFF = 0x01, |
| 204 | TRACE_FLAG_NEED_RESCHED = 0x02, |
| 205 | TRACE_FLAG_HARDIRQ = 0x04, |
| 206 | TRACE_FLAG_SOFTIRQ = 0x08, |
| 207 | TRACE_FLAG_CONT = 0x10, |
| 208 | }; |
| 209 | |
| 210 | /* |
| 211 | * TRACE_ITER_SYM_MASK masks the options in trace_flags that |
| 212 | * control the output of kernel symbols. |
| 213 | */ |
| 214 | #define TRACE_ITER_SYM_MASK \ |
| 215 | (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR) |
| 216 | |
| 217 | /* These must match the bit postions in trace_iterator_flags */ |
| 218 | static const char *trace_options[] = { |
| 219 | "print-parent", |
| 220 | "sym-offset", |
| 221 | "sym-addr", |
| 222 | "verbose", |
| 223 | "raw", |
| 224 | "hex", |
| 225 | "bin", |
| 226 | "block", |
| 227 | "stacktrace", |
| 228 | "sched-tree", |
| 229 | NULL |
| 230 | }; |
| 231 | |
| 232 | /* |
| 233 | * ftrace_max_lock is used to protect the swapping of buffers |
| 234 | * when taking a max snapshot. The buffers themselves are |
| 235 | * protected by per_cpu spinlocks. But the action of the swap |
| 236 | * needs its own lock. |
| 237 | * |
| 238 | * This is defined as a raw_spinlock_t in order to help |
| 239 | * with performance when lockdep debugging is enabled. |
| 240 | */ |
| 241 | static raw_spinlock_t ftrace_max_lock = |
| 242 | (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
| 243 | |
| 244 | /* |
| 245 | * Copy the new maximum trace into the separate maximum-trace |
| 246 | * structure. (this way the maximum trace is permanently saved, |
| 247 | * for later retrieval via /debugfs/tracing/latency_trace) |
| 248 | */ |
| 249 | static void |
| 250 | __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 251 | { |
| 252 | struct trace_array_cpu *data = tr->data[cpu]; |
| 253 | |
| 254 | max_tr.cpu = cpu; |
| 255 | max_tr.time_start = data->preempt_timestamp; |
| 256 | |
| 257 | data = max_tr.data[cpu]; |
| 258 | data->saved_latency = tracing_max_latency; |
| 259 | |
| 260 | memcpy(data->comm, tsk->comm, TASK_COMM_LEN); |
| 261 | data->pid = tsk->pid; |
| 262 | data->uid = tsk->uid; |
| 263 | data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; |
| 264 | data->policy = tsk->policy; |
| 265 | data->rt_priority = tsk->rt_priority; |
| 266 | |
| 267 | /* record this tasks comm */ |
| 268 | tracing_record_cmdline(current); |
| 269 | } |
| 270 | |
| 271 | #define CHECK_COND(cond) \ |
| 272 | if (unlikely(cond)) { \ |
| 273 | tracing_disabled = 1; \ |
| 274 | WARN_ON(1); \ |
| 275 | return -1; \ |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * check_pages - integrity check of trace buffers |
| 280 | * |
| 281 | * As a safty measure we check to make sure the data pages have not |
| 282 | * been corrupted. |
| 283 | */ |
| 284 | int check_pages(struct trace_array_cpu *data) |
| 285 | { |
| 286 | struct page *page, *tmp; |
| 287 | |
| 288 | CHECK_COND(data->trace_pages.next->prev != &data->trace_pages); |
| 289 | CHECK_COND(data->trace_pages.prev->next != &data->trace_pages); |
| 290 | |
| 291 | list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) { |
| 292 | CHECK_COND(page->lru.next->prev != &page->lru); |
| 293 | CHECK_COND(page->lru.prev->next != &page->lru); |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * head_page - page address of the first page in per_cpu buffer. |
| 301 | * |
| 302 | * head_page returns the page address of the first page in |
| 303 | * a per_cpu buffer. This also preforms various consistency |
| 304 | * checks to make sure the buffer has not been corrupted. |
| 305 | */ |
| 306 | void *head_page(struct trace_array_cpu *data) |
| 307 | { |
| 308 | struct page *page; |
| 309 | |
| 310 | if (list_empty(&data->trace_pages)) |
| 311 | return NULL; |
| 312 | |
| 313 | page = list_entry(data->trace_pages.next, struct page, lru); |
| 314 | BUG_ON(&page->lru == &data->trace_pages); |
| 315 | |
| 316 | return page_address(page); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * trace_seq_printf - sequence printing of trace information |
| 321 | * @s: trace sequence descriptor |
| 322 | * @fmt: printf format string |
| 323 | * |
| 324 | * The tracer may use either sequence operations or its own |
| 325 | * copy to user routines. To simplify formating of a trace |
| 326 | * trace_seq_printf is used to store strings into a special |
| 327 | * buffer (@s). Then the output may be either used by |
| 328 | * the sequencer or pulled into another buffer. |
| 329 | */ |
| 330 | int |
| 331 | trace_seq_printf(struct trace_seq *s, const char *fmt, ...) |
| 332 | { |
| 333 | int len = (PAGE_SIZE - 1) - s->len; |
| 334 | va_list ap; |
| 335 | int ret; |
| 336 | |
| 337 | if (!len) |
| 338 | return 0; |
| 339 | |
| 340 | va_start(ap, fmt); |
| 341 | ret = vsnprintf(s->buffer + s->len, len, fmt, ap); |
| 342 | va_end(ap); |
| 343 | |
| 344 | /* If we can't write it all, don't bother writing anything */ |
| 345 | if (ret >= len) |
| 346 | return 0; |
| 347 | |
| 348 | s->len += ret; |
| 349 | |
| 350 | return len; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * trace_seq_puts - trace sequence printing of simple string |
| 355 | * @s: trace sequence descriptor |
| 356 | * @str: simple string to record |
| 357 | * |
| 358 | * The tracer may use either the sequence operations or its own |
| 359 | * copy to user routines. This function records a simple string |
| 360 | * into a special buffer (@s) for later retrieval by a sequencer |
| 361 | * or other mechanism. |
| 362 | */ |
| 363 | static int |
| 364 | trace_seq_puts(struct trace_seq *s, const char *str) |
| 365 | { |
| 366 | int len = strlen(str); |
| 367 | |
| 368 | if (len > ((PAGE_SIZE - 1) - s->len)) |
| 369 | return 0; |
| 370 | |
| 371 | memcpy(s->buffer + s->len, str, len); |
| 372 | s->len += len; |
| 373 | |
| 374 | return len; |
| 375 | } |
| 376 | |
| 377 | static int |
| 378 | trace_seq_putc(struct trace_seq *s, unsigned char c) |
| 379 | { |
| 380 | if (s->len >= (PAGE_SIZE - 1)) |
| 381 | return 0; |
| 382 | |
| 383 | s->buffer[s->len++] = c; |
| 384 | |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | static int |
| 389 | trace_seq_putmem(struct trace_seq *s, void *mem, size_t len) |
| 390 | { |
| 391 | if (len > ((PAGE_SIZE - 1) - s->len)) |
| 392 | return 0; |
| 393 | |
| 394 | memcpy(s->buffer + s->len, mem, len); |
| 395 | s->len += len; |
| 396 | |
| 397 | return len; |
| 398 | } |
| 399 | |
| 400 | #define HEX_CHARS 17 |
| 401 | static const char hex2asc[] = "0123456789abcdef"; |
| 402 | |
| 403 | static int |
| 404 | trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len) |
| 405 | { |
| 406 | unsigned char hex[HEX_CHARS]; |
| 407 | unsigned char *data = mem; |
| 408 | unsigned char byte; |
| 409 | int i, j; |
| 410 | |
| 411 | BUG_ON(len >= HEX_CHARS); |
| 412 | |
| 413 | #ifdef __BIG_ENDIAN |
| 414 | for (i = 0, j = 0; i < len; i++) { |
| 415 | #else |
| 416 | for (i = len-1, j = 0; i >= 0; i--) { |
| 417 | #endif |
| 418 | byte = data[i]; |
| 419 | |
| 420 | hex[j++] = hex2asc[byte & 0x0f]; |
| 421 | hex[j++] = hex2asc[byte >> 4]; |
| 422 | } |
| 423 | hex[j++] = ' '; |
| 424 | |
| 425 | return trace_seq_putmem(s, hex, j); |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | trace_seq_reset(struct trace_seq *s) |
| 430 | { |
| 431 | s->len = 0; |
| 432 | s->readpos = 0; |
| 433 | } |
| 434 | |
| 435 | ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt) |
| 436 | { |
| 437 | int len; |
| 438 | int ret; |
| 439 | |
| 440 | if (s->len <= s->readpos) |
| 441 | return -EBUSY; |
| 442 | |
| 443 | len = s->len - s->readpos; |
| 444 | if (cnt > len) |
| 445 | cnt = len; |
| 446 | ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt); |
| 447 | if (ret) |
| 448 | return -EFAULT; |
| 449 | |
| 450 | s->readpos += len; |
| 451 | return cnt; |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | trace_print_seq(struct seq_file *m, struct trace_seq *s) |
| 456 | { |
| 457 | int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len; |
| 458 | |
| 459 | s->buffer[len] = 0; |
| 460 | seq_puts(m, s->buffer); |
| 461 | |
| 462 | trace_seq_reset(s); |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * flip the trace buffers between two trace descriptors. |
| 467 | * This usually is the buffers between the global_trace and |
| 468 | * the max_tr to record a snapshot of a current trace. |
| 469 | * |
| 470 | * The ftrace_max_lock must be held. |
| 471 | */ |
| 472 | static void |
| 473 | flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2) |
| 474 | { |
| 475 | struct list_head flip_pages; |
| 476 | |
| 477 | INIT_LIST_HEAD(&flip_pages); |
| 478 | |
| 479 | memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx, |
| 480 | sizeof(struct trace_array_cpu) - |
| 481 | offsetof(struct trace_array_cpu, trace_head_idx)); |
| 482 | |
| 483 | check_pages(tr1); |
| 484 | check_pages(tr2); |
| 485 | list_splice_init(&tr1->trace_pages, &flip_pages); |
| 486 | list_splice_init(&tr2->trace_pages, &tr1->trace_pages); |
| 487 | list_splice_init(&flip_pages, &tr2->trace_pages); |
| 488 | BUG_ON(!list_empty(&flip_pages)); |
| 489 | check_pages(tr1); |
| 490 | check_pages(tr2); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * update_max_tr - snapshot all trace buffers from global_trace to max_tr |
| 495 | * @tr: tracer |
| 496 | * @tsk: the task with the latency |
| 497 | * @cpu: The cpu that initiated the trace. |
| 498 | * |
| 499 | * Flip the buffers between the @tr and the max_tr and record information |
| 500 | * about which task was the cause of this latency. |
| 501 | */ |
| 502 | void |
| 503 | update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 504 | { |
| 505 | struct trace_array_cpu *data; |
| 506 | int i; |
| 507 | |
| 508 | WARN_ON_ONCE(!irqs_disabled()); |
| 509 | __raw_spin_lock(&ftrace_max_lock); |
| 510 | /* clear out all the previous traces */ |
| 511 | for_each_tracing_cpu(i) { |
| 512 | data = tr->data[i]; |
| 513 | flip_trace(max_tr.data[i], data); |
| 514 | tracing_reset(data); |
| 515 | } |
| 516 | |
| 517 | __update_max_tr(tr, tsk, cpu); |
| 518 | __raw_spin_unlock(&ftrace_max_lock); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * update_max_tr_single - only copy one trace over, and reset the rest |
| 523 | * @tr - tracer |
| 524 | * @tsk - task with the latency |
| 525 | * @cpu - the cpu of the buffer to copy. |
| 526 | * |
| 527 | * Flip the trace of a single CPU buffer between the @tr and the max_tr. |
| 528 | */ |
| 529 | void |
| 530 | update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| 531 | { |
| 532 | struct trace_array_cpu *data = tr->data[cpu]; |
| 533 | int i; |
| 534 | |
| 535 | WARN_ON_ONCE(!irqs_disabled()); |
| 536 | __raw_spin_lock(&ftrace_max_lock); |
| 537 | for_each_tracing_cpu(i) |
| 538 | tracing_reset(max_tr.data[i]); |
| 539 | |
| 540 | flip_trace(max_tr.data[cpu], data); |
| 541 | tracing_reset(data); |
| 542 | |
| 543 | __update_max_tr(tr, tsk, cpu); |
| 544 | __raw_spin_unlock(&ftrace_max_lock); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * register_tracer - register a tracer with the ftrace system. |
| 549 | * @type - the plugin for the tracer |
| 550 | * |
| 551 | * Register a new plugin tracer. |
| 552 | */ |
| 553 | int register_tracer(struct tracer *type) |
| 554 | { |
| 555 | struct tracer *t; |
| 556 | int len; |
| 557 | int ret = 0; |
| 558 | |
| 559 | if (!type->name) { |
| 560 | pr_info("Tracer must have a name\n"); |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | mutex_lock(&trace_types_lock); |
| 565 | for (t = trace_types; t; t = t->next) { |
| 566 | if (strcmp(type->name, t->name) == 0) { |
| 567 | /* already found */ |
| 568 | pr_info("Trace %s already registered\n", |
| 569 | type->name); |
| 570 | ret = -1; |
| 571 | goto out; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
| 576 | if (type->selftest) { |
| 577 | struct tracer *saved_tracer = current_trace; |
| 578 | struct trace_array_cpu *data; |
| 579 | struct trace_array *tr = &global_trace; |
| 580 | int saved_ctrl = tr->ctrl; |
| 581 | int i; |
| 582 | /* |
| 583 | * Run a selftest on this tracer. |
| 584 | * Here we reset the trace buffer, and set the current |
| 585 | * tracer to be this tracer. The tracer can then run some |
| 586 | * internal tracing to verify that everything is in order. |
| 587 | * If we fail, we do not register this tracer. |
| 588 | */ |
| 589 | for_each_tracing_cpu(i) { |
| 590 | data = tr->data[i]; |
| 591 | if (!head_page(data)) |
| 592 | continue; |
| 593 | tracing_reset(data); |
| 594 | } |
| 595 | current_trace = type; |
| 596 | tr->ctrl = 0; |
| 597 | /* the test is responsible for initializing and enabling */ |
| 598 | pr_info("Testing tracer %s: ", type->name); |
| 599 | ret = type->selftest(type, tr); |
| 600 | /* the test is responsible for resetting too */ |
| 601 | current_trace = saved_tracer; |
| 602 | tr->ctrl = saved_ctrl; |
| 603 | if (ret) { |
| 604 | printk(KERN_CONT "FAILED!\n"); |
| 605 | goto out; |
| 606 | } |
| 607 | /* Only reset on passing, to avoid touching corrupted buffers */ |
| 608 | for_each_tracing_cpu(i) { |
| 609 | data = tr->data[i]; |
| 610 | if (!head_page(data)) |
| 611 | continue; |
| 612 | tracing_reset(data); |
| 613 | } |
| 614 | printk(KERN_CONT "PASSED\n"); |
| 615 | } |
| 616 | #endif |
| 617 | |
| 618 | type->next = trace_types; |
| 619 | trace_types = type; |
| 620 | len = strlen(type->name); |
| 621 | if (len > max_tracer_type_len) |
| 622 | max_tracer_type_len = len; |
| 623 | |
| 624 | out: |
| 625 | mutex_unlock(&trace_types_lock); |
| 626 | |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | void unregister_tracer(struct tracer *type) |
| 631 | { |
| 632 | struct tracer **t; |
| 633 | int len; |
| 634 | |
| 635 | mutex_lock(&trace_types_lock); |
| 636 | for (t = &trace_types; *t; t = &(*t)->next) { |
| 637 | if (*t == type) |
| 638 | goto found; |
| 639 | } |
| 640 | pr_info("Trace %s not registered\n", type->name); |
| 641 | goto out; |
| 642 | |
| 643 | found: |
| 644 | *t = (*t)->next; |
| 645 | if (strlen(type->name) != max_tracer_type_len) |
| 646 | goto out; |
| 647 | |
| 648 | max_tracer_type_len = 0; |
| 649 | for (t = &trace_types; *t; t = &(*t)->next) { |
| 650 | len = strlen((*t)->name); |
| 651 | if (len > max_tracer_type_len) |
| 652 | max_tracer_type_len = len; |
| 653 | } |
| 654 | out: |
| 655 | mutex_unlock(&trace_types_lock); |
| 656 | } |
| 657 | |
| 658 | void tracing_reset(struct trace_array_cpu *data) |
| 659 | { |
| 660 | data->trace_idx = 0; |
| 661 | data->overrun = 0; |
| 662 | data->trace_head = data->trace_tail = head_page(data); |
| 663 | data->trace_head_idx = 0; |
| 664 | data->trace_tail_idx = 0; |
| 665 | } |
| 666 | |
| 667 | #define SAVED_CMDLINES 128 |
| 668 | static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; |
| 669 | static unsigned map_cmdline_to_pid[SAVED_CMDLINES]; |
| 670 | static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN]; |
| 671 | static int cmdline_idx; |
| 672 | static DEFINE_SPINLOCK(trace_cmdline_lock); |
| 673 | |
| 674 | /* temporary disable recording */ |
| 675 | atomic_t trace_record_cmdline_disabled __read_mostly; |
| 676 | |
| 677 | static void trace_init_cmdlines(void) |
| 678 | { |
| 679 | memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline)); |
| 680 | memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid)); |
| 681 | cmdline_idx = 0; |
| 682 | } |
| 683 | |
| 684 | void trace_stop_cmdline_recording(void); |
| 685 | |
| 686 | static void trace_save_cmdline(struct task_struct *tsk) |
| 687 | { |
| 688 | unsigned map; |
| 689 | unsigned idx; |
| 690 | |
| 691 | if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT)) |
| 692 | return; |
| 693 | |
| 694 | /* |
| 695 | * It's not the end of the world if we don't get |
| 696 | * the lock, but we also don't want to spin |
| 697 | * nor do we want to disable interrupts, |
| 698 | * so if we miss here, then better luck next time. |
| 699 | */ |
| 700 | if (!spin_trylock(&trace_cmdline_lock)) |
| 701 | return; |
| 702 | |
| 703 | idx = map_pid_to_cmdline[tsk->pid]; |
| 704 | if (idx >= SAVED_CMDLINES) { |
| 705 | idx = (cmdline_idx + 1) % SAVED_CMDLINES; |
| 706 | |
| 707 | map = map_cmdline_to_pid[idx]; |
| 708 | if (map <= PID_MAX_DEFAULT) |
| 709 | map_pid_to_cmdline[map] = (unsigned)-1; |
| 710 | |
| 711 | map_pid_to_cmdline[tsk->pid] = idx; |
| 712 | |
| 713 | cmdline_idx = idx; |
| 714 | } |
| 715 | |
| 716 | memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN); |
| 717 | |
| 718 | spin_unlock(&trace_cmdline_lock); |
| 719 | } |
| 720 | |
| 721 | static char *trace_find_cmdline(int pid) |
| 722 | { |
| 723 | char *cmdline = "<...>"; |
| 724 | unsigned map; |
| 725 | |
| 726 | if (!pid) |
| 727 | return "<idle>"; |
| 728 | |
| 729 | if (pid > PID_MAX_DEFAULT) |
| 730 | goto out; |
| 731 | |
| 732 | map = map_pid_to_cmdline[pid]; |
| 733 | if (map >= SAVED_CMDLINES) |
| 734 | goto out; |
| 735 | |
| 736 | cmdline = saved_cmdlines[map]; |
| 737 | |
| 738 | out: |
| 739 | return cmdline; |
| 740 | } |
| 741 | |
| 742 | void tracing_record_cmdline(struct task_struct *tsk) |
| 743 | { |
| 744 | if (atomic_read(&trace_record_cmdline_disabled)) |
| 745 | return; |
| 746 | |
| 747 | trace_save_cmdline(tsk); |
| 748 | } |
| 749 | |
| 750 | static inline struct list_head * |
| 751 | trace_next_list(struct trace_array_cpu *data, struct list_head *next) |
| 752 | { |
| 753 | /* |
| 754 | * Roundrobin - but skip the head (which is not a real page): |
| 755 | */ |
| 756 | next = next->next; |
| 757 | if (unlikely(next == &data->trace_pages)) |
| 758 | next = next->next; |
| 759 | BUG_ON(next == &data->trace_pages); |
| 760 | |
| 761 | return next; |
| 762 | } |
| 763 | |
| 764 | static inline void * |
| 765 | trace_next_page(struct trace_array_cpu *data, void *addr) |
| 766 | { |
| 767 | struct list_head *next; |
| 768 | struct page *page; |
| 769 | |
| 770 | page = virt_to_page(addr); |
| 771 | |
| 772 | next = trace_next_list(data, &page->lru); |
| 773 | page = list_entry(next, struct page, lru); |
| 774 | |
| 775 | return page_address(page); |
| 776 | } |
| 777 | |
| 778 | static inline struct trace_entry * |
| 779 | tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data) |
| 780 | { |
| 781 | unsigned long idx, idx_next; |
| 782 | struct trace_entry *entry; |
| 783 | |
| 784 | data->trace_idx++; |
| 785 | idx = data->trace_head_idx; |
| 786 | idx_next = idx + 1; |
| 787 | |
| 788 | BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE); |
| 789 | |
| 790 | entry = data->trace_head + idx * TRACE_ENTRY_SIZE; |
| 791 | |
| 792 | if (unlikely(idx_next >= ENTRIES_PER_PAGE)) { |
| 793 | data->trace_head = trace_next_page(data, data->trace_head); |
| 794 | idx_next = 0; |
| 795 | } |
| 796 | |
| 797 | if (data->trace_head == data->trace_tail && |
| 798 | idx_next == data->trace_tail_idx) { |
| 799 | /* overrun */ |
| 800 | data->overrun++; |
| 801 | data->trace_tail_idx++; |
| 802 | if (data->trace_tail_idx >= ENTRIES_PER_PAGE) { |
| 803 | data->trace_tail = |
| 804 | trace_next_page(data, data->trace_tail); |
| 805 | data->trace_tail_idx = 0; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | data->trace_head_idx = idx_next; |
| 810 | |
| 811 | return entry; |
| 812 | } |
| 813 | |
| 814 | static inline void |
| 815 | tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags) |
| 816 | { |
| 817 | struct task_struct *tsk = current; |
| 818 | unsigned long pc; |
| 819 | |
| 820 | pc = preempt_count(); |
| 821 | |
| 822 | entry->field.preempt_count = pc & 0xff; |
| 823 | entry->field.pid = (tsk) ? tsk->pid : 0; |
| 824 | entry->field.t = ftrace_now(raw_smp_processor_id()); |
| 825 | entry->field.flags = |
| 826 | (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
| 827 | ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | |
| 828 | ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) | |
| 829 | (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0); |
| 830 | } |
| 831 | |
| 832 | void |
| 833 | trace_function(struct trace_array *tr, struct trace_array_cpu *data, |
| 834 | unsigned long ip, unsigned long parent_ip, unsigned long flags) |
| 835 | { |
| 836 | struct trace_entry *entry; |
| 837 | unsigned long irq_flags; |
| 838 | |
| 839 | raw_local_irq_save(irq_flags); |
| 840 | __raw_spin_lock(&data->lock); |
| 841 | entry = tracing_get_trace_entry(tr, data); |
| 842 | tracing_generic_entry_update(entry, flags); |
| 843 | entry->type = TRACE_FN; |
| 844 | entry->field.fn.ip = ip; |
| 845 | entry->field.fn.parent_ip = parent_ip; |
| 846 | __raw_spin_unlock(&data->lock); |
| 847 | raw_local_irq_restore(irq_flags); |
| 848 | } |
| 849 | |
| 850 | void |
| 851 | ftrace(struct trace_array *tr, struct trace_array_cpu *data, |
| 852 | unsigned long ip, unsigned long parent_ip, unsigned long flags) |
| 853 | { |
| 854 | if (likely(!atomic_read(&data->disabled))) |
| 855 | trace_function(tr, data, ip, parent_ip, flags); |
| 856 | } |
| 857 | |
| 858 | #ifdef CONFIG_MMIOTRACE |
| 859 | void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data, |
| 860 | struct mmiotrace_rw *rw) |
| 861 | { |
| 862 | struct trace_entry *entry; |
| 863 | unsigned long irq_flags; |
| 864 | |
| 865 | raw_local_irq_save(irq_flags); |
| 866 | __raw_spin_lock(&data->lock); |
| 867 | |
| 868 | entry = tracing_get_trace_entry(tr, data); |
| 869 | tracing_generic_entry_update(entry, 0); |
| 870 | entry->type = TRACE_MMIO_RW; |
| 871 | entry->field.mmiorw = *rw; |
| 872 | |
| 873 | __raw_spin_unlock(&data->lock); |
| 874 | raw_local_irq_restore(irq_flags); |
| 875 | |
| 876 | trace_wake_up(); |
| 877 | } |
| 878 | |
| 879 | void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data, |
| 880 | struct mmiotrace_map *map) |
| 881 | { |
| 882 | struct trace_entry *entry; |
| 883 | unsigned long irq_flags; |
| 884 | |
| 885 | raw_local_irq_save(irq_flags); |
| 886 | __raw_spin_lock(&data->lock); |
| 887 | |
| 888 | entry = tracing_get_trace_entry(tr, data); |
| 889 | tracing_generic_entry_update(entry, 0); |
| 890 | entry->type = TRACE_MMIO_MAP; |
| 891 | entry->field.mmiomap = *map; |
| 892 | |
| 893 | __raw_spin_unlock(&data->lock); |
| 894 | raw_local_irq_restore(irq_flags); |
| 895 | |
| 896 | trace_wake_up(); |
| 897 | } |
| 898 | #endif |
| 899 | |
| 900 | void __trace_stack(struct trace_array *tr, |
| 901 | struct trace_array_cpu *data, |
| 902 | unsigned long flags, |
| 903 | int skip) |
| 904 | { |
| 905 | struct trace_entry *entry; |
| 906 | struct stack_trace trace; |
| 907 | |
| 908 | if (!(trace_flags & TRACE_ITER_STACKTRACE)) |
| 909 | return; |
| 910 | |
| 911 | entry = tracing_get_trace_entry(tr, data); |
| 912 | tracing_generic_entry_update(entry, flags); |
| 913 | entry->type = TRACE_STACK; |
| 914 | |
| 915 | memset(&entry->field.stack, 0, sizeof(entry->field.stack)); |
| 916 | |
| 917 | trace.nr_entries = 0; |
| 918 | trace.max_entries = FTRACE_STACK_ENTRIES; |
| 919 | trace.skip = skip; |
| 920 | trace.entries = entry->field.stack.caller; |
| 921 | |
| 922 | save_stack_trace(&trace); |
| 923 | } |
| 924 | |
| 925 | void |
| 926 | __trace_special(void *__tr, void *__data, |
| 927 | unsigned long arg1, unsigned long arg2, unsigned long arg3) |
| 928 | { |
| 929 | struct trace_array_cpu *data = __data; |
| 930 | struct trace_array *tr = __tr; |
| 931 | struct trace_entry *entry; |
| 932 | unsigned long irq_flags; |
| 933 | |
| 934 | raw_local_irq_save(irq_flags); |
| 935 | __raw_spin_lock(&data->lock); |
| 936 | entry = tracing_get_trace_entry(tr, data); |
| 937 | tracing_generic_entry_update(entry, 0); |
| 938 | entry->type = TRACE_SPECIAL; |
| 939 | entry->field.special.arg1 = arg1; |
| 940 | entry->field.special.arg2 = arg2; |
| 941 | entry->field.special.arg3 = arg3; |
| 942 | __trace_stack(tr, data, irq_flags, 4); |
| 943 | __raw_spin_unlock(&data->lock); |
| 944 | raw_local_irq_restore(irq_flags); |
| 945 | |
| 946 | trace_wake_up(); |
| 947 | } |
| 948 | |
| 949 | void |
| 950 | tracing_sched_switch_trace(struct trace_array *tr, |
| 951 | struct trace_array_cpu *data, |
| 952 | struct task_struct *prev, |
| 953 | struct task_struct *next, |
| 954 | unsigned long flags) |
| 955 | { |
| 956 | struct trace_entry *entry; |
| 957 | unsigned long irq_flags; |
| 958 | |
| 959 | raw_local_irq_save(irq_flags); |
| 960 | __raw_spin_lock(&data->lock); |
| 961 | entry = tracing_get_trace_entry(tr, data); |
| 962 | tracing_generic_entry_update(entry, flags); |
| 963 | entry->type = TRACE_CTX; |
| 964 | entry->field.ctx.prev_pid = prev->pid; |
| 965 | entry->field.ctx.prev_prio = prev->prio; |
| 966 | entry->field.ctx.prev_state = prev->state; |
| 967 | entry->field.ctx.next_pid = next->pid; |
| 968 | entry->field.ctx.next_prio = next->prio; |
| 969 | entry->field.ctx.next_state = next->state; |
| 970 | __trace_stack(tr, data, flags, 5); |
| 971 | __raw_spin_unlock(&data->lock); |
| 972 | raw_local_irq_restore(irq_flags); |
| 973 | } |
| 974 | |
| 975 | void |
| 976 | tracing_sched_wakeup_trace(struct trace_array *tr, |
| 977 | struct trace_array_cpu *data, |
| 978 | struct task_struct *wakee, |
| 979 | struct task_struct *curr, |
| 980 | unsigned long flags) |
| 981 | { |
| 982 | struct trace_entry *entry; |
| 983 | unsigned long irq_flags; |
| 984 | |
| 985 | raw_local_irq_save(irq_flags); |
| 986 | __raw_spin_lock(&data->lock); |
| 987 | entry = tracing_get_trace_entry(tr, data); |
| 988 | tracing_generic_entry_update(entry, flags); |
| 989 | entry->type = TRACE_WAKE; |
| 990 | entry->field.ctx.prev_pid = curr->pid; |
| 991 | entry->field.ctx.prev_prio = curr->prio; |
| 992 | entry->field.ctx.prev_state = curr->state; |
| 993 | entry->field.ctx.next_pid = wakee->pid; |
| 994 | entry->field.ctx.next_prio = wakee->prio; |
| 995 | entry->field.ctx.next_state = wakee->state; |
| 996 | __trace_stack(tr, data, flags, 6); |
| 997 | __raw_spin_unlock(&data->lock); |
| 998 | raw_local_irq_restore(irq_flags); |
| 999 | |
| 1000 | trace_wake_up(); |
| 1001 | } |
| 1002 | |
| 1003 | void |
| 1004 | ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) |
| 1005 | { |
| 1006 | struct trace_array *tr = &global_trace; |
| 1007 | struct trace_array_cpu *data; |
| 1008 | unsigned long flags; |
| 1009 | long disabled; |
| 1010 | int cpu; |
| 1011 | |
| 1012 | if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl) |
| 1013 | return; |
| 1014 | |
| 1015 | local_irq_save(flags); |
| 1016 | cpu = raw_smp_processor_id(); |
| 1017 | data = tr->data[cpu]; |
| 1018 | disabled = atomic_inc_return(&data->disabled); |
| 1019 | |
| 1020 | if (likely(disabled == 1)) |
| 1021 | __trace_special(tr, data, arg1, arg2, arg3); |
| 1022 | |
| 1023 | atomic_dec(&data->disabled); |
| 1024 | local_irq_restore(flags); |
| 1025 | } |
| 1026 | |
| 1027 | #ifdef CONFIG_FTRACE |
| 1028 | static void |
| 1029 | function_trace_call(unsigned long ip, unsigned long parent_ip) |
| 1030 | { |
| 1031 | struct trace_array *tr = &global_trace; |
| 1032 | struct trace_array_cpu *data; |
| 1033 | unsigned long flags; |
| 1034 | long disabled; |
| 1035 | int cpu; |
| 1036 | |
| 1037 | if (unlikely(!ftrace_function_enabled)) |
| 1038 | return; |
| 1039 | |
| 1040 | if (skip_trace(ip)) |
| 1041 | return; |
| 1042 | |
| 1043 | local_irq_save(flags); |
| 1044 | cpu = raw_smp_processor_id(); |
| 1045 | data = tr->data[cpu]; |
| 1046 | disabled = atomic_inc_return(&data->disabled); |
| 1047 | |
| 1048 | if (likely(disabled == 1)) |
| 1049 | trace_function(tr, data, ip, parent_ip, flags); |
| 1050 | |
| 1051 | atomic_dec(&data->disabled); |
| 1052 | local_irq_restore(flags); |
| 1053 | } |
| 1054 | |
| 1055 | static struct ftrace_ops trace_ops __read_mostly = |
| 1056 | { |
| 1057 | .func = function_trace_call, |
| 1058 | }; |
| 1059 | |
| 1060 | void tracing_start_function_trace(void) |
| 1061 | { |
| 1062 | ftrace_function_enabled = 0; |
| 1063 | register_ftrace_function(&trace_ops); |
| 1064 | if (tracer_enabled) |
| 1065 | ftrace_function_enabled = 1; |
| 1066 | } |
| 1067 | |
| 1068 | void tracing_stop_function_trace(void) |
| 1069 | { |
| 1070 | ftrace_function_enabled = 0; |
| 1071 | unregister_ftrace_function(&trace_ops); |
| 1072 | } |
| 1073 | #endif |
| 1074 | |
| 1075 | enum trace_file_type { |
| 1076 | TRACE_FILE_LAT_FMT = 1, |
| 1077 | }; |
| 1078 | |
| 1079 | /* Return the current entry. */ |
| 1080 | static struct trace_entry * |
| 1081 | trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data, |
| 1082 | struct trace_iterator *iter, int cpu) |
| 1083 | { |
| 1084 | struct page *page; |
| 1085 | struct trace_entry *array; |
| 1086 | |
| 1087 | if (iter->next_idx[cpu] >= tr->entries || |
| 1088 | iter->next_idx[cpu] >= data->trace_idx || |
| 1089 | (data->trace_head == data->trace_tail && |
| 1090 | data->trace_head_idx == data->trace_tail_idx)) |
| 1091 | return NULL; |
| 1092 | |
| 1093 | if (!iter->next_page[cpu]) { |
| 1094 | /* Initialize the iterator for this cpu trace buffer */ |
| 1095 | WARN_ON(!data->trace_tail); |
| 1096 | page = virt_to_page(data->trace_tail); |
| 1097 | iter->next_page[cpu] = &page->lru; |
| 1098 | iter->next_page_idx[cpu] = data->trace_tail_idx; |
| 1099 | } |
| 1100 | |
| 1101 | page = list_entry(iter->next_page[cpu], struct page, lru); |
| 1102 | BUG_ON(&data->trace_pages == &page->lru); |
| 1103 | |
| 1104 | array = page_address(page); |
| 1105 | |
| 1106 | WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE); |
| 1107 | return &array[iter->next_page_idx[cpu]]; |
| 1108 | } |
| 1109 | |
| 1110 | /* Increment the index counter of an iterator by one */ |
| 1111 | static void trace_iterator_increment(struct trace_iterator *iter, int cpu) |
| 1112 | { |
| 1113 | iter->idx++; |
| 1114 | iter->next_idx[cpu]++; |
| 1115 | iter->next_page_idx[cpu]++; |
| 1116 | |
| 1117 | if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) { |
| 1118 | struct trace_array_cpu *data = iter->tr->data[cpu]; |
| 1119 | |
| 1120 | iter->next_page_idx[cpu] = 0; |
| 1121 | iter->next_page[cpu] = |
| 1122 | trace_next_list(data, iter->next_page[cpu]); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | static struct trace_entry * |
| 1127 | trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data, |
| 1128 | struct trace_iterator *iter, int cpu) |
| 1129 | { |
| 1130 | struct list_head *next_page; |
| 1131 | struct trace_entry *ent; |
| 1132 | int idx, next_idx, next_page_idx; |
| 1133 | |
| 1134 | ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu); |
| 1135 | |
| 1136 | if (likely(!ent || ent->type != TRACE_CONT)) |
| 1137 | return ent; |
| 1138 | |
| 1139 | /* save the iterator details */ |
| 1140 | idx = iter->idx; |
| 1141 | next_idx = iter->next_idx[cpu]; |
| 1142 | next_page_idx = iter->next_page_idx[cpu]; |
| 1143 | next_page = iter->next_page[cpu]; |
| 1144 | |
| 1145 | /* find a real entry */ |
| 1146 | do { |
| 1147 | trace_iterator_increment(iter, cpu); |
| 1148 | ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu); |
| 1149 | } while (ent && ent->type != TRACE_CONT); |
| 1150 | |
| 1151 | /* reset the iterator */ |
| 1152 | iter->idx = idx; |
| 1153 | iter->next_idx[cpu] = next_idx; |
| 1154 | iter->next_page_idx[cpu] = next_page_idx; |
| 1155 | iter->next_page[cpu] = next_page; |
| 1156 | |
| 1157 | return ent; |
| 1158 | } |
| 1159 | |
| 1160 | static struct trace_entry * |
| 1161 | __find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc) |
| 1162 | { |
| 1163 | struct trace_array *tr = iter->tr; |
| 1164 | struct trace_entry *ent, *next = NULL; |
| 1165 | int next_cpu = -1; |
| 1166 | int cpu; |
| 1167 | |
| 1168 | for_each_tracing_cpu(cpu) { |
| 1169 | if (!head_page(tr->data[cpu])) |
| 1170 | continue; |
| 1171 | |
| 1172 | ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu); |
| 1173 | |
| 1174 | if (ent && ent->type == TRACE_CONT) { |
| 1175 | struct trace_array_cpu *data = tr->data[cpu]; |
| 1176 | |
| 1177 | if (!inc) |
| 1178 | ent = trace_entry_next(tr, data, iter, cpu); |
| 1179 | else { |
| 1180 | while (ent && ent->type == TRACE_CONT) { |
| 1181 | trace_iterator_increment(iter, cpu); |
| 1182 | ent = trace_entry_idx(tr, tr->data[cpu], |
| 1183 | iter, cpu); |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Pick the entry with the smallest timestamp: |
| 1190 | */ |
| 1191 | if (ent && (!next || ent->field.t < next->field.t)) { |
| 1192 | next = ent; |
| 1193 | next_cpu = cpu; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | if (ent_cpu) |
| 1198 | *ent_cpu = next_cpu; |
| 1199 | |
| 1200 | return next; |
| 1201 | } |
| 1202 | |
| 1203 | /* Find the next real entry, without updating the iterator itself */ |
| 1204 | static struct trace_entry * |
| 1205 | find_next_entry(struct trace_iterator *iter, int *ent_cpu) |
| 1206 | { |
| 1207 | return __find_next_entry(iter, ent_cpu, 0); |
| 1208 | } |
| 1209 | |
| 1210 | /* Find the next real entry, and increment the iterator to the next entry */ |
| 1211 | static void *find_next_entry_inc(struct trace_iterator *iter) |
| 1212 | { |
| 1213 | struct trace_entry *next; |
| 1214 | int next_cpu = -1; |
| 1215 | |
| 1216 | next = __find_next_entry(iter, &next_cpu, 1); |
| 1217 | |
| 1218 | iter->prev_ent = iter->ent; |
| 1219 | iter->prev_cpu = iter->cpu; |
| 1220 | |
| 1221 | iter->ent = next; |
| 1222 | iter->cpu = next_cpu; |
| 1223 | |
| 1224 | if (next) |
| 1225 | trace_iterator_increment(iter, iter->cpu); |
| 1226 | |
| 1227 | return next ? iter : NULL; |
| 1228 | } |
| 1229 | |
| 1230 | static void trace_consume(struct trace_iterator *iter) |
| 1231 | { |
| 1232 | struct trace_array_cpu *data = iter->tr->data[iter->cpu]; |
| 1233 | struct trace_entry *ent; |
| 1234 | |
| 1235 | again: |
| 1236 | data->trace_tail_idx++; |
| 1237 | if (data->trace_tail_idx >= ENTRIES_PER_PAGE) { |
| 1238 | data->trace_tail = trace_next_page(data, data->trace_tail); |
| 1239 | data->trace_tail_idx = 0; |
| 1240 | } |
| 1241 | |
| 1242 | /* Check if we empty it, then reset the index */ |
| 1243 | if (data->trace_head == data->trace_tail && |
| 1244 | data->trace_head_idx == data->trace_tail_idx) |
| 1245 | data->trace_idx = 0; |
| 1246 | |
| 1247 | ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu], |
| 1248 | iter, iter->cpu); |
| 1249 | if (ent && ent->type == TRACE_CONT) |
| 1250 | goto again; |
| 1251 | } |
| 1252 | |
| 1253 | static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
| 1254 | { |
| 1255 | struct trace_iterator *iter = m->private; |
| 1256 | int i = (int)*pos; |
| 1257 | void *ent; |
| 1258 | |
| 1259 | (*pos)++; |
| 1260 | |
| 1261 | /* can't go backwards */ |
| 1262 | if (iter->idx > i) |
| 1263 | return NULL; |
| 1264 | |
| 1265 | if (iter->idx < 0) |
| 1266 | ent = find_next_entry_inc(iter); |
| 1267 | else |
| 1268 | ent = iter; |
| 1269 | |
| 1270 | while (ent && iter->idx < i) |
| 1271 | ent = find_next_entry_inc(iter); |
| 1272 | |
| 1273 | iter->pos = *pos; |
| 1274 | |
| 1275 | return ent; |
| 1276 | } |
| 1277 | |
| 1278 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 1279 | { |
| 1280 | struct trace_iterator *iter = m->private; |
| 1281 | void *p = NULL; |
| 1282 | loff_t l = 0; |
| 1283 | int i; |
| 1284 | |
| 1285 | mutex_lock(&trace_types_lock); |
| 1286 | |
| 1287 | if (!current_trace || current_trace != iter->trace) { |
| 1288 | mutex_unlock(&trace_types_lock); |
| 1289 | return NULL; |
| 1290 | } |
| 1291 | |
| 1292 | atomic_inc(&trace_record_cmdline_disabled); |
| 1293 | |
| 1294 | /* let the tracer grab locks here if needed */ |
| 1295 | if (current_trace->start) |
| 1296 | current_trace->start(iter); |
| 1297 | |
| 1298 | if (*pos != iter->pos) { |
| 1299 | iter->ent = NULL; |
| 1300 | iter->cpu = 0; |
| 1301 | iter->idx = -1; |
| 1302 | iter->prev_ent = NULL; |
| 1303 | iter->prev_cpu = -1; |
| 1304 | |
| 1305 | for_each_tracing_cpu(i) { |
| 1306 | iter->next_idx[i] = 0; |
| 1307 | iter->next_page[i] = NULL; |
| 1308 | } |
| 1309 | |
| 1310 | for (p = iter; p && l < *pos; p = s_next(m, p, &l)) |
| 1311 | ; |
| 1312 | |
| 1313 | } else { |
| 1314 | l = *pos - 1; |
| 1315 | p = s_next(m, p, &l); |
| 1316 | } |
| 1317 | |
| 1318 | return p; |
| 1319 | } |
| 1320 | |
| 1321 | static void s_stop(struct seq_file *m, void *p) |
| 1322 | { |
| 1323 | struct trace_iterator *iter = m->private; |
| 1324 | |
| 1325 | atomic_dec(&trace_record_cmdline_disabled); |
| 1326 | |
| 1327 | /* let the tracer release locks here if needed */ |
| 1328 | if (current_trace && current_trace == iter->trace && iter->trace->stop) |
| 1329 | iter->trace->stop(iter); |
| 1330 | |
| 1331 | mutex_unlock(&trace_types_lock); |
| 1332 | } |
| 1333 | |
| 1334 | #define KRETPROBE_MSG "[unknown/kretprobe'd]" |
| 1335 | |
| 1336 | #ifdef CONFIG_KRETPROBES |
| 1337 | static inline int kretprobed(unsigned long addr) |
| 1338 | { |
| 1339 | return addr == (unsigned long)kretprobe_trampoline; |
| 1340 | } |
| 1341 | #else |
| 1342 | static inline int kretprobed(unsigned long addr) |
| 1343 | { |
| 1344 | return 0; |
| 1345 | } |
| 1346 | #endif /* CONFIG_KRETPROBES */ |
| 1347 | |
| 1348 | static int |
| 1349 | seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address) |
| 1350 | { |
| 1351 | #ifdef CONFIG_KALLSYMS |
| 1352 | char str[KSYM_SYMBOL_LEN]; |
| 1353 | |
| 1354 | kallsyms_lookup(address, NULL, NULL, NULL, str); |
| 1355 | |
| 1356 | return trace_seq_printf(s, fmt, str); |
| 1357 | #endif |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
| 1361 | static int |
| 1362 | seq_print_sym_offset(struct trace_seq *s, const char *fmt, |
| 1363 | unsigned long address) |
| 1364 | { |
| 1365 | #ifdef CONFIG_KALLSYMS |
| 1366 | char str[KSYM_SYMBOL_LEN]; |
| 1367 | |
| 1368 | sprint_symbol(str, address); |
| 1369 | return trace_seq_printf(s, fmt, str); |
| 1370 | #endif |
| 1371 | return 1; |
| 1372 | } |
| 1373 | |
| 1374 | #ifndef CONFIG_64BIT |
| 1375 | # define IP_FMT "%08lx" |
| 1376 | #else |
| 1377 | # define IP_FMT "%016lx" |
| 1378 | #endif |
| 1379 | |
| 1380 | static int |
| 1381 | seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags) |
| 1382 | { |
| 1383 | int ret; |
| 1384 | |
| 1385 | if (!ip) |
| 1386 | return trace_seq_printf(s, "0"); |
| 1387 | |
| 1388 | if (sym_flags & TRACE_ITER_SYM_OFFSET) |
| 1389 | ret = seq_print_sym_offset(s, "%s", ip); |
| 1390 | else |
| 1391 | ret = seq_print_sym_short(s, "%s", ip); |
| 1392 | |
| 1393 | if (!ret) |
| 1394 | return 0; |
| 1395 | |
| 1396 | if (sym_flags & TRACE_ITER_SYM_ADDR) |
| 1397 | ret = trace_seq_printf(s, " <" IP_FMT ">", ip); |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
| 1401 | static void print_lat_help_header(struct seq_file *m) |
| 1402 | { |
| 1403 | seq_puts(m, "# _------=> CPU# \n"); |
| 1404 | seq_puts(m, "# / _-----=> irqs-off \n"); |
| 1405 | seq_puts(m, "# | / _----=> need-resched \n"); |
| 1406 | seq_puts(m, "# || / _---=> hardirq/softirq \n"); |
| 1407 | seq_puts(m, "# ||| / _--=> preempt-depth \n"); |
| 1408 | seq_puts(m, "# |||| / \n"); |
| 1409 | seq_puts(m, "# ||||| delay \n"); |
| 1410 | seq_puts(m, "# cmd pid ||||| time | caller \n"); |
| 1411 | seq_puts(m, "# \\ / ||||| \\ | / \n"); |
| 1412 | } |
| 1413 | |
| 1414 | static void print_func_help_header(struct seq_file *m) |
| 1415 | { |
| 1416 | seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n"); |
| 1417 | seq_puts(m, "# | | | | |\n"); |
| 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | static void |
| 1422 | print_trace_header(struct seq_file *m, struct trace_iterator *iter) |
| 1423 | { |
| 1424 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
| 1425 | struct trace_array *tr = iter->tr; |
| 1426 | struct trace_array_cpu *data = tr->data[tr->cpu]; |
| 1427 | struct tracer *type = current_trace; |
| 1428 | unsigned long total = 0; |
| 1429 | unsigned long entries = 0; |
| 1430 | int cpu; |
| 1431 | const char *name = "preemption"; |
| 1432 | |
| 1433 | if (type) |
| 1434 | name = type->name; |
| 1435 | |
| 1436 | for_each_tracing_cpu(cpu) { |
| 1437 | if (head_page(tr->data[cpu])) { |
| 1438 | total += tr->data[cpu]->trace_idx; |
| 1439 | if (tr->data[cpu]->trace_idx > tr->entries) |
| 1440 | entries += tr->entries; |
| 1441 | else |
| 1442 | entries += tr->data[cpu]->trace_idx; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | seq_printf(m, "%s latency trace v1.1.5 on %s\n", |
| 1447 | name, UTS_RELEASE); |
| 1448 | seq_puts(m, "-----------------------------------" |
| 1449 | "---------------------------------\n"); |
| 1450 | seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |" |
| 1451 | " (M:%s VP:%d, KP:%d, SP:%d HP:%d", |
| 1452 | nsecs_to_usecs(data->saved_latency), |
| 1453 | entries, |
| 1454 | total, |
| 1455 | tr->cpu, |
| 1456 | #if defined(CONFIG_PREEMPT_NONE) |
| 1457 | "server", |
| 1458 | #elif defined(CONFIG_PREEMPT_VOLUNTARY) |
| 1459 | "desktop", |
| 1460 | #elif defined(CONFIG_PREEMPT) |
| 1461 | "preempt", |
| 1462 | #else |
| 1463 | "unknown", |
| 1464 | #endif |
| 1465 | /* These are reserved for later use */ |
| 1466 | 0, 0, 0, 0); |
| 1467 | #ifdef CONFIG_SMP |
| 1468 | seq_printf(m, " #P:%d)\n", num_online_cpus()); |
| 1469 | #else |
| 1470 | seq_puts(m, ")\n"); |
| 1471 | #endif |
| 1472 | seq_puts(m, " -----------------\n"); |
| 1473 | seq_printf(m, " | task: %.16s-%d " |
| 1474 | "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n", |
| 1475 | data->comm, data->pid, data->uid, data->nice, |
| 1476 | data->policy, data->rt_priority); |
| 1477 | seq_puts(m, " -----------------\n"); |
| 1478 | |
| 1479 | if (data->critical_start) { |
| 1480 | seq_puts(m, " => started at: "); |
| 1481 | seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags); |
| 1482 | trace_print_seq(m, &iter->seq); |
| 1483 | seq_puts(m, "\n => ended at: "); |
| 1484 | seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags); |
| 1485 | trace_print_seq(m, &iter->seq); |
| 1486 | seq_puts(m, "\n"); |
| 1487 | } |
| 1488 | |
| 1489 | seq_puts(m, "\n"); |
| 1490 | } |
| 1491 | |
| 1492 | static void |
| 1493 | lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu) |
| 1494 | { |
| 1495 | struct trace_field *field = &entry->field; |
| 1496 | int hardirq, softirq; |
| 1497 | char *comm; |
| 1498 | |
| 1499 | comm = trace_find_cmdline(field->pid); |
| 1500 | |
| 1501 | trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid); |
| 1502 | trace_seq_printf(s, "%d", cpu); |
| 1503 | trace_seq_printf(s, "%c%c", |
| 1504 | (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.', |
| 1505 | ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.')); |
| 1506 | |
| 1507 | hardirq = field->flags & TRACE_FLAG_HARDIRQ; |
| 1508 | softirq = field->flags & TRACE_FLAG_SOFTIRQ; |
| 1509 | if (hardirq && softirq) { |
| 1510 | trace_seq_putc(s, 'H'); |
| 1511 | } else { |
| 1512 | if (hardirq) { |
| 1513 | trace_seq_putc(s, 'h'); |
| 1514 | } else { |
| 1515 | if (softirq) |
| 1516 | trace_seq_putc(s, 's'); |
| 1517 | else |
| 1518 | trace_seq_putc(s, '.'); |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | if (field->preempt_count) |
| 1523 | trace_seq_printf(s, "%x", field->preempt_count); |
| 1524 | else |
| 1525 | trace_seq_puts(s, "."); |
| 1526 | } |
| 1527 | |
| 1528 | unsigned long preempt_mark_thresh = 100; |
| 1529 | |
| 1530 | static void |
| 1531 | lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs, |
| 1532 | unsigned long rel_usecs) |
| 1533 | { |
| 1534 | trace_seq_printf(s, " %4lldus", abs_usecs); |
| 1535 | if (rel_usecs > preempt_mark_thresh) |
| 1536 | trace_seq_puts(s, "!: "); |
| 1537 | else if (rel_usecs > 1) |
| 1538 | trace_seq_puts(s, "+: "); |
| 1539 | else |
| 1540 | trace_seq_puts(s, " : "); |
| 1541 | } |
| 1542 | |
| 1543 | static const char state_to_char[] = TASK_STATE_TO_CHAR_STR; |
| 1544 | |
| 1545 | static void |
| 1546 | trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter) |
| 1547 | { |
| 1548 | struct trace_array *tr = iter->tr; |
| 1549 | struct trace_array_cpu *data = tr->data[iter->cpu]; |
| 1550 | struct trace_entry *ent; |
| 1551 | |
| 1552 | ent = trace_entry_idx(tr, data, iter, iter->cpu); |
| 1553 | if (!ent || ent->type != TRACE_CONT) { |
| 1554 | trace_seq_putc(s, '\n'); |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | do { |
| 1559 | trace_seq_printf(s, "%s", ent->cont.buf); |
| 1560 | trace_iterator_increment(iter, iter->cpu); |
| 1561 | ent = trace_entry_idx(tr, data, iter, iter->cpu); |
| 1562 | } while (ent && ent->type == TRACE_CONT); |
| 1563 | } |
| 1564 | |
| 1565 | static int |
| 1566 | print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) |
| 1567 | { |
| 1568 | struct trace_seq *s = &iter->seq; |
| 1569 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
| 1570 | struct trace_entry *next_entry = find_next_entry(iter, NULL); |
| 1571 | unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE); |
| 1572 | struct trace_entry *entry = iter->ent; |
| 1573 | struct trace_field *field = &entry->field; |
| 1574 | unsigned long abs_usecs; |
| 1575 | unsigned long rel_usecs; |
| 1576 | char *comm; |
| 1577 | int S, T; |
| 1578 | int i; |
| 1579 | unsigned state; |
| 1580 | |
| 1581 | if (!next_entry) |
| 1582 | next_entry = entry; |
| 1583 | |
| 1584 | if (entry->type == TRACE_CONT) |
| 1585 | return 1; |
| 1586 | |
| 1587 | rel_usecs = ns2usecs(next_entry->field.t - entry->field.t); |
| 1588 | abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start); |
| 1589 | |
| 1590 | if (verbose) { |
| 1591 | comm = trace_find_cmdline(field->pid); |
| 1592 | trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]" |
| 1593 | " %ld.%03ldms (+%ld.%03ldms): ", |
| 1594 | comm, |
| 1595 | field->pid, cpu, field->flags, |
| 1596 | field->preempt_count, trace_idx, |
| 1597 | ns2usecs(field->t), |
| 1598 | abs_usecs/1000, |
| 1599 | abs_usecs % 1000, rel_usecs/1000, |
| 1600 | rel_usecs % 1000); |
| 1601 | } else { |
| 1602 | lat_print_generic(s, entry, cpu); |
| 1603 | lat_print_timestamp(s, abs_usecs, rel_usecs); |
| 1604 | } |
| 1605 | switch (entry->type) { |
| 1606 | case TRACE_FN: |
| 1607 | seq_print_ip_sym(s, field->fn.ip, sym_flags); |
| 1608 | trace_seq_puts(s, " ("); |
| 1609 | if (kretprobed(field->fn.parent_ip)) |
| 1610 | trace_seq_puts(s, KRETPROBE_MSG); |
| 1611 | else |
| 1612 | seq_print_ip_sym(s, field->fn.parent_ip, sym_flags); |
| 1613 | trace_seq_puts(s, ")\n"); |
| 1614 | break; |
| 1615 | case TRACE_CTX: |
| 1616 | case TRACE_WAKE: |
| 1617 | T = field->ctx.next_state < sizeof(state_to_char) ? |
| 1618 | state_to_char[field->ctx.next_state] : 'X'; |
| 1619 | |
| 1620 | state = field->ctx.prev_state ? |
| 1621 | __ffs(field->ctx.prev_state) + 1 : 0; |
| 1622 | S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X'; |
| 1623 | comm = trace_find_cmdline(field->ctx.next_pid); |
| 1624 | trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n", |
| 1625 | field->ctx.prev_pid, |
| 1626 | field->ctx.prev_prio, |
| 1627 | S, entry->type == TRACE_CTX ? "==>" : " +", |
| 1628 | field->ctx.next_pid, |
| 1629 | field->ctx.next_prio, |
| 1630 | T, comm); |
| 1631 | break; |
| 1632 | case TRACE_SPECIAL: |
| 1633 | trace_seq_printf(s, "# %ld %ld %ld\n", |
| 1634 | field->special.arg1, |
| 1635 | field->special.arg2, |
| 1636 | field->special.arg3); |
| 1637 | break; |
| 1638 | case TRACE_STACK: |
| 1639 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { |
| 1640 | if (i) |
| 1641 | trace_seq_puts(s, " <= "); |
| 1642 | seq_print_ip_sym(s, field->stack.caller[i], sym_flags); |
| 1643 | } |
| 1644 | trace_seq_puts(s, "\n"); |
| 1645 | break; |
| 1646 | case TRACE_PRINT: |
| 1647 | seq_print_ip_sym(s, field->print.ip, sym_flags); |
| 1648 | trace_seq_printf(s, ": %s", field->print.buf); |
| 1649 | if (field->flags && TRACE_FLAG_CONT) |
| 1650 | trace_seq_print_cont(s, iter); |
| 1651 | break; |
| 1652 | default: |
| 1653 | trace_seq_printf(s, "Unknown type %d\n", entry->type); |
| 1654 | } |
| 1655 | return 1; |
| 1656 | } |
| 1657 | |
| 1658 | static int print_trace_fmt(struct trace_iterator *iter) |
| 1659 | { |
| 1660 | struct trace_seq *s = &iter->seq; |
| 1661 | unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK); |
| 1662 | struct trace_entry *entry; |
| 1663 | struct trace_field *field; |
| 1664 | unsigned long usec_rem; |
| 1665 | unsigned long long t; |
| 1666 | unsigned long secs; |
| 1667 | char *comm; |
| 1668 | int ret; |
| 1669 | int S, T; |
| 1670 | int i; |
| 1671 | |
| 1672 | entry = iter->ent; |
| 1673 | |
| 1674 | if (entry->type == TRACE_CONT) |
| 1675 | return 1; |
| 1676 | |
| 1677 | field = &entry->field; |
| 1678 | |
| 1679 | comm = trace_find_cmdline(iter->ent->field.pid); |
| 1680 | |
| 1681 | t = ns2usecs(field->t); |
| 1682 | usec_rem = do_div(t, 1000000ULL); |
| 1683 | secs = (unsigned long)t; |
| 1684 | |
| 1685 | ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid); |
| 1686 | if (!ret) |
| 1687 | return 0; |
| 1688 | ret = trace_seq_printf(s, "[%02d] ", iter->cpu); |
| 1689 | if (!ret) |
| 1690 | return 0; |
| 1691 | ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem); |
| 1692 | if (!ret) |
| 1693 | return 0; |
| 1694 | |
| 1695 | switch (entry->type) { |
| 1696 | case TRACE_FN: |
| 1697 | ret = seq_print_ip_sym(s, field->fn.ip, sym_flags); |
| 1698 | if (!ret) |
| 1699 | return 0; |
| 1700 | if ((sym_flags & TRACE_ITER_PRINT_PARENT) && |
| 1701 | field->fn.parent_ip) { |
| 1702 | ret = trace_seq_printf(s, " <-"); |
| 1703 | if (!ret) |
| 1704 | return 0; |
| 1705 | if (kretprobed(field->fn.parent_ip)) |
| 1706 | ret = trace_seq_puts(s, KRETPROBE_MSG); |
| 1707 | else |
| 1708 | ret = seq_print_ip_sym(s, |
| 1709 | field->fn.parent_ip, |
| 1710 | sym_flags); |
| 1711 | if (!ret) |
| 1712 | return 0; |
| 1713 | } |
| 1714 | ret = trace_seq_printf(s, "\n"); |
| 1715 | if (!ret) |
| 1716 | return 0; |
| 1717 | break; |
| 1718 | case TRACE_CTX: |
| 1719 | case TRACE_WAKE: |
| 1720 | S = field->ctx.prev_state < sizeof(state_to_char) ? |
| 1721 | state_to_char[field->ctx.prev_state] : 'X'; |
| 1722 | T = field->ctx.next_state < sizeof(state_to_char) ? |
| 1723 | state_to_char[field->ctx.next_state] : 'X'; |
| 1724 | ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n", |
| 1725 | field->ctx.prev_pid, |
| 1726 | field->ctx.prev_prio, |
| 1727 | S, |
| 1728 | entry->type == TRACE_CTX ? "==>" : " +", |
| 1729 | field->ctx.next_pid, |
| 1730 | field->ctx.next_prio, |
| 1731 | T); |
| 1732 | if (!ret) |
| 1733 | return 0; |
| 1734 | break; |
| 1735 | case TRACE_SPECIAL: |
| 1736 | ret = trace_seq_printf(s, "# %ld %ld %ld\n", |
| 1737 | field->special.arg1, |
| 1738 | field->special.arg2, |
| 1739 | field->special.arg3); |
| 1740 | if (!ret) |
| 1741 | return 0; |
| 1742 | break; |
| 1743 | case TRACE_STACK: |
| 1744 | for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { |
| 1745 | if (i) { |
| 1746 | ret = trace_seq_puts(s, " <= "); |
| 1747 | if (!ret) |
| 1748 | return 0; |
| 1749 | } |
| 1750 | ret = seq_print_ip_sym(s, field->stack.caller[i], |
| 1751 | sym_flags); |
| 1752 | if (!ret) |
| 1753 | return 0; |
| 1754 | } |
| 1755 | ret = trace_seq_puts(s, "\n"); |
| 1756 | if (!ret) |
| 1757 | return 0; |
| 1758 | break; |
| 1759 | case TRACE_PRINT: |
| 1760 | seq_print_ip_sym(s, field->print.ip, sym_flags); |
| 1761 | trace_seq_printf(s, ": %s", field->print.buf); |
| 1762 | if (field->flags && TRACE_FLAG_CONT) |
| 1763 | trace_seq_print_cont(s, iter); |
| 1764 | break; |
| 1765 | } |
| 1766 | return 1; |
| 1767 | } |
| 1768 | |
| 1769 | static int print_raw_fmt(struct trace_iterator *iter) |
| 1770 | { |
| 1771 | struct trace_seq *s = &iter->seq; |
| 1772 | struct trace_entry *entry; |
| 1773 | struct trace_field *field; |
| 1774 | int ret; |
| 1775 | int S, T; |
| 1776 | |
| 1777 | entry = iter->ent; |
| 1778 | |
| 1779 | if (entry->type == TRACE_CONT) |
| 1780 | return 1; |
| 1781 | |
| 1782 | field = &entry->field; |
| 1783 | |
| 1784 | ret = trace_seq_printf(s, "%d %d %llu ", |
| 1785 | field->pid, iter->cpu, field->t); |
| 1786 | if (!ret) |
| 1787 | return 0; |
| 1788 | |
| 1789 | switch (entry->type) { |
| 1790 | case TRACE_FN: |
| 1791 | ret = trace_seq_printf(s, "%x %x\n", |
| 1792 | field->fn.ip, |
| 1793 | field->fn.parent_ip); |
| 1794 | if (!ret) |
| 1795 | return 0; |
| 1796 | break; |
| 1797 | case TRACE_CTX: |
| 1798 | case TRACE_WAKE: |
| 1799 | S = field->ctx.prev_state < sizeof(state_to_char) ? |
| 1800 | state_to_char[field->ctx.prev_state] : 'X'; |
| 1801 | T = field->ctx.next_state < sizeof(state_to_char) ? |
| 1802 | state_to_char[field->ctx.next_state] : 'X'; |
| 1803 | if (entry->type == TRACE_WAKE) |
| 1804 | S = '+'; |
| 1805 | ret = trace_seq_printf(s, "%d %d %c %d %d %c\n", |
| 1806 | field->ctx.prev_pid, |
| 1807 | field->ctx.prev_prio, |
| 1808 | S, |
| 1809 | field->ctx.next_pid, |
| 1810 | field->ctx.next_prio, |
| 1811 | T); |
| 1812 | if (!ret) |
| 1813 | return 0; |
| 1814 | break; |
| 1815 | case TRACE_SPECIAL: |
| 1816 | case TRACE_STACK: |
| 1817 | ret = trace_seq_printf(s, "# %ld %ld %ld\n", |
| 1818 | field->special.arg1, |
| 1819 | field->special.arg2, |
| 1820 | field->special.arg3); |
| 1821 | if (!ret) |
| 1822 | return 0; |
| 1823 | break; |
| 1824 | case TRACE_PRINT: |
| 1825 | trace_seq_printf(s, "# %lx %s", |
| 1826 | field->print.ip, field->print.buf); |
| 1827 | if (field->flags && TRACE_FLAG_CONT) |
| 1828 | trace_seq_print_cont(s, iter); |
| 1829 | break; |
| 1830 | } |
| 1831 | return 1; |
| 1832 | } |
| 1833 | |
| 1834 | #define SEQ_PUT_FIELD_RET(s, x) \ |
| 1835 | do { \ |
| 1836 | if (!trace_seq_putmem(s, &(x), sizeof(x))) \ |
| 1837 | return 0; \ |
| 1838 | } while (0) |
| 1839 | |
| 1840 | #define SEQ_PUT_HEX_FIELD_RET(s, x) \ |
| 1841 | do { \ |
| 1842 | if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \ |
| 1843 | return 0; \ |
| 1844 | } while (0) |
| 1845 | |
| 1846 | static int print_hex_fmt(struct trace_iterator *iter) |
| 1847 | { |
| 1848 | struct trace_seq *s = &iter->seq; |
| 1849 | unsigned char newline = '\n'; |
| 1850 | struct trace_entry *entry; |
| 1851 | struct trace_field *field; |
| 1852 | int S, T; |
| 1853 | |
| 1854 | entry = iter->ent; |
| 1855 | |
| 1856 | if (entry->type == TRACE_CONT) |
| 1857 | return 1; |
| 1858 | |
| 1859 | field = &entry->field; |
| 1860 | |
| 1861 | SEQ_PUT_HEX_FIELD_RET(s, field->pid); |
| 1862 | SEQ_PUT_HEX_FIELD_RET(s, iter->cpu); |
| 1863 | SEQ_PUT_HEX_FIELD_RET(s, field->t); |
| 1864 | |
| 1865 | switch (entry->type) { |
| 1866 | case TRACE_FN: |
| 1867 | SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip); |
| 1868 | SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip); |
| 1869 | break; |
| 1870 | case TRACE_CTX: |
| 1871 | case TRACE_WAKE: |
| 1872 | S = field->ctx.prev_state < sizeof(state_to_char) ? |
| 1873 | state_to_char[field->ctx.prev_state] : 'X'; |
| 1874 | T = field->ctx.next_state < sizeof(state_to_char) ? |
| 1875 | state_to_char[field->ctx.next_state] : 'X'; |
| 1876 | if (entry->type == TRACE_WAKE) |
| 1877 | S = '+'; |
| 1878 | SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid); |
| 1879 | SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio); |
| 1880 | SEQ_PUT_HEX_FIELD_RET(s, S); |
| 1881 | SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid); |
| 1882 | SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio); |
| 1883 | SEQ_PUT_HEX_FIELD_RET(s, T); |
| 1884 | break; |
| 1885 | case TRACE_SPECIAL: |
| 1886 | case TRACE_STACK: |
| 1887 | SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1); |
| 1888 | SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2); |
| 1889 | SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3); |
| 1890 | break; |
| 1891 | } |
| 1892 | SEQ_PUT_FIELD_RET(s, newline); |
| 1893 | |
| 1894 | return 1; |
| 1895 | } |
| 1896 | |
| 1897 | static int print_bin_fmt(struct trace_iterator *iter) |
| 1898 | { |
| 1899 | struct trace_seq *s = &iter->seq; |
| 1900 | struct trace_entry *entry; |
| 1901 | struct trace_field *field; |
| 1902 | |
| 1903 | entry = iter->ent; |
| 1904 | |
| 1905 | if (entry->type == TRACE_CONT) |
| 1906 | return 1; |
| 1907 | |
| 1908 | field = &entry->field; |
| 1909 | |
| 1910 | SEQ_PUT_FIELD_RET(s, field->pid); |
| 1911 | SEQ_PUT_FIELD_RET(s, field->cpu); |
| 1912 | SEQ_PUT_FIELD_RET(s, field->t); |
| 1913 | |
| 1914 | switch (entry->type) { |
| 1915 | case TRACE_FN: |
| 1916 | SEQ_PUT_FIELD_RET(s, field->fn.ip); |
| 1917 | SEQ_PUT_FIELD_RET(s, field->fn.parent_ip); |
| 1918 | break; |
| 1919 | case TRACE_CTX: |
| 1920 | SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid); |
| 1921 | SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio); |
| 1922 | SEQ_PUT_FIELD_RET(s, field->ctx.prev_state); |
| 1923 | SEQ_PUT_FIELD_RET(s, field->ctx.next_pid); |
| 1924 | SEQ_PUT_FIELD_RET(s, field->ctx.next_prio); |
| 1925 | SEQ_PUT_FIELD_RET(s, field->ctx.next_state); |
| 1926 | break; |
| 1927 | case TRACE_SPECIAL: |
| 1928 | case TRACE_STACK: |
| 1929 | SEQ_PUT_FIELD_RET(s, field->special.arg1); |
| 1930 | SEQ_PUT_FIELD_RET(s, field->special.arg2); |
| 1931 | SEQ_PUT_FIELD_RET(s, field->special.arg3); |
| 1932 | break; |
| 1933 | } |
| 1934 | return 1; |
| 1935 | } |
| 1936 | |
| 1937 | static int trace_empty(struct trace_iterator *iter) |
| 1938 | { |
| 1939 | struct trace_array_cpu *data; |
| 1940 | int cpu; |
| 1941 | |
| 1942 | for_each_tracing_cpu(cpu) { |
| 1943 | data = iter->tr->data[cpu]; |
| 1944 | |
| 1945 | if (head_page(data) && data->trace_idx && |
| 1946 | (data->trace_tail != data->trace_head || |
| 1947 | data->trace_tail_idx != data->trace_head_idx)) |
| 1948 | return 0; |
| 1949 | } |
| 1950 | return 1; |
| 1951 | } |
| 1952 | |
| 1953 | static int print_trace_line(struct trace_iterator *iter) |
| 1954 | { |
| 1955 | if (iter->trace && iter->trace->print_line) |
| 1956 | return iter->trace->print_line(iter); |
| 1957 | |
| 1958 | if (trace_flags & TRACE_ITER_BIN) |
| 1959 | return print_bin_fmt(iter); |
| 1960 | |
| 1961 | if (trace_flags & TRACE_ITER_HEX) |
| 1962 | return print_hex_fmt(iter); |
| 1963 | |
| 1964 | if (trace_flags & TRACE_ITER_RAW) |
| 1965 | return print_raw_fmt(iter); |
| 1966 | |
| 1967 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) |
| 1968 | return print_lat_fmt(iter, iter->idx, iter->cpu); |
| 1969 | |
| 1970 | return print_trace_fmt(iter); |
| 1971 | } |
| 1972 | |
| 1973 | static int s_show(struct seq_file *m, void *v) |
| 1974 | { |
| 1975 | struct trace_iterator *iter = v; |
| 1976 | |
| 1977 | if (iter->ent == NULL) { |
| 1978 | if (iter->tr) { |
| 1979 | seq_printf(m, "# tracer: %s\n", iter->trace->name); |
| 1980 | seq_puts(m, "#\n"); |
| 1981 | } |
| 1982 | if (iter->iter_flags & TRACE_FILE_LAT_FMT) { |
| 1983 | /* print nothing if the buffers are empty */ |
| 1984 | if (trace_empty(iter)) |
| 1985 | return 0; |
| 1986 | print_trace_header(m, iter); |
| 1987 | if (!(trace_flags & TRACE_ITER_VERBOSE)) |
| 1988 | print_lat_help_header(m); |
| 1989 | } else { |
| 1990 | if (!(trace_flags & TRACE_ITER_VERBOSE)) |
| 1991 | print_func_help_header(m); |
| 1992 | } |
| 1993 | } else { |
| 1994 | print_trace_line(iter); |
| 1995 | trace_print_seq(m, &iter->seq); |
| 1996 | } |
| 1997 | |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
| 2001 | static struct seq_operations tracer_seq_ops = { |
| 2002 | .start = s_start, |
| 2003 | .next = s_next, |
| 2004 | .stop = s_stop, |
| 2005 | .show = s_show, |
| 2006 | }; |
| 2007 | |
| 2008 | static struct trace_iterator * |
| 2009 | __tracing_open(struct inode *inode, struct file *file, int *ret) |
| 2010 | { |
| 2011 | struct trace_iterator *iter; |
| 2012 | |
| 2013 | if (tracing_disabled) { |
| 2014 | *ret = -ENODEV; |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | |
| 2018 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 2019 | if (!iter) { |
| 2020 | *ret = -ENOMEM; |
| 2021 | goto out; |
| 2022 | } |
| 2023 | |
| 2024 | mutex_lock(&trace_types_lock); |
| 2025 | if (current_trace && current_trace->print_max) |
| 2026 | iter->tr = &max_tr; |
| 2027 | else |
| 2028 | iter->tr = inode->i_private; |
| 2029 | iter->trace = current_trace; |
| 2030 | iter->pos = -1; |
| 2031 | |
| 2032 | /* TODO stop tracer */ |
| 2033 | *ret = seq_open(file, &tracer_seq_ops); |
| 2034 | if (!*ret) { |
| 2035 | struct seq_file *m = file->private_data; |
| 2036 | m->private = iter; |
| 2037 | |
| 2038 | /* stop the trace while dumping */ |
| 2039 | if (iter->tr->ctrl) { |
| 2040 | tracer_enabled = 0; |
| 2041 | ftrace_function_enabled = 0; |
| 2042 | } |
| 2043 | |
| 2044 | if (iter->trace && iter->trace->open) |
| 2045 | iter->trace->open(iter); |
| 2046 | } else { |
| 2047 | kfree(iter); |
| 2048 | iter = NULL; |
| 2049 | } |
| 2050 | mutex_unlock(&trace_types_lock); |
| 2051 | |
| 2052 | out: |
| 2053 | return iter; |
| 2054 | } |
| 2055 | |
| 2056 | int tracing_open_generic(struct inode *inode, struct file *filp) |
| 2057 | { |
| 2058 | if (tracing_disabled) |
| 2059 | return -ENODEV; |
| 2060 | |
| 2061 | filp->private_data = inode->i_private; |
| 2062 | return 0; |
| 2063 | } |
| 2064 | |
| 2065 | int tracing_release(struct inode *inode, struct file *file) |
| 2066 | { |
| 2067 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 2068 | struct trace_iterator *iter = m->private; |
| 2069 | |
| 2070 | mutex_lock(&trace_types_lock); |
| 2071 | if (iter->trace && iter->trace->close) |
| 2072 | iter->trace->close(iter); |
| 2073 | |
| 2074 | /* reenable tracing if it was previously enabled */ |
| 2075 | if (iter->tr->ctrl) { |
| 2076 | tracer_enabled = 1; |
| 2077 | /* |
| 2078 | * It is safe to enable function tracing even if it |
| 2079 | * isn't used |
| 2080 | */ |
| 2081 | ftrace_function_enabled = 1; |
| 2082 | } |
| 2083 | mutex_unlock(&trace_types_lock); |
| 2084 | |
| 2085 | seq_release(inode, file); |
| 2086 | kfree(iter); |
| 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | static int tracing_open(struct inode *inode, struct file *file) |
| 2091 | { |
| 2092 | int ret; |
| 2093 | |
| 2094 | __tracing_open(inode, file, &ret); |
| 2095 | |
| 2096 | return ret; |
| 2097 | } |
| 2098 | |
| 2099 | static int tracing_lt_open(struct inode *inode, struct file *file) |
| 2100 | { |
| 2101 | struct trace_iterator *iter; |
| 2102 | int ret; |
| 2103 | |
| 2104 | iter = __tracing_open(inode, file, &ret); |
| 2105 | |
| 2106 | if (!ret) |
| 2107 | iter->iter_flags |= TRACE_FILE_LAT_FMT; |
| 2108 | |
| 2109 | return ret; |
| 2110 | } |
| 2111 | |
| 2112 | |
| 2113 | static void * |
| 2114 | t_next(struct seq_file *m, void *v, loff_t *pos) |
| 2115 | { |
| 2116 | struct tracer *t = m->private; |
| 2117 | |
| 2118 | (*pos)++; |
| 2119 | |
| 2120 | if (t) |
| 2121 | t = t->next; |
| 2122 | |
| 2123 | m->private = t; |
| 2124 | |
| 2125 | return t; |
| 2126 | } |
| 2127 | |
| 2128 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 2129 | { |
| 2130 | struct tracer *t = m->private; |
| 2131 | loff_t l = 0; |
| 2132 | |
| 2133 | mutex_lock(&trace_types_lock); |
| 2134 | for (; t && l < *pos; t = t_next(m, t, &l)) |
| 2135 | ; |
| 2136 | |
| 2137 | return t; |
| 2138 | } |
| 2139 | |
| 2140 | static void t_stop(struct seq_file *m, void *p) |
| 2141 | { |
| 2142 | mutex_unlock(&trace_types_lock); |
| 2143 | } |
| 2144 | |
| 2145 | static int t_show(struct seq_file *m, void *v) |
| 2146 | { |
| 2147 | struct tracer *t = v; |
| 2148 | |
| 2149 | if (!t) |
| 2150 | return 0; |
| 2151 | |
| 2152 | seq_printf(m, "%s", t->name); |
| 2153 | if (t->next) |
| 2154 | seq_putc(m, ' '); |
| 2155 | else |
| 2156 | seq_putc(m, '\n'); |
| 2157 | |
| 2158 | return 0; |
| 2159 | } |
| 2160 | |
| 2161 | static struct seq_operations show_traces_seq_ops = { |
| 2162 | .start = t_start, |
| 2163 | .next = t_next, |
| 2164 | .stop = t_stop, |
| 2165 | .show = t_show, |
| 2166 | }; |
| 2167 | |
| 2168 | static int show_traces_open(struct inode *inode, struct file *file) |
| 2169 | { |
| 2170 | int ret; |
| 2171 | |
| 2172 | if (tracing_disabled) |
| 2173 | return -ENODEV; |
| 2174 | |
| 2175 | ret = seq_open(file, &show_traces_seq_ops); |
| 2176 | if (!ret) { |
| 2177 | struct seq_file *m = file->private_data; |
| 2178 | m->private = trace_types; |
| 2179 | } |
| 2180 | |
| 2181 | return ret; |
| 2182 | } |
| 2183 | |
| 2184 | static struct file_operations tracing_fops = { |
| 2185 | .open = tracing_open, |
| 2186 | .read = seq_read, |
| 2187 | .llseek = seq_lseek, |
| 2188 | .release = tracing_release, |
| 2189 | }; |
| 2190 | |
| 2191 | static struct file_operations tracing_lt_fops = { |
| 2192 | .open = tracing_lt_open, |
| 2193 | .read = seq_read, |
| 2194 | .llseek = seq_lseek, |
| 2195 | .release = tracing_release, |
| 2196 | }; |
| 2197 | |
| 2198 | static struct file_operations show_traces_fops = { |
| 2199 | .open = show_traces_open, |
| 2200 | .read = seq_read, |
| 2201 | .release = seq_release, |
| 2202 | }; |
| 2203 | |
| 2204 | /* |
| 2205 | * Only trace on a CPU if the bitmask is set: |
| 2206 | */ |
| 2207 | static cpumask_t tracing_cpumask = CPU_MASK_ALL; |
| 2208 | |
| 2209 | /* |
| 2210 | * When tracing/tracing_cpu_mask is modified then this holds |
| 2211 | * the new bitmask we are about to install: |
| 2212 | */ |
| 2213 | static cpumask_t tracing_cpumask_new; |
| 2214 | |
| 2215 | /* |
| 2216 | * The tracer itself will not take this lock, but still we want |
| 2217 | * to provide a consistent cpumask to user-space: |
| 2218 | */ |
| 2219 | static DEFINE_MUTEX(tracing_cpumask_update_lock); |
| 2220 | |
| 2221 | /* |
| 2222 | * Temporary storage for the character representation of the |
| 2223 | * CPU bitmask (and one more byte for the newline): |
| 2224 | */ |
| 2225 | static char mask_str[NR_CPUS + 1]; |
| 2226 | |
| 2227 | static ssize_t |
| 2228 | tracing_cpumask_read(struct file *filp, char __user *ubuf, |
| 2229 | size_t count, loff_t *ppos) |
| 2230 | { |
| 2231 | int len; |
| 2232 | |
| 2233 | mutex_lock(&tracing_cpumask_update_lock); |
| 2234 | |
| 2235 | len = cpumask_scnprintf(mask_str, count, tracing_cpumask); |
| 2236 | if (count - len < 2) { |
| 2237 | count = -EINVAL; |
| 2238 | goto out_err; |
| 2239 | } |
| 2240 | len += sprintf(mask_str + len, "\n"); |
| 2241 | count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1); |
| 2242 | |
| 2243 | out_err: |
| 2244 | mutex_unlock(&tracing_cpumask_update_lock); |
| 2245 | |
| 2246 | return count; |
| 2247 | } |
| 2248 | |
| 2249 | static ssize_t |
| 2250 | tracing_cpumask_write(struct file *filp, const char __user *ubuf, |
| 2251 | size_t count, loff_t *ppos) |
| 2252 | { |
| 2253 | int err, cpu; |
| 2254 | |
| 2255 | mutex_lock(&tracing_cpumask_update_lock); |
| 2256 | err = cpumask_parse_user(ubuf, count, tracing_cpumask_new); |
| 2257 | if (err) |
| 2258 | goto err_unlock; |
| 2259 | |
| 2260 | raw_local_irq_disable(); |
| 2261 | __raw_spin_lock(&ftrace_max_lock); |
| 2262 | for_each_tracing_cpu(cpu) { |
| 2263 | /* |
| 2264 | * Increase/decrease the disabled counter if we are |
| 2265 | * about to flip a bit in the cpumask: |
| 2266 | */ |
| 2267 | if (cpu_isset(cpu, tracing_cpumask) && |
| 2268 | !cpu_isset(cpu, tracing_cpumask_new)) { |
| 2269 | atomic_inc(&global_trace.data[cpu]->disabled); |
| 2270 | } |
| 2271 | if (!cpu_isset(cpu, tracing_cpumask) && |
| 2272 | cpu_isset(cpu, tracing_cpumask_new)) { |
| 2273 | atomic_dec(&global_trace.data[cpu]->disabled); |
| 2274 | } |
| 2275 | } |
| 2276 | __raw_spin_unlock(&ftrace_max_lock); |
| 2277 | raw_local_irq_enable(); |
| 2278 | |
| 2279 | tracing_cpumask = tracing_cpumask_new; |
| 2280 | |
| 2281 | mutex_unlock(&tracing_cpumask_update_lock); |
| 2282 | |
| 2283 | return count; |
| 2284 | |
| 2285 | err_unlock: |
| 2286 | mutex_unlock(&tracing_cpumask_update_lock); |
| 2287 | |
| 2288 | return err; |
| 2289 | } |
| 2290 | |
| 2291 | static struct file_operations tracing_cpumask_fops = { |
| 2292 | .open = tracing_open_generic, |
| 2293 | .read = tracing_cpumask_read, |
| 2294 | .write = tracing_cpumask_write, |
| 2295 | }; |
| 2296 | |
| 2297 | static ssize_t |
| 2298 | tracing_iter_ctrl_read(struct file *filp, char __user *ubuf, |
| 2299 | size_t cnt, loff_t *ppos) |
| 2300 | { |
| 2301 | char *buf; |
| 2302 | int r = 0; |
| 2303 | int len = 0; |
| 2304 | int i; |
| 2305 | |
| 2306 | /* calulate max size */ |
| 2307 | for (i = 0; trace_options[i]; i++) { |
| 2308 | len += strlen(trace_options[i]); |
| 2309 | len += 3; /* "no" and space */ |
| 2310 | } |
| 2311 | |
| 2312 | /* +2 for \n and \0 */ |
| 2313 | buf = kmalloc(len + 2, GFP_KERNEL); |
| 2314 | if (!buf) |
| 2315 | return -ENOMEM; |
| 2316 | |
| 2317 | for (i = 0; trace_options[i]; i++) { |
| 2318 | if (trace_flags & (1 << i)) |
| 2319 | r += sprintf(buf + r, "%s ", trace_options[i]); |
| 2320 | else |
| 2321 | r += sprintf(buf + r, "no%s ", trace_options[i]); |
| 2322 | } |
| 2323 | |
| 2324 | r += sprintf(buf + r, "\n"); |
| 2325 | WARN_ON(r >= len + 2); |
| 2326 | |
| 2327 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2328 | |
| 2329 | kfree(buf); |
| 2330 | |
| 2331 | return r; |
| 2332 | } |
| 2333 | |
| 2334 | static ssize_t |
| 2335 | tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf, |
| 2336 | size_t cnt, loff_t *ppos) |
| 2337 | { |
| 2338 | char buf[64]; |
| 2339 | char *cmp = buf; |
| 2340 | int neg = 0; |
| 2341 | int i; |
| 2342 | |
| 2343 | if (cnt >= sizeof(buf)) |
| 2344 | return -EINVAL; |
| 2345 | |
| 2346 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2347 | return -EFAULT; |
| 2348 | |
| 2349 | buf[cnt] = 0; |
| 2350 | |
| 2351 | if (strncmp(buf, "no", 2) == 0) { |
| 2352 | neg = 1; |
| 2353 | cmp += 2; |
| 2354 | } |
| 2355 | |
| 2356 | for (i = 0; trace_options[i]; i++) { |
| 2357 | int len = strlen(trace_options[i]); |
| 2358 | |
| 2359 | if (strncmp(cmp, trace_options[i], len) == 0) { |
| 2360 | if (neg) |
| 2361 | trace_flags &= ~(1 << i); |
| 2362 | else |
| 2363 | trace_flags |= (1 << i); |
| 2364 | break; |
| 2365 | } |
| 2366 | } |
| 2367 | /* |
| 2368 | * If no option could be set, return an error: |
| 2369 | */ |
| 2370 | if (!trace_options[i]) |
| 2371 | return -EINVAL; |
| 2372 | |
| 2373 | filp->f_pos += cnt; |
| 2374 | |
| 2375 | return cnt; |
| 2376 | } |
| 2377 | |
| 2378 | static struct file_operations tracing_iter_fops = { |
| 2379 | .open = tracing_open_generic, |
| 2380 | .read = tracing_iter_ctrl_read, |
| 2381 | .write = tracing_iter_ctrl_write, |
| 2382 | }; |
| 2383 | |
| 2384 | static const char readme_msg[] = |
| 2385 | "tracing mini-HOWTO:\n\n" |
| 2386 | "# mkdir /debug\n" |
| 2387 | "# mount -t debugfs nodev /debug\n\n" |
| 2388 | "# cat /debug/tracing/available_tracers\n" |
| 2389 | "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n" |
| 2390 | "# cat /debug/tracing/current_tracer\n" |
| 2391 | "none\n" |
| 2392 | "# echo sched_switch > /debug/tracing/current_tracer\n" |
| 2393 | "# cat /debug/tracing/current_tracer\n" |
| 2394 | "sched_switch\n" |
| 2395 | "# cat /debug/tracing/iter_ctrl\n" |
| 2396 | "noprint-parent nosym-offset nosym-addr noverbose\n" |
| 2397 | "# echo print-parent > /debug/tracing/iter_ctrl\n" |
| 2398 | "# echo 1 > /debug/tracing/tracing_enabled\n" |
| 2399 | "# cat /debug/tracing/trace > /tmp/trace.txt\n" |
| 2400 | "echo 0 > /debug/tracing/tracing_enabled\n" |
| 2401 | ; |
| 2402 | |
| 2403 | static ssize_t |
| 2404 | tracing_readme_read(struct file *filp, char __user *ubuf, |
| 2405 | size_t cnt, loff_t *ppos) |
| 2406 | { |
| 2407 | return simple_read_from_buffer(ubuf, cnt, ppos, |
| 2408 | readme_msg, strlen(readme_msg)); |
| 2409 | } |
| 2410 | |
| 2411 | static struct file_operations tracing_readme_fops = { |
| 2412 | .open = tracing_open_generic, |
| 2413 | .read = tracing_readme_read, |
| 2414 | }; |
| 2415 | |
| 2416 | static ssize_t |
| 2417 | tracing_ctrl_read(struct file *filp, char __user *ubuf, |
| 2418 | size_t cnt, loff_t *ppos) |
| 2419 | { |
| 2420 | struct trace_array *tr = filp->private_data; |
| 2421 | char buf[64]; |
| 2422 | int r; |
| 2423 | |
| 2424 | r = sprintf(buf, "%ld\n", tr->ctrl); |
| 2425 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2426 | } |
| 2427 | |
| 2428 | static ssize_t |
| 2429 | tracing_ctrl_write(struct file *filp, const char __user *ubuf, |
| 2430 | size_t cnt, loff_t *ppos) |
| 2431 | { |
| 2432 | struct trace_array *tr = filp->private_data; |
| 2433 | char buf[64]; |
| 2434 | long val; |
| 2435 | int ret; |
| 2436 | |
| 2437 | if (cnt >= sizeof(buf)) |
| 2438 | return -EINVAL; |
| 2439 | |
| 2440 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2441 | return -EFAULT; |
| 2442 | |
| 2443 | buf[cnt] = 0; |
| 2444 | |
| 2445 | ret = strict_strtoul(buf, 10, &val); |
| 2446 | if (ret < 0) |
| 2447 | return ret; |
| 2448 | |
| 2449 | val = !!val; |
| 2450 | |
| 2451 | mutex_lock(&trace_types_lock); |
| 2452 | if (tr->ctrl ^ val) { |
| 2453 | if (val) |
| 2454 | tracer_enabled = 1; |
| 2455 | else |
| 2456 | tracer_enabled = 0; |
| 2457 | |
| 2458 | tr->ctrl = val; |
| 2459 | |
| 2460 | if (current_trace && current_trace->ctrl_update) |
| 2461 | current_trace->ctrl_update(tr); |
| 2462 | } |
| 2463 | mutex_unlock(&trace_types_lock); |
| 2464 | |
| 2465 | filp->f_pos += cnt; |
| 2466 | |
| 2467 | return cnt; |
| 2468 | } |
| 2469 | |
| 2470 | static ssize_t |
| 2471 | tracing_set_trace_read(struct file *filp, char __user *ubuf, |
| 2472 | size_t cnt, loff_t *ppos) |
| 2473 | { |
| 2474 | char buf[max_tracer_type_len+2]; |
| 2475 | int r; |
| 2476 | |
| 2477 | mutex_lock(&trace_types_lock); |
| 2478 | if (current_trace) |
| 2479 | r = sprintf(buf, "%s\n", current_trace->name); |
| 2480 | else |
| 2481 | r = sprintf(buf, "\n"); |
| 2482 | mutex_unlock(&trace_types_lock); |
| 2483 | |
| 2484 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2485 | } |
| 2486 | |
| 2487 | static ssize_t |
| 2488 | tracing_set_trace_write(struct file *filp, const char __user *ubuf, |
| 2489 | size_t cnt, loff_t *ppos) |
| 2490 | { |
| 2491 | struct trace_array *tr = &global_trace; |
| 2492 | struct tracer *t; |
| 2493 | char buf[max_tracer_type_len+1]; |
| 2494 | int i; |
| 2495 | |
| 2496 | if (cnt > max_tracer_type_len) |
| 2497 | cnt = max_tracer_type_len; |
| 2498 | |
| 2499 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2500 | return -EFAULT; |
| 2501 | |
| 2502 | buf[cnt] = 0; |
| 2503 | |
| 2504 | /* strip ending whitespace. */ |
| 2505 | for (i = cnt - 1; i > 0 && isspace(buf[i]); i--) |
| 2506 | buf[i] = 0; |
| 2507 | |
| 2508 | mutex_lock(&trace_types_lock); |
| 2509 | for (t = trace_types; t; t = t->next) { |
| 2510 | if (strcmp(t->name, buf) == 0) |
| 2511 | break; |
| 2512 | } |
| 2513 | if (!t || t == current_trace) |
| 2514 | goto out; |
| 2515 | |
| 2516 | if (current_trace && current_trace->reset) |
| 2517 | current_trace->reset(tr); |
| 2518 | |
| 2519 | current_trace = t; |
| 2520 | if (t->init) |
| 2521 | t->init(tr); |
| 2522 | |
| 2523 | out: |
| 2524 | mutex_unlock(&trace_types_lock); |
| 2525 | |
| 2526 | filp->f_pos += cnt; |
| 2527 | |
| 2528 | return cnt; |
| 2529 | } |
| 2530 | |
| 2531 | static ssize_t |
| 2532 | tracing_max_lat_read(struct file *filp, char __user *ubuf, |
| 2533 | size_t cnt, loff_t *ppos) |
| 2534 | { |
| 2535 | unsigned long *ptr = filp->private_data; |
| 2536 | char buf[64]; |
| 2537 | int r; |
| 2538 | |
| 2539 | r = snprintf(buf, sizeof(buf), "%ld\n", |
| 2540 | *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr)); |
| 2541 | if (r > sizeof(buf)) |
| 2542 | r = sizeof(buf); |
| 2543 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2544 | } |
| 2545 | |
| 2546 | static ssize_t |
| 2547 | tracing_max_lat_write(struct file *filp, const char __user *ubuf, |
| 2548 | size_t cnt, loff_t *ppos) |
| 2549 | { |
| 2550 | long *ptr = filp->private_data; |
| 2551 | char buf[64]; |
| 2552 | long val; |
| 2553 | int ret; |
| 2554 | |
| 2555 | if (cnt >= sizeof(buf)) |
| 2556 | return -EINVAL; |
| 2557 | |
| 2558 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2559 | return -EFAULT; |
| 2560 | |
| 2561 | buf[cnt] = 0; |
| 2562 | |
| 2563 | ret = strict_strtoul(buf, 10, &val); |
| 2564 | if (ret < 0) |
| 2565 | return ret; |
| 2566 | |
| 2567 | *ptr = val * 1000; |
| 2568 | |
| 2569 | return cnt; |
| 2570 | } |
| 2571 | |
| 2572 | static atomic_t tracing_reader; |
| 2573 | |
| 2574 | static int tracing_open_pipe(struct inode *inode, struct file *filp) |
| 2575 | { |
| 2576 | struct trace_iterator *iter; |
| 2577 | |
| 2578 | if (tracing_disabled) |
| 2579 | return -ENODEV; |
| 2580 | |
| 2581 | /* We only allow for reader of the pipe */ |
| 2582 | if (atomic_inc_return(&tracing_reader) != 1) { |
| 2583 | atomic_dec(&tracing_reader); |
| 2584 | return -EBUSY; |
| 2585 | } |
| 2586 | |
| 2587 | /* create a buffer to store the information to pass to userspace */ |
| 2588 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
| 2589 | if (!iter) |
| 2590 | return -ENOMEM; |
| 2591 | |
| 2592 | mutex_lock(&trace_types_lock); |
| 2593 | iter->tr = &global_trace; |
| 2594 | iter->trace = current_trace; |
| 2595 | filp->private_data = iter; |
| 2596 | |
| 2597 | if (iter->trace->pipe_open) |
| 2598 | iter->trace->pipe_open(iter); |
| 2599 | mutex_unlock(&trace_types_lock); |
| 2600 | |
| 2601 | return 0; |
| 2602 | } |
| 2603 | |
| 2604 | static int tracing_release_pipe(struct inode *inode, struct file *file) |
| 2605 | { |
| 2606 | struct trace_iterator *iter = file->private_data; |
| 2607 | |
| 2608 | kfree(iter); |
| 2609 | atomic_dec(&tracing_reader); |
| 2610 | |
| 2611 | return 0; |
| 2612 | } |
| 2613 | |
| 2614 | static unsigned int |
| 2615 | tracing_poll_pipe(struct file *filp, poll_table *poll_table) |
| 2616 | { |
| 2617 | struct trace_iterator *iter = filp->private_data; |
| 2618 | |
| 2619 | if (trace_flags & TRACE_ITER_BLOCK) { |
| 2620 | /* |
| 2621 | * Always select as readable when in blocking mode |
| 2622 | */ |
| 2623 | return POLLIN | POLLRDNORM; |
| 2624 | } else { |
| 2625 | if (!trace_empty(iter)) |
| 2626 | return POLLIN | POLLRDNORM; |
| 2627 | poll_wait(filp, &trace_wait, poll_table); |
| 2628 | if (!trace_empty(iter)) |
| 2629 | return POLLIN | POLLRDNORM; |
| 2630 | |
| 2631 | return 0; |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * Consumer reader. |
| 2637 | */ |
| 2638 | static ssize_t |
| 2639 | tracing_read_pipe(struct file *filp, char __user *ubuf, |
| 2640 | size_t cnt, loff_t *ppos) |
| 2641 | { |
| 2642 | struct trace_iterator *iter = filp->private_data; |
| 2643 | struct trace_array_cpu *data; |
| 2644 | static cpumask_t mask; |
| 2645 | unsigned long flags; |
| 2646 | #ifdef CONFIG_FTRACE |
| 2647 | int ftrace_save; |
| 2648 | #endif |
| 2649 | int cpu; |
| 2650 | ssize_t sret; |
| 2651 | |
| 2652 | /* return any leftover data */ |
| 2653 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
| 2654 | if (sret != -EBUSY) |
| 2655 | return sret; |
| 2656 | sret = 0; |
| 2657 | |
| 2658 | trace_seq_reset(&iter->seq); |
| 2659 | |
| 2660 | mutex_lock(&trace_types_lock); |
| 2661 | if (iter->trace->read) { |
| 2662 | sret = iter->trace->read(iter, filp, ubuf, cnt, ppos); |
| 2663 | if (sret) |
| 2664 | goto out; |
| 2665 | } |
| 2666 | |
| 2667 | while (trace_empty(iter)) { |
| 2668 | |
| 2669 | if ((filp->f_flags & O_NONBLOCK)) { |
| 2670 | sret = -EAGAIN; |
| 2671 | goto out; |
| 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * This is a make-shift waitqueue. The reason we don't use |
| 2676 | * an actual wait queue is because: |
| 2677 | * 1) we only ever have one waiter |
| 2678 | * 2) the tracing, traces all functions, we don't want |
| 2679 | * the overhead of calling wake_up and friends |
| 2680 | * (and tracing them too) |
| 2681 | * Anyway, this is really very primitive wakeup. |
| 2682 | */ |
| 2683 | set_current_state(TASK_INTERRUPTIBLE); |
| 2684 | iter->tr->waiter = current; |
| 2685 | |
| 2686 | mutex_unlock(&trace_types_lock); |
| 2687 | |
| 2688 | /* sleep for 100 msecs, and try again. */ |
| 2689 | schedule_timeout(HZ/10); |
| 2690 | |
| 2691 | mutex_lock(&trace_types_lock); |
| 2692 | |
| 2693 | iter->tr->waiter = NULL; |
| 2694 | |
| 2695 | if (signal_pending(current)) { |
| 2696 | sret = -EINTR; |
| 2697 | goto out; |
| 2698 | } |
| 2699 | |
| 2700 | if (iter->trace != current_trace) |
| 2701 | goto out; |
| 2702 | |
| 2703 | /* |
| 2704 | * We block until we read something and tracing is disabled. |
| 2705 | * We still block if tracing is disabled, but we have never |
| 2706 | * read anything. This allows a user to cat this file, and |
| 2707 | * then enable tracing. But after we have read something, |
| 2708 | * we give an EOF when tracing is again disabled. |
| 2709 | * |
| 2710 | * iter->pos will be 0 if we haven't read anything. |
| 2711 | */ |
| 2712 | if (!tracer_enabled && iter->pos) |
| 2713 | break; |
| 2714 | |
| 2715 | continue; |
| 2716 | } |
| 2717 | |
| 2718 | /* stop when tracing is finished */ |
| 2719 | if (trace_empty(iter)) |
| 2720 | goto out; |
| 2721 | |
| 2722 | if (cnt >= PAGE_SIZE) |
| 2723 | cnt = PAGE_SIZE - 1; |
| 2724 | |
| 2725 | /* reset all but tr, trace, and overruns */ |
| 2726 | memset(&iter->seq, 0, |
| 2727 | sizeof(struct trace_iterator) - |
| 2728 | offsetof(struct trace_iterator, seq)); |
| 2729 | iter->pos = -1; |
| 2730 | |
| 2731 | /* |
| 2732 | * We need to stop all tracing on all CPUS to read the |
| 2733 | * the next buffer. This is a bit expensive, but is |
| 2734 | * not done often. We fill all what we can read, |
| 2735 | * and then release the locks again. |
| 2736 | */ |
| 2737 | |
| 2738 | cpus_clear(mask); |
| 2739 | local_irq_save(flags); |
| 2740 | #ifdef CONFIG_FTRACE |
| 2741 | ftrace_save = ftrace_enabled; |
| 2742 | ftrace_enabled = 0; |
| 2743 | #endif |
| 2744 | smp_wmb(); |
| 2745 | for_each_tracing_cpu(cpu) { |
| 2746 | data = iter->tr->data[cpu]; |
| 2747 | |
| 2748 | if (!head_page(data) || !data->trace_idx) |
| 2749 | continue; |
| 2750 | |
| 2751 | atomic_inc(&data->disabled); |
| 2752 | cpu_set(cpu, mask); |
| 2753 | } |
| 2754 | |
| 2755 | for_each_cpu_mask(cpu, mask) { |
| 2756 | data = iter->tr->data[cpu]; |
| 2757 | __raw_spin_lock(&data->lock); |
| 2758 | |
| 2759 | if (data->overrun > iter->last_overrun[cpu]) |
| 2760 | iter->overrun[cpu] += |
| 2761 | data->overrun - iter->last_overrun[cpu]; |
| 2762 | iter->last_overrun[cpu] = data->overrun; |
| 2763 | } |
| 2764 | |
| 2765 | while (find_next_entry_inc(iter) != NULL) { |
| 2766 | int ret; |
| 2767 | int len = iter->seq.len; |
| 2768 | |
| 2769 | ret = print_trace_line(iter); |
| 2770 | if (!ret) { |
| 2771 | /* don't print partial lines */ |
| 2772 | iter->seq.len = len; |
| 2773 | break; |
| 2774 | } |
| 2775 | |
| 2776 | trace_consume(iter); |
| 2777 | |
| 2778 | if (iter->seq.len >= cnt) |
| 2779 | break; |
| 2780 | } |
| 2781 | |
| 2782 | for_each_cpu_mask(cpu, mask) { |
| 2783 | data = iter->tr->data[cpu]; |
| 2784 | __raw_spin_unlock(&data->lock); |
| 2785 | } |
| 2786 | |
| 2787 | for_each_cpu_mask(cpu, mask) { |
| 2788 | data = iter->tr->data[cpu]; |
| 2789 | atomic_dec(&data->disabled); |
| 2790 | } |
| 2791 | #ifdef CONFIG_FTRACE |
| 2792 | ftrace_enabled = ftrace_save; |
| 2793 | #endif |
| 2794 | local_irq_restore(flags); |
| 2795 | |
| 2796 | /* Now copy what we have to the user */ |
| 2797 | sret = trace_seq_to_user(&iter->seq, ubuf, cnt); |
| 2798 | if (iter->seq.readpos >= iter->seq.len) |
| 2799 | trace_seq_reset(&iter->seq); |
| 2800 | if (sret == -EBUSY) |
| 2801 | sret = 0; |
| 2802 | |
| 2803 | out: |
| 2804 | mutex_unlock(&trace_types_lock); |
| 2805 | |
| 2806 | return sret; |
| 2807 | } |
| 2808 | |
| 2809 | static ssize_t |
| 2810 | tracing_entries_read(struct file *filp, char __user *ubuf, |
| 2811 | size_t cnt, loff_t *ppos) |
| 2812 | { |
| 2813 | struct trace_array *tr = filp->private_data; |
| 2814 | char buf[64]; |
| 2815 | int r; |
| 2816 | |
| 2817 | r = sprintf(buf, "%lu\n", tr->entries); |
| 2818 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2819 | } |
| 2820 | |
| 2821 | static ssize_t |
| 2822 | tracing_entries_write(struct file *filp, const char __user *ubuf, |
| 2823 | size_t cnt, loff_t *ppos) |
| 2824 | { |
| 2825 | unsigned long val; |
| 2826 | char buf[64]; |
| 2827 | int i, ret; |
| 2828 | |
| 2829 | if (cnt >= sizeof(buf)) |
| 2830 | return -EINVAL; |
| 2831 | |
| 2832 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2833 | return -EFAULT; |
| 2834 | |
| 2835 | buf[cnt] = 0; |
| 2836 | |
| 2837 | ret = strict_strtoul(buf, 10, &val); |
| 2838 | if (ret < 0) |
| 2839 | return ret; |
| 2840 | |
| 2841 | /* must have at least 1 entry */ |
| 2842 | if (!val) |
| 2843 | return -EINVAL; |
| 2844 | |
| 2845 | mutex_lock(&trace_types_lock); |
| 2846 | |
| 2847 | if (current_trace != &no_tracer) { |
| 2848 | cnt = -EBUSY; |
| 2849 | pr_info("ftrace: set current_tracer to none" |
| 2850 | " before modifying buffer size\n"); |
| 2851 | goto out; |
| 2852 | } |
| 2853 | |
| 2854 | if (val > global_trace.entries) { |
| 2855 | long pages_requested; |
| 2856 | unsigned long freeable_pages; |
| 2857 | |
| 2858 | /* make sure we have enough memory before mapping */ |
| 2859 | pages_requested = |
| 2860 | (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE; |
| 2861 | |
| 2862 | /* account for each buffer (and max_tr) */ |
| 2863 | pages_requested *= tracing_nr_buffers * 2; |
| 2864 | |
| 2865 | /* Check for overflow */ |
| 2866 | if (pages_requested < 0) { |
| 2867 | cnt = -ENOMEM; |
| 2868 | goto out; |
| 2869 | } |
| 2870 | |
| 2871 | freeable_pages = determine_dirtyable_memory(); |
| 2872 | |
| 2873 | /* we only allow to request 1/4 of useable memory */ |
| 2874 | if (pages_requested > |
| 2875 | ((freeable_pages + tracing_pages_allocated) / 4)) { |
| 2876 | cnt = -ENOMEM; |
| 2877 | goto out; |
| 2878 | } |
| 2879 | |
| 2880 | while (global_trace.entries < val) { |
| 2881 | if (trace_alloc_page()) { |
| 2882 | cnt = -ENOMEM; |
| 2883 | goto out; |
| 2884 | } |
| 2885 | /* double check that we don't go over the known pages */ |
| 2886 | if (tracing_pages_allocated > pages_requested) |
| 2887 | break; |
| 2888 | } |
| 2889 | |
| 2890 | } else { |
| 2891 | /* include the number of entries in val (inc of page entries) */ |
| 2892 | while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1)) |
| 2893 | trace_free_page(); |
| 2894 | } |
| 2895 | |
| 2896 | /* check integrity */ |
| 2897 | for_each_tracing_cpu(i) |
| 2898 | check_pages(global_trace.data[i]); |
| 2899 | |
| 2900 | filp->f_pos += cnt; |
| 2901 | |
| 2902 | /* If check pages failed, return ENOMEM */ |
| 2903 | if (tracing_disabled) |
| 2904 | cnt = -ENOMEM; |
| 2905 | out: |
| 2906 | max_tr.entries = global_trace.entries; |
| 2907 | mutex_unlock(&trace_types_lock); |
| 2908 | |
| 2909 | return cnt; |
| 2910 | } |
| 2911 | |
| 2912 | static struct file_operations tracing_max_lat_fops = { |
| 2913 | .open = tracing_open_generic, |
| 2914 | .read = tracing_max_lat_read, |
| 2915 | .write = tracing_max_lat_write, |
| 2916 | }; |
| 2917 | |
| 2918 | static struct file_operations tracing_ctrl_fops = { |
| 2919 | .open = tracing_open_generic, |
| 2920 | .read = tracing_ctrl_read, |
| 2921 | .write = tracing_ctrl_write, |
| 2922 | }; |
| 2923 | |
| 2924 | static struct file_operations set_tracer_fops = { |
| 2925 | .open = tracing_open_generic, |
| 2926 | .read = tracing_set_trace_read, |
| 2927 | .write = tracing_set_trace_write, |
| 2928 | }; |
| 2929 | |
| 2930 | static struct file_operations tracing_pipe_fops = { |
| 2931 | .open = tracing_open_pipe, |
| 2932 | .poll = tracing_poll_pipe, |
| 2933 | .read = tracing_read_pipe, |
| 2934 | .release = tracing_release_pipe, |
| 2935 | }; |
| 2936 | |
| 2937 | static struct file_operations tracing_entries_fops = { |
| 2938 | .open = tracing_open_generic, |
| 2939 | .read = tracing_entries_read, |
| 2940 | .write = tracing_entries_write, |
| 2941 | }; |
| 2942 | |
| 2943 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 2944 | |
| 2945 | static ssize_t |
| 2946 | tracing_read_long(struct file *filp, char __user *ubuf, |
| 2947 | size_t cnt, loff_t *ppos) |
| 2948 | { |
| 2949 | unsigned long *p = filp->private_data; |
| 2950 | char buf[64]; |
| 2951 | int r; |
| 2952 | |
| 2953 | r = sprintf(buf, "%ld\n", *p); |
| 2954 | |
| 2955 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2956 | } |
| 2957 | |
| 2958 | static struct file_operations tracing_read_long_fops = { |
| 2959 | .open = tracing_open_generic, |
| 2960 | .read = tracing_read_long, |
| 2961 | }; |
| 2962 | #endif |
| 2963 | |
| 2964 | static struct dentry *d_tracer; |
| 2965 | |
| 2966 | struct dentry *tracing_init_dentry(void) |
| 2967 | { |
| 2968 | static int once; |
| 2969 | |
| 2970 | if (d_tracer) |
| 2971 | return d_tracer; |
| 2972 | |
| 2973 | d_tracer = debugfs_create_dir("tracing", NULL); |
| 2974 | |
| 2975 | if (!d_tracer && !once) { |
| 2976 | once = 1; |
| 2977 | pr_warning("Could not create debugfs directory 'tracing'\n"); |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | |
| 2981 | return d_tracer; |
| 2982 | } |
| 2983 | |
| 2984 | #ifdef CONFIG_FTRACE_SELFTEST |
| 2985 | /* Let selftest have access to static functions in this file */ |
| 2986 | #include "trace_selftest.c" |
| 2987 | #endif |
| 2988 | |
| 2989 | static __init void tracer_init_debugfs(void) |
| 2990 | { |
| 2991 | struct dentry *d_tracer; |
| 2992 | struct dentry *entry; |
| 2993 | |
| 2994 | d_tracer = tracing_init_dentry(); |
| 2995 | |
| 2996 | entry = debugfs_create_file("tracing_enabled", 0644, d_tracer, |
| 2997 | &global_trace, &tracing_ctrl_fops); |
| 2998 | if (!entry) |
| 2999 | pr_warning("Could not create debugfs 'tracing_enabled' entry\n"); |
| 3000 | |
| 3001 | entry = debugfs_create_file("iter_ctrl", 0644, d_tracer, |
| 3002 | NULL, &tracing_iter_fops); |
| 3003 | if (!entry) |
| 3004 | pr_warning("Could not create debugfs 'iter_ctrl' entry\n"); |
| 3005 | |
| 3006 | entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer, |
| 3007 | NULL, &tracing_cpumask_fops); |
| 3008 | if (!entry) |
| 3009 | pr_warning("Could not create debugfs 'tracing_cpumask' entry\n"); |
| 3010 | |
| 3011 | entry = debugfs_create_file("latency_trace", 0444, d_tracer, |
| 3012 | &global_trace, &tracing_lt_fops); |
| 3013 | if (!entry) |
| 3014 | pr_warning("Could not create debugfs 'latency_trace' entry\n"); |
| 3015 | |
| 3016 | entry = debugfs_create_file("trace", 0444, d_tracer, |
| 3017 | &global_trace, &tracing_fops); |
| 3018 | if (!entry) |
| 3019 | pr_warning("Could not create debugfs 'trace' entry\n"); |
| 3020 | |
| 3021 | entry = debugfs_create_file("available_tracers", 0444, d_tracer, |
| 3022 | &global_trace, &show_traces_fops); |
| 3023 | if (!entry) |
| 3024 | pr_warning("Could not create debugfs 'trace' entry\n"); |
| 3025 | |
| 3026 | entry = debugfs_create_file("current_tracer", 0444, d_tracer, |
| 3027 | &global_trace, &set_tracer_fops); |
| 3028 | if (!entry) |
| 3029 | pr_warning("Could not create debugfs 'trace' entry\n"); |
| 3030 | |
| 3031 | entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer, |
| 3032 | &tracing_max_latency, |
| 3033 | &tracing_max_lat_fops); |
| 3034 | if (!entry) |
| 3035 | pr_warning("Could not create debugfs " |
| 3036 | "'tracing_max_latency' entry\n"); |
| 3037 | |
| 3038 | entry = debugfs_create_file("tracing_thresh", 0644, d_tracer, |
| 3039 | &tracing_thresh, &tracing_max_lat_fops); |
| 3040 | if (!entry) |
| 3041 | pr_warning("Could not create debugfs " |
| 3042 | "'tracing_threash' entry\n"); |
| 3043 | entry = debugfs_create_file("README", 0644, d_tracer, |
| 3044 | NULL, &tracing_readme_fops); |
| 3045 | if (!entry) |
| 3046 | pr_warning("Could not create debugfs 'README' entry\n"); |
| 3047 | |
| 3048 | entry = debugfs_create_file("trace_pipe", 0644, d_tracer, |
| 3049 | NULL, &tracing_pipe_fops); |
| 3050 | if (!entry) |
| 3051 | pr_warning("Could not create debugfs " |
| 3052 | "'tracing_threash' entry\n"); |
| 3053 | |
| 3054 | entry = debugfs_create_file("trace_entries", 0644, d_tracer, |
| 3055 | &global_trace, &tracing_entries_fops); |
| 3056 | if (!entry) |
| 3057 | pr_warning("Could not create debugfs " |
| 3058 | "'tracing_threash' entry\n"); |
| 3059 | |
| 3060 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 3061 | entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer, |
| 3062 | &ftrace_update_tot_cnt, |
| 3063 | &tracing_read_long_fops); |
| 3064 | if (!entry) |
| 3065 | pr_warning("Could not create debugfs " |
| 3066 | "'dyn_ftrace_total_info' entry\n"); |
| 3067 | #endif |
| 3068 | #ifdef CONFIG_SYSPROF_TRACER |
| 3069 | init_tracer_sysprof_debugfs(d_tracer); |
| 3070 | #endif |
| 3071 | } |
| 3072 | |
| 3073 | #define TRACE_BUF_SIZE 1024 |
| 3074 | #define TRACE_PRINT_BUF_SIZE \ |
| 3075 | (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf)) |
| 3076 | #define TRACE_CONT_BUF_SIZE sizeof(struct trace_field) |
| 3077 | |
| 3078 | /** |
| 3079 | * ftrace_printk - printf formatting in the ftrace buffer |
| 3080 | * @fmt - the printf format for printing. |
| 3081 | * |
| 3082 | * Note: __ftrace_printk is an internal function for ftrace_printk and |
| 3083 | * the @ip is passed in via the ftrace_printk macro. |
| 3084 | * |
| 3085 | * This function allows a kernel developer to debug fast path sections |
| 3086 | * that printk is not appropriate for. By scattering in various |
| 3087 | * printk like tracing in the code, a developer can quickly see |
| 3088 | * where problems are occurring. |
| 3089 | * |
| 3090 | * This is intended as a debugging tool for the developer only. |
| 3091 | * Please reframe from leaving ftrace_printks scattered around in |
| 3092 | * your code. |
| 3093 | */ |
| 3094 | int __ftrace_printk(unsigned long ip, const char *fmt, ...) |
| 3095 | { |
| 3096 | struct trace_array *tr = &global_trace; |
| 3097 | static DEFINE_SPINLOCK(trace_buf_lock); |
| 3098 | static char trace_buf[TRACE_BUF_SIZE]; |
| 3099 | struct trace_array_cpu *data; |
| 3100 | struct trace_entry *entry; |
| 3101 | unsigned long flags; |
| 3102 | long disabled; |
| 3103 | va_list ap; |
| 3104 | int cpu, len = 0, write, written = 0; |
| 3105 | |
| 3106 | if (likely(!ftrace_function_enabled)) |
| 3107 | return 0; |
| 3108 | |
| 3109 | local_irq_save(flags); |
| 3110 | cpu = raw_smp_processor_id(); |
| 3111 | data = tr->data[cpu]; |
| 3112 | disabled = atomic_inc_return(&data->disabled); |
| 3113 | |
| 3114 | if (unlikely(disabled != 1 || !ftrace_function_enabled)) |
| 3115 | goto out; |
| 3116 | |
| 3117 | spin_lock(&trace_buf_lock); |
| 3118 | va_start(ap, fmt); |
| 3119 | len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap); |
| 3120 | va_end(ap); |
| 3121 | |
| 3122 | len = min(len, TRACE_BUF_SIZE-1); |
| 3123 | trace_buf[len] = 0; |
| 3124 | |
| 3125 | __raw_spin_lock(&data->lock); |
| 3126 | entry = tracing_get_trace_entry(tr, data); |
| 3127 | tracing_generic_entry_update(entry, flags); |
| 3128 | entry->type = TRACE_PRINT; |
| 3129 | entry->field.print.ip = ip; |
| 3130 | |
| 3131 | write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1)); |
| 3132 | |
| 3133 | memcpy(&entry->field.print.buf, trace_buf, write); |
| 3134 | entry->field.print.buf[write] = 0; |
| 3135 | written = write; |
| 3136 | |
| 3137 | if (written != len) |
| 3138 | entry->field.flags |= TRACE_FLAG_CONT; |
| 3139 | |
| 3140 | while (written != len) { |
| 3141 | entry = tracing_get_trace_entry(tr, data); |
| 3142 | |
| 3143 | entry->type = TRACE_CONT; |
| 3144 | write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1)); |
| 3145 | memcpy(&entry->cont.buf, trace_buf+written, write); |
| 3146 | entry->cont.buf[write] = 0; |
| 3147 | written += write; |
| 3148 | } |
| 3149 | __raw_spin_unlock(&data->lock); |
| 3150 | |
| 3151 | spin_unlock(&trace_buf_lock); |
| 3152 | |
| 3153 | out: |
| 3154 | atomic_dec(&data->disabled); |
| 3155 | local_irq_restore(flags); |
| 3156 | |
| 3157 | return len; |
| 3158 | } |
| 3159 | EXPORT_SYMBOL_GPL(__ftrace_printk); |
| 3160 | |
| 3161 | static int trace_alloc_page(void) |
| 3162 | { |
| 3163 | struct trace_array_cpu *data; |
| 3164 | struct page *page, *tmp; |
| 3165 | LIST_HEAD(pages); |
| 3166 | void *array; |
| 3167 | unsigned pages_allocated = 0; |
| 3168 | int i; |
| 3169 | |
| 3170 | /* first allocate a page for each CPU */ |
| 3171 | for_each_tracing_cpu(i) { |
| 3172 | array = (void *)__get_free_page(GFP_KERNEL); |
| 3173 | if (array == NULL) { |
| 3174 | printk(KERN_ERR "tracer: failed to allocate page" |
| 3175 | "for trace buffer!\n"); |
| 3176 | goto free_pages; |
| 3177 | } |
| 3178 | |
| 3179 | pages_allocated++; |
| 3180 | page = virt_to_page(array); |
| 3181 | list_add(&page->lru, &pages); |
| 3182 | |
| 3183 | /* Only allocate if we are actually using the max trace */ |
| 3184 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 3185 | array = (void *)__get_free_page(GFP_KERNEL); |
| 3186 | if (array == NULL) { |
| 3187 | printk(KERN_ERR "tracer: failed to allocate page" |
| 3188 | "for trace buffer!\n"); |
| 3189 | goto free_pages; |
| 3190 | } |
| 3191 | pages_allocated++; |
| 3192 | page = virt_to_page(array); |
| 3193 | list_add(&page->lru, &pages); |
| 3194 | #endif |
| 3195 | } |
| 3196 | |
| 3197 | /* Now that we successfully allocate a page per CPU, add them */ |
| 3198 | for_each_tracing_cpu(i) { |
| 3199 | data = global_trace.data[i]; |
| 3200 | page = list_entry(pages.next, struct page, lru); |
| 3201 | list_del_init(&page->lru); |
| 3202 | list_add_tail(&page->lru, &data->trace_pages); |
| 3203 | ClearPageLRU(page); |
| 3204 | |
| 3205 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 3206 | data = max_tr.data[i]; |
| 3207 | page = list_entry(pages.next, struct page, lru); |
| 3208 | list_del_init(&page->lru); |
| 3209 | list_add_tail(&page->lru, &data->trace_pages); |
| 3210 | SetPageLRU(page); |
| 3211 | #endif |
| 3212 | } |
| 3213 | tracing_pages_allocated += pages_allocated; |
| 3214 | global_trace.entries += ENTRIES_PER_PAGE; |
| 3215 | |
| 3216 | return 0; |
| 3217 | |
| 3218 | free_pages: |
| 3219 | list_for_each_entry_safe(page, tmp, &pages, lru) { |
| 3220 | list_del_init(&page->lru); |
| 3221 | __free_page(page); |
| 3222 | } |
| 3223 | return -ENOMEM; |
| 3224 | } |
| 3225 | |
| 3226 | static int trace_free_page(void) |
| 3227 | { |
| 3228 | struct trace_array_cpu *data; |
| 3229 | struct page *page; |
| 3230 | struct list_head *p; |
| 3231 | int i; |
| 3232 | int ret = 0; |
| 3233 | |
| 3234 | /* free one page from each buffer */ |
| 3235 | for_each_tracing_cpu(i) { |
| 3236 | data = global_trace.data[i]; |
| 3237 | p = data->trace_pages.next; |
| 3238 | if (p == &data->trace_pages) { |
| 3239 | /* should never happen */ |
| 3240 | WARN_ON(1); |
| 3241 | tracing_disabled = 1; |
| 3242 | ret = -1; |
| 3243 | break; |
| 3244 | } |
| 3245 | page = list_entry(p, struct page, lru); |
| 3246 | ClearPageLRU(page); |
| 3247 | list_del(&page->lru); |
| 3248 | tracing_pages_allocated--; |
| 3249 | tracing_pages_allocated--; |
| 3250 | __free_page(page); |
| 3251 | |
| 3252 | tracing_reset(data); |
| 3253 | |
| 3254 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 3255 | data = max_tr.data[i]; |
| 3256 | p = data->trace_pages.next; |
| 3257 | if (p == &data->trace_pages) { |
| 3258 | /* should never happen */ |
| 3259 | WARN_ON(1); |
| 3260 | tracing_disabled = 1; |
| 3261 | ret = -1; |
| 3262 | break; |
| 3263 | } |
| 3264 | page = list_entry(p, struct page, lru); |
| 3265 | ClearPageLRU(page); |
| 3266 | list_del(&page->lru); |
| 3267 | __free_page(page); |
| 3268 | |
| 3269 | tracing_reset(data); |
| 3270 | #endif |
| 3271 | } |
| 3272 | global_trace.entries -= ENTRIES_PER_PAGE; |
| 3273 | |
| 3274 | return ret; |
| 3275 | } |
| 3276 | |
| 3277 | __init static int tracer_alloc_buffers(void) |
| 3278 | { |
| 3279 | struct trace_array_cpu *data; |
| 3280 | void *array; |
| 3281 | struct page *page; |
| 3282 | int pages = 0; |
| 3283 | int ret = -ENOMEM; |
| 3284 | int i; |
| 3285 | |
| 3286 | /* TODO: make the number of buffers hot pluggable with CPUS */ |
| 3287 | tracing_nr_buffers = num_possible_cpus(); |
| 3288 | tracing_buffer_mask = cpu_possible_map; |
| 3289 | |
| 3290 | /* Allocate the first page for all buffers */ |
| 3291 | for_each_tracing_cpu(i) { |
| 3292 | data = global_trace.data[i] = &per_cpu(global_trace_cpu, i); |
| 3293 | max_tr.data[i] = &per_cpu(max_data, i); |
| 3294 | |
| 3295 | array = (void *)__get_free_page(GFP_KERNEL); |
| 3296 | if (array == NULL) { |
| 3297 | printk(KERN_ERR "tracer: failed to allocate page" |
| 3298 | "for trace buffer!\n"); |
| 3299 | goto free_buffers; |
| 3300 | } |
| 3301 | |
| 3302 | /* set the array to the list */ |
| 3303 | INIT_LIST_HEAD(&data->trace_pages); |
| 3304 | page = virt_to_page(array); |
| 3305 | list_add(&page->lru, &data->trace_pages); |
| 3306 | /* use the LRU flag to differentiate the two buffers */ |
| 3307 | ClearPageLRU(page); |
| 3308 | |
| 3309 | data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
| 3310 | max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
| 3311 | |
| 3312 | /* Only allocate if we are actually using the max trace */ |
| 3313 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 3314 | array = (void *)__get_free_page(GFP_KERNEL); |
| 3315 | if (array == NULL) { |
| 3316 | printk(KERN_ERR "tracer: failed to allocate page" |
| 3317 | "for trace buffer!\n"); |
| 3318 | goto free_buffers; |
| 3319 | } |
| 3320 | |
| 3321 | INIT_LIST_HEAD(&max_tr.data[i]->trace_pages); |
| 3322 | page = virt_to_page(array); |
| 3323 | list_add(&page->lru, &max_tr.data[i]->trace_pages); |
| 3324 | SetPageLRU(page); |
| 3325 | #endif |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * Since we allocate by orders of pages, we may be able to |
| 3330 | * round up a bit. |
| 3331 | */ |
| 3332 | global_trace.entries = ENTRIES_PER_PAGE; |
| 3333 | pages++; |
| 3334 | |
| 3335 | while (global_trace.entries < trace_nr_entries) { |
| 3336 | if (trace_alloc_page()) |
| 3337 | break; |
| 3338 | pages++; |
| 3339 | } |
| 3340 | max_tr.entries = global_trace.entries; |
| 3341 | |
| 3342 | pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n", |
| 3343 | pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE); |
| 3344 | pr_info(" actual entries %ld\n", global_trace.entries); |
| 3345 | |
| 3346 | tracer_init_debugfs(); |
| 3347 | |
| 3348 | trace_init_cmdlines(); |
| 3349 | |
| 3350 | register_tracer(&no_tracer); |
| 3351 | current_trace = &no_tracer; |
| 3352 | |
| 3353 | /* All seems OK, enable tracing */ |
| 3354 | global_trace.ctrl = tracer_enabled; |
| 3355 | tracing_disabled = 0; |
| 3356 | |
| 3357 | return 0; |
| 3358 | |
| 3359 | free_buffers: |
| 3360 | for (i-- ; i >= 0; i--) { |
| 3361 | struct page *page, *tmp; |
| 3362 | struct trace_array_cpu *data = global_trace.data[i]; |
| 3363 | |
| 3364 | if (data) { |
| 3365 | list_for_each_entry_safe(page, tmp, |
| 3366 | &data->trace_pages, lru) { |
| 3367 | list_del_init(&page->lru); |
| 3368 | __free_page(page); |
| 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | #ifdef CONFIG_TRACER_MAX_TRACE |
| 3373 | data = max_tr.data[i]; |
| 3374 | if (data) { |
| 3375 | list_for_each_entry_safe(page, tmp, |
| 3376 | &data->trace_pages, lru) { |
| 3377 | list_del_init(&page->lru); |
| 3378 | __free_page(page); |
| 3379 | } |
| 3380 | } |
| 3381 | #endif |
| 3382 | } |
| 3383 | return ret; |
| 3384 | } |
| 3385 | fs_initcall(tracer_alloc_buffers); |