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