FunctionInfo.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include <optional>
  15. using namespace llvm;
  16. using namespace gsym;
  17. /// FunctionInfo information type that is used to encode the optional data
  18. /// that is associated with a FunctionInfo object.
  19. enum InfoType : uint32_t {
  20. EndOfList = 0u,
  21. LineTableInfo = 1u,
  22. InlineInfo = 2u
  23. };
  24. raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const FunctionInfo &FI) {
  25. OS << FI.Range << ": " << "Name=" << HEX32(FI.Name) << '\n';
  26. if (FI.OptLineTable)
  27. OS << FI.OptLineTable << '\n';
  28. if (FI.Inline)
  29. OS << FI.Inline << '\n';
  30. return OS;
  31. }
  32. llvm::Expected<FunctionInfo> FunctionInfo::decode(DataExtractor &Data,
  33. uint64_t BaseAddr) {
  34. FunctionInfo FI;
  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 = {BaseAddr, BaseAddr + 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) {
  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) {
  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. uint64_t Offset = 0;
  147. LR.FuncRange = {FuncAddr, FuncAddr + Data.getU32(&Offset)};
  148. uint32_t NameOffset = Data.getU32(&Offset);
  149. // The "lookup" functions doesn't report errors as accurately as the "decode"
  150. // function as it is meant to be fast. For more accurage errors we could call
  151. // "decode".
  152. if (!Data.isValidOffset(Offset))
  153. return createStringError(std::errc::io_error,
  154. "FunctionInfo data is truncated");
  155. // This function will be called with the result of a binary search of the
  156. // address table, we must still make sure the address does not fall into a
  157. // gap between functions are after the last function.
  158. if (LR.FuncRange.size() > 0 && !LR.FuncRange.contains(Addr))
  159. return createStringError(std::errc::io_error,
  160. "address 0x%" PRIx64 " is not in GSYM", Addr);
  161. if (NameOffset == 0)
  162. return createStringError(std::errc::io_error,
  163. "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x00000000",
  164. Offset - 4);
  165. LR.FuncName = GR.getString(NameOffset);
  166. bool Done = false;
  167. std::optional<LineEntry> LineEntry;
  168. std::optional<DataExtractor> InlineInfoData;
  169. while (!Done) {
  170. if (!Data.isValidOffsetForDataOfSize(Offset, 8))
  171. return createStringError(std::errc::io_error,
  172. "FunctionInfo data is truncated");
  173. const uint32_t IT = Data.getU32(&Offset);
  174. const uint32_t InfoLength = Data.getU32(&Offset);
  175. const StringRef InfoBytes = Data.getData().substr(Offset, InfoLength);
  176. if (InfoLength != InfoBytes.size())
  177. return createStringError(std::errc::io_error,
  178. "FunctionInfo data is truncated");
  179. DataExtractor InfoData(InfoBytes, Data.isLittleEndian(),
  180. Data.getAddressSize());
  181. switch (IT) {
  182. case InfoType::EndOfList:
  183. Done = true;
  184. break;
  185. case InfoType::LineTableInfo:
  186. if (auto ExpectedLE = LineTable::lookup(InfoData, FuncAddr, Addr))
  187. LineEntry = ExpectedLE.get();
  188. else
  189. return ExpectedLE.takeError();
  190. break;
  191. case InfoType::InlineInfo:
  192. // We will parse the inline info after our line table, but only if
  193. // we have a line entry.
  194. InlineInfoData = InfoData;
  195. break;
  196. default:
  197. break;
  198. }
  199. Offset += InfoLength;
  200. }
  201. if (!LineEntry) {
  202. // We don't have a valid line entry for our address, fill in our source
  203. // location as best we can and return.
  204. SourceLocation SrcLoc;
  205. SrcLoc.Name = LR.FuncName;
  206. SrcLoc.Offset = Addr - FuncAddr;
  207. LR.Locations.push_back(SrcLoc);
  208. return LR;
  209. }
  210. std::optional<FileEntry> LineEntryFile = GR.getFile(LineEntry->File);
  211. if (!LineEntryFile)
  212. return createStringError(std::errc::invalid_argument,
  213. "failed to extract file[%" PRIu32 "]",
  214. LineEntry->File);
  215. SourceLocation SrcLoc;
  216. SrcLoc.Name = LR.FuncName;
  217. SrcLoc.Offset = Addr - FuncAddr;
  218. SrcLoc.Dir = GR.getString(LineEntryFile->Dir);
  219. SrcLoc.Base = GR.getString(LineEntryFile->Base);
  220. SrcLoc.Line = LineEntry->Line;
  221. LR.Locations.push_back(SrcLoc);
  222. // If we don't have inline information, we are done.
  223. if (!InlineInfoData)
  224. return LR;
  225. // We have inline information. Try to augment the lookup result with this
  226. // data.
  227. llvm::Error Err = InlineInfo::lookup(GR, *InlineInfoData, FuncAddr, Addr,
  228. LR.Locations);
  229. if (Err)
  230. return std::move(Err);
  231. return LR;
  232. }