Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 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/DebugInfo/DIContext.h" |
| 18 | #include "llvm/Object/ObjectFile.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | using namespace object; |
| 26 | |
| 27 | namespace symbolize { |
| 28 | |
| 29 | class ModuleInfo; |
| 30 | |
| 31 | class LLVMSymbolizer { |
| 32 | public: |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 33 | struct Options { |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 34 | bool UseSymbolTable : 1; |
| 35 | bool PrintFunctions : 1; |
| 36 | bool PrintInlining : 1; |
| 37 | bool Demangle : 1; |
| 38 | Options(bool UseSymbolTable = true, bool PrintFunctions = true, |
| 39 | bool PrintInlining = true, bool Demangle = true) |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 40 | : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions), |
| 41 | PrintInlining(PrintInlining), Demangle(Demangle) { |
| 42 | } |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 45 | LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {} |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 46 | |
| 47 | // Returns the result of symbolization for module name/offset as |
| 48 | // a string (possibly containing newlines). |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 49 | std::string |
| 50 | symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset); |
| 51 | std::string |
| 52 | symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset); |
Dmitry Vyukov | e9e10d1 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 53 | void flush(); |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 54 | private: |
| 55 | ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName); |
| 56 | std::string printDILineInfo(DILineInfo LineInfo) const; |
| 57 | void DemangleName(std::string &Name) const; |
| 58 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 59 | typedef std::map<std::string, ModuleInfo *> ModuleMapTy; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 60 | ModuleMapTy Modules; |
| 61 | Options Opts; |
Alexey Samsonov | 638c63c | 2013-02-04 15:55:26 +0000 | [diff] [blame] | 62 | static const char kBadString[]; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class ModuleInfo { |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 66 | public: |
Dmitry Vyukov | b181919 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 67 | ModuleInfo(ObjectFile *Obj, DIContext *DICtx); |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 68 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 69 | DILineInfo symbolizeCode(uint64_t ModuleOffset, |
| 70 | const LLVMSymbolizer::Options &Opts) const; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 71 | DIInliningInfo symbolizeInlinedCode( |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 72 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const; |
| 73 | bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start, |
| 74 | uint64_t &Size) const; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 75 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 76 | private: |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 77 | bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, |
| 78 | std::string &Name, uint64_t &Addr, |
| 79 | uint64_t &Size) const; |
Dmitry Vyukov | b181919 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 80 | OwningPtr<ObjectFile> Module; |
| 81 | OwningPtr<DIContext> DebugInfoContext; |
| 82 | |
| 83 | struct SymbolDesc { |
| 84 | uint64_t Addr; |
Alexey Samsonov | b656464 | 2013-06-07 15:25:27 +0000 | [diff] [blame^] | 85 | // If size is 0, assume that symbol occupies the whole memory range up to |
| 86 | // the following symbol. |
| 87 | uint64_t Size; |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 88 | friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) { |
Alexey Samsonov | 888ca96 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 89 | return s1.Addr < s2.Addr; |
Dmitry Vyukov | b181919 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 90 | } |
| 91 | }; |
| 92 | typedef std::map<SymbolDesc, StringRef> SymbolMapTy; |
| 93 | SymbolMapTy Functions; |
| 94 | SymbolMapTy Objects; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 97 | } // namespace symbolize |
| 98 | } // namespace llvm |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 99 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 100 | #endif // LLVM_SYMBOLIZE_H |