42 std::lock_guard<std::mutex> Guard(
Mutex);
43 const auto NextIndex = Files.size();
45 auto R = FileEntryToIndex.
insert(std::make_pair(FE, NextIndex));
47 Files.emplace_back(FE);
48 return R.first->second;
57 const FileEntry SrcFE = SrcGC.Files[FileIdx];
62 : StrTab.
add(SrcGC.StringOffsetMap.find(SrcFE.
Dir)->second);
65 return insertFileEntry(DstFE);
69 std::optional<uint64_t> SegmentSize)
const {
71 return saveSegments(Path, ByteOrder, *SegmentSize);
81 std::lock_guard<std::mutex> Guard(
Mutex);
84 "no functions to encode");
87 "GsymCreator wasn't finalized prior to encoding");
89 if (Funcs.size() > UINT32_MAX)
91 "too many FunctionInfos");
93 std::optional<uint64_t> BaseAddress = getBaseAddress();
97 "invalid base address");
107 memset(Hdr.
UUID, 0,
sizeof(Hdr.
UUID));
119 const uint64_t MaxAddressOffset = getMaxAddressOffset();
122 for (
const auto &FuncInfo : Funcs) {
128 assert(AddrOffset <= MaxAddressOffset);
129 (void)MaxAddressOffset;
132 O.writeU8(
static_cast<uint8_t
>(AddrOffset));
135 O.writeU16(
static_cast<uint16_t>(AddrOffset));
138 O.writeU32(
static_cast<uint32_t>(AddrOffset));
141 O.writeU64(AddrOffset);
148 const off_t AddrInfoOffsetsOffset = O.tell();
149 for (
size_t i = 0, n = Funcs.size(); i < n; ++i)
155 assert(Files[0].Dir == 0);
157 size_t NumFiles = Files.size();
158 if (NumFiles > UINT32_MAX)
160 O.writeU32(
static_cast<uint32_t>(NumFiles));
161 for (
auto File : Files) {
162 O.writeU32(File.Dir);
163 O.writeU32(File.Base);
167 const off_t StrtabOffset = O.tell();
168 StrTab.
write(O.get_stream());
169 const off_t StrtabSize = O.tell() - StrtabOffset;
170 std::vector<uint32_t> AddrInfoOffsets;
173 for (
const auto &FuncInfo : Funcs) {
175 AddrInfoOffsets.push_back(OffsetOrErr.get());
177 return OffsetOrErr.takeError();
185 for (
auto AddrInfoOffset : AddrInfoOffsets) {
186 O.fixup32(AddrInfoOffset, AddrInfoOffsetsOffset +
Offset);
194 if (Funcs.size() < 2)
199 std::vector<FunctionInfo> TopLevelFuncs;
202 TopLevelFuncs.emplace_back(std::move(Funcs.front()));
207 for (
size_t Idx = 1;
Idx < Funcs.size(); ++
Idx) {
220 std::move(MatchFunc));
223 TopLevelFuncs.emplace_back(std::move(MatchFunc));
226 uint32_t mergedCount = Funcs.size() - TopLevelFuncs.size();
228 if (mergedCount != 0)
229 Out <<
"Have " << mergedCount
230 <<
" merged functions as children of other functions\n";
236 std::lock_guard<std::mutex> Guard(
Mutex);
265 const auto NumBefore = Funcs.size();
274 std::vector<FunctionInfo> FinalizedFuncs;
275 FinalizedFuncs.reserve(Funcs.size());
276 FinalizedFuncs.emplace_back(std::move(Funcs.front()));
277 for (
size_t Idx=1;
Idx < NumBefore; ++
Idx) {
283 const bool ranges_equal = Prev.
Range == Curr.
Range;
293 if (!(Prev == Curr)) {
296 "Duplicate address ranges with different debug info.",
298 OS <<
"warning: same address range contains "
300 <<
"info. Removing:\n"
301 << Prev <<
"\nIn favor of this one:\n"
313 OS <<
"warning: function ranges overlap:\n"
317 FinalizedFuncs.emplace_back(std::move(Curr));
326 FinalizedFuncs.emplace_back(std::move(Curr));
337 if (!Funcs.empty() && Funcs.back().Range.size() == 0 && ValidTextRanges) {
339 ValidTextRanges->getRangeThatContains(Funcs.back().Range.start())) {
340 Funcs.back().Range = {Funcs.back().Range.start(),
Range->end()};
343 Out <<
"Pruned " << NumBefore - Funcs.size() <<
" functions, ended with "
344 << Funcs.size() <<
" total\n";
353 return StrTab.
add(SrcGC.StringOffsetMap.find(StrOff)->second);
362 std::lock_guard<std::mutex> Guard(
Mutex);
378 if (StringOffsetMap.count(StrOff) == 0)
379 StringOffsetMap.insert(std::make_pair(StrOff, CHStr));
384 std::lock_guard<std::mutex> Guard(
Mutex);
385 Funcs.emplace_back(std::move(FI));
390 std::lock_guard<std::mutex> Guard(
Mutex);
391 for (
auto &FI : Funcs) {
398 std::function<
bool(
const FunctionInfo &)>
const &Callback)
const {
399 std::lock_guard<std::mutex> Guard(
Mutex);
400 for (
const auto &FI : Funcs) {
407 std::lock_guard<std::mutex> Guard(
Mutex);
413 return ValidTextRanges->contains(
Addr);
417std::optional<uint64_t> GsymCreator::getFirstFunctionAddress()
const {
422 if ((Finalized || IsSegment) && !Funcs.empty())
423 return std::optional<uint64_t>(Funcs.front().startAddress());
427std::optional<uint64_t> GsymCreator::getLastFunctionAddress()
const {
432 if ((Finalized || IsSegment) && !Funcs.empty())
433 return std::optional<uint64_t>(Funcs.back().startAddress());
437std::optional<uint64_t> GsymCreator::getBaseAddress()
const {
440 return getFirstFunctionAddress();
443uint64_t GsymCreator::getMaxAddressOffset()
const {
444 switch (getAddressOffsetSize()) {
445 case 1:
return UINT8_MAX;
446 case 2:
return UINT16_MAX;
447 case 4:
return UINT32_MAX;
453uint8_t GsymCreator::getAddressOffsetSize()
const {
454 const std::optional<uint64_t> BaseAddress = getBaseAddress();
455 const std::optional<uint64_t> LastFuncAddr = getLastFunctionAddress();
456 if (BaseAddress && LastFuncAddr) {
457 const uint64_t AddrDelta = *LastFuncAddr - *BaseAddress;
458 if (AddrDelta <= UINT8_MAX)
460 else if (AddrDelta <= UINT16_MAX)
462 else if (AddrDelta <= UINT32_MAX)
469uint64_t GsymCreator::calculateHeaderAndTableSize()
const {
471 const size_t NumFuncs = Funcs.size();
473 Size += NumFuncs * getAddressOffsetSize();
488 II.Name = copyString(SrcGC,
II.Name);
489 II.CallFile = copyFile(SrcGC,
II.CallFile);
490 for (
auto &ChildII:
II.Children)
491 fixupInlineInfo(SrcGC, ChildII);
502 DstFI.
Name = copyString(SrcGC, SrcFI.
Name);
510 const size_t NumLines = DstLT.
size();
511 for (
size_t I=0;
I<NumLines; ++
I) {
513 LE.File = copyFile(SrcGC,
LE.File);
521 fixupInlineInfo(SrcGC, *DstFI.
Inline);
523 std::lock_guard<std::mutex> Guard(
Mutex);
524 Funcs.emplace_back(DstFI);
525 return Funcs.back().cacheEncoding();
531 if (SegmentSize == 0)
533 "invalid segment size zero");
536 const size_t NumFuncs = Funcs.size();
537 while (FuncIdx < NumFuncs) {
549 std::string SegmentedGsymPath;
551 std::optional<uint64_t> FirstFuncAddr =
GC->getFirstFunctionAddress();
555 Err =
GC->save(SegmentedGsymPath, ByteOrder, std::nullopt);
569 if (FuncIdx >= Funcs.size())
570 return std::unique_ptr<GsymCreator>();
572 std::unique_ptr<GsymCreator> GC(
new GsymCreator(
true));
579 GC->setBaseAddress(*BaseAddress);
582 const size_t NumFuncs = Funcs.size();
587 for (; FuncIdx < NumFuncs; ++FuncIdx) {
588 const uint64_t HeaderAndTableSize = GC->calculateHeaderAndTableSize();
589 if (HeaderAndTableSize + SegmentFuncInfosSize >= SegmentSize) {
590 if (SegmentFuncInfosSize == 0)
592 "a segment size of %" PRIu64
" is to small to "
593 "fit any function infos, specify a larger value",
598 SegmentFuncInfosSize +=
alignTo(GC->copyFunctionInfo(*
this, FuncIdx), 4);
600 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
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
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::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.
void prepareMergedFunctions(OutputAggregator &Out)
Organize merged FunctionInfo's.
llvm::Error encode(FileWriter &O) const
Encode a GSYM into the file writer stream at the current position.
llvm::Error finalize(OutputAggregator &OS)
Finalize the data in the GSYM creator prior to saving the data out.
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)
void Report(StringRef s, std::function< void(raw_ostream &o)> detailCallback)
A raw_ostream that writes to a file descriptor.
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
std::optional< MergedFunctionsInfo > MergedFunctions
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.
Line entries are used to encode the line tables in FunctionInfo objects.