FunctionInfo.h 8.6 KB

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