Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf script: Display PERF_RECORD_CALLCHAIN_DEFERRED

Handle the deferred callchains in the script output.

$ perf script
...
pwd 2312 121.163435: 249113 cpu/cycles/P:
ffffffff845b78d8 __build_id_parse.isra.0+0x218 ([kernel.kallsyms])
ffffffff83bb5bf6 perf_event_mmap+0x2e6 ([kernel.kallsyms])
ffffffff83c31959 mprotect_fixup+0x1e9 ([kernel.kallsyms])
ffffffff83c31dc5 do_mprotect_pkey+0x2b5 ([kernel.kallsyms])
ffffffff83c3206f __x64_sys_mprotect+0x1f ([kernel.kallsyms])
ffffffff845e6692 do_syscall_64+0x62 ([kernel.kallsyms])
ffffffff8360012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
b00000006 (cookie) ([unknown])

pwd 2312 121.163447: DEFERRED CALLCHAIN [cookie: b00000006]
7f18fe337fa7 mprotect+0x7 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe330e0f _dl_sysdep_start+0x7f (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
7f18fe331448 _dl_start_user+0x0 (/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)

Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

+93 -1
+89
tools/perf/builtin-script.c
··· 2706 2706 return ret; 2707 2707 } 2708 2708 2709 + static int process_deferred_sample_event(const struct perf_tool *tool, 2710 + union perf_event *event, 2711 + struct perf_sample *sample, 2712 + struct evsel *evsel, 2713 + struct machine *machine) 2714 + { 2715 + struct perf_script *scr = container_of(tool, struct perf_script, tool); 2716 + struct perf_event_attr *attr = &evsel->core.attr; 2717 + struct evsel_script *es = evsel->priv; 2718 + unsigned int type = output_type(attr->type); 2719 + struct addr_location al; 2720 + FILE *fp = es->fp; 2721 + int ret = 0; 2722 + 2723 + if (output[type].fields == 0) 2724 + return 0; 2725 + 2726 + /* Set thread to NULL to indicate addr_al and al are not initialized */ 2727 + addr_location__init(&al); 2728 + 2729 + if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num, 2730 + sample->time)) { 2731 + goto out_put; 2732 + } 2733 + 2734 + if (debug_mode) { 2735 + if (sample->time < last_timestamp) { 2736 + pr_err("Samples misordered, previous: %" PRIu64 2737 + " this: %" PRIu64 "\n", last_timestamp, 2738 + sample->time); 2739 + nr_unordered++; 2740 + } 2741 + last_timestamp = sample->time; 2742 + goto out_put; 2743 + } 2744 + 2745 + if (filter_cpu(sample)) 2746 + goto out_put; 2747 + 2748 + if (machine__resolve(machine, &al, sample) < 0) { 2749 + pr_err("problem processing %d event, skipping it.\n", 2750 + event->header.type); 2751 + ret = -1; 2752 + goto out_put; 2753 + } 2754 + 2755 + if (al.filtered) 2756 + goto out_put; 2757 + 2758 + if (!show_event(sample, evsel, al.thread, &al, NULL)) 2759 + goto out_put; 2760 + 2761 + if (evswitch__discard(&scr->evswitch, evsel)) 2762 + goto out_put; 2763 + 2764 + perf_sample__fprintf_start(scr, sample, al.thread, evsel, 2765 + PERF_RECORD_CALLCHAIN_DEFERRED, fp); 2766 + fprintf(fp, "DEFERRED CALLCHAIN [cookie: %llx]", 2767 + (unsigned long long)event->callchain_deferred.cookie); 2768 + 2769 + if (PRINT_FIELD(IP)) { 2770 + struct callchain_cursor *cursor = NULL; 2771 + 2772 + if (symbol_conf.use_callchain && sample->callchain) { 2773 + cursor = get_tls_callchain_cursor(); 2774 + if (thread__resolve_callchain(al.thread, cursor, evsel, 2775 + sample, NULL, NULL, 2776 + scripting_max_stack)) { 2777 + pr_info("cannot resolve deferred callchains\n"); 2778 + cursor = NULL; 2779 + } 2780 + } 2781 + 2782 + fputc(cursor ? '\n' : ' ', fp); 2783 + sample__fprintf_sym(sample, &al, 0, output[type].print_ip_opts, 2784 + cursor, symbol_conf.bt_stop_list, fp); 2785 + } 2786 + 2787 + fprintf(fp, "\n"); 2788 + 2789 + if (verbose > 0) 2790 + fflush(fp); 2791 + 2792 + out_put: 2793 + addr_location__exit(&al); 2794 + return ret; 2795 + } 2796 + 2709 2797 // Used when scr->per_event_dump is not set 2710 2798 static struct evsel_script es_stdout; 2711 2799 ··· 4391 4303 4392 4304 perf_tool__init(&script.tool, !unsorted_dump); 4393 4305 script.tool.sample = process_sample_event; 4306 + script.tool.callchain_deferred = process_deferred_sample_event; 4394 4307 script.tool.mmap = perf_event__process_mmap; 4395 4308 script.tool.mmap2 = perf_event__process_mmap2; 4396 4309 script.tool.comm = perf_event__process_comm;
+4 -1
tools/perf/util/evsel_fprintf.c
··· 168 168 node_al.addr = addr; 169 169 node_al.map = map__get(map); 170 170 171 - if (print_symoffset) { 171 + if (sample->deferred_callchain && 172 + sample->deferred_cookie == node->ip) { 173 + printed += fprintf(fp, "(cookie)"); 174 + } else if (print_symoffset) { 172 175 printed += __symbol__fprintf_symname_offs(sym, &node_al, 173 176 print_unknown_as_addr, 174 177 true, fp);