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