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