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