Path: blob/main/contrib/llvm-project/llvm/tools/llvm-objdump/SourcePrinter.cpp
35231 views
//===-- SourcePrinter.cpp - source interleaving utilities ----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the LiveVariablePrinter and SourcePrinter classes to9// keep track of DWARF info as the current address is updated, and print out the10// source file line and variable liveness as needed.11//12//===----------------------------------------------------------------------===//1314#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"2223#define DEBUG_TYPE "objdump"2425namespace llvm {26namespace objdump {2728bool LiveVariable::liveAtAddress(object::SectionedAddress Addr) {29if (LocExpr.Range == std::nullopt)30return false;31return LocExpr.Range->SectionIndex == Addr.SectionIndex &&32LocExpr.Range->LowPC <= Addr.Address &&33LocExpr.Range->HighPC > Addr.Address;34}3536void LiveVariable::print(raw_ostream &OS, const MCRegisterInfo &MRI) const {37DataExtractor Data({LocExpr.Expr.data(), LocExpr.Expr.size()},38Unit->getContext().isLittleEndian(), 0);39DWARFExpression Expression(Data, Unit->getAddressByteSize());4041auto GetRegName = [&MRI, &OS](uint64_t DwarfRegNum, bool IsEH) -> StringRef {42if (std::optional<unsigned> LLVMRegNum =43MRI.getLLVMRegNum(DwarfRegNum, IsEH))44if (const char *RegName = MRI.getName(*LLVMRegNum))45return StringRef(RegName);46OS << "<unknown register " << DwarfRegNum << ">";47return {};48};4950Expression.printCompact(OS, GetRegName);51}5253void LiveVariablePrinter::addVariable(DWARFDie FuncDie, DWARFDie VarDie) {54uint64_t FuncLowPC, FuncHighPC, SectionIndex;55FuncDie.getLowAndHighPC(FuncLowPC, FuncHighPC, SectionIndex);56const char *VarName = VarDie.getName(DINameKind::ShortName);57DWARFUnit *U = VarDie.getDwarfUnit();5859Expected<DWARFLocationExpressionsVector> Locs =60VarDie.getLocations(dwarf::DW_AT_location);61if (!Locs) {62// If the variable doesn't have any locations, just ignore it. We don't63// report an error or warning here as that could be noisy on optimised64// code.65consumeError(Locs.takeError());66return;67}6869for (const DWARFLocationExpression &LocExpr : *Locs) {70if (LocExpr.Range) {71LiveVariables.emplace_back(LocExpr, VarName, U, FuncDie);72} else {73// If the LocExpr does not have an associated range, it is valid for74// the whole of the function.75// TODO: technically it is not valid for any range covered by another76// LocExpr, does that happen in reality?77DWARFLocationExpression WholeFuncExpr{78DWARFAddressRange(FuncLowPC, FuncHighPC, SectionIndex), LocExpr.Expr};79LiveVariables.emplace_back(WholeFuncExpr, VarName, U, FuncDie);80}81}82}8384void LiveVariablePrinter::addFunction(DWARFDie D) {85for (const DWARFDie &Child : D.children()) {86if (Child.getTag() == dwarf::DW_TAG_variable ||87Child.getTag() == dwarf::DW_TAG_formal_parameter)88addVariable(D, Child);89else90addFunction(Child);91}92}9394// Get the column number (in characters) at which the first live variable95// line should be printed.96unsigned LiveVariablePrinter::getIndentLevel() const {97return DbgIndent + getInstStartColumn(STI);98}99100// Indent to the first live-range column to the right of the currently101// printed line, and return the index of that column.102// TODO: formatted_raw_ostream uses "column" to mean a number of characters103// since the last \n, and we use it to mean the number of slots in which we104// put live variable lines. Pick a less overloaded word.105unsigned LiveVariablePrinter::moveToFirstVarColumn(formatted_raw_ostream &OS) {106// Logical column number: column zero is the first column we print in, each107// logical column is 2 physical columns wide.108unsigned FirstUnprintedLogicalColumn =109std::max((int)(OS.getColumn() - getIndentLevel() + 1) / 2, 0);110// Physical column number: the actual column number in characters, with111// zero being the left-most side of the screen.112unsigned FirstUnprintedPhysicalColumn =113getIndentLevel() + FirstUnprintedLogicalColumn * 2;114115if (FirstUnprintedPhysicalColumn > OS.getColumn())116OS.PadToColumn(FirstUnprintedPhysicalColumn);117118return FirstUnprintedLogicalColumn;119}120121unsigned LiveVariablePrinter::findFreeColumn() {122for (unsigned ColIdx = 0; ColIdx < ActiveCols.size(); ++ColIdx)123if (!ActiveCols[ColIdx].isActive())124return ColIdx;125126size_t OldSize = ActiveCols.size();127ActiveCols.grow(std::max<size_t>(OldSize * 2, 1));128return OldSize;129}130131void LiveVariablePrinter::dump() const {132for (const LiveVariable &LV : LiveVariables) {133dbgs() << LV.VarName << " @ " << LV.LocExpr.Range << ": ";134LV.print(dbgs(), MRI);135dbgs() << "\n";136}137}138139void LiveVariablePrinter::addCompileUnit(DWARFDie D) {140if (D.getTag() == dwarf::DW_TAG_subprogram)141addFunction(D);142else143for (const DWARFDie &Child : D.children())144addFunction(Child);145}146147/// Update to match the state of the instruction between ThisAddr and148/// NextAddr. In the common case, any live range active at ThisAddr is149/// live-in to the instruction, and any live range active at NextAddr is150/// live-out of the instruction. If IncludeDefinedVars is false, then live151/// ranges starting at NextAddr will be ignored.152void LiveVariablePrinter::update(object::SectionedAddress ThisAddr,153object::SectionedAddress NextAddr,154bool IncludeDefinedVars) {155// First, check variables which have already been assigned a column, so156// that we don't change their order.157SmallSet<unsigned, 8> CheckedVarIdxs;158for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {159if (!ActiveCols[ColIdx].isActive())160continue;161CheckedVarIdxs.insert(ActiveCols[ColIdx].VarIdx);162LiveVariable &LV = LiveVariables[ActiveCols[ColIdx].VarIdx];163ActiveCols[ColIdx].LiveIn = LV.liveAtAddress(ThisAddr);164ActiveCols[ColIdx].LiveOut = LV.liveAtAddress(NextAddr);165LLVM_DEBUG(dbgs() << "pass 1, " << ThisAddr.Address << "-"166<< NextAddr.Address << ", " << LV.VarName << ", Col "167<< ColIdx << ": LiveIn=" << ActiveCols[ColIdx].LiveIn168<< ", LiveOut=" << ActiveCols[ColIdx].LiveOut << "\n");169170if (!ActiveCols[ColIdx].LiveIn && !ActiveCols[ColIdx].LiveOut)171ActiveCols[ColIdx].VarIdx = Column::NullVarIdx;172}173174// Next, look for variables which don't already have a column, but which175// are now live.176if (IncludeDefinedVars) {177for (unsigned VarIdx = 0, End = LiveVariables.size(); VarIdx < End;178++VarIdx) {179if (CheckedVarIdxs.count(VarIdx))180continue;181LiveVariable &LV = LiveVariables[VarIdx];182bool LiveIn = LV.liveAtAddress(ThisAddr);183bool LiveOut = LV.liveAtAddress(NextAddr);184if (!LiveIn && !LiveOut)185continue;186187unsigned ColIdx = findFreeColumn();188LLVM_DEBUG(dbgs() << "pass 2, " << ThisAddr.Address << "-"189<< NextAddr.Address << ", " << LV.VarName << ", Col "190<< ColIdx << ": LiveIn=" << LiveIn191<< ", LiveOut=" << LiveOut << "\n");192ActiveCols[ColIdx].VarIdx = VarIdx;193ActiveCols[ColIdx].LiveIn = LiveIn;194ActiveCols[ColIdx].LiveOut = LiveOut;195ActiveCols[ColIdx].MustDrawLabel = true;196}197}198}199200enum class LineChar {201RangeStart,202RangeMid,203RangeEnd,204LabelVert,205LabelCornerNew,206LabelCornerActive,207LabelHoriz,208};209const char *LiveVariablePrinter::getLineChar(LineChar C) const {210bool IsASCII = DbgVariables == DVASCII;211switch (C) {212case LineChar::RangeStart:213return IsASCII ? "^" : (const char *)u8"\u2548";214case LineChar::RangeMid:215return IsASCII ? "|" : (const char *)u8"\u2503";216case LineChar::RangeEnd:217return IsASCII ? "v" : (const char *)u8"\u253b";218case LineChar::LabelVert:219return IsASCII ? "|" : (const char *)u8"\u2502";220case LineChar::LabelCornerNew:221return IsASCII ? "/" : (const char *)u8"\u250c";222case LineChar::LabelCornerActive:223return IsASCII ? "|" : (const char *)u8"\u2520";224case LineChar::LabelHoriz:225return IsASCII ? "-" : (const char *)u8"\u2500";226}227llvm_unreachable("Unhandled LineChar enum");228}229230/// Print live ranges to the right of an existing line. This assumes the231/// line is not an instruction, so doesn't start or end any live ranges, so232/// we only need to print active ranges or empty columns. If AfterInst is233/// true, this is being printed after the last instruction fed to update(),234/// otherwise this is being printed before it.235void LiveVariablePrinter::printAfterOtherLine(formatted_raw_ostream &OS,236bool AfterInst) {237if (ActiveCols.size()) {238unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);239for (size_t ColIdx = FirstUnprintedColumn, End = ActiveCols.size();240ColIdx < End; ++ColIdx) {241if (ActiveCols[ColIdx].isActive()) {242if ((AfterInst && ActiveCols[ColIdx].LiveOut) ||243(!AfterInst && ActiveCols[ColIdx].LiveIn))244OS << getLineChar(LineChar::RangeMid);245else if (!AfterInst && ActiveCols[ColIdx].LiveOut)246OS << getLineChar(LineChar::LabelVert);247else248OS << " ";249}250OS << " ";251}252}253OS << "\n";254}255256/// Print any live variable range info needed to the right of a257/// non-instruction line of disassembly. This is where we print the variable258/// names and expressions, with thin line-drawing characters connecting them259/// to the live range which starts at the next instruction. If MustPrint is260/// true, we have to print at least one line (with the continuation of any261/// already-active live ranges) because something has already been printed262/// earlier on this line.263void LiveVariablePrinter::printBetweenInsts(formatted_raw_ostream &OS,264bool MustPrint) {265bool PrintedSomething = false;266for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {267if (ActiveCols[ColIdx].isActive() && ActiveCols[ColIdx].MustDrawLabel) {268// First we need to print the live range markers for any active269// columns to the left of this one.270OS.PadToColumn(getIndentLevel());271for (unsigned ColIdx2 = 0; ColIdx2 < ColIdx; ++ColIdx2) {272if (ActiveCols[ColIdx2].isActive()) {273if (ActiveCols[ColIdx2].MustDrawLabel && !ActiveCols[ColIdx2].LiveIn)274OS << getLineChar(LineChar::LabelVert) << " ";275else276OS << getLineChar(LineChar::RangeMid) << " ";277} else278OS << " ";279}280281// Then print the variable name and location of the new live range,282// with box drawing characters joining it to the live range line.283OS << getLineChar(ActiveCols[ColIdx].LiveIn ? LineChar::LabelCornerActive284: LineChar::LabelCornerNew)285<< getLineChar(LineChar::LabelHoriz) << " ";286WithColor(OS, raw_ostream::GREEN)287<< LiveVariables[ActiveCols[ColIdx].VarIdx].VarName;288OS << " = ";289{290WithColor ExprColor(OS, raw_ostream::CYAN);291LiveVariables[ActiveCols[ColIdx].VarIdx].print(OS, MRI);292}293294// If there are any columns to the right of the expression we just295// printed, then continue their live range lines.296unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);297for (unsigned ColIdx2 = FirstUnprintedColumn, End = ActiveCols.size();298ColIdx2 < End; ++ColIdx2) {299if (ActiveCols[ColIdx2].isActive() && ActiveCols[ColIdx2].LiveIn)300OS << getLineChar(LineChar::RangeMid) << " ";301else302OS << " ";303}304305OS << "\n";306PrintedSomething = true;307}308}309310for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx)311if (ActiveCols[ColIdx].isActive())312ActiveCols[ColIdx].MustDrawLabel = false;313314// If we must print something (because we printed a line/column number),315// but don't have any new variables to print, then print a line which316// just continues any existing live ranges.317if (MustPrint && !PrintedSomething)318printAfterOtherLine(OS, false);319}320321/// Print the live variable ranges to the right of a disassembled instruction.322void LiveVariablePrinter::printAfterInst(formatted_raw_ostream &OS) {323if (!ActiveCols.size())324return;325unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);326for (unsigned ColIdx = FirstUnprintedColumn, End = ActiveCols.size();327ColIdx < End; ++ColIdx) {328if (!ActiveCols[ColIdx].isActive())329OS << " ";330else if (ActiveCols[ColIdx].LiveIn && ActiveCols[ColIdx].LiveOut)331OS << getLineChar(LineChar::RangeMid) << " ";332else if (ActiveCols[ColIdx].LiveOut)333OS << getLineChar(LineChar::RangeStart) << " ";334else if (ActiveCols[ColIdx].LiveIn)335OS << getLineChar(LineChar::RangeEnd) << " ";336else337llvm_unreachable("var must be live in or out!");338}339}340341bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {342std::unique_ptr<MemoryBuffer> Buffer;343if (LineInfo.Source) {344Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);345} else {346auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);347if (!BufferOrError) {348if (MissingSources.insert(LineInfo.FileName).second)349reportWarning("failed to find source " + LineInfo.FileName,350Obj->getFileName());351return false;352}353Buffer = std::move(*BufferOrError);354}355// Chomp the file to get lines356const char *BufferStart = Buffer->getBufferStart(),357*BufferEnd = Buffer->getBufferEnd();358std::vector<StringRef> &Lines = LineCache[LineInfo.FileName];359const char *Start = BufferStart;360for (const char *I = BufferStart; I != BufferEnd; ++I)361if (*I == '\n') {362Lines.emplace_back(Start, I - Start - (BufferStart < I && I[-1] == '\r'));363Start = I + 1;364}365if (Start < BufferEnd)366Lines.emplace_back(Start, BufferEnd - Start);367SourceCache[LineInfo.FileName] = std::move(Buffer);368return true;369}370371void SourcePrinter::printSourceLine(formatted_raw_ostream &OS,372object::SectionedAddress Address,373StringRef ObjectFilename,374LiveVariablePrinter &LVP,375StringRef Delimiter) {376if (!Symbolizer)377return;378379DILineInfo LineInfo = DILineInfo();380Expected<DILineInfo> ExpectedLineInfo =381Symbolizer->symbolizeCode(*Obj, Address);382std::string ErrorMessage;383if (ExpectedLineInfo) {384LineInfo = *ExpectedLineInfo;385} else if (!WarnedInvalidDebugInfo) {386WarnedInvalidDebugInfo = true;387// TODO Untested.388reportWarning("failed to parse debug information: " +389toString(ExpectedLineInfo.takeError()),390ObjectFilename);391}392393if (!objdump::Prefix.empty() &&394sys::path::is_absolute_gnu(LineInfo.FileName)) {395// FileName has at least one character since is_absolute_gnu is false for396// an empty string.397assert(!LineInfo.FileName.empty());398if (PrefixStrip > 0) {399uint32_t Level = 0;400auto StrippedNameStart = LineInfo.FileName.begin();401402// Path.h iterator skips extra separators. Therefore it cannot be used403// here to keep compatibility with GNU Objdump.404for (auto Pos = StrippedNameStart + 1, End = LineInfo.FileName.end();405Pos != End && Level < PrefixStrip; ++Pos) {406if (sys::path::is_separator(*Pos)) {407StrippedNameStart = Pos;408++Level;409}410}411412LineInfo.FileName =413std::string(StrippedNameStart, LineInfo.FileName.end());414}415416SmallString<128> FilePath;417sys::path::append(FilePath, Prefix, LineInfo.FileName);418419LineInfo.FileName = std::string(FilePath);420}421422if (PrintLines)423printLines(OS, LineInfo, Delimiter, LVP);424if (PrintSource)425printSources(OS, LineInfo, ObjectFilename, Delimiter, LVP);426OldLineInfo = LineInfo;427}428429void SourcePrinter::printLines(formatted_raw_ostream &OS,430const DILineInfo &LineInfo, StringRef Delimiter,431LiveVariablePrinter &LVP) {432bool PrintFunctionName = LineInfo.FunctionName != DILineInfo::BadString &&433LineInfo.FunctionName != OldLineInfo.FunctionName;434if (PrintFunctionName) {435OS << Delimiter << LineInfo.FunctionName;436// If demangling is successful, FunctionName will end with "()". Print it437// only if demangling did not run or was unsuccessful.438if (!StringRef(LineInfo.FunctionName).ends_with("()"))439OS << "()";440OS << ":\n";441}442if (LineInfo.FileName != DILineInfo::BadString && LineInfo.Line != 0 &&443(OldLineInfo.Line != LineInfo.Line ||444OldLineInfo.FileName != LineInfo.FileName || PrintFunctionName)) {445OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line;446LVP.printBetweenInsts(OS, true);447}448}449450// Get the source line text for LineInfo:451// - use LineInfo::LineSource if available;452// - use LineCache if LineInfo::Source otherwise.453StringRef SourcePrinter::getLine(const DILineInfo &LineInfo,454StringRef ObjectFilename) {455if (LineInfo.LineSource)456return LineInfo.LineSource.value();457458if (SourceCache.find(LineInfo.FileName) == SourceCache.end())459if (!cacheSource(LineInfo))460return {};461462auto LineBuffer = LineCache.find(LineInfo.FileName);463if (LineBuffer == LineCache.end())464return {};465466if (LineInfo.Line > LineBuffer->second.size()) {467reportWarning(468formatv("debug info line number {0} exceeds the number of lines in {1}",469LineInfo.Line, LineInfo.FileName),470ObjectFilename);471return {};472}473474// Vector begins at 0, line numbers are non-zero475return LineBuffer->second[LineInfo.Line - 1];476}477478void SourcePrinter::printSources(formatted_raw_ostream &OS,479const DILineInfo &LineInfo,480StringRef ObjectFilename, StringRef Delimiter,481LiveVariablePrinter &LVP) {482if (LineInfo.FileName == DILineInfo::BadString || LineInfo.Line == 0 ||483(OldLineInfo.Line == LineInfo.Line &&484OldLineInfo.FileName == LineInfo.FileName))485return;486487StringRef Line = getLine(LineInfo, ObjectFilename);488if (!Line.empty()) {489OS << Delimiter << Line;490LVP.printBetweenInsts(OS, true);491}492}493494SourcePrinter::SourcePrinter(const object::ObjectFile *Obj,495StringRef DefaultArch)496: Obj(Obj) {497symbolize::LLVMSymbolizer::Options SymbolizerOpts;498SymbolizerOpts.PrintFunctions =499DILineInfoSpecifier::FunctionNameKind::LinkageName;500SymbolizerOpts.Demangle = Demangle;501SymbolizerOpts.DefaultArch = std::string(DefaultArch);502Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));503}504505} // namespace objdump506} // namespace llvm507508509