#include "SourcePrinter.h"
#include "llvm-objdump.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/FormatVariadic.h"
#define DEBUG_TYPE "objdump"
namespace llvm {
namespace objdump {
unsigned getInstStartColumn(const MCSubtargetInfo &STI) {
return !ShowRawInsn ? 16 : STI.getTargetTriple().isX86() ? 40 : 24;
}
bool LiveVariable::liveAtAddress(object::SectionedAddress Addr) {
if (LocExpr.Range == None)
return false;
return LocExpr.Range->SectionIndex == Addr.SectionIndex &&
LocExpr.Range->LowPC <= Addr.Address &&
LocExpr.Range->HighPC > Addr.Address;
}
void LiveVariable::print(raw_ostream &OS, const MCRegisterInfo &MRI) const {
DataExtractor Data({LocExpr.Expr.data(), LocExpr.Expr.size()},
Unit->getContext().isLittleEndian(), 0);
DWARFExpression Expression(Data, Unit->getAddressByteSize());
Expression.printCompact(OS, MRI);
}
void LiveVariablePrinter::addVariable(DWARFDie FuncDie, DWARFDie VarDie) {
uint64_t FuncLowPC, FuncHighPC, SectionIndex;
FuncDie.getLowAndHighPC(FuncLowPC, FuncHighPC, SectionIndex);
const char *VarName = VarDie.getName(DINameKind::ShortName);
DWARFUnit *U = VarDie.getDwarfUnit();
Expected<DWARFLocationExpressionsVector> Locs =
VarDie.getLocations(dwarf::DW_AT_location);
if (!Locs) {
consumeError(Locs.takeError());
return;
}
for (const DWARFLocationExpression &LocExpr : *Locs) {
if (LocExpr.Range) {
LiveVariables.emplace_back(LocExpr, VarName, U, FuncDie);
} else {
DWARFLocationExpression WholeFuncExpr{
DWARFAddressRange(FuncLowPC, FuncHighPC, SectionIndex), LocExpr.Expr};
LiveVariables.emplace_back(WholeFuncExpr, VarName, U, FuncDie);
}
}
}
void LiveVariablePrinter::addFunction(DWARFDie D) {
for (const DWARFDie &Child : D.children()) {
if (Child.getTag() == dwarf::DW_TAG_variable ||
Child.getTag() == dwarf::DW_TAG_formal_parameter)
addVariable(D, Child);
else
addFunction(Child);
}
}
unsigned LiveVariablePrinter::getIndentLevel() const {
return DbgIndent + getInstStartColumn(STI);
}
unsigned LiveVariablePrinter::moveToFirstVarColumn(formatted_raw_ostream &OS) {
unsigned FirstUnprintedLogicalColumn =
std::max((int)(OS.getColumn() - getIndentLevel() + 1) / 2, 0);
unsigned FirstUnprintedPhysicalColumn =
getIndentLevel() + FirstUnprintedLogicalColumn * 2;
if (FirstUnprintedPhysicalColumn > OS.getColumn())
OS.PadToColumn(FirstUnprintedPhysicalColumn);
return FirstUnprintedLogicalColumn;
}
unsigned LiveVariablePrinter::findFreeColumn() {
for (unsigned ColIdx = 0; ColIdx < ActiveCols.size(); ++ColIdx)
if (!ActiveCols[ColIdx].isActive())
return ColIdx;
size_t OldSize = ActiveCols.size();
ActiveCols.grow(std::max<size_t>(OldSize * 2, 1));
return OldSize;
}
void LiveVariablePrinter::dump() const {
for (const LiveVariable &LV : LiveVariables) {
dbgs() << LV.VarName << " @ " << LV.LocExpr.Range << ": ";
LV.print(dbgs(), MRI);
dbgs() << "\n";
}
}
void LiveVariablePrinter::addCompileUnit(DWARFDie D) {
if (D.getTag() == dwarf::DW_TAG_subprogram)
addFunction(D);
else
for (const DWARFDie &Child : D.children())
addFunction(Child);
}
void LiveVariablePrinter::update(object::SectionedAddress ThisAddr,
object::SectionedAddress NextAddr,
bool IncludeDefinedVars) {
SmallSet<unsigned, 8> CheckedVarIdxs;
for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
if (!ActiveCols[ColIdx].isActive())
continue;
CheckedVarIdxs.insert(ActiveCols[ColIdx].VarIdx);
LiveVariable &LV = LiveVariables[ActiveCols[ColIdx].VarIdx];
ActiveCols[ColIdx].LiveIn = LV.liveAtAddress(ThisAddr);
ActiveCols[ColIdx].LiveOut = LV.liveAtAddress(NextAddr);
LLVM_DEBUG(dbgs() << "pass 1, " << ThisAddr.Address << "-"
<< NextAddr.Address << ", " << LV.VarName << ", Col "
<< ColIdx << ": LiveIn=" << ActiveCols[ColIdx].LiveIn
<< ", LiveOut=" << ActiveCols[ColIdx].LiveOut << "\n");
if (!ActiveCols[ColIdx].LiveIn && !ActiveCols[ColIdx].LiveOut)
ActiveCols[ColIdx].VarIdx = Column::NullVarIdx;
}
if (IncludeDefinedVars) {
for (unsigned VarIdx = 0, End = LiveVariables.size(); VarIdx < End;
++VarIdx) {
if (CheckedVarIdxs.count(VarIdx))
continue;
LiveVariable &LV = LiveVariables[VarIdx];
bool LiveIn = LV.liveAtAddress(ThisAddr);
bool LiveOut = LV.liveAtAddress(NextAddr);
if (!LiveIn && !LiveOut)
continue;
unsigned ColIdx = findFreeColumn();
LLVM_DEBUG(dbgs() << "pass 2, " << ThisAddr.Address << "-"
<< NextAddr.Address << ", " << LV.VarName << ", Col "
<< ColIdx << ": LiveIn=" << LiveIn
<< ", LiveOut=" << LiveOut << "\n");
ActiveCols[ColIdx].VarIdx = VarIdx;
ActiveCols[ColIdx].LiveIn = LiveIn;
ActiveCols[ColIdx].LiveOut = LiveOut;
ActiveCols[ColIdx].MustDrawLabel = true;
}
}
}
enum class LineChar {
RangeStart,
RangeMid,
RangeEnd,
LabelVert,
LabelCornerNew,
LabelCornerActive,
LabelHoriz,
};
const char *LiveVariablePrinter::getLineChar(LineChar C) const {
bool IsASCII = DbgVariables == DVASCII;
switch (C) {
case LineChar::RangeStart:
return IsASCII ? "^" : (const char *)u8"\u2548";
case LineChar::RangeMid:
return IsASCII ? "|" : (const char *)u8"\u2503";
case LineChar::RangeEnd:
return IsASCII ? "v" : (const char *)u8"\u253b";
case LineChar::LabelVert:
return IsASCII ? "|" : (const char *)u8"\u2502";
case LineChar::LabelCornerNew:
return IsASCII ? "/" : (const char *)u8"\u250c";
case LineChar::LabelCornerActive:
return IsASCII ? "|" : (const char *)u8"\u2520";
case LineChar::LabelHoriz:
return IsASCII ? "-" : (const char *)u8"\u2500";
}
llvm_unreachable("Unhandled LineChar enum");
}
void LiveVariablePrinter::printAfterOtherLine(formatted_raw_ostream &OS,
bool AfterInst) {
if (ActiveCols.size()) {
unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
for (size_t ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
ColIdx < End; ++ColIdx) {
if (ActiveCols[ColIdx].isActive()) {
if ((AfterInst && ActiveCols[ColIdx].LiveOut) ||
(!AfterInst && ActiveCols[ColIdx].LiveIn))
OS << getLineChar(LineChar::RangeMid);
else if (!AfterInst && ActiveCols[ColIdx].LiveOut)
OS << getLineChar(LineChar::LabelVert);
else
OS << " ";
}
OS << " ";
}
}
OS << "\n";
}
void LiveVariablePrinter::printBetweenInsts(formatted_raw_ostream &OS,
bool MustPrint) {
bool PrintedSomething = false;
for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
if (ActiveCols[ColIdx].isActive() && ActiveCols[ColIdx].MustDrawLabel) {
OS.PadToColumn(getIndentLevel());
for (unsigned ColIdx2 = 0; ColIdx2 < ColIdx; ++ColIdx2) {
if (ActiveCols[ColIdx2].isActive()) {
if (ActiveCols[ColIdx2].MustDrawLabel && !ActiveCols[ColIdx2].LiveIn)
OS << getLineChar(LineChar::LabelVert) << " ";
else
OS << getLineChar(LineChar::RangeMid) << " ";
} else
OS << " ";
}
OS << getLineChar(ActiveCols[ColIdx].LiveIn ? LineChar::LabelCornerActive
: LineChar::LabelCornerNew)
<< getLineChar(LineChar::LabelHoriz) << " ";
WithColor(OS, raw_ostream::GREEN)
<< LiveVariables[ActiveCols[ColIdx].VarIdx].VarName;
OS << " = ";
{
WithColor ExprColor(OS, raw_ostream::CYAN);
LiveVariables[ActiveCols[ColIdx].VarIdx].print(OS, MRI);
}
unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
for (unsigned ColIdx2 = FirstUnprintedColumn, End = ActiveCols.size();
ColIdx2 < End; ++ColIdx2) {
if (ActiveCols[ColIdx2].isActive() && ActiveCols[ColIdx2].LiveIn)
OS << getLineChar(LineChar::RangeMid) << " ";
else
OS << " ";
}
OS << "\n";
PrintedSomething = true;
}
}
for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx)
if (ActiveCols[ColIdx].isActive())
ActiveCols[ColIdx].MustDrawLabel = false;
if (MustPrint && !PrintedSomething)
printAfterOtherLine(OS, false);
}
void LiveVariablePrinter::printAfterInst(formatted_raw_ostream &OS) {
if (!ActiveCols.size())
return;
unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
for (unsigned ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
ColIdx < End; ++ColIdx) {
if (!ActiveCols[ColIdx].isActive())
OS << " ";
else if (ActiveCols[ColIdx].LiveIn && ActiveCols[ColIdx].LiveOut)
OS << getLineChar(LineChar::RangeMid) << " ";
else if (ActiveCols[ColIdx].LiveOut)
OS << getLineChar(LineChar::RangeStart) << " ";
else if (ActiveCols[ColIdx].LiveIn)
OS << getLineChar(LineChar::RangeEnd) << " ";
else
llvm_unreachable("var must be live in or out!");
}
}
bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
std::unique_ptr<MemoryBuffer> Buffer;
if (LineInfo.Source) {
Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);
} else {
auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);
if (!BufferOrError) {
if (MissingSources.insert(LineInfo.FileName).second)
reportWarning("failed to find source " + LineInfo.FileName,
Obj->getFileName());
return false;
}
Buffer = std::move(*BufferOrError);
}
const char *BufferStart = Buffer->getBufferStart(),
*BufferEnd = Buffer->getBufferEnd();
std::vector<StringRef> &Lines = LineCache[LineInfo.FileName];
const char *Start = BufferStart;
for (const char *I = BufferStart; I != BufferEnd; ++I)
if (*I == '\n') {
Lines.emplace_back(Start, I - Start - (BufferStart < I && I[-1] == '\r'));
Start = I + 1;
}
if (Start < BufferEnd)
Lines.emplace_back(Start, BufferEnd - Start);
SourceCache[LineInfo.FileName] = std::move(Buffer);
return true;
}
void SourcePrinter::printSourceLine(formatted_raw_ostream &OS,
object::SectionedAddress Address,
StringRef ObjectFilename,
LiveVariablePrinter &LVP,
StringRef Delimiter) {
if (!Symbolizer)
return;
DILineInfo LineInfo = DILineInfo();
Expected<DILineInfo> ExpectedLineInfo =
Symbolizer->symbolizeCode(*Obj, Address);
std::string ErrorMessage;
if (ExpectedLineInfo) {
LineInfo = *ExpectedLineInfo;
} else if (!WarnedInvalidDebugInfo) {
WarnedInvalidDebugInfo = true;
reportWarning("failed to parse debug information: " +
toString(ExpectedLineInfo.takeError()),
ObjectFilename);
}
if (!objdump::Prefix.empty() &&
sys::path::is_absolute_gnu(LineInfo.FileName)) {
assert(!LineInfo.FileName.empty());
if (PrefixStrip > 0) {
uint32_t Level = 0;
auto StrippedNameStart = LineInfo.FileName.begin();
for (auto Pos = StrippedNameStart + 1, End = LineInfo.FileName.end();
Pos != End && Level < PrefixStrip; ++Pos) {
if (sys::path::is_separator(*Pos)) {
StrippedNameStart = Pos;
++Level;
}
}
LineInfo.FileName =
std::string(StrippedNameStart, LineInfo.FileName.end());
}
SmallString<128> FilePath;
sys::path::append(FilePath, Prefix, LineInfo.FileName);
LineInfo.FileName = std::string(FilePath);
}
if (PrintLines)
printLines(OS, LineInfo, Delimiter, LVP);
if (PrintSource)
printSources(OS, LineInfo, ObjectFilename, Delimiter, LVP);
OldLineInfo = LineInfo;
}
void SourcePrinter::printLines(formatted_raw_ostream &OS,
const DILineInfo &LineInfo, StringRef Delimiter,
LiveVariablePrinter &LVP) {
bool PrintFunctionName = LineInfo.FunctionName != DILineInfo::BadString &&
LineInfo.FunctionName != OldLineInfo.FunctionName;
if (PrintFunctionName) {
OS << Delimiter << LineInfo.FunctionName;
if (!StringRef(LineInfo.FunctionName).endswith("()"))
OS << "()";
OS << ":\n";
}
if (LineInfo.FileName != DILineInfo::BadString && LineInfo.Line != 0 &&
(OldLineInfo.Line != LineInfo.Line ||
OldLineInfo.FileName != LineInfo.FileName || PrintFunctionName)) {
OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line;
LVP.printBetweenInsts(OS, true);
}
}
void SourcePrinter::printSources(formatted_raw_ostream &OS,
const DILineInfo &LineInfo,
StringRef ObjectFilename, StringRef Delimiter,
LiveVariablePrinter &LVP) {
if (LineInfo.FileName == DILineInfo::BadString || LineInfo.Line == 0 ||
(OldLineInfo.Line == LineInfo.Line &&
OldLineInfo.FileName == LineInfo.FileName))
return;
if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
if (!cacheSource(LineInfo))
return;
auto LineBuffer = LineCache.find(LineInfo.FileName);
if (LineBuffer != LineCache.end()) {
if (LineInfo.Line > LineBuffer->second.size()) {
reportWarning(
formatv(
"debug info line number {0} exceeds the number of lines in {1}",
LineInfo.Line, LineInfo.FileName),
ObjectFilename);
return;
}
OS << Delimiter << LineBuffer->second[LineInfo.Line - 1];
LVP.printBetweenInsts(OS, true);
}
}
SourcePrinter::SourcePrinter(const object::ObjectFile *Obj,
StringRef DefaultArch)
: Obj(Obj) {
symbolize::LLVMSymbolizer::Options SymbolizerOpts;
SymbolizerOpts.PrintFunctions =
DILineInfoSpecifier::FunctionNameKind::LinkageName;
SymbolizerOpts.Demangle = Demangle;
SymbolizerOpts.DefaultArch = std::string(DefaultArch);
Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
}
} }