Alexey Samsonov | ea83baf | 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 | //===----------------------------------------------------------------------===// |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 13 | #ifndef LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H |
| 14 | #define LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 15 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DIContext.h" |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 18 | #include "llvm/Object/MachOUniversal.h" |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ObjectFile.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include <map> |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 22 | #include <memory> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 27 | typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 28 | using namespace object; |
| 29 | |
| 30 | namespace symbolize { |
| 31 | |
| 32 | class ModuleInfo; |
| 33 | |
| 34 | class LLVMSymbolizer { |
| 35 | public: |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 36 | struct Options { |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 37 | bool UseSymbolTable : 1; |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 38 | FunctionNameKind PrintFunctions; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 39 | bool PrintInlining : 1; |
| 40 | bool Demangle : 1; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 41 | std::string DefaultArch; |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 42 | std::vector<std::string> DsymHints; |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 43 | Options(bool UseSymbolTable = true, |
| 44 | FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName, |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 45 | bool PrintInlining = true, bool Demangle = true, |
| 46 | std::string DefaultArch = "") |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 47 | : UseSymbolTable(UseSymbolTable), |
| 48 | PrintFunctions(PrintFunctions), PrintInlining(PrintInlining), |
| 49 | Demangle(Demangle), DefaultArch(DefaultArch) {} |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 52 | LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {} |
Alexey Samsonov | fe3a5d9 | 2013-06-28 15:08:29 +0000 | [diff] [blame] | 53 | ~LLVMSymbolizer() { |
| 54 | flush(); |
| 55 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 56 | |
| 57 | // Returns the result of symbolization for module name/offset as |
| 58 | // a string (possibly containing newlines). |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 59 | std::string |
| 60 | symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset); |
| 61 | std::string |
| 62 | symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset); |
Dmitry Vyukov | e8504e2 | 2013-03-19 10:24:42 +0000 | [diff] [blame] | 63 | void flush(); |
Alexey Samsonov | 601beb7 | 2013-06-28 12:06:25 +0000 | [diff] [blame] | 64 | static std::string DemangleName(const std::string &Name); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 65 | private: |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 66 | typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 67 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 68 | ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 69 | ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj, |
| 70 | const std::string &ArchName); |
| 71 | |
| 72 | /// \brief Returns pair of pointers to object and debug object. |
| 73 | ObjectPair getOrCreateObjects(const std::string &Path, |
| 74 | const std::string &ArchName); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 75 | /// \brief Returns a parsed object file for a given architecture in a |
| 76 | /// universal binary (or the binary itself if it is an object file). |
| 77 | ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName); |
| 78 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 79 | std::string printDILineInfo(DILineInfo LineInfo) const; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 80 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 81 | // Owns all the parsed binaries and object files. |
David Blaikie | 2f2021a | 2014-04-22 05:26:14 +0000 | [diff] [blame] | 82 | SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 83 | SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers; |
| 84 | void addOwningBinary(OwningBinary<Binary> Bin) { |
| 85 | ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary())); |
| 86 | MemoryBuffers.push_back(std::move(Bin.getBuffer())); |
| 87 | } |
| 88 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 89 | // Owns module info objects. |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 90 | std::map<std::string, ModuleInfo *> Modules; |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 91 | std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *> |
| 92 | ObjectFileForArch; |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 93 | std::map<std::pair<std::string, std::string>, ObjectPair> |
| 94 | ObjectPairForPathArch; |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 95 | |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 96 | Options Opts; |
Alexey Samsonov | d6cef10 | 2013-02-04 15:55:26 +0000 | [diff] [blame] | 97 | static const char kBadString[]; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | class ModuleInfo { |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 101 | public: |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 102 | ModuleInfo(ObjectFile *Obj, DIContext *DICtx); |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 103 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 104 | DILineInfo symbolizeCode(uint64_t ModuleOffset, |
| 105 | const LLVMSymbolizer::Options &Opts) const; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 106 | DIInliningInfo symbolizeInlinedCode( |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 107 | uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const; |
| 108 | bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start, |
| 109 | uint64_t &Size) const; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 110 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 111 | private: |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 112 | bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address, |
| 113 | std::string &Name, uint64_t &Addr, |
| 114 | uint64_t &Size) const; |
Alexey Samsonov | 464d2e4 | 2014-03-17 07:28:19 +0000 | [diff] [blame] | 115 | void addSymbol(const SymbolRef &Symbol); |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 116 | ObjectFile *Module; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 117 | std::unique_ptr<DIContext> DebugInfoContext; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 118 | |
| 119 | struct SymbolDesc { |
| 120 | uint64_t Addr; |
Alexey Samsonov | 35c987d | 2013-06-07 15:25:27 +0000 | [diff] [blame] | 121 | // If size is 0, assume that symbol occupies the whole memory range up to |
| 122 | // the following symbol. |
| 123 | uint64_t Size; |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 124 | friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) { |
Alexey Samsonov | 5239d58 | 2013-06-04 07:57:38 +0000 | [diff] [blame] | 125 | return s1.Addr < s2.Addr; |
Dmitry Vyukov | ef8fb72 | 2013-02-14 13:06:18 +0000 | [diff] [blame] | 126 | } |
| 127 | }; |
Alexander Potapenko | 45bfe37 | 2014-10-14 13:40:44 +0000 | [diff] [blame] | 128 | std::map<SymbolDesc, StringRef> Functions; |
| 129 | std::map<SymbolDesc, StringRef> Objects; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 132 | } // namespace symbolize |
| 133 | } // namespace llvm |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 134 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 135 | #endif |