Adrian Prantl | a9e2383 | 2016-01-14 18:31:07 +0000 | [diff] [blame] | 1 | //=== tools/dsymutil/DebugMap.h - Generic debug map representation -*- C++ -*-// |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// |
| 12 | /// This file contains the class declaration of the DebugMap |
| 13 | /// entity. A DebugMap lists all the object files linked together to |
| 14 | /// produce an executable along with the linked address of all the |
| 15 | /// atoms used in these object files. |
| 16 | /// The DebugMap is an input to the DwarfLinker class that will |
| 17 | /// extract the Dwarf debug information from the referenced object |
| 18 | /// files and link their usefull debug info together. |
| 19 | /// |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | #ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H |
| 22 | #define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H |
| 23 | |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringMap.h" |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/iterator_range.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 28 | #include "llvm/Object/ObjectFile.h" |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Chrono.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorOr.h" |
| 31 | #include "llvm/Support/Format.h" |
Frederic Riss | c0866ad | 2015-06-05 16:35:44 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Path.h" |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 33 | #include "llvm/Support/YAMLTraits.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
| 36 | namespace llvm { |
| 37 | class raw_ostream; |
| 38 | |
| 39 | namespace dsymutil { |
| 40 | class DebugMapObject; |
| 41 | |
| 42 | /// \brief The DebugMap object stores the list of object files to |
| 43 | /// query for debug information along with the mapping between the |
| 44 | /// symbols' addresses in the object file to their linked address in |
| 45 | /// the linked binary. |
| 46 | /// |
| 47 | /// A DebugMap producer could look like this: |
| 48 | /// DebugMap *DM = new DebugMap(); |
| 49 | /// for (const auto &Obj: LinkedObjects) { |
| 50 | /// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath()); |
| 51 | /// for (const auto &Sym: Obj.getLinkedSymbols()) |
| 52 | /// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(), |
| 53 | /// Sym.getBinaryAddress()); |
| 54 | /// } |
| 55 | /// |
| 56 | /// A DebugMap consumer can then use the map to link the debug |
| 57 | /// information. For example something along the lines of: |
| 58 | /// for (const auto &DMO: DM->objects()) { |
| 59 | /// auto Obj = createBinary(DMO.getObjectFilename()); |
| 60 | /// for (auto &DIE: Obj.getDwarfDIEs()) { |
| 61 | /// if (SymbolMapping *Sym = DMO.lookup(DIE.getName())) |
| 62 | /// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress); |
| 63 | /// else |
| 64 | /// DIE.discardSubtree(); |
| 65 | /// } |
| 66 | /// } |
| 67 | class DebugMap { |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 68 | Triple BinaryTriple; |
Frederic Riss | 2c69d36 | 2015-08-26 05:09:59 +0000 | [diff] [blame] | 69 | std::string BinaryPath; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 70 | typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer; |
| 71 | ObjectContainer Objects; |
| 72 | |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 73 | /// For YAML IO support. |
| 74 | ///@{ |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 75 | friend yaml::MappingTraits<std::unique_ptr<DebugMap>>; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 76 | friend yaml::MappingTraits<DebugMap>; |
| 77 | DebugMap() = default; |
| 78 | ///@} |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 79 | public: |
Frederic Riss | 2c69d36 | 2015-08-26 05:09:59 +0000 | [diff] [blame] | 80 | DebugMap(const Triple &BinaryTriple, StringRef BinaryPath) |
| 81 | : BinaryTriple(BinaryTriple), BinaryPath(BinaryPath) {} |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 82 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 83 | typedef ObjectContainer::const_iterator const_iterator; |
| 84 | |
| 85 | iterator_range<const_iterator> objects() const { |
| 86 | return make_range(begin(), end()); |
| 87 | } |
| 88 | |
| 89 | const_iterator begin() const { return Objects.begin(); } |
| 90 | |
| 91 | const_iterator end() const { return Objects.end(); } |
| 92 | |
| 93 | /// This function adds an DebugMapObject to the list owned by this |
| 94 | /// debug map. |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 95 | DebugMapObject & |
| 96 | addDebugMapObject(StringRef ObjectFilePath, |
| 97 | sys::TimePoint<std::chrono::seconds> Timestamp); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 98 | |
Frederic Riss | 717354f | 2015-02-28 00:28:56 +0000 | [diff] [blame] | 99 | const Triple &getTriple() const { return BinaryTriple; } |
Frederic Riss | e4a6fef | 2015-01-19 23:33:14 +0000 | [diff] [blame] | 100 | |
Frederic Riss | 2c69d36 | 2015-08-26 05:09:59 +0000 | [diff] [blame] | 101 | StringRef getBinaryPath() const { return BinaryPath; } |
| 102 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 103 | void print(raw_ostream &OS) const; |
| 104 | |
| 105 | #ifndef NDEBUG |
| 106 | void dump() const; |
| 107 | #endif |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 108 | |
| 109 | /// Read a debug map for \a InputFile. |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 110 | static ErrorOr<std::vector<std::unique_ptr<DebugMap>>> |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 111 | parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /// \brief The DebugMapObject represents one object file described by |
| 115 | /// the DebugMap. It contains a list of mappings between addresses in |
| 116 | /// the object file and in the linked binary for all the linked atoms |
| 117 | /// in this object file. |
| 118 | class DebugMapObject { |
| 119 | public: |
| 120 | struct SymbolMapping { |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame] | 121 | Optional<yaml::Hex64> ObjectAddress; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 122 | yaml::Hex64 BinaryAddress; |
| 123 | yaml::Hex32 Size; |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame] | 124 | SymbolMapping(Optional<uint64_t> ObjectAddr, uint64_t BinaryAddress, |
| 125 | uint32_t Size) |
| 126 | : BinaryAddress(BinaryAddress), Size(Size) { |
| 127 | if (ObjectAddr) |
| 128 | ObjectAddress = *ObjectAddr; |
| 129 | } |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 130 | /// For YAML IO support |
| 131 | SymbolMapping() = default; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Zachary Turner | 9e91c28 | 2016-08-05 23:12:31 +0000 | [diff] [blame] | 134 | typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping; |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 135 | typedef StringMapEntry<SymbolMapping> DebugMapEntry; |
| 136 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 137 | /// \brief Adds a symbol mapping to this DebugMapObject. |
| 138 | /// \returns false if the symbol was already registered. The request |
| 139 | /// is discarded in this case. |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame] | 140 | bool addSymbol(llvm::StringRef SymName, Optional<uint64_t> ObjectAddress, |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 141 | uint64_t LinkedAddress, uint32_t Size); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 142 | |
Benjamin Kramer | 342554f | 2014-12-13 19:19:07 +0000 | [diff] [blame] | 143 | /// \brief Lookup a symbol mapping. |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 144 | /// \returns null if the symbol isn't found. |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 145 | const DebugMapEntry *lookupSymbol(StringRef SymbolName) const; |
| 146 | |
| 147 | /// \brief Lookup an objectfile address. |
| 148 | /// \returns null if the address isn't found. |
| 149 | const DebugMapEntry *lookupObjectAddress(uint64_t Address) const; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 150 | |
| 151 | llvm::StringRef getObjectFilename() const { return Filename; } |
| 152 | |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 153 | sys::TimePoint<std::chrono::seconds> getTimestamp() const { |
| 154 | return Timestamp; |
| 155 | } |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 156 | |
Frederic Riss | b046408 | 2015-03-15 02:02:53 +0000 | [diff] [blame] | 157 | iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const { |
| 158 | return make_range(Symbols.begin(), Symbols.end()); |
| 159 | } |
| 160 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 161 | void print(raw_ostream &OS) const; |
| 162 | #ifndef NDEBUG |
| 163 | void dump() const; |
| 164 | #endif |
| 165 | private: |
| 166 | friend class DebugMap; |
| 167 | /// DebugMapObjects can only be constructed by the owning DebugMap. |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 168 | DebugMapObject(StringRef ObjectFilename, |
| 169 | sys::TimePoint<std::chrono::seconds> Timestamp); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 170 | |
| 171 | std::string Filename; |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 172 | sys::TimePoint<std::chrono::seconds> Timestamp; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 173 | StringMap<SymbolMapping> Symbols; |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 174 | DenseMap<uint64_t, DebugMapEntry *> AddressToMapping; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 175 | |
| 176 | /// For YAMLIO support. |
| 177 | ///@{ |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 178 | friend yaml::MappingTraits<dsymutil::DebugMapObject>; |
| 179 | friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 180 | DebugMapObject() = default; |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
Benjamin Kramer | 0eae9ec | 2016-10-27 15:23:44 +0000 | [diff] [blame] | 183 | DebugMapObject(DebugMapObject &&) = default; |
| 184 | DebugMapObject &operator=(DebugMapObject &&) = default; |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 185 | ///@} |
| 186 | }; |
| 187 | } |
| 188 | } |
| 189 | |
Frederic Riss | 88ad8d2 | 2015-06-01 21:25:53 +0000 | [diff] [blame] | 190 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping) |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 191 | |
| 192 | namespace llvm { |
| 193 | namespace yaml { |
| 194 | |
| 195 | using namespace llvm::dsymutil; |
| 196 | |
| 197 | template <> |
| 198 | struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 199 | static void mapping(IO &io, |
| 200 | std::pair<std::string, DebugMapObject::SymbolMapping> &s); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 201 | static const bool flow = true; |
| 202 | }; |
| 203 | |
| 204 | template <> struct MappingTraits<dsymutil::DebugMapObject> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 205 | struct YamlDMO; |
| 206 | static void mapping(IO &io, dsymutil::DebugMapObject &DMO); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | template <> struct ScalarTraits<Triple> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 210 | static void output(const Triple &val, void *, llvm::raw_ostream &out); |
| 211 | static StringRef input(StringRef scalar, void *, Triple &value); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 212 | static bool mustQuote(StringRef) { return true; } |
| 213 | }; |
| 214 | |
| 215 | template <> |
| 216 | struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> { |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 217 | static size_t |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 218 | size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 219 | static dsymutil::DebugMapObject & |
| 220 | element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq, |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 221 | size_t index); |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | template <> struct MappingTraits<dsymutil::DebugMap> { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 225 | static void mapping(IO &io, dsymutil::DebugMap &DM); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 226 | }; |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 227 | |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 228 | template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> { |
| 229 | static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM); |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 230 | }; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H |