2013-01-22 22:21:19 +08:00
|
|
|
//===-- LLVMSymbolize.cpp -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation for LLVM symbolization library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-27 01:56:12 +08:00
|
|
|
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
|
|
|
|
|
2015-10-30 06:21:37 +08:00
|
|
|
#include "SymbolizableObjectFile.h"
|
|
|
|
|
2013-03-19 23:33:18 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-08-27 02:12:03 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2015-04-24 01:37:47 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
2015-04-28 01:19:51 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/PDB.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/PDBContext.h"
|
2014-02-26 21:10:01 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2013-01-22 22:21:19 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
2015-12-19 06:02:14 +08:00
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2015-08-11 05:47:11 +08:00
|
|
|
#include "llvm/Support/COFF.h"
|
2013-01-22 22:21:19 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2013-08-15 01:09:30 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2014-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2013-06-04 15:57:38 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-08-15 01:09:30 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-01-22 22:21:19 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-03-29 01:40:08 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2013-01-22 22:21:19 +08:00
|
|
|
|
2015-05-07 06:26:30 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <DbgHelp.h>
|
2015-07-02 22:34:57 +08:00
|
|
|
#pragma comment(lib, "dbghelp.lib")
|
2015-08-11 05:47:11 +08:00
|
|
|
|
|
|
|
// Windows.h conflicts with our COFF header definitions.
|
|
|
|
#ifdef IMAGE_FILE_MACHINE_I386
|
|
|
|
#undef IMAGE_FILE_MACHINE_I386
|
|
|
|
#endif
|
2015-05-07 06:26:30 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-22 22:21:19 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace symbolize {
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
ErrorOr<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();
|
2015-10-09 08:15:01 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (Opts.RelativeAddresses)
|
|
|
|
ModuleOffset += Info->getModulePreferredBase();
|
|
|
|
|
2015-10-27 06:34:56 +08:00
|
|
|
DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts.PrintFunctions,
|
|
|
|
Opts.UseSymbolTable);
|
2015-11-04 05:36:13 +08:00
|
|
|
if (Opts.Demangle)
|
|
|
|
LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info);
|
2015-11-04 06:20:52 +08:00
|
|
|
return LineInfo;
|
2013-01-22 22:21:19 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
ErrorOr<DIInliningInfo>
|
2015-11-04 06:20:52 +08:00
|
|
|
LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName,
|
|
|
|
uint64_t ModuleOffset) {
|
2015-11-04 08:30:24 +08:00
|
|
|
auto InfoOrErr = getOrCreateModuleInfo(ModuleName);
|
|
|
|
if (auto EC = InfoOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
SymbolizableModule *Info = InfoOrErr.get();
|
2015-10-30 08:40:20 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (Opts.RelativeAddresses)
|
|
|
|
ModuleOffset += Info->getModulePreferredBase();
|
|
|
|
|
|
|
|
DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
|
|
|
|
ModuleOffset, Opts.PrintFunctions, Opts.UseSymbolTable);
|
2015-11-04 05:36:13 +08:00
|
|
|
if (Opts.Demangle) {
|
|
|
|
for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
|
|
|
|
auto *Frame = InlinedContext.getMutableFrame(i);
|
|
|
|
Frame->FunctionName = DemangleName(Frame->FunctionName, Info);
|
|
|
|
}
|
|
|
|
}
|
2015-11-04 06:20:52 +08:00
|
|
|
return InlinedContext;
|
2015-10-30 08:40:20 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
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();
|
2015-11-04 05:36:13 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (Opts.RelativeAddresses)
|
|
|
|
ModuleOffset += Info->getModulePreferredBase();
|
|
|
|
|
|
|
|
DIGlobal Global = Info->symbolizeData(ModuleOffset);
|
|
|
|
if (Opts.Demangle)
|
|
|
|
Global.Name = DemangleName(Global.Name, Info);
|
2015-11-04 06:20:52 +08:00
|
|
|
return Global;
|
2013-01-22 22:21:19 +08:00
|
|
|
}
|
|
|
|
|
2013-03-19 18:24:42 +08:00
|
|
|
void LLVMSymbolizer::flush() {
|
2015-12-19 06:02:14 +08:00
|
|
|
ObjectForUBPathAndArch.clear();
|
|
|
|
BinaryForPath.clear();
|
2014-10-17 08:50:19 +08:00
|
|
|
ObjectPairForPathArch.clear();
|
2015-12-19 06:02:14 +08:00
|
|
|
Modules.clear();
|
2013-03-19 18:24:42 +08:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
namespace {
|
|
|
|
|
2014-10-17 08:50:19 +08:00
|
|
|
// 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.
|
|
|
|
std::string getDarwinDWARFResourceForPath(
|
|
|
|
const std::string &Path, const std::string &Basename) {
|
|
|
|
SmallString<16> ResourceName = StringRef(Path);
|
|
|
|
if (sys::path::extension(Path) != ".dSYM") {
|
|
|
|
ResourceName += ".dSYM";
|
|
|
|
}
|
2013-01-22 22:21:19 +08:00
|
|
|
sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
|
|
|
|
sys::path::append(ResourceName, Basename);
|
|
|
|
return ResourceName.str();
|
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Path);
|
|
|
|
if (!MB)
|
2013-08-15 01:09:30 +08:00
|
|
|
return false;
|
2014-07-07 01:43:13 +08:00
|
|
|
return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
|
2013-08-15 01:09:30 +08:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
bool findDebugBinary(const std::string &OrigPath,
|
|
|
|
const std::string &DebuglinkName, uint32_t CRCHash,
|
|
|
|
std::string &Result) {
|
2013-08-27 02:12:03 +08:00
|
|
|
std::string OrigRealPath = OrigPath;
|
|
|
|
#if defined(HAVE_REALPATH)
|
2014-04-25 12:24:47 +08:00
|
|
|
if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
|
2013-08-27 02:12:03 +08:00
|
|
|
OrigRealPath = RP;
|
|
|
|
free(RP);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SmallString<16> OrigDir(OrigRealPath);
|
2013-08-15 01:09:30 +08:00
|
|
|
llvm::sys::path::remove_filename(OrigDir);
|
|
|
|
SmallString<16> DebugPath = OrigDir;
|
|
|
|
// Try /path/to/original_binary/debuglink_name
|
|
|
|
llvm::sys::path::append(DebugPath, DebuglinkName);
|
|
|
|
if (checkFileCRC(DebugPath, CRCHash)) {
|
|
|
|
Result = DebugPath.str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Try /path/to/original_binary/.debug/debuglink_name
|
2013-08-27 02:12:03 +08:00
|
|
|
DebugPath = OrigRealPath;
|
2013-08-15 01:09:30 +08:00
|
|
|
llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
|
|
|
|
if (checkFileCRC(DebugPath, CRCHash)) {
|
|
|
|
Result = DebugPath.str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Try /usr/lib/debug/path/to/original_binary/debuglink_name
|
|
|
|
DebugPath = "/usr/lib/debug";
|
|
|
|
llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
|
|
|
|
DebuglinkName);
|
|
|
|
if (checkFileCRC(DebugPath, CRCHash)) {
|
|
|
|
Result = DebugPath.str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
|
|
|
|
uint32_t &CRCHash) {
|
2013-08-15 01:09:30 +08:00
|
|
|
if (!Obj)
|
|
|
|
return false;
|
2014-03-13 22:37:36 +08:00
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
2013-08-15 01:09:30 +08:00
|
|
|
StringRef Name;
|
2014-03-13 22:37:36 +08:00
|
|
|
Section.getName(Name);
|
2013-08-15 01:09:30 +08:00
|
|
|
Name = Name.substr(Name.find_first_not_of("._"));
|
|
|
|
if (Name == "gnu_debuglink") {
|
|
|
|
StringRef Data;
|
2014-03-13 22:37:36 +08:00
|
|
|
Section.getContents(Data);
|
2013-08-15 01:09:30 +08:00
|
|
|
DataExtractor DE(Data, Obj->isLittleEndian(), 0);
|
|
|
|
uint32_t Offset = 0;
|
|
|
|
if (const char *DebugNameStr = DE.getCStr(&Offset)) {
|
|
|
|
// 4-byte align the offset.
|
|
|
|
Offset = (Offset + 3) & ~0x3;
|
|
|
|
if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
|
|
|
|
DebugName = DebugNameStr;
|
|
|
|
CRCHash = DE.getU32(&Offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-17 08:50:19 +08:00
|
|
|
bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
|
|
|
|
const MachOObjectFile *Obj) {
|
|
|
|
ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
|
|
|
|
ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
|
|
|
|
if (dbg_uuid.empty() || bin_uuid.empty())
|
|
|
|
return false;
|
|
|
|
return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
|
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2014-10-17 08:50:19 +08:00
|
|
|
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
|
|
|
|
// resource directory.
|
|
|
|
std::vector<std::string> DsymPaths;
|
|
|
|
StringRef Filename = sys::path::filename(ExePath);
|
|
|
|
DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
|
|
|
|
for (const auto &Path : Opts.DsymHints) {
|
|
|
|
DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
|
|
|
|
}
|
2015-12-19 06:02:14 +08:00
|
|
|
for (const auto &Path : DsymPaths) {
|
|
|
|
auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
|
2015-11-04 08:30:24 +08:00
|
|
|
if (!DbgObjOrErr)
|
|
|
|
continue;
|
|
|
|
ObjectFile *DbgObj = DbgObjOrErr.get();
|
2015-12-19 06:02:14 +08:00
|
|
|
const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
|
2015-11-04 08:30:24 +08:00
|
|
|
if (!MachDbgObj)
|
|
|
|
continue;
|
2015-12-19 06:02:14 +08:00
|
|
|
if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
|
2015-11-04 08:30:24 +08:00
|
|
|
return DbgObj;
|
2014-10-17 08:50:19 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:26 +08:00
|
|
|
ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path,
|
|
|
|
const ObjectFile *Obj,
|
|
|
|
const std::string &ArchName) {
|
|
|
|
std::string DebuglinkName;
|
|
|
|
uint32_t CRCHash;
|
|
|
|
std::string DebugBinaryPath;
|
|
|
|
if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
|
|
|
|
return nullptr;
|
|
|
|
if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath))
|
|
|
|
return nullptr;
|
2015-12-19 06:02:14 +08:00
|
|
|
auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
|
2015-11-04 08:30:26 +08:00
|
|
|
if (!DbgObjOrErr)
|
|
|
|
return nullptr;
|
|
|
|
return DbgObjOrErr.get();
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
ErrorOr<LLVMSymbolizer::ObjectPair>
|
2015-12-19 06:02:14 +08:00
|
|
|
LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path,
|
|
|
|
const std::string &ArchName) {
|
2014-10-17 08:50:19 +08:00
|
|
|
const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
|
|
|
|
if (I != ObjectPairForPathArch.end())
|
2013-06-28 16:15:40 +08:00
|
|
|
return I->second;
|
2015-11-04 08:30:24 +08:00
|
|
|
|
2015-12-19 06:02:14 +08:00
|
|
|
auto ObjOrErr = getOrCreateObject(Path, ArchName);
|
2015-11-04 08:30:24 +08:00
|
|
|
if (auto EC = ObjOrErr.getError()) {
|
|
|
|
ObjectPairForPathArch.insert(
|
|
|
|
std::make_pair(std::make_pair(Path, ArchName), EC));
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFile *Obj = ObjOrErr.get();
|
|
|
|
assert(Obj != nullptr);
|
|
|
|
ObjectFile *DbgObj = nullptr;
|
|
|
|
|
|
|
|
if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
|
|
|
|
DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
|
2015-11-04 08:30:26 +08:00
|
|
|
if (!DbgObj)
|
|
|
|
DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
|
2014-10-17 08:50:19 +08:00
|
|
|
if (!DbgObj)
|
|
|
|
DbgObj = Obj;
|
|
|
|
ObjectPair Res = std::make_pair(Obj, DbgObj);
|
2015-11-04 08:30:24 +08:00
|
|
|
ObjectPairForPathArch.insert(
|
|
|
|
std::make_pair(std::make_pair(Path, ArchName), Res));
|
2013-06-28 16:15:40 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
ErrorOr<ObjectFile *>
|
2015-12-19 06:02:14 +08:00
|
|
|
LLVMSymbolizer::getOrCreateObject(const std::string &Path,
|
|
|
|
const std::string &ArchName) {
|
|
|
|
const auto &I = BinaryForPath.find(Path);
|
|
|
|
Binary *Bin = nullptr;
|
|
|
|
if (I == BinaryForPath.end()) {
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
|
|
|
|
if (!BinOrErr) {
|
|
|
|
auto EC = errorToErrorCode(BinOrErr.takeError());
|
2015-12-19 06:02:14 +08:00
|
|
|
BinaryForPath.insert(std::make_pair(Path, EC));
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
assert(Bin != nullptr);
|
2015-12-19 06:02:14 +08:00
|
|
|
|
2013-06-28 16:15:40 +08:00
|
|
|
if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
|
2015-12-19 06:02:14 +08:00
|
|
|
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();
|
|
|
|
}
|
2016-06-01 04:35:34 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
|
2015-06-23 05:33:24 +08:00
|
|
|
UB->getObjectForArch(ArchName);
|
2016-06-01 04:35:34 +08:00
|
|
|
if (!ObjOrErr) {
|
|
|
|
auto EC = errorToErrorCode(ObjOrErr.takeError());
|
2015-12-19 06:02:14 +08:00
|
|
|
ObjectForUBPathAndArch.insert(
|
|
|
|
std::make_pair(std::make_pair(Path, ArchName), EC));
|
2015-11-04 08:30:24 +08:00
|
|
|
return EC;
|
2013-06-28 16:15:40 +08:00
|
|
|
}
|
2015-12-19 06:02:14 +08:00
|
|
|
ObjectFile *Res = ObjOrErr->get();
|
|
|
|
ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName),
|
|
|
|
std::move(ObjOrErr.get())));
|
2015-11-04 08:30:24 +08:00
|
|
|
return Res;
|
2013-06-28 16:15:40 +08:00
|
|
|
}
|
2015-11-04 08:30:24 +08:00
|
|
|
if (Bin->isObject()) {
|
|
|
|
return cast<ObjectFile>(Bin);
|
|
|
|
}
|
|
|
|
return object_error::arch_not_found;
|
2013-06-28 16:15:40 +08:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:30:24 +08:00
|
|
|
ErrorOr<SymbolizableModule *>
|
2013-02-15 16:54:47 +08:00
|
|
|
LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
|
2014-10-14 21:40:44 +08:00
|
|
|
const auto &I = Modules.find(ModuleName);
|
2015-11-04 08:30:24 +08:00
|
|
|
if (I != Modules.end()) {
|
|
|
|
auto &InfoOrErr = I->second;
|
|
|
|
if (auto EC = InfoOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
return InfoOrErr->get();
|
|
|
|
}
|
2013-06-28 16:15:40 +08:00
|
|
|
std::string BinaryName = ModuleName;
|
|
|
|
std::string ArchName = Opts.DefaultArch;
|
2013-07-17 14:45:36 +08:00
|
|
|
size_t ColonPos = ModuleName.find_last_of(':');
|
|
|
|
// Verify that substring after colon form a valid arch name.
|
2013-06-28 16:15:40 +08:00
|
|
|
if (ColonPos != std::string::npos) {
|
2013-07-17 14:45:36 +08:00
|
|
|
std::string ArchStr = ModuleName.substr(ColonPos + 1);
|
2013-07-17 14:53:51 +08:00
|
|
|
if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
|
2013-07-17 14:45:36 +08:00
|
|
|
BinaryName = ModuleName.substr(0, ColonPos);
|
|
|
|
ArchName = ArchStr;
|
|
|
|
}
|
2013-06-28 16:15:40 +08:00
|
|
|
}
|
2015-12-19 06:02:14 +08:00
|
|
|
auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
|
2015-11-04 08:30:24 +08:00
|
|
|
if (auto EC = ObjectsOrErr.getError()) {
|
2013-06-28 16:15:40 +08:00
|
|
|
// Failed to find valid object file.
|
2015-11-04 08:30:24 +08:00
|
|
|
Modules.insert(std::make_pair(ModuleName, EC));
|
|
|
|
return EC;
|
2013-01-22 22:21:19 +08:00
|
|
|
}
|
2015-11-04 08:30:24 +08:00
|
|
|
ObjectPair Objects = ObjectsOrErr.get();
|
|
|
|
|
2015-10-27 03:41:23 +08:00
|
|
|
std::unique_ptr<DIContext> Context;
|
2015-04-28 01:19:51 +08:00
|
|
|
if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
|
2016-05-05 04:32:13 +08:00
|
|
|
using namespace pdb;
|
2015-04-28 01:19:51 +08:00
|
|
|
// 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;
|
2016-05-07 04:51:57 +08:00
|
|
|
auto Error = loadDataForEXE(
|
|
|
|
PDB_ReaderType::DIA, Objects.first->getFileName(), Session);
|
|
|
|
if (!Error) {
|
2015-10-27 03:41:23 +08:00
|
|
|
Context.reset(new PDBContext(*CoffObject, std::move(Session)));
|
2016-05-07 06:29:34 +08:00
|
|
|
} else {
|
|
|
|
// Drop error
|
|
|
|
handleAllErrors(std::move(Error),
|
|
|
|
[](const ErrorInfoBase &) { return Error::success(); });
|
2015-05-07 06:26:30 +08:00
|
|
|
}
|
2015-04-28 01:19:51 +08:00
|
|
|
}
|
|
|
|
if (!Context)
|
2015-10-27 03:41:23 +08:00
|
|
|
Context.reset(new DWARFContextInMemory(*Objects.second));
|
2013-06-28 16:15:40 +08:00
|
|
|
assert(Context);
|
2015-11-04 08:30:24 +08:00
|
|
|
auto InfoOrErr =
|
2015-10-30 06:21:37 +08:00
|
|
|
SymbolizableObjectFile::create(Objects.first, std::move(Context));
|
2015-11-04 08:30:24 +08:00
|
|
|
auto InsertResult =
|
|
|
|
Modules.insert(std::make_pair(ModuleName, std::move(InfoOrErr)));
|
|
|
|
assert(InsertResult.second);
|
|
|
|
if (auto EC = InsertResult.first->second.getError())
|
|
|
|
return EC;
|
|
|
|
return InsertResult.first->second->get();
|
2013-01-22 22:21:19 +08:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-08-11 05:47:11 +08:00
|
|
|
// 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'.
|
2016-03-29 01:40:08 +08:00
|
|
|
StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
|
2015-08-11 05:47:11 +08:00
|
|
|
// Remove any '_' or '@' prefix.
|
|
|
|
char Front = SymbolName.empty() ? '\0' : SymbolName[0];
|
|
|
|
if (Front == '_' || Front == '@')
|
|
|
|
SymbolName = SymbolName.drop_front();
|
|
|
|
|
|
|
|
// Remove any '@[0-9]+' suffix.
|
|
|
|
if (Front != '?') {
|
|
|
|
size_t AtPos = SymbolName.rfind('@');
|
|
|
|
if (AtPos != StringRef::npos &&
|
|
|
|
std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(),
|
|
|
|
[](char C) { return C >= '0' && C <= '9'; })) {
|
|
|
|
SymbolName = SymbolName.substr(0, AtPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any ending '@' for vectorcall.
|
|
|
|
if (SymbolName.endswith("@"))
|
|
|
|
SymbolName = SymbolName.drop_back();
|
|
|
|
|
|
|
|
return SymbolName;
|
|
|
|
}
|
|
|
|
|
2016-03-29 01:40:08 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-01-22 22:21:19 +08:00
|
|
|
#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,
|
|
|
|
size_t *length, int *status);
|
|
|
|
#endif
|
|
|
|
|
2015-08-11 05:47:11 +08:00
|
|
|
std::string LLVMSymbolizer::DemangleName(const std::string &Name,
|
2015-10-30 06:21:37 +08:00
|
|
|
const SymbolizableModule *ModInfo) {
|
2013-01-22 22:21:19 +08:00
|
|
|
#if !defined(_MSC_VER)
|
2014-01-17 01:25:12 +08:00
|
|
|
// We can spoil names of symbols with C linkage, so use an heuristic
|
|
|
|
// approach to check if the name should be demangled.
|
2015-08-11 05:47:11 +08:00
|
|
|
if (Name.substr(0, 2) == "_Z") {
|
|
|
|
int status = 0;
|
|
|
|
char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
|
|
|
|
if (status != 0)
|
|
|
|
return Name;
|
|
|
|
std::string Result = DemangledName;
|
|
|
|
free(DemangledName);
|
|
|
|
return Result;
|
|
|
|
}
|
2013-06-28 20:06:25 +08:00
|
|
|
#else
|
2015-08-11 05:47:11 +08:00
|
|
|
if (!Name.empty() && Name.front() == '?') {
|
|
|
|
// Only do MSVC C++ demangling on symbols starting with '?'.
|
|
|
|
char DemangledName[1024] = {0};
|
|
|
|
DWORD result = ::UnDecorateSymbolName(
|
|
|
|
Name.c_str(), DemangledName, 1023,
|
|
|
|
UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected
|
|
|
|
UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc
|
|
|
|
UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications
|
|
|
|
UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers
|
|
|
|
UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords
|
|
|
|
UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types
|
|
|
|
return (result == 0) ? Name : std::string(DemangledName);
|
|
|
|
}
|
2013-01-22 22:21:19 +08:00
|
|
|
#endif
|
2015-10-30 07:49:19 +08:00
|
|
|
if (ModInfo && ModInfo->isWin32Module())
|
2015-08-11 05:47:11 +08:00
|
|
|
return std::string(demanglePE32ExternCFunc(Name));
|
|
|
|
return Name;
|
2013-01-22 22:21:19 +08:00
|
|
|
}
|
|
|
|
|
2013-02-15 16:54:47 +08:00
|
|
|
} // namespace symbolize
|
|
|
|
} // namespace llvm
|