SourcePrinter.cpp 19 KB

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