1 //=== tools/dsymutil/DebugMap.h - Generic debug map representation -*- C++ -*-//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
20 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
22 #define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Support/Chrono.h"
30 #include "llvm/Support/ErrorOr.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/YAMLTraits.h"
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.
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());
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);
64 /// DIE.discardSubtree();
69 std::string BinaryPath;
70 typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
71 ObjectContainer Objects;
73 /// For YAML IO support.
75 friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
76 friend yaml::MappingTraits<DebugMap>;
80 DebugMap(const Triple &BinaryTriple, StringRef BinaryPath)
81 : BinaryTriple(BinaryTriple), BinaryPath(BinaryPath) {}
83 typedef ObjectContainer::const_iterator const_iterator;
85 iterator_range<const_iterator> objects() const {
86 return make_range(begin(), end());
89 const_iterator begin() const { return Objects.begin(); }
91 const_iterator end() const { return Objects.end(); }
93 /// This function adds an DebugMapObject to the list owned by this
96 addDebugMapObject(StringRef ObjectFilePath,
97 sys::TimePoint<std::chrono::seconds> Timestamp);
99 const Triple &getTriple() const { return BinaryTriple; }
101 StringRef getBinaryPath() const { return BinaryPath; }
103 void print(raw_ostream &OS) const;
109 /// Read a debug map for \a InputFile.
110 static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
111 parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
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 {
120 struct SymbolMapping {
121 Optional<yaml::Hex64> ObjectAddress;
122 yaml::Hex64 BinaryAddress;
124 SymbolMapping(Optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
126 : BinaryAddress(BinaryAddress), Size(Size) {
128 ObjectAddress = *ObjectAddr;
130 /// For YAML IO support
131 SymbolMapping() = default;
134 typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
135 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
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.
140 bool addSymbol(llvm::StringRef SymName, Optional<uint64_t> ObjectAddress,
141 uint64_t LinkedAddress, uint32_t Size);
143 /// \brief Lookup a symbol mapping.
144 /// \returns null if the symbol isn't found.
145 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
147 /// \brief Lookup an objectfile address.
148 /// \returns null if the address isn't found.
149 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
151 llvm::StringRef getObjectFilename() const { return Filename; }
153 sys::TimePoint<std::chrono::seconds> getTimestamp() const {
157 iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
158 return make_range(Symbols.begin(), Symbols.end());
161 void print(raw_ostream &OS) const;
166 friend class DebugMap;
167 /// DebugMapObjects can only be constructed by the owning DebugMap.
168 DebugMapObject(StringRef ObjectFilename,
169 sys::TimePoint<std::chrono::seconds> Timestamp);
171 std::string Filename;
172 sys::TimePoint<std::chrono::seconds> Timestamp;
173 StringMap<SymbolMapping> Symbols;
174 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
176 /// For YAMLIO support.
178 friend yaml::MappingTraits<dsymutil::DebugMapObject>;
179 friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
180 DebugMapObject() = default;
183 DebugMapObject(DebugMapObject &&) = default;
184 DebugMapObject &operator=(DebugMapObject &&) = default;
190 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
195 using namespace llvm::dsymutil;
198 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
199 static void mapping(IO &io,
200 std::pair<std::string, DebugMapObject::SymbolMapping> &s);
201 static const bool flow = true;
204 template <> struct MappingTraits<dsymutil::DebugMapObject> {
206 static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
209 template <> struct ScalarTraits<Triple> {
210 static void output(const Triple &val, void *, llvm::raw_ostream &out);
211 static StringRef input(StringRef scalar, void *, Triple &value);
212 static bool mustQuote(StringRef) { return true; }
216 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
218 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
219 static dsymutil::DebugMapObject &
220 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
224 template <> struct MappingTraits<dsymutil::DebugMap> {
225 static void mapping(IO &io, dsymutil::DebugMap &DM);
228 template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
229 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
234 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H