clang-formatize llvm-symbolizer code

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175255 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov
2013-02-15 08:54:47 +00:00
parent 94328f4ff3
commit c4439c3508
3 changed files with 69 additions and 78 deletions

View File

@@ -22,13 +22,14 @@ namespace llvm {
namespace symbolize { namespace symbolize {
static bool error(error_code ec) { static bool error(error_code ec) {
if (!ec) return false; if (!ec)
return false;
errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n"; errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
return true; return true;
} }
static uint32_t getDILineInfoSpecifierFlags( static uint32_t
const LLVMSymbolizer::Options &Opts) { getDILineInfoSpecifierFlags(const LLVMSymbolizer::Options &Opts) {
uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo | uint32_t Flags = llvm::DILineInfoSpecifier::FileLineInfo |
llvm::DILineInfoSpecifier::AbsoluteFilePath; llvm::DILineInfoSpecifier::AbsoluteFilePath;
if (Opts.PrintFunctions) if (Opts.PrintFunctions)
@@ -44,35 +45,31 @@ static void patchFunctionNameInDILineInfo(const std::string &NewFunctionName,
} }
ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx) ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
: Module(Obj) : Module(Obj), DebugInfoContext(DICtx) {
, DebugInfoContext(DICtx) {
error_code ec; error_code ec;
for (symbol_iterator si = Module->begin_symbols(), for (symbol_iterator si = Module->begin_symbols(), se = Module->end_symbols();
se = Module->end_symbols();
si != se; si.increment(ec)) { si != se; si.increment(ec)) {
if (error(ec)) if (error(ec))
return; return;
SymbolRef::Type SymbolType; SymbolRef::Type SymbolType;
if (error(si->getType(SymbolType))) if (error(si->getType(SymbolType)))
continue; continue;
if (SymbolType != SymbolRef::ST_Function if (SymbolType != SymbolRef::ST_Function &&
&& SymbolType != SymbolRef::ST_Data) SymbolType != SymbolRef::ST_Data)
continue; continue;
uint64_t SymbolAddress; uint64_t SymbolAddress;
if (error(si->getAddress(SymbolAddress)) if (error(si->getAddress(SymbolAddress)) ||
|| SymbolAddress == UnknownAddressOrSize) SymbolAddress == UnknownAddressOrSize)
continue; continue;
uint64_t SymbolSize; uint64_t SymbolSize;
if (error(si->getSize(SymbolSize)) if (error(si->getSize(SymbolSize)) || SymbolSize == UnknownAddressOrSize)
|| SymbolSize == UnknownAddressOrSize)
continue; continue;
StringRef SymbolName; StringRef SymbolName;
if (error(si->getName(SymbolName))) if (error(si->getName(SymbolName)))
continue; continue;
// FIXME: If a function has alias, there are two entries in symbol table // FIXME: If a function has alias, there are two entries in symbol table
// with same address size. Make sure we choose the correct one. // with same address size. Make sure we choose the correct one.
SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? SymbolMapTy &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Functions : Objects;
SymbolDesc SD = { SymbolAddress, SymbolAddress + SymbolSize }; SymbolDesc SD = { SymbolAddress, SymbolAddress + SymbolSize };
M.insert(std::make_pair(SD, SymbolName)); M.insert(std::make_pair(SD, SymbolName));
} }
@@ -81,8 +78,7 @@ ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
std::string &Name, uint64_t &Addr, std::string &Name, uint64_t &Addr,
uint64_t &Size) const { uint64_t &Size) const {
const SymbolMapTy& M = Type == SymbolRef::ST_Function ? const SymbolMapTy &M = Type == SymbolRef::ST_Function ? Functions : Objects;
Functions : Objects;
SymbolDesc SD = { Address, Address + 1 }; SymbolDesc SD = { Address, Address + 1 };
SymbolMapTy::const_iterator it = M.find(SD); SymbolMapTy::const_iterator it = M.find(SD);
if (it == M.end()) if (it == M.end())
@@ -95,8 +91,8 @@ bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
return true; return true;
} }
DILineInfo ModuleInfo::symbolizeCode(uint64_t ModuleOffset, DILineInfo ModuleInfo::symbolizeCode(
const LLVMSymbolizer::Options& Opts) const { uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
DILineInfo LineInfo; DILineInfo LineInfo;
if (DebugInfoContext) { if (DebugInfoContext) {
LineInfo = DebugInfoContext->getLineInfoForAddress( LineInfo = DebugInfoContext->getLineInfoForAddress(
@@ -106,16 +102,16 @@ DILineInfo ModuleInfo::symbolizeCode(uint64_t ModuleOffset,
if (Opts.PrintFunctions && Opts.UseSymbolTable) { if (Opts.PrintFunctions && Opts.UseSymbolTable) {
std::string FunctionName; std::string FunctionName;
uint64_t Start, Size; uint64_t Start, Size;
if (getNameFromSymbolTable(SymbolRef::ST_Function, if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
ModuleOffset, FunctionName, Start, Size)) { FunctionName, Start, Size)) {
patchFunctionNameInDILineInfo(FunctionName, LineInfo); patchFunctionNameInDILineInfo(FunctionName, LineInfo);
} }
} }
return LineInfo; return LineInfo;
} }
DIInliningInfo ModuleInfo::symbolizeInlinedCode(uint64_t ModuleOffset, DIInliningInfo ModuleInfo::symbolizeInlinedCode(
const LLVMSymbolizer::Options& Opts) const { uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
DIInliningInfo InlinedContext; DIInliningInfo InlinedContext;
if (DebugInfoContext) { if (DebugInfoContext) {
InlinedContext = DebugInfoContext->getInliningInfoForAddress( InlinedContext = DebugInfoContext->getInliningInfoForAddress(
@@ -128,14 +124,13 @@ DIInliningInfo ModuleInfo::symbolizeInlinedCode(uint64_t ModuleOffset,
// Override the function name in lower frame with name from symbol table. // Override the function name in lower frame with name from symbol table.
if (Opts.PrintFunctions && Opts.UseSymbolTable) { if (Opts.PrintFunctions && Opts.UseSymbolTable) {
DIInliningInfo PatchedInlinedContext; DIInliningInfo PatchedInlinedContext;
for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
i < n; i++) {
DILineInfo LineInfo = InlinedContext.getFrame(i); DILineInfo LineInfo = InlinedContext.getFrame(i);
if (i == n - 1) { if (i == n - 1) {
std::string FunctionName; std::string FunctionName;
uint64_t Start, Size; uint64_t Start, Size;
if (getNameFromSymbolTable(SymbolRef::ST_Function, if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
ModuleOffset, FunctionName, Start, Size)) { FunctionName, Start, Size)) {
patchFunctionNameInDILineInfo(FunctionName, LineInfo); patchFunctionNameInDILineInfo(FunctionName, LineInfo);
} }
} }
@@ -148,8 +143,8 @@ DIInliningInfo ModuleInfo::symbolizeInlinedCode(uint64_t ModuleOffset,
bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name, bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
uint64_t &Start, uint64_t &Size) const { uint64_t &Start, uint64_t &Size) const {
return getNameFromSymbolTable(SymbolRef::ST_Data, return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
ModuleOffset, Name, Start, Size); Size);
} }
const char LLVMSymbolizer::kBadString[] = "??"; const char LLVMSymbolizer::kBadString[] = "??";
@@ -160,8 +155,8 @@ std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
if (Info == 0) if (Info == 0)
return printDILineInfo(DILineInfo()); return printDILineInfo(DILineInfo());
if (Opts.PrintInlining) { if (Opts.PrintInlining) {
DIInliningInfo InlinedContext = Info->symbolizeInlinedCode( DIInliningInfo InlinedContext =
ModuleOffset, Opts); Info->symbolizeInlinedCode(ModuleOffset, Opts);
uint32_t FramesNum = InlinedContext.getNumberOfFrames(); uint32_t FramesNum = InlinedContext.getNumberOfFrames();
assert(FramesNum > 0); assert(FramesNum > 0);
std::string Result; std::string Result;
@@ -192,8 +187,7 @@ std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
} }
// Returns true if the object endianness is known. // Returns true if the object endianness is known.
static bool getObjectEndianness(const ObjectFile *Obj, static bool getObjectEndianness(const ObjectFile *Obj, bool &IsLittleEndian) {
bool &IsLittleEndian) {
// FIXME: Implement this when libLLVMObject allows to do it easily. // FIXME: Implement this when libLLVMObject allows to do it easily.
IsLittleEndian = true; IsLittleEndian = true;
return true; return true;
@@ -215,8 +209,8 @@ static std::string getDarwinDWARFResourceForModule(const std::string &Path) {
return ResourceName.str(); return ResourceName.str();
} }
ModuleInfo *LLVMSymbolizer::getOrCreateModuleInfo( ModuleInfo *
const std::string &ModuleName) { LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
ModuleMapTy::iterator I = Modules.find(ModuleName); ModuleMapTy::iterator I = Modules.find(ModuleName);
if (I != Modules.end()) if (I != Modules.end())
return I->second; return I->second;
@@ -235,8 +229,8 @@ ModuleInfo *LLVMSymbolizer::getOrCreateModuleInfo(
// resource directory. // resource directory.
ObjectFile *DbgObj = Obj; ObjectFile *DbgObj = Obj;
if (isa<MachOObjectFile>(Obj)) { if (isa<MachOObjectFile>(Obj)) {
const std::string &ResourceName = getDarwinDWARFResourceForModule( const std::string &ResourceName =
ModuleName); getDarwinDWARFResourceForModule(ModuleName);
ObjectFile *ResourceObj = getObjectFile(ResourceName); ObjectFile *ResourceObj = getObjectFile(ResourceName);
if (ResourceObj != 0) if (ResourceObj != 0)
DbgObj = ResourceObj; DbgObj = ResourceObj;
@@ -265,8 +259,8 @@ std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
std::string Filename = LineInfo.getFileName(); std::string Filename = LineInfo.getFileName();
if (Filename == kDILineInfoBadString) if (Filename == kDILineInfoBadString)
Filename = kBadString; Filename = kBadString;
Result << Filename << ":" << LineInfo.getLine() Result << Filename << ":" << LineInfo.getLine() << ":" << LineInfo.getColumn()
<< ":" << LineInfo.getColumn() << "\n"; << "\n";
return Result.str(); return Result.str();
} }

View File

@@ -37,20 +37,19 @@ public:
bool Demangle : 1; bool Demangle : 1;
Options(bool UseSymbolTable = true, bool PrintFunctions = true, Options(bool UseSymbolTable = true, bool PrintFunctions = true,
bool PrintInlining = true, bool Demangle = true) bool PrintInlining = true, bool Demangle = true)
: UseSymbolTable(UseSymbolTable), : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
PrintFunctions(PrintFunctions), PrintInlining(PrintInlining), Demangle(Demangle) {
PrintInlining(PrintInlining), }
Demangle(Demangle) { }
}; };
LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {} LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
// Returns the result of symbolization for module name/offset as // Returns the result of symbolization for module name/offset as
// a string (possibly containing newlines). // a string (possibly containing newlines).
std::string symbolizeCode(const std::string &ModuleName, std::string
uint64_t ModuleOffset); symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
std::string symbolizeData(const std::string &ModuleName, std::string
uint64_t ModuleOffset); symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
private: private:
ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName); ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
std::string printDILineInfo(DILineInfo LineInfo) const; std::string printDILineInfo(DILineInfo LineInfo) const;
@@ -66,12 +65,12 @@ class ModuleInfo {
public: public:
ModuleInfo(ObjectFile *Obj, DIContext *DICtx); ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
DILineInfo symbolizeCode( DILineInfo symbolizeCode(uint64_t ModuleOffset,
uint64_t ModuleOffset, const LLVMSymbolizer::Options& Opts) const; const LLVMSymbolizer::Options &Opts) const;
DIInliningInfo symbolizeInlinedCode( DIInliningInfo symbolizeInlinedCode(
uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const; uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
bool symbolizeData(uint64_t ModuleOffset, std::string &Name, bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
uint64_t &Start, uint64_t &Size) const; uint64_t &Size) const;
private: private:
bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,

View File

@@ -45,11 +45,9 @@ ClPrintInlining("inlining", cl::init(true),
cl::desc("Print all inlined frames for a given address")); cl::desc("Print all inlined frames for a given address"));
static cl::opt<bool> static cl::opt<bool>
ClDemangle("demangle", cl::init(true), ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
cl::desc("Demangle function names"));
static bool parseCommand(bool &IsData, static bool parseCommand(bool &IsData, std::string &ModuleName,
std::string &ModuleName,
uint64_t &ModuleOffset) { uint64_t &ModuleOffset) {
const char *kDataCmd = "DATA "; const char *kDataCmd = "DATA ";
const char *kCodeCmd = "CODE "; const char *kCodeCmd = "CODE ";