53LLVMSymbolizer::symbolizeCodeCommon(
const T &ModuleSpecifier,
56 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
58 return InfoOrErr.takeError();
70 ModuleOffset.
Address +=
Info->getModulePreferredBase();
83 return symbolizeCodeCommon(Obj, ModuleOffset);
89 return symbolizeCodeCommon(
ModuleName, ModuleOffset);
95 return symbolizeCodeCommon(
BuildID, ModuleOffset);
101 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
103 return InfoOrErr.takeError();
115 ModuleOffset.
Address +=
Info->getModulePreferredBase();
121 for (
int i = 0, n =
InlinedContext.getNumberOfFrames(); i < n; i++) {
129Expected<DIInliningInfo>
132 return symbolizeInlinedCodeCommon(Obj, ModuleOffset);
138 return symbolizeInlinedCodeCommon(
ModuleName, ModuleOffset);
144 return symbolizeInlinedCodeCommon(
BuildID, ModuleOffset);
149LLVMSymbolizer::symbolizeDataCommon(
const T &ModuleSpecifier,
152 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
154 return InfoOrErr.takeError();
166 ModuleOffset.
Address +=
Info->getModulePreferredBase();
177 return symbolizeDataCommon(Obj, ModuleOffset);
183 return symbolizeDataCommon(
ModuleName, ModuleOffset);
189 return symbolizeDataCommon(
BuildID, ModuleOffset);
194LLVMSymbolizer::symbolizeFrameCommon(
const T &ModuleSpecifier,
196 auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier);
198 return InfoOrErr.takeError();
204 return std::vector<DILocal>();
210 ModuleOffset.
Address +=
Info->getModulePreferredBase();
212 return Info->symbolizeFrame(ModuleOffset);
218 return symbolizeFrameCommon(Obj, ModuleOffset);
224 return symbolizeFrameCommon(
ModuleName, ModuleOffset);
230 return symbolizeFrameCommon(
BuildID, ModuleOffset);
234 ObjectForUBPathAndArch.clear();
237 BinaryForPath.clear();
238 ObjectPairForPathArch.clear();
240 BuildIDPaths.
clear();
249std::string getDarwinDWARFResourceForPath(
const std::string &Path,
250 const std::string &Basename) {
253 ResourceName +=
".dSYM";
257 return std::string(ResourceName.
str());
260bool checkFileCRC(StringRef Path,
uint32_t CRCHash) {
261 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
265 return CRCHash ==
llvm::crc32(arrayRefFromStringRef(MB.get()->getBuffer()));
268bool getGNUDebuglinkContents(
const ObjectFile *Obj, std::string &DebugName,
272 for (
const SectionRef &Section : Obj->sections()) {
277 if (
Name ==
"gnu_debuglink") {
278 Expected<StringRef> ContentsOrErr =
Section.getContents();
279 if (!ContentsOrErr) {
283 DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
285 if (
const char *DebugNameStr = DE.getCStr(&
Offset)) {
288 if (DE.isValidOffsetForDataOfSize(
Offset, 4)) {
289 DebugName = DebugNameStr;
290 CRCHash = DE.getU32(&
Offset);
300bool darwinDsymMatchesBinary(
const MachOObjectFile *DbgObj,
301 const MachOObjectFile *Obj) {
302 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
303 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
304 if (dbg_uuid.empty() || bin_uuid.empty())
306 return !
memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
311ObjectFile *LLVMSymbolizer::lookUpDsymFile(
const std::string &ExePath,
312 const MachOObjectFile *MachExeObj,
313 const std::string &ArchName) {
316 std::vector<std::string> DsymPaths;
319 getDarwinDWARFResourceForPath(ExePath, std::string(Filename)));
320 for (
const auto &Path : Opts.
DsymHints) {
322 getDarwinDWARFResourceForPath(Path, std::string(Filename)));
324 for (
const auto &Path : DsymPaths) {
325 auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
334 const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
337 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
343ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(
const std::string &Path,
344 const ObjectFile *Obj,
345 const std::string &ArchName) {
346 std::string DebuglinkName;
348 std::string DebugBinaryPath;
349 if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
351 if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath))
353 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
359 return DbgObjOrErr.get();
362ObjectFile *LLVMSymbolizer::lookUpBuildIDObject(
const std::string &Path,
363 const ELFObjectFileBase *Obj,
364 const std::string &ArchName) {
370 std::string DebugBinaryPath;
371 if (!getOrFindDebugBinary(*
BuildID, DebugBinaryPath))
373 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
378 return DbgObjOrErr.get();
381bool LLVMSymbolizer::findDebugBinary(
const std::string &OrigPath,
382 const std::string &DebuglinkName,
383 uint32_t CRCHash, std::string &Result) {
384 SmallString<16> OrigDir(OrigPath);
386 SmallString<16> DebugPath = OrigDir;
389 if (checkFileCRC(DebugPath, CRCHash)) {
390 Result = std::string(DebugPath.str());
396 if (checkFileCRC(DebugPath, CRCHash)) {
397 Result = std::string(DebugPath.str());
408#if defined(__NetBSD__)
410 DebugPath =
"/usr/libdata/debug";
413 DebugPath =
"/usr/lib/debug";
418 if (checkFileCRC(DebugPath, CRCHash)) {
419 Result = std::string(DebugPath.str());
431 std::string &Result) {
433 auto I = BuildIDPaths.
find(BuildIDStr);
434 if (
I != BuildIDPaths.
end()) {
440 if (std::optional<std::string> Path = BIDFetcher->fetch(
BuildID)) {
442 auto InsertResult = BuildIDPaths.
insert({BuildIDStr, Result});
443 assert(InsertResult.second);
451Expected<LLVMSymbolizer::ObjectPair>
452LLVMSymbolizer::getOrCreateObjectPair(
const std::string &Path,
453 const std::string &ArchName) {
454 auto I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
455 if (
I != ObjectPairForPathArch.end()) {
456 recordAccess(BinaryForPath.find(Path)->second);
460 auto ObjOrErr = getOrCreateObject(Path, ArchName);
462 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName),
463 ObjectPair(
nullptr,
nullptr));
464 return ObjOrErr.takeError();
471 if (
auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
472 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
473 else if (
auto ELFObj = dyn_cast<const ELFObjectFileBase>(Obj))
474 DbgObj = lookUpBuildIDObject(Path, ELFObj, ArchName);
476 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
479 ObjectPair Res = std::make_pair(Obj, DbgObj);
480 std::string DbgObjPath = DbgObj->getFileName().str();
482 ObjectPairForPathArch.emplace(std::make_pair(Path, ArchName), Res);
483 BinaryForPath.find(DbgObjPath)->second.pushEvictor([
this,
I = Pair.first]() {
484 ObjectPairForPathArch.erase(I);
489Expected<ObjectFile *>
490LLVMSymbolizer::getOrCreateObject(
const std::string &Path,
491 const std::string &ArchName) {
493 auto Pair = BinaryForPath.emplace(Path, OwningBinary<Binary>());
495 Bin = Pair.first->second->getBinary();
496 recordAccess(Pair.first->second);
498 Expected<OwningBinary<Binary>> BinOrErr =
createBinary(Path);
500 return BinOrErr.takeError();
502 CachedBinary &CachedBin = Pair.first->second;
503 CachedBin = std::move(BinOrErr.get());
504 CachedBin.pushEvictor([
this,
I = Pair.first]() { BinaryForPath.erase(I); });
505 LRUBinaries.push_back(CachedBin);
506 CacheSize += CachedBin.size();
507 Bin = CachedBin->getBinary();
513 if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(
Bin)) {
514 auto I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
515 if (
I != ObjectForUBPathAndArch.end())
516 return I->second.get();
518 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
519 UB->getMachOObjectForArch(ArchName);
521 ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
522 std::unique_ptr<ObjectFile>());
523 return ObjOrErr.takeError();
526 auto Pair = ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
527 std::move(ObjOrErr.get()));
528 BinaryForPath.find(Path)->second.pushEvictor(
529 [
this, Iter = Pair.first]() { ObjectForUBPathAndArch.erase(Iter); });
532 if (
Bin->isObject()) {
533 return cast<ObjectFile>(
Bin);
538Expected<SymbolizableModule *>
539LLVMSymbolizer::createModuleInfo(
const ObjectFile *Obj,
540 std::unique_ptr<DIContext> Context,
544 std::unique_ptr<SymbolizableModule> SymMod;
546 SymMod = std::move(*InfoOrErr);
547 auto InsertResult = Modules.insert(
548 std::make_pair(std::string(
ModuleName), std::move(SymMod)));
549 assert(InsertResult.second);
551 return InfoOrErr.takeError();
552 return InsertResult.first->second.get();
555Expected<SymbolizableModule *>
556LLVMSymbolizer::getOrCreateModuleInfo(
const std::string &
ModuleName) {
559 size_t ColonPos =
ModuleName.find_last_of(
':');
561 if (ColonPos != std::string::npos) {
562 std::string ArchStr =
ModuleName.substr(ColonPos + 1);
570 if (
I != Modules.end()) {
571 recordAccess(BinaryForPath.find(BinaryName)->second);
572 return I->second.get();
575 auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
578 Modules.emplace(
ModuleName, std::unique_ptr<SymbolizableModule>());
579 return ObjectsOrErr.takeError();
581 ObjectPair Objects = ObjectsOrErr.get();
583 std::unique_ptr<DIContext>
Context;
586 if (
auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
587 const codeview::DebugInfo *DebugInfo;
588 StringRef PDBFileName;
589 auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName);
590 if (!EC && DebugInfo !=
nullptr && !PDBFileName.empty()) {
592 std::unique_ptr<IPDBSession> Session;
595 Opts.
UseDIA ? PDB_ReaderType::DIA : PDB_ReaderType::Native;
596 if (
auto Err =
loadDataForEXE(ReaderType, Objects.first->getFileName(),
598 Modules.emplace(
ModuleName, std::unique_ptr<SymbolizableModule>());
602 Context.reset(
new PDBContext(*CoffObject, std::move(Session)));
610 createModuleInfo(Objects.first, std::move(Context),
ModuleName);
613 BinaryForPath.find(BinaryName)->second.pushEvictor([
this,
I]() {
620Expected<SymbolizableModule *>
621LLVMSymbolizer::getOrCreateModuleInfo(
const ObjectFile &Obj) {
622 StringRef ObjName = Obj.getFileName();
623 auto I = Modules.find(ObjName);
624 if (
I != Modules.end())
625 return I->second.get();
629 return createModuleInfo(&Obj, std::move(Context), ObjName);
632Expected<SymbolizableModule *>
633LLVMSymbolizer::getOrCreateModuleInfo(ArrayRef<uint8_t>
BuildID) {
635 if (!getOrFindDebugBinary(
BuildID, Path)) {
637 Twine(
"could not find build ID '") +
640 return getOrCreateModuleInfo(Path);
651StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
655 bool HasAtNumSuffix =
false;
661 HasAtNumSuffix =
true;
666 bool IsVectorCall =
false;
667 if (HasAtNumSuffix &&
SymbolName.endswith(
"@")) {
673 if (!IsVectorCall && (Front ==
'_' || Front ==
'@'))
688 if (!
Name.empty() &&
Name.front() ==
'?') {
692 Name.c_str(),
nullptr,
nullptr,
nullptr, &status,
697 Result = DemangledName;
702 if (DbiModuleDescriptor && DbiModuleDescriptor->
isWin32Module()) {
703 std::string DemangledCName(demanglePE32ExternCFunc(
Name));
708 return DemangledCName;
714 if (
Bin->getBinary())
715 LRUBinaries.splice(LRUBinaries.end(), LRUBinaries,
Bin.getIterator());
722 while (CacheSize > Opts.
MaxCacheSize && !LRUBinaries.empty() &&
723 std::next(LRUBinaries.begin()) != LRUBinaries.end()) {
725 CacheSize -=
Bin.size();
726 LRUBinaries.pop_front();
733 this->Evictor = [OldEvictor = std::move(this->Evictor),
734 NewEvictor = std::move(NewEvictor)]() {
739 this->Evictor = std::move(NewEvictor);
This file declares a library for handling Build IDs and using them to find debug info.
Analysis containing CSE Info
Merge contiguous icmps into a memcmp
static bool isDigit(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
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)
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...
StringRef str() const
Explicit conversion to StringRef.
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.
static constexpr size_t npos
BuildIDFetcher searches local cache directories for debug info.
This class is the base class for all object file types.
void pushEvictor(std::function< void()> Evictor)
Expected< DIInliningInfo > symbolizeInlinedCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Expected< DILineInfo > symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
static std::string DemangleName(const std::string &Name, const SymbolizableModule *DbiModuleDescriptor)
Expected< DIGlobal > symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Expected< std::vector< DILocal > > symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
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.
std::optional< BuildIDRef > getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Error loadDataForEXE(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
PDB_ReaderType
Specifies which PDB reader implementation is to be used.
static std::string toHex(uint64_t V)
static StringRef getBuildIDStr(ArrayRef< uint8_t > BuildID)
void make_absolute(const Twine ¤t_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
StringRef relative_path(StringRef path, 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.
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.
char * microsoftDemangle(const char *mangled_name, size_t *n_read, char *buf, size_t *n_buf, int *status, MSDemangleFlags Flags=MSDF_None)
Demangles the Microsoft symbol pointed at by mangled_name and returns it.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
@ no_such_file_or_directory
bool nonMicrosoftDemangle(const char *MangledName, std::string &Result)
@ Global
Append to llvm.global_dtors.
uint32_t crc32(ArrayRef< uint8_t > Data)
@ MSDF_NoCallingConvention
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.
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.
std::vector< std::string > DsymHints
std::string FallbackDebugPath
FunctionNameKind PrintFunctions
FileLineInfoKind PathStyle