FunctionInfo.h 8.3 KB

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