1*82d56013Sjoerg //===-- SourcePrinter.cpp - source interleaving utilities ----------------===//
2*82d56013Sjoerg //
3*82d56013Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*82d56013Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*82d56013Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*82d56013Sjoerg //
7*82d56013Sjoerg //===----------------------------------------------------------------------===//
8*82d56013Sjoerg //
9*82d56013Sjoerg // This file implements the LiveVariablePrinter and SourcePrinter classes to
10*82d56013Sjoerg // keep track of DWARF info as the current address is updated, and print out the
11*82d56013Sjoerg // source file line and variable liveness as needed.
12*82d56013Sjoerg //
13*82d56013Sjoerg //===----------------------------------------------------------------------===//
14*82d56013Sjoerg
15*82d56013Sjoerg #include "SourcePrinter.h"
16*82d56013Sjoerg #include "llvm-objdump.h"
17*82d56013Sjoerg #include "llvm/ADT/SmallSet.h"
18*82d56013Sjoerg #include "llvm/ADT/StringSet.h"
19*82d56013Sjoerg #include "llvm/MC/MCSubtargetInfo.h"
20*82d56013Sjoerg #include "llvm/Support/FormatVariadic.h"
21*82d56013Sjoerg
22*82d56013Sjoerg #define DEBUG_TYPE "objdump"
23*82d56013Sjoerg
24*82d56013Sjoerg namespace llvm {
25*82d56013Sjoerg namespace objdump {
26*82d56013Sjoerg
getInstStartColumn(const MCSubtargetInfo & STI)27*82d56013Sjoerg unsigned getInstStartColumn(const MCSubtargetInfo &STI) {
28*82d56013Sjoerg return !ShowRawInsn ? 16 : STI.getTargetTriple().isX86() ? 40 : 24;
29*82d56013Sjoerg }
30*82d56013Sjoerg
liveAtAddress(object::SectionedAddress Addr)31*82d56013Sjoerg bool LiveVariable::liveAtAddress(object::SectionedAddress Addr) {
32*82d56013Sjoerg if (LocExpr.Range == None)
33*82d56013Sjoerg return false;
34*82d56013Sjoerg return LocExpr.Range->SectionIndex == Addr.SectionIndex &&
35*82d56013Sjoerg LocExpr.Range->LowPC <= Addr.Address &&
36*82d56013Sjoerg LocExpr.Range->HighPC > Addr.Address;
37*82d56013Sjoerg }
38*82d56013Sjoerg
print(raw_ostream & OS,const MCRegisterInfo & MRI) const39*82d56013Sjoerg void LiveVariable::print(raw_ostream &OS, const MCRegisterInfo &MRI) const {
40*82d56013Sjoerg DataExtractor Data({LocExpr.Expr.data(), LocExpr.Expr.size()},
41*82d56013Sjoerg Unit->getContext().isLittleEndian(), 0);
42*82d56013Sjoerg DWARFExpression Expression(Data, Unit->getAddressByteSize());
43*82d56013Sjoerg Expression.printCompact(OS, MRI);
44*82d56013Sjoerg }
45*82d56013Sjoerg
addVariable(DWARFDie FuncDie,DWARFDie VarDie)46*82d56013Sjoerg void LiveVariablePrinter::addVariable(DWARFDie FuncDie, DWARFDie VarDie) {
47*82d56013Sjoerg uint64_t FuncLowPC, FuncHighPC, SectionIndex;
48*82d56013Sjoerg FuncDie.getLowAndHighPC(FuncLowPC, FuncHighPC, SectionIndex);
49*82d56013Sjoerg const char *VarName = VarDie.getName(DINameKind::ShortName);
50*82d56013Sjoerg DWARFUnit *U = VarDie.getDwarfUnit();
51*82d56013Sjoerg
52*82d56013Sjoerg Expected<DWARFLocationExpressionsVector> Locs =
53*82d56013Sjoerg VarDie.getLocations(dwarf::DW_AT_location);
54*82d56013Sjoerg if (!Locs) {
55*82d56013Sjoerg // If the variable doesn't have any locations, just ignore it. We don't
56*82d56013Sjoerg // report an error or warning here as that could be noisy on optimised
57*82d56013Sjoerg // code.
58*82d56013Sjoerg consumeError(Locs.takeError());
59*82d56013Sjoerg return;
60*82d56013Sjoerg }
61*82d56013Sjoerg
62*82d56013Sjoerg for (const DWARFLocationExpression &LocExpr : *Locs) {
63*82d56013Sjoerg if (LocExpr.Range) {
64*82d56013Sjoerg LiveVariables.emplace_back(LocExpr, VarName, U, FuncDie);
65*82d56013Sjoerg } else {
66*82d56013Sjoerg // If the LocExpr does not have an associated range, it is valid for
67*82d56013Sjoerg // the whole of the function.
68*82d56013Sjoerg // TODO: technically it is not valid for any range covered by another
69*82d56013Sjoerg // LocExpr, does that happen in reality?
70*82d56013Sjoerg DWARFLocationExpression WholeFuncExpr{
71*82d56013Sjoerg DWARFAddressRange(FuncLowPC, FuncHighPC, SectionIndex), LocExpr.Expr};
72*82d56013Sjoerg LiveVariables.emplace_back(WholeFuncExpr, VarName, U, FuncDie);
73*82d56013Sjoerg }
74*82d56013Sjoerg }
75*82d56013Sjoerg }
76*82d56013Sjoerg
addFunction(DWARFDie D)77*82d56013Sjoerg void LiveVariablePrinter::addFunction(DWARFDie D) {
78*82d56013Sjoerg for (const DWARFDie &Child : D.children()) {
79*82d56013Sjoerg if (Child.getTag() == dwarf::DW_TAG_variable ||
80*82d56013Sjoerg Child.getTag() == dwarf::DW_TAG_formal_parameter)
81*82d56013Sjoerg addVariable(D, Child);
82*82d56013Sjoerg else
83*82d56013Sjoerg addFunction(Child);
84*82d56013Sjoerg }
85*82d56013Sjoerg }
86*82d56013Sjoerg
87*82d56013Sjoerg // Get the column number (in characters) at which the first live variable
88*82d56013Sjoerg // line should be printed.
getIndentLevel() const89*82d56013Sjoerg unsigned LiveVariablePrinter::getIndentLevel() const {
90*82d56013Sjoerg return DbgIndent + getInstStartColumn(STI);
91*82d56013Sjoerg }
92*82d56013Sjoerg
93*82d56013Sjoerg // Indent to the first live-range column to the right of the currently
94*82d56013Sjoerg // printed line, and return the index of that column.
95*82d56013Sjoerg // TODO: formatted_raw_ostream uses "column" to mean a number of characters
96*82d56013Sjoerg // since the last \n, and we use it to mean the number of slots in which we
97*82d56013Sjoerg // put live variable lines. Pick a less overloaded word.
moveToFirstVarColumn(formatted_raw_ostream & OS)98*82d56013Sjoerg unsigned LiveVariablePrinter::moveToFirstVarColumn(formatted_raw_ostream &OS) {
99*82d56013Sjoerg // Logical column number: column zero is the first column we print in, each
100*82d56013Sjoerg // logical column is 2 physical columns wide.
101*82d56013Sjoerg unsigned FirstUnprintedLogicalColumn =
102*82d56013Sjoerg std::max((int)(OS.getColumn() - getIndentLevel() + 1) / 2, 0);
103*82d56013Sjoerg // Physical column number: the actual column number in characters, with
104*82d56013Sjoerg // zero being the left-most side of the screen.
105*82d56013Sjoerg unsigned FirstUnprintedPhysicalColumn =
106*82d56013Sjoerg getIndentLevel() + FirstUnprintedLogicalColumn * 2;
107*82d56013Sjoerg
108*82d56013Sjoerg if (FirstUnprintedPhysicalColumn > OS.getColumn())
109*82d56013Sjoerg OS.PadToColumn(FirstUnprintedPhysicalColumn);
110*82d56013Sjoerg
111*82d56013Sjoerg return FirstUnprintedLogicalColumn;
112*82d56013Sjoerg }
113*82d56013Sjoerg
findFreeColumn()114*82d56013Sjoerg unsigned LiveVariablePrinter::findFreeColumn() {
115*82d56013Sjoerg for (unsigned ColIdx = 0; ColIdx < ActiveCols.size(); ++ColIdx)
116*82d56013Sjoerg if (!ActiveCols[ColIdx].isActive())
117*82d56013Sjoerg return ColIdx;
118*82d56013Sjoerg
119*82d56013Sjoerg size_t OldSize = ActiveCols.size();
120*82d56013Sjoerg ActiveCols.grow(std::max<size_t>(OldSize * 2, 1));
121*82d56013Sjoerg return OldSize;
122*82d56013Sjoerg }
123*82d56013Sjoerg
dump() const124*82d56013Sjoerg void LiveVariablePrinter::dump() const {
125*82d56013Sjoerg for (const LiveVariable &LV : LiveVariables) {
126*82d56013Sjoerg dbgs() << LV.VarName << " @ " << LV.LocExpr.Range << ": ";
127*82d56013Sjoerg LV.print(dbgs(), MRI);
128*82d56013Sjoerg dbgs() << "\n";
129*82d56013Sjoerg }
130*82d56013Sjoerg }
131*82d56013Sjoerg
addCompileUnit(DWARFDie D)132*82d56013Sjoerg void LiveVariablePrinter::addCompileUnit(DWARFDie D) {
133*82d56013Sjoerg if (D.getTag() == dwarf::DW_TAG_subprogram)
134*82d56013Sjoerg addFunction(D);
135*82d56013Sjoerg else
136*82d56013Sjoerg for (const DWARFDie &Child : D.children())
137*82d56013Sjoerg addFunction(Child);
138*82d56013Sjoerg }
139*82d56013Sjoerg
140*82d56013Sjoerg /// Update to match the state of the instruction between ThisAddr and
141*82d56013Sjoerg /// NextAddr. In the common case, any live range active at ThisAddr is
142*82d56013Sjoerg /// live-in to the instruction, and any live range active at NextAddr is
143*82d56013Sjoerg /// live-out of the instruction. If IncludeDefinedVars is false, then live
144*82d56013Sjoerg /// ranges starting at NextAddr will be ignored.
update(object::SectionedAddress ThisAddr,object::SectionedAddress NextAddr,bool IncludeDefinedVars)145*82d56013Sjoerg void LiveVariablePrinter::update(object::SectionedAddress ThisAddr,
146*82d56013Sjoerg object::SectionedAddress NextAddr,
147*82d56013Sjoerg bool IncludeDefinedVars) {
148*82d56013Sjoerg // First, check variables which have already been assigned a column, so
149*82d56013Sjoerg // that we don't change their order.
150*82d56013Sjoerg SmallSet<unsigned, 8> CheckedVarIdxs;
151*82d56013Sjoerg for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
152*82d56013Sjoerg if (!ActiveCols[ColIdx].isActive())
153*82d56013Sjoerg continue;
154*82d56013Sjoerg CheckedVarIdxs.insert(ActiveCols[ColIdx].VarIdx);
155*82d56013Sjoerg LiveVariable &LV = LiveVariables[ActiveCols[ColIdx].VarIdx];
156*82d56013Sjoerg ActiveCols[ColIdx].LiveIn = LV.liveAtAddress(ThisAddr);
157*82d56013Sjoerg ActiveCols[ColIdx].LiveOut = LV.liveAtAddress(NextAddr);
158*82d56013Sjoerg LLVM_DEBUG(dbgs() << "pass 1, " << ThisAddr.Address << "-"
159*82d56013Sjoerg << NextAddr.Address << ", " << LV.VarName << ", Col "
160*82d56013Sjoerg << ColIdx << ": LiveIn=" << ActiveCols[ColIdx].LiveIn
161*82d56013Sjoerg << ", LiveOut=" << ActiveCols[ColIdx].LiveOut << "\n");
162*82d56013Sjoerg
163*82d56013Sjoerg if (!ActiveCols[ColIdx].LiveIn && !ActiveCols[ColIdx].LiveOut)
164*82d56013Sjoerg ActiveCols[ColIdx].VarIdx = Column::NullVarIdx;
165*82d56013Sjoerg }
166*82d56013Sjoerg
167*82d56013Sjoerg // Next, look for variables which don't already have a column, but which
168*82d56013Sjoerg // are now live.
169*82d56013Sjoerg if (IncludeDefinedVars) {
170*82d56013Sjoerg for (unsigned VarIdx = 0, End = LiveVariables.size(); VarIdx < End;
171*82d56013Sjoerg ++VarIdx) {
172*82d56013Sjoerg if (CheckedVarIdxs.count(VarIdx))
173*82d56013Sjoerg continue;
174*82d56013Sjoerg LiveVariable &LV = LiveVariables[VarIdx];
175*82d56013Sjoerg bool LiveIn = LV.liveAtAddress(ThisAddr);
176*82d56013Sjoerg bool LiveOut = LV.liveAtAddress(NextAddr);
177*82d56013Sjoerg if (!LiveIn && !LiveOut)
178*82d56013Sjoerg continue;
179*82d56013Sjoerg
180*82d56013Sjoerg unsigned ColIdx = findFreeColumn();
181*82d56013Sjoerg LLVM_DEBUG(dbgs() << "pass 2, " << ThisAddr.Address << "-"
182*82d56013Sjoerg << NextAddr.Address << ", " << LV.VarName << ", Col "
183*82d56013Sjoerg << ColIdx << ": LiveIn=" << LiveIn
184*82d56013Sjoerg << ", LiveOut=" << LiveOut << "\n");
185*82d56013Sjoerg ActiveCols[ColIdx].VarIdx = VarIdx;
186*82d56013Sjoerg ActiveCols[ColIdx].LiveIn = LiveIn;
187*82d56013Sjoerg ActiveCols[ColIdx].LiveOut = LiveOut;
188*82d56013Sjoerg ActiveCols[ColIdx].MustDrawLabel = true;
189*82d56013Sjoerg }
190*82d56013Sjoerg }
191*82d56013Sjoerg }
192*82d56013Sjoerg
193*82d56013Sjoerg enum class LineChar {
194*82d56013Sjoerg RangeStart,
195*82d56013Sjoerg RangeMid,
196*82d56013Sjoerg RangeEnd,
197*82d56013Sjoerg LabelVert,
198*82d56013Sjoerg LabelCornerNew,
199*82d56013Sjoerg LabelCornerActive,
200*82d56013Sjoerg LabelHoriz,
201*82d56013Sjoerg };
getLineChar(LineChar C) const202*82d56013Sjoerg const char *LiveVariablePrinter::getLineChar(LineChar C) const {
203*82d56013Sjoerg bool IsASCII = DbgVariables == DVASCII;
204*82d56013Sjoerg switch (C) {
205*82d56013Sjoerg case LineChar::RangeStart:
206*82d56013Sjoerg return IsASCII ? "^" : (const char *)u8"\u2548";
207*82d56013Sjoerg case LineChar::RangeMid:
208*82d56013Sjoerg return IsASCII ? "|" : (const char *)u8"\u2503";
209*82d56013Sjoerg case LineChar::RangeEnd:
210*82d56013Sjoerg return IsASCII ? "v" : (const char *)u8"\u253b";
211*82d56013Sjoerg case LineChar::LabelVert:
212*82d56013Sjoerg return IsASCII ? "|" : (const char *)u8"\u2502";
213*82d56013Sjoerg case LineChar::LabelCornerNew:
214*82d56013Sjoerg return IsASCII ? "/" : (const char *)u8"\u250c";
215*82d56013Sjoerg case LineChar::LabelCornerActive:
216*82d56013Sjoerg return IsASCII ? "|" : (const char *)u8"\u2520";
217*82d56013Sjoerg case LineChar::LabelHoriz:
218*82d56013Sjoerg return IsASCII ? "-" : (const char *)u8"\u2500";
219*82d56013Sjoerg }
220*82d56013Sjoerg llvm_unreachable("Unhandled LineChar enum");
221*82d56013Sjoerg }
222*82d56013Sjoerg
223*82d56013Sjoerg /// Print live ranges to the right of an existing line. This assumes the
224*82d56013Sjoerg /// line is not an instruction, so doesn't start or end any live ranges, so
225*82d56013Sjoerg /// we only need to print active ranges or empty columns. If AfterInst is
226*82d56013Sjoerg /// true, this is being printed after the last instruction fed to update(),
227*82d56013Sjoerg /// otherwise this is being printed before it.
printAfterOtherLine(formatted_raw_ostream & OS,bool AfterInst)228*82d56013Sjoerg void LiveVariablePrinter::printAfterOtherLine(formatted_raw_ostream &OS,
229*82d56013Sjoerg bool AfterInst) {
230*82d56013Sjoerg if (ActiveCols.size()) {
231*82d56013Sjoerg unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
232*82d56013Sjoerg for (size_t ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
233*82d56013Sjoerg ColIdx < End; ++ColIdx) {
234*82d56013Sjoerg if (ActiveCols[ColIdx].isActive()) {
235*82d56013Sjoerg if ((AfterInst && ActiveCols[ColIdx].LiveOut) ||
236*82d56013Sjoerg (!AfterInst && ActiveCols[ColIdx].LiveIn))
237*82d56013Sjoerg OS << getLineChar(LineChar::RangeMid);
238*82d56013Sjoerg else if (!AfterInst && ActiveCols[ColIdx].LiveOut)
239*82d56013Sjoerg OS << getLineChar(LineChar::LabelVert);
240*82d56013Sjoerg else
241*82d56013Sjoerg OS << " ";
242*82d56013Sjoerg }
243*82d56013Sjoerg OS << " ";
244*82d56013Sjoerg }
245*82d56013Sjoerg }
246*82d56013Sjoerg OS << "\n";
247*82d56013Sjoerg }
248*82d56013Sjoerg
249*82d56013Sjoerg /// Print any live variable range info needed to the right of a
250*82d56013Sjoerg /// non-instruction line of disassembly. This is where we print the variable
251*82d56013Sjoerg /// names and expressions, with thin line-drawing characters connecting them
252*82d56013Sjoerg /// to the live range which starts at the next instruction. If MustPrint is
253*82d56013Sjoerg /// true, we have to print at least one line (with the continuation of any
254*82d56013Sjoerg /// already-active live ranges) because something has already been printed
255*82d56013Sjoerg /// earlier on this line.
printBetweenInsts(formatted_raw_ostream & OS,bool MustPrint)256*82d56013Sjoerg void LiveVariablePrinter::printBetweenInsts(formatted_raw_ostream &OS,
257*82d56013Sjoerg bool MustPrint) {
258*82d56013Sjoerg bool PrintedSomething = false;
259*82d56013Sjoerg for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
260*82d56013Sjoerg if (ActiveCols[ColIdx].isActive() && ActiveCols[ColIdx].MustDrawLabel) {
261*82d56013Sjoerg // First we need to print the live range markers for any active
262*82d56013Sjoerg // columns to the left of this one.
263*82d56013Sjoerg OS.PadToColumn(getIndentLevel());
264*82d56013Sjoerg for (unsigned ColIdx2 = 0; ColIdx2 < ColIdx; ++ColIdx2) {
265*82d56013Sjoerg if (ActiveCols[ColIdx2].isActive()) {
266*82d56013Sjoerg if (ActiveCols[ColIdx2].MustDrawLabel && !ActiveCols[ColIdx2].LiveIn)
267*82d56013Sjoerg OS << getLineChar(LineChar::LabelVert) << " ";
268*82d56013Sjoerg else
269*82d56013Sjoerg OS << getLineChar(LineChar::RangeMid) << " ";
270*82d56013Sjoerg } else
271*82d56013Sjoerg OS << " ";
272*82d56013Sjoerg }
273*82d56013Sjoerg
274*82d56013Sjoerg // Then print the variable name and location of the new live range,
275*82d56013Sjoerg // with box drawing characters joining it to the live range line.
276*82d56013Sjoerg OS << getLineChar(ActiveCols[ColIdx].LiveIn ? LineChar::LabelCornerActive
277*82d56013Sjoerg : LineChar::LabelCornerNew)
278*82d56013Sjoerg << getLineChar(LineChar::LabelHoriz) << " ";
279*82d56013Sjoerg WithColor(OS, raw_ostream::GREEN)
280*82d56013Sjoerg << LiveVariables[ActiveCols[ColIdx].VarIdx].VarName;
281*82d56013Sjoerg OS << " = ";
282*82d56013Sjoerg {
283*82d56013Sjoerg WithColor ExprColor(OS, raw_ostream::CYAN);
284*82d56013Sjoerg LiveVariables[ActiveCols[ColIdx].VarIdx].print(OS, MRI);
285*82d56013Sjoerg }
286*82d56013Sjoerg
287*82d56013Sjoerg // If there are any columns to the right of the expression we just
288*82d56013Sjoerg // printed, then continue their live range lines.
289*82d56013Sjoerg unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
290*82d56013Sjoerg for (unsigned ColIdx2 = FirstUnprintedColumn, End = ActiveCols.size();
291*82d56013Sjoerg ColIdx2 < End; ++ColIdx2) {
292*82d56013Sjoerg if (ActiveCols[ColIdx2].isActive() && ActiveCols[ColIdx2].LiveIn)
293*82d56013Sjoerg OS << getLineChar(LineChar::RangeMid) << " ";
294*82d56013Sjoerg else
295*82d56013Sjoerg OS << " ";
296*82d56013Sjoerg }
297*82d56013Sjoerg
298*82d56013Sjoerg OS << "\n";
299*82d56013Sjoerg PrintedSomething = true;
300*82d56013Sjoerg }
301*82d56013Sjoerg }
302*82d56013Sjoerg
303*82d56013Sjoerg for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx)
304*82d56013Sjoerg if (ActiveCols[ColIdx].isActive())
305*82d56013Sjoerg ActiveCols[ColIdx].MustDrawLabel = false;
306*82d56013Sjoerg
307*82d56013Sjoerg // If we must print something (because we printed a line/column number),
308*82d56013Sjoerg // but don't have any new variables to print, then print a line which
309*82d56013Sjoerg // just continues any existing live ranges.
310*82d56013Sjoerg if (MustPrint && !PrintedSomething)
311*82d56013Sjoerg printAfterOtherLine(OS, false);
312*82d56013Sjoerg }
313*82d56013Sjoerg
314*82d56013Sjoerg /// Print the live variable ranges to the right of a disassembled instruction.
printAfterInst(formatted_raw_ostream & OS)315*82d56013Sjoerg void LiveVariablePrinter::printAfterInst(formatted_raw_ostream &OS) {
316*82d56013Sjoerg if (!ActiveCols.size())
317*82d56013Sjoerg return;
318*82d56013Sjoerg unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
319*82d56013Sjoerg for (unsigned ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
320*82d56013Sjoerg ColIdx < End; ++ColIdx) {
321*82d56013Sjoerg if (!ActiveCols[ColIdx].isActive())
322*82d56013Sjoerg OS << " ";
323*82d56013Sjoerg else if (ActiveCols[ColIdx].LiveIn && ActiveCols[ColIdx].LiveOut)
324*82d56013Sjoerg OS << getLineChar(LineChar::RangeMid) << " ";
325*82d56013Sjoerg else if (ActiveCols[ColIdx].LiveOut)
326*82d56013Sjoerg OS << getLineChar(LineChar::RangeStart) << " ";
327*82d56013Sjoerg else if (ActiveCols[ColIdx].LiveIn)
328*82d56013Sjoerg OS << getLineChar(LineChar::RangeEnd) << " ";
329*82d56013Sjoerg else
330*82d56013Sjoerg llvm_unreachable("var must be live in or out!");
331*82d56013Sjoerg }
332*82d56013Sjoerg }
333*82d56013Sjoerg
cacheSource(const DILineInfo & LineInfo)334*82d56013Sjoerg bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
335*82d56013Sjoerg std::unique_ptr<MemoryBuffer> Buffer;
336*82d56013Sjoerg if (LineInfo.Source) {
337*82d56013Sjoerg Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);
338*82d56013Sjoerg } else {
339*82d56013Sjoerg auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);
340*82d56013Sjoerg if (!BufferOrError) {
341*82d56013Sjoerg if (MissingSources.insert(LineInfo.FileName).second)
342*82d56013Sjoerg reportWarning("failed to find source " + LineInfo.FileName,
343*82d56013Sjoerg Obj->getFileName());
344*82d56013Sjoerg return false;
345*82d56013Sjoerg }
346*82d56013Sjoerg Buffer = std::move(*BufferOrError);
347*82d56013Sjoerg }
348*82d56013Sjoerg // Chomp the file to get lines
349*82d56013Sjoerg const char *BufferStart = Buffer->getBufferStart(),
350*82d56013Sjoerg *BufferEnd = Buffer->getBufferEnd();
351*82d56013Sjoerg std::vector<StringRef> &Lines = LineCache[LineInfo.FileName];
352*82d56013Sjoerg const char *Start = BufferStart;
353*82d56013Sjoerg for (const char *I = BufferStart; I != BufferEnd; ++I)
354*82d56013Sjoerg if (*I == '\n') {
355*82d56013Sjoerg Lines.emplace_back(Start, I - Start - (BufferStart < I && I[-1] == '\r'));
356*82d56013Sjoerg Start = I + 1;
357*82d56013Sjoerg }
358*82d56013Sjoerg if (Start < BufferEnd)
359*82d56013Sjoerg Lines.emplace_back(Start, BufferEnd - Start);
360*82d56013Sjoerg SourceCache[LineInfo.FileName] = std::move(Buffer);
361*82d56013Sjoerg return true;
362*82d56013Sjoerg }
363*82d56013Sjoerg
printSourceLine(formatted_raw_ostream & OS,object::SectionedAddress Address,StringRef ObjectFilename,LiveVariablePrinter & LVP,StringRef Delimiter)364*82d56013Sjoerg void SourcePrinter::printSourceLine(formatted_raw_ostream &OS,
365*82d56013Sjoerg object::SectionedAddress Address,
366*82d56013Sjoerg StringRef ObjectFilename,
367*82d56013Sjoerg LiveVariablePrinter &LVP,
368*82d56013Sjoerg StringRef Delimiter) {
369*82d56013Sjoerg if (!Symbolizer)
370*82d56013Sjoerg return;
371*82d56013Sjoerg
372*82d56013Sjoerg DILineInfo LineInfo = DILineInfo();
373*82d56013Sjoerg Expected<DILineInfo> ExpectedLineInfo =
374*82d56013Sjoerg Symbolizer->symbolizeCode(*Obj, Address);
375*82d56013Sjoerg std::string ErrorMessage;
376*82d56013Sjoerg if (ExpectedLineInfo) {
377*82d56013Sjoerg LineInfo = *ExpectedLineInfo;
378*82d56013Sjoerg } else if (!WarnedInvalidDebugInfo) {
379*82d56013Sjoerg WarnedInvalidDebugInfo = true;
380*82d56013Sjoerg // TODO Untested.
381*82d56013Sjoerg reportWarning("failed to parse debug information: " +
382*82d56013Sjoerg toString(ExpectedLineInfo.takeError()),
383*82d56013Sjoerg ObjectFilename);
384*82d56013Sjoerg }
385*82d56013Sjoerg
386*82d56013Sjoerg if (!objdump::Prefix.empty() &&
387*82d56013Sjoerg sys::path::is_absolute_gnu(LineInfo.FileName)) {
388*82d56013Sjoerg // FileName has at least one character since is_absolute_gnu is false for
389*82d56013Sjoerg // an empty string.
390*82d56013Sjoerg assert(!LineInfo.FileName.empty());
391*82d56013Sjoerg if (PrefixStrip > 0) {
392*82d56013Sjoerg uint32_t Level = 0;
393*82d56013Sjoerg auto StrippedNameStart = LineInfo.FileName.begin();
394*82d56013Sjoerg
395*82d56013Sjoerg // Path.h iterator skips extra separators. Therefore it cannot be used
396*82d56013Sjoerg // here to keep compatibility with GNU Objdump.
397*82d56013Sjoerg for (auto Pos = StrippedNameStart + 1, End = LineInfo.FileName.end();
398*82d56013Sjoerg Pos != End && Level < PrefixStrip; ++Pos) {
399*82d56013Sjoerg if (sys::path::is_separator(*Pos)) {
400*82d56013Sjoerg StrippedNameStart = Pos;
401*82d56013Sjoerg ++Level;
402*82d56013Sjoerg }
403*82d56013Sjoerg }
404*82d56013Sjoerg
405*82d56013Sjoerg LineInfo.FileName =
406*82d56013Sjoerg std::string(StrippedNameStart, LineInfo.FileName.end());
407*82d56013Sjoerg }
408*82d56013Sjoerg
409*82d56013Sjoerg SmallString<128> FilePath;
410*82d56013Sjoerg sys::path::append(FilePath, Prefix, LineInfo.FileName);
411*82d56013Sjoerg
412*82d56013Sjoerg LineInfo.FileName = std::string(FilePath);
413*82d56013Sjoerg }
414*82d56013Sjoerg
415*82d56013Sjoerg if (PrintLines)
416*82d56013Sjoerg printLines(OS, LineInfo, Delimiter, LVP);
417*82d56013Sjoerg if (PrintSource)
418*82d56013Sjoerg printSources(OS, LineInfo, ObjectFilename, Delimiter, LVP);
419*82d56013Sjoerg OldLineInfo = LineInfo;
420*82d56013Sjoerg }
421*82d56013Sjoerg
printLines(formatted_raw_ostream & OS,const DILineInfo & LineInfo,StringRef Delimiter,LiveVariablePrinter & LVP)422*82d56013Sjoerg void SourcePrinter::printLines(formatted_raw_ostream &OS,
423*82d56013Sjoerg const DILineInfo &LineInfo, StringRef Delimiter,
424*82d56013Sjoerg LiveVariablePrinter &LVP) {
425*82d56013Sjoerg bool PrintFunctionName = LineInfo.FunctionName != DILineInfo::BadString &&
426*82d56013Sjoerg LineInfo.FunctionName != OldLineInfo.FunctionName;
427*82d56013Sjoerg if (PrintFunctionName) {
428*82d56013Sjoerg OS << Delimiter << LineInfo.FunctionName;
429*82d56013Sjoerg // If demangling is successful, FunctionName will end with "()". Print it
430*82d56013Sjoerg // only if demangling did not run or was unsuccessful.
431*82d56013Sjoerg if (!StringRef(LineInfo.FunctionName).endswith("()"))
432*82d56013Sjoerg OS << "()";
433*82d56013Sjoerg OS << ":\n";
434*82d56013Sjoerg }
435*82d56013Sjoerg if (LineInfo.FileName != DILineInfo::BadString && LineInfo.Line != 0 &&
436*82d56013Sjoerg (OldLineInfo.Line != LineInfo.Line ||
437*82d56013Sjoerg OldLineInfo.FileName != LineInfo.FileName || PrintFunctionName)) {
438*82d56013Sjoerg OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line;
439*82d56013Sjoerg LVP.printBetweenInsts(OS, true);
440*82d56013Sjoerg }
441*82d56013Sjoerg }
442*82d56013Sjoerg
printSources(formatted_raw_ostream & OS,const DILineInfo & LineInfo,StringRef ObjectFilename,StringRef Delimiter,LiveVariablePrinter & LVP)443*82d56013Sjoerg void SourcePrinter::printSources(formatted_raw_ostream &OS,
444*82d56013Sjoerg const DILineInfo &LineInfo,
445*82d56013Sjoerg StringRef ObjectFilename, StringRef Delimiter,
446*82d56013Sjoerg LiveVariablePrinter &LVP) {
447*82d56013Sjoerg if (LineInfo.FileName == DILineInfo::BadString || LineInfo.Line == 0 ||
448*82d56013Sjoerg (OldLineInfo.Line == LineInfo.Line &&
449*82d56013Sjoerg OldLineInfo.FileName == LineInfo.FileName))
450*82d56013Sjoerg return;
451*82d56013Sjoerg
452*82d56013Sjoerg if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
453*82d56013Sjoerg if (!cacheSource(LineInfo))
454*82d56013Sjoerg return;
455*82d56013Sjoerg auto LineBuffer = LineCache.find(LineInfo.FileName);
456*82d56013Sjoerg if (LineBuffer != LineCache.end()) {
457*82d56013Sjoerg if (LineInfo.Line > LineBuffer->second.size()) {
458*82d56013Sjoerg reportWarning(
459*82d56013Sjoerg formatv(
460*82d56013Sjoerg "debug info line number {0} exceeds the number of lines in {1}",
461*82d56013Sjoerg LineInfo.Line, LineInfo.FileName),
462*82d56013Sjoerg ObjectFilename);
463*82d56013Sjoerg return;
464*82d56013Sjoerg }
465*82d56013Sjoerg // Vector begins at 0, line numbers are non-zero
466*82d56013Sjoerg OS << Delimiter << LineBuffer->second[LineInfo.Line - 1];
467*82d56013Sjoerg LVP.printBetweenInsts(OS, true);
468*82d56013Sjoerg }
469*82d56013Sjoerg }
470*82d56013Sjoerg
SourcePrinter(const object::ObjectFile * Obj,StringRef DefaultArch)471*82d56013Sjoerg SourcePrinter::SourcePrinter(const object::ObjectFile *Obj,
472*82d56013Sjoerg StringRef DefaultArch)
473*82d56013Sjoerg : Obj(Obj) {
474*82d56013Sjoerg symbolize::LLVMSymbolizer::Options SymbolizerOpts;
475*82d56013Sjoerg SymbolizerOpts.PrintFunctions =
476*82d56013Sjoerg DILineInfoSpecifier::FunctionNameKind::LinkageName;
477*82d56013Sjoerg SymbolizerOpts.Demangle = Demangle;
478*82d56013Sjoerg SymbolizerOpts.DefaultArch = std::string(DefaultArch);
479*82d56013Sjoerg Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
480*82d56013Sjoerg }
481*82d56013Sjoerg
482*82d56013Sjoerg } // namespace objdump
483*82d56013Sjoerg } // namespace llvm
484