func-id-helper.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===- xray-fc-account.cpp: XRay Function Call Accounting Tool ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Implementation of the helper tools dealing with XRay-generated function ids.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "func-id-helper.h"
  13. #include "llvm/Support/Path.h"
  14. #include <sstream>
  15. using namespace llvm;
  16. using namespace xray;
  17. std::string FuncIdConversionHelper::SymbolOrNumber(int32_t FuncId) const {
  18. auto CacheIt = CachedNames.find(FuncId);
  19. if (CacheIt != CachedNames.end())
  20. return CacheIt->second;
  21. std::ostringstream F;
  22. auto It = FunctionAddresses.find(FuncId);
  23. if (It == FunctionAddresses.end()) {
  24. F << "#" << FuncId;
  25. return F.str();
  26. }
  27. object::SectionedAddress ModuleAddress;
  28. ModuleAddress.Address = It->second;
  29. // TODO: set proper section index here.
  30. // object::SectionedAddress::UndefSection works for only absolute addresses.
  31. ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;
  32. if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress)) {
  33. auto &DI = *ResOrErr;
  34. if (DI.FunctionName == DILineInfo::BadString)
  35. F << "@(" << std::hex << It->second << ")";
  36. else
  37. F << DI.FunctionName;
  38. } else
  39. handleAllErrors(ResOrErr.takeError(), [&](const ErrorInfoBase &) {
  40. F << "@(" << std::hex << It->second << ")";
  41. });
  42. auto S = F.str();
  43. CachedNames[FuncId] = S;
  44. return S;
  45. }
  46. std::string FuncIdConversionHelper::FileLineAndColumn(int32_t FuncId) const {
  47. auto It = FunctionAddresses.find(FuncId);
  48. if (It == FunctionAddresses.end())
  49. return "(unknown)";
  50. std::ostringstream F;
  51. object::SectionedAddress ModuleAddress;
  52. ModuleAddress.Address = It->second;
  53. // TODO: set proper section index here.
  54. // object::SectionedAddress::UndefSection works for only absolute addresses.
  55. ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;
  56. auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress);
  57. if (!ResOrErr) {
  58. consumeError(ResOrErr.takeError());
  59. return "(unknown)";
  60. }
  61. auto &DI = *ResOrErr;
  62. F << sys::path::filename(DI.FileName).str() << ":" << DI.Line << ":"
  63. << DI.Column;
  64. return F.str();
  65. }