func-id-helper.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/MemoryBuffer.h"
  14. #include "llvm/Support/Path.h"
  15. #include <sstream>
  16. using namespace llvm;
  17. using namespace xray;
  18. std::string FuncIdConversionHelper::SymbolOrNumber(int32_t FuncId) const {
  19. auto CacheIt = CachedNames.find(FuncId);
  20. if (CacheIt != CachedNames.end())
  21. return CacheIt->second;
  22. std::ostringstream F;
  23. auto It = FunctionAddresses.find(FuncId);
  24. if (It == FunctionAddresses.end()) {
  25. F << "#" << FuncId;
  26. return F.str();
  27. }
  28. object::SectionedAddress ModuleAddress;
  29. ModuleAddress.Address = It->second;
  30. // TODO: set proper section index here.
  31. // object::SectionedAddress::UndefSection works for only absolute addresses.
  32. ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;
  33. if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress)) {
  34. auto &DI = *ResOrErr;
  35. if (DI.FunctionName == DILineInfo::BadString)
  36. F << "@(" << std::hex << It->second << ")";
  37. else
  38. F << DI.FunctionName;
  39. } else
  40. handleAllErrors(ResOrErr.takeError(), [&](const ErrorInfoBase &) {
  41. F << "@(" << std::hex << It->second << ")";
  42. });
  43. auto S = F.str();
  44. CachedNames[FuncId] = S;
  45. return S;
  46. }
  47. std::string FuncIdConversionHelper::FileLineAndColumn(int32_t FuncId) const {
  48. auto It = FunctionAddresses.find(FuncId);
  49. if (It == FunctionAddresses.end())
  50. return "(unknown)";
  51. std::ostringstream F;
  52. object::SectionedAddress ModuleAddress;
  53. ModuleAddress.Address = It->second;
  54. // TODO: set proper section index here.
  55. // object::SectionedAddress::UndefSection works for only absolute addresses.
  56. ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection;
  57. auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress);
  58. if (!ResOrErr) {
  59. consumeError(ResOrErr.takeError());
  60. return "(unknown)";
  61. }
  62. auto &DI = *ResOrErr;
  63. F << sys::path::filename(DI.FileName).str() << ":" << DI.Line << ":"
  64. << DI.Column;
  65. return F.str();
  66. }