InlineInfo.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InlineInfo.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. #ifndef LLVM_DEBUGINFO_GSYM_INLINEINFO_H
  14. #define LLVM_DEBUGINFO_GSYM_INLINEINFO_H
  15. #include "llvm/DebugInfo/GSYM/ExtractRanges.h"
  16. #include "llvm/DebugInfo/GSYM/LineEntry.h"
  17. #include "llvm/DebugInfo/GSYM/LookupResult.h"
  18. #include "llvm/Support/Error.h"
  19. #include <stdint.h>
  20. #include <vector>
  21. namespace llvm {
  22. class raw_ostream;
  23. namespace gsym {
  24. class GsymReader;
  25. /// Inline information stores the name of the inline function along with
  26. /// an array of address ranges. It also stores the call file and call line
  27. /// that called this inline function. This allows us to unwind inline call
  28. /// stacks back to the inline or concrete function that called this
  29. /// function. Inlined functions contained in this function are stored in the
  30. /// "Children" variable. All address ranges must be sorted and all address
  31. /// ranges of all children must be contained in the ranges of this function.
  32. /// Any clients that encode information will need to ensure the ranges are
  33. /// all contined correctly or lookups could fail. Add ranges in these objects
  34. /// must be contained in the top level FunctionInfo address ranges as well.
  35. ///
  36. /// ENCODING
  37. ///
  38. /// When saved to disk, the inline info encodes all ranges to be relative to
  39. /// a parent address range. This will be the FunctionInfo's start address if
  40. /// the InlineInfo is directly contained in a FunctionInfo, or a the start
  41. /// address of the containing parent InlineInfo's first "Ranges" member. This
  42. /// allows address ranges to be efficiently encoded using ULEB128 encodings as
  43. /// we encode the offset and size of each range instead of full addresses. This
  44. /// also makes any encoded addresses easy to relocate as we just need to
  45. /// relocate the FunctionInfo's start address.
  46. ///
  47. /// - The AddressRanges member "Ranges" is encoded using an appropriate base
  48. /// address as described above.
  49. /// - UINT8 boolean value that specifies if the InlineInfo object has children.
  50. /// - UINT32 string table offset that points to the name of the inline
  51. /// function.
  52. /// - ULEB128 integer that specifies the file of the call site that called
  53. /// this function.
  54. /// - ULEB128 integer that specifies the source line of the call site that
  55. /// called this function.
  56. /// - if this object has children, enocode each child InlineInfo using the
  57. /// the first address range's start address as the base address.
  58. ///
  59. struct InlineInfo {
  60. uint32_t Name; ///< String table offset in the string table.
  61. uint32_t CallFile; ///< 1 based file index in the file table.
  62. uint32_t CallLine; ///< Source line number.
  63. AddressRanges Ranges;
  64. std::vector<InlineInfo> Children;
  65. InlineInfo() : Name(0), CallFile(0), CallLine(0) {}
  66. void clear() {
  67. Name = 0;
  68. CallFile = 0;
  69. CallLine = 0;
  70. Ranges.clear();
  71. Children.clear();
  72. }
  73. bool isValid() const { return !Ranges.empty(); }
  74. using InlineArray = std::vector<const InlineInfo *>;
  75. /// Lookup a single address within the inline info data.
  76. ///
  77. /// Clients have the option to decode an entire InlineInfo object (using
  78. /// InlineInfo::decode() ) or just find the matching inline info using this
  79. /// function. The benefit of using this function is that only the information
  80. /// needed for the lookup will be extracted, other info can be skipped and
  81. /// parsing can stop as soon as the deepest match is found. This allows
  82. /// symbolication tools to be fast and efficient and avoid allocation costs
  83. /// when doing lookups.
  84. ///
  85. /// This function will augment the SourceLocations array \a SrcLocs with any
  86. /// inline information that pertains to \a Addr. If no inline information
  87. /// exists for \a Addr, then \a SrcLocs will be left untouched. If there is
  88. /// inline information for \a Addr, then \a SrcLocs will be modifiied to
  89. /// contain the deepest most inline function's SourceLocation at index zero
  90. /// in the array and proceed up the the concrete function source file and
  91. /// line at the end of the array.
  92. ///
  93. /// \param GR The GSYM reader that contains the string and file table that
  94. /// will be used to fill in the source locations.
  95. ///
  96. /// \param Data The binary stream to read the data from. This object must
  97. /// have the data for the LineTable object starting at offset zero. The data
  98. /// can contain more data than needed.
  99. ///
  100. /// \param BaseAddr The base address to use when decoding the line table.
  101. /// This will be the FunctionInfo's start address and will be used to
  102. /// decode the correct addresses for the inline information.
  103. ///
  104. /// \param Addr The address to lookup.
  105. ///
  106. /// \param SrcLocs The inline source locations that matches \a Addr. This
  107. /// array must be initialized with the matching line entry
  108. /// from the line table upon entry. The name of the concrete
  109. /// function must be supplied since it will get pushed to
  110. /// the last SourceLocation entry and the inline information
  111. /// will fill in the source file and line from the inline
  112. /// information.
  113. ///
  114. /// \returns An error if the inline information is corrupt, or
  115. /// Error::success() for all other cases, even when no information
  116. /// is added to \a SrcLocs.
  117. static llvm::Error lookup(const GsymReader &GR, DataExtractor &Data,
  118. uint64_t BaseAddr, uint64_t Addr,
  119. SourceLocations &SrcLocs);
  120. /// Lookup an address in the InlineInfo object
  121. ///
  122. /// This function is used to symbolicate an inline call stack and can
  123. /// turn one address in the program into one or more inline call stacks
  124. /// and have the stack trace show the original call site from
  125. /// non-inlined code.
  126. ///
  127. /// \param Addr the address to lookup
  128. ///
  129. /// \returns optional vector of InlineInfo objects that describe the
  130. /// inline call stack for a given address, false otherwise.
  131. std::optional<InlineArray> getInlineStack(uint64_t Addr) const;
  132. /// Decode an InlineInfo object from a binary data stream.
  133. ///
  134. /// \param Data The binary stream to read the data from. This object must
  135. /// have the data for the InlineInfo object starting at offset zero. The data
  136. /// can contain more data than needed.
  137. ///
  138. /// \param BaseAddr The base address to use when decoding all address ranges.
  139. /// This will be the FunctionInfo's start address if this object is directly
  140. /// contained in a FunctionInfo object, or the start address of the first
  141. /// address range in an InlineInfo object of this object is a child of
  142. /// another InlineInfo object.
  143. /// \returns An InlineInfo or an error describing the issue that was
  144. /// encountered during decoding.
  145. static llvm::Expected<InlineInfo> decode(DataExtractor &Data,
  146. uint64_t BaseAddr);
  147. /// Encode this InlineInfo object into FileWriter stream.
  148. ///
  149. /// \param O The binary stream to write the data to at the current file
  150. /// position.
  151. ///
  152. /// \param BaseAddr The base address to use when encoding all address ranges.
  153. /// This will be the FunctionInfo's start address if this object is directly
  154. /// contained in a FunctionInfo object, or the start address of the first
  155. /// address range in an InlineInfo object of this object is a child of
  156. /// another InlineInfo object.
  157. ///
  158. /// \returns An error object that indicates success or failure or the
  159. /// encoding process.
  160. llvm::Error encode(FileWriter &O, uint64_t BaseAddr) const;
  161. };
  162. inline bool operator==(const InlineInfo &LHS, const InlineInfo &RHS) {
  163. return LHS.Name == RHS.Name && LHS.CallFile == RHS.CallFile &&
  164. LHS.CallLine == RHS.CallLine && LHS.Ranges == RHS.Ranges &&
  165. LHS.Children == RHS.Children;
  166. }
  167. raw_ostream &operator<<(raw_ostream &OS, const InlineInfo &FI);
  168. } // namespace gsym
  169. } // namespace llvm
  170. #endif // LLVM_DEBUGINFO_GSYM_INLINEINFO_H
  171. #ifdef __GNUC__
  172. #pragma GCC diagnostic pop
  173. #endif