GsymCreator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- GsymCreator.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_GSYMCREATOR_H
  14. #define LLVM_DEBUGINFO_GSYM_GSYMCREATOR_H
  15. #include <functional>
  16. #include <memory>
  17. #include <mutex>
  18. #include <thread>
  19. #include "llvm/ADT/AddressRanges.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/StringSet.h"
  22. #include "llvm/DebugInfo/GSYM/FileEntry.h"
  23. #include "llvm/DebugInfo/GSYM/FunctionInfo.h"
  24. #include "llvm/MC/StringTableBuilder.h"
  25. #include "llvm/Support/Endian.h"
  26. #include "llvm/Support/Error.h"
  27. #include "llvm/Support/Path.h"
  28. namespace llvm {
  29. namespace gsym {
  30. class FileWriter;
  31. /// GsymCreator is used to emit GSYM data to a stand alone file or section
  32. /// within a file.
  33. ///
  34. /// The GsymCreator is designed to be used in 3 stages:
  35. /// - Create FunctionInfo objects and add them
  36. /// - Finalize the GsymCreator object
  37. /// - Save to file or section
  38. ///
  39. /// The first stage involves creating FunctionInfo objects from another source
  40. /// of information like compiler debug info metadata, DWARF or Breakpad files.
  41. /// Any strings in the FunctionInfo or contained information, like InlineInfo
  42. /// or LineTable objects, should get the string table offsets by calling
  43. /// GsymCreator::insertString(...). Any file indexes that are needed should be
  44. /// obtained by calling GsymCreator::insertFile(...). All of the function calls
  45. /// in GsymCreator are thread safe. This allows multiple threads to create and
  46. /// add FunctionInfo objects while parsing debug information.
  47. ///
  48. /// Once all of the FunctionInfo objects have been added, the
  49. /// GsymCreator::finalize(...) must be called prior to saving. This function
  50. /// will sort the FunctionInfo objects, finalize the string table, and do any
  51. /// other passes on the information needed to prepare the information to be
  52. /// saved.
  53. ///
  54. /// Once the object has been finalized, it can be saved to a file or section.
  55. ///
  56. /// ENCODING
  57. ///
  58. /// GSYM files are designed to be memory mapped into a process as shared, read
  59. /// only data, and used as is.
  60. ///
  61. /// The GSYM file format when in a stand alone file consists of:
  62. /// - Header
  63. /// - Address Table
  64. /// - Function Info Offsets
  65. /// - File Table
  66. /// - String Table
  67. /// - Function Info Data
  68. ///
  69. /// HEADER
  70. ///
  71. /// The header is fully described in "llvm/DebugInfo/GSYM/Header.h".
  72. ///
  73. /// ADDRESS TABLE
  74. ///
  75. /// The address table immediately follows the header in the file and consists
  76. /// of Header.NumAddresses address offsets. These offsets are sorted and can be
  77. /// binary searched for efficient lookups. Addresses in the address table are
  78. /// stored as offsets from a 64 bit base address found in Header.BaseAddress.
  79. /// This allows the address table to contain 8, 16, or 32 offsets. This allows
  80. /// the address table to not require full 64 bit addresses for each address.
  81. /// The resulting GSYM size is smaller and causes fewer pages to be touched
  82. /// during address lookups when the address table is smaller. The size of the
  83. /// address offsets in the address table is specified in the header in
  84. /// Header.AddrOffSize. The first offset in the address table is aligned to
  85. /// Header.AddrOffSize alignment to ensure efficient access when loaded into
  86. /// memory.
  87. ///
  88. /// FUNCTION INFO OFFSETS TABLE
  89. ///
  90. /// The function info offsets table immediately follows the address table and
  91. /// consists of Header.NumAddresses 32 bit file offsets: one for each address
  92. /// in the address table. This data is aligned to a 4 byte boundary. The
  93. /// offsets in this table are the relative offsets from the start offset of the
  94. /// GSYM header and point to the function info data for each address in the
  95. /// address table. Keeping this data separate from the address table helps to
  96. /// reduce the number of pages that are touched when address lookups occur on a
  97. /// GSYM file.
  98. ///
  99. /// FILE TABLE
  100. ///
  101. /// The file table immediately follows the function info offsets table. The
  102. /// encoding of the FileTable is:
  103. ///
  104. /// struct FileTable {
  105. /// uint32_t Count;
  106. /// FileEntry Files[];
  107. /// };
  108. ///
  109. /// The file table starts with a 32 bit count of the number of files that are
  110. /// used in all of the function info, followed by that number of FileEntry
  111. /// structures. The file table is aligned to a 4 byte boundary, Each file in
  112. /// the file table is represented with a FileEntry structure.
  113. /// See "llvm/DebugInfo/GSYM/FileEntry.h" for details.
  114. ///
  115. /// STRING TABLE
  116. ///
  117. /// The string table follows the file table in stand alone GSYM files and
  118. /// contains all strings for everything contained in the GSYM file. Any string
  119. /// data should be added to the string table and any references to strings
  120. /// inside GSYM information must be stored as 32 bit string table offsets into
  121. /// this string table. The string table always starts with an empty string at
  122. /// offset zero and is followed by any strings needed by the GSYM information.
  123. /// The start of the string table is not aligned to any boundary.
  124. ///
  125. /// FUNCTION INFO DATA
  126. ///
  127. /// The function info data is the payload that contains information about the
  128. /// address that is being looked up. It contains all of the encoded
  129. /// FunctionInfo objects. Each encoded FunctionInfo's data is pointed to by an
  130. /// entry in the Function Info Offsets Table. For details on the exact encoding
  131. /// of FunctionInfo objects, see "llvm/DebugInfo/GSYM/FunctionInfo.h".
  132. class GsymCreator {
  133. // Private member variables require Mutex protections
  134. mutable std::mutex Mutex;
  135. std::vector<FunctionInfo> Funcs;
  136. StringTableBuilder StrTab;
  137. StringSet<> StringStorage;
  138. DenseMap<llvm::gsym::FileEntry, uint32_t> FileEntryToIndex;
  139. std::vector<llvm::gsym::FileEntry> Files;
  140. std::vector<uint8_t> UUID;
  141. std::optional<AddressRanges> ValidTextRanges;
  142. AddressRanges Ranges;
  143. std::optional<uint64_t> BaseAddress;
  144. bool Finalized = false;
  145. bool Quiet;
  146. public:
  147. GsymCreator(bool Quiet = false);
  148. /// Save a GSYM file to a stand alone file.
  149. ///
  150. /// \param Path The file path to save the GSYM file to.
  151. /// \param ByteOrder The endianness to use when saving the file.
  152. /// \returns An error object that indicates success or failure of the save.
  153. llvm::Error save(StringRef Path, llvm::support::endianness ByteOrder) const;
  154. /// Encode a GSYM into the file writer stream at the current position.
  155. ///
  156. /// \param O The stream to save the binary data to
  157. /// \returns An error object that indicates success or failure of the save.
  158. llvm::Error encode(FileWriter &O) const;
  159. /// Insert a string into the GSYM string table.
  160. ///
  161. /// All strings used by GSYM files must be uniqued by adding them to this
  162. /// string pool and using the returned offset for any string values.
  163. ///
  164. /// \param S The string to insert into the string table.
  165. /// \param Copy If true, then make a backing copy of the string. If false,
  166. /// the string is owned by another object that will stay around
  167. /// long enough for the GsymCreator to save the GSYM file.
  168. /// \returns The unique 32 bit offset into the string table.
  169. uint32_t insertString(StringRef S, bool Copy = true);
  170. /// Insert a file into this GSYM creator.
  171. ///
  172. /// Inserts a file by adding a FileEntry into the "Files" member variable if
  173. /// the file has not already been added. The file path is split into
  174. /// directory and filename which are both added to the string table. This
  175. /// allows paths to be stored efficiently by reusing the directories that are
  176. /// common between multiple files.
  177. ///
  178. /// \param Path The path to the file to insert.
  179. /// \param Style The path style for the "Path" parameter.
  180. /// \returns The unique file index for the inserted file.
  181. uint32_t insertFile(StringRef Path,
  182. sys::path::Style Style = sys::path::Style::native);
  183. /// Add a function info to this GSYM creator.
  184. ///
  185. /// All information in the FunctionInfo object must use the
  186. /// GsymCreator::insertString(...) function when creating string table
  187. /// offsets for names and other strings.
  188. ///
  189. /// \param FI The function info object to emplace into our functions list.
  190. void addFunctionInfo(FunctionInfo &&FI);
  191. /// Finalize the data in the GSYM creator prior to saving the data out.
  192. ///
  193. /// Finalize must be called after all FunctionInfo objects have been added
  194. /// and before GsymCreator::save() is called.
  195. ///
  196. /// \param OS Output stream to report duplicate function infos, overlapping
  197. /// function infos, and function infos that were merged or removed.
  198. /// \returns An error object that indicates success or failure of the
  199. /// finalize.
  200. llvm::Error finalize(llvm::raw_ostream &OS);
  201. /// Set the UUID value.
  202. ///
  203. /// \param UUIDBytes The new UUID bytes.
  204. void setUUID(llvm::ArrayRef<uint8_t> UUIDBytes) {
  205. UUID.assign(UUIDBytes.begin(), UUIDBytes.end());
  206. }
  207. /// Thread safe iteration over all function infos.
  208. ///
  209. /// \param Callback A callback function that will get called with each
  210. /// FunctionInfo. If the callback returns false, stop iterating.
  211. void forEachFunctionInfo(
  212. std::function<bool(FunctionInfo &)> const &Callback);
  213. /// Thread safe const iteration over all function infos.
  214. ///
  215. /// \param Callback A callback function that will get called with each
  216. /// FunctionInfo. If the callback returns false, stop iterating.
  217. void forEachFunctionInfo(
  218. std::function<bool(const FunctionInfo &)> const &Callback) const;
  219. /// Get the current number of FunctionInfo objects contained in this
  220. /// object.
  221. size_t getNumFunctionInfos() const;
  222. /// Check if an address has already been added as a function info.
  223. ///
  224. /// FunctionInfo data can come from many sources: debug info, symbol tables,
  225. /// exception information, and more. Symbol tables should be added after
  226. /// debug info and can use this function to see if a symbol's start address
  227. /// has already been added to the GsymReader. Calling this before adding
  228. /// a function info from a source other than debug info avoids clients adding
  229. /// many redundant FunctionInfo objects from many sources only for them to be
  230. /// removed during the finalize() call.
  231. bool hasFunctionInfoForAddress(uint64_t Addr) const;
  232. /// Set valid .text address ranges that all functions must be contained in.
  233. void SetValidTextRanges(AddressRanges &TextRanges) {
  234. ValidTextRanges = TextRanges;
  235. }
  236. /// Get the valid text ranges.
  237. const std::optional<AddressRanges> GetValidTextRanges() const {
  238. return ValidTextRanges;
  239. }
  240. /// Check if an address is a valid code address.
  241. ///
  242. /// Any functions whose addresses do not exist within these function bounds
  243. /// will not be converted into the final GSYM. This allows the object file
  244. /// to figure out the valid file address ranges of all the code sections
  245. /// and ensure we don't add invalid functions to the final output. Many
  246. /// linkers have issues when dead stripping functions from DWARF debug info
  247. /// where they set the DW_AT_low_pc to zero, but newer DWARF has the
  248. /// DW_AT_high_pc as an offset from the DW_AT_low_pc and these size
  249. /// attributes have no relocations that can be applied. This results in DWARF
  250. /// where many functions have an DW_AT_low_pc of zero and a valid offset size
  251. /// for DW_AT_high_pc. If we extract all valid ranges from an object file
  252. /// that are marked with executable permissions, we can properly ensure that
  253. /// these functions are removed.
  254. ///
  255. /// \param Addr An address to check.
  256. ///
  257. /// \returns True if the address is in the valid text ranges or if no valid
  258. /// text ranges have been set, false otherwise.
  259. bool IsValidTextAddress(uint64_t Addr) const;
  260. /// Set the base address to use for the GSYM file.
  261. ///
  262. /// Setting the base address to use for the GSYM file. Object files typically
  263. /// get loaded from a base address when the OS loads them into memory. Using
  264. /// GSYM files for symbolication becomes easier if the base address in the
  265. /// GSYM header is the same address as it allows addresses to be easily slid
  266. /// and allows symbolication without needing to find the original base
  267. /// address in the original object file.
  268. ///
  269. /// \param Addr The address to use as the base address of the GSYM file
  270. /// when it is saved to disk.
  271. void setBaseAddress(uint64_t Addr) {
  272. BaseAddress = Addr;
  273. }
  274. /// Whether the transformation should be quiet, i.e. not output warnings.
  275. bool isQuiet() const { return Quiet; }
  276. };
  277. } // namespace gsym
  278. } // namespace llvm
  279. #endif // LLVM_DEBUGINFO_GSYM_GSYMCREATOR_H
  280. #ifdef __GNUC__
  281. #pragma GCC diagnostic pop
  282. #endif