diff options
Diffstat (limited to 'contrib/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp')
-rw-r--r-- | contrib/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp | 185 |
1 files changed, 114 insertions, 71 deletions
diff --git a/contrib/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp b/contrib/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp index 3da1963..adbe0cb 100644 --- a/contrib/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/contrib/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -20,6 +20,7 @@ #include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/DebugInfo/PDB/PDB.h" #include "llvm/DebugInfo/PDB/PDBContext.h" +#include "llvm/Object/COFF.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/MachO.h" #include "llvm/Object/MachOUniversal.h" @@ -31,7 +32,10 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" -#include <stdlib.h> +#include <algorithm> +#include <cassert> +#include <cstdlib> +#include <cstring> #if defined(_MSC_VER) #include <Windows.h> @@ -47,12 +51,18 @@ namespace llvm { namespace symbolize { -ErrorOr<DILineInfo> LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, +Expected<DILineInfo> LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset) { - auto InfoOrErr = getOrCreateModuleInfo(ModuleName); - if (auto EC = InfoOrErr.getError()) - return EC; - SymbolizableModule *Info = InfoOrErr.get(); + SymbolizableModule *Info; + if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) + Info = InfoOrErr.get(); + else + return InfoOrErr.takeError(); + + // A null module means an error has already been reported. Return an empty + // result. + if (!Info) + return DILineInfo(); // If the user is giving us relative addresses, add the preferred base of the // object to the offset before we do the query. It's what DIContext expects. @@ -66,13 +76,19 @@ ErrorOr<DILineInfo> LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, return LineInfo; } -ErrorOr<DIInliningInfo> +Expected<DIInliningInfo> LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName, uint64_t ModuleOffset) { - auto InfoOrErr = getOrCreateModuleInfo(ModuleName); - if (auto EC = InfoOrErr.getError()) - return EC; - SymbolizableModule *Info = InfoOrErr.get(); + SymbolizableModule *Info; + if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) + Info = InfoOrErr.get(); + else + return InfoOrErr.takeError(); + + // A null module means an error has already been reported. Return an empty + // result. + if (!Info) + return DIInliningInfo(); // If the user is giving us relative addresses, add the preferred base of the // object to the offset before we do the query. It's what DIContext expects. @@ -90,12 +106,18 @@ LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName, return InlinedContext; } -ErrorOr<DIGlobal> LLVMSymbolizer::symbolizeData(const std::string &ModuleName, - uint64_t ModuleOffset) { - auto InfoOrErr = getOrCreateModuleInfo(ModuleName); - if (auto EC = InfoOrErr.getError()) - return EC; - SymbolizableModule *Info = InfoOrErr.get(); +Expected<DIGlobal> LLVMSymbolizer::symbolizeData(const std::string &ModuleName, + uint64_t ModuleOffset) { + SymbolizableModule *Info; + if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) + Info = InfoOrErr.get(); + else + return InfoOrErr.takeError(); + + // A null module means an error has already been reported. Return an empty + // result. + if (!Info) + return DIGlobal(); // If the user is giving us relative addresses, add the preferred base of // the object to the offset before we do the query. It's what DIContext @@ -116,11 +138,12 @@ void LLVMSymbolizer::flush() { Modules.clear(); } +namespace { + // For Path="/path/to/foo" and Basename="foo" assume that debug info is in // /path/to/foo.dSYM/Contents/Resources/DWARF/foo. // For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in // /path/to/bar.dSYM/Contents/Resources/DWARF/foo. -static std::string getDarwinDWARFResourceForPath( const std::string &Path, const std::string &Basename) { SmallString<16> ResourceName = StringRef(Path); @@ -132,7 +155,7 @@ std::string getDarwinDWARFResourceForPath( return ResourceName.str(); } -static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { +bool checkFileCRC(StringRef Path, uint32_t CRCHash) { ErrorOr<std::unique_ptr<MemoryBuffer>> MB = MemoryBuffer::getFileOrSTDIN(Path); if (!MB) @@ -140,9 +163,9 @@ static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer()); } -static bool findDebugBinary(const std::string &OrigPath, - const std::string &DebuglinkName, uint32_t CRCHash, - std::string &Result) { +bool findDebugBinary(const std::string &OrigPath, + const std::string &DebuglinkName, uint32_t CRCHash, + std::string &Result) { std::string OrigRealPath = OrigPath; #if defined(HAVE_REALPATH) if (char *RP = realpath(OrigPath.c_str(), nullptr)) { @@ -177,8 +200,8 @@ static bool findDebugBinary(const std::string &OrigPath, return false; } -static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, - uint32_t &CRCHash) { +bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName, + uint32_t &CRCHash) { if (!Obj) return false; for (const SectionRef &Section : Obj->sections()) { @@ -205,7 +228,6 @@ static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugNam return false; } -static bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, const MachOObjectFile *Obj) { ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid(); @@ -215,6 +237,8 @@ bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj, return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size()); } +} // end anonymous namespace + ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, const MachOObjectFile *MachExeObj, const std::string &ArchName) { // On Darwin we may find DWARF in separate object file in @@ -227,9 +251,14 @@ ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, } for (const auto &Path : DsymPaths) { auto DbgObjOrErr = getOrCreateObject(Path, ArchName); - if (!DbgObjOrErr) + if (!DbgObjOrErr) { + // Ignore errors, the file might not exist. + consumeError(DbgObjOrErr.takeError()); continue; + } ObjectFile *DbgObj = DbgObjOrErr.get(); + if (!DbgObj) + continue; const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj); if (!MachDbgObj) continue; @@ -250,23 +279,27 @@ ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path, if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) return nullptr; auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName); - if (!DbgObjOrErr) + if (!DbgObjOrErr) { + // Ignore errors, the file might not exist. + consumeError(DbgObjOrErr.takeError()); return nullptr; + } return DbgObjOrErr.get(); } -ErrorOr<LLVMSymbolizer::ObjectPair> +Expected<LLVMSymbolizer::ObjectPair> LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, const std::string &ArchName) { const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName)); - if (I != ObjectPairForPathArch.end()) + if (I != ObjectPairForPathArch.end()) { return I->second; + } auto ObjOrErr = getOrCreateObject(Path, ArchName); - if (auto EC = ObjOrErr.getError()) { - ObjectPairForPathArch.insert( - std::make_pair(std::make_pair(Path, ArchName), EC)); - return EC; + if (!ObjOrErr) { + ObjectPairForPathArch.insert(std::make_pair(std::make_pair(Path, ArchName), + ObjectPair(nullptr, nullptr))); + return ObjOrErr.takeError(); } ObjectFile *Obj = ObjOrErr.get(); @@ -285,40 +318,37 @@ LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path, return Res; } -ErrorOr<ObjectFile *> +Expected<ObjectFile *> LLVMSymbolizer::getOrCreateObject(const std::string &Path, const std::string &ArchName) { const auto &I = BinaryForPath.find(Path); Binary *Bin = nullptr; if (I == BinaryForPath.end()) { - ErrorOr<OwningBinary<Binary>> BinOrErr = createBinary(Path); - if (auto EC = BinOrErr.getError()) { - BinaryForPath.insert(std::make_pair(Path, EC)); - return EC; + Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path); + if (!BinOrErr) { + BinaryForPath.insert(std::make_pair(Path, OwningBinary<Binary>())); + return BinOrErr.takeError(); } Bin = BinOrErr->getBinary(); BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get()))); - } else if (auto EC = I->second.getError()) { - return EC; } else { - Bin = I->second->getBinary(); + Bin = I->second.getBinary(); } - assert(Bin != nullptr); + if (!Bin) + return static_cast<ObjectFile *>(nullptr); - if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) { + if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) { const auto &I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName)); if (I != ObjectForUBPathAndArch.end()) { - if (auto EC = I->second.getError()) - return EC; - return I->second->get(); + return I->second.get(); } - ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = + Expected<std::unique_ptr<ObjectFile>> ObjOrErr = UB->getObjectForArch(ArchName); - if (auto EC = ObjOrErr.getError()) { - ObjectForUBPathAndArch.insert( - std::make_pair(std::make_pair(Path, ArchName), EC)); - return EC; + if (!ObjOrErr) { + ObjectForUBPathAndArch.insert(std::make_pair( + std::make_pair(Path, ArchName), std::unique_ptr<ObjectFile>())); + return ObjOrErr.takeError(); } ObjectFile *Res = ObjOrErr->get(); ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName), @@ -328,17 +358,14 @@ LLVMSymbolizer::getOrCreateObject(const std::string &Path, if (Bin->isObject()) { return cast<ObjectFile>(Bin); } - return object_error::arch_not_found; + return errorCodeToError(object_error::arch_not_found); } -ErrorOr<SymbolizableModule *> +Expected<SymbolizableModule *> LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { const auto &I = Modules.find(ModuleName); if (I != Modules.end()) { - auto &InfoOrErr = I->second; - if (auto EC = InfoOrErr.getError()) - return EC; - return InfoOrErr->get(); + return I->second.get(); } std::string BinaryName = ModuleName; std::string ArchName = Opts.DefaultArch; @@ -352,21 +379,30 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { } } auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName); - if (auto EC = ObjectsOrErr.getError()) { + if (!ObjectsOrErr) { // Failed to find valid object file. - Modules.insert(std::make_pair(ModuleName, EC)); - return EC; + Modules.insert( + std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>())); + return ObjectsOrErr.takeError(); } ObjectPair Objects = ObjectsOrErr.get(); std::unique_ptr<DIContext> Context; + // If this is a COFF object containing PDB info, use a PDBContext to + // symbolize. Otherwise, use DWARF. if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) { - // If this is a COFF object, assume it contains PDB debug information. If - // we don't find any we will fall back to the DWARF case. - std::unique_ptr<IPDBSession> Session; - PDB_ErrorCode Error = loadDataForEXE(PDB_ReaderType::DIA, - Objects.first->getFileName(), Session); - if (Error == PDB_ErrorCode::Success) { + const debug_pdb_info *PDBInfo; + StringRef PDBFileName; + auto EC = CoffObject->getDebugPDBInfo(PDBInfo, PDBFileName); + if (!EC && PDBInfo != nullptr) { + using namespace pdb; + std::unique_ptr<IPDBSession> Session; + if (auto Err = loadDataForEXE(PDB_ReaderType::DIA, + Objects.first->getFileName(), Session)) { + Modules.insert( + std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>())); + return std::move(Err); + } Context.reset(new PDBContext(*CoffObject, std::move(Session))); } } @@ -375,21 +411,26 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { assert(Context); auto InfoOrErr = SymbolizableObjectFile::create(Objects.first, std::move(Context)); + std::unique_ptr<SymbolizableModule> SymMod; + if (InfoOrErr) + SymMod = std::move(InfoOrErr.get()); auto InsertResult = - Modules.insert(std::make_pair(ModuleName, std::move(InfoOrErr))); + Modules.insert(std::make_pair(ModuleName, std::move(SymMod))); assert(InsertResult.second); - if (auto EC = InsertResult.first->second.getError()) - return EC; - return InsertResult.first->second->get(); + if (auto EC = InfoOrErr.getError()) + return errorCodeToError(EC); + return InsertResult.first->second.get(); } +namespace { + // Undo these various manglings for Win32 extern "C" functions: // cdecl - _foo // stdcall - _foo@12 // fastcall - @foo@12 // vectorcall - foo@@12 // These are all different linkage names for 'foo'. -static StringRef demanglePE32ExternCFunc(StringRef SymbolName) { +StringRef demanglePE32ExternCFunc(StringRef SymbolName) { // Remove any '_' or '@' prefix. char Front = SymbolName.empty() ? '\0' : SymbolName[0]; if (Front == '_' || Front == '@') @@ -412,6 +453,8 @@ static StringRef demanglePE32ExternCFunc(StringRef SymbolName) { return SymbolName; } +} // end anonymous namespace + #if !defined(_MSC_VER) // Assume that __cxa_demangle is provided by libcxxabi (except for Windows). extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer, |