41 std::lock_guard<std::mutex> Guard(
Mutex);
42 const auto NextIndex = Files.size();
44 auto R = FileEntryToIndex.
insert(std::make_pair(FE, NextIndex));
46 Files.emplace_back(FE);
47 return R.first->second;
56 const FileEntry SrcFE = SrcGC.Files[FileIdx];
58 uint32_t Dir = StrTab.
add(SrcGC.StringOffsetMap.find(SrcFE.
Dir)->second);
61 return insertFileEntry(DstFE);
65 std::optional<uint64_t> SegmentSize)
const {
67 return saveSegments(Path, ByteOrder, *SegmentSize);
77 std::lock_guard<std::mutex> Guard(
Mutex);
80 "no functions to encode");
83 "GsymCreator wasn't finalized prior to encoding");
85 if (Funcs.size() > UINT32_MAX)
87 "too many FunctionInfos");
89 std::optional<uint64_t> BaseAddress = getBaseAddress();
93 "invalid base address");
103 memset(Hdr.
UUID, 0,
sizeof(Hdr.
UUID));
115 const uint64_t MaxAddressOffset = getMaxAddressOffset();
118 for (
const auto &FuncInfo : Funcs) {
124 assert(AddrOffset <= MaxAddressOffset);
125 (void)MaxAddressOffset;
128 O.writeU8(
static_cast<uint8_t
>(AddrOffset));
131 O.writeU16(
static_cast<uint16_t>(AddrOffset));
134 O.writeU32(
static_cast<uint32_t>(AddrOffset));
137 O.writeU64(AddrOffset);
144 const off_t AddrInfoOffsetsOffset = O.tell();
145 for (
size_t i = 0, n = Funcs.size(); i < n; ++i)
151 assert(Files[0].Dir == 0);
153 size_t NumFiles = Files.size();
154 if (NumFiles > UINT32_MAX)
156 O.writeU32(
static_cast<uint32_t>(NumFiles));
157 for (
auto File : Files) {
158 O.writeU32(File.Dir);
159 O.writeU32(File.Base);
163 const off_t StrtabOffset = O.tell();
164 StrTab.
write(O.get_stream());
165 const off_t StrtabSize = O.tell() - StrtabOffset;
166 std::vector<uint32_t> AddrInfoOffsets;
169 for (
const auto &FuncInfo : Funcs) {
171 AddrInfoOffsets.push_back(OffsetOrErr.get());
173 return OffsetOrErr.takeError();
181 for (
auto AddrInfoOffset : AddrInfoOffsets) {
182 O.fixup32(AddrInfoOffset, AddrInfoOffsetsOffset +
Offset);
189 std::lock_guard<std::mutex> Guard(
Mutex);
218 const auto NumBefore = Funcs.size();
227 std::vector<FunctionInfo> FinalizedFuncs;
228 FinalizedFuncs.reserve(Funcs.size());
229 FinalizedFuncs.emplace_back(std::move(Funcs.front()));
230 for (
size_t Idx=1;
Idx < NumBefore; ++
Idx) {
236 const bool ranges_equal = Prev.
Range == Curr.
Range;
246 if (!(Prev == Curr)) {
249 OS <<
"warning: same address range contains "
251 <<
"info. Removing:\n"
252 << Prev <<
"\nIn favor of this one:\n"
263 OS <<
"warning: function ranges overlap:\n"
267 FinalizedFuncs.emplace_back(std::move(Curr));
276 FinalizedFuncs.emplace_back(std::move(Curr));
287 if (!Funcs.empty() && Funcs.back().Range.size() == 0 && ValidTextRanges) {
289 ValidTextRanges->getRangeThatContains(Funcs.back().Range.start())) {
290 Funcs.back().Range = {Funcs.back().Range.start(), Range->end()};
293 OS <<
"Pruned " << NumBefore - Funcs.size() <<
" functions, ended with "
294 << Funcs.size() <<
" total\n";
303 return StrTab.
add(SrcGC.StringOffsetMap.find(StrOff)->second);
312 std::lock_guard<std::mutex> Guard(
Mutex);
328 if (StringOffsetMap.count(StrOff) == 0)
329 StringOffsetMap.insert(std::make_pair(StrOff, CHStr));
334 std::lock_guard<std::mutex> Guard(
Mutex);
335 Funcs.emplace_back(std::move(FI));
340 std::lock_guard<std::mutex> Guard(
Mutex);
341 for (
auto &FI : Funcs) {
348 std::function<
bool(
const FunctionInfo &)>
const &Callback)
const {
349 std::lock_guard<std::mutex> Guard(
Mutex);
350 for (
const auto &FI : Funcs) {
357 std::lock_guard<std::mutex> Guard(
Mutex);
363 return ValidTextRanges->contains(
Addr);
367std::optional<uint64_t> GsymCreator::getFirstFunctionAddress()
const {
372 if ((Finalized || IsSegment) && !Funcs.empty())
373 return std::optional<uint64_t>(Funcs.front().startAddress());
377std::optional<uint64_t> GsymCreator::getLastFunctionAddress()
const {
382 if ((Finalized || IsSegment) && !Funcs.empty())
383 return std::optional<uint64_t>(Funcs.back().startAddress());
387std::optional<uint64_t> GsymCreator::getBaseAddress()
const {
390 return getFirstFunctionAddress();
393uint64_t GsymCreator::getMaxAddressOffset()
const {
394 switch (getAddressOffsetSize()) {
395 case 1:
return UINT8_MAX;
396 case 2:
return UINT16_MAX;
397 case 4:
return UINT32_MAX;
403uint8_t GsymCreator::getAddressOffsetSize()
const {
404 const std::optional<uint64_t> BaseAddress = getBaseAddress();
405 const std::optional<uint64_t> LastFuncAddr = getLastFunctionAddress();
406 if (BaseAddress && LastFuncAddr) {
407 const uint64_t AddrDelta = *LastFuncAddr - *BaseAddress;
408 if (AddrDelta <= UINT8_MAX)
410 else if (AddrDelta <= UINT16_MAX)
412 else if (AddrDelta <= UINT32_MAX)
419uint64_t GsymCreator::calculateHeaderAndTableSize()
const {
421 const size_t NumFuncs = Funcs.size();
423 Size += NumFuncs * getAddressOffsetSize();
438 II.
Name = copyString(SrcGC, II.
Name);
441 fixupInlineInfo(SrcGC, ChildII);
452 DstFI.
Name = copyString(SrcGC, SrcFI.
Name);
460 const size_t NumLines = DstLT.
size();
461 for (
size_t I=0;
I<NumLines; ++
I) {
463 LE.File = copyFile(SrcGC,
LE.File);
471 fixupInlineInfo(SrcGC, *DstFI.
Inline);
473 std::lock_guard<std::mutex> Guard(
Mutex);
474 Funcs.emplace_back(DstFI);
475 return Funcs.back().cacheEncoding();
481 if (SegmentSize == 0)
483 "invalid segment size zero");
486 const size_t NumFuncs = Funcs.size();
487 while (FuncIdx < NumFuncs) {
498 std::string SegmentedGsymPath;
500 std::optional<uint64_t> FirstFuncAddr =
GC->getFirstFunctionAddress();
504 Err =
GC->save(SegmentedGsymPath, ByteOrder, std::nullopt);
518 if (FuncIdx >= Funcs.size())
519 return std::unique_ptr<GsymCreator>();
521 std::unique_ptr<GsymCreator> GC(
new GsymCreator(
true));
528 GC->setBaseAddress(*BaseAddress);
531 const size_t NumFuncs = Funcs.size();
536 for (; FuncIdx < NumFuncs; ++FuncIdx) {
537 const uint64_t HeaderAndTableSize = GC->calculateHeaderAndTableSize();
538 if (HeaderAndTableSize + SegmentFuncInfosSize >= SegmentSize) {
539 if (SegmentFuncInfosSize == 0)
541 "a segment size of %" PRIu64
" is to small to "
542 "fit any function infos, specify a larger value",
547 SegmentFuncInfosSize +=
alignTo(GC->copyFunctionInfo(*
this, FuncIdx), 4);
549 return std::move(GC);
#define offsetof(TYPE, MEMBER)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::pair< llvm::MachO::Target, std::string > UUID
bool intersects(const AddressRange &R) const
bool contains(uint64_t Addr) const
A container which contains a StringRef plus a precomputed hash.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Subclass of Error for the sole purpose of identifying the success path in the type system.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
std::pair< typename Base::iterator, bool > insert(StringRef key)
Utility for building string tables with deduplicated suffixes.
void finalizeInOrder()
Finalize the string table without reording it.
bool contains(StringRef S) const
Check if a string is contained in the string table.
void write(raw_ostream &OS) const
size_t add(CachedHashStringRef S)
Add a string to the builder.
A simplified binary data writer class that doesn't require targets, target definitions,...
GsymCreator is used to emit GSYM data to a stand alone file or section within a file.
void addFunctionInfo(FunctionInfo &&FI)
Add a function info to this GSYM creator.
uint32_t insertString(StringRef S, bool Copy=true)
Insert a string into the GSYM string table.
llvm::Error finalize(llvm::raw_ostream &OS)
Finalize the data in the GSYM creator prior to saving the data out.
llvm::Expected< std::unique_ptr< GsymCreator > > createSegment(uint64_t SegmentSize, size_t &FuncIdx) const
Create a segmented GSYM creator starting with function info index FuncIdx.
llvm::Error save(StringRef Path, llvm::endianness ByteOrder, std::optional< uint64_t > SegmentSize=std::nullopt) const
Save a GSYM file to a stand alone file.
llvm::Error encode(FileWriter &O) const
Encode a GSYM into the file writer stream at the current position.
uint32_t insertFile(StringRef Path, sys::path::Style Style=sys::path::Style::native)
Insert a file into this GSYM creator.
size_t getNumFunctionInfos() const
Get the current number of FunctionInfo objects contained in this object.
bool IsValidTextAddress(uint64_t Addr) const
Check if an address is a valid code address.
void forEachFunctionInfo(std::function< bool(FunctionInfo &)> const &Callback)
Thread safe iteration over all function infos.
GsymCreator(bool Quiet=false)
LineTable class contains deserialized versions of line tables for each function's address ranges.
LineEntry & get(size_t i)
A raw_ostream that writes to a file descriptor.
A raw_ostream that discards all output.
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr uint32_t GSYM_MAGIC
constexpr uint32_t GSYM_VERSION
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
void sort(IteratorTy Start, IteratorTy End)
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Files in GSYM are contained in FileEntry structs where we split the directory and basename into two d...
uint32_t Dir
Offsets in the string table.
Function information in GSYM files encodes information for one contiguous address range.
std::optional< InlineInfo > Inline
bool hasRichInfo() const
Query if a FunctionInfo has rich debug info.
uint32_t Name
String table offset in the string table.
std::optional< LineTable > OptLineTable
Inline information stores the name of the inline function along with an array of address ranges.
std::vector< InlineInfo > Children
uint32_t CallFile
1 based file index in the file table.
uint32_t Name
String table offset in the string table.
Line entries are used to encode the line tables in FunctionInfo objects.