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
24namespace llvm {
25namespace objdump {
26
27bool 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
35void 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(RegNum: DwarfRegNum, isEH: IsEH))
43 if (const char *RegName = MRI.getName(RegNo: *LLVMRegNum))
44 return StringRef(RegName);
45 OS << "<unknown register " << DwarfRegNum << ">";
46 return {};
47 };
48
49 printDwarfExpressionCompact(E: &Expression, OS, GetNameForDWARFReg: GetRegName);
50}
51
52void LiveVariablePrinter::addVariable(DWARFDie FuncDie, DWARFDie VarDie) {
53 uint64_t FuncLowPC, FuncHighPC, SectionIndex;
54 FuncDie.getLowAndHighPC(LowPC&: FuncLowPC, HighPC&: FuncHighPC, SectionIndex);
55 const char *VarName = VarDie.getName(Kind: DINameKind::ShortName);
56 DWARFUnit *U = VarDie.getDwarfUnit();
57
58 Expected<DWARFLocationExpressionsVector> Locs =
59 VarDie.getLocations(Attr: 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(Err: Locs.takeError());
65 return;
66 }
67
68 for (const DWARFLocationExpression &LocExpr : *Locs) {
69 if (LocExpr.Range) {
70 LiveVariables.emplace_back(args: LocExpr, args&: VarName, args&: U, args&: 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 .Range: DWARFAddressRange(FuncLowPC, FuncHighPC, SectionIndex), .Expr: LocExpr.Expr};
78 LiveVariables.emplace_back(args&: WholeFuncExpr, args&: VarName, args&: U, args&: FuncDie);
79 }
80 }
81}
82
83void 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(FuncDie: D, VarDie: Child);
88 else
89 addFunction(D: Child);
90 }
91}
92
93// Get the column number (in characters) at which the first live variable
94// line should be printed.
95unsigned 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.
104unsigned 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(a: (int)(OS.getColumn() - getIndentLevel() + 1) / 2, b: 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(NewCol: FirstUnprintedPhysicalColumn);
116
117 return FirstUnprintedLogicalColumn;
118}
119
120unsigned 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(n: std::max<size_t>(a: OldSize * 2, b: 1));
127 return OldSize;
128}
129
130void LiveVariablePrinter::dump() const {
131 for (const LiveVariable &LV : LiveVariables) {
132 dbgs() << LV.VarName << " @ " << LV.LocExpr.Range << ": ";
133 LV.print(OS&: dbgs(), MRI);
134 dbgs() << "\n";
135 }
136}
137
138void 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(D: 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.
151void 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(V: ActiveCols[ColIdx].VarIdx);
161 LiveVariable &LV = LiveVariables[ActiveCols[ColIdx].VarIdx];
162 ActiveCols[ColIdx].LiveIn = LV.liveAtAddress(Addr: ThisAddr);
163 ActiveCols[ColIdx].LiveOut = LV.liveAtAddress(Addr: 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(V: VarIdx))
179 continue;
180 LiveVariable &LV = LiveVariables[VarIdx];
181 bool LiveIn = LV.liveAtAddress(Addr: ThisAddr);
182 bool LiveOut = LV.liveAtAddress(Addr: 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
199enum class LineChar {
200 RangeStart,
201 RangeMid,
202 RangeEnd,
203 LabelVert,
204 LabelCornerNew,
205 LabelCornerActive,
206 LabelHoriz,
207};
208const 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.
234void 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(C: LineChar::RangeMid);
244 else if (!AfterInst && ActiveCols[ColIdx].LiveOut)
245 OS << getLineChar(C: 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.
262void 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(NewCol: 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(C: LineChar::LabelVert) << " ";
274 else
275 OS << getLineChar(C: 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(C: ActiveCols[ColIdx].LiveIn ? LineChar::LabelCornerActive
283 : LineChar::LabelCornerNew)
284 << getLineChar(C: 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(C: 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, AfterInst: false);
318}
319
320/// Print the live variable ranges to the right of a disassembled instruction.
321void 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(C: LineChar::RangeMid) << " ";
331 else if (ActiveCols[ColIdx].LiveOut)
332 OS << getLineChar(C: LineChar::RangeStart) << " ";
333 else if (ActiveCols[ColIdx].LiveIn)
334 OS << getLineChar(C: LineChar::RangeEnd) << " ";
335 else
336 llvm_unreachable("var must be live in or out!");
337 }
338}
339
340bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
341 std::unique_ptr<MemoryBuffer> Buffer;
342 if (LineInfo.Source) {
343 Buffer = MemoryBuffer::getMemBuffer(InputData: *LineInfo.Source);
344 } else {
345 auto BufferOrError =
346 MemoryBuffer::getFile(Filename: LineInfo.FileName, /*IsText=*/true);
347 if (!BufferOrError) {
348 if (MissingSources.insert(key: LineInfo.FileName).second)
349 reportWarning(Message: "failed to find source " + LineInfo.FileName,
350 File: 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(args&: Start, args: I - Start - (BufferStart < I && I[-1] == '\r'));
363 Start = I + 1;
364 }
365 if (Start < BufferEnd)
366 Lines.emplace_back(args&: Start, args: BufferEnd - Start);
367 SourceCache[LineInfo.FileName] = std::move(Buffer);
368 return true;
369}
370
371void 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: *Obj, ModuleOffset: Address);
382 if (ExpectedLineInfo) {
383 LineInfo = *ExpectedLineInfo;
384 } else if (!WarnedInvalidDebugInfo) {
385 WarnedInvalidDebugInfo = true;
386 // TODO Untested.
387 reportWarning(Message: "failed to parse debug information: " +
388 toString(E: ExpectedLineInfo.takeError()),
389 File: ObjectFilename);
390 }
391
392 if (!objdump::Prefix.empty() &&
393 sys::path::is_absolute_gnu(path: 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(value: *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(path&: FilePath, a: Prefix, b: 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
428void 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(Suffix: "()"))
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, MustPrint: 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.
452StringRef SourcePrinter::getLine(const DILineInfo &LineInfo,
453 StringRef ObjectFilename) {
454 if (LineInfo.LineSource)
455 return LineInfo.LineSource.value();
456
457 if (SourceCache.find(x: LineInfo.FileName) == SourceCache.end())
458 if (!cacheSource(LineInfo))
459 return {};
460
461 auto LineBuffer = LineCache.find(x: LineInfo.FileName);
462 if (LineBuffer == LineCache.end())
463 return {};
464
465 if (LineInfo.Line > LineBuffer->second.size()) {
466 reportWarning(
467 Message: formatv(Fmt: "debug info line number {0} exceeds the number of lines in {1}",
468 Vals: LineInfo.Line, Vals: LineInfo.FileName),
469 File: ObjectFilename);
470 return {};
471 }
472
473 // Vector begins at 0, line numbers are non-zero
474 return LineBuffer->second[LineInfo.Line - 1];
475}
476
477void 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, MustPrint: true);
490 }
491}
492
493SourcePrinter::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(p: new symbolize::LLVMSymbolizer(SymbolizerOpts));
502}
503
504} // namespace objdump
505} // namespace llvm
506