Path: blob/main/contrib/llvm-project/llvm/tools/llvm-xray/func-id-helper.cpp
35231 views
//===- xray-fc-account.cpp: XRay Function Call Accounting Tool ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Implementation of the helper tools dealing with XRay-generated function ids.9//10//===----------------------------------------------------------------------===//1112#include "func-id-helper.h"13#include "llvm/Support/MemoryBuffer.h"14#include "llvm/Support/Path.h"15#include <sstream>1617using namespace llvm;18using namespace xray;1920std::string FuncIdConversionHelper::SymbolOrNumber(int32_t FuncId) const {21auto CacheIt = CachedNames.find(FuncId);22if (CacheIt != CachedNames.end())23return CacheIt->second;2425std::ostringstream F;26auto It = FunctionAddresses.find(FuncId);27if (It == FunctionAddresses.end()) {28F << "#" << FuncId;29return F.str();30}3132object::SectionedAddress ModuleAddress;33ModuleAddress.Address = It->second;34// TODO: set proper section index here.35// object::SectionedAddress::UndefSection works for only absolute addresses.36ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;37if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress)) {38auto &DI = *ResOrErr;39if (DI.FunctionName == DILineInfo::BadString)40F << "@(" << std::hex << It->second << ")";41else42F << DI.FunctionName;43} else44handleAllErrors(ResOrErr.takeError(), [&](const ErrorInfoBase &) {45F << "@(" << std::hex << It->second << ")";46});4748auto S = F.str();49CachedNames[FuncId] = S;50return S;51}5253std::string FuncIdConversionHelper::FileLineAndColumn(int32_t FuncId) const {54auto It = FunctionAddresses.find(FuncId);55if (It == FunctionAddresses.end())56return "(unknown)";5758std::ostringstream F;59object::SectionedAddress ModuleAddress;60ModuleAddress.Address = It->second;61// TODO: set proper section index here.62// object::SectionedAddress::UndefSection works for only absolute addresses.63ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;64auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress);65if (!ResOrErr) {66consumeError(ResOrErr.takeError());67return "(unknown)";68}6970auto &DI = *ResOrErr;71F << sys::path::filename(DI.FileName).str() << ":" << DI.Line << ":"72<< DI.Column;7374return F.str();75}767778