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