#include "llvm/DebugInfo/GSYM/GsymCreator.h"
#include "llvm/DebugInfo/GSYM/FileWriter.h"
#include "llvm/DebugInfo/GSYM/Header.h"
#include "llvm/DebugInfo/GSYM/LineTable.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>
using namespace llvm;
using namespace gsym;
GsymCreator::GsymCreator(bool Quiet)
: StrTab(StringTableBuilder::ELF), Quiet(Quiet) {
insertFile(StringRef());
}
uint32_t GsymCreator::insertFile(StringRef Path, llvm::sys::path::Style Style) {
llvm::StringRef directory = llvm::sys::path::parent_path(Path, Style);
llvm::StringRef filename = llvm::sys::path::filename(Path, Style);
const uint32_t Dir = insertString(directory);
const uint32_t Base = insertString(filename);
FileEntry FE(Dir, Base);
std::lock_guard<std::mutex> Guard(Mutex);
const auto NextIndex = Files.size();
auto R = FileEntryToIndex.insert(std::make_pair(FE, NextIndex));
if (R.second)
Files.emplace_back(FE);
return R.first->second;
}
llvm::Error GsymCreator::save(StringRef Path,
llvm::support::endianness ByteOrder) const {
std::error_code EC;
raw_fd_ostream OutStrm(Path, EC);
if (EC)
return llvm::errorCodeToError(EC);
FileWriter O(OutStrm, ByteOrder);
return encode(O);
}
llvm::Error GsymCreator::encode(FileWriter &O) const {
std::lock_guard<std::mutex> Guard(Mutex);
if (Funcs.empty())
return createStringError(std::errc::invalid_argument,
"no functions to encode");
if (!Finalized)
return createStringError(std::errc::invalid_argument,
"GsymCreator wasn't finalized prior to encoding");
if (Funcs.size() > UINT32_MAX)
return createStringError(std::errc::invalid_argument,
"too many FunctionInfos");
const uint64_t MinAddr =
BaseAddress ? *BaseAddress : Funcs.front().startAddress();
const uint64_t MaxAddr = Funcs.back().startAddress();
const uint64_t AddrDelta = MaxAddr - MinAddr;
Header Hdr;
Hdr.Magic = GSYM_MAGIC;
Hdr.Version = GSYM_VERSION;
Hdr.AddrOffSize = 0;
Hdr.UUIDSize = static_cast<uint8_t>(UUID.size());
Hdr.BaseAddress = MinAddr;
Hdr.NumAddresses = static_cast<uint32_t>(Funcs.size());
Hdr.StrtabOffset = 0; Hdr.StrtabSize = 0; memset(Hdr.UUID, 0, sizeof(Hdr.UUID));
if (UUID.size() > sizeof(Hdr.UUID))
return createStringError(std::errc::invalid_argument,
"invalid UUID size %u", (uint32_t)UUID.size());
if (AddrDelta <= UINT8_MAX)
Hdr.AddrOffSize = 1;
else if (AddrDelta <= UINT16_MAX)
Hdr.AddrOffSize = 2;
else if (AddrDelta <= UINT32_MAX)
Hdr.AddrOffSize = 4;
else
Hdr.AddrOffSize = 8;
if (UUID.size() > 0)
memcpy(Hdr.UUID, UUID.data(), UUID.size());
llvm::Error Err = Hdr.encode(O);
if (Err)
return Err;
O.alignTo(Hdr.AddrOffSize);
for (const auto &FuncInfo : Funcs) {
uint64_t AddrOffset = FuncInfo.startAddress() - Hdr.BaseAddress;
switch (Hdr.AddrOffSize) {
case 1:
O.writeU8(static_cast<uint8_t>(AddrOffset));
break;
case 2:
O.writeU16(static_cast<uint16_t>(AddrOffset));
break;
case 4:
O.writeU32(static_cast<uint32_t>(AddrOffset));
break;
case 8:
O.writeU64(AddrOffset);
break;
}
}
O.alignTo(4);
const off_t AddrInfoOffsetsOffset = O.tell();
for (size_t i = 0, n = Funcs.size(); i < n; ++i)
O.writeU32(0);
O.alignTo(4);
assert(!Files.empty());
assert(Files[0].Dir == 0);
assert(Files[0].Base == 0);
size_t NumFiles = Files.size();
if (NumFiles > UINT32_MAX)
return createStringError(std::errc::invalid_argument, "too many files");
O.writeU32(static_cast<uint32_t>(NumFiles));
for (auto File : Files) {
O.writeU32(File.Dir);
O.writeU32(File.Base);
}
const off_t StrtabOffset = O.tell();
StrTab.write(O.get_stream());
const off_t StrtabSize = O.tell() - StrtabOffset;
std::vector<uint32_t> AddrInfoOffsets;
for (const auto &FuncInfo : Funcs) {
if (Expected<uint64_t> OffsetOrErr = FuncInfo.encode(O))
AddrInfoOffsets.push_back(OffsetOrErr.get());
else
return OffsetOrErr.takeError();
}
O.fixup32((uint32_t)StrtabOffset, offsetof(Header, StrtabOffset));
O.fixup32((uint32_t)StrtabSize, offsetof(Header, StrtabSize));
uint64_t Offset = 0;
for (auto AddrInfoOffset : AddrInfoOffsets) {
O.fixup32(AddrInfoOffset, AddrInfoOffsetsOffset + Offset);
Offset += 4;
}
return ErrorSuccess();
}
template <class ForwardIt, class BinaryPredicate>
static ForwardIt removeIfBinary(ForwardIt FirstIt, ForwardIt LastIt,
BinaryPredicate Pred) {
if (FirstIt != LastIt) {
auto PrevIt = FirstIt++;
FirstIt = std::find_if(FirstIt, LastIt, [&](const auto &Curr) {
return Pred(*PrevIt++, Curr);
});
if (FirstIt != LastIt)
for (ForwardIt CurrIt = FirstIt; ++CurrIt != LastIt;)
if (!Pred(*PrevIt, *CurrIt)) {
PrevIt = FirstIt;
*FirstIt++ = std::move(*CurrIt);
}
}
return FirstIt;
}
llvm::Error GsymCreator::finalize(llvm::raw_ostream &OS) {
std::lock_guard<std::mutex> Guard(Mutex);
if (Finalized)
return createStringError(std::errc::invalid_argument, "already finalized");
Finalized = true;
llvm::sort(Funcs);
StrTab.finalizeInOrder();
auto NumBefore = Funcs.size();
Funcs.erase(
removeIfBinary(Funcs.begin(), Funcs.end(),
[&](const auto &Prev, const auto &Curr) {
const bool ranges_equal = Prev.Range == Curr.Range;
if (ranges_equal || Prev.Range.intersects(Curr.Range)) {
if (ranges_equal) {
if (Prev == Curr) {
return true;
} else {
if (!Prev.hasRichInfo() && Curr.hasRichInfo()) {
return true;
} else {
if (!Quiet) {
OS << "warning: same address range contains "
"different debug "
<< "info. Removing:\n"
<< Prev << "\nIn favor of this one:\n"
<< Curr << "\n";
}
return true;
}
}
} else {
if (!Quiet) { OS << "warning: function ranges overlap:\n"
<< Prev << "\n"
<< Curr << "\n";
}
}
} else if (Prev.Range.size() == 0 &&
Curr.Range.contains(Prev.Range.start())) {
if (!Quiet) {
OS << "warning: removing symbol:\n"
<< Prev << "\nKeeping:\n"
<< Curr << "\n";
}
return true;
}
return false;
}),
Funcs.end());
if (!Funcs.empty() && Funcs.back().Range.size() == 0 && ValidTextRanges) {
if (auto Range =
ValidTextRanges->getRangeThatContains(Funcs.back().Range.start())) {
Funcs.back().Range = {Funcs.back().Range.start(), Range->end()};
}
}
OS << "Pruned " << NumBefore - Funcs.size() << " functions, ended with "
<< Funcs.size() << " total\n";
return Error::success();
}
uint32_t GsymCreator::insertString(StringRef S, bool Copy) {
if (S.empty())
return 0;
CachedHashStringRef CHStr(S);
std::lock_guard<std::mutex> Guard(Mutex);
if (Copy) {
if (!StrTab.contains(CHStr))
CHStr = CachedHashStringRef{StringStorage.insert(S).first->getKey(),
CHStr.hash()};
}
return StrTab.add(CHStr);
}
void GsymCreator::addFunctionInfo(FunctionInfo &&FI) {
std::lock_guard<std::mutex> Guard(Mutex);
Ranges.insert(FI.Range);
Funcs.emplace_back(std::move(FI));
}
void GsymCreator::forEachFunctionInfo(
std::function<bool(FunctionInfo &)> const &Callback) {
std::lock_guard<std::mutex> Guard(Mutex);
for (auto &FI : Funcs) {
if (!Callback(FI))
break;
}
}
void GsymCreator::forEachFunctionInfo(
std::function<bool(const FunctionInfo &)> const &Callback) const {
std::lock_guard<std::mutex> Guard(Mutex);
for (const auto &FI : Funcs) {
if (!Callback(FI))
break;
}
}
size_t GsymCreator::getNumFunctionInfos() const {
std::lock_guard<std::mutex> Guard(Mutex);
return Funcs.size();
}
bool GsymCreator::IsValidTextAddress(uint64_t Addr) const {
if (ValidTextRanges)
return ValidTextRanges->contains(Addr);
return true; }
bool GsymCreator::hasFunctionInfoForAddress(uint64_t Addr) const {
std::lock_guard<std::mutex> Guard(Mutex);
return Ranges.contains(Addr);
}