Path: blob/master/tools/perf/arch/powerpc/util/skip-callchain-idx.c
29274 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Use DWARF Debug information to skip unnecessary callchain entries.3*4* Copyright (C) 2014 Sukadev Bhattiprolu, IBM Corporation.5* Copyright (C) 2014 Ulrich Weigand, IBM Corporation.6*/7#include <inttypes.h>8#include <dwarf.h>9#include <elfutils/libdwfl.h>1011#include "util/thread.h"12#include "util/callchain.h"13#include "util/debug.h"14#include "util/dso.h"15#include "util/event.h" // struct ip_callchain16#include "util/map.h"17#include "util/symbol.h"1819/*20* When saving the callchain on Power, the kernel conservatively saves21* excess entries in the callchain. A few of these entries are needed22* in some cases but not others. If the unnecessary entries are not23* ignored, we end up with duplicate arcs in the call-graphs. Use24* DWARF debug information to skip over any unnecessary callchain25* entries.26*27* See function header for arch_adjust_callchain() below for more details.28*29* The libdwfl code in this file is based on code from elfutils30* (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc).31*/32static char *debuginfo_path;3334static const Dwfl_Callbacks offline_callbacks = {35.debuginfo_path = &debuginfo_path,36.find_debuginfo = dwfl_standard_find_debuginfo,37.section_address = dwfl_offline_section_address,38};394041/*42* Use the DWARF expression for the Call-frame-address and determine43* if return address is in LR and if a new frame was allocated.44*/45static int check_return_reg(int ra_regno, Dwarf_Frame *frame)46{47Dwarf_Op ops_mem[3];48Dwarf_Op dummy;49Dwarf_Op *ops = &dummy;50size_t nops;51int result;5253result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops);54if (result < 0) {55pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1));56return -1;57}5859/*60* Check if return address is on the stack. If return address61* is in a register (typically R0), it is yet to be saved on62* the stack.63*/64if ((nops != 0 || ops != NULL) &&65!(nops == 1 && ops[0].atom == DW_OP_regx &&66ops[0].number2 == 0 && ops[0].offset == 0))67return 0;6869/*70* Return address is in LR. Check if a frame was allocated71* but not-yet used.72*/73result = dwarf_frame_cfa(frame, &ops, &nops);74if (result < 0) {75pr_debug("dwarf_frame_cfa() returns %d, %s\n", result,76dwarf_errmsg(-1));77return -1;78}7980/*81* If call frame address is in r1, no new frame was allocated.82*/83if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 &&84ops[0].number2 == 0)85return 1;8687/*88* A new frame was allocated but has not yet been used.89*/90return 2;91}9293/*94* Get the DWARF frame from the .eh_frame section.95*/96static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc)97{98int result;99Dwarf_Addr bias;100Dwarf_CFI *cfi;101Dwarf_Frame *frame;102103cfi = dwfl_module_eh_cfi(mod, &bias);104if (!cfi) {105pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));106return NULL;107}108109result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);110if (result) {111pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));112return NULL;113}114115return frame;116}117118/*119* Get the DWARF frame from the .debug_frame section.120*/121static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc)122{123Dwarf_CFI *cfi;124Dwarf_Addr bias;125Dwarf_Frame *frame;126int result;127128cfi = dwfl_module_dwarf_cfi(mod, &bias);129if (!cfi) {130pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1));131return NULL;132}133134result = dwarf_cfi_addrframe(cfi, pc-bias, &frame);135if (result) {136pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1));137return NULL;138}139140return frame;141}142143/*144* Return:145* 0 if return address for the program counter @pc is on stack146* 1 if return address is in LR and no new stack frame was allocated147* 2 if return address is in LR and a new frame was allocated (but not148* yet used)149* -1 in case of errors150*/151static int check_return_addr(struct dso *dso, u64 map_start, Dwarf_Addr pc)152{153int rc = -1;154Dwfl *dwfl;155Dwfl_Module *mod;156Dwarf_Frame *frame;157int ra_regno;158Dwarf_Addr start = pc;159Dwarf_Addr end = pc;160bool signalp;161const char *exec_file = dso__long_name(dso);162163dwfl = RC_CHK_ACCESS(dso)->dwfl;164165if (!dwfl) {166dwfl = dwfl_begin(&offline_callbacks);167if (!dwfl) {168pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1));169return -1;170}171172mod = dwfl_report_elf(dwfl, exec_file, exec_file, -1,173map_start, false);174if (!mod) {175pr_debug("dwfl_report_elf() failed %s\n",176dwarf_errmsg(-1));177/*178* We normally cache the DWARF debug info and never179* call dwfl_end(). But to prevent fd leak, free in180* case of error.181*/182dwfl_end(dwfl);183goto out;184}185RC_CHK_ACCESS(dso)->dwfl = dwfl;186}187188mod = dwfl_addrmodule(dwfl, pc);189if (!mod) {190pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1));191goto out;192}193194/*195* To work with split debug info files (eg: glibc), check both196* .eh_frame and .debug_frame sections of the ELF header.197*/198frame = get_eh_frame(mod, pc);199if (!frame) {200frame = get_dwarf_frame(mod, pc);201if (!frame)202goto out;203}204205ra_regno = dwarf_frame_info(frame, &start, &end, &signalp);206if (ra_regno < 0) {207pr_debug("Return address register unavailable: %s\n",208dwarf_errmsg(-1));209goto out;210}211212rc = check_return_reg(ra_regno, frame);213214out:215return rc;216}217218/*219* The callchain saved by the kernel always includes the link register (LR).220*221* 0: PERF_CONTEXT_USER222* 1: Program counter (Next instruction pointer)223* 2: LR value224* 3: Caller's caller225* 4: ...226*227* The value in LR is only needed when it holds a return address. If the228* return address is on the stack, we should ignore the LR value.229*230* Further, when the return address is in the LR, if a new frame was just231* allocated but the LR was not saved into it, then the LR contains the232* caller, slot 4: contains the caller's caller and the contents of slot 3:233* (chain->ips[3]) is undefined and must be ignored.234*235* Use DWARF debug information to determine if any entries need to be skipped.236*237* Return:238* index: of callchain entry that needs to be ignored (if any)239* -1 if no entry needs to be ignored or in case of errors240*/241int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain)242{243struct addr_location al;244struct dso *dso = NULL;245int rc;246u64 ip;247u64 skip_slot = -1;248249if (!chain || chain->nr < 3)250return skip_slot;251252addr_location__init(&al);253ip = chain->ips[1];254255thread__find_symbol(thread, PERF_RECORD_MISC_USER, ip, &al);256257if (al.map)258dso = map__dso(al.map);259260if (!dso) {261pr_debug("%" PRIx64 " dso is NULL\n", ip);262addr_location__exit(&al);263return skip_slot;264}265266rc = check_return_addr(dso, map__start(al.map), ip);267268pr_debug("[DSO %s, sym %s, ip 0x%" PRIx64 "] rc %d\n",269dso__long_name(dso), al.sym->name, ip, rc);270271if (rc == 0) {272/*273* Return address on stack. Ignore LR value in callchain274*/275skip_slot = 2;276} else if (rc == 2) {277/*278* New frame allocated but return address still in LR.279* Ignore the caller's caller entry in callchain.280*/281skip_slot = 3;282}283284addr_location__exit(&al);285return skip_slot;286}287288289