Symbolize.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Symbolize.h ----------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Header for LLVM symbolization library.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  18. #define LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  19. #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
  20. #include "llvm/Object/Binary.h"
  21. #include "llvm/Object/ObjectFile.h"
  22. #include "llvm/Object/ELFObjectFile.h"
  23. #include "llvm/Support/Error.h"
  24. #include <algorithm>
  25. #include <cstdint>
  26. #include <map>
  27. #include <memory>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. namespace llvm {
  32. namespace symbolize {
  33. using namespace object;
  34. using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
  35. using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
  36. class LLVMSymbolizer {
  37. public:
  38. struct Options {
  39. FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
  40. FileLineInfoKind PathStyle = FileLineInfoKind::AbsoluteFilePath;
  41. bool UseSymbolTable = true;
  42. bool Demangle = true;
  43. bool RelativeAddresses = false;
  44. bool UntagAddresses = false;
  45. bool UseDIA = false;
  46. std::string DefaultArch;
  47. std::vector<std::string> DsymHints;
  48. std::string FallbackDebugPath;
  49. std::string DWPName;
  50. std::vector<std::string> DebugFileDirectory;
  51. };
  52. LLVMSymbolizer() = default;
  53. LLVMSymbolizer(const Options &Opts) : Opts(Opts) {}
  54. ~LLVMSymbolizer() {
  55. flush();
  56. }
  57. Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
  58. object::SectionedAddress ModuleOffset);
  59. Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
  60. object::SectionedAddress ModuleOffset);
  61. Expected<DIInliningInfo>
  62. symbolizeInlinedCode(const std::string &ModuleName,
  63. object::SectionedAddress ModuleOffset);
  64. Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
  65. object::SectionedAddress ModuleOffset);
  66. Expected<std::vector<DILocal>>
  67. symbolizeFrame(const std::string &ModuleName,
  68. object::SectionedAddress ModuleOffset);
  69. void flush();
  70. static std::string
  71. DemangleName(const std::string &Name,
  72. const SymbolizableModule *DbiModuleDescriptor);
  73. private:
  74. // Bundles together object file with code/data and object file with
  75. // corresponding debug info. These objects can be the same.
  76. using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
  77. Expected<DILineInfo>
  78. symbolizeCodeCommon(SymbolizableModule *Info,
  79. object::SectionedAddress ModuleOffset);
  80. /// Returns a SymbolizableModule or an error if loading debug info failed.
  81. /// Only one attempt is made to load a module, and errors during loading are
  82. /// only reported once. Subsequent calls to get module info for a module that
  83. /// failed to load will return nullptr.
  84. Expected<SymbolizableModule *>
  85. getOrCreateModuleInfo(const std::string &ModuleName);
  86. Expected<SymbolizableModule *>
  87. createModuleInfo(const ObjectFile *Obj,
  88. std::unique_ptr<DIContext> Context,
  89. StringRef ModuleName);
  90. ObjectFile *lookUpDsymFile(const std::string &Path,
  91. const MachOObjectFile *ExeObj,
  92. const std::string &ArchName);
  93. ObjectFile *lookUpDebuglinkObject(const std::string &Path,
  94. const ObjectFile *Obj,
  95. const std::string &ArchName);
  96. ObjectFile *lookUpBuildIDObject(const std::string &Path,
  97. const ELFObjectFileBase *Obj,
  98. const std::string &ArchName);
  99. /// Returns pair of pointers to object and debug object.
  100. Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
  101. const std::string &ArchName);
  102. /// Return a pointer to object file at specified path, for a specified
  103. /// architecture (e.g. if path refers to a Mach-O universal binary, only one
  104. /// object file from it will be returned).
  105. Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
  106. const std::string &ArchName);
  107. std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>>
  108. Modules;
  109. /// Contains cached results of getOrCreateObjectPair().
  110. std::map<std::pair<std::string, std::string>, ObjectPair>
  111. ObjectPairForPathArch;
  112. /// Contains parsed binary for each path, or parsing error.
  113. std::map<std::string, OwningBinary<Binary>> BinaryForPath;
  114. /// Parsed object file for path/architecture pair, where "path" refers
  115. /// to Mach-O universal binary.
  116. std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
  117. ObjectForUBPathAndArch;
  118. Options Opts;
  119. };
  120. } // end namespace symbolize
  121. } // end namespace llvm
  122. #endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  123. #ifdef __GNUC__
  124. #pragma GCC diagnostic pop
  125. #endif