1 //===-- SourcePrinter.h - source interleaving utilities --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
10 #define LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
12 #include "llvm/ADT/IndexedMap.h"
13 #include "llvm/ADT/StringSet.h"
14 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
15 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
16 #include "llvm/MC/MCRegisterInfo.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 #include "llvm/Support/FormattedStream.h"
19 #include <unordered_map>
25 /// Stores a single expression representing the location of a source-level
26 /// variable, along with the PC range for which that expression is valid.
28 DWARFLocationExpression LocExpr
;
31 const DWARFDie FuncDie
;
33 LiveVariable(const DWARFLocationExpression
&LocExpr
, const char *VarName
,
34 DWARFUnit
*Unit
, const DWARFDie FuncDie
)
35 : LocExpr(LocExpr
), VarName(VarName
), Unit(Unit
), FuncDie(FuncDie
) {}
37 bool liveAtAddress(object::SectionedAddress Addr
);
39 void print(raw_ostream
&OS
, const MCRegisterInfo
&MRI
) const;
42 /// Helper class for printing source variable locations alongside disassembly.
43 class LiveVariablePrinter
{
44 // Information we want to track about one column in which we are printing a
45 // variable live range.
47 unsigned VarIdx
= NullVarIdx
;
50 bool MustDrawLabel
= false;
52 bool isActive() const { return VarIdx
!= NullVarIdx
; }
54 static constexpr unsigned NullVarIdx
= std::numeric_limits
<unsigned>::max();
57 // All live variables we know about in the object/image file.
58 std::vector
<LiveVariable
> LiveVariables
;
60 // The columns we are currently drawing.
61 IndexedMap
<Column
> ActiveCols
;
63 const MCRegisterInfo
&MRI
;
64 const MCSubtargetInfo
&STI
;
66 void addVariable(DWARFDie FuncDie
, DWARFDie VarDie
);
68 void addFunction(DWARFDie D
);
70 // Get the column number (in characters) at which the first live variable
71 // line should be printed.
72 unsigned getIndentLevel() const;
74 // Indent to the first live-range column to the right of the currently
75 // printed line, and return the index of that column.
76 // TODO: formatted_raw_ostream uses "column" to mean a number of characters
77 // since the last \n, and we use it to mean the number of slots in which we
78 // put live variable lines. Pick a less overloaded word.
79 unsigned moveToFirstVarColumn(formatted_raw_ostream
&OS
);
81 unsigned findFreeColumn();
84 LiveVariablePrinter(const MCRegisterInfo
&MRI
, const MCSubtargetInfo
&STI
)
85 : ActiveCols(Column()), MRI(MRI
), STI(STI
) {}
89 void addCompileUnit(DWARFDie D
);
91 /// Update to match the state of the instruction between ThisAddr and
92 /// NextAddr. In the common case, any live range active at ThisAddr is
93 /// live-in to the instruction, and any live range active at NextAddr is
94 /// live-out of the instruction. If IncludeDefinedVars is false, then live
95 /// ranges starting at NextAddr will be ignored.
96 void update(object::SectionedAddress ThisAddr
,
97 object::SectionedAddress NextAddr
, bool IncludeDefinedVars
);
108 const char *getLineChar(LineChar C
) const;
110 /// Print live ranges to the right of an existing line. This assumes the
111 /// line is not an instruction, so doesn't start or end any live ranges, so
112 /// we only need to print active ranges or empty columns. If AfterInst is
113 /// true, this is being printed after the last instruction fed to update(),
114 /// otherwise this is being printed before it.
115 void printAfterOtherLine(formatted_raw_ostream
&OS
, bool AfterInst
);
117 /// Print any live variable range info needed to the right of a
118 /// non-instruction line of disassembly. This is where we print the variable
119 /// names and expressions, with thin line-drawing characters connecting them
120 /// to the live range which starts at the next instruction. If MustPrint is
121 /// true, we have to print at least one line (with the continuation of any
122 /// already-active live ranges) because something has already been printed
123 /// earlier on this line.
124 void printBetweenInsts(formatted_raw_ostream
&OS
, bool MustPrint
);
126 /// Print the live variable ranges to the right of a disassembled instruction.
127 void printAfterInst(formatted_raw_ostream
&OS
);
130 class SourcePrinter
{
132 DILineInfo OldLineInfo
;
133 const object::ObjectFile
*Obj
= nullptr;
134 std::unique_ptr
<symbolize::LLVMSymbolizer
> Symbolizer
;
135 // File name to file contents of source.
136 std::unordered_map
<std::string
, std::unique_ptr
<MemoryBuffer
>> SourceCache
;
137 // Mark the line endings of the cached source.
138 std::unordered_map
<std::string
, std::vector
<StringRef
>> LineCache
;
139 // Keep track of missing sources.
140 StringSet
<> MissingSources
;
141 // Only emit 'invalid debug info' warning once.
142 bool WarnedInvalidDebugInfo
= false;
145 bool cacheSource(const DILineInfo
&LineInfoFile
);
147 void printLines(formatted_raw_ostream
&OS
, const DILineInfo
&LineInfo
,
148 StringRef Delimiter
, LiveVariablePrinter
&LVP
);
150 void printSources(formatted_raw_ostream
&OS
, const DILineInfo
&LineInfo
,
151 StringRef ObjectFilename
, StringRef Delimiter
,
152 LiveVariablePrinter
&LVP
);
154 // Returns line source code corresponding to `LineInfo`.
155 // Returns empty string if source code cannot be found.
156 StringRef
getLine(const DILineInfo
&LineInfo
, StringRef ObjectFilename
);
159 SourcePrinter() = default;
160 SourcePrinter(const object::ObjectFile
*Obj
, StringRef DefaultArch
);
161 virtual ~SourcePrinter() = default;
162 virtual void printSourceLine(formatted_raw_ostream
&OS
,
163 object::SectionedAddress Address
,
164 StringRef ObjectFilename
,
165 LiveVariablePrinter
&LVP
,
166 StringRef Delimiter
= "; ");
169 } // namespace objdump