FunctionInfo.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //===- FunctionInfo.h -------------------------------------------*- 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. #ifndef LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H
  9. #define LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H
  10. #include "llvm/ADT/Optional.h"
  11. #include "llvm/DebugInfo/GSYM/InlineInfo.h"
  12. #include "llvm/DebugInfo/GSYM/LineTable.h"
  13. #include "llvm/DebugInfo/GSYM/LookupResult.h"
  14. #include "llvm/DebugInfo/GSYM/Range.h"
  15. #include "llvm/DebugInfo/GSYM/StringTable.h"
  16. #include <cstdint>
  17. #include <tuple>
  18. namespace llvm {
  19. class raw_ostream;
  20. namespace gsym {
  21. class GsymReader;
  22. /// Function information in GSYM files encodes information for one contiguous
  23. /// address range. If a function has discontiguous address ranges, they will
  24. /// need to be encoded using multiple FunctionInfo objects.
  25. ///
  26. /// ENCODING
  27. ///
  28. /// The function information gets the function start address as an argument
  29. /// to the FunctionInfo::decode(...) function. This information is calculated
  30. /// from the GSYM header and an address offset from the GSYM address offsets
  31. /// table. The encoded FunctionInfo information must be aligned to a 4 byte
  32. /// boundary.
  33. ///
  34. /// The encoded data for a FunctionInfo starts with fixed data that all
  35. /// function info objects have:
  36. ///
  37. /// ENCODING NAME DESCRIPTION
  38. /// ========= =========== ====================================================
  39. /// uint32_t Size The size in bytes of this function.
  40. /// uint32_t Name The string table offset of the function name.
  41. ///
  42. /// The optional data in a FunctionInfo object follows this fixed information
  43. /// and consists of a stream of tuples that consist of:
  44. ///
  45. /// ENCODING NAME DESCRIPTION
  46. /// ========= =========== ====================================================
  47. /// uint32_t InfoType An "InfoType" enumeration that describes the type
  48. /// of optional data that is encoded.
  49. /// uint32_t InfoLength The size in bytes of the encoded data that
  50. /// immediately follows this length if this value is
  51. /// greater than zero.
  52. /// uint8_t[] InfoData Encoded bytes that represent the data for the
  53. /// "InfoType". These bytes are only present if
  54. /// "InfoLength" is greater than zero.
  55. ///
  56. /// The "InfoType" is an enumeration:
  57. ///
  58. /// enum InfoType {
  59. /// EndOfList = 0u,
  60. /// LineTableInfo = 1u,
  61. /// InlineInfo = 2u
  62. /// };
  63. ///
  64. /// This stream of tuples is terminated by a "InfoType" whose value is
  65. /// InfoType::EndOfList and a zero for "InfoLength". This signifies the end of
  66. /// the optional information list. This format allows us to add new optional
  67. /// information data to a FunctionInfo object over time and allows older
  68. /// clients to still parse the format and skip over any data that they don't
  69. /// understand or want to parse.
  70. ///
  71. /// So the function information encoding essientially looks like:
  72. ///
  73. /// struct {
  74. /// uint32_t Size;
  75. /// uint32_t Name;
  76. /// struct {
  77. /// uint32_t InfoType;
  78. /// uint32_t InfoLength;
  79. /// uint8_t InfoData[InfoLength];
  80. /// }[N];
  81. /// }
  82. ///
  83. /// Where "N" is the number of tuples.
  84. struct FunctionInfo {
  85. AddressRange Range;
  86. uint32_t Name; ///< String table offset in the string table.
  87. llvm::Optional<LineTable> OptLineTable;
  88. llvm::Optional<InlineInfo> Inline;
  89. FunctionInfo(uint64_t Addr = 0, uint64_t Size = 0, uint32_t N = 0)
  90. : Range(Addr, Addr + Size), Name(N) {}
  91. /// Query if a FunctionInfo has rich debug info.
  92. ///
  93. /// \returns A bool that indicates if this object has something else than
  94. /// range and name. When converting information from a symbol table and from
  95. /// debug info, we might end up with multiple FunctionInfo objects for the
  96. /// same range and we need to be able to tell which one is the better object
  97. /// to use.
  98. bool hasRichInfo() const {
  99. return OptLineTable.hasValue() || Inline.hasValue();
  100. }
  101. /// Query if a FunctionInfo object is valid.
  102. ///
  103. /// Address and size can be zero and there can be no line entries for a
  104. /// symbol so the only indication this entry is valid is if the name is
  105. /// not zero. This can happen when extracting information from symbol
  106. /// tables that do not encode symbol sizes. In that case only the
  107. /// address and name will be filled in.
  108. ///
  109. /// \returns A boolean indicating if this FunctionInfo is valid.
  110. bool isValid() const {
  111. return Name != 0;
  112. }
  113. /// Decode an object from a binary data stream.
  114. ///
  115. /// \param Data The binary stream to read the data from. This object must
  116. /// have the data for the object starting at offset zero. The data
  117. /// can contain more data than needed.
  118. ///
  119. /// \param BaseAddr The FunctionInfo's start address and will be used as the
  120. /// base address when decoding any contained information like the line table
  121. /// and the inline info.
  122. ///
  123. /// \returns An FunctionInfo or an error describing the issue that was
  124. /// encountered during decoding.
  125. static llvm::Expected<FunctionInfo> decode(DataExtractor &Data,
  126. uint64_t BaseAddr);
  127. /// Encode this object into FileWriter stream.
  128. ///
  129. /// \param O The binary stream to write the data to at the current file
  130. /// position.
  131. ///
  132. /// \returns An error object that indicates failure or the offset of the
  133. /// function info that was successfully written into the stream.
  134. llvm::Expected<uint64_t> encode(FileWriter &O) const;
  135. /// Lookup an address within a FunctionInfo object's data stream.
  136. ///
  137. /// Instead of decoding an entire FunctionInfo object when doing lookups,
  138. /// we can decode only the information we need from the FunctionInfo's data
  139. /// for the specific address. The lookup result information is returned as
  140. /// a LookupResult.
  141. ///
  142. /// \param Data The binary stream to read the data from. This object must
  143. /// have the data for the object starting at offset zero. The data
  144. /// can contain more data than needed.
  145. ///
  146. /// \param GR The GSYM reader that contains the string and file table that
  147. /// will be used to fill in information in the returned result.
  148. ///
  149. /// \param FuncAddr The function start address decoded from the GsymReader.
  150. ///
  151. /// \param Addr The address to lookup.
  152. ///
  153. /// \returns An LookupResult or an error describing the issue that was
  154. /// encountered during decoding. An error should only be returned if the
  155. /// address is not contained in the FunctionInfo or if the data is corrupted.
  156. static llvm::Expected<LookupResult> lookup(DataExtractor &Data,
  157. const GsymReader &GR,
  158. uint64_t FuncAddr,
  159. uint64_t Addr);
  160. uint64_t startAddress() const { return Range.Start; }
  161. uint64_t endAddress() const { return Range.End; }
  162. uint64_t size() const { return Range.size(); }
  163. void setStartAddress(uint64_t Addr) { Range.Start = Addr; }
  164. void setEndAddress(uint64_t Addr) { Range.End = Addr; }
  165. void setSize(uint64_t Size) { Range.End = Range.Start + Size; }
  166. void clear() {
  167. Range = {0, 0};
  168. Name = 0;
  169. OptLineTable = None;
  170. Inline = None;
  171. }
  172. };
  173. inline bool operator==(const FunctionInfo &LHS, const FunctionInfo &RHS) {
  174. return LHS.Range == RHS.Range && LHS.Name == RHS.Name &&
  175. LHS.OptLineTable == RHS.OptLineTable && LHS.Inline == RHS.Inline;
  176. }
  177. inline bool operator!=(const FunctionInfo &LHS, const FunctionInfo &RHS) {
  178. return !(LHS == RHS);
  179. }
  180. /// This sorting will order things consistently by address range first, but then
  181. /// followed by inlining being valid and line tables. We might end up with a
  182. /// FunctionInfo from debug info that will have the same range as one from the
  183. /// symbol table, but we want to quickly be able to sort and use the best version
  184. /// when creating the final GSYM file.
  185. inline bool operator<(const FunctionInfo &LHS, const FunctionInfo &RHS) {
  186. // First sort by address range
  187. if (LHS.Range != RHS.Range)
  188. return LHS.Range < RHS.Range;
  189. // Then sort by inline
  190. if (LHS.Inline.hasValue() != RHS.Inline.hasValue())
  191. return RHS.Inline.hasValue();
  192. return LHS.OptLineTable < RHS.OptLineTable;
  193. }
  194. raw_ostream &operator<<(raw_ostream &OS, const FunctionInfo &R);
  195. } // namespace gsym
  196. } // namespace llvm
  197. #endif // LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H