xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/SourcePrinter.h (revision 66fd12cf4896eb08ad8e7a2627537f84ead84dd3)
1 //===-- SourcePrinter.h -  source interleaving utilities --------*- C++ -*-===//
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 #ifndef LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
10 #define LLVM_TOOLS_LLVM_OBJDUMP_SOURCEPRINTER_H
11 
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>
20 #include <vector>
21 
22 namespace llvm {
23 namespace objdump {
24 
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.
27 struct LiveVariable {
28   DWARFLocationExpression LocExpr;
29   const char *VarName;
30   DWARFUnit *Unit;
31   const DWARFDie FuncDie;
32 
33   LiveVariable(const DWARFLocationExpression &LocExpr, const char *VarName,
34                DWARFUnit *Unit, const DWARFDie FuncDie)
35       : LocExpr(LocExpr), VarName(VarName), Unit(Unit), FuncDie(FuncDie) {}
36 
37   bool liveAtAddress(object::SectionedAddress Addr);
38 
39   void print(raw_ostream &OS, const MCRegisterInfo &MRI) const;
40 };
41 
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.
46   struct Column {
47     unsigned VarIdx = NullVarIdx;
48     bool LiveIn = false;
49     bool LiveOut = false;
50     bool MustDrawLabel = false;
51 
52     bool isActive() const { return VarIdx != NullVarIdx; }
53 
54     static constexpr unsigned NullVarIdx = std::numeric_limits<unsigned>::max();
55   };
56 
57   // All live variables we know about in the object/image file.
58   std::vector<LiveVariable> LiveVariables;
59 
60   // The columns we are currently drawing.
61   IndexedMap<Column> ActiveCols;
62 
63   const MCRegisterInfo &MRI;
64   const MCSubtargetInfo &STI;
65 
66   void addVariable(DWARFDie FuncDie, DWARFDie VarDie);
67 
68   void addFunction(DWARFDie D);
69 
70   // Get the column number (in characters) at which the first live variable
71   // line should be printed.
72   unsigned getIndentLevel() const;
73 
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);
80 
81   unsigned findFreeColumn();
82 
83 public:
84   LiveVariablePrinter(const MCRegisterInfo &MRI, const MCSubtargetInfo &STI)
85       : ActiveCols(Column()), MRI(MRI), STI(STI) {}
86 
87   void dump() const;
88 
89   void addCompileUnit(DWARFDie D);
90 
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);
98 
99   enum class LineChar {
100     RangeStart,
101     RangeMid,
102     RangeEnd,
103     LabelVert,
104     LabelCornerNew,
105     LabelCornerActive,
106     LabelHoriz,
107   };
108   const char *getLineChar(LineChar C) const;
109 
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);
116 
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);
125 
126   /// Print the live variable ranges to the right of a disassembled instruction.
127   void printAfterInst(formatted_raw_ostream &OS);
128 };
129 
130 class SourcePrinter {
131 protected:
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;
143 
144 private:
145   bool cacheSource(const DILineInfo &LineInfoFile);
146 
147   void printLines(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
148                   StringRef Delimiter, LiveVariablePrinter &LVP);
149 
150   void printSources(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
151                     StringRef ObjectFilename, StringRef Delimiter,
152                     LiveVariablePrinter &LVP);
153 
154 public:
155   SourcePrinter() = default;
156   SourcePrinter(const object::ObjectFile *Obj, StringRef DefaultArch);
157   virtual ~SourcePrinter() = default;
158   virtual void printSourceLine(formatted_raw_ostream &OS,
159                                object::SectionedAddress Address,
160                                StringRef ObjectFilename,
161                                LiveVariablePrinter &LVP,
162                                StringRef Delimiter = "; ");
163 };
164 
165 } // namespace objdump
166 } // namespace llvm
167 
168 #endif
169