LLDB  mainline
LineTable.h
Go to the documentation of this file.
1 //===-- LineTable.h ---------------------------------------------*- 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 LLDB_SYMBOL_LINETABLE_H
10 #define LLDB_SYMBOL_LINETABLE_H
11 
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/ModuleChild.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/LineEntry.h"
16 #include "lldb/Utility/RangeMap.h"
17 #include "lldb/lldb-private.h"
18 #include <vector>
19 
20 namespace lldb_private {
21 
22 /// \class LineSequence LineTable.h "lldb/Symbol/LineTable.h" An abstract base
23 /// class used during symbol table creation.
24 class LineSequence {
25 public:
26  LineSequence();
27 
28  virtual ~LineSequence() = default;
29 
30  virtual void Clear() = 0;
31 
32 private:
33  LineSequence(const LineSequence &) = delete;
34  const LineSequence &operator=(const LineSequence &) = delete;
35 };
36 
37 /// \class LineTable LineTable.h "lldb/Symbol/LineTable.h"
38 /// A line table class.
39 class LineTable {
40 public:
41  /// Construct with compile unit.
42  ///
43  /// \param[in] comp_unit
44  /// The compile unit to which this line table belongs.
45  LineTable(CompileUnit *comp_unit);
46 
47  /// Construct with entries found in \a sequences.
48  ///
49  /// \param[in] sequences
50  /// Unsorted list of line sequences.
51  LineTable(CompileUnit *comp_unit,
52  std::vector<std::unique_ptr<LineSequence>> &&sequences);
53 
54  /// Destructor.
55  ~LineTable();
56 
57  /// Adds a new line entry to this line table.
58  ///
59  /// All line entries are maintained in file address order.
60  ///
61  /// \param[in] line_entry
62  /// A const reference to a new line_entry to add to this line
63  /// table.
64  ///
65  /// \see Address::DumpStyle
66  // void
67  // AddLineEntry (const LineEntry& line_entry);
68 
69  // Called when you can't guarantee the addresses are in increasing order
70  void InsertLineEntry(lldb::addr_t file_addr, uint32_t line, uint16_t column,
71  uint16_t file_idx, bool is_start_of_statement,
72  bool is_start_of_basic_block, bool is_prologue_end,
73  bool is_epilogue_begin, bool is_terminal_entry);
74 
75  // Used to instantiate the LineSequence helper class
76  static std::unique_ptr<LineSequence> CreateLineSequenceContainer();
77 
78  // Append an entry to a caller-provided collection that will later be
79  // inserted in this line table.
80  static void AppendLineEntryToSequence(LineSequence *sequence, lldb::addr_t file_addr,
81  uint32_t line, uint16_t column,
82  uint16_t file_idx, bool is_start_of_statement,
83  bool is_start_of_basic_block,
84  bool is_prologue_end, bool is_epilogue_begin,
85  bool is_terminal_entry);
86 
87  // Insert a sequence of entries into this line table.
88  void InsertSequence(LineSequence *sequence);
89 
90  /// Dump all line entries in this line table to the stream \a s.
91  ///
92  /// \param[in] s
93  /// The stream to which to dump the object description.
94  ///
95  /// \param[in] style
96  /// The display style for the address.
97  ///
98  /// \see Address::DumpStyle
99  void Dump(Stream *s, Target *target, Address::DumpStyle style,
100  Address::DumpStyle fallback_style, bool show_line_ranges);
101 
102  void GetDescription(Stream *s, Target *target, lldb::DescriptionLevel level);
103 
104  /// Find a line entry that contains the section offset address \a so_addr.
105  ///
106  /// \param[in] so_addr
107  /// A section offset address object containing the address we
108  /// are searching for.
109  ///
110  /// \param[out] line_entry
111  /// A copy of the line entry that was found if \b true is
112  /// returned, otherwise \a entry is left unmodified.
113  ///
114  /// \param[out] index_ptr
115  /// A pointer to a 32 bit integer that will get the actual line
116  /// entry index if it is not nullptr.
117  ///
118  /// \return
119  /// Returns \b true if \a so_addr is contained in a line entry
120  /// in this line table, \b false otherwise.
121  bool FindLineEntryByAddress(const Address &so_addr, LineEntry &line_entry,
122  uint32_t *index_ptr = nullptr);
123 
124  /// Find a line entry index that has a matching file index and source line
125  /// number.
126  ///
127  /// Finds the next line entry that has a matching \a file_idx and source
128  /// line number \a line starting at the \a start_idx entries into the line
129  /// entry collection.
130  ///
131  /// \param[in] start_idx
132  /// The number of entries to skip when starting the search.
133  ///
134  /// \param[out] file_idx
135  /// The file index to search for that should be found prior
136  /// to calling this function using the following functions:
137  /// CompileUnit::GetSupportFiles()
138  /// FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
139  ///
140  /// \param[in] line
141  /// The source line to match.
142  ///
143  /// \param[in] exact
144  /// If true, match only if you find a line entry exactly matching \a line.
145  /// If false, return the closest line entry greater than \a line.
146  ///
147  /// \param[out] line_entry_ptr
148  /// A pointer to a line entry object that will get a copy of
149  /// the line entry if \b true is returned, otherwise \a
150  /// line_entry is left untouched.
151  ///
152  /// \return
153  /// Returns \b true if a matching line entry is found in this
154  /// line table, \b false otherwise.
155  ///
156  /// \see CompileUnit::GetSupportFiles()
157  /// \see FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
158  uint32_t FindLineEntryIndexByFileIndex(uint32_t start_idx, uint32_t file_idx,
159  uint32_t line, bool exact,
160  LineEntry *line_entry_ptr);
161 
162  uint32_t FindLineEntryIndexByFileIndex(
163  uint32_t start_idx, const std::vector<uint32_t> &file_indexes,
164  uint32_t line, bool exact, LineEntry *line_entry_ptr);
165 
166  size_t FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
167  SymbolContextList &sc_list);
168 
169  /// Get the line entry from the line table at index \a idx.
170  ///
171  /// \param[in] idx
172  /// An index into the line table entry collection.
173  ///
174  /// \return
175  /// A valid line entry if \a idx is a valid index, or an invalid
176  /// line entry if \a idx is not valid.
177  ///
178  /// \see LineTable::GetSize()
179  /// \see LineEntry::IsValid() const
180  bool GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry);
181 
182  /// Gets the size of the line table in number of line table entries.
183  ///
184  /// \return
185  /// The number of line table entries in this line table.
186  uint32_t GetSize() const;
187 
190 
191  /// Gets all contiguous file address ranges for the entire line table.
192  ///
193  /// \param[out] file_ranges
194  /// A collection of file address ranges that will be filled in
195  /// by this function.
196  ///
197  /// \param[out] append
198  /// If \b true, then append to \a file_ranges, otherwise clear
199  /// \a file_ranges prior to adding any ranges.
200  ///
201  /// \return
202  /// The number of address ranges added to \a file_ranges
203  size_t GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
204  bool append);
205 
208 
209  LineTable *LinkLineTable(const FileRangeMap &file_range_map);
210 
211 protected:
212  struct Entry {
214  : file_addr(LLDB_INVALID_ADDRESS), line(0),
215  is_start_of_statement(false), is_start_of_basic_block(false),
216  is_prologue_end(false), is_epilogue_begin(false),
217  is_terminal_entry(false), column(0), file_idx(0) {}
218 
219  Entry(lldb::addr_t _file_addr, uint32_t _line, uint16_t _column,
220  uint16_t _file_idx, bool _is_start_of_statement,
221  bool _is_start_of_basic_block, bool _is_prologue_end,
222  bool _is_epilogue_begin, bool _is_terminal_entry)
223  : file_addr(_file_addr), line(_line),
224  is_start_of_statement(_is_start_of_statement),
225  is_start_of_basic_block(_is_start_of_basic_block),
226  is_prologue_end(_is_prologue_end),
227  is_epilogue_begin(_is_epilogue_begin),
228  is_terminal_entry(_is_terminal_entry), column(_column),
229  file_idx(_file_idx) {}
230 
231  int bsearch_compare(const void *key, const void *arrmem);
232 
233  void Clear() {
234  file_addr = LLDB_INVALID_ADDRESS;
235  line = 0;
236  column = 0;
237  file_idx = 0;
238  is_start_of_statement = false;
239  is_start_of_basic_block = false;
240  is_prologue_end = false;
241  is_epilogue_begin = false;
242  is_terminal_entry = false;
243  }
244 
245  static int Compare(const Entry &lhs, const Entry &rhs) {
246 // Compare the sections before calling
247 #define SCALAR_COMPARE(a, b) \
248  if (a < b) \
249  return -1; \
250  if (a > b) \
251  return +1
253  SCALAR_COMPARE(lhs.line, rhs.line);
254  SCALAR_COMPARE(lhs.column, rhs.column);
257  // rhs and lhs reversed on purpose below.
260  // rhs and lhs reversed on purpose below.
262  SCALAR_COMPARE(lhs.file_idx, rhs.file_idx);
263 #undef SCALAR_COMPARE
264  return 0;
265  }
266 
268  public:
269  LessThanBinaryPredicate(LineTable *line_table);
270  bool operator()(const LineTable::Entry &, const LineTable::Entry &) const;
271  bool operator()(const std::unique_ptr<LineSequence> &,
272  const std::unique_ptr<LineSequence> &) const;
273 
274  protected:
276  };
277 
278  static bool EntryAddressLessThan(const Entry &lhs, const Entry &rhs) {
279  return lhs.file_addr < rhs.file_addr;
280  }
281 
282  // Member variables.
283  /// The file address for this line entry.
285  /// The source line number, or zero if there is no line number
286  /// information.
288  /// Indicates this entry is the beginning of a statement.
290  /// Indicates this entry is the beginning of a basic block.
292  /// Indicates this entry is one (of possibly many) where execution
293  /// should be suspended for an entry breakpoint of a function.
295  /// Indicates this entry is one (of possibly many) where execution
296  /// should be suspended for an exit breakpoint of a function.
298  /// Indicates this entry is that of the first byte after the end
299  /// of a sequence of target machine instructions.
301  /// The column number of the source line, or zero if there is no
302  /// column information.
304  /// The file index into CompileUnit's file table, or zero if there
305  /// is no file information.
307  };
308 
313  };
314 
315  // Types
316  typedef std::vector<lldb_private::Section *>
317  section_collection; ///< The collection type for the sections.
318  typedef std::vector<Entry>
319  entry_collection; ///< The collection type for the line entries.
320  // Member variables.
322  *m_comp_unit; ///< The compile unit that this line table belongs to.
324  m_entries; ///< The collection of line entries in this line table.
325 
326  // Helper class
328  public:
329  LineSequenceImpl() = default;
330 
331  ~LineSequenceImpl() override = default;
332 
333  void Clear() override;
334 
336  m_entries; ///< The collection of line entries in this sequence.
337  };
338 
339  bool ConvertEntryAtIndexToLineEntry(uint32_t idx, LineEntry &line_entry);
340 
341 private:
342  LineTable(const LineTable &) = delete;
343  const LineTable &operator=(const LineTable &) = delete;
344 };
345 
346 } // namespace lldb_private
347 
348 #endif // LLDB_SYMBOL_LINETABLE_H
A line table entry class.
Definition: LineEntry.h:20
Defines a list of symbol context objects.
A class that represents a running process on the host machine.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t is_start_of_statement
Indicates this entry is the beginning of a statement.
Definition: LineTable.h:289
uint32_t is_terminal_entry
Indicates this entry is that of the first byte after the end of a sequence of target machine instruct...
Definition: LineTable.h:300
RangeDataVector< lldb::addr_t, lldb::addr_t, lldb::addr_t > FileRangeMap
Definition: LineTable.h:207
uint16_t file_idx
The file index into CompileUnit&#39;s file table, or zero if there is no file information.
Definition: LineTable.h:306
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
DumpStyle
Dump styles allow the Address::Dump(Stream *,DumpStyle) const function to display Address contents in...
Definition: Address.h:63
A class that describes a compilation unit.
Definition: CompileUnit.h:37
Entry(lldb::addr_t _file_addr, uint32_t _line, uint16_t _column, uint16_t _file_idx, bool _is_start_of_statement, bool _is_start_of_basic_block, bool _is_prologue_end, bool _is_epilogue_begin, bool _is_terminal_entry)
Definition: LineTable.h:219
lldb_private::Section * a_section
Definition: LineTable.h:311
uint32_t is_epilogue_begin
Indicates this entry is one (of possibly many) where execution should be suspended for an exit breakp...
Definition: LineTable.h:297
lldb_private::RangeVector< lldb::addr_t, lldb::addr_t, 32 > FileAddressRanges
Definition: LineTable.h:189
uint32_t is_start_of_basic_block
Indicates this entry is the beginning of a basic block.
Definition: LineTable.h:291
static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence, const CompilandIndexItem &cci, lldb::addr_t base_addr, uint32_t file_number, const LineFragmentHeader &block, const LineNumberEntry &cur)
uint32_t is_prologue_end
Indicates this entry is one (of possibly many) where execution should be suspended for an entry break...
Definition: LineTable.h:294
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
virtual void Clear()=0
entry_collection m_entries
The collection of line entries in this line table.
Definition: LineTable.h:324
virtual ~LineSequence()=default
A line table class.
Definition: LineTable.h:39
uint16_t column
The column number of the source line, or zero if there is no column information.
Definition: LineTable.h:303
static bool EntryAddressLessThan(const Entry &lhs, const Entry &rhs)
Definition: LineTable.h:278
A section + offset based address class.
Definition: Address.h:59
#define SCALAR_COMPARE(a, b)
uint64_t addr_t
Definition: lldb-types.h:83
An abstract base class used during symbol table creation.
Definition: LineTable.h:24
CompileUnit * m_comp_unit
The compile unit that this line table belongs to.
Definition: LineTable.h:322
lldb::addr_t file_addr
The file address for this line entry.
Definition: LineTable.h:284
static int Compare(const Entry &lhs, const Entry &rhs)
Definition: LineTable.h:245
entry_collection m_entries
The collection of line entries in this sequence.
Definition: LineTable.h:336
const LineSequence & operator=(const LineSequence &)=delete
std::vector< Entry > entry_collection
The collection type for the line entries.
Definition: LineTable.h:319
std::vector< lldb_private::Section * > section_collection
The collection type for the sections.
Definition: LineTable.h:317
uint32_t line
The source line number, or zero if there is no line number information.
Definition: LineTable.h:287