1336809Sdim//===- xray-fc-account.cpp: XRay Function Call Accounting Tool ------------===//
2336809Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336809Sdim//
7336809Sdim//===----------------------------------------------------------------------===//
8336809Sdim//
9336809Sdim// Implementation of the helper tools dealing with XRay-generated function ids.
10336809Sdim//
11336809Sdim//===----------------------------------------------------------------------===//
12336809Sdim
13336809Sdim#include "func-id-helper.h"
14336809Sdim#include "llvm/Support/Path.h"
15336809Sdim#include <sstream>
16336809Sdim
17336809Sdimusing namespace llvm;
18336809Sdimusing namespace xray;
19336809Sdim
20336809Sdimstd::string FuncIdConversionHelper::SymbolOrNumber(int32_t FuncId) const {
21336809Sdim  auto CacheIt = CachedNames.find(FuncId);
22336809Sdim  if (CacheIt != CachedNames.end())
23336809Sdim    return CacheIt->second;
24336809Sdim
25336809Sdim  std::ostringstream F;
26336809Sdim  auto It = FunctionAddresses.find(FuncId);
27336809Sdim  if (It == FunctionAddresses.end()) {
28336809Sdim    F << "#" << FuncId;
29336809Sdim    return F.str();
30336809Sdim  }
31336809Sdim
32353358Sdim  object::SectionedAddress ModuleAddress;
33353358Sdim  ModuleAddress.Address = It->second;
34353358Sdim  // TODO: set proper section index here.
35353358Sdim  // object::SectionedAddress::UndefSection works for only absolute addresses.
36353358Sdim  ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;
37353358Sdim  if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress)) {
38336809Sdim    auto &DI = *ResOrErr;
39360784Sdim    if (DI.FunctionName == DILineInfo::BadString)
40336809Sdim      F << "@(" << std::hex << It->second << ")";
41336809Sdim    else
42336809Sdim      F << DI.FunctionName;
43336809Sdim  } else
44336809Sdim    handleAllErrors(ResOrErr.takeError(), [&](const ErrorInfoBase &) {
45336809Sdim      F << "@(" << std::hex << It->second << ")";
46336809Sdim    });
47336809Sdim
48336809Sdim  auto S = F.str();
49336809Sdim  CachedNames[FuncId] = S;
50336809Sdim  return S;
51336809Sdim}
52336809Sdim
53336809Sdimstd::string FuncIdConversionHelper::FileLineAndColumn(int32_t FuncId) const {
54336809Sdim  auto It = FunctionAddresses.find(FuncId);
55336809Sdim  if (It == FunctionAddresses.end())
56336809Sdim    return "(unknown)";
57336809Sdim
58336809Sdim  std::ostringstream F;
59353358Sdim  object::SectionedAddress ModuleAddress;
60353358Sdim  ModuleAddress.Address = It->second;
61353358Sdim  // TODO: set proper section index here.
62353358Sdim  // object::SectionedAddress::UndefSection works for only absolute addresses.
63353358Sdim  ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;
64353358Sdim  auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress);
65336809Sdim  if (!ResOrErr) {
66336809Sdim    consumeError(ResOrErr.takeError());
67336809Sdim    return "(unknown)";
68336809Sdim  }
69336809Sdim
70336809Sdim  auto &DI = *ResOrErr;
71336809Sdim  F << sys::path::filename(DI.FileName).str() << ":" << DI.Line << ":"
72336809Sdim    << DI.Column;
73336809Sdim
74336809Sdim  return F.str();
75336809Sdim}
76