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" An abstract base
29 /// 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 /// 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 so_addr.
116 /// @param[in] so_addr
117 /// A section offset address object containing the address we
118 /// are searching for.
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.
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.
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);
135 //------------------------------------------------------------------
136 /// Find a line entry index that has a matching file index and source line
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.
143 /// @param[in] start_idx
144 /// The number of entries to skip when starting the search.
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
153 /// The source line to match.
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.
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.
165 /// Returns \b true if a matching line entry is found in this
166 /// line table, \b false otherwise.
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);
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);
179 size_t FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
180 SymbolContextList &sc_list);
182 //------------------------------------------------------------------
183 /// Get the line entry from the line table at index \a idx.
186 /// An index into the line table entry collection.
189 /// A valid line entry if \a idx is a valid index, or an invalid
190 /// line entry if \a idx is not valid.
192 /// @see LineTable::GetSize()
193 /// @see LineEntry::IsValid() const
194 //------------------------------------------------------------------
195 bool GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry);
197 //------------------------------------------------------------------
198 /// Gets the size of the line table in number of line table entries.
201 /// The number of line table entries in this line table.
202 //------------------------------------------------------------------
203 uint32_t GetSize() const;
205 typedef lldb_private::RangeArray<lldb::addr_t, lldb::addr_t, 32>
208 //------------------------------------------------------------------
209 /// Gets all contiguous file address ranges for the entire line table.
211 /// @param[out] file_ranges
212 /// A collection of file address ranges that will be filled in
213 /// by this function.
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.
220 /// The number of address ranges added to \a file_ranges
221 //------------------------------------------------------------------
222 size_t GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
225 //------------------------------------------------------------------
226 /// Given a file range link map, relink the current line table and return a
227 /// fixed up line table.
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.
234 /// A new line table if at least one line table entry was able
236 //------------------------------------------------------------------
237 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t>
240 LineTable *LinkLineTable(const FileRangeMap &file_range_map);
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) {}
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) {}
261 int bsearch_compare(const void *key, const void *arrmem);
264 file_addr = LLDB_INVALID_ADDRESS;
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;
275 static int Compare(const Entry &lhs, const Entry &rhs) {
276 // Compare the sections before calling
277 #define SCALAR_COMPARE(a, b) \
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
297 class LessThanBinaryPredicate {
299 LessThanBinaryPredicate(LineTable *line_table);
300 bool operator()(const LineTable::Entry &, const LineTable::Entry &) const;
303 LineTable *m_line_table;
306 static bool EntryAddressLessThan(const Entry &lhs, const Entry &rhs) {
307 return lhs.file_addr < rhs.file_addr;
310 //------------------------------------------------------------------
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
322 is_start_of_basic_block : 1, ///< Indicates this entry is the beginning
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.
335 struct EntrySearchInfo {
336 LineTable *line_table;
337 lldb_private::Section *a_section;
341 //------------------------------------------------------------------
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 //------------------------------------------------------------------
350 //------------------------------------------------------------------
352 *m_comp_unit; ///< The compile unit that this line table belongs to.
354 m_entries; ///< The collection of line entries in this line table.
356 //------------------------------------------------------------------
358 //------------------------------------------------------------------
359 class LineSequenceImpl : public LineSequence {
361 LineSequenceImpl() = default;
363 ~LineSequenceImpl() override = default;
365 void Clear() override;
368 m_entries; ///< The collection of line entries in this sequence.
371 bool ConvertEntryAtIndexToLineEntry(uint32_t idx, LineEntry &line_entry);
374 DISALLOW_COPY_AND_ASSIGN(LineTable);
377 } // namespace lldb_private
379 #endif // liblldb_LineTable_h_