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,36 +45,32 @@ 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,9 +78,8 @@ 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())
return false; return false;
@@ -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;
@@ -224,7 +218,7 @@ ModuleInfo *LLVMSymbolizer::getOrCreateModuleInfo(
ObjectFile *Obj = getObjectFile(ModuleName); ObjectFile *Obj = getObjectFile(ModuleName);
if (Obj == 0) { if (Obj == 0) {
// Module name doesn't point to a valid object file. // Module name doesn't point to a valid object file.
Modules.insert(make_pair(ModuleName, (ModuleInfo*)0)); Modules.insert(make_pair(ModuleName, (ModuleInfo *)0));
return 0; return 0;
} }
@@ -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();
} }
@@ -289,5 +283,5 @@ void LLVMSymbolizer::DemangleName(std::string &Name) const {
#endif #endif
} }
} // namespace symbolize } // namespace symbolize
} // namespace llvm } // namespace llvm

View File

@@ -30,50 +30,49 @@ class ModuleInfo;
class LLVMSymbolizer { class LLVMSymbolizer {
public: public:
struct Options { struct Options {
bool UseSymbolTable : 1; bool UseSymbolTable : 1;
bool PrintFunctions : 1; bool PrintFunctions : 1;
bool PrintInlining : 1; bool PrintInlining : 1;
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;
void DemangleName(std::string &Name) const; void DemangleName(std::string &Name) const;
typedef std::map<std::string, ModuleInfo*> ModuleMapTy; typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
ModuleMapTy Modules; ModuleMapTy Modules;
Options Opts; Options Opts;
static const char kBadString[]; static const char kBadString[];
}; };
class ModuleInfo { 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,
std::string &Name, uint64_t &Addr, std::string &Name, uint64_t &Addr,
uint64_t &Size) const; uint64_t &Size) const;
@@ -83,7 +82,7 @@ class ModuleInfo {
struct SymbolDesc { struct SymbolDesc {
uint64_t Addr; uint64_t Addr;
uint64_t AddrEnd; uint64_t AddrEnd;
friend bool operator<(const SymbolDesc& s1, const SymbolDesc& s2) { friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
return s1.AddrEnd <= s2.Addr; return s1.AddrEnd <= s2.Addr;
} }
}; };
@@ -92,7 +91,7 @@ class ModuleInfo {
SymbolMapTy Objects; SymbolMapTy Objects;
}; };
} // namespace symbolize } // namespace symbolize
} // namespace llvm } // namespace llvm
#endif // LLVM_SYMBOLIZE_H #endif // LLVM_SYMBOLIZE_H

View File

@@ -32,24 +32,22 @@ using namespace symbolize;
static cl::opt<bool> static cl::opt<bool>
ClUseSymbolTable("use-symbol-table", cl::init(true), ClUseSymbolTable("use-symbol-table", cl::init(true),
cl::desc("Prefer names in symbol table to names " cl::desc("Prefer names in symbol table to names "
"in debug info")); "in debug info"));
static cl::opt<bool> static cl::opt<bool>
ClPrintFunctions("functions", cl::init(true), ClPrintFunctions("functions", cl::init(true),
cl::desc("Print function names as well as line " cl::desc("Print function names as well as line "
"information for a given address")); "information for a given address"));
static cl::opt<bool> static cl::opt<bool>
ClPrintInlining("inlining", cl::init(true), 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 ";
@@ -75,7 +73,7 @@ static bool parseCommand(bool &IsData,
// FIXME: Handle case when filename is given in quotes. // FIXME: Handle case when filename is given in quotes.
if (char *FilePath = strtok(pos, kDelimiters)) { if (char *FilePath = strtok(pos, kDelimiters)) {
ModuleName = FilePath; ModuleName = FilePath;
if (char *OffsetStr = strtok((char*)0, kDelimiters)) if (char *OffsetStr = strtok((char *)0, kDelimiters))
ModuleOffsetStr = OffsetStr; ModuleOffsetStr = OffsetStr;
} }
if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset)) if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset))
@@ -87,7 +85,7 @@ int main(int argc, char **argv) {
// Print stack trace if we signal out. // Print stack trace if we signal out.
sys::PrintStackTraceOnErrorSignal(); sys::PrintStackTraceOnErrorSignal();
PrettyStackTraceProgram X(argc, argv); PrettyStackTraceProgram X(argc, argv);
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n"); cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n");
LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions, LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,