xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/GSYM/GsymCreator.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
18bcb0991SDimitry Andric //===- GsymCreator.h --------------------------------------------*- C++ -*-===//
28bcb0991SDimitry Andric //
38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68bcb0991SDimitry Andric //
78bcb0991SDimitry Andric //===----------------------------------------------------------------------===//
88bcb0991SDimitry Andric 
98bcb0991SDimitry Andric #ifndef LLVM_DEBUGINFO_GSYM_GSYMCREATOR_H
108bcb0991SDimitry Andric #define LLVM_DEBUGINFO_GSYM_GSYMCREATOR_H
118bcb0991SDimitry Andric 
128bcb0991SDimitry Andric #include <functional>
138bcb0991SDimitry Andric #include <memory>
148bcb0991SDimitry Andric #include <mutex>
158bcb0991SDimitry Andric #include <thread>
168bcb0991SDimitry Andric 
1781ad6265SDimitry Andric #include "llvm/ADT/AddressRanges.h"
188bcb0991SDimitry Andric #include "llvm/ADT/ArrayRef.h"
195ffd83dbSDimitry Andric #include "llvm/ADT/StringSet.h"
208bcb0991SDimitry Andric #include "llvm/DebugInfo/GSYM/FileEntry.h"
218bcb0991SDimitry Andric #include "llvm/DebugInfo/GSYM/FunctionInfo.h"
228bcb0991SDimitry Andric #include "llvm/MC/StringTableBuilder.h"
238bcb0991SDimitry Andric #include "llvm/Support/Endian.h"
248bcb0991SDimitry Andric #include "llvm/Support/Error.h"
258bcb0991SDimitry Andric #include "llvm/Support/Path.h"
268bcb0991SDimitry Andric 
278bcb0991SDimitry Andric namespace llvm {
288bcb0991SDimitry Andric 
298bcb0991SDimitry Andric namespace gsym {
308bcb0991SDimitry Andric class FileWriter;
31*0fca6ea1SDimitry Andric class OutputAggregator;
328bcb0991SDimitry Andric 
338bcb0991SDimitry Andric /// GsymCreator is used to emit GSYM data to a stand alone file or section
348bcb0991SDimitry Andric /// within a file.
358bcb0991SDimitry Andric ///
368bcb0991SDimitry Andric /// The GsymCreator is designed to be used in 3 stages:
378bcb0991SDimitry Andric /// - Create FunctionInfo objects and add them
388bcb0991SDimitry Andric /// - Finalize the GsymCreator object
398bcb0991SDimitry Andric /// - Save to file or section
408bcb0991SDimitry Andric ///
418bcb0991SDimitry Andric /// The first stage involves creating FunctionInfo objects from another source
428bcb0991SDimitry Andric /// of information like compiler debug info metadata, DWARF or Breakpad files.
438bcb0991SDimitry Andric /// Any strings in the FunctionInfo or contained information, like InlineInfo
448bcb0991SDimitry Andric /// or LineTable objects, should get the string table offsets by calling
458bcb0991SDimitry Andric /// GsymCreator::insertString(...). Any file indexes that are needed should be
468bcb0991SDimitry Andric /// obtained by calling GsymCreator::insertFile(...). All of the function calls
478bcb0991SDimitry Andric /// in GsymCreator are thread safe. This allows multiple threads to create and
488bcb0991SDimitry Andric /// add FunctionInfo objects while parsing debug information.
498bcb0991SDimitry Andric ///
508bcb0991SDimitry Andric /// Once all of the FunctionInfo objects have been added, the
518bcb0991SDimitry Andric /// GsymCreator::finalize(...) must be called prior to saving. This function
528bcb0991SDimitry Andric /// will sort the FunctionInfo objects, finalize the string table, and do any
538bcb0991SDimitry Andric /// other passes on the information needed to prepare the information to be
548bcb0991SDimitry Andric /// saved.
558bcb0991SDimitry Andric ///
568bcb0991SDimitry Andric /// Once the object has been finalized, it can be saved to a file or section.
578bcb0991SDimitry Andric ///
588bcb0991SDimitry Andric /// ENCODING
598bcb0991SDimitry Andric ///
608bcb0991SDimitry Andric /// GSYM files are designed to be memory mapped into a process as shared, read
618bcb0991SDimitry Andric /// only data, and used as is.
628bcb0991SDimitry Andric ///
638bcb0991SDimitry Andric /// The GSYM file format when in a stand alone file consists of:
648bcb0991SDimitry Andric ///   - Header
658bcb0991SDimitry Andric ///   - Address Table
668bcb0991SDimitry Andric ///   - Function Info Offsets
678bcb0991SDimitry Andric ///   - File Table
688bcb0991SDimitry Andric ///   - String Table
698bcb0991SDimitry Andric ///   - Function Info Data
708bcb0991SDimitry Andric ///
718bcb0991SDimitry Andric /// HEADER
728bcb0991SDimitry Andric ///
738bcb0991SDimitry Andric /// The header is fully described in "llvm/DebugInfo/GSYM/Header.h".
748bcb0991SDimitry Andric ///
758bcb0991SDimitry Andric /// ADDRESS TABLE
768bcb0991SDimitry Andric ///
778bcb0991SDimitry Andric /// The address table immediately follows the header in the file and consists
788bcb0991SDimitry Andric /// of Header.NumAddresses address offsets. These offsets are sorted and can be
798bcb0991SDimitry Andric /// binary searched for efficient lookups. Addresses in the address table are
808bcb0991SDimitry Andric /// stored as offsets from a 64 bit base address found in Header.BaseAddress.
818bcb0991SDimitry Andric /// This allows the address table to contain 8, 16, or 32 offsets. This allows
828bcb0991SDimitry Andric /// the address table to not require full 64 bit addresses for each address.
838bcb0991SDimitry Andric /// The resulting GSYM size is smaller and causes fewer pages to be touched
848bcb0991SDimitry Andric /// during address lookups when the address table is smaller. The size of the
858bcb0991SDimitry Andric /// address offsets in the address table is specified in the header in
86480093f4SDimitry Andric /// Header.AddrOffSize. The first offset in the address table is aligned to
87480093f4SDimitry Andric /// Header.AddrOffSize alignment to ensure efficient access when loaded into
888bcb0991SDimitry Andric /// memory.
898bcb0991SDimitry Andric ///
908bcb0991SDimitry Andric /// FUNCTION INFO OFFSETS TABLE
918bcb0991SDimitry Andric ///
928bcb0991SDimitry Andric /// The function info offsets table immediately follows the address table and
938bcb0991SDimitry Andric /// consists of Header.NumAddresses 32 bit file offsets: one for each address
94480093f4SDimitry Andric /// in the address table. This data is aligned to a 4 byte boundary. The
958bcb0991SDimitry Andric /// offsets in this table are the relative offsets from the start offset of the
968bcb0991SDimitry Andric /// GSYM header and point to the function info data for each address in the
978bcb0991SDimitry Andric /// address table. Keeping this data separate from the address table helps to
988bcb0991SDimitry Andric /// reduce the number of pages that are touched when address lookups occur on a
998bcb0991SDimitry Andric /// GSYM file.
1008bcb0991SDimitry Andric ///
1018bcb0991SDimitry Andric /// FILE TABLE
1028bcb0991SDimitry Andric ///
1038bcb0991SDimitry Andric /// The file table immediately follows the function info offsets table. The
1048bcb0991SDimitry Andric /// encoding of the FileTable is:
1058bcb0991SDimitry Andric ///
1068bcb0991SDimitry Andric /// struct FileTable {
1078bcb0991SDimitry Andric ///   uint32_t Count;
1088bcb0991SDimitry Andric ///   FileEntry Files[];
1098bcb0991SDimitry Andric /// };
1108bcb0991SDimitry Andric ///
1118bcb0991SDimitry Andric /// The file table starts with a 32 bit count of the number of files that are
1128bcb0991SDimitry Andric /// used in all of the function info, followed by that number of FileEntry
1138bcb0991SDimitry Andric /// structures. The file table is aligned to a 4 byte boundary, Each file in
1148bcb0991SDimitry Andric /// the file table is represented with a FileEntry structure.
1158bcb0991SDimitry Andric /// See "llvm/DebugInfo/GSYM/FileEntry.h" for details.
1168bcb0991SDimitry Andric ///
1178bcb0991SDimitry Andric /// STRING TABLE
1188bcb0991SDimitry Andric ///
1198bcb0991SDimitry Andric /// The string table follows the file table in stand alone GSYM files and
1208bcb0991SDimitry Andric /// contains all strings for everything contained in the GSYM file. Any string
1218bcb0991SDimitry Andric /// data should be added to the string table and any references to strings
1228bcb0991SDimitry Andric /// inside GSYM information must be stored as 32 bit string table offsets into
1238bcb0991SDimitry Andric /// this string table. The string table always starts with an empty string at
1248bcb0991SDimitry Andric /// offset zero and is followed by any strings needed by the GSYM information.
1258bcb0991SDimitry Andric /// The start of the string table is not aligned to any boundary.
1268bcb0991SDimitry Andric ///
1278bcb0991SDimitry Andric /// FUNCTION INFO DATA
1288bcb0991SDimitry Andric ///
1298bcb0991SDimitry Andric /// The function info data is the payload that contains information about the
1308bcb0991SDimitry Andric /// address that is being looked up. It contains all of the encoded
1318bcb0991SDimitry Andric /// FunctionInfo objects. Each encoded FunctionInfo's data is pointed to by an
1328bcb0991SDimitry Andric /// entry in the Function Info Offsets Table. For details on the exact encoding
1338bcb0991SDimitry Andric /// of FunctionInfo objects, see "llvm/DebugInfo/GSYM/FunctionInfo.h".
1348bcb0991SDimitry Andric class GsymCreator {
1358bcb0991SDimitry Andric   // Private member variables require Mutex protections
136fe6060f1SDimitry Andric   mutable std::mutex Mutex;
1378bcb0991SDimitry Andric   std::vector<FunctionInfo> Funcs;
1388bcb0991SDimitry Andric   StringTableBuilder StrTab;
1395ffd83dbSDimitry Andric   StringSet<> StringStorage;
1408bcb0991SDimitry Andric   DenseMap<llvm::gsym::FileEntry, uint32_t> FileEntryToIndex;
14106c3fb27SDimitry Andric   // Needed for mapping string offsets back to the string stored in \a StrTab.
14206c3fb27SDimitry Andric   DenseMap<uint64_t, CachedHashStringRef> StringOffsetMap;
1438bcb0991SDimitry Andric   std::vector<llvm::gsym::FileEntry> Files;
1448bcb0991SDimitry Andric   std::vector<uint8_t> UUID;
145bdd1243dSDimitry Andric   std::optional<AddressRanges> ValidTextRanges;
146bdd1243dSDimitry Andric   std::optional<uint64_t> BaseAddress;
1475f757f3fSDimitry Andric   bool IsSegment = false;
1488bcb0991SDimitry Andric   bool Finalized = false;
149fe6060f1SDimitry Andric   bool Quiet;
1508bcb0991SDimitry Andric 
15106c3fb27SDimitry Andric 
15206c3fb27SDimitry Andric   /// Get the first function start address.
15306c3fb27SDimitry Andric   ///
15406c3fb27SDimitry Andric   /// \returns The start address of the first FunctionInfo or std::nullopt if
15506c3fb27SDimitry Andric   /// there are no function infos.
15606c3fb27SDimitry Andric   std::optional<uint64_t> getFirstFunctionAddress() const;
15706c3fb27SDimitry Andric 
15806c3fb27SDimitry Andric   /// Get the last function address.
15906c3fb27SDimitry Andric   ///
16006c3fb27SDimitry Andric   /// \returns The start address of the last FunctionInfo or std::nullopt if
16106c3fb27SDimitry Andric   /// there are no function infos.
16206c3fb27SDimitry Andric   std::optional<uint64_t> getLastFunctionAddress() const;
16306c3fb27SDimitry Andric 
16406c3fb27SDimitry Andric   /// Get the base address to use for this GSYM file.
16506c3fb27SDimitry Andric   ///
16606c3fb27SDimitry Andric   /// \returns The base address to put into the header and to use when creating
16706c3fb27SDimitry Andric   ///          the address offset table or std::nullpt if there are no valid
16806c3fb27SDimitry Andric   ///          function infos or if the base address wasn't specified.
16906c3fb27SDimitry Andric   std::optional<uint64_t> getBaseAddress() const;
17006c3fb27SDimitry Andric 
17106c3fb27SDimitry Andric   /// Get the size of an address offset in the address offset table.
17206c3fb27SDimitry Andric   ///
17306c3fb27SDimitry Andric   /// GSYM files store offsets from the base address in the address offset table
17406c3fb27SDimitry Andric   /// and we store the size of the address offsets in the GSYM header. This
17506c3fb27SDimitry Andric   /// function will calculate the size in bytes of these address offsets based
17606c3fb27SDimitry Andric   /// on the current contents of the GSYM file.
17706c3fb27SDimitry Andric   ///
17806c3fb27SDimitry Andric   /// \returns The size in byets of the address offsets.
17906c3fb27SDimitry Andric   uint8_t getAddressOffsetSize() const;
18006c3fb27SDimitry Andric 
18106c3fb27SDimitry Andric   /// Get the maximum address offset for the current address offset size.
18206c3fb27SDimitry Andric   ///
18306c3fb27SDimitry Andric   /// This is used when creating the address offset table to ensure we have
18406c3fb27SDimitry Andric   /// values that are in range so we don't end up truncating address offsets
18506c3fb27SDimitry Andric   /// when creating GSYM files as the code evolves.
18606c3fb27SDimitry Andric   ///
18706c3fb27SDimitry Andric   /// \returns The maximum address offset value that will be encoded into a GSYM
18806c3fb27SDimitry Andric   /// file.
18906c3fb27SDimitry Andric   uint64_t getMaxAddressOffset() const;
19006c3fb27SDimitry Andric 
19106c3fb27SDimitry Andric   /// Calculate the byte size of the GSYM header and tables sizes.
19206c3fb27SDimitry Andric   ///
19306c3fb27SDimitry Andric   /// This function will calculate the exact size in bytes of the encocded GSYM
19406c3fb27SDimitry Andric   /// for the following items:
19506c3fb27SDimitry Andric   /// - The GSYM header
19606c3fb27SDimitry Andric   /// - The Address offset table
19706c3fb27SDimitry Andric   /// - The Address info offset table
19806c3fb27SDimitry Andric   /// - The file table
19906c3fb27SDimitry Andric   /// - The string table
20006c3fb27SDimitry Andric   ///
20106c3fb27SDimitry Andric   /// This is used to help split GSYM files into segments.
20206c3fb27SDimitry Andric   ///
20306c3fb27SDimitry Andric   /// \returns Size in bytes the GSYM header and tables.
20406c3fb27SDimitry Andric   uint64_t calculateHeaderAndTableSize() const;
20506c3fb27SDimitry Andric 
20606c3fb27SDimitry Andric   /// Copy a FunctionInfo from the \a SrcGC GSYM creator into this creator.
20706c3fb27SDimitry Andric   ///
20806c3fb27SDimitry Andric   /// Copy the function info and only the needed files and strings and add a
20906c3fb27SDimitry Andric   /// converted FunctionInfo into this object. This is used to segment GSYM
21006c3fb27SDimitry Andric   /// files into separate files while only transferring the files and strings
21106c3fb27SDimitry Andric   /// that are needed from \a SrcGC.
21206c3fb27SDimitry Andric   ///
21306c3fb27SDimitry Andric   /// \param SrcGC The source gsym creator to copy from.
21406c3fb27SDimitry Andric   /// \param FuncInfoIdx The function info index within \a SrcGC to copy.
21506c3fb27SDimitry Andric   /// \returns The number of bytes it will take to encode the function info in
21606c3fb27SDimitry Andric   /// this GsymCreator. This helps calculate the size of the current GSYM
21706c3fb27SDimitry Andric   /// segment file.
21806c3fb27SDimitry Andric   uint64_t copyFunctionInfo(const GsymCreator &SrcGC, size_t FuncInfoIdx);
21906c3fb27SDimitry Andric 
22006c3fb27SDimitry Andric   /// Copy a string from \a SrcGC into this object.
22106c3fb27SDimitry Andric   ///
22206c3fb27SDimitry Andric   /// Copy a string from \a SrcGC by string table offset into this GSYM creator.
22306c3fb27SDimitry Andric   /// If a string has already been copied, the uniqued string table offset will
22406c3fb27SDimitry Andric   /// be returned, otherwise the string will be copied and a unique offset will
22506c3fb27SDimitry Andric   /// be returned.
22606c3fb27SDimitry Andric   ///
22706c3fb27SDimitry Andric   /// \param SrcGC The source gsym creator to copy from.
22806c3fb27SDimitry Andric   /// \param StrOff The string table offset from \a SrcGC to copy.
22906c3fb27SDimitry Andric   /// \returns The new string table offset of the string within this object.
23006c3fb27SDimitry Andric   uint32_t copyString(const GsymCreator &SrcGC, uint32_t StrOff);
23106c3fb27SDimitry Andric 
23206c3fb27SDimitry Andric   /// Copy a file from \a SrcGC into this object.
23306c3fb27SDimitry Andric   ///
23406c3fb27SDimitry Andric   /// Copy a file from \a SrcGC by file index into this GSYM creator. Files
23506c3fb27SDimitry Andric   /// consist of two string table entries, one for the directory and one for the
23606c3fb27SDimitry Andric   /// filename, this function will copy any needed strings ensure the file is
23706c3fb27SDimitry Andric   /// uniqued within this object. If a file already exists in this GSYM creator
23806c3fb27SDimitry Andric   /// the uniqued index will be returned, else the stirngs will be copied and
23906c3fb27SDimitry Andric   /// the new file index will be returned.
24006c3fb27SDimitry Andric   ///
24106c3fb27SDimitry Andric   /// \param SrcGC The source gsym creator to copy from.
24206c3fb27SDimitry Andric   /// \param FileIdx The 1 based file table index within \a SrcGC to copy. A
24306c3fb27SDimitry Andric   /// file index of zero will always return zero as the zero is a reserved file
24406c3fb27SDimitry Andric   /// index that means no file.
24506c3fb27SDimitry Andric   /// \returns The new file index of the file within this object.
24606c3fb27SDimitry Andric   uint32_t copyFile(const GsymCreator &SrcGC, uint32_t FileIdx);
24706c3fb27SDimitry Andric 
24806c3fb27SDimitry Andric   /// Inserts a FileEntry into the file table.
24906c3fb27SDimitry Andric   ///
25006c3fb27SDimitry Andric   /// This is used to insert a file entry in a thread safe way into this object.
25106c3fb27SDimitry Andric   ///
25206c3fb27SDimitry Andric   /// \param FE A file entry object that contains valid string table offsets
25306c3fb27SDimitry Andric   /// from this object already.
25406c3fb27SDimitry Andric   uint32_t insertFileEntry(FileEntry FE);
25506c3fb27SDimitry Andric 
25606c3fb27SDimitry Andric   /// Fixup any string and file references by updating any file indexes and
25706c3fb27SDimitry Andric   /// strings offsets in the InlineInfo parameter.
25806c3fb27SDimitry Andric   ///
25906c3fb27SDimitry Andric   /// When copying InlineInfo entries, we can simply make a copy of the object
26006c3fb27SDimitry Andric   /// and then fixup the files and strings for efficiency.
26106c3fb27SDimitry Andric   ///
26206c3fb27SDimitry Andric   /// \param SrcGC The source gsym creator to copy from.
26306c3fb27SDimitry Andric   /// \param II The inline info that contains file indexes and string offsets
26406c3fb27SDimitry Andric   /// that come from \a SrcGC. The entries will be updated by coping any files
26506c3fb27SDimitry Andric   /// and strings over into this object.
26606c3fb27SDimitry Andric   void fixupInlineInfo(const GsymCreator &SrcGC, InlineInfo &II);
26706c3fb27SDimitry Andric 
26806c3fb27SDimitry Andric   /// Save this GSYM file into segments that are roughly \a SegmentSize in size.
26906c3fb27SDimitry Andric   ///
27006c3fb27SDimitry Andric   /// When segemented GSYM files are saved to disk, they will use \a Path as a
27106c3fb27SDimitry Andric   /// prefix and then have the first function info address appended to the path
27206c3fb27SDimitry Andric   /// when each segment is saved. Each segmented GSYM file has a only the
27306c3fb27SDimitry Andric   /// strings and files that are needed to save the function infos that are in
27406c3fb27SDimitry Andric   /// each segment. These smaller files are easy to compress and download
27506c3fb27SDimitry Andric   /// separately and allow for efficient lookups with very large GSYM files and
27606c3fb27SDimitry Andric   /// segmenting them allows servers to download only the segments that are
27706c3fb27SDimitry Andric   /// needed.
27806c3fb27SDimitry Andric   ///
27906c3fb27SDimitry Andric   /// \param Path The path prefix to use when saving the GSYM files.
28006c3fb27SDimitry Andric   /// \param ByteOrder The endianness to use when saving the file.
28106c3fb27SDimitry Andric   /// \param SegmentSize The size in bytes to segment the GSYM file into.
2825f757f3fSDimitry Andric   llvm::Error saveSegments(StringRef Path, llvm::endianness ByteOrder,
28306c3fb27SDimitry Andric                            uint64_t SegmentSize) const;
28406c3fb27SDimitry Andric 
2855f757f3fSDimitry Andric   /// Let this creator know that this is a segment of another GsymCreator.
2865f757f3fSDimitry Andric   ///
2875f757f3fSDimitry Andric   /// When we have a segment, we know that function infos will be added in
2885f757f3fSDimitry Andric   /// ascending address range order without having to be finalized. We also
2895f757f3fSDimitry Andric   /// don't need to sort and unique entries during the finalize function call.
setIsSegment()2905f757f3fSDimitry Andric   void setIsSegment() {
2915f757f3fSDimitry Andric     IsSegment = true;
2925f757f3fSDimitry Andric   }
2935f757f3fSDimitry Andric 
2948bcb0991SDimitry Andric public:
295fe6060f1SDimitry Andric   GsymCreator(bool Quiet = false);
2968bcb0991SDimitry Andric 
2978bcb0991SDimitry Andric   /// Save a GSYM file to a stand alone file.
2988bcb0991SDimitry Andric   ///
2998bcb0991SDimitry Andric   /// \param Path The file path to save the GSYM file to.
3008bcb0991SDimitry Andric   /// \param ByteOrder The endianness to use when saving the file.
30106c3fb27SDimitry Andric   /// \param SegmentSize The size in bytes to segment the GSYM file into. If
30206c3fb27SDimitry Andric   ///                    this option is set this function will create N segments
30306c3fb27SDimitry Andric   ///                    that are all around \a SegmentSize bytes in size. This
30406c3fb27SDimitry Andric   ///                    allows a very large GSYM file to be broken up into
30506c3fb27SDimitry Andric   ///                    shards. Each GSYM file will have its own file table,
30606c3fb27SDimitry Andric   ///                    and string table that only have the files and strings
30706c3fb27SDimitry Andric   ///                    needed for the shared. If this argument has no value,
30806c3fb27SDimitry Andric   ///                    a single GSYM file that contains all function
30906c3fb27SDimitry Andric   ///                    information will be created.
3108bcb0991SDimitry Andric   /// \returns An error object that indicates success or failure of the save.
3115f757f3fSDimitry Andric   llvm::Error save(StringRef Path, llvm::endianness ByteOrder,
31206c3fb27SDimitry Andric                    std::optional<uint64_t> SegmentSize = std::nullopt) const;
3138bcb0991SDimitry Andric 
3148bcb0991SDimitry Andric   /// Encode a GSYM into the file writer stream at the current position.
3158bcb0991SDimitry Andric   ///
3168bcb0991SDimitry Andric   /// \param O The stream to save the binary data to
3178bcb0991SDimitry Andric   /// \returns An error object that indicates success or failure of the save.
3188bcb0991SDimitry Andric   llvm::Error encode(FileWriter &O) const;
3198bcb0991SDimitry Andric 
3208bcb0991SDimitry Andric   /// Insert a string into the GSYM string table.
3218bcb0991SDimitry Andric   ///
3228bcb0991SDimitry Andric   /// All strings used by GSYM files must be uniqued by adding them to this
3238bcb0991SDimitry Andric   /// string pool and using the returned offset for any string values.
3248bcb0991SDimitry Andric   ///
3258bcb0991SDimitry Andric   /// \param S The string to insert into the string table.
3265ffd83dbSDimitry Andric   /// \param Copy If true, then make a backing copy of the string. If false,
3275ffd83dbSDimitry Andric   ///             the string is owned by another object that will stay around
3285ffd83dbSDimitry Andric   ///             long enough for the GsymCreator to save the GSYM file.
3298bcb0991SDimitry Andric   /// \returns The unique 32 bit offset into the string table.
3305ffd83dbSDimitry Andric   uint32_t insertString(StringRef S, bool Copy = true);
3318bcb0991SDimitry Andric 
3328bcb0991SDimitry Andric   /// Insert a file into this GSYM creator.
3338bcb0991SDimitry Andric   ///
3348bcb0991SDimitry Andric   /// Inserts a file by adding a FileEntry into the "Files" member variable if
3358bcb0991SDimitry Andric   /// the file has not already been added. The file path is split into
3368bcb0991SDimitry Andric   /// directory and filename which are both added to the string table. This
3378bcb0991SDimitry Andric   /// allows paths to be stored efficiently by reusing the directories that are
3388bcb0991SDimitry Andric   /// common between multiple files.
3398bcb0991SDimitry Andric   ///
3408bcb0991SDimitry Andric   /// \param   Path The path to the file to insert.
3418bcb0991SDimitry Andric   /// \param   Style The path style for the "Path" parameter.
3428bcb0991SDimitry Andric   /// \returns The unique file index for the inserted file.
3438bcb0991SDimitry Andric   uint32_t insertFile(StringRef Path,
3448bcb0991SDimitry Andric                       sys::path::Style Style = sys::path::Style::native);
3458bcb0991SDimitry Andric 
3468bcb0991SDimitry Andric   /// Add a function info to this GSYM creator.
3478bcb0991SDimitry Andric   ///
3488bcb0991SDimitry Andric   /// All information in the FunctionInfo object must use the
3498bcb0991SDimitry Andric   /// GsymCreator::insertString(...) function when creating string table
3508bcb0991SDimitry Andric   /// offsets for names and other strings.
3518bcb0991SDimitry Andric   ///
3528bcb0991SDimitry Andric   /// \param   FI The function info object to emplace into our functions list.
3538bcb0991SDimitry Andric   void addFunctionInfo(FunctionInfo &&FI);
3548bcb0991SDimitry Andric 
3558bcb0991SDimitry Andric   /// Finalize the data in the GSYM creator prior to saving the data out.
3568bcb0991SDimitry Andric   ///
3578bcb0991SDimitry Andric   /// Finalize must be called after all FunctionInfo objects have been added
3588bcb0991SDimitry Andric   /// and before GsymCreator::save() is called.
3598bcb0991SDimitry Andric   ///
3608bcb0991SDimitry Andric   /// \param  OS Output stream to report duplicate function infos, overlapping
3618bcb0991SDimitry Andric   ///         function infos, and function infos that were merged or removed.
3628bcb0991SDimitry Andric   /// \returns An error object that indicates success or failure of the
3638bcb0991SDimitry Andric   ///          finalize.
364*0fca6ea1SDimitry Andric   llvm::Error finalize(OutputAggregator &OS);
3658bcb0991SDimitry Andric 
3668bcb0991SDimitry Andric   /// Set the UUID value.
3678bcb0991SDimitry Andric   ///
3688bcb0991SDimitry Andric   /// \param UUIDBytes The new UUID bytes.
setUUID(llvm::ArrayRef<uint8_t> UUIDBytes)3698bcb0991SDimitry Andric   void setUUID(llvm::ArrayRef<uint8_t> UUIDBytes) {
3708bcb0991SDimitry Andric     UUID.assign(UUIDBytes.begin(), UUIDBytes.end());
3718bcb0991SDimitry Andric   }
3728bcb0991SDimitry Andric 
3738bcb0991SDimitry Andric   /// Thread safe iteration over all function infos.
3748bcb0991SDimitry Andric   ///
3758bcb0991SDimitry Andric   /// \param  Callback A callback function that will get called with each
3768bcb0991SDimitry Andric   ///         FunctionInfo. If the callback returns false, stop iterating.
3778bcb0991SDimitry Andric   void forEachFunctionInfo(
3788bcb0991SDimitry Andric       std::function<bool(FunctionInfo &)> const &Callback);
3798bcb0991SDimitry Andric 
3808bcb0991SDimitry Andric   /// Thread safe const iteration over all function infos.
3818bcb0991SDimitry Andric   ///
3828bcb0991SDimitry Andric   /// \param  Callback A callback function that will get called with each
3838bcb0991SDimitry Andric   ///         FunctionInfo. If the callback returns false, stop iterating.
3848bcb0991SDimitry Andric   void forEachFunctionInfo(
3858bcb0991SDimitry Andric       std::function<bool(const FunctionInfo &)> const &Callback) const;
3868bcb0991SDimitry Andric 
3875ffd83dbSDimitry Andric   /// Get the current number of FunctionInfo objects contained in this
3885ffd83dbSDimitry Andric   /// object.
3895ffd83dbSDimitry Andric   size_t getNumFunctionInfos() const;
3905ffd83dbSDimitry Andric 
3915ffd83dbSDimitry Andric   /// Set valid .text address ranges that all functions must be contained in.
SetValidTextRanges(AddressRanges & TextRanges)3925ffd83dbSDimitry Andric   void SetValidTextRanges(AddressRanges &TextRanges) {
3935ffd83dbSDimitry Andric     ValidTextRanges = TextRanges;
3945ffd83dbSDimitry Andric   }
3955ffd83dbSDimitry Andric 
3965ffd83dbSDimitry Andric   /// Get the valid text ranges.
GetValidTextRanges()397bdd1243dSDimitry Andric   const std::optional<AddressRanges> GetValidTextRanges() const {
3985ffd83dbSDimitry Andric     return ValidTextRanges;
3995ffd83dbSDimitry Andric   }
4005ffd83dbSDimitry Andric 
4015ffd83dbSDimitry Andric   /// Check if an address is a valid code address.
4025ffd83dbSDimitry Andric   ///
4035ffd83dbSDimitry Andric   /// Any functions whose addresses do not exist within these function bounds
4045ffd83dbSDimitry Andric   /// will not be converted into the final GSYM. This allows the object file
4055ffd83dbSDimitry Andric   /// to figure out the valid file address ranges of all the code sections
4065ffd83dbSDimitry Andric   /// and ensure we don't add invalid functions to the final output. Many
4075ffd83dbSDimitry Andric   /// linkers have issues when dead stripping functions from DWARF debug info
4085ffd83dbSDimitry Andric   /// where they set the DW_AT_low_pc to zero, but newer DWARF has the
4095ffd83dbSDimitry Andric   /// DW_AT_high_pc as an offset from the DW_AT_low_pc and these size
4105ffd83dbSDimitry Andric   /// attributes have no relocations that can be applied. This results in DWARF
4115ffd83dbSDimitry Andric   /// where many functions have an DW_AT_low_pc of zero and a valid offset size
4125ffd83dbSDimitry Andric   /// for DW_AT_high_pc. If we extract all valid ranges from an object file
4135ffd83dbSDimitry Andric   /// that are marked with executable permissions, we can properly ensure that
4145ffd83dbSDimitry Andric   /// these functions are removed.
4155ffd83dbSDimitry Andric   ///
4165ffd83dbSDimitry Andric   /// \param Addr An address to check.
4175ffd83dbSDimitry Andric   ///
4185ffd83dbSDimitry Andric   /// \returns True if the address is in the valid text ranges or if no valid
4195ffd83dbSDimitry Andric   ///          text ranges have been set, false otherwise.
4205ffd83dbSDimitry Andric   bool IsValidTextAddress(uint64_t Addr) const;
4215ffd83dbSDimitry Andric 
4225ffd83dbSDimitry Andric   /// Set the base address to use for the GSYM file.
4235ffd83dbSDimitry Andric   ///
4245ffd83dbSDimitry Andric   /// Setting the base address to use for the GSYM file. Object files typically
4255ffd83dbSDimitry Andric   /// get loaded from a base address when the OS loads them into memory. Using
4265ffd83dbSDimitry Andric   /// GSYM files for symbolication becomes easier if the base address in the
4275ffd83dbSDimitry Andric   /// GSYM header is the same address as it allows addresses to be easily slid
4285ffd83dbSDimitry Andric   /// and allows symbolication without needing to find the original base
4295ffd83dbSDimitry Andric   /// address in the original object file.
4305ffd83dbSDimitry Andric   ///
4315ffd83dbSDimitry Andric   /// \param  Addr The address to use as the base address of the GSYM file
4325ffd83dbSDimitry Andric   ///              when it is saved to disk.
setBaseAddress(uint64_t Addr)4335ffd83dbSDimitry Andric   void setBaseAddress(uint64_t Addr) {
4345ffd83dbSDimitry Andric     BaseAddress = Addr;
4355ffd83dbSDimitry Andric   }
436fe6060f1SDimitry Andric 
437fe6060f1SDimitry Andric   /// Whether the transformation should be quiet, i.e. not output warnings.
isQuiet()438fe6060f1SDimitry Andric   bool isQuiet() const { return Quiet; }
43906c3fb27SDimitry Andric 
44006c3fb27SDimitry Andric 
44106c3fb27SDimitry Andric   /// Create a segmented GSYM creator starting with function info index
44206c3fb27SDimitry Andric   /// \a FuncIdx.
44306c3fb27SDimitry Andric   ///
44406c3fb27SDimitry Andric   /// This function will create a GsymCreator object that will encode into
44506c3fb27SDimitry Andric   /// roughly \a SegmentSize bytes and return it. It is used by the private
44606c3fb27SDimitry Andric   /// saveSegments(...) function and also is used by the GSYM unit tests to test
44706c3fb27SDimitry Andric   /// segmenting of GSYM files. The returned GsymCreator can be finalized and
44806c3fb27SDimitry Andric   /// encoded.
44906c3fb27SDimitry Andric   ///
45006c3fb27SDimitry Andric   /// \param [in] SegmentSize The size in bytes to roughly segment the GSYM file
45106c3fb27SDimitry Andric   /// into.
45206c3fb27SDimitry Andric   /// \param [in,out] FuncIdx The index of the first function info to encode
45306c3fb27SDimitry Andric   /// into the returned GsymCreator. This index will be updated so it can be
45406c3fb27SDimitry Andric   /// used in subsequent calls to this function to allow more segments to be
45506c3fb27SDimitry Andric   /// created.
45606c3fb27SDimitry Andric   /// \returns An expected unique pointer to a GsymCreator or an error. The
45706c3fb27SDimitry Andric   /// returned unique pointer can be NULL if there are no more functions to
45806c3fb27SDimitry Andric   /// encode.
45906c3fb27SDimitry Andric   llvm::Expected<std::unique_ptr<GsymCreator>>
46006c3fb27SDimitry Andric   createSegment(uint64_t SegmentSize, size_t &FuncIdx) const;
4618bcb0991SDimitry Andric };
4628bcb0991SDimitry Andric 
4638bcb0991SDimitry Andric } // namespace gsym
4648bcb0991SDimitry Andric } // namespace llvm
4658bcb0991SDimitry Andric 
466fe6060f1SDimitry Andric #endif // LLVM_DEBUGINFO_GSYM_GSYMCREATOR_H
467