xref: /llvm-project/llvm/tools/llvm-objdump/SourcePrinter.h (revision c8e055d485eabf1c8830d77797e3686ced0f7754)
1c623945dSTim Northover //===-- SourcePrinter.h -  source interleaving utilities --------*- C++ -*-===//
2c623945dSTim Northover //
3c623945dSTim Northover // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c623945dSTim Northover // See https://llvm.org/LICENSE.txt for license information.
5c623945dSTim Northover // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c623945dSTim Northover //
7c623945dSTim Northover //===----------------------------------------------------------------------===//
8c623945dSTim Northover 
9c623945dSTim Northover #ifndef LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
10c623945dSTim Northover #define LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
11c623945dSTim Northover 
12c623945dSTim Northover #include "llvm/ADT/IndexedMap.h"
13c623945dSTim Northover #include "llvm/ADT/StringSet.h"
14c623945dSTim Northover #include "llvm/DebugInfo/DWARF/DWARFContext.h"
15c623945dSTim Northover #include "llvm/DebugInfo/Symbolize/Symbolize.h"
16617ed4f0SShubham Sandeep Rastogi #include "llvm/MC/MCRegisterInfo.h"
17e72c195fSserge-sans-paille #include "llvm/MC/MCSubtargetInfo.h"
18c623945dSTim Northover #include "llvm/Support/FormattedStream.h"
19c623945dSTim Northover #include <unordered_map>
20c623945dSTim Northover #include <vector>
21c623945dSTim Northover 
22c623945dSTim Northover namespace llvm {
23c623945dSTim Northover namespace objdump {
24c623945dSTim Northover 
25c623945dSTim Northover /// Stores a single expression representing the location of a source-level
26c623945dSTim Northover /// variable, along with the PC range for which that expression is valid.
27c623945dSTim Northover struct LiveVariable {
28c623945dSTim Northover   DWARFLocationExpression LocExpr;
29c623945dSTim Northover   const char *VarName;
30c623945dSTim Northover   DWARFUnit *Unit;
31c623945dSTim Northover   const DWARFDie FuncDie;
32c623945dSTim Northover 
LiveVariableLiveVariable33c623945dSTim Northover   LiveVariable(const DWARFLocationExpression &LocExpr, const char *VarName,
34c623945dSTim Northover                DWARFUnit *Unit, const DWARFDie FuncDie)
35c623945dSTim Northover       : LocExpr(LocExpr), VarName(VarName), Unit(Unit), FuncDie(FuncDie) {}
36c623945dSTim Northover 
37c623945dSTim Northover   bool liveAtAddress(object::SectionedAddress Addr);
38c623945dSTim Northover 
39c623945dSTim Northover   void print(raw_ostream &OS, const MCRegisterInfo &MRI) const;
40c623945dSTim Northover };
41c623945dSTim Northover 
42c623945dSTim Northover /// Helper class for printing source variable locations alongside disassembly.
43c623945dSTim Northover class LiveVariablePrinter {
44c623945dSTim Northover   // Information we want to track about one column in which we are printing a
45c623945dSTim Northover   // variable live range.
46c623945dSTim Northover   struct Column {
47c623945dSTim Northover     unsigned VarIdx = NullVarIdx;
48c623945dSTim Northover     bool LiveIn = false;
49c623945dSTim Northover     bool LiveOut = false;
50c623945dSTim Northover     bool MustDrawLabel = false;
51c623945dSTim Northover 
isActiveColumn52c623945dSTim Northover     bool isActive() const { return VarIdx != NullVarIdx; }
53c623945dSTim Northover 
54c623945dSTim Northover     static constexpr unsigned NullVarIdx = std::numeric_limits<unsigned>::max();
55c623945dSTim Northover   };
56c623945dSTim Northover 
57c623945dSTim Northover   // All live variables we know about in the object/image file.
58c623945dSTim Northover   std::vector<LiveVariable> LiveVariables;
59c623945dSTim Northover 
60c623945dSTim Northover   // The columns we are currently drawing.
61c623945dSTim Northover   IndexedMap<Column> ActiveCols;
62c623945dSTim Northover 
63c623945dSTim Northover   const MCRegisterInfo &MRI;
64c623945dSTim Northover   const MCSubtargetInfo &STI;
65c623945dSTim Northover 
66c623945dSTim Northover   void addVariable(DWARFDie FuncDie, DWARFDie VarDie);
67c623945dSTim Northover 
68c623945dSTim Northover   void addFunction(DWARFDie D);
69c623945dSTim Northover 
70c623945dSTim Northover   // Get the column number (in characters) at which the first live variable
71c623945dSTim Northover   // line should be printed.
72c623945dSTim Northover   unsigned getIndentLevel() const;
73c623945dSTim Northover 
74c623945dSTim Northover   // Indent to the first live-range column to the right of the currently
75c623945dSTim Northover   // printed line, and return the index of that column.
76c623945dSTim Northover   // TODO: formatted_raw_ostream uses "column" to mean a number of characters
77c623945dSTim Northover   // since the last \n, and we use it to mean the number of slots in which we
78c623945dSTim Northover   // put live variable lines. Pick a less overloaded word.
79c623945dSTim Northover   unsigned moveToFirstVarColumn(formatted_raw_ostream &OS);
80c623945dSTim Northover 
81c623945dSTim Northover   unsigned findFreeColumn();
82c623945dSTim Northover 
83c623945dSTim Northover public:
LiveVariablePrinter(const MCRegisterInfo & MRI,const MCSubtargetInfo & STI)84c623945dSTim Northover   LiveVariablePrinter(const MCRegisterInfo &MRI, const MCSubtargetInfo &STI)
85f44473ecSKazu Hirata       : ActiveCols(Column()), MRI(MRI), STI(STI) {}
86c623945dSTim Northover 
87c623945dSTim Northover   void dump() const;
88c623945dSTim Northover 
89c623945dSTim Northover   void addCompileUnit(DWARFDie D);
90c623945dSTim Northover 
91c623945dSTim Northover   /// Update to match the state of the instruction between ThisAddr and
92c623945dSTim Northover   /// NextAddr. In the common case, any live range active at ThisAddr is
93c623945dSTim Northover   /// live-in to the instruction, and any live range active at NextAddr is
94c623945dSTim Northover   /// live-out of the instruction. If IncludeDefinedVars is false, then live
95c623945dSTim Northover   /// ranges starting at NextAddr will be ignored.
96c623945dSTim Northover   void update(object::SectionedAddress ThisAddr,
97c623945dSTim Northover               object::SectionedAddress NextAddr, bool IncludeDefinedVars);
98c623945dSTim Northover 
99c623945dSTim Northover   enum class LineChar {
100c623945dSTim Northover     RangeStart,
101c623945dSTim Northover     RangeMid,
102c623945dSTim Northover     RangeEnd,
103c623945dSTim Northover     LabelVert,
104c623945dSTim Northover     LabelCornerNew,
105c623945dSTim Northover     LabelCornerActive,
106c623945dSTim Northover     LabelHoriz,
107c623945dSTim Northover   };
108c623945dSTim Northover   const char *getLineChar(LineChar C) const;
109c623945dSTim Northover 
110c623945dSTim Northover   /// Print live ranges to the right of an existing line. This assumes the
111c623945dSTim Northover   /// line is not an instruction, so doesn't start or end any live ranges, so
112c623945dSTim Northover   /// we only need to print active ranges or empty columns. If AfterInst is
113c623945dSTim Northover   /// true, this is being printed after the last instruction fed to update(),
114c623945dSTim Northover   /// otherwise this is being printed before it.
115c623945dSTim Northover   void printAfterOtherLine(formatted_raw_ostream &OS, bool AfterInst);
116c623945dSTim Northover 
117c623945dSTim Northover   /// Print any live variable range info needed to the right of a
118c623945dSTim Northover   /// non-instruction line of disassembly. This is where we print the variable
119c623945dSTim Northover   /// names and expressions, with thin line-drawing characters connecting them
120c623945dSTim Northover   /// to the live range which starts at the next instruction. If MustPrint is
121c623945dSTim Northover   /// true, we have to print at least one line (with the continuation of any
122c623945dSTim Northover   /// already-active live ranges) because something has already been printed
123c623945dSTim Northover   /// earlier on this line.
124c623945dSTim Northover   void printBetweenInsts(formatted_raw_ostream &OS, bool MustPrint);
125c623945dSTim Northover 
126c623945dSTim Northover   /// Print the live variable ranges to the right of a disassembled instruction.
127c623945dSTim Northover   void printAfterInst(formatted_raw_ostream &OS);
128c623945dSTim Northover };
129c623945dSTim Northover 
130c623945dSTim Northover class SourcePrinter {
131c623945dSTim Northover protected:
132c623945dSTim Northover   DILineInfo OldLineInfo;
133c623945dSTim Northover   const object::ObjectFile *Obj = nullptr;
134c623945dSTim Northover   std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
135c623945dSTim Northover   // File name to file contents of source.
136c623945dSTim Northover   std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
137c623945dSTim Northover   // Mark the line endings of the cached source.
138c623945dSTim Northover   std::unordered_map<std::string, std::vector<StringRef>> LineCache;
139c623945dSTim Northover   // Keep track of missing sources.
140c623945dSTim Northover   StringSet<> MissingSources;
141c623945dSTim Northover   // Only emit 'invalid debug info' warning once.
142c623945dSTim Northover   bool WarnedInvalidDebugInfo = false;
143c623945dSTim Northover 
144c623945dSTim Northover private:
145c623945dSTim Northover   bool cacheSource(const DILineInfo &LineInfoFile);
146c623945dSTim Northover 
147c623945dSTim Northover   void printLines(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
148c623945dSTim Northover                   StringRef Delimiter, LiveVariablePrinter &LVP);
149c623945dSTim Northover 
150c623945dSTim Northover   void printSources(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
151c623945dSTim Northover                     StringRef ObjectFilename, StringRef Delimiter,
152c623945dSTim Northover                     LiveVariablePrinter &LVP);
153c623945dSTim Northover 
154*c8e055d4SEduard Zingerman   // Returns line source code corresponding to `LineInfo`.
155*c8e055d4SEduard Zingerman   // Returns empty string if source code cannot be found.
156*c8e055d4SEduard Zingerman   StringRef getLine(const DILineInfo &LineInfo, StringRef ObjectFilename);
157*c8e055d4SEduard Zingerman 
158c623945dSTim Northover public:
159c623945dSTim Northover   SourcePrinter() = default;
160c623945dSTim Northover   SourcePrinter(const object::ObjectFile *Obj, StringRef DefaultArch);
161c623945dSTim Northover   virtual ~SourcePrinter() = default;
162c623945dSTim Northover   virtual void printSourceLine(formatted_raw_ostream &OS,
163c623945dSTim Northover                                object::SectionedAddress Address,
164c623945dSTim Northover                                StringRef ObjectFilename,
165c623945dSTim Northover                                LiveVariablePrinter &LVP,
166c623945dSTim Northover                                StringRef Delimiter = "; ");
167c623945dSTim Northover };
168c623945dSTim Northover 
169c623945dSTim Northover } // namespace objdump
170c623945dSTim Northover } // namespace llvm
171c623945dSTim Northover 
172c623945dSTim Northover #endif
173