FunctionInfo.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //===- FunctionInfo.cpp ---------------------------------------------------===//
  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. #include "llvm/DebugInfo/GSYM/FunctionInfo.h"
  9. #include "llvm/DebugInfo/GSYM/FileWriter.h"
  10. #include "llvm/DebugInfo/GSYM/GsymReader.h"
  11. #include "llvm/DebugInfo/GSYM/LineTable.h"
  12. #include "llvm/DebugInfo/GSYM/InlineInfo.h"
  13. #include "llvm/Support/DataExtractor.h"
  14. using namespace llvm;
  15. using namespace gsym;
  16. /// FunctionInfo information type that is used to encode the optional data
  17. /// that is associated with a FunctionInfo object.
  18. enum InfoType : uint32_t {
  19. EndOfList = 0u,
  20. LineTableInfo = 1u,
  21. InlineInfo = 2u
  22. };
  23. raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const FunctionInfo &FI) {
  24. OS << FI.Range << ": " << "Name=" << HEX32(FI.Name) << '\n';
  25. if (FI.OptLineTable)
  26. OS << FI.OptLineTable << '\n';
  27. if (FI.Inline)
  28. OS << FI.Inline << '\n';
  29. return OS;
  30. }
  31. llvm::Expected<FunctionInfo> FunctionInfo::decode(DataExtractor &Data,
  32. uint64_t BaseAddr) {
  33. FunctionInfo FI;
  34. FI.Range.Start = BaseAddr;
  35. uint64_t Offset = 0;
  36. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  37. return createStringError(std::errc::io_error,
  38. "0x%8.8" PRIx64 ": missing FunctionInfo Size", Offset);
  39. FI.Range.End = FI.Range.Start + Data.getU32(&Offset);
  40. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  41. return createStringError(std::errc::io_error,
  42. "0x%8.8" PRIx64 ": missing FunctionInfo Name", Offset);
  43. FI.Name = Data.getU32(&Offset);
  44. if (FI.Name == 0)
  45. return createStringError(std::errc::io_error,
  46. "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x%8.8x",
  47. Offset - 4, FI.Name);
  48. bool Done = false;
  49. while (!Done) {
  50. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  51. return createStringError(std::errc::io_error,
  52. "0x%8.8" PRIx64 ": missing FunctionInfo InfoType value", Offset);
  53. const uint32_t IT = Data.getU32(&Offset);
  54. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  55. return createStringError(std::errc::io_error,
  56. "0x%8.8" PRIx64 ": missing FunctionInfo InfoType length", Offset);
  57. const uint32_t InfoLength = Data.getU32(&Offset);
  58. if (!Data.isValidOffsetForDataOfSize(Offset, InfoLength))
  59. return createStringError(std::errc::io_error,
  60. "0x%8.8" PRIx64 ": missing FunctionInfo data for InfoType %u",
  61. Offset, IT);
  62. DataExtractor InfoData(Data.getData().substr(Offset, InfoLength),
  63. Data.isLittleEndian(),
  64. Data.getAddressSize());
  65. switch (IT) {
  66. case InfoType::EndOfList:
  67. Done = true;
  68. break;
  69. case InfoType::LineTableInfo:
  70. if (Expected<LineTable> LT = LineTable::decode(InfoData, BaseAddr))
  71. FI.OptLineTable = std::move(LT.get());
  72. else
  73. return LT.takeError();
  74. break;
  75. case InfoType::InlineInfo:
  76. if (Expected<InlineInfo> II = InlineInfo::decode(InfoData, BaseAddr))
  77. FI.Inline = std::move(II.get());
  78. else
  79. return II.takeError();
  80. break;
  81. default:
  82. return createStringError(std::errc::io_error,
  83. "0x%8.8" PRIx64 ": unsupported InfoType %u",
  84. Offset-8, IT);
  85. }
  86. Offset += InfoLength;
  87. }
  88. return std::move(FI);
  89. }
  90. llvm::Expected<uint64_t> FunctionInfo::encode(FileWriter &O) const {
  91. if (!isValid())
  92. return createStringError(std::errc::invalid_argument,
  93. "attempted to encode invalid FunctionInfo object");
  94. // Align FunctionInfo data to a 4 byte alignment.
  95. O.alignTo(4);
  96. const uint64_t FuncInfoOffset = O.tell();
  97. // Write the size in bytes of this function as a uint32_t. This can be zero
  98. // if we just have a symbol from a symbol table and that symbol has no size.
  99. O.writeU32(size());
  100. // Write the name of this function as a uint32_t string table offset.
  101. O.writeU32(Name);
  102. if (OptLineTable.hasValue()) {
  103. O.writeU32(InfoType::LineTableInfo);
  104. // Write a uint32_t length as zero for now, we will fix this up after
  105. // writing the LineTable out with the number of bytes that were written.
  106. O.writeU32(0);
  107. const auto StartOffset = O.tell();
  108. llvm::Error err = OptLineTable->encode(O, Range.Start);
  109. if (err)
  110. return std::move(err);
  111. const auto Length = O.tell() - StartOffset;
  112. if (Length > UINT32_MAX)
  113. return createStringError(std::errc::invalid_argument,
  114. "LineTable length is greater than UINT32_MAX");
  115. // Fixup the size of the LineTable data with the correct size.
  116. O.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
  117. }
  118. // Write out the inline function info if we have any and if it is valid.
  119. if (Inline.hasValue()) {
  120. O.writeU32(InfoType::InlineInfo);
  121. // Write a uint32_t length as zero for now, we will fix this up after
  122. // writing the LineTable out with the number of bytes that were written.
  123. O.writeU32(0);
  124. const auto StartOffset = O.tell();
  125. llvm::Error err = Inline->encode(O, Range.Start);
  126. if (err)
  127. return std::move(err);
  128. const auto Length = O.tell() - StartOffset;
  129. if (Length > UINT32_MAX)
  130. return createStringError(std::errc::invalid_argument,
  131. "InlineInfo length is greater than UINT32_MAX");
  132. // Fixup the size of the InlineInfo data with the correct size.
  133. O.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
  134. }
  135. // Terminate the data chunks with and end of list with zero size
  136. O.writeU32(InfoType::EndOfList);
  137. O.writeU32(0);
  138. return FuncInfoOffset;
  139. }
  140. llvm::Expected<LookupResult> FunctionInfo::lookup(DataExtractor &Data,
  141. const GsymReader &GR,
  142. uint64_t FuncAddr,
  143. uint64_t Addr) {
  144. LookupResult LR;
  145. LR.LookupAddr = Addr;
  146. LR.FuncRange.Start = FuncAddr;
  147. uint64_t Offset = 0;
  148. LR.FuncRange.End = FuncAddr + Data.getU32(&Offset);
  149. uint32_t NameOffset = Data.getU32(&Offset);
  150. // The "lookup" functions doesn't report errors as accurately as the "decode"
  151. // function as it is meant to be fast. For more accurage errors we could call
  152. // "decode".
  153. if (!Data.isValidOffset(Offset))
  154. return createStringError(std::errc::io_error,
  155. "FunctionInfo data is truncated");
  156. // This function will be called with the result of a binary search of the
  157. // address table, we must still make sure the address does not fall into a
  158. // gap between functions are after the last function.
  159. if (LR.FuncRange.size() > 0 && !LR.FuncRange.contains(Addr))
  160. return createStringError(std::errc::io_error,
  161. "address 0x%" PRIx64 " is not in GSYM", Addr);
  162. if (NameOffset == 0)
  163. return createStringError(std::errc::io_error,
  164. "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x00000000",
  165. Offset - 4);
  166. LR.FuncName = GR.getString(NameOffset);
  167. bool Done = false;
  168. Optional<LineEntry> LineEntry;
  169. Optional<DataExtractor> InlineInfoData;
  170. while (!Done) {
  171. if (!Data.isValidOffsetForDataOfSize(Offset, 8))
  172. return createStringError(std::errc::io_error,
  173. "FunctionInfo data is truncated");
  174. const uint32_t IT = Data.getU32(&Offset);
  175. const uint32_t InfoLength = Data.getU32(&Offset);
  176. const StringRef InfoBytes = Data.getData().substr(Offset, InfoLength);
  177. if (InfoLength != InfoBytes.size())
  178. return createStringError(std::errc::io_error,
  179. "FunctionInfo data is truncated");
  180. DataExtractor InfoData(InfoBytes, Data.isLittleEndian(),
  181. Data.getAddressSize());
  182. switch (IT) {
  183. case InfoType::EndOfList:
  184. Done = true;
  185. break;
  186. case InfoType::LineTableInfo:
  187. if (auto ExpectedLE = LineTable::lookup(InfoData, FuncAddr, Addr))
  188. LineEntry = ExpectedLE.get();
  189. else
  190. return ExpectedLE.takeError();
  191. break;
  192. case InfoType::InlineInfo:
  193. // We will parse the inline info after our line table, but only if
  194. // we have a line entry.
  195. InlineInfoData = InfoData;
  196. break;
  197. default:
  198. break;
  199. }
  200. Offset += InfoLength;
  201. }
  202. if (!LineEntry) {
  203. // We don't have a valid line entry for our address, fill in our source
  204. // location as best we can and return.
  205. SourceLocation SrcLoc;
  206. SrcLoc.Name = LR.FuncName;
  207. SrcLoc.Offset = Addr - FuncAddr;
  208. LR.Locations.push_back(SrcLoc);
  209. return LR;
  210. }
  211. Optional<FileEntry> LineEntryFile = GR.getFile(LineEntry->File);
  212. if (!LineEntryFile)
  213. return createStringError(std::errc::invalid_argument,
  214. "failed to extract file[%" PRIu32 "]",
  215. LineEntry->File);
  216. SourceLocation SrcLoc;
  217. SrcLoc.Name = LR.FuncName;
  218. SrcLoc.Offset = Addr - FuncAddr;
  219. SrcLoc.Dir = GR.getString(LineEntryFile->Dir);
  220. SrcLoc.Base = GR.getString(LineEntryFile->Base);
  221. SrcLoc.Line = LineEntry->Line;
  222. LR.Locations.push_back(SrcLoc);
  223. // If we don't have inline information, we are done.
  224. if (!InlineInfoData)
  225. return LR;
  226. // We have inline information. Try to augment the lookup result with this
  227. // data.
  228. llvm::Error Err = InlineInfo::lookup(GR, *InlineInfoData, FuncAddr, Addr,
  229. LR.Locations);
  230. if (Err)
  231. return std::move(Err);
  232. return LR;
  233. }