1 //===- tools/dsymutil/DebugMap.h - Generic debug map representation -------===//
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/ErrorOr.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/TimeValue.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
95 DebugMapObject &addDebugMapObject(StringRef ObjectFilePath,
96 sys::TimeValue Timestamp);
98 const Triple &getTriple() const { return BinaryTriple; }
100 StringRef getBinaryPath() const { return BinaryPath; }
102 void print(raw_ostream &OS) const;
108 /// Read a debug map for \a InputFile.
109 static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
110 parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
113 /// \brief The DebugMapObject represents one object file described by
114 /// the DebugMap. It contains a list of mappings between addresses in
115 /// the object file and in the linked binary for all the linked atoms
116 /// in this object file.
117 class DebugMapObject {
119 struct SymbolMapping {
120 yaml::Hex64 ObjectAddress;
121 yaml::Hex64 BinaryAddress;
123 SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size)
124 : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress),
126 /// For YAML IO support
127 SymbolMapping() = default;
130 typedef StringMapEntry<SymbolMapping> DebugMapEntry;
132 /// \brief Adds a symbol mapping to this DebugMapObject.
133 /// \returns false if the symbol was already registered. The request
134 /// is discarded in this case.
135 bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
136 uint64_t LinkedAddress, uint32_t Size);
138 /// \brief Lookup a symbol mapping.
139 /// \returns null if the symbol isn't found.
140 const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
142 /// \brief Lookup an objectfile address.
143 /// \returns null if the address isn't found.
144 const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
146 llvm::StringRef getObjectFilename() const { return Filename; }
148 sys::TimeValue getTimestamp() const { return Timestamp; }
150 iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
151 return make_range(Symbols.begin(), Symbols.end());
154 void print(raw_ostream &OS) const;
159 friend class DebugMap;
160 /// DebugMapObjects can only be constructed by the owning DebugMap.
161 DebugMapObject(StringRef ObjectFilename, sys::TimeValue Timestamp);
163 std::string Filename;
164 sys::TimeValue Timestamp;
165 StringMap<SymbolMapping> Symbols;
166 DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
168 /// For YAMLIO support.
170 typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
171 friend yaml::MappingTraits<dsymutil::DebugMapObject>;
172 friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
173 friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>;
174 DebugMapObject() = default;
177 DebugMapObject &operator=(DebugMapObject RHS) {
178 std::swap(Filename, RHS.Filename);
179 std::swap(Timestamp, RHS.Timestamp);
180 std::swap(Symbols, RHS.Symbols);
181 std::swap(AddressToMapping, RHS.AddressToMapping);
184 DebugMapObject(DebugMapObject &&RHS) {
185 Filename = std::move(RHS.Filename);
186 Timestamp = std::move(RHS.Timestamp);
187 Symbols = std::move(RHS.Symbols);
188 AddressToMapping = std::move(RHS.AddressToMapping);
195 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
200 using namespace llvm::dsymutil;
203 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
204 static void mapping(IO &io,
205 std::pair<std::string, DebugMapObject::SymbolMapping> &s);
206 static const bool flow = true;
209 template <> struct MappingTraits<dsymutil::DebugMapObject> {
211 static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
214 template <> struct ScalarTraits<Triple> {
215 static void output(const Triple &val, void *, llvm::raw_ostream &out);
216 static StringRef input(StringRef scalar, void *, Triple &value);
217 static bool mustQuote(StringRef) { return true; }
221 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
223 size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
224 static dsymutil::DebugMapObject &
225 element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
229 template <> struct MappingTraits<dsymutil::DebugMap> {
230 static void mapping(IO &io, dsymutil::DebugMap &DM);
233 template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
234 static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
239 #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H