1//===-- SourcePrinter.h - source interleaving utilities --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
10#define LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/IndexedMap.h"
14#include "llvm/ADT/MapVector.h"
15#include "llvm/ADT/StringSet.h"
16#include "llvm/DebugInfo/DWARF/DWARFContext.h"
17#include "llvm/DebugInfo/Symbolize/Symbolize.h"
18#include "llvm/MC/MCRegisterInfo.h"
19#include "llvm/MC/MCSubtargetInfo.h"
20#include "llvm/Support/FormattedStream.h"
21#include <set>
22#include <vector>
23
24namespace llvm {
25namespace objdump {
26
27/// Base class for representing the location of a source-level variable or
28/// an inlined function.
29class LiveElement {
30protected:
31 const char *Name;
32 DWARFUnit *Unit;
33 const DWARFDie FuncDie;
34
35public:
36 LiveElement(const char *Name, DWARFUnit *Unit, const DWARFDie FuncDie)
37 : Name(Name), Unit(Unit), FuncDie(FuncDie) {}
38
39 virtual ~LiveElement() = default;
40 const char *getName() const { return Name; }
41
42 virtual bool liveAtAddress(object::SectionedAddress Addr) const = 0;
43 virtual void print(raw_ostream &OS, const MCRegisterInfo &MRI) const = 0;
44 virtual void dump(raw_ostream &OS) const = 0;
45 virtual void printElementLine(raw_ostream &OS,
46 object::SectionedAddress Address,
47 bool IsEnd) const {}
48};
49
50class InlinedFunction : public LiveElement {
51private:
52 DWARFDie InlinedFuncDie;
53 DWARFAddressRange Range;
54
55public:
56 InlinedFunction(const char *FunctionName, DWARFUnit *Unit,
57 const DWARFDie FuncDie, const DWARFDie InlinedFuncDie,
58 DWARFAddressRange &Range)
59 : LiveElement(FunctionName, Unit, FuncDie),
60 InlinedFuncDie(InlinedFuncDie), Range(Range) {}
61
62 bool liveAtAddress(object::SectionedAddress Addr) const override;
63 void print(raw_ostream &OS, const MCRegisterInfo &MRI) const override;
64 void dump(raw_ostream &OS) const override;
65 void printElementLine(raw_ostream &OS, object::SectionedAddress Address,
66 bool IsEnd) const override;
67};
68
69/// Stores a single expression representing the location of a source-level
70/// variable, along with the PC range for which that expression is valid.
71class LiveVariable : public LiveElement {
72private:
73 DWARFLocationExpression LocExpr;
74
75public:
76 LiveVariable(const DWARFLocationExpression &LocExpr, const char *VarName,
77 DWARFUnit *Unit, const DWARFDie FuncDie)
78 : LiveElement(VarName, Unit, FuncDie), LocExpr(LocExpr) {}
79
80 bool liveAtAddress(object::SectionedAddress Addr) const override;
81 void print(raw_ostream &OS, const MCRegisterInfo &MRI) const override;
82 void dump(raw_ostream &OS) const override;
83 const DWARFLocationExpression &getLocExpr() const { return LocExpr; }
84};
85
86/// Helper class for printing source locations for variables and inlined
87/// subroutines alongside disassembly.
88class LiveElementPrinter {
89 // Information we want to track about one column in which we are printing an
90 // element live range.
91 struct Column {
92 unsigned ElementIdx = NullElementIdx;
93 bool LiveIn = false;
94 bool LiveOut = false;
95 bool MustDrawLabel = false;
96
97 bool isActive() const { return ElementIdx != NullElementIdx; }
98
99 static constexpr unsigned NullElementIdx =
100 std::numeric_limits<unsigned>::max();
101
102 // Clear the column's data.
103 void clear() {
104 ElementIdx = NullElementIdx;
105 LiveIn = false;
106 LiveOut = false;
107 MustDrawLabel = false;
108 }
109 };
110
111 // Vector that owns all LiveElement objects for memory management.
112 std::vector<std::unique_ptr<LiveElement>> LiveElements;
113 // Map for fast lookup of live elements by their starting address (LowPC).
114 llvm::MapVector<uint64_t, std::vector<LiveElement *>> LiveElementsByAddress;
115 // Map for fast lookup of live elements by their ending address (HighPC).
116 llvm::MapVector<uint64_t, std::vector<LiveElement *>>
117 LiveElementsByEndAddress;
118 // Map from a LiveElement pointer to its index in the LiveElements vector.
119 llvm::DenseMap<LiveElement *, unsigned> ElementPtrToIndex;
120 // Map from a live element index to column index for efficient lookup.
121 llvm::DenseMap<unsigned, unsigned> ElementToColumn;
122 // Vector of columns currently used for printing live ranges.
123 std::vector<Column> ActiveCols;
124 // Set of available column indices kept sorted for efficient reuse.
125 std::set<unsigned> FreeCols;
126 // Vector of available column indices that can be reused.
127 std::vector<unsigned> ColumnsToFreeNextCycle;
128
129 const MCRegisterInfo &MRI;
130 const MCSubtargetInfo &STI;
131
132 void registerNewVariable();
133
134 void addInlinedFunction(DWARFDie FuncDie, DWARFDie InlinedFuncDie);
135 void addVariable(DWARFDie FuncDie, DWARFDie VarDie);
136
137 void addFunction(DWARFDie D);
138
139 // Get the column number (in characters) at which the first live element
140 // line should be printed.
141 unsigned getIndentLevel() const;
142
143 // Indent to the first live-range column to the right of the currently
144 // printed line, and return the index of that column.
145 // TODO: formatted_raw_ostream uses "column" to mean a number of characters
146 // since the last \n, and we use it to mean the number of slots in which we
147 // put live element lines. Pick a less overloaded word.
148 unsigned moveToFirstVarColumn(formatted_raw_ostream &OS);
149
150 // Get an existing column for a live element, or find a free one.
151 unsigned getOrCreateColumn(unsigned ElementIdx);
152
153 // Free a column when its element is no longer live.
154 void freeColumn(unsigned ColIdx);
155
156 // Returns the indices of all currently active elements, sorted by their DWARF
157 // discovery order.
158 std::vector<unsigned> getSortedActiveElementIndices() const;
159
160public:
161 LiveElementPrinter(const MCRegisterInfo &MRI, const MCSubtargetInfo &STI)
162 : MRI(MRI), STI(STI) {}
163
164 void dump() const;
165
166 void addCompileUnit(DWARFDie D);
167
168 /// Update to match the state of the instruction between ThisAddr and
169 /// NextAddr. In the common case, any live range active at ThisAddr is
170 /// live-in to the instruction, and any live range active at NextAddr is
171 /// live-out of the instruction. If IncludeDefinedVars is false, then live
172 /// ranges starting at NextAddr will be ignored.
173 void update(object::SectionedAddress ThisAddr,
174 object::SectionedAddress NextAddr, bool IncludeDefinedVars);
175
176 enum class LineChar {
177 RangeStart,
178 RangeMid,
179 RangeEnd,
180 LabelVert,
181 LabelCornerNew,
182 LabelCornerActive,
183 LabelHoriz,
184 };
185 const char *getLineChar(LineChar C) const;
186
187 /// Print live ranges to the right of an existing line. This assumes the
188 /// line is not an instruction, so doesn't start or end any live ranges, so
189 /// we only need to print active ranges or empty columns. If AfterInst is
190 /// true, this is being printed after the last instruction fed to update(),
191 /// otherwise this is being printed before it.
192 void printAfterOtherLine(formatted_raw_ostream &OS, bool AfterInst);
193
194 /// Print any live element range info needed to the right of a
195 /// non-instruction line of disassembly. This is where we print the variable
196 /// names and expressions, with thin line-drawing characters connecting them
197 /// to the live range which starts at the next instruction. If MustPrint is
198 /// true, we have to print at least one line (with the continuation of any
199 /// already-active live ranges) because something has already been printed
200 /// earlier on this line.
201 void printBetweenInsts(formatted_raw_ostream &OS, bool MustPrint);
202
203 /// Print the live element ranges to the right of a disassembled instruction.
204 void printAfterInst(formatted_raw_ostream &OS);
205
206 /// Print a line to idenfity the start/end of a live element.
207 void printBoundaryLine(formatted_raw_ostream &OS,
208 object::SectionedAddress Addr, bool IsEnd);
209};
210
211class SourcePrinter {
212protected:
213 DILineInfo OldLineInfo;
214 const object::ObjectFile *Obj = nullptr;
215 std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
216 // File name to file contents of source.
217 StringMap<std::unique_ptr<MemoryBuffer>> SourceCache;
218 // Mark the line endings of the cached source.
219 StringMap<std::vector<StringRef>> LineCache;
220 // Keep track of missing sources.
221 StringSet<> MissingSources;
222 // Only emit 'invalid debug info' warning once.
223 bool WarnedInvalidDebugInfo = false;
224
225private:
226 bool cacheSource(const DILineInfo &LineInfoFile);
227
228 void printLines(formatted_raw_ostream &OS, object::SectionedAddress Address,
229 const DILineInfo &LineInfo, StringRef Delimiter,
230 LiveElementPrinter &LEP);
231
232 void printSources(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
233 StringRef ObjectFilename, StringRef Delimiter,
234 LiveElementPrinter &LEP);
235
236 // Returns line source code corresponding to `LineInfo`.
237 // Returns empty string if source code cannot be found.
238 StringRef getLine(const DILineInfo &LineInfo, StringRef ObjectFilename);
239
240public:
241 SourcePrinter() = default;
242 SourcePrinter(const object::ObjectFile *Obj, StringRef DefaultArch);
243 virtual ~SourcePrinter() = default;
244 virtual void printSourceLine(formatted_raw_ostream &OS,
245 object::SectionedAddress Address,
246 StringRef ObjectFilename,
247 LiveElementPrinter &LEP,
248 StringRef Delimiter = "; ");
249};
250
251} // namespace objdump
252} // namespace llvm
253
254#endif
255