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