InlineInfo.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //===- InlineInfo.cpp -------------------------------------------*- C++ -*-===//
  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/FileEntry.h"
  9. #include "llvm/DebugInfo/GSYM/FileWriter.h"
  10. #include "llvm/DebugInfo/GSYM/GsymReader.h"
  11. #include "llvm/DebugInfo/GSYM/InlineInfo.h"
  12. #include "llvm/Support/DataExtractor.h"
  13. #include <algorithm>
  14. #include <inttypes.h>
  15. using namespace llvm;
  16. using namespace gsym;
  17. raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const InlineInfo &II) {
  18. if (!II.isValid())
  19. return OS;
  20. bool First = true;
  21. for (auto Range : II.Ranges) {
  22. if (First)
  23. First = false;
  24. else
  25. OS << ' ';
  26. OS << Range;
  27. }
  28. OS << " Name = " << HEX32(II.Name) << ", CallFile = " << II.CallFile
  29. << ", CallLine = " << II.CallFile << '\n';
  30. for (const auto &Child : II.Children)
  31. OS << Child;
  32. return OS;
  33. }
  34. static bool getInlineStackHelper(const InlineInfo &II, uint64_t Addr,
  35. std::vector<const InlineInfo *> &InlineStack) {
  36. if (II.Ranges.contains(Addr)) {
  37. // If this is the top level that represents the concrete function,
  38. // there will be no name and we shoud clear the inline stack. Otherwise
  39. // we have found an inline call stack that we need to insert.
  40. if (II.Name != 0)
  41. InlineStack.insert(InlineStack.begin(), &II);
  42. for (const auto &Child : II.Children) {
  43. if (::getInlineStackHelper(Child, Addr, InlineStack))
  44. break;
  45. }
  46. return !InlineStack.empty();
  47. }
  48. return false;
  49. }
  50. llvm::Optional<InlineInfo::InlineArray> InlineInfo::getInlineStack(uint64_t Addr) const {
  51. InlineArray Result;
  52. if (getInlineStackHelper(*this, Addr, Result))
  53. return Result;
  54. return llvm::None;
  55. }
  56. /// Skip an InlineInfo object in the specified data at the specified offset.
  57. ///
  58. /// Used during the InlineInfo::lookup() call to quickly skip child InlineInfo
  59. /// objects where the addres ranges isn't contained in the InlineInfo object
  60. /// or its children. This avoids allocations by not appending child InlineInfo
  61. /// objects to the InlineInfo::Children array.
  62. ///
  63. /// \param Data The binary stream to read the data from.
  64. ///
  65. /// \param Offset The byte offset within \a Data.
  66. ///
  67. /// \param SkippedRanges If true, address ranges have already been skipped.
  68. static bool skip(DataExtractor &Data, uint64_t &Offset, bool SkippedRanges) {
  69. if (!SkippedRanges) {
  70. if (AddressRanges::skip(Data, Offset) == 0)
  71. return false;
  72. }
  73. bool HasChildren = Data.getU8(&Offset) != 0;
  74. Data.getU32(&Offset); // Skip Inline.Name.
  75. Data.getULEB128(&Offset); // Skip Inline.CallFile.
  76. Data.getULEB128(&Offset); // Skip Inline.CallLine.
  77. if (HasChildren) {
  78. while (skip(Data, Offset, false /* SkippedRanges */))
  79. /* Do nothing */;
  80. }
  81. // We skipped a valid InlineInfo.
  82. return true;
  83. }
  84. /// A Lookup helper functions.
  85. ///
  86. /// Used during the InlineInfo::lookup() call to quickly only parse an
  87. /// InlineInfo object if the address falls within this object. This avoids
  88. /// allocations by not appending child InlineInfo objects to the
  89. /// InlineInfo::Children array and also skips any InlineInfo objects that do
  90. /// not contain the address we are looking up.
  91. ///
  92. /// \param Data The binary stream to read the data from.
  93. ///
  94. /// \param Offset The byte offset within \a Data.
  95. ///
  96. /// \param BaseAddr The address that the relative address range offsets are
  97. /// relative to.
  98. static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset,
  99. uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs,
  100. llvm::Error &Err) {
  101. InlineInfo Inline;
  102. Inline.Ranges.decode(Data, BaseAddr, Offset);
  103. if (Inline.Ranges.empty())
  104. return true;
  105. // Check if the address is contained within the inline information, and if
  106. // not, quickly skip this InlineInfo object and all its children.
  107. if (!Inline.Ranges.contains(Addr)) {
  108. skip(Data, Offset, true /* SkippedRanges */);
  109. return false;
  110. }
  111. // The address range is contained within this InlineInfo, add the source
  112. // location for this InlineInfo and any children that contain the address.
  113. bool HasChildren = Data.getU8(&Offset) != 0;
  114. Inline.Name = Data.getU32(&Offset);
  115. Inline.CallFile = (uint32_t)Data.getULEB128(&Offset);
  116. Inline.CallLine = (uint32_t)Data.getULEB128(&Offset);
  117. if (HasChildren) {
  118. // Child address ranges are encoded relative to the first address in the
  119. // parent InlineInfo object.
  120. const auto ChildBaseAddr = Inline.Ranges[0].Start;
  121. bool Done = false;
  122. while (!Done)
  123. Done = lookup(GR, Data, Offset, ChildBaseAddr, Addr, SrcLocs, Err);
  124. }
  125. Optional<FileEntry> CallFile = GR.getFile(Inline.CallFile);
  126. if (!CallFile) {
  127. Err = createStringError(std::errc::invalid_argument,
  128. "failed to extract file[%" PRIu32 "]",
  129. Inline.CallFile);
  130. return false;
  131. }
  132. if (CallFile->Dir || CallFile->Base) {
  133. SourceLocation SrcLoc;
  134. SrcLoc.Name = SrcLocs.back().Name;
  135. SrcLoc.Offset = SrcLocs.back().Offset;
  136. SrcLoc.Dir = GR.getString(CallFile->Dir);
  137. SrcLoc.Base = GR.getString(CallFile->Base);
  138. SrcLoc.Line = Inline.CallLine;
  139. SrcLocs.back().Name = GR.getString(Inline.Name);
  140. SrcLocs.back().Offset = Addr - Inline.Ranges[0].Start;
  141. SrcLocs.push_back(SrcLoc);
  142. }
  143. return true;
  144. }
  145. llvm::Error InlineInfo::lookup(const GsymReader &GR, DataExtractor &Data,
  146. uint64_t BaseAddr, uint64_t Addr,
  147. SourceLocations &SrcLocs) {
  148. // Call our recursive helper function starting at offset zero.
  149. uint64_t Offset = 0;
  150. llvm::Error Err = Error::success();
  151. ::lookup(GR, Data, Offset, BaseAddr, Addr, SrcLocs, Err);
  152. return Err;
  153. }
  154. /// Decode an InlineInfo in Data at the specified offset.
  155. ///
  156. /// A local helper function to decode InlineInfo objects. This function is
  157. /// called recursively when parsing child InlineInfo objects.
  158. ///
  159. /// \param Data The data extractor to decode from.
  160. /// \param Offset The offset within \a Data to decode from.
  161. /// \param BaseAddr The base address to use when decoding address ranges.
  162. /// \returns An InlineInfo or an error describing the issue that was
  163. /// encountered during decoding.
  164. static llvm::Expected<InlineInfo> decode(DataExtractor &Data, uint64_t &Offset,
  165. uint64_t BaseAddr) {
  166. InlineInfo Inline;
  167. if (!Data.isValidOffset(Offset))
  168. return createStringError(std::errc::io_error,
  169. "0x%8.8" PRIx64 ": missing InlineInfo address ranges data", Offset);
  170. Inline.Ranges.decode(Data, BaseAddr, Offset);
  171. if (Inline.Ranges.empty())
  172. return Inline;
  173. if (!Data.isValidOffsetForDataOfSize(Offset, 1))
  174. return createStringError(std::errc::io_error,
  175. "0x%8.8" PRIx64 ": missing InlineInfo uint8_t indicating children",
  176. Offset);
  177. bool HasChildren = Data.getU8(&Offset) != 0;
  178. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  179. return createStringError(std::errc::io_error,
  180. "0x%8.8" PRIx64 ": missing InlineInfo uint32_t for name", Offset);
  181. Inline.Name = Data.getU32(&Offset);
  182. if (!Data.isValidOffset(Offset))
  183. return createStringError(std::errc::io_error,
  184. "0x%8.8" PRIx64 ": missing ULEB128 for InlineInfo call file", Offset);
  185. Inline.CallFile = (uint32_t)Data.getULEB128(&Offset);
  186. if (!Data.isValidOffset(Offset))
  187. return createStringError(std::errc::io_error,
  188. "0x%8.8" PRIx64 ": missing ULEB128 for InlineInfo call line", Offset);
  189. Inline.CallLine = (uint32_t)Data.getULEB128(&Offset);
  190. if (HasChildren) {
  191. // Child address ranges are encoded relative to the first address in the
  192. // parent InlineInfo object.
  193. const auto ChildBaseAddr = Inline.Ranges[0].Start;
  194. while (true) {
  195. llvm::Expected<InlineInfo> Child = decode(Data, Offset, ChildBaseAddr);
  196. if (!Child)
  197. return Child.takeError();
  198. // InlineInfo with empty Ranges termintes a child sibling chain.
  199. if (Child.get().Ranges.empty())
  200. break;
  201. Inline.Children.emplace_back(std::move(*Child));
  202. }
  203. }
  204. return Inline;
  205. }
  206. llvm::Expected<InlineInfo> InlineInfo::decode(DataExtractor &Data,
  207. uint64_t BaseAddr) {
  208. uint64_t Offset = 0;
  209. return ::decode(Data, Offset, BaseAddr);
  210. }
  211. llvm::Error InlineInfo::encode(FileWriter &O, uint64_t BaseAddr) const {
  212. // Users must verify the InlineInfo is valid prior to calling this funtion.
  213. // We don't want to emit any InlineInfo objects if they are not valid since
  214. // it will waste space in the GSYM file.
  215. if (!isValid())
  216. return createStringError(std::errc::invalid_argument,
  217. "attempted to encode invalid InlineInfo object");
  218. Ranges.encode(O, BaseAddr);
  219. bool HasChildren = !Children.empty();
  220. O.writeU8(HasChildren);
  221. O.writeU32(Name);
  222. O.writeULEB(CallFile);
  223. O.writeULEB(CallLine);
  224. if (HasChildren) {
  225. // Child address ranges are encoded as relative to the first
  226. // address in the Ranges for this object. This keeps the offsets
  227. // small and allows for efficient encoding using ULEB offsets.
  228. const uint64_t ChildBaseAddr = Ranges[0].Start;
  229. for (const auto &Child : Children) {
  230. // Make sure all child address ranges are contained in the parent address
  231. // ranges.
  232. for (const auto &ChildRange: Child.Ranges) {
  233. if (!Ranges.contains(ChildRange))
  234. return createStringError(std::errc::invalid_argument,
  235. "child range not contained in parent");
  236. }
  237. llvm::Error Err = Child.encode(O, ChildBaseAddr);
  238. if (Err)
  239. return Err;
  240. }
  241. // Terminate child sibling chain by emitting a zero. This zero will cause
  242. // the decodeAll() function above to return false and stop the decoding
  243. // of child InlineInfo objects that are siblings.
  244. O.writeULEB(0);
  245. }
  246. return Error::success();
  247. }