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 print_retaddr = false; 885 trace_seq_printf(s, "} /* %ps", func); 886 } 887 888 if (print_retaddr) 889 print_graph_retaddr(s, (struct fgraph_retaddr_ent_entry *)entry, 890 trace_flags, false); 891 892 if (print_retval) { 893 if (hex_format || (err_code == 0)) 894 trace_seq_printf(s, " ret=0x%lx", retval); 895 else 896 trace_seq_printf(s, " ret=%ld", err_code); 897 } 898 899 if (!entry || print_retval || print_retaddr) 900 trace_seq_puts(s, " */"); 901 } 902 903 #else 904 905 #define print_graph_retval(_seq, _ent, _ret, _func, _opt_flags, _trace_flags, args_size) \ 906 do {} while (0) 907 908 #endif 909 910 /* Case of a leaf function on its call entry */ 911 static enum print_line_t 912 print_graph_entry_leaf(struct trace_iterator *iter, 913 struct ftrace_graph_ent_entry *entry, 914 struct ftrace_graph_ret_entry *ret_entry, 915 struct trace_seq *s, u32 flags) 916 { 917 struct fgraph_data *data = iter->private; 918 struct trace_array *tr = iter->tr; 919 struct ftrace_graph_ret *graph_ret; 920 struct ftrace_graph_ent *call; 921 unsigned long long duration; 922 unsigned long ret_func; 923 int args_size; 924 int cpu = iter->cpu; 925 int i; 926 927 args_size = iter->ent_size - offsetof(struct ftrace_graph_ent_entry, args); 928 929 graph_ret = &ret_entry->ret; 930 call = &entry->graph_ent; 931 duration = ret_entry->rettime - ret_entry->calltime; 932 933 if (data) { 934 struct fgraph_cpu_data *cpu_data; 935 936 cpu_data = per_cpu_ptr(data->cpu_data, cpu); 937 938 /* 939 * Comments display at + 1 to depth. Since 940 * this is a leaf function, keep the comments 941 * equal to this depth. 942 */ 943 cpu_data->depth = call->depth - 1; 944 945 /* No need to keep this function around for this depth */ 946 if (call->depth < FTRACE_RETFUNC_DEPTH && 947 !WARN_ON_ONCE(call->depth < 0)) 948 cpu_data->enter_funcs[call->depth] = 0; 949 } 950 951 /* Overhead and duration */ 952 print_graph_duration(tr, duration, s, flags); 953 954 /* Function */ 955 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) 956 trace_seq_putc(s, ' '); 957 958 ret_func = graph_ret->func + iter->tr->text_delta; 959 960 /* 961 * Write out the function return value or return address 962 */ 963 if (flags & (__TRACE_GRAPH_PRINT_RETVAL | __TRACE_GRAPH_PRINT_RETADDR)) { 964 print_graph_retval(s, entry, graph_ret, 965 (void *)graph_ret->func + iter->tr->text_delta, 966 flags, tr->trace_flags, args_size); 967 } else { 968 trace_seq_printf(s, "%ps", (void *)ret_func); 969 970 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long)) { 971 print_function_args(s, entry->args, ret_func); 972 trace_seq_putc(s, ';'); 973 } else 974 trace_seq_puts(s, "();"); 975 } 976 trace_seq_putc(s, '\n'); 977 978 print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET, 979 cpu, iter->ent->pid, flags); 980 981 return trace_handle_return(s); 982 } 983 984 static enum print_line_t 985 print_graph_entry_nested(struct trace_iterator *iter, 986 struct ftrace_graph_ent_entry *entry, 987 struct trace_seq *s, int cpu, u32 flags) 988 { 989 struct ftrace_graph_ent *call = &entry->graph_ent; 990 struct fgraph_data *data = iter->private; 991 struct trace_array *tr = iter->tr; 992 unsigned long func; 993 int args_size; 994 int i; 995 996 if (data) { 997 struct fgraph_cpu_data *cpu_data; 998 int cpu = iter->cpu; 999 1000 cpu_data = per_cpu_ptr(data->cpu_data, cpu); 1001 cpu_data->depth = call->depth; 1002 1003 /* Save this function pointer to see if the exit matches */ 1004 if (call->depth < FTRACE_RETFUNC_DEPTH && 1005 !WARN_ON_ONCE(call->depth < 0)) 1006 cpu_data->enter_funcs[call->depth] = call->func; 1007 } 1008 1009 /* No time */ 1010 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL); 1011 1012 /* Function */ 1013 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) 1014 trace_seq_putc(s, ' '); 1015 1016 func = call->func + iter->tr->text_delta; 1017 1018 trace_seq_printf(s, "%ps", (void *)func); 1019 1020 args_size = iter->ent_size - offsetof(struct ftrace_graph_ent_entry, args); 1021 1022 if (args_size >= FTRACE_REGS_MAX_ARGS * sizeof(long)) 1023 print_function_args(s, entry->args, func); 1024 else 1025 trace_seq_puts(s, "()"); 1026 1027 trace_seq_puts(s, " {"); 1028 1029 if (flags & __TRACE_GRAPH_PRINT_RETADDR && 1030 entry->ent.type == TRACE_GRAPH_RETADDR_ENT) 1031 print_graph_retaddr(s, (struct fgraph_retaddr_ent_entry *)entry, 1032 tr->trace_flags, true); 1033 trace_seq_putc(s, '\n'); 1034 1035 if (trace_seq_has_overflowed(s)) 1036 return TRACE_TYPE_PARTIAL_LINE; 1037 1038 /* 1039 * we already consumed the current entry to check the next one 1040 * and see if this is a leaf. 1041 */ 1042 return TRACE_TYPE_NO_CONSUME; 1043 } 1044 1045 static void 1046 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s, 1047 int type, unsigned long addr, u32 flags) 1048 { 1049 struct fgraph_data *data = iter->private; 1050 struct trace_entry *ent = iter->ent; 1051 struct trace_array *tr = iter->tr; 1052 int cpu = iter->cpu; 1053 1054 /* Pid */ 1055 verif_pid(s, ent->pid, cpu, data); 1056 1057 if (type) 1058 /* Interrupt */ 1059 print_graph_irq(iter, addr, type, cpu, ent->pid, flags); 1060 1061 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO)) 1062 return; 1063 1064 /* Absolute time */ 1065 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1066 print_graph_abs_time(iter->ts, s); 1067 1068 /* Relative time */ 1069 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1070 print_graph_rel_time(iter, s); 1071 1072 /* Cpu */ 1073 if (flags & TRACE_GRAPH_PRINT_CPU) 1074 print_graph_cpu(s, cpu); 1075 1076 /* Proc */ 1077 if (flags & TRACE_GRAPH_PRINT_PROC) { 1078 print_graph_proc(s, ent->pid); 1079 trace_seq_puts(s, " | "); 1080 } 1081 1082 /* Latency format */ 1083 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) 1084 print_graph_lat_fmt(s, ent); 1085 1086 return; 1087 } 1088 1089 /* 1090 * Entry check for irq code 1091 * 1092 * returns 1 if 1093 * - we are inside irq code 1094 * - we just entered irq code 1095 * 1096 * returns 0 if 1097 * - funcgraph-interrupts option is set 1098 * - we are not inside irq code 1099 */ 1100 static int 1101 check_irq_entry(struct trace_iterator *iter, u32 flags, 1102 unsigned long addr, int depth) 1103 { 1104 int cpu = iter->cpu; 1105 int *depth_irq; 1106 struct fgraph_data *data = iter->private; 1107 1108 addr += iter->tr->text_delta; 1109 1110 /* 1111 * If we are either displaying irqs, or we got called as 1112 * a graph event and private data does not exist, 1113 * then we bypass the irq check. 1114 */ 1115 if ((flags & TRACE_GRAPH_PRINT_IRQS) || 1116 (!data)) 1117 return 0; 1118 1119 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq); 1120 1121 /* 1122 * We are inside the irq code 1123 */ 1124 if (*depth_irq >= 0) 1125 return 1; 1126 1127 if ((addr < (unsigned long)__irqentry_text_start) || 1128 (addr >= (unsigned long)__irqentry_text_end)) 1129 return 0; 1130 1131 /* 1132 * We are entering irq code. 1133 */ 1134 *depth_irq = depth; 1135 return 1; 1136 } 1137 1138 /* 1139 * Return check for irq code 1140 * 1141 * returns 1 if 1142 * - we are inside irq code 1143 * - we just left irq code 1144 * 1145 * returns 0 if 1146 * - funcgraph-interrupts option is set 1147 * - we are not inside irq code 1148 */ 1149 static int 1150 check_irq_return(struct trace_iterator *iter, u32 flags, int depth) 1151 { 1152 int cpu = iter->cpu; 1153 int *depth_irq; 1154 struct fgraph_data *data = iter->private; 1155 1156 /* 1157 * If we are either displaying irqs, or we got called as 1158 * a graph event and private data does not exist, 1159 * then we bypass the irq check. 1160 */ 1161 if ((flags & TRACE_GRAPH_PRINT_IRQS) || 1162 (!data)) 1163 return 0; 1164 1165 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq); 1166 1167 /* 1168 * We are not inside the irq code. 1169 */ 1170 if (*depth_irq == -1) 1171 return 0; 1172 1173 /* 1174 * We are inside the irq code, and this is returning entry. 1175 * Let's not trace it and clear the entry depth, since 1176 * we are out of irq code. 1177 * 1178 * This condition ensures that we 'leave the irq code' once 1179 * we are out of the entry depth. Thus protecting us from 1180 * the RETURN entry loss. 1181 */ 1182 if (*depth_irq >= depth) { 1183 *depth_irq = -1; 1184 return 1; 1185 } 1186 1187 /* 1188 * We are inside the irq code, and this is not the entry. 1189 */ 1190 return 1; 1191 } 1192 1193 static enum print_line_t 1194 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s, 1195 struct trace_iterator *iter, u32 flags) 1196 { 1197 struct fgraph_data *data = iter->private; 1198 struct ftrace_graph_ent *call; 1199 struct ftrace_graph_ret_entry *leaf_ret; 1200 static enum print_line_t ret; 1201 int cpu = iter->cpu; 1202 /* 1203 * print_graph_entry() may consume the current event, 1204 * thus @field may become invalid, so we need to save it. 1205 * sizeof(struct ftrace_graph_ent_entry) is very small, 1206 * it can be safely saved at the stack. 1207 */ 1208 struct ftrace_graph_ent_entry *entry; 1209 u8 save_buf[sizeof(*entry) + FTRACE_REGS_MAX_ARGS * sizeof(long)]; 1210 1211 /* The ent_size is expected to be as big as the entry */ 1212 if (iter->ent_size > sizeof(save_buf)) 1213 iter->ent_size = sizeof(save_buf); 1214 1215 entry = (void *)save_buf; 1216 memcpy(entry, field, iter->ent_size); 1217 1218 call = &entry->graph_ent; 1219 1220 if (check_irq_entry(iter, flags, call->func, call->depth)) 1221 return TRACE_TYPE_HANDLED; 1222 1223 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags); 1224 1225 leaf_ret = get_return_for_leaf(iter, entry); 1226 if (leaf_ret) 1227 ret = print_graph_entry_leaf(iter, entry, leaf_ret, s, flags); 1228 else 1229 ret = print_graph_entry_nested(iter, entry, s, cpu, flags); 1230 1231 if (data) { 1232 /* 1233 * If we failed to write our output, then we need to make 1234 * note of it. Because we already consumed our entry. 1235 */ 1236 if (s->full) { 1237 data->failed = 1; 1238 data->cpu = cpu; 1239 } else 1240 data->failed = 0; 1241 } 1242 1243 return ret; 1244 } 1245 1246 static enum print_line_t 1247 print_graph_return(struct ftrace_graph_ret_entry *retentry, struct trace_seq *s, 1248 struct trace_entry *ent, struct trace_iterator *iter, 1249 u32 flags) 1250 { 1251 struct ftrace_graph_ret *trace = &retentry->ret; 1252 u64 calltime = retentry->calltime; 1253 u64 rettime = retentry->rettime; 1254 unsigned long long duration = rettime - calltime; 1255 struct fgraph_data *data = iter->private; 1256 struct trace_array *tr = iter->tr; 1257 unsigned long func; 1258 pid_t pid = ent->pid; 1259 int cpu = iter->cpu; 1260 int func_match = 1; 1261 int i; 1262 1263 func = trace->func + iter->tr->text_delta; 1264 1265 if (check_irq_return(iter, flags, trace->depth)) 1266 return TRACE_TYPE_HANDLED; 1267 1268 if (data) { 1269 struct fgraph_cpu_data *cpu_data; 1270 int cpu = iter->cpu; 1271 1272 cpu_data = per_cpu_ptr(data->cpu_data, cpu); 1273 1274 /* 1275 * Comments display at + 1 to depth. This is the 1276 * return from a function, we now want the comments 1277 * to display at the same level of the bracket. 1278 */ 1279 cpu_data->depth = trace->depth - 1; 1280 1281 if (trace->depth < FTRACE_RETFUNC_DEPTH && 1282 !WARN_ON_ONCE(trace->depth < 0)) { 1283 if (cpu_data->enter_funcs[trace->depth] != trace->func) 1284 func_match = 0; 1285 cpu_data->enter_funcs[trace->depth] = 0; 1286 } 1287 } 1288 1289 print_graph_prologue(iter, s, 0, 0, flags); 1290 1291 /* Overhead and duration */ 1292 print_graph_duration(tr, duration, s, flags); 1293 1294 /* Closing brace */ 1295 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) 1296 trace_seq_putc(s, ' '); 1297 1298 /* 1299 * Always write out the function name and its return value if the 1300 * funcgraph-retval option is enabled. 1301 */ 1302 if (flags & __TRACE_GRAPH_PRINT_RETVAL) { 1303 print_graph_retval(s, NULL, trace, (void *)func, flags, 1304 tr->trace_flags, 0); 1305 } else { 1306 /* 1307 * If the return function does not have a matching entry, 1308 * then the entry was lost. Instead of just printing 1309 * the '}' and letting the user guess what function this 1310 * belongs to, write out the function name. Always do 1311 * that if the funcgraph-tail option is enabled. 1312 */ 1313 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL)) 1314 trace_seq_puts(s, "}"); 1315 else 1316 trace_seq_printf(s, "} /* %ps */", (void *)func); 1317 } 1318 trace_seq_putc(s, '\n'); 1319 1320 /* Overrun */ 1321 if (flags & TRACE_GRAPH_PRINT_OVERRUN) 1322 trace_seq_printf(s, " (Overruns: %u)\n", 1323 trace->overrun); 1324 1325 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, 1326 cpu, pid, flags); 1327 1328 return trace_handle_return(s); 1329 } 1330 1331 static enum print_line_t 1332 print_graph_comment(struct trace_seq *s, struct trace_entry *ent, 1333 struct trace_iterator *iter, u32 flags) 1334 { 1335 struct trace_array *tr = iter->tr; 1336 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK); 1337 struct fgraph_data *data = iter->private; 1338 struct trace_event *event; 1339 int depth = 0; 1340 int ret; 1341 int i; 1342 1343 if (data) 1344 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth; 1345 1346 print_graph_prologue(iter, s, 0, 0, flags); 1347 1348 /* No time */ 1349 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL); 1350 1351 /* Indentation */ 1352 if (depth > 0) 1353 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) 1354 trace_seq_putc(s, ' '); 1355 1356 /* The comment */ 1357 trace_seq_puts(s, "/* "); 1358 1359 switch (iter->ent->type) { 1360 case TRACE_BPUTS: 1361 ret = trace_print_bputs_msg_only(iter); 1362 if (ret != TRACE_TYPE_HANDLED) 1363 return ret; 1364 break; 1365 case TRACE_BPRINT: 1366 ret = trace_print_bprintk_msg_only(iter); 1367 if (ret != TRACE_TYPE_HANDLED) 1368 return ret; 1369 break; 1370 case TRACE_PRINT: 1371 ret = trace_print_printk_msg_only(iter); 1372 if (ret != TRACE_TYPE_HANDLED) 1373 return ret; 1374 break; 1375 default: 1376 event = ftrace_find_event(ent->type); 1377 if (!event) 1378 return TRACE_TYPE_UNHANDLED; 1379 1380 ret = event->funcs->trace(iter, sym_flags, event); 1381 if (ret != TRACE_TYPE_HANDLED) 1382 return ret; 1383 } 1384 1385 if (trace_seq_has_overflowed(s)) 1386 goto out; 1387 1388 /* Strip ending newline */ 1389 if (s->buffer[s->seq.len - 1] == '\n') { 1390 s->buffer[s->seq.len - 1] = '\0'; 1391 s->seq.len--; 1392 } 1393 1394 trace_seq_puts(s, " */\n"); 1395 out: 1396 return trace_handle_return(s); 1397 } 1398 1399 1400 enum print_line_t 1401 print_graph_function_flags(struct trace_iterator *iter, u32 flags) 1402 { 1403 struct ftrace_graph_ent_entry *field; 1404 struct fgraph_data *data = iter->private; 1405 struct trace_entry *entry = iter->ent; 1406 struct trace_seq *s = &iter->seq; 1407 int cpu = iter->cpu; 1408 int ret; 1409 1410 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) { 1411 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0; 1412 return TRACE_TYPE_HANDLED; 1413 } 1414 1415 /* 1416 * If the last output failed, there's a possibility we need 1417 * to print out the missing entry which would never go out. 1418 */ 1419 if (data && data->failed) { 1420 field = &data->ent.ent; 1421 iter->cpu = data->cpu; 1422 ret = print_graph_entry(field, s, iter, flags); 1423 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) { 1424 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1; 1425 ret = TRACE_TYPE_NO_CONSUME; 1426 } 1427 iter->cpu = cpu; 1428 return ret; 1429 } 1430 1431 switch (entry->type) { 1432 case TRACE_GRAPH_ENT: { 1433 trace_assign_type(field, entry); 1434 return print_graph_entry(field, s, iter, flags); 1435 } 1436 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 1437 case TRACE_GRAPH_RETADDR_ENT: { 1438 struct fgraph_retaddr_ent_entry saved; 1439 struct fgraph_retaddr_ent_entry *rfield; 1440 1441 trace_assign_type(rfield, entry); 1442 saved = *rfield; 1443 return print_graph_entry((struct ftrace_graph_ent_entry *)&saved, s, iter, flags); 1444 } 1445 #endif 1446 case TRACE_GRAPH_RET: { 1447 struct ftrace_graph_ret_entry *field; 1448 trace_assign_type(field, entry); 1449 return print_graph_return(field, s, entry, iter, flags); 1450 } 1451 case TRACE_STACK: 1452 case TRACE_FN: 1453 /* dont trace stack and functions as comments */ 1454 return TRACE_TYPE_UNHANDLED; 1455 1456 default: 1457 return print_graph_comment(s, entry, iter, flags); 1458 } 1459 1460 return TRACE_TYPE_HANDLED; 1461 } 1462 1463 static enum print_line_t 1464 print_graph_function(struct trace_iterator *iter) 1465 { 1466 return print_graph_function_flags(iter, tracer_flags.val); 1467 } 1468 1469 static enum print_line_t 1470 print_graph_function_event(struct trace_iterator *iter, int flags, 1471 struct trace_event *event) 1472 { 1473 return print_graph_function(iter); 1474 } 1475 1476 static void print_lat_header(struct seq_file *s, u32 flags) 1477 { 1478 static const char spaces[] = " " /* 16 spaces */ 1479 " " /* 4 spaces */ 1480 " "; /* 17 spaces */ 1481 int size = 0; 1482 1483 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1484 size += 16; 1485 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1486 size += 16; 1487 if (flags & TRACE_GRAPH_PRINT_CPU) 1488 size += 4; 1489 if (flags & TRACE_GRAPH_PRINT_PROC) 1490 size += 17; 1491 1492 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces); 1493 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces); 1494 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces); 1495 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces); 1496 seq_printf(s, "#%.*s||| / \n", size, spaces); 1497 } 1498 1499 static void __print_graph_headers_flags(struct trace_array *tr, 1500 struct seq_file *s, u32 flags) 1501 { 1502 int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT; 1503 1504 if (lat) 1505 print_lat_header(s, flags); 1506 1507 /* 1st line */ 1508 seq_putc(s, '#'); 1509 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1510 seq_puts(s, " TIME "); 1511 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1512 seq_puts(s, " REL TIME "); 1513 if (flags & TRACE_GRAPH_PRINT_CPU) 1514 seq_puts(s, " CPU"); 1515 if (flags & TRACE_GRAPH_PRINT_PROC) 1516 seq_puts(s, " TASK/PID "); 1517 if (lat) 1518 seq_puts(s, "|||| "); 1519 if (flags & TRACE_GRAPH_PRINT_DURATION) 1520 seq_puts(s, " DURATION "); 1521 seq_puts(s, " FUNCTION CALLS\n"); 1522 1523 /* 2nd line */ 1524 seq_putc(s, '#'); 1525 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) 1526 seq_puts(s, " | "); 1527 if (flags & TRACE_GRAPH_PRINT_REL_TIME) 1528 seq_puts(s, " | "); 1529 if (flags & TRACE_GRAPH_PRINT_CPU) 1530 seq_puts(s, " | "); 1531 if (flags & TRACE_GRAPH_PRINT_PROC) 1532 seq_puts(s, " | | "); 1533 if (lat) 1534 seq_puts(s, "|||| "); 1535 if (flags & TRACE_GRAPH_PRINT_DURATION) 1536 seq_puts(s, " | | "); 1537 seq_puts(s, " | | | |\n"); 1538 } 1539 1540 static void print_graph_headers(struct seq_file *s) 1541 { 1542 print_graph_headers_flags(s, tracer_flags.val); 1543 } 1544 1545 void print_graph_headers_flags(struct seq_file *s, u32 flags) 1546 { 1547 struct trace_iterator *iter = s->private; 1548 struct trace_array *tr = iter->tr; 1549 1550 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO)) 1551 return; 1552 1553 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) { 1554 /* print nothing if the buffers are empty */ 1555 if (trace_empty(iter)) 1556 return; 1557 1558 print_trace_header(s, iter); 1559 } 1560 1561 __print_graph_headers_flags(tr, s, flags); 1562 } 1563 1564 void graph_trace_open(struct trace_iterator *iter) 1565 { 1566 /* pid and depth on the last trace processed */ 1567 struct fgraph_data *data; 1568 gfp_t gfpflags; 1569 int cpu; 1570 1571 iter->private = NULL; 1572 1573 /* We can be called in atomic context via ftrace_dump() */ 1574 gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL; 1575 1576 data = kzalloc(sizeof(*data), gfpflags); 1577 if (!data) 1578 goto out_err; 1579 1580 data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags); 1581 if (!data->cpu_data) 1582 goto out_err_free; 1583 1584 for_each_possible_cpu(cpu) { 1585 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid); 1586 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth); 1587 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore); 1588 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq); 1589 1590 *pid = -1; 1591 *depth = 0; 1592 *ignore = 0; 1593 *depth_irq = -1; 1594 } 1595 1596 iter->private = data; 1597 1598 return; 1599 1600 out_err_free: 1601 kfree(data); 1602 out_err: 1603 pr_warn("function graph tracer: not enough memory\n"); 1604 } 1605 1606 void graph_trace_close(struct trace_iterator *iter) 1607 { 1608 struct fgraph_data *data = iter->private; 1609 1610 if (data) { 1611 free_percpu(data->cpu_data); 1612 kfree(data); 1613 iter->private = NULL; 1614 } 1615 } 1616 1617 static int 1618 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) 1619 { 1620 if (bit == TRACE_GRAPH_PRINT_IRQS) 1621 ftrace_graph_skip_irqs = !set; 1622 1623 if (bit == TRACE_GRAPH_SLEEP_TIME) 1624 ftrace_graph_sleep_time_control(set); 1625 1626 if (bit == TRACE_GRAPH_GRAPH_TIME) 1627 ftrace_graph_graph_time_control(set); 1628 1629 if (bit == TRACE_GRAPH_ARGS) 1630 return ftrace_graph_trace_args(tr, set); 1631 1632 return 0; 1633 } 1634 1635 static struct trace_event_functions graph_functions = { 1636 .trace = print_graph_function_event, 1637 }; 1638 1639 static struct trace_event graph_trace_entry_event = { 1640 .type = TRACE_GRAPH_ENT, 1641 .funcs = &graph_functions, 1642 }; 1643 1644 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 1645 static struct trace_event graph_trace_retaddr_entry_event = { 1646 .type = TRACE_GRAPH_RETADDR_ENT, 1647 .funcs = &graph_functions, 1648 }; 1649 #endif 1650 1651 static struct trace_event graph_trace_ret_event = { 1652 .type = TRACE_GRAPH_RET, 1653 .funcs = &graph_functions 1654 }; 1655 1656 static struct tracer graph_trace __tracer_data = { 1657 .name = "function_graph", 1658 .update_thresh = graph_trace_update_thresh, 1659 .open = graph_trace_open, 1660 .pipe_open = graph_trace_open, 1661 .close = graph_trace_close, 1662 .pipe_close = graph_trace_close, 1663 .init = graph_trace_init, 1664 .reset = graph_trace_reset, 1665 .print_line = print_graph_function, 1666 .print_header = print_graph_headers, 1667 .flags = &tracer_flags, 1668 .set_flag = func_graph_set_flag, 1669 .allow_instances = true, 1670 #ifdef CONFIG_FTRACE_SELFTEST 1671 .selftest = trace_selftest_startup_function_graph, 1672 #endif 1673 }; 1674 1675 1676 static ssize_t 1677 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt, 1678 loff_t *ppos) 1679 { 1680 unsigned long val; 1681 int ret; 1682 1683 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 1684 if (ret) 1685 return ret; 1686 1687 fgraph_max_depth = val; 1688 1689 *ppos += cnt; 1690 1691 return cnt; 1692 } 1693 1694 static ssize_t 1695 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt, 1696 loff_t *ppos) 1697 { 1698 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/ 1699 int n; 1700 1701 n = sprintf(buf, "%d\n", fgraph_max_depth); 1702 1703 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n); 1704 } 1705 1706 static const struct file_operations graph_depth_fops = { 1707 .open = tracing_open_generic, 1708 .write = graph_depth_write, 1709 .read = graph_depth_read, 1710 .llseek = generic_file_llseek, 1711 }; 1712 1713 static __init int init_graph_tracefs(void) 1714 { 1715 int ret; 1716 1717 ret = tracing_init_dentry(); 1718 if (ret) 1719 return 0; 1720 1721 trace_create_file("max_graph_depth", TRACE_MODE_WRITE, NULL, 1722 NULL, &graph_depth_fops); 1723 1724 return 0; 1725 } 1726 fs_initcall(init_graph_tracefs); 1727 1728 static __init int init_graph_trace(void) 1729 { 1730 max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1); 1731 1732 if (!register_trace_event(&graph_trace_entry_event)) { 1733 pr_warn("Warning: could not register graph trace events\n"); 1734 return 1; 1735 } 1736 1737 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR 1738 if (!register_trace_event(&graph_trace_retaddr_entry_event)) { 1739 pr_warn("Warning: could not register graph trace retaddr events\n"); 1740 return 1; 1741 } 1742 #endif 1743 1744 if (!register_trace_event(&graph_trace_ret_event)) { 1745 pr_warn("Warning: could not register graph trace events\n"); 1746 return 1; 1747 } 1748 1749 return register_tracer(&graph_trace); 1750 } 1751 1752 core_initcall(init_graph_trace); 1753