1 //===-- LLVMSymbolize.h ----------------------------------------- C++ -----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Header for LLVM symbolization library. 11 // 12 //===----------------------------------------------------------------------===// 13 #ifndef LLVM_SYMBOLIZE_H 14 #define LLVM_SYMBOLIZE_H 15 16 #include "llvm/ADT/OwningPtr.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/DebugInfo/DIContext.h" 19 #include "llvm/Object/MachOUniversal.h" 20 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include <map> 23 #include <string> 24 25 namespace llvm { 26 27 using namespace object; 28 29 namespace symbolize { 30 31 class ModuleInfo; 32 33 class LLVMSymbolizer { 34 public: 35 struct Options { 36 bool UseSymbolTable : 1; 37 bool PrintFunctions : 1; 38 bool PrintInlining : 1; 39 bool Demangle : 1; 40 std::string DefaultArch; 41 Options(bool UseSymbolTable = true, bool PrintFunctions = true, 42 bool PrintInlining = true, bool Demangle = true, 43 std::string DefaultArch = "") 44 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions), 45 PrintInlining(PrintInlining), Demangle(Demangle), 46 DefaultArch(DefaultArch) { 47 } 48 }; 49 50 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {} 51 ~LLVMSymbolizer() { 52 flush(); 53 } 54 55 // Returns the result of symbolization for module name/offset as 56 // a string (possibly containing newlines). 57 std::string 58 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset); 59 std::string 60 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset); 61 void flush(); 62 static std::string DemangleName(const std::string &Name); 63 private: 64 typedef std::pair<Binary*, Binary*> BinaryPair; 65 66 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName); 67 /// \brief Returns pair of pointers to binary and debug binary. 68 BinaryPair getOrCreateBinary(const std::string &Path); 69 /// \brief Returns a parsed object file for a given architecture in a 70 /// universal binary (or the binary itself if it is an object file). 71 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName); 72 73 std::string printDILineInfo(DILineInfo LineInfo) const; 74 static std::string DemangleGlobalName(const std::string &Name); 75 76 // Owns all the parsed binaries and object files. 77 SmallVector<Binary*, 4> ParsedBinariesAndObjects; 78 // Owns module info objects. 79 typedef std::map<std::string, ModuleInfo *> ModuleMapTy; 80 ModuleMapTy Modules; 81 typedef std::map<std::string, BinaryPair> BinaryMapTy; 82 BinaryMapTy BinaryForPath; 83 typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *> 84 ObjectFileForArchMapTy; 85 ObjectFileForArchMapTy ObjectFileForArch; 86 87 Options Opts; 88 static const char kBadString[]; 89 }; 90 91 class ModuleInfo { 92 public: 93 ModuleInfo(ObjectFile *Obj, DIContext *DICtx); 94 95 DILineInfo symbolizeCode(uint64_t ModuleOffset, 96 const LLVMSymbolizer::Options &Opts) const; 97 DIInliningInfo symbolizeInlinedCode( 98 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const; 99 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start, 100 uint64_t &Size) const; 101 102 private: 103 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, 104 std::string &Name, uint64_t &Addr, 105 uint64_t &Size) const; 106 ObjectFile *Module; 107 OwningPtr<DIContext> DebugInfoContext; 108 109 struct SymbolDesc { 110 uint64_t Addr; 111 // If size is 0, assume that symbol occupies the whole memory range up to 112 // the following symbol. 113 uint64_t Size; 114 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) { 115 return s1.Addr < s2.Addr; 116 } 117 }; 118 typedef std::map<SymbolDesc, StringRef> SymbolMapTy; 119 SymbolMapTy Functions; 120 SymbolMapTy Objects; 121 }; 122 123 } // namespace symbolize 124 } // namespace llvm 125 126 #endif // LLVM_SYMBOLIZE_H 127