55LLVMSymbolizer::symbolizeCodeCommon(
const T &ModuleSpecifier,
60 return InfoOrErr.takeError();
72 ModuleOffset.
Address +=
Info->getModulePreferredBase();
87 return symbolizeCodeCommon(Obj, ModuleOffset);
93 return symbolizeCodeCommon(
ModuleName, ModuleOffset);
99 return symbolizeCodeCommon(
BuildID, ModuleOffset);
107 return InfoOrErr.takeError();
119 ModuleOffset.
Address +=
Info->getModulePreferredBase();
127 for (
int i = 0, n =
InlinedContext.getNumberOfFrames(); i < n; i++) {
135Expected<DIInliningInfo>
138 return symbolizeInlinedCodeCommon(Obj, ModuleOffset);
144 return symbolizeInlinedCodeCommon(
ModuleName, ModuleOffset);
150 return symbolizeInlinedCodeCommon(
BuildID, ModuleOffset);
155LLVMSymbolizer::symbolizeDataCommon(
const T &ModuleSpecifier,
160 return InfoOrErr.takeError();
172 ModuleOffset.
Address +=
Info->getModulePreferredBase();
183 return symbolizeDataCommon(Obj, ModuleOffset);
189 return symbolizeDataCommon(
ModuleName, ModuleOffset);
195 return symbolizeDataCommon(
BuildID, ModuleOffset);
200LLVMSymbolizer::symbolizeFrameCommon(
const T &ModuleSpecifier,
204 return InfoOrErr.takeError();
210 return std::vector<DILocal>();
216 ModuleOffset.
Address +=
Info->getModulePreferredBase();
218 return Info->symbolizeFrame(ModuleOffset);
224 return symbolizeFrameCommon(Obj, ModuleOffset);
230 return symbolizeFrameCommon(
ModuleName, ModuleOffset);
236 return symbolizeFrameCommon(
BuildID, ModuleOffset);
241LLVMSymbolizer::findSymbolCommon(
const T &ModuleSpecifier,
StringRef Symbol,
245 return InfoOrErr.takeError();
248 std::vector<DILineInfo> Result;
262 Result.push_back(std::move(LineInfo));
269Expected<std::vector<DILineInfo>>
272 return findSymbolCommon(Obj, Symbol,
Offset);
288 ObjectForUBPathAndArch.clear();
291 BinaryForPath.clear();
292 ObjectPairForPathArch.clear();
294 BuildIDPaths.
clear();
303std::string getDarwinDWARFResourceForPath(
const std::string &Path,
304 const std::string &Basename) {
307 ResourceName +=
".dSYM";
311 return std::string(ResourceName);
314bool checkFileCRC(StringRef Path,
uint32_t CRCHash) {
315 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
319 return CRCHash ==
llvm::crc32(arrayRefFromStringRef(MB.get()->getBuffer()));
322bool getGNUDebuglinkContents(
const ObjectFile *Obj, std::string &DebugName,
326 for (
const SectionRef &Section : Obj->sections()) {
331 if (
Name ==
"gnu_debuglink") {
332 Expected<StringRef> ContentsOrErr =
Section.getContents();
333 if (!ContentsOrErr) {
337 DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
339 if (
const char *DebugNameStr = DE.getCStr(&
Offset)) {
342 if (DE.isValidOffsetForDataOfSize(
Offset, 4)) {
343 DebugName = DebugNameStr;
344 CRCHash = DE.getU32(&
Offset);
354bool darwinDsymMatchesBinary(
const MachOObjectFile *DbgObj,
355 const MachOObjectFile *Obj) {
356 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
357 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
358 if (dbg_uuid.empty() || bin_uuid.empty())
360 return !
memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
365ObjectFile *LLVMSymbolizer::lookUpDsymFile(
const std::string &ExePath,
366 const MachOObjectFile *MachExeObj,
367 const std::string &ArchName) {
370 std::vector<std::string> DsymPaths;
373 getDarwinDWARFResourceForPath(ExePath, std::string(Filename)));
374 for (
const auto &Path : Opts.
DsymHints) {
376 getDarwinDWARFResourceForPath(Path, std::string(Filename)));
378 for (
const auto &Path : DsymPaths) {
379 auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
388 const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
391 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
397ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(
const std::string &Path,
398 const ObjectFile *Obj,
399 const std::string &ArchName) {
400 std::string DebuglinkName;
402 std::string DebugBinaryPath;
403 if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
405 if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath))
407 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
413 return DbgObjOrErr.get();
416ObjectFile *LLVMSymbolizer::lookUpBuildIDObject(
const std::string &Path,
417 const ELFObjectFileBase *Obj,
418 const std::string &ArchName) {
422 std::string DebugBinaryPath;
423 if (!getOrFindDebugBinary(
BuildID, DebugBinaryPath))
425 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
430 return DbgObjOrErr.get();
433bool LLVMSymbolizer::findDebugBinary(
const std::string &OrigPath,
434 const std::string &DebuglinkName,
435 uint32_t CRCHash, std::string &Result) {
436 SmallString<16> OrigDir(OrigPath);
438 SmallString<16> DebugPath = OrigDir;
441 if (checkFileCRC(DebugPath, CRCHash)) {
442 Result = std::string(DebugPath);
448 if (checkFileCRC(DebugPath, CRCHash)) {
449 Result = std::string(DebugPath);
460#if defined(__NetBSD__)
462 DebugPath =
"/usr/libdata/debug";
465 DebugPath =
"/usr/lib/debug";
470 if (checkFileCRC(DebugPath, CRCHash)) {
471 Result = std::string(DebugPath);
483 std::string &Result) {
485 auto I = BuildIDPaths.
find(BuildIDStr);
486 if (
I != BuildIDPaths.
end()) {
492 if (std::optional<std::string> Path = BIDFetcher->fetch(
BuildID)) {
494 auto InsertResult = BuildIDPaths.
insert({BuildIDStr, Result});
495 assert(InsertResult.second);
503std::string LLVMSymbolizer::lookUpGsymFile(
const std::string &Path) {
508 sys::fs::file_status
Status;
514 if (
const auto GsymPath = Path +
".gsym"; CheckGsymFile(GsymPath))
524 if (CheckGsymFile(GsymPath))
525 return static_cast<std::string
>(GsymPath);
531Expected<LLVMSymbolizer::ObjectPair>
532LLVMSymbolizer::getOrCreateObjectPair(
const std::string &Path,
533 const std::string &ArchName) {
534 auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
535 if (
I != ObjectPairForPathArch.end()) {
536 recordAccess(BinaryForPath.find(Path)->second);
540 auto ObjOrErr = getOrCreateObject(Path, ArchName);
542 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName),
543 ObjectPair(
nullptr,
nullptr));
544 return ObjOrErr.takeError();
551 if (
auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
552 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
553 else if (
auto ELFObj = dyn_cast<const ELFObjectFileBase>(Obj))
554 DbgObj = lookUpBuildIDObject(Path, ELFObj, ArchName);
556 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
559 ObjectPair Res = std::make_pair(Obj, DbgObj);
560 std::string DbgObjPath = DbgObj->getFileName().str();
562 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res);
563 BinaryForPath.find(DbgObjPath)->second.pushEvictor([
this,
I = Pair.first]() {
564 ObjectPairForPathArch.erase(I);
569Expected<ObjectFile *>
570LLVMSymbolizer::getOrCreateObject(
const std::string &Path,
571 const std::string &ArchName) {
573 auto Pair = BinaryForPath.emplace(Path, OwningBinary<Binary>());
575 Bin = Pair.first->second->getBinary();
576 recordAccess(Pair.first->second);
578 Expected<OwningBinary<Binary>> BinOrErr =
createBinary(Path);
580 return BinOrErr.takeError();
582 CachedBinary &CachedBin = Pair.first->second;
583 CachedBin = std::move(BinOrErr.get());
584 CachedBin.pushEvictor([
this,
I = Pair.first]() { BinaryForPath.erase(I); });
585 LRUBinaries.push_back(CachedBin);
586 CacheSize += CachedBin.size();
587 Bin = CachedBin->getBinary();
593 if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(
Bin)) {
594 auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
595 if (
I != ObjectForUBPathAndArch.end())
596 return I->second.get();
598 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
599 UB->getMachOObjectForArch(ArchName);
601 ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
602 std::unique_ptr<ObjectFile>());
603 return ObjOrErr.takeError();
606 auto Pair = ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
607 std::move(ObjOrErr.get()));
608 BinaryForPath.find(Path)->second.pushEvictor(
609 [
this, Iter = Pair.first]() { ObjectForUBPathAndArch.erase(Iter); });
612 if (
Bin->isObject()) {
613 return cast<ObjectFile>(
Bin);
618Expected<SymbolizableModule *>
619LLVMSymbolizer::createModuleInfo(
const ObjectFile *Obj,
620 std::unique_ptr<DIContext> Context,
624 std::unique_ptr<SymbolizableModule> SymMod;
626 SymMod = std::move(*InfoOrErr);
627 auto InsertResult = Modules.insert(
628 std::make_pair(std::string(
ModuleName), std::move(SymMod)));
629 assert(InsertResult.second);
631 return InfoOrErr.takeError();
632 return InsertResult.first->second.get();
635Expected<SymbolizableModule *>
639 size_t ColonPos =
ModuleName.find_last_of(
':');
641 if (ColonPos != std::string::npos) {
650 if (
I != Modules.end()) {
651 recordAccess(BinaryForPath.find(BinaryName)->second);
652 return I->second.get();
656 getOrCreateObjectPair(std::string{BinaryName}, std::string{ArchName});
659 Modules.emplace(
ModuleName, std::unique_ptr<SymbolizableModule>());
660 return ObjectsOrErr.takeError();
662 ObjectPair Objects = ObjectsOrErr.get();
664 std::unique_ptr<DIContext>
Context;
672 const auto GsymFile = lookUpGsymFile(BinaryName.
str());
673 if (!GsymFile.empty()) {
677 std::unique_ptr<gsym::GsymReader> Reader =
678 std::make_unique<gsym::GsymReader>(std::move(*ReaderOrErr));
680 Context = std::make_unique<gsym::GsymContext>(std::move(Reader));
684 if (
auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
687 auto EC = CoffObject->getDebugPDBInfo(
DebugInfo, PDBFileName);
690 Objects.first->sections(), [](
SectionRef Section) ->
bool {
691 if (Expected<StringRef> SectionName = Section.getName())
692 return SectionName.get() ==
".debug_info";
695 if (!EC && !HasDwarf &&
DebugInfo !=
nullptr && !PDBFileName.
empty()) {
697 std::unique_ptr<IPDBSession> Session;
699 PDB_ReaderType ReaderType =
700 Opts.
UseDIA ? PDB_ReaderType::DIA : PDB_ReaderType::Native;
701 if (
auto Err = loadDataForEXE(ReaderType, Objects.first->getFileName(),
703 Modules.emplace(
ModuleName, std::unique_ptr<SymbolizableModule>());
707 Context.reset(
new PDBContext(*CoffObject, std::move(Session)));
719 BinaryForPath.find(BinaryName)->second.pushEvictor([
this,
I]() {
736 auto I = Modules.find(ObjName);
737 if (
I != Modules.end())
738 return I->second.get();
740 std::unique_ptr<DIContext>
Context;
746 return createModuleInfo(&Obj, std::move(Context), ObjName);
749Expected<SymbolizableModule *>
752 if (!getOrFindDebugBinary(
BuildID, Path)) {
754 "could not find build ID");
767StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
771 bool HasAtNumSuffix =
false;
777 HasAtNumSuffix =
true;
782 bool IsVectorCall =
false;
783 if (HasAtNumSuffix &&
SymbolName.ends_with(
"@")) {
789 if (!IsVectorCall && (Front ==
'_' || Front ==
'@'))
804 if (
Name.starts_with(
'?')) {
808 Name,
nullptr, &status,
812 return std::string{
Name};
813 Result = DemangledName;
818 if (DbiModuleDescriptor && DbiModuleDescriptor->
isWin32Module()) {
819 std::string DemangledCName(demanglePE32ExternCFunc(
Name));
824 return DemangledCName;
826 return std::string{
Name};
830 if (
Bin->getBinary())
831 LRUBinaries.splice(LRUBinaries.end(), LRUBinaries,
Bin.getIterator());
838 while (CacheSize > Opts.
MaxCacheSize && !LRUBinaries.empty() &&
839 std::next(LRUBinaries.begin()) != LRUBinaries.end()) {
841 CacheSize -=
Bin.size();
842 LRUBinaries.pop_front();
849 this->Evictor = [OldEvictor = std::move(this->Evictor),
850 NewEvictor = std::move(NewEvictor)]() {
855 this->Evictor = std::move(NewEvictor);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a library for handling Build IDs and using them to find debug info.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Merge contiguous icmps into a memcmp
static bool isDigit(const char C)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
static std::unique_ptr< BTFContext > create(const object::ObjectFile &Obj, std::function< void(Error)> ErrorHandler=WithColor::defaultErrorHandler)
static LLVM_ABI bool hasBTFSections(const ObjectFile &Obj)
A format-neutral container for inlined code description.
static std::unique_ptr< DWARFContext > create(const object::ObjectFile &Obj, ProcessDebugRelocations RelocAction=ProcessDebugRelocations::Process, const LoadedObjectInfo *L=nullptr, std::string DWPName="", std::function< void(Error)> RecoverableErrorHandler=WithColor::defaultErrorHandler, std::function< void(Error)> WarningHandler=WithColor::defaultWarningHandler, bool ThreadSafe=false)
Tagged union holding either a T or a Error.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
iterator find(StringRef Key)
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
static constexpr size_t npos
Triple - Helper class for working with autoconf configuration names.
bool isBPF() const
Tests whether the target is eBPF.
static LLVM_ABI llvm::Expected< GsymReader > openFile(StringRef Path)
Construct a GsymReader from a file on disk.
StringRef getFileName() const
BuildIDFetcher searches local cache directories for debug info.
This class is the base class for all object file types.
Triple makeTriple() const
Create a triple from the data in this object file.
virtual bool hasDebugInfo() const
This is a value type class that represents a single section in the list of sections in the object fil...
LLVM_ABI void pushEvictor(std::function< void()> Evictor)
static LLVM_ABI std::string DemangleName(StringRef Name, const SymbolizableModule *DbiModuleDescriptor)
LLVM_ABI void pruneCache()
LLVM_ABI Expected< std::vector< DILineInfo > > findSymbol(const ObjectFile &Obj, StringRef Symbol, uint64_t Offset)
LLVM_ABI Expected< DIInliningInfo > symbolizeInlinedCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI LLVMSymbolizer()
LLVM_ABI Expected< DILineInfo > symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI Expected< DIGlobal > symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI Expected< std::vector< DILocal > > symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI Expected< SymbolizableModule * > getOrCreateModuleInfo(StringRef ModuleName)
Returns a SymbolizableModule or an error if loading debug info failed.
LLVM_ABI ~LLVMSymbolizer()
virtual bool isWin32Module() const =0
static Expected< std::unique_ptr< SymbolizableObjectFile > > create(const object::ObjectFile *Obj, std::unique_ptr< DIContext > DICtx, bool UntagAddresses)
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
LLVM_ABI BuildIDRef getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
LLVM_ABI Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
static bool useBTFContext(const ObjectFile &Obj)
static StringRef getBuildIDStr(ArrayRef< uint8_t > BuildID)
LLVM_ABI void make_absolute(const Twine ¤t_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI bool is_directory(const basic_file_status &status)
Does status represent a directory?
LLVM_ABI void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
LLVM_ABI StringRef extension(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get extension.
LLVM_ABI StringRef relative_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get relative path.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
DEMANGLE_ABI bool nonMicrosoftDemangle(std::string_view MangledName, std::string &Result, bool CanHaveLeadingDot=true, bool ParseParams=true)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
@ no_such_file_or_directory
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
@ Global
Append to llvm.global_dtors.
LLVM_ABI uint32_t crc32(ArrayRef< uint8_t > Data)
DEMANGLE_ABI char * microsoftDemangle(std::string_view mangled_name, size_t *n_read, int *status, MSDemangleFlags Flags=MSDF_None)
Demangles the Microsoft symbol pointed at by mangled_name and returns it.
@ MSDF_NoCallingConvention
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
void consumeError(Error Err)
Consume a Error without doing anything.
Implement std::hash so that hash_code can be used in STL containers.
Container for description of a global variable.
Controls which fields of DILineInfo container should be filled with data.
A format-neutral container for source line information.
static constexpr const char *const BadString
std::vector< std::string > GsymFileDirectory
std::vector< std::string > DsymHints
std::string FallbackDebugPath
FunctionNameKind PrintFunctions
FileLineInfoKind PathStyle