1 //===-- LineTable.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_LineTable_h_
11 #define liblldb_LineTable_h_
17 // Other libraries and framework 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"
25 namespace lldb_private {
27 //----------------------------------------------------------------------
28 /// @class LineSequence LineTable.h "lldb/Symbol/LineTable.h"
29 /// @brief An abstract base class used during symbol table creation.
30 //----------------------------------------------------------------------
35 virtual ~LineSequence() = default;
37 virtual void Clear() = 0;
40 DISALLOW_COPY_AND_ASSIGN(LineSequence);
43 //----------------------------------------------------------------------
44 /// @class LineTable LineTable.h "lldb/Symbol/LineTable.h"
45 /// @brief A line table class.
46 //----------------------------------------------------------------------
49 //------------------------------------------------------------------
50 /// Construct with compile unit.
52 /// @param[in] comp_unit
53 /// The compile unit to which this line table belongs.
54 //------------------------------------------------------------------
55 LineTable(CompileUnit *comp_unit);
57 //------------------------------------------------------------------
59 //------------------------------------------------------------------
62 //------------------------------------------------------------------
63 /// Adds a new line entry to this line table.
65 /// All line entries are maintained in file address order.
67 /// @param[in] line_entry
68 /// A const reference to a new line_entry to add to this line
71 /// @see Address::DumpStyle
72 //------------------------------------------------------------------
74 // AddLineEntry (const LineEntry& line_entry);
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);
82 // Used to instantiate the LineSequence helper class
83 LineSequence *CreateLineSequenceContainer();
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);
94 // Insert a sequence of entries into this line table.
95 void InsertSequence(LineSequence *sequence);
97 //------------------------------------------------------------------
98 /// Dump all line entries in this line table to the stream \a s.
101 /// The stream to which to dump the object description.
104 /// The display style for the address.
106 /// @see Address::DumpStyle
107 //------------------------------------------------------------------
108 void Dump(Stream *s, Target *target, Address::DumpStyle style,
109 Address::DumpStyle fallback_style, bool show_line_ranges);
111 void GetDescription(Stream *s, Target *target, lldb::DescriptionLevel level);
113 //------------------------------------------------------------------
114 /// Find a line entry that contains the section offset address \a
117 /// @param[in] so_addr
118 /// A section offset address object containing the address we
119 /// are searching for.
121 /// @param[out] line_entry
122 /// A copy of the line entry that was found if \b true is
123 /// returned, otherwise \a entry is left unmodified.
125 /// @param[out] index_ptr
126 /// A pointer to a 32 bit integer that will get the actual line
127 /// entry index if it is not nullptr.
130 /// Returns \b true if \a so_addr is contained in a line entry
131 /// in this line table, \b false otherwise.
132 //------------------------------------------------------------------
133 bool FindLineEntryByAddress(const Address &so_addr, LineEntry &line_entry,
134 uint32_t *index_ptr = nullptr);
136 //------------------------------------------------------------------
137 /// Find a line entry index that has a matching file index and
138 /// source line number.
140 /// Finds the next line entry that has a matching \a file_idx and
141 /// source line number \a line starting at the \a start_idx entries
142 /// into the line entry collection.
144 /// @param[in] start_idx
145 /// The number of entries to skip when starting the search.
147 /// @param[out] file_idx
148 /// The file index to search for that should be found prior
149 /// to calling this function using the following functions:
150 /// CompileUnit::GetSupportFiles()
151 /// FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
154 /// The source line to match.
157 /// If true, match only if you find a line entry exactly matching \a line.
158 /// If false, return the closest line entry greater than \a line.
160 /// @param[out] line_entry
161 /// A reference to a line entry object that will get a copy of
162 /// the line entry if \b true is returned, otherwise \a
163 /// line_entry is left untouched.
166 /// Returns \b true if a matching line entry is found in this
167 /// line table, \b false otherwise.
169 /// @see CompileUnit::GetSupportFiles()
170 /// @see FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
171 //------------------------------------------------------------------
172 uint32_t FindLineEntryIndexByFileIndex(uint32_t start_idx, uint32_t file_idx,
173 uint32_t line, bool exact,
174 LineEntry *line_entry_ptr);
176 uint32_t FindLineEntryIndexByFileIndex(
177 uint32_t start_idx, const std::vector<uint32_t> &file_indexes,
178 uint32_t line, bool exact, LineEntry *line_entry_ptr);
180 size_t FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
181 SymbolContextList &sc_list);
183 //------------------------------------------------------------------
184 /// Get the line entry from the line table at index \a idx.
187 /// An index into the line table entry collection.
190 /// A valid line entry if \a idx is a valid index, or an invalid
191 /// line entry if \a idx is not valid.
193 /// @see LineTable::GetSize()
194 /// @see LineEntry::IsValid() const
195 //------------------------------------------------------------------
196 bool GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry);
198 //------------------------------------------------------------------
199 /// Gets the size of the line table in number of line table entries.
202 /// The number of line table entries in this line table.
203 //------------------------------------------------------------------
204 uint32_t GetSize() const;
206 typedef lldb_private::RangeArray<lldb::addr_t, lldb::addr_t, 32>
209 //------------------------------------------------------------------
210 /// Gets all contiguous file address ranges for the entire line table.
212 /// @param[out] file_ranges
213 /// A collection of file address ranges that will be filled in
214 /// by this function.
216 /// @param[out] append
217 /// If \b true, then append to \a file_ranges, otherwise clear
218 /// \a file_ranges prior to adding any ranges.
221 /// The number of address ranges added to \a file_ranges
222 //------------------------------------------------------------------
223 size_t GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
226 //------------------------------------------------------------------
227 /// Given a file range link map, relink the current line table
228 /// and return a fixed up line table.
230 /// @param[out] file_range_map
231 /// A collection of file ranges that maps to new file ranges
232 /// that will be used when linking the line table.
235 /// A new line table if at least one line table entry was able
237 //------------------------------------------------------------------
238 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t>
241 LineTable *LinkLineTable(const FileRangeMap &file_range_map);
246 : file_addr(LLDB_INVALID_ADDRESS), line(0), column(0), file_idx(0),
247 is_start_of_statement(false), is_start_of_basic_block(false),
248 is_prologue_end(false), is_epilogue_begin(false),
249 is_terminal_entry(false) {}
251 Entry(lldb::addr_t _file_addr, uint32_t _line, uint16_t _column,
252 uint16_t _file_idx, bool _is_start_of_statement,
253 bool _is_start_of_basic_block, bool _is_prologue_end,
254 bool _is_epilogue_begin, bool _is_terminal_entry)
255 : file_addr(_file_addr), line(_line), column(_column),
256 file_idx(_file_idx), is_start_of_statement(_is_start_of_statement),
257 is_start_of_basic_block(_is_start_of_basic_block),
258 is_prologue_end(_is_prologue_end),
259 is_epilogue_begin(_is_epilogue_begin),
260 is_terminal_entry(_is_terminal_entry) {}
262 int bsearch_compare(const void *key, const void *arrmem);
265 file_addr = LLDB_INVALID_ADDRESS;
269 is_start_of_statement = false;
270 is_start_of_basic_block = false;
271 is_prologue_end = false;
272 is_epilogue_begin = false;
273 is_terminal_entry = false;
276 static int Compare(const Entry &lhs, const Entry &rhs) {
277 // Compare the sections before calling
278 #define SCALAR_COMPARE(a, b) \
283 SCALAR_COMPARE(lhs.file_addr, rhs.file_addr);
284 SCALAR_COMPARE(lhs.line, rhs.line);
285 SCALAR_COMPARE(lhs.column, rhs.column);
286 SCALAR_COMPARE(lhs.is_start_of_statement, rhs.is_start_of_statement);
287 SCALAR_COMPARE(lhs.is_start_of_basic_block, rhs.is_start_of_basic_block);
288 // rhs and lhs reversed on purpose below.
289 SCALAR_COMPARE(rhs.is_prologue_end, lhs.is_prologue_end);
290 SCALAR_COMPARE(lhs.is_epilogue_begin, rhs.is_epilogue_begin);
291 // rhs and lhs reversed on purpose below.
292 SCALAR_COMPARE(rhs.is_terminal_entry, lhs.is_terminal_entry);
293 SCALAR_COMPARE(lhs.file_idx, rhs.file_idx);
294 #undef SCALAR_COMPARE
298 class LessThanBinaryPredicate {
300 LessThanBinaryPredicate(LineTable *line_table);
301 bool operator()(const LineTable::Entry &, const LineTable::Entry &) const;
304 LineTable *m_line_table;
307 static bool EntryAddressLessThan(const Entry &lhs, const Entry &rhs) {
308 return lhs.file_addr < rhs.file_addr;
311 //------------------------------------------------------------------
313 //------------------------------------------------------------------
314 lldb::addr_t file_addr; ///< The file address for this line entry
315 uint32_t line; ///< The source line number, or zero if there is no line
316 ///number information.
317 uint16_t column; ///< The column number of the source line, or zero if there
318 ///is no column information.
319 uint16_t file_idx : 11, ///< The file index into CompileUnit's file table,
320 ///or zero if there is no file information.
321 is_start_of_statement : 1, ///< Indicates this entry is the beginning of
323 is_start_of_basic_block : 1, ///< Indicates this entry is the beginning
325 is_prologue_end : 1, ///< Indicates this entry is one (of possibly many)
326 ///where execution should be suspended for an entry
327 ///breakpoint of a function.
328 is_epilogue_begin : 1, ///< Indicates this entry is one (of possibly
329 ///many) where execution should be suspended for
330 ///an exit breakpoint of a function.
331 is_terminal_entry : 1; ///< Indicates this entry is that of the first
332 ///byte after the end of a sequence of target
333 ///machine instructions.
336 struct EntrySearchInfo {
337 LineTable *line_table;
338 lldb_private::Section *a_section;
342 //------------------------------------------------------------------
344 //------------------------------------------------------------------
345 typedef std::vector<lldb_private::Section *>
346 section_collection; ///< The collection type for the sections.
347 typedef std::vector<Entry>
348 entry_collection; ///< The collection type for the line entries.
349 //------------------------------------------------------------------
351 //------------------------------------------------------------------
353 *m_comp_unit; ///< The compile unit that this line table belongs to.
355 m_entries; ///< The collection of line entries in this line table.
357 //------------------------------------------------------------------
359 //------------------------------------------------------------------
360 class LineSequenceImpl : public LineSequence {
362 LineSequenceImpl() = default;
364 ~LineSequenceImpl() override = default;
366 void Clear() override;
369 m_entries; ///< The collection of line entries in this sequence.
372 bool ConvertEntryAtIndexToLineEntry(uint32_t idx, LineEntry &line_entry);
375 DISALLOW_COPY_AND_ASSIGN(LineTable);
378 } // namespace lldb_private
380 #endif // liblldb_LineTable_h_