SourcePrinter.cpp 18 KB

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