20 #include "llvm/Config/config.h"
45 #pragma comment(lib, "dbghelp.lib")
48 #ifdef IMAGE_FILE_MACHINE_I386
49 #undef IMAGE_FILE_MACHINE_I386
57 uint64_t ModuleOffset) {
59 if (
auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
60 Info = InfoOrErr.get();
62 return InfoOrErr.takeError();
83 uint64_t ModuleOffset) {
85 if (
auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
86 Info = InfoOrErr.get();
88 return InfoOrErr.takeError();
108 return InlinedContext;
112 uint64_t ModuleOffset) {
114 if (
auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
115 Info = InfoOrErr.get();
117 return InfoOrErr.takeError();
137 ObjectForUBPathAndArch.clear();
138 BinaryForPath.clear();
139 ObjectPairForPathArch.clear();
149 std::string getDarwinDWARFResourceForPath(
150 const std::string &Path,
const std::string &Basename) {
153 ResourceName +=
".dSYM";
157 return ResourceName.
str();
160 bool checkFileCRC(StringRef Path,
uint32_t CRCHash) {
161 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
168 bool findDebugBinary(
const std::string &OrigPath,
169 const std::string &DebuglinkName,
uint32_t CRCHash,
170 std::string &Result) {
171 std::string OrigRealPath = OrigPath;
172 #if defined(HAVE_REALPATH)
173 if (
char *
RP = realpath(OrigPath.c_str(),
nullptr)) {
178 SmallString<16> OrigDir(OrigRealPath);
180 SmallString<16> DebugPath = OrigDir;
183 if (checkFileCRC(DebugPath, CRCHash)) {
184 Result = DebugPath.str();
188 DebugPath = OrigRealPath;
190 if (checkFileCRC(DebugPath, CRCHash)) {
191 Result = DebugPath.str();
195 DebugPath =
"/usr/lib/debug";
198 if (checkFileCRC(DebugPath, CRCHash)) {
199 Result = DebugPath.str();
205 bool getGNUDebuglinkContents(
const ObjectFile *Obj, std::string &DebugName,
209 for (
const SectionRef &
Section : Obj->sections()) {
212 Name = Name.substr(Name.find_first_not_of(
"._"));
213 if (Name ==
"gnu_debuglink") {
216 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
218 if (
const char *DebugNameStr = DE.getCStr(&Offset)) {
220 Offset = (Offset + 3) & ~0x3;
221 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
222 DebugName = DebugNameStr;
223 CRCHash = DE.getU32(&Offset);
233 bool darwinDsymMatchesBinary(
const MachOObjectFile *DbgObj,
234 const MachOObjectFile *Obj) {
235 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
236 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
237 if (dbg_uuid.empty() || bin_uuid.empty())
239 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
244 ObjectFile *LLVMSymbolizer::lookUpDsymFile(
const std::string &ExePath,
245 const MachOObjectFile *MachExeObj,
const std::string &ArchName) {
248 std::vector<std::string> DsymPaths;
250 DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
251 for (
const auto &Path : Opts.
DsymHints) {
252 DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
254 for (
const auto &Path : DsymPaths) {
255 auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
264 const MachOObjectFile *MachDbgObj =
dyn_cast<
const MachOObjectFile>(DbgObj);
267 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
273 ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(
const std::string &Path,
274 const ObjectFile *Obj,
275 const std::string &ArchName) {
276 std::string DebuglinkName;
278 std::string DebugBinaryPath;
279 if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
281 if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath))
283 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
289 return DbgObjOrErr.get();
292 Expected<LLVMSymbolizer::ObjectPair>
293 LLVMSymbolizer::getOrCreateObjectPair(
const std::string &Path,
294 const std::string &ArchName) {
295 const auto &
I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
296 if (
I != ObjectPairForPathArch.end()) {
300 auto ObjOrErr = getOrCreateObject(Path, ArchName);
302 ObjectPairForPathArch.insert(std::make_pair(std::make_pair(Path, ArchName),
303 ObjectPair(
nullptr,
nullptr)));
304 return ObjOrErr.takeError();
311 if (
auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
312 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
314 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
317 ObjectPair Res = std::make_pair(Obj, DbgObj);
318 ObjectPairForPathArch.insert(
319 std::make_pair(std::make_pair(Path, ArchName), Res));
323 Expected<ObjectFile *>
324 LLVMSymbolizer::getOrCreateObject(
const std::string &Path,
325 const std::string &ArchName) {
326 const auto &
I = BinaryForPath.find(Path);
327 Binary *Bin =
nullptr;
328 if (
I == BinaryForPath.end()) {
329 Expected<OwningBinary<Binary>> BinOrErr =
createBinary(Path);
331 BinaryForPath.insert(std::make_pair(Path, OwningBinary<Binary>()));
332 return BinOrErr.takeError();
334 Bin = BinOrErr->getBinary();
335 BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get())));
337 Bin =
I->second.getBinary();
343 if (MachOUniversalBinary *UB = dyn_cast_or_null<MachOUniversalBinary>(Bin)) {
344 const auto &
I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
345 if (
I != ObjectForUBPathAndArch.end()) {
346 return I->second.get();
348 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
349 UB->getObjectForArch(ArchName);
351 ObjectForUBPathAndArch.insert(std::make_pair(
352 std::make_pair(Path, ArchName), std::unique_ptr<ObjectFile>()));
353 return ObjOrErr.takeError();
356 ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName),
357 std::move(ObjOrErr.get())));
360 if (Bin->isObject()) {
361 return cast<ObjectFile>(Bin);
366 Expected<SymbolizableModule *>
367 LLVMSymbolizer::getOrCreateModuleInfo(
const std::string &ModuleName) {
368 const auto &
I = Modules.find(ModuleName);
369 if (
I != Modules.end()) {
370 return I->second.get();
372 std::string BinaryName = ModuleName;
374 size_t ColonPos = ModuleName.find_last_of(
':');
376 if (ColonPos != std::string::npos) {
377 std::string ArchStr = ModuleName.substr(ColonPos + 1);
379 BinaryName = ModuleName.substr(0, ColonPos);
383 auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
387 std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>()));
388 return ObjectsOrErr.takeError();
390 ObjectPair Objects = ObjectsOrErr.get();
392 std::unique_ptr<DIContext>
Context;
395 if (
auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
396 const codeview::DebugInfo *DebugInfo;
397 StringRef PDBFileName;
398 auto EC = CoffObject->getDebugPDBInfo(DebugInfo, PDBFileName);
399 if (!EC && DebugInfo !=
nullptr && !PDBFileName.empty()) {
401 std::unique_ptr<IPDBSession> Session;
403 Objects.first->getFileName(), Session)) {
405 std::make_pair(ModuleName, std::unique_ptr<SymbolizableModule>()));
406 return std::move(Err);
408 Context.reset(
new PDBContext(*CoffObject, std::move(Session)));
412 Context.reset(
new DWARFContextInMemory(*Objects.second));
416 std::unique_ptr<SymbolizableModule> SymMod;
418 SymMod = std::move(InfoOrErr.get());
420 Modules.insert(std::make_pair(ModuleName, std::move(SymMod)));
421 assert(InsertResult.second);
422 if (
auto EC = InfoOrErr.getError())
424 return InsertResult.first->second.get();
435 StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
437 char Front = SymbolName.empty() ?
'\0' : SymbolName[0];
438 if (Front ==
'_' || Front ==
'@')
439 SymbolName = SymbolName.drop_front();
443 size_t AtPos = SymbolName.rfind(
'@');
445 std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(),
446 [](
char C) {
return C >=
'0' &&
C <=
'9'; })) {
447 SymbolName = SymbolName.substr(0, AtPos);
452 if (SymbolName.endswith(
"@"))
453 SymbolName = SymbolName.drop_back();
460 #if !defined(_MSC_VER)
462 extern "C" char *
__cxa_demangle(
const char *mangled_name,
char *output_buffer,
463 size_t *length,
int *
status);
469 #if !defined(_MSC_VER)
472 if (Name.substr(0, 2) ==
"_Z") {
477 std::string Result = DemangledName;
482 if (!Name.empty() && Name.front() ==
'?') {
484 char DemangledName[1024] = {0};
485 DWORD result = ::UnDecorateSymbolName(
486 Name.c_str(), DemangledName, 1023,
487 UNDNAME_NO_ACCESS_SPECIFIERS |
488 UNDNAME_NO_ALLOCATION_LANGUAGE |
489 UNDNAME_NO_THROW_SIGNATURES |
490 UNDNAME_NO_MEMBER_TYPE |
491 UNDNAME_NO_MS_KEYWORDS |
492 UNDNAME_NO_FUNCTION_RETURNS);
493 return (result == 0) ? Name : std::string(DemangledName);
496 if (DbiModuleDescriptor && DbiModuleDescriptor->
isWin32Module())
497 return std::string(demanglePE32ExternCFunc(Name));
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
DILineInfo * getMutableFrame(unsigned Index)
virtual DIGlobal symbolizeData(uint64_t ModuleOffset) const =0
virtual DILineInfo symbolizeCode(uint64_t ModuleOffset, FunctionNameKind FNKind, bool UseSymbolTable) const =0
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
virtual bool isWin32Module() const =0
DILineInfo - a format-neutral container for source line information.
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Expected< DILineInfo > symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset)
Tagged union holding either a T or a Error.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
uint32_t getNumberOfFrames() const
Error loadDataForEXE(PDB_ReaderType Type, StringRef Path, std::unique_ptr< IPDBSession > &Session)
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
virtual DIInliningInfo symbolizeInlinedCode(uint64_t ModuleOffset, FunctionNameKind FNKind, bool UseSymbolTable) const =0
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
DIInliningInfo - a format-neutral container for inlined code description.
std::vector< std::string > DsymHints
FunctionNameKind PrintFunctions
void consumeError(Error Err)
Consume a Error without doing anything.
static std::string DemangleName(const std::string &Name, const SymbolizableModule *DbiModuleDescriptor)
char * __cxa_demangle(const char *mangled_name, char *output_buffer, size_t *length, int *status)
uint32_t crc32(StringRef Buffer)
StringRef str() const
Explicit conversion to StringRef.
virtual uint64_t getModulePreferredBase() const =0
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Expected< DIInliningInfo > symbolizeInlinedCode(const std::string &ModuleName, uint64_t ModuleOffset)
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
StringRef - Represent a constant reference to a string, i.e.
static ErrorOr< std::unique_ptr< SymbolizableObjectFile > > create(object::ObjectFile *Obj, std::unique_ptr< DIContext > DICtx)
Expected< DIGlobal > symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset)
DIGlobal - container for description of a global variable.
StringRef extension(StringRef path, Style style=Style::native)
Get extension.