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);
200 if (Funcs.size() < 2)
205 std::vector<FunctionInfo> TopLevelFuncs;
208 TopLevelFuncs.emplace_back(std::move(Funcs.front()));
213 for (
size_t Idx = 1;
Idx < Funcs.size(); ++
Idx) {
226 std::move(MatchFunc));
229 TopLevelFuncs.emplace_back(std::move(MatchFunc));
232 uint32_t mergedCount = Funcs.size() - TopLevelFuncs.size();
234 if (mergedCount != 0)
235 Out <<
"Have " << mergedCount
236 <<
" merged functions as children of other functions\n";
242 std::lock_guard<std::mutex> Guard(
Mutex);
271 const auto NumBefore = Funcs.size();
280 std::vector<FunctionInfo> FinalizedFuncs;
281 FinalizedFuncs.reserve(Funcs.size());
282 FinalizedFuncs.emplace_back(std::move(Funcs.front()));
283 for (
size_t Idx=1;
Idx < NumBefore; ++
Idx) {
289 const bool ranges_equal = Prev.
Range == Curr.
Range;
299 if (!(Prev == Curr)) {
302 "Duplicate address ranges with different debug info.",
304 OS <<
"warning: same address range contains "
306 <<
"info. Removing:\n"
307 << Prev <<
"\nIn favor of this one:\n"
319 OS <<
"warning: function ranges overlap:\n"
323 FinalizedFuncs.emplace_back(std::move(Curr));
332 FinalizedFuncs.emplace_back(std::move(Curr));
343 if (!Funcs.empty() && Funcs.back().Range.size() == 0 && ValidTextRanges) {
345 ValidTextRanges->getRangeThatContains(Funcs.back().Range.start())) {
346 Funcs.back().Range = {Funcs.back().Range.start(),
Range->end()};
349 Out <<
"Pruned " << NumBefore - Funcs.size() <<
" functions, ended with "
350 << Funcs.size() <<
" total\n";
359 return StrTab.
add(SrcGC.StringOffsetMap.find(StrOff)->second);
368 std::lock_guard<std::mutex> Guard(
Mutex);
384 StringOffsetMap.try_emplace(StrOff, CHStr);
389 auto I = StringOffsetMap.find(
Offset);
390 assert(
I != StringOffsetMap.end() &&
391 "GsymCreator::getString expects a valid offset as parameter.");
392 return I->second.val();
396 std::lock_guard<std::mutex> Guard(
Mutex);
397 Funcs.emplace_back(std::move(FI));
402 std::lock_guard<std::mutex> Guard(
Mutex);
403 for (
auto &FI : Funcs) {
410 std::function<
bool(
const FunctionInfo &)>
const &Callback)
const {
411 std::lock_guard<std::mutex> Guard(
Mutex);
412 for (
const auto &FI : Funcs) {
419 std::lock_guard<std::mutex> Guard(
Mutex);
425 return ValidTextRanges->contains(
Addr);
429std::optional<uint64_t> GsymCreator::getFirstFunctionAddress()
const {
434 if ((Finalized || IsSegment) && !Funcs.empty())
435 return std::optional<uint64_t>(Funcs.front().startAddress());
439std::optional<uint64_t> GsymCreator::getLastFunctionAddress()
const {
444 if ((Finalized || IsSegment) && !Funcs.empty())
445 return std::optional<uint64_t>(Funcs.back().startAddress());
449std::optional<uint64_t> GsymCreator::getBaseAddress()
const {
452 return getFirstFunctionAddress();
455uint64_t GsymCreator::getMaxAddressOffset()
const {
456 switch (getAddressOffsetSize()) {
457 case 1:
return UINT8_MAX;
458 case 2:
return UINT16_MAX;
459 case 4:
return UINT32_MAX;
465uint8_t GsymCreator::getAddressOffsetSize()
const {
466 const std::optional<uint64_t> BaseAddress = getBaseAddress();
467 const std::optional<uint64_t> LastFuncAddr = getLastFunctionAddress();
468 if (BaseAddress && LastFuncAddr) {
469 const uint64_t AddrDelta = *LastFuncAddr - *BaseAddress;
470 if (AddrDelta <= UINT8_MAX)
472 else if (AddrDelta <= UINT16_MAX)
474 else if (AddrDelta <= UINT32_MAX)
481uint64_t GsymCreator::calculateHeaderAndTableSize()
const {
483 const size_t NumFuncs = Funcs.size();
485 Size += NumFuncs * getAddressOffsetSize();
500 II.Name = copyString(SrcGC,
II.Name);
501 II.CallFile = copyFile(SrcGC,
II.CallFile);
502 for (
auto &ChildII:
II.Children)
503 fixupInlineInfo(SrcGC, ChildII);
514 DstFI.
Name = copyString(SrcGC, SrcFI.
Name);
522 const size_t NumLines = DstLT.
size();
523 for (
size_t I=0;
I<NumLines; ++
I) {
525 LE.File = copyFile(SrcGC,
LE.File);
533 fixupInlineInfo(SrcGC, *DstFI.
Inline);
535 std::lock_guard<std::mutex> Guard(
Mutex);
536 Funcs.emplace_back(DstFI);
537 return Funcs.back().cacheEncoding();
543 if (SegmentSize == 0)
545 "invalid segment size zero");
548 const size_t NumFuncs = Funcs.size();
549 while (FuncIdx < NumFuncs) {
561 std::string SegmentedGsymPath;
563 std::optional<uint64_t> FirstFuncAddr =
GC->getFirstFunctionAddress();
567 Err =
GC->save(SegmentedGsymPath, ByteOrder, std::nullopt);
581 if (FuncIdx >= Funcs.size())
582 return std::unique_ptr<GsymCreator>();
584 std::unique_ptr<GsymCreator> GC(
new GsymCreator(
true));
591 GC->setBaseAddress(*BaseAddress);
594 const size_t NumFuncs = Funcs.size();
599 for (; FuncIdx < NumFuncs; ++FuncIdx) {
600 const uint64_t HeaderAndTableSize = GC->calculateHeaderAndTableSize();
601 if (HeaderAndTableSize + SegmentFuncInfosSize >= SegmentSize) {
602 if (SegmentFuncInfosSize == 0)
604 "a segment size of %" PRIu64
" is to small to "
605 "fit any function infos, specify a larger value",
610 SegmentFuncInfosSize +=
alignTo(GC->copyFunctionInfo(*
this, FuncIdx), 4);
612 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.
llvm::Error loadYAML(StringRef YAMLFile)
This method reads the specified YAML file, parses its content, and updates the Funcs vector with call...
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.
StringRef getString(uint32_t Offset)
Retrieve a string from the GSYM string table given its offset.
llvm::Error loadCallSitesFromYAML(StringRef YAMLFile)
Load call site information from a YAML file.
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.