1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Function graph tracer. 5 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com> 6 * Mostly borrowed from function tracer which 7 * is Copyright (c) Steven Rostedt <srostedt@redhat.com> 8 * 9 */ 10 #include <linux/uaccess.h> 11 #include <linux/ftrace.h> 12 #include <linux/interrupt.h> 13 #include <linux/slab.h> 14 #include <linux/fs.h> 15 16 #include "trace.h" 17 #include "trace_output.h" 18 19 /* When set, irq functions will be ignored */ 20 static int ftrace_graph_skip_irqs; 21 22 struct fgraph_cpu_data { 23 pid_t last_pid; 24 int depth; 25 int depth_irq; 26 int ignore; 27 unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH]; 28 }; 29 30 struct fgraph_data { 31 struct fgraph_cpu_data __percpu *cpu_data; 32 33 /* Place to preserve last processed entry. */ 34 union { 35 struct ftrace_graph_ent_entry ent; 36 struct fgraph_retaddr_ent_entry rent; 37 } ent; 38 struct ftrace_graph_ret_entry ret; 39 int failed; 40 int cpu; 41 }; 42 43 #define TRACE_GRAPH_INDENT 2 44 45 unsigned int fgraph_max_depth; 46 47 static struct tracer_opt trace_opts[] = { 48 /* Display overruns? (for self-debug purpose) */ 49 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) }, 50 /* Display CPU ? */ 51 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) }, 52 /* Display Overhead ? */ 53 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) }, 54 /* Display proc name/pid */ 55 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) }, 56 /* Display duration of execution */ 57 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) }, 58 /* Display absolute time of an entry */ 59 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) }, 60 /* Display interrupts */ 61 { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) }, 62 /* Display function name after trailing } */ 63 { TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) }, 64 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL 65 /* Display function return value ? */ 66 { TRACER_OPT(funcgraph-retval, TRACE_GRAPH_PRINT_RETVAL) }, 67 /* Display function return value in hexadecimal format ? */ 68 { TRACER_OPT(funcgraph-retval-hex, TRACE_GRAPH_PRINT_RETVAL_HEX) }, 69 #endif 70 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 71 /* Display function return address ? */ 72 { TRACER_OPT(funcgraph-retaddr, TRACE_GRAPH_PRINT_RETADDR) }, 73 #endif 74 #ifdef CONFIG_FUNCTION_TRACE_ARGS 75 /* Display function arguments ? */ 76 { TRACER_OPT(funcgraph-args, TRACE_GRAPH_ARGS) }, 77 #endif 78 /* Include sleep time (scheduled out) between entry and return */ 79 { TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) }, 80 81 #ifdef CONFIG_FUNCTION_PROFILER 82 /* Include time within nested functions */ 83 { TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) }, 84 #endif 85 86 { } /* Empty entry */ 87 }; 88 89 static struct tracer_flags tracer_flags = { 90 /* Don't display overruns, proc, or tail by default */ 91 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD | 92 TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS | 93 TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME, 94 .opts = trace_opts 95 }; 96 97 static bool tracer_flags_is_set(u32 flags) 98 { 99 return (tracer_flags.val & flags) == flags; 100 } 101 102 /* 103 * DURATION column is being also used to display IRQ signs, 104 * following values are used by print_graph_irq and others 105 * to fill in space into DURATION column. 106 */ 107 enum { 108 FLAGS_FILL_FULL = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT, 109 FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT, 110 FLAGS_FILL_END = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT, 111 }; 112 113 static void 114 print_graph_duration(struct trace_array *tr, unsigned long long duration, 115 struct trace_seq *s, u32 flags); 116 117 static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, 118 unsigned int trace_ctx, struct ftrace_regs *fregs) 119 { 120 struct ring_buffer_event *event; 121 struct trace_buffer *buffer = tr->array_buffer.buffer; 122 struct ftrace_graph_ent_entry *entry; 123 int size; 124 125 /* If fregs is defined, add FTRACE_REGS_MAX_ARGS long size words */ 126 size = sizeof(*entry) + (FTRACE_REGS_MAX_ARGS * !!fregs * sizeof(long)); 127 128 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT, size, trace_ctx); 129 if (!event) 130 return 0; 131 132 entry = ring_buffer_event_data(event); 133 entry->graph_ent = *trace; 134 135 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 136 if (fregs) { 137 for (int i = 0; i < FTRACE_REGS_MAX_ARGS; i++) 138 entry->args[i] = ftrace_regs_get_argument(fregs, i); 139 } 140 #endif 141 142 trace_buffer_unlock_commit_nostack(buffer, event); 143 144 return 1; 145 } 146 147 int __trace_graph_entry(struct trace_array *tr, 148 struct ftrace_graph_ent *trace, 149 unsigned int trace_ctx) 150 { 151 return __graph_entry(tr, trace, trace_ctx, NULL); 152 } 153 154 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 155 int __trace_graph_retaddr_entry(struct trace_array *tr, 156 struct ftrace_graph_ent *trace, 157 unsigned int trace_ctx, 158 unsigned long retaddr) 159 { 160 struct ring_buffer_event *event; 161 struct trace_buffer *buffer = tr->array_buffer.buffer; 162 struct fgraph_retaddr_ent_entry *entry; 163 164 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RETADDR_ENT, 165 sizeof(*entry), trace_ctx); 166 if (!event) 167 return 0; 168 entry = ring_buffer_event_data(event); 169 entry->graph_ent.func = trace->func; 170 entry->graph_ent.depth = trace->depth; 171 entry->graph_ent.retaddr = retaddr; 172 trace_buffer_unlock_commit_nostack(buffer, event); 173 174 return 1; 175 } 176 #else 177 int __trace_graph_retaddr_entry(struct trace_array *tr, 178 struct ftrace_graph_ent *trace, 179 unsigned int trace_ctx, 180 unsigned long retaddr) 181 { 182 return 1; 183 } 184 #endif 185 186 static inline int ftrace_graph_ignore_irqs(void) 187 { 188 if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT)) 189 return 0; 190 191 return in_hardirq(); 192 } 193 194 struct fgraph_times { 195 unsigned long long calltime; 196 unsigned long long sleeptime; /* may be optional! */ 197 }; 198 199 static int graph_entry(struct ftrace_graph_ent *trace, 200 struct fgraph_ops *gops, 201 struct ftrace_regs *fregs) 202 { 203 unsigned long *task_var = fgraph_get_task_var(gops); 204 struct trace_array *tr = gops->private; 205 struct trace_array_cpu *data; 206 struct fgraph_times *ftimes; 207 unsigned int trace_ctx; 208 long disabled; 209 int ret = 0; 210 int cpu; 211 212 if (*task_var & TRACE_GRAPH_NOTRACE) 213 return 0; 214 215 /* 216 * Do not trace a function if it's filtered by set_graph_notrace. 217 * Make the index of ret stack negative to indicate that it should 218 * ignore further functions. But it needs its own ret stack entry 219 * to recover the original index in order to continue tracing after 220 * returning from the function. 221 */ 222 if (ftrace_graph_notrace_addr(trace->func)) { 223 *task_var |= TRACE_GRAPH_NOTRACE; 224 /* 225 * Need to return 1 to have the return called 226 * that will clear the NOTRACE bit. 227 */ 228 return 1; 229 } 230 231 if (!ftrace_trace_task(tr)) 232 return 0; 233 234 if (ftrace_graph_ignore_func(gops, trace)) 235 return 0; 236 237 if (ftrace_graph_ignore_irqs()) 238 return 0; 239 240 if (fgraph_sleep_time) { 241 /* Only need to record the calltime */ 242 ftimes = fgraph_reserve_data(gops->idx, sizeof(ftimes->calltime)); 243 } else { 244 ftimes = fgraph_reserve_data(gops->idx, sizeof(*ftimes)); 245 if (ftimes) 246 ftimes->sleeptime = current->ftrace_sleeptime; 247 } 248 if (!ftimes) 249 return 0; 250 251 ftimes->calltime = trace_clock_local(); 252 253 /* 254 * Stop here if tracing_threshold is set. We only write function return 255 * events to the ring buffer. 256 */ 257 if (tracing_thresh) 258 return 1; 259 260 preempt_disable_notrace(); 261 cpu = raw_smp_processor_id(); 262 data = per_cpu_ptr(tr->array_buffer.data, cpu); 263 disabled = atomic_read(&data->disabled); 264 if (likely(!disabled)) { 265 trace_ctx = tracing_gen_ctx(); 266 if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) && 267 tracer_flags_is_set(TRACE_GRAPH_PRINT_RETADDR)) { 268 unsigned long retaddr = ftrace_graph_top_ret_addr(current); 269 ret = __trace_graph_retaddr_entry(tr, trace, trace_ctx, retaddr); 270 } else { 271 ret = __graph_entry(tr, trace, trace_ctx, fregs); 272 } 273 } 274 preempt_enable_notrace(); 275 276 return ret; 277 } 278 279 int trace_graph_entry(struct ftrace_graph_ent *trace, 280 struct fgraph_ops *gops, 281 struct ftrace_regs *fregs) 282 { 283 return graph_entry(trace, gops, NULL); 284 } 285 286 static int trace_graph_entry_args(struct ftrace_graph_ent *trace, 287 struct fgraph_ops *gops, 288 struct ftrace_regs *fregs) 289 { 290 return graph_entry(trace, gops, fregs); 291 } 292 293 static void 294 __trace_graph_function(struct trace_array *tr, 295 unsigned long ip, unsigned int trace_ctx) 296 { 297 u64 time = trace_clock_local(); 298 struct ftrace_graph_ent ent = { 299 .func = ip, 300 .depth = 0, 301 }; 302 struct ftrace_graph_ret ret = { 303 .func = ip, 304 .depth = 0, 305 }; 306 307 __trace_graph_entry(tr, &ent, trace_ctx); 308 __trace_graph_return(tr, &ret, trace_ctx, time, time); 309 } 310 311 void 312 trace_graph_function(struct trace_array *tr, 313 unsigned long ip, unsigned long parent_ip, 314 unsigned int trace_ctx) 315 { 316 __trace_graph_function(tr, ip, trace_ctx); 317 } 318 319 void __trace_graph_return(struct trace_array *tr, 320 struct ftrace_graph_ret *trace, 321 unsigned int trace_ctx, 322 u64 calltime, u64 rettime) 323 { 324 struct ring_buffer_event *event; 325 struct trace_buffer *buffer = tr->array_buffer.buffer; 326 struct ftrace_graph_ret_entry *entry; 327 328 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET, 329 sizeof(*entry), trace_ctx); 330 if (!event) 331 return; 332 entry = ring_buffer_event_data(event); 333 entry->ret = *trace; 334 entry->calltime = calltime; 335 entry->rettime = rettime; 336 trace_buffer_unlock_commit_nostack(buffer, event); 337 } 338 339 static void handle_nosleeptime(struct ftrace_graph_ret *trace, 340 struct fgraph_times *ftimes, 341 int size) 342 { 343 if (fgraph_sleep_time || size < sizeof(*ftimes)) 344 return; 345 346 ftimes->calltime += current->ftrace_sleeptime - ftimes->sleeptime; 347 } 348 349 void trace_graph_return(struct ftrace_graph_ret *trace, 350 struct fgraph_ops *gops, struct ftrace_regs *fregs) 351 { 352 unsigned long *task_var = fgraph_get_task_var(gops); 353 struct trace_array *tr = gops->private; 354 struct trace_array_cpu *data; 355 struct fgraph_times *ftimes; 356 unsigned int trace_ctx; 357 u64 calltime, rettime; 358 long disabled; 359 int size; 360 int cpu; 361 362 rettime = trace_clock_local(); 363 364 ftrace_graph_addr_finish(gops, trace); 365 366 if (*task_var & TRACE_GRAPH_NOTRACE) { 367 *task_var &= ~TRACE_GRAPH_NOTRACE; 368 return; 369 } 370 371 ftimes = fgraph_retrieve_data(gops->idx, &size); 372 if (!ftimes) 373 return; 374 375 handle_nosleeptime(trace, ftimes, size); 376 377 calltime = ftimes->calltime; 378 379 preempt_disable_notrace(); 380 cpu = raw_smp_processor_id(); 381 data = per_cpu_ptr(tr->array_buffer.data, cpu); 382 disabled = atomic_read(&data->disabled); 383 if (likely(!disabled)) { 384 trace_ctx = tracing_gen_ctx(); 385 __trace_graph_return(tr, trace, trace_ctx, calltime, rettime); 386 } 387 preempt_enable_notrace(); 388 } 389 390 static void trace_graph_thresh_return(struct ftrace_graph_ret *trace, 391 struct fgraph_ops *gops, 392 struct ftrace_regs *fregs) 393 { 394 struct fgraph_times *ftimes; 395 int size; 396 397 ftrace_graph_addr_finish(gops, trace); 398 399 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) { 400 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT); 401 return; 402 } 403 404 ftimes = fgraph_retrieve_data(gops->idx, &size); 405 if (!ftimes) 406 return; 407 408 handle_nosleeptime(trace, ftimes, size); 409 410 if (tracing_thresh && 411 (trace_clock_local() - ftimes->calltime < tracing_thresh)) 412 return; 413 else 414 trace_graph_return(trace, gops, fregs); 415 } 416 417 static struct fgraph_ops funcgraph_ops = { 418 .entryfunc = &trace_graph_entry, 419 .retfunc = &trace_graph_return, 420 }; 421 422 int allocate_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops) 423 { 424 struct fgraph_ops *gops; 425 426 gops = kzalloc(sizeof(*gops), GFP_KERNEL); 427 if (!gops) 428 return -ENOMEM; 429 430 gops->entryfunc = &trace_graph_entry; 431 gops->retfunc = &trace_graph_return; 432 433 tr->gops = gops; 434 gops->private = tr; 435 436 fgraph_init_ops(&gops->ops, ops); 437 438 return 0; 439 } 440 441 void free_fgraph_ops(struct trace_array *tr) 442 { 443 kfree(tr->gops); 444 } 445 446 __init void init_array_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops) 447 { 448 tr->gops = &funcgraph_ops; 449 funcgraph_ops.private = tr; 450 fgraph_init_ops(&tr->gops->ops, ops); 451 } 452 453 static int graph_trace_init(struct trace_array *tr) 454 { 455 int ret; 456 457 if (tracer_flags_is_set(TRACE_GRAPH_ARGS)) 458 tr->gops->entryfunc = trace_graph_entry_args; 459 else 460 tr->gops->entryfunc = trace_graph_entry; 461 462 if (tracing_thresh) 463 tr->gops->retfunc = trace_graph_thresh_return; 464 else 465 tr->gops->retfunc = trace_graph_return; 466 467 /* Make gops functions visible before we start tracing */ 468 smp_mb(); 469 470 ret = register_ftrace_graph(tr->gops); 471 if (ret) 472 return ret; 473 tracing_start_cmdline_record(); 474 475 return 0; 476 } 477 478 static int ftrace_graph_trace_args(struct trace_array *tr, int set) 479 { 480 trace_func_graph_ent_t entry; 481 482 if (set) 483 entry = trace_graph_entry_args; 484 else 485 entry = trace_graph_entry; 486 487 /* See if there's any changes */ 488 if (tr->gops->entryfunc == entry) 489 return 0; 490 491 unregister_ftrace_graph(tr->gops); 492 493 tr->gops->entryfunc = entry; 494 495 /* Make gops functions visible before we start tracing */ 496 smp_mb(); 497 return register_ftrace_graph(tr->gops); 498 } 499 500 static void graph_trace_reset(struct trace_array *tr) 501 { 502 tracing_stop_cmdline_record(); 503 unregister_ftrace_graph(tr->gops); 504 } 505 506 static int graph_trace_update_thresh(struct trace_array *tr) 507 { 508 graph_trace_reset(tr); 509 return graph_trace_init(tr); 510 } 511 512 static int max_bytes_for_cpu; 513 514 static void print_graph_cpu(struct trace_seq *s, int cpu) 515 { 516 /* 517 * Start with a space character - to make it stand out 518 * to the right a bit when trace output is pasted into 519 * email: 520 */ 521 trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu); 522 } 523 524 #define TRACE_GRAPH_PROCINFO_LENGTH 14 525 526 static void print_graph_proc(struct trace_seq *s, pid_t pid) 527 { 528 char comm[TASK_COMM_LEN]; 529 /* sign + log10(MAX_INT) + '\0' */ 530 char pid_str[11]; 531 int spaces = 0; 532 int len; 533 int i; 534 535 trace_find_cmdline(pid, comm); 536 comm[7] = '\0'; 537 sprintf(pid_str, "%d", pid); 538 539 /* 1 stands for the "-" character */ 540 len = strlen(comm) + strlen(pid_str) + 1; 541 542 if (len < TRACE_GRAPH_PROCINFO_LENGTH) 543 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len; 544 545 /* First spaces to align center */ 546 for (i = 0; i < spaces / 2; i++) 547 trace_seq_putc(s, ' '); 548 549 trace_seq_printf(s, "%s-%s", comm, pid_str); 550 551 /* Last spaces to align center */ 552 for (i = 0; i < spaces - (spaces / 2); i++) 553 trace_seq_putc(s, ' '); 554 } 555 556 557 static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry) 558 { 559 trace_seq_putc(s, ' '); 560 trace_print_lat_fmt(s, entry); 561 trace_seq_puts(s, " | "); 562 } 563 564 /* If the pid changed since the last trace, output this event */ 565 static void 566 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data) 567 { 568 pid_t prev_pid; 569 pid_t *last_pid; 570 571 if (!data) 572 return; 573 574 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid); 575 576 if (*last_pid == pid) 577 return; 578 579 prev_pid = *last_pid; 580 *last_pid = pid; 581 582 if (prev_pid == -1) 583 return; 584 /* 585 * Context-switch trace line: 586 587 ------------------------------------------ 588 | 1) migration/0--1 => sshd-1755 589 ------------------------------------------ 590 591 */ 592 trace_seq_puts(s, " ------------------------------------------\n"); 593 print_graph_cpu(s, cpu); 594 print_graph_proc(s, prev_pid); 595 trace_seq_puts(s, " => "); 596 print_graph_proc(s, pid); 597 trace_seq_puts(s, "\n ------------------------------------------\n\n"); 598 } 599 600 static struct ftrace_graph_ret_entry * 601 get_return_for_leaf(struct trace_iterator *iter, 602 struct ftrace_graph_ent_entry *curr) 603 { 604 struct fgraph_data *data = iter->private; 605 struct ring_buffer_iter *ring_iter = NULL; 606 struct ring_buffer_event *event; 607 struct ftrace_graph_ret_entry *next; 608 609 /* 610 * If the previous output failed to write to the seq buffer, 611 * then we just reuse the data from before. 612 */ 613 if (data && data->failed) { 614 curr = &data->ent.ent; 615 next = &data->ret; 616 } else { 617 618 ring_iter = trace_buffer_iter(iter, iter->cpu); 619 620 /* First peek to compare current entry and the next one */ 621 if (ring_iter) 622 event = ring_buffer_iter_peek(ring_iter, NULL); 623 else { 624 /* 625 * We need to consume the current entry to see 626 * the next one. 627 */ 628 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, 629 NULL, NULL); 630 event = ring_buffer_peek(iter->array_buffer->buffer, iter->cpu, 631 NULL, NULL); 632 } 633 634 if (!event) 635 return NULL; 636 637 next = ring_buffer_event_data(event); 638 639 if (data) { 640 /* 641 * Save current and next entries for later reference 642 * if the output fails. 643 */ 644 if (unlikely(curr->ent.type == TRACE_GRAPH_RETADDR_ENT)) 645 data->ent.rent = *(struct fgraph_retaddr_ent_entry *)curr; 646 else 647 data->ent.ent = *curr; 648 /* 649 * If the next event is not a return type, then 650 * we only care about what type it is. Otherwise we can 651 * safely copy the entire event. 652 */ 653 if (next->ent.type == TRACE_GRAPH_RET) 654 data->ret = *next; 655 else 656 data->ret.ent.type = next->ent.type; 657 } 658 } 659 660 if (next->ent.type != TRACE_GRAPH_RET) 661 return NULL; 662 663 if (curr->ent.pid != next->ent.pid || 664 curr->graph_ent.func != next->ret.func) 665 return NULL; 666 667 /* this is a leaf, now advance the iterator */ 668 if (ring_iter) 669 ring_buffer_iter_advance(ring_iter); 670 671 return next; 672 } 673 674 static void print_graph_abs_time(u64 t, struct trace_seq *s) 675 { 676 unsigned long usecs_rem; 677 678 usecs_rem = do_div(t, NSEC_PER_SEC); 679 usecs_rem /= 1000; 680 681 trace_seq_printf(s, "%5lu.%06lu | ", 682 (unsigned long)t, usecs_rem); 683 } 684 685 static void 686 print_graph_rel_time(struct trace_iterator *iter, struct trace_seq *s) 687 { 688 unsigned long long usecs; 689 690 usecs = iter->ts - iter->array_buffer->time_start; 691 do_div(usecs, NSEC_PER_USEC); 692 693 trace_seq_printf(s, "%9llu us | ", usecs); 694 } 695 696 static void 697 print_graph_irq(struct trace_iterator *iter, unsigned long addr, 698 enum trace_type type, int cpu, pid_t pid, u32 flags) 699 { 700 struct trace_array *tr = iter->tr; 701 struct trace_seq *s = &iter->seq; 702 struct trace_entry *ent = iter->ent; 703 704 addr += iter->tr->text_delta; 705 706 if (addr < (unsigned long)__irqentry_text_start || 707 addr >= (unsigned long)__irqentry_text_end) 708 return; 709 710 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) { 711 /* Absolute time */ 712 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 713 print_graph_abs_time(iter->ts, s); 714 715 /* Relative time */ 716 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 717 print_graph_rel_time(iter, s); 718 719 /* Cpu */ 720 if (flags & TRACE_GRAPH_PRINT_CPU) 721 print_graph_cpu(s, cpu); 722 723 /* Proc */ 724 if (flags & TRACE_GRAPH_PRINT_PROC) { 725 print_graph_proc(s, pid); 726 trace_seq_puts(s, " | "); 727 } 728 729 /* Latency format */ 730 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) 731 print_graph_lat_fmt(s, ent); 732 } 733 734 /* No overhead */ 735 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START); 736 737 if (type == TRACE_GRAPH_ENT) 738 trace_seq_puts(s, "==========>"); 739 else 740 trace_seq_puts(s, "<=========="); 741 742 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END); 743 trace_seq_putc(s, '\n'); 744 } 745 746 void 747 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s) 748 { 749 unsigned long nsecs_rem = do_div(duration, 1000); 750 /* log10(ULONG_MAX) + '\0' */ 751 char usecs_str[21]; 752 char nsecs_str[5]; 753 int len; 754 int i; 755 756 sprintf(usecs_str, "%lu", (unsigned long) duration); 757 758 /* Print msecs */ 759 trace_seq_printf(s, "%s", usecs_str); 760 761 len = strlen(usecs_str); 762 763 /* Print nsecs (we don't want to exceed 7 numbers) */ 764 if (len < 7) { 765 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len); 766 767 snprintf(nsecs_str, slen, "%03lu", nsecs_rem); 768 trace_seq_printf(s, ".%s", nsecs_str); 769 len += strlen(nsecs_str) + 1; 770 } 771 772 trace_seq_puts(s, " us "); 773 774 /* Print remaining spaces to fit the row's width */ 775 for (i = len; i < 8; i++) 776 trace_seq_putc(s, ' '); 777 } 778 779 static void 780 print_graph_duration(struct trace_array *tr, unsigned long long duration, 781 struct trace_seq *s, u32 flags) 782 { 783 if (!(flags & TRACE_GRAPH_PRINT_DURATION) || 784 !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO)) 785 return; 786 787 /* No real adata, just filling the column with spaces */ 788 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) { 789 case FLAGS_FILL_FULL: 790 trace_seq_puts(s, " | "); 791 return; 792 case FLAGS_FILL_START: 793 trace_seq_puts(s, " "); 794 return; 795 case FLAGS_FILL_END: 796 trace_seq_puts(s, " |"); 797 return; 798 } 799 800 /* Signal a overhead of time execution to the output */ 801 if (flags & TRACE_GRAPH_PRINT_OVERHEAD) 802 trace_seq_printf(s, "%c ", trace_find_mark(duration)); 803 else 804 trace_seq_puts(s, " "); 805 806 trace_print_graph_duration(duration, s); 807 trace_seq_puts(s, "| "); 808 } 809 810 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL 811 #define __TRACE_GRAPH_PRINT_RETVAL TRACE_GRAPH_PRINT_RETVAL 812 #else 813 #define __TRACE_GRAPH_PRINT_RETVAL 0 814 #endif 815 816 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 817 #define __TRACE_GRAPH_PRINT_RETADDR TRACE_GRAPH_PRINT_RETADDR 818 static void print_graph_retaddr(struct trace_seq *s, struct fgraph_retaddr_ent_entry *entry, 819 u32 trace_flags, bool comment) 820 { 821 if (comment) 822 trace_seq_puts(s, " /*"); 823 824 trace_seq_puts(s, " <-"); 825 seq_print_ip_sym(s, entry->graph_ent.retaddr, trace_flags | TRACE_ITER_SYM_OFFSET); 826 827 if (comment) 828 trace_seq_puts(s, " */"); 829 } 830 #else 831 #define __TRACE_GRAPH_PRINT_RETADDR 0 832 #define print_graph_retaddr(_seq, _entry, _tflags, _comment) do { } while (0) 833 #endif 834 835 #if defined(CONFIG_FUNCTION_GRAPH_RETVAL) || defined(CONFIG_FUNCTION_GRAPH_RETADDR) 836 837 static void print_graph_retval(struct trace_seq *s, struct ftrace_graph_ent_entry *entry, 838 struct ftrace_graph_ret *graph_ret, void *func, 839 u32 opt_flags, u32 trace_flags, int args_size) 840 { 841 unsigned long err_code = 0; 842 unsigned long retval = 0; 843 bool print_retaddr = false; 844 bool print_retval = false; 845 bool hex_format = !!(opt_flags & TRACE_GRAPH_PRINT_RETVAL_HEX); 846 847 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL 848 retval = graph_ret->retval; 849 print_retval = !!(opt_flags & TRACE_GRAPH_PRINT_RETVAL); 850 #endif 851 852 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 853 print_retaddr = !!(opt_flags & TRACE_GRAPH_PRINT_RETADDR); 854 #endif 855 856 if (print_retval && retval && !hex_format) { 857 /* Check if the return value matches the negative format */ 858 if (IS_ENABLED(CONFIG_64BIT) && (retval & BIT(31)) && 859 (((u64)retval) >> 32) == 0) { 860 err_code = sign_extend64(retval, 31); 861 } else { 862 err_code = retval; 863 } 864 865 if (!IS_ERR_VALUE(err_code)) 866 err_code = 0; 867 } 868 869 if (entry) { 870 if (entry->ent.type != TRACE_GRAPH_RETADDR_ENT) 871 print_retaddr = false; 872 873 trace_seq_printf(s, "%ps", func); 874 875 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long)) { 876 print_function_args(s, entry->args, (unsigned long)func); 877 trace_seq_putc(s, ';'); 878 } else 879 trace_seq_puts(s, "();"); 880 881 if (print_retval || print_retaddr) 882 trace_seq_puts(s, " /*"); 883 else 884 trace_seq_putc(s, '\n'); 885 } else { 886 print_retaddr = false; 887 trace_seq_printf(s, "} /* %ps", func); 888 } 889 890 if (print_retaddr) 891 print_graph_retaddr(s, (struct fgraph_retaddr_ent_entry *)entry, 892 trace_flags, false); 893 894 if (print_retval) { 895 if (hex_format || (err_code == 0)) 896 trace_seq_printf(s, " ret=0x%lx", retval); 897 else 898 trace_seq_printf(s, " ret=%ld", err_code); 899 } 900 901 if (!entry || print_retval || print_retaddr) 902 trace_seq_puts(s, " */\n"); 903 } 904 905 #else 906 907 #define print_graph_retval(_seq, _ent, _ret, _func, _opt_flags, _trace_flags, args_size) \ 908 do {} while (0) 909 910 #endif 911 912 /* Case of a leaf function on its call entry */ 913 static enum print_line_t 914 print_graph_entry_leaf(struct trace_iterator *iter, 915 struct ftrace_graph_ent_entry *entry, 916 struct ftrace_graph_ret_entry *ret_entry, 917 struct trace_seq *s, u32 flags) 918 { 919 struct fgraph_data *data = iter->private; 920 struct trace_array *tr = iter->tr; 921 struct ftrace_graph_ret *graph_ret; 922 struct ftrace_graph_ent *call; 923 unsigned long long duration; 924 unsigned long ret_func; 925 int args_size; 926 int cpu = iter->cpu; 927 int i; 928 929 args_size = iter->ent_size - offsetof(struct ftrace_graph_ent_entry, args); 930 931 graph_ret = &ret_entry->ret; 932 call = &entry->graph_ent; 933 duration = ret_entry->rettime - ret_entry->calltime; 934 935 if (data) { 936 struct fgraph_cpu_data *cpu_data; 937 938 cpu_data = per_cpu_ptr(data->cpu_data, cpu); 939 940 /* 941 * Comments display at + 1 to depth. Since 942 * this is a leaf function, keep the comments 943 * equal to this depth. 944 */ 945 cpu_data->depth = call->depth - 1; 946 947 /* No need to keep this function around for this depth */ 948 if (call->depth < FTRACE_RETFUNC_DEPTH && 949 !WARN_ON_ONCE(call->depth < 0)) 950 cpu_data->enter_funcs[call->depth] = 0; 951 } 952 953 /* Overhead and duration */ 954 print_graph_duration(tr, duration, s, flags); 955 956 /* Function */ 957 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) 958 trace_seq_putc(s, ' '); 959 960 ret_func = graph_ret->func + iter->tr->text_delta; 961 962 /* 963 * Write out the function return value or return address 964 */ 965 if (flags & (__TRACE_GRAPH_PRINT_RETVAL | __TRACE_GRAPH_PRINT_RETADDR)) { 966 print_graph_retval(s, entry, graph_ret, 967 (void *)graph_ret->func + iter->tr->text_delta, 968 flags, tr->trace_flags, args_size); 969 } else { 970 trace_seq_printf(s, "%ps", (void *)ret_func); 971 972 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long)) { 973 print_function_args(s, entry->args, ret_func); 974 trace_seq_putc(s, ';'); 975 } else 976 trace_seq_puts(s, "();"); 977 } 978 trace_seq_printf(s, "\n"); 979 980 print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET, 981 cpu, iter->ent->pid, flags); 982 983 return trace_handle_return(s); 984 } 985 986 static enum print_line_t 987 print_graph_entry_nested(struct trace_iterator *iter, 988 struct ftrace_graph_ent_entry *entry, 989 struct trace_seq *s, int cpu, u32 flags) 990 { 991 struct ftrace_graph_ent *call = &entry->graph_ent; 992 struct fgraph_data *data = iter->private; 993 struct trace_array *tr = iter->tr; 994 unsigned long func; 995 int args_size; 996 int i; 997 998 if (data) { 999 struct fgraph_cpu_data *cpu_data; 1000 int cpu = iter->cpu; 1001 1002 cpu_data = per_cpu_ptr(data->cpu_data, cpu); 1003 cpu_data->depth = call->depth; 1004 1005 /* Save this function pointer to see if the exit matches */ 1006 if (call->depth < FTRACE_RETFUNC_DEPTH && 1007 !WARN_ON_ONCE(call->depth < 0)) 1008 cpu_data->enter_funcs[call->depth] = call->func; 1009 } 1010 1011 /* No time */ 1012 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL); 1013 1014 /* Function */ 1015 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) 1016 trace_seq_putc(s, ' '); 1017 1018 func = call->func + iter->tr->text_delta; 1019 1020 trace_seq_printf(s, "%ps", (void *)func); 1021 1022 args_size = iter->ent_size - offsetof(struct ftrace_graph_ent_entry, args); 1023 1024 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long)) 1025 print_function_args(s, entry->args, func); 1026 else 1027 trace_seq_puts(s, "()"); 1028 1029 trace_seq_puts(s, " {"); 1030 1031 if (flags & __TRACE_GRAPH_PRINT_RETADDR && 1032 entry->ent.type == TRACE_GRAPH_RETADDR_ENT) 1033 print_graph_retaddr(s, (struct fgraph_retaddr_ent_entry *)entry, 1034 tr->trace_flags, true); 1035 trace_seq_putc(s, '\n'); 1036 1037 if (trace_seq_has_overflowed(s)) 1038 return TRACE_TYPE_PARTIAL_LINE; 1039 1040 /* 1041 * we already consumed the current entry to check the next one 1042 * and see if this is a leaf. 1043 */ 1044 return TRACE_TYPE_NO_CONSUME; 1045 } 1046 1047 static void 1048 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s, 1049 int type, unsigned long addr, u32 flags) 1050 { 1051 struct fgraph_data *data = iter->private; 1052 struct trace_entry *ent = iter->ent; 1053 struct trace_array *tr = iter->tr; 1054 int cpu = iter->cpu; 1055 1056 /* Pid */ 1057 verif_pid(s, ent->pid, cpu, data); 1058 1059 if (type) 1060 /* Interrupt */ 1061 print_graph_irq(iter, addr, type, cpu, ent->pid, flags); 1062 1063 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO)) 1064 return; 1065 1066 /* Absolute time */ 1067 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1068 print_graph_abs_time(iter->ts, s); 1069 1070 /* Relative time */ 1071 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1072 print_graph_rel_time(iter, s); 1073 1074 /* Cpu */ 1075 if (flags & TRACE_GRAPH_PRINT_CPU) 1076 print_graph_cpu(s, cpu); 1077 1078 /* Proc */ 1079 if (flags & TRACE_GRAPH_PRINT_PROC) { 1080 print_graph_proc(s, ent->pid); 1081 trace_seq_puts(s, " | "); 1082 } 1083 1084 /* Latency format */ 1085 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) 1086 print_graph_lat_fmt(s, ent); 1087 1088 return; 1089 } 1090 1091 /* 1092 * Entry check for irq code 1093 * 1094 * returns 1 if 1095 * - we are inside irq code 1096 * - we just entered irq code 1097 * 1098 * returns 0 if 1099 * - funcgraph-interrupts option is set 1100 * - we are not inside irq code 1101 */ 1102 static int 1103 check_irq_entry(struct trace_iterator *iter, u32 flags, 1104 unsigned long addr, int depth) 1105 { 1106 int cpu = iter->cpu; 1107 int *depth_irq; 1108 struct fgraph_data *data = iter->private; 1109 1110 addr += iter->tr->text_delta; 1111 1112 /* 1113 * If we are either displaying irqs, or we got called as 1114 * a graph event and private data does not exist, 1115 * then we bypass the irq check. 1116 */ 1117 if ((flags & TRACE_GRAPH_PRINT_IRQS) || 1118 (!data)) 1119 return 0; 1120 1121 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq); 1122 1123 /* 1124 * We are inside the irq code 1125 */ 1126 if (*depth_irq >= 0) 1127 return 1; 1128 1129 if ((addr < (unsigned long)__irqentry_text_start) || 1130 (addr >= (unsigned long)__irqentry_text_end)) 1131 return 0; 1132 1133 /* 1134 * We are entering irq code. 1135 */ 1136 *depth_irq = depth; 1137 return 1; 1138 } 1139 1140 /* 1141 * Return check for irq code 1142 * 1143 * returns 1 if 1144 * - we are inside irq code 1145 * - we just left irq code 1146 * 1147 * returns 0 if 1148 * - funcgraph-interrupts option is set 1149 * - we are not inside irq code 1150 */ 1151 static int 1152 check_irq_return(struct trace_iterator *iter, u32 flags, int depth) 1153 { 1154 int cpu = iter->cpu; 1155 int *depth_irq; 1156 struct fgraph_data *data = iter->private; 1157 1158 /* 1159 * If we are either displaying irqs, or we got called as 1160 * a graph event and private data does not exist, 1161 * then we bypass the irq check. 1162 */ 1163 if ((flags & TRACE_GRAPH_PRINT_IRQS) || 1164 (!data)) 1165 return 0; 1166 1167 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq); 1168 1169 /* 1170 * We are not inside the irq code. 1171 */ 1172 if (*depth_irq == -1) 1173 return 0; 1174 1175 /* 1176 * We are inside the irq code, and this is returning entry. 1177 * Let's not trace it and clear the entry depth, since 1178 * we are out of irq code. 1179 * 1180 * This condition ensures that we 'leave the irq code' once 1181 * we are out of the entry depth. Thus protecting us from 1182 * the RETURN entry loss. 1183 */ 1184 if (*depth_irq >= depth) { 1185 *depth_irq = -1; 1186 return 1; 1187 } 1188 1189 /* 1190 * We are inside the irq code, and this is not the entry. 1191 */ 1192 return 1; 1193 } 1194 1195 static enum print_line_t 1196 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s, 1197 struct trace_iterator *iter, u32 flags) 1198 { 1199 struct fgraph_data *data = iter->private; 1200 struct ftrace_graph_ent *call; 1201 struct ftrace_graph_ret_entry *leaf_ret; 1202 static enum print_line_t ret; 1203 int cpu = iter->cpu; 1204 /* 1205 * print_graph_entry() may consume the current event, 1206 * thus @field may become invalid, so we need to save it. 1207 * sizeof(struct ftrace_graph_ent_entry) is very small, 1208 * it can be safely saved at the stack. 1209 */ 1210 struct ftrace_graph_ent_entry *entry; 1211 u8 save_buf[sizeof(*entry) + FTRACE_REGS_MAX_ARGS * sizeof(long)]; 1212 1213 /* The ent_size is expected to be as big as the entry */ 1214 if (iter->ent_size > sizeof(save_buf)) 1215 iter->ent_size = sizeof(save_buf); 1216 1217 entry = (void *)save_buf; 1218 memcpy(entry, field, iter->ent_size); 1219 1220 call = &entry->graph_ent; 1221 1222 if (check_irq_entry(iter, flags, call->func, call->depth)) 1223 return TRACE_TYPE_HANDLED; 1224 1225 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags); 1226 1227 leaf_ret = get_return_for_leaf(iter, entry); 1228 if (leaf_ret) 1229 ret = print_graph_entry_leaf(iter, entry, leaf_ret, s, flags); 1230 else 1231 ret = print_graph_entry_nested(iter, entry, s, cpu, flags); 1232 1233 if (data) { 1234 /* 1235 * If we failed to write our output, then we need to make 1236 * note of it. Because we already consumed our entry. 1237 */ 1238 if (s->full) { 1239 data->failed = 1; 1240 data->cpu = cpu; 1241 } else 1242 data->failed = 0; 1243 } 1244 1245 return ret; 1246 } 1247 1248 static enum print_line_t 1249 print_graph_return(struct ftrace_graph_ret_entry *retentry, struct trace_seq *s, 1250 struct trace_entry *ent, struct trace_iterator *iter, 1251 u32 flags) 1252 { 1253 struct ftrace_graph_ret *trace = &retentry->ret; 1254 u64 calltime = retentry->calltime; 1255 u64 rettime = retentry->rettime; 1256 unsigned long long duration = rettime - calltime; 1257 struct fgraph_data *data = iter->private; 1258 struct trace_array *tr = iter->tr; 1259 unsigned long func; 1260 pid_t pid = ent->pid; 1261 int cpu = iter->cpu; 1262 int func_match = 1; 1263 int i; 1264 1265 func = trace->func + iter->tr->text_delta; 1266 1267 if (check_irq_return(iter, flags, trace->depth)) 1268 return TRACE_TYPE_HANDLED; 1269 1270 if (data) { 1271 struct fgraph_cpu_data *cpu_data; 1272 int cpu = iter->cpu; 1273 1274 cpu_data = per_cpu_ptr(data->cpu_data, cpu); 1275 1276 /* 1277 * Comments display at + 1 to depth. This is the 1278 * return from a function, we now want the comments 1279 * to display at the same level of the bracket. 1280 */ 1281 cpu_data->depth = trace->depth - 1; 1282 1283 if (trace->depth < FTRACE_RETFUNC_DEPTH && 1284 !WARN_ON_ONCE(trace->depth < 0)) { 1285 if (cpu_data->enter_funcs[trace->depth] != trace->func) 1286 func_match = 0; 1287 cpu_data->enter_funcs[trace->depth] = 0; 1288 } 1289 } 1290 1291 print_graph_prologue(iter, s, 0, 0, flags); 1292 1293 /* Overhead and duration */ 1294 print_graph_duration(tr, duration, s, flags); 1295 1296 /* Closing brace */ 1297 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) 1298 trace_seq_putc(s, ' '); 1299 1300 /* 1301 * Always write out the function name and its return value if the 1302 * funcgraph-retval option is enabled. 1303 */ 1304 if (flags & __TRACE_GRAPH_PRINT_RETVAL) { 1305 print_graph_retval(s, NULL, trace, (void *)func, flags, 1306 tr->trace_flags, 0); 1307 } else { 1308 /* 1309 * If the return function does not have a matching entry, 1310 * then the entry was lost. Instead of just printing 1311 * the '}' and letting the user guess what function this 1312 * belongs to, write out the function name. Always do 1313 * that if the funcgraph-tail option is enabled. 1314 */ 1315 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL)) 1316 trace_seq_puts(s, "}\n"); 1317 else 1318 trace_seq_printf(s, "} /* %ps */\n", (void *)func); 1319 } 1320 1321 /* Overrun */ 1322 if (flags & TRACE_GRAPH_PRINT_OVERRUN) 1323 trace_seq_printf(s, " (Overruns: %u)\n", 1324 trace->overrun); 1325 1326 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, 1327 cpu, pid, flags); 1328 1329 return trace_handle_return(s); 1330 } 1331 1332 static enum print_line_t 1333 print_graph_comment(struct trace_seq *s, struct trace_entry *ent, 1334 struct trace_iterator *iter, u32 flags) 1335 { 1336 struct trace_array *tr = iter->tr; 1337 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK); 1338 struct fgraph_data *data = iter->private; 1339 struct trace_event *event; 1340 int depth = 0; 1341 int ret; 1342 int i; 1343 1344 if (data) 1345 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth; 1346 1347 print_graph_prologue(iter, s, 0, 0, flags); 1348 1349 /* No time */ 1350 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL); 1351 1352 /* Indentation */ 1353 if (depth > 0) 1354 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) 1355 trace_seq_putc(s, ' '); 1356 1357 /* The comment */ 1358 trace_seq_puts(s, "/* "); 1359 1360 switch (iter->ent->type) { 1361 case TRACE_BPUTS: 1362 ret = trace_print_bputs_msg_only(iter); 1363 if (ret != TRACE_TYPE_HANDLED) 1364 return ret; 1365 break; 1366 case TRACE_BPRINT: 1367 ret = trace_print_bprintk_msg_only(iter); 1368 if (ret != TRACE_TYPE_HANDLED) 1369 return ret; 1370 break; 1371 case TRACE_PRINT: 1372 ret = trace_print_printk_msg_only(iter); 1373 if (ret != TRACE_TYPE_HANDLED) 1374 return ret; 1375 break; 1376 default: 1377 event = ftrace_find_event(ent->type); 1378 if (!event) 1379 return TRACE_TYPE_UNHANDLED; 1380 1381 ret = event->funcs->trace(iter, sym_flags, event); 1382 if (ret != TRACE_TYPE_HANDLED) 1383 return ret; 1384 } 1385 1386 if (trace_seq_has_overflowed(s)) 1387 goto out; 1388 1389 /* Strip ending newline */ 1390 if (s->buffer[s->seq.len - 1] == '\n') { 1391 s->buffer[s->seq.len - 1] = '\0'; 1392 s->seq.len--; 1393 } 1394 1395 trace_seq_puts(s, " */\n"); 1396 out: 1397 return trace_handle_return(s); 1398 } 1399 1400 1401 enum print_line_t 1402 print_graph_function_flags(struct trace_iterator *iter, u32 flags) 1403 { 1404 struct ftrace_graph_ent_entry *field; 1405 struct fgraph_data *data = iter->private; 1406 struct trace_entry *entry = iter->ent; 1407 struct trace_seq *s = &iter->seq; 1408 int cpu = iter->cpu; 1409 int ret; 1410 1411 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) { 1412 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0; 1413 return TRACE_TYPE_HANDLED; 1414 } 1415 1416 /* 1417 * If the last output failed, there's a possibility we need 1418 * to print out the missing entry which would never go out. 1419 */ 1420 if (data && data->failed) { 1421 field = &data->ent.ent; 1422 iter->cpu = data->cpu; 1423 ret = print_graph_entry(field, s, iter, flags); 1424 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) { 1425 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1; 1426 ret = TRACE_TYPE_NO_CONSUME; 1427 } 1428 iter->cpu = cpu; 1429 return ret; 1430 } 1431 1432 switch (entry->type) { 1433 case TRACE_GRAPH_ENT: { 1434 trace_assign_type(field, entry); 1435 return print_graph_entry(field, s, iter, flags); 1436 } 1437 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 1438 case TRACE_GRAPH_RETADDR_ENT: { 1439 struct fgraph_retaddr_ent_entry saved; 1440 struct fgraph_retaddr_ent_entry *rfield; 1441 1442 trace_assign_type(rfield, entry); 1443 saved = *rfield; 1444 return print_graph_entry((struct ftrace_graph_ent_entry *)&saved, s, iter, flags); 1445 } 1446 #endif 1447 case TRACE_GRAPH_RET: { 1448 struct ftrace_graph_ret_entry *field; 1449 trace_assign_type(field, entry); 1450 return print_graph_return(field, s, entry, iter, flags); 1451 } 1452 case TRACE_STACK: 1453 case TRACE_FN: 1454 /* dont trace stack and functions as comments */ 1455 return TRACE_TYPE_UNHANDLED; 1456 1457 default: 1458 return print_graph_comment(s, entry, iter, flags); 1459 } 1460 1461 return TRACE_TYPE_HANDLED; 1462 } 1463 1464 static enum print_line_t 1465 print_graph_function(struct trace_iterator *iter) 1466 { 1467 return print_graph_function_flags(iter, tracer_flags.val); 1468 } 1469 1470 static enum print_line_t 1471 print_graph_function_event(struct trace_iterator *iter, int flags, 1472 struct trace_event *event) 1473 { 1474 return print_graph_function(iter); 1475 } 1476 1477 static void print_lat_header(struct seq_file *s, u32 flags) 1478 { 1479 static const char spaces[] = " " /* 16 spaces */ 1480 " " /* 4 spaces */ 1481 " "; /* 17 spaces */ 1482 int size = 0; 1483 1484 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1485 size += 16; 1486 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1487 size += 16; 1488 if (flags & TRACE_GRAPH_PRINT_CPU) 1489 size += 4; 1490 if (flags & TRACE_GRAPH_PRINT_PROC) 1491 size += 17; 1492 1493 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces); 1494 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces); 1495 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces); 1496 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces); 1497 seq_printf(s, "#%.*s||| / \n", size, spaces); 1498 } 1499 1500 static void __print_graph_headers_flags(struct trace_array *tr, 1501 struct seq_file *s, u32 flags) 1502 { 1503 int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT; 1504 1505 if (lat) 1506 print_lat_header(s, flags); 1507 1508 /* 1st line */ 1509 seq_putc(s, '#'); 1510 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1511 seq_puts(s, " TIME "); 1512 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1513 seq_puts(s, " REL TIME "); 1514 if (flags & TRACE_GRAPH_PRINT_CPU) 1515 seq_puts(s, " CPU"); 1516 if (flags & TRACE_GRAPH_PRINT_PROC) 1517 seq_puts(s, " TASK/PID "); 1518 if (lat) 1519 seq_puts(s, "|||| "); 1520 if (flags & TRACE_GRAPH_PRINT_DURATION) 1521 seq_puts(s, " DURATION "); 1522 seq_puts(s, " FUNCTION CALLS\n"); 1523 1524 /* 2nd line */ 1525 seq_putc(s, '#'); 1526 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1527 seq_puts(s, " | "); 1528 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1529 seq_puts(s, " | "); 1530 if (flags & TRACE_GRAPH_PRINT_CPU) 1531 seq_puts(s, " | "); 1532 if (flags & TRACE_GRAPH_PRINT_PROC) 1533 seq_puts(s, " | | "); 1534 if (lat) 1535 seq_puts(s, "|||| "); 1536 if (flags & TRACE_GRAPH_PRINT_DURATION) 1537 seq_puts(s, " | | "); 1538 seq_puts(s, " | | | |\n"); 1539 } 1540 1541 static void print_graph_headers(struct seq_file *s) 1542 { 1543 print_graph_headers_flags(s, tracer_flags.val); 1544 } 1545 1546 void print_graph_headers_flags(struct seq_file *s, u32 flags) 1547 { 1548 struct trace_iterator *iter = s->private; 1549 struct trace_array *tr = iter->tr; 1550 1551 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO)) 1552 return; 1553 1554 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) { 1555 /* print nothing if the buffers are empty */ 1556 if (trace_empty(iter)) 1557 return; 1558 1559 print_trace_header(s, iter); 1560 } 1561 1562 __print_graph_headers_flags(tr, s, flags); 1563 } 1564 1565 void graph_trace_open(struct trace_iterator *iter) 1566 { 1567 /* pid and depth on the last trace processed */ 1568 struct fgraph_data *data; 1569 gfp_t gfpflags; 1570 int cpu; 1571 1572 iter->private = NULL; 1573 1574 /* We can be called in atomic context via ftrace_dump() */ 1575 gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL; 1576 1577 data = kzalloc(sizeof(*data), gfpflags); 1578 if (!data) 1579 goto out_err; 1580 1581 data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags); 1582 if (!data->cpu_data) 1583 goto out_err_free; 1584 1585 for_each_possible_cpu(cpu) { 1586 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid); 1587 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth); 1588 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore); 1589 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq); 1590 1591 *pid = -1; 1592 *depth = 0; 1593 *ignore = 0; 1594 *depth_irq = -1; 1595 } 1596 1597 iter->private = data; 1598 1599 return; 1600 1601 out_err_free: 1602 kfree(data); 1603 out_err: 1604 pr_warn("function graph tracer: not enough memory\n"); 1605 } 1606 1607 void graph_trace_close(struct trace_iterator *iter) 1608 { 1609 struct fgraph_data *data = iter->private; 1610 1611 if (data) { 1612 free_percpu(data->cpu_data); 1613 kfree(data); 1614 iter->private = NULL; 1615 } 1616 } 1617 1618 static int 1619 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) 1620 { 1621 if (bit == TRACE_GRAPH_PRINT_IRQS) 1622 ftrace_graph_skip_irqs = !set; 1623 1624 if (bit == TRACE_GRAPH_SLEEP_TIME) 1625 ftrace_graph_sleep_time_control(set); 1626 1627 if (bit == TRACE_GRAPH_GRAPH_TIME) 1628 ftrace_graph_graph_time_control(set); 1629 1630 if (bit == TRACE_GRAPH_ARGS) 1631 return ftrace_graph_trace_args(tr, set); 1632 1633 return 0; 1634 } 1635 1636 static struct trace_event_functions graph_functions = { 1637 .trace = print_graph_function_event, 1638 }; 1639 1640 static struct trace_event graph_trace_entry_event = { 1641 .type = TRACE_GRAPH_ENT, 1642 .funcs = &graph_functions, 1643 }; 1644 1645 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 1646 static struct trace_event graph_trace_retaddr_entry_event = { 1647 .type = TRACE_GRAPH_RETADDR_ENT, 1648 .funcs = &graph_functions, 1649 }; 1650 #endif 1651 1652 static struct trace_event graph_trace_ret_event = { 1653 .type = TRACE_GRAPH_RET, 1654 .funcs = &graph_functions 1655 }; 1656 1657 static struct tracer graph_trace __tracer_data = { 1658 .name = "function_graph", 1659 .update_thresh = graph_trace_update_thresh, 1660 .open = graph_trace_open, 1661 .pipe_open = graph_trace_open, 1662 .close = graph_trace_close, 1663 .pipe_close = graph_trace_close, 1664 .init = graph_trace_init, 1665 .reset = graph_trace_reset, 1666 .print_line = print_graph_function, 1667 .print_header = print_graph_headers, 1668 .flags = &tracer_flags, 1669 .set_flag = func_graph_set_flag, 1670 .allow_instances = true, 1671 #ifdef CONFIG_FTRACE_SELFTEST 1672 .selftest = trace_selftest_startup_function_graph, 1673 #endif 1674 }; 1675 1676 1677 static ssize_t 1678 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt, 1679 loff_t *ppos) 1680 { 1681 unsigned long val; 1682 int ret; 1683 1684 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1685 if (ret) 1686 return ret; 1687 1688 fgraph_max_depth = val; 1689 1690 *ppos += cnt; 1691 1692 return cnt; 1693 } 1694 1695 static ssize_t 1696 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt, 1697 loff_t *ppos) 1698 { 1699 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/ 1700 int n; 1701 1702 n = sprintf(buf, "%d\n", fgraph_max_depth); 1703 1704 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n); 1705 } 1706 1707 static const struct file_operations graph_depth_fops = { 1708 .open = tracing_open_generic, 1709 .write = graph_depth_write, 1710 .read = graph_depth_read, 1711 .llseek = generic_file_llseek, 1712 }; 1713 1714 static __init int init_graph_tracefs(void) 1715 { 1716 int ret; 1717 1718 ret = tracing_init_dentry(); 1719 if (ret) 1720 return 0; 1721 1722 trace_create_file("max_graph_depth", TRACE_MODE_WRITE, NULL, 1723 NULL, &graph_depth_fops); 1724 1725 return 0; 1726 } 1727 fs_initcall(init_graph_tracefs); 1728 1729 static __init int init_graph_trace(void) 1730 { 1731 max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1); 1732 1733 if (!register_trace_event(&graph_trace_entry_event)) { 1734 pr_warn("Warning: could not register graph trace events\n"); 1735 return 1; 1736 } 1737 1738 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 1739 if (!register_trace_event(&graph_trace_retaddr_entry_event)) { 1740 pr_warn("Warning: could not register graph trace retaddr events\n"); 1741 return 1; 1742 } 1743 #endif 1744 1745 if (!register_trace_event(&graph_trace_ret_event)) { 1746 pr_warn("Warning: could not register graph trace events\n"); 1747 return 1; 1748 } 1749 1750 return register_tracer(&graph_trace); 1751 } 1752 1753 core_initcall(init_graph_trace); 1754