LineTable.h revision 254729
1169691Skan//===-- LineTable.h ---------------------------------------------*- C++ -*-===//
2169691Skan//
3169691Skan//                     The LLVM Compiler Infrastructure
4169691Skan//
5169691Skan// This file is distributed under the University of Illinois Open Source
6169691Skan// License. See LICENSE.TXT for details.
7169691Skan//
8169691Skan//===----------------------------------------------------------------------===//
9169691Skan
10169691Skan#ifndef liblldb_LineTable_h_
11169691Skan#define liblldb_LineTable_h_
12169691Skan
13169691Skan#include <vector>
14169691Skan
15169691Skan#include "lldb/lldb-private.h"
16169691Skan#include "lldb/Symbol/LineEntry.h"
17169691Skan#include "lldb/Core/ModuleChild.h"
18169691Skan#include "lldb/Core/Section.h"
19169691Skan#include "lldb/Core/RangeMap.h"
20169691Skan
21169691Skannamespace lldb_private {
22169691Skan
23169691Skan//----------------------------------------------------------------------
24169691Skan/// @class LineSequence LineTable.h "lldb/Symbol/LineTable.h"
25169691Skan/// @brief An abstract base class used during symbol table creation.
26169691Skan//----------------------------------------------------------------------
27169691Skanclass LineSequence
28169691Skan{
29169691Skanpublic:
30169691Skan    LineSequence ();
31169691Skan
32169691Skan    virtual
33169691Skan    ~LineSequence() {}
34169691Skan
35169691Skan    virtual void
36169691Skan    Clear() = 0;
37169691Skan
38169691Skanprivate:
39169691Skan    DISALLOW_COPY_AND_ASSIGN (LineSequence);
40169691Skan};
41169691Skan
42169691Skan//----------------------------------------------------------------------
43169691Skan/// @class LineTable LineTable.h "lldb/Symbol/LineTable.h"
44169691Skan/// @brief A line table class.
45169691Skan//----------------------------------------------------------------------
46169691Skanclass LineTable
47169691Skan{
48169691Skanpublic:
49169691Skan    //------------------------------------------------------------------
50169691Skan    /// Construct with compile unit.
51169691Skan    ///
52169691Skan    /// @param[in] comp_unit
53169691Skan    ///     The compile unit to which this line table belongs.
54169691Skan    //------------------------------------------------------------------
55169691Skan    LineTable (CompileUnit* comp_unit);
56169691Skan
57169691Skan    //------------------------------------------------------------------
58169691Skan    /// Destructor.
59169691Skan    //------------------------------------------------------------------
60169691Skan    ~LineTable ();
61169691Skan
62169691Skan    //------------------------------------------------------------------
63169691Skan    /// Adds a new line entry to this line table.
64169691Skan    ///
65169691Skan    /// All line entries are maintained in file address order.
66169691Skan    ///
67169691Skan    /// @param[in] line_entry
68169691Skan    ///     A const reference to a new line_entry to add to this line
69169691Skan    ///     table.
70169691Skan    ///
71169691Skan    /// @see Address::DumpStyle
72169691Skan    //------------------------------------------------------------------
73169691Skan//  void
74169691Skan//  AddLineEntry (const LineEntry& line_entry);
75169691Skan
76169691Skan    // Called when you can't guarantee the addresses are in increasing order
77169691Skan    void
78169691Skan    InsertLineEntry (lldb::addr_t file_addr,
79169691Skan                     uint32_t line,
80169691Skan                     uint16_t column,
81169691Skan                     uint16_t file_idx,
82169691Skan                     bool is_start_of_statement,
83169691Skan                     bool is_start_of_basic_block,
84169691Skan                     bool is_prologue_end,
85169691Skan                     bool is_epilogue_begin,
86169691Skan                     bool is_terminal_entry);
87169691Skan
88169691Skan    // Used to instantiate the LineSequence helper classw
89169691Skan    LineSequence*
90169691Skan    CreateLineSequenceContainer ();
91169691Skan
92169691Skan    // Append an entry to a caller-provided collection that will later be
93169691Skan    // inserted in this line table.
94169691Skan    void
95169691Skan    AppendLineEntryToSequence (LineSequence* sequence,
96169691Skan                               lldb::addr_t file_addr,
97169691Skan                               uint32_t line,
98169691Skan                               uint16_t column,
99169691Skan                               uint16_t file_idx,
100169691Skan                               bool is_start_of_statement,
101169691Skan                               bool is_start_of_basic_block,
102169691Skan                               bool is_prologue_end,
103169691Skan                               bool is_epilogue_begin,
104169691Skan                               bool is_terminal_entry);
105169691Skan
106169691Skan    // Insert a sequence of entries into this line table.
107169691Skan    void
108169691Skan    InsertSequence (LineSequence* sequence);
109169691Skan
110169691Skan    //------------------------------------------------------------------
111169691Skan    /// Dump all line entries in this line table to the stream \a s.
112169691Skan    ///
113169691Skan    /// @param[in] s
114169691Skan    ///     The stream to which to dump the object descripton.
115169691Skan    ///
116169691Skan    /// @param[in] style
117169691Skan    ///     The display style for the address.
118169691Skan    ///
119169691Skan    /// @see Address::DumpStyle
120169691Skan    //------------------------------------------------------------------
121169691Skan    void
122169691Skan    Dump (Stream *s, Target *target,
123169691Skan          Address::DumpStyle style,
124169691Skan          Address::DumpStyle fallback_style,
125169691Skan          bool show_line_ranges);
126169691Skan
127169691Skan    void
128169691Skan    GetDescription (Stream *s,
129169691Skan                    Target *target,
130169691Skan                    lldb::DescriptionLevel level);
131169691Skan
132169691Skan    //------------------------------------------------------------------
133169691Skan    /// Find a line entry that contains the section offset address \a
134169691Skan    /// so_addr.
135169691Skan    ///
136169691Skan    /// @param[in] so_addr
137169691Skan    ///     A section offset address object containing the address we
138169691Skan    ///     are searching for.
139169691Skan    ///
140169691Skan    /// @param[out] line_entry
141169691Skan    ///     A copy of the line entry that was found if \b true is
142169691Skan    ///     returned, otherwise \a entry is left unmodified.
143169691Skan    ///
144169691Skan    /// @param[out] index_ptr
145169691Skan    ///     A pointer to a 32 bit integer that will get the actual line
146169691Skan    ///     entry index if it is not NULL.
147169691Skan    ///
148169691Skan    /// @return
149169691Skan    ///     Returns \b true if \a so_addr is contained in a line entry
150169691Skan    ///     in this line table, \b false otherwise.
151169691Skan    //------------------------------------------------------------------
152169691Skan    bool
153169691Skan    FindLineEntryByAddress (const Address &so_addr, LineEntry& line_entry, uint32_t *index_ptr = NULL);
154169691Skan
155169691Skan    //------------------------------------------------------------------
156169691Skan    /// Find a line entry index that has a matching file index and
157169691Skan    /// source line number.
158169691Skan    ///
159169691Skan    /// Finds the next line entry that has a matching \a file_idx and
160169691Skan    /// source line number \a line starting at the \a start_idx entries
161169691Skan    /// into the line entry collection.
162169691Skan    ///
163169691Skan    /// @param[in] start_idx
164169691Skan    ///     The number of entries to skip when starting the search.
165169691Skan    ///
166169691Skan    /// @param[out] file_idx
167169691Skan    ///     The file index to search for that should be found prior
168169691Skan    ///     to calling this function using the following functions:
169169691Skan    ///     CompileUnit::GetSupportFiles()
170169691Skan    ///     FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
171169691Skan    ///
172169691Skan    /// @param[in] line
173169691Skan    ///     The source line to match.
174169691Skan    ///
175169691Skan    /// @param[in] exact
176169691Skan    ///     If true, match only if you find a line entry exactly matching \a line.
177169691Skan    ///     If false, return the closest line entry greater than \a line.
178169691Skan    ///
179169691Skan    /// @param[out] line_entry
180169691Skan    ///     A reference to a line entry object that will get a copy of
181169691Skan    ///     the line entry if \b true is returned, otherwise \a
182169691Skan    ///     line_entry is left untouched.
183169691Skan    ///
184169691Skan    /// @return
185169691Skan    ///     Returns \b true if a matching line entry is found in this
186169691Skan    ///     line table, \b false otherwise.
187169691Skan    ///
188169691Skan    /// @see CompileUnit::GetSupportFiles()
189169691Skan    /// @see FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
190169691Skan    //------------------------------------------------------------------
191169691Skan    uint32_t
192169691Skan    FindLineEntryIndexByFileIndex (uint32_t start_idx,
193169691Skan                                   uint32_t file_idx,
194169691Skan                                   uint32_t line,
195169691Skan                                   bool exact,
196169691Skan                                   LineEntry* line_entry_ptr);
197169691Skan
198169691Skan    uint32_t
199169691Skan    FindLineEntryIndexByFileIndex (uint32_t start_idx,
200169691Skan                                   const std::vector<uint32_t> &file_indexes,
201169691Skan                                   uint32_t line,
202169691Skan                                   bool exact,
203169691Skan                                   LineEntry* line_entry_ptr);
204169691Skan
205169691Skan    size_t
206169691Skan    FineLineEntriesForFileIndex (uint32_t file_idx,
207169691Skan                                 bool append,
208169691Skan                                 SymbolContextList &sc_list);
209169691Skan
210169691Skan    //------------------------------------------------------------------
211169691Skan    /// Get the line entry from the line table at index \a idx.
212169691Skan    ///
213169691Skan    /// @param[in] idx
214169691Skan    ///     An index into the line table entry collection.
215169691Skan    ///
216169691Skan    /// @return
217169691Skan    ///     A valid line entry if \a idx is a valid index, or an invalid
218169691Skan    ///     line entry if \a idx is not valid.
219169691Skan    ///
220169691Skan    /// @see LineTable::GetSize()
221169691Skan    /// @see LineEntry::IsValid() const
222169691Skan    //------------------------------------------------------------------
223    bool
224    GetLineEntryAtIndex(uint32_t idx, LineEntry& line_entry);
225
226    //------------------------------------------------------------------
227    /// Gets the size of the line table in number of line table entries.
228    ///
229    /// @return
230    ///     The number of line table entries in this line table.
231    //------------------------------------------------------------------
232    uint32_t
233    GetSize () const;
234
235    typedef lldb_private::RangeArray<lldb::addr_t, lldb::addr_t, 32> FileAddressRanges;
236
237    //------------------------------------------------------------------
238    /// Gets all contiguous file address ranges for the entire line table.
239    ///
240    /// @param[out] file_ranges
241    ///     A collection of file address ranges that will be filled in
242    ///     by this function.
243    ///
244    /// @param[out] append
245    ///     If \b true, then append to \a file_ranges, otherwise clear
246    ///     \a file_ranges prior to adding any ranges.
247    ///
248    /// @return
249    ///     The number of address ranges added to \a file_ranges
250    //------------------------------------------------------------------
251    size_t
252    GetContiguousFileAddressRanges (FileAddressRanges &file_ranges, bool append);
253
254    //------------------------------------------------------------------
255    /// Given a file range link map, relink the current line table
256    /// and return a fixed up line table.
257    ///
258    /// @param[out] file_range_map
259    ///     A collection of file ranges that maps to new file ranges
260    ///     that will be used when linking the line table.
261    ///
262    /// @return
263    ///     A new line table if at least one line table entry was able
264    ///     to be mapped.
265    //------------------------------------------------------------------
266    typedef RangeDataVector<lldb::addr_t, lldb::addr_t, lldb::addr_t> FileRangeMap;
267
268    LineTable *
269    LinkLineTable (const FileRangeMap &file_range_map);
270
271protected:
272
273    struct Entry
274    {
275        Entry () :
276            file_addr (LLDB_INVALID_ADDRESS),
277            line (0),
278            column (0),
279            file_idx (0),
280            is_start_of_statement (false),
281            is_start_of_basic_block (false),
282            is_prologue_end (false),
283            is_epilogue_begin (false),
284            is_terminal_entry (false)
285        {
286        }
287
288        Entry ( lldb::addr_t _file_addr,
289                uint32_t _line,
290                uint16_t _column,
291                uint16_t _file_idx,
292                bool _is_start_of_statement,
293                bool _is_start_of_basic_block,
294                bool _is_prologue_end,
295                bool _is_epilogue_begin,
296                bool _is_terminal_entry) :
297            file_addr (_file_addr),
298            line (_line),
299            column (_column),
300            file_idx (_file_idx),
301            is_start_of_statement (_is_start_of_statement),
302            is_start_of_basic_block (_is_start_of_basic_block),
303            is_prologue_end (_is_prologue_end),
304            is_epilogue_begin (_is_epilogue_begin),
305            is_terminal_entry (_is_terminal_entry)
306        {
307        }
308
309        int
310        bsearch_compare (const void *key, const void *arrmem);
311
312        void
313        Clear ()
314        {
315            file_addr = LLDB_INVALID_ADDRESS;
316            line = 0;
317            column = 0;
318            file_idx = 0;
319            is_start_of_statement = false;
320            is_start_of_basic_block = false;
321            is_prologue_end = false;
322            is_epilogue_begin = false;
323            is_terminal_entry = false;
324        }
325
326        static int
327        Compare (const Entry& lhs, const Entry& rhs)
328        {
329            // Compare the sections before calling
330            #define SCALAR_COMPARE(a,b) if (a < b) return -1; if (a > b) return +1
331            SCALAR_COMPARE (lhs.file_addr, rhs.file_addr);
332            SCALAR_COMPARE (lhs.line, rhs.line);
333            SCALAR_COMPARE (lhs.column, rhs.column);
334            SCALAR_COMPARE (lhs.is_start_of_statement, rhs.is_start_of_statement);
335            SCALAR_COMPARE (lhs.is_start_of_basic_block, rhs.is_start_of_basic_block);
336            // rhs and lhs reversed on purpose below.
337            SCALAR_COMPARE (rhs.is_prologue_end, lhs.is_prologue_end);
338            SCALAR_COMPARE (lhs.is_epilogue_begin, rhs.is_epilogue_begin);
339            // rhs and lhs reversed on purpose below.
340            SCALAR_COMPARE (rhs.is_terminal_entry, lhs.is_terminal_entry);
341            SCALAR_COMPARE (lhs.file_idx, rhs.file_idx);
342            #undef SCALAR_COMPARE
343            return 0;
344        }
345
346
347        class LessThanBinaryPredicate
348        {
349        public:
350            LessThanBinaryPredicate(LineTable *line_table);
351            bool operator() (const LineTable::Entry&, const LineTable::Entry&) const;
352        protected:
353            LineTable *m_line_table;
354        };
355
356        static bool EntryAddressLessThan (const Entry& lhs, const Entry& rhs)
357        {
358            return lhs.file_addr < rhs.file_addr;
359        }
360
361        //------------------------------------------------------------------
362        // Member variables.
363        //------------------------------------------------------------------
364        lldb::addr_t file_addr;                 ///< The file address for this line entry
365        uint32_t    line;                       ///< The source line number, or zero if there is no line number information.
366        uint16_t    column;                     ///< The column number of the source line, or zero if there is no column information.
367        uint16_t    file_idx:11,                ///< The file index into CompileUnit's file table, or zero if there is no file information.
368                    is_start_of_statement:1,    ///< Indicates this entry is the beginning of a statement.
369                    is_start_of_basic_block:1,  ///< Indicates this entry is the beginning of a basic block.
370                    is_prologue_end:1,          ///< Indicates this entry is one (of possibly many) where execution should be suspended for an entry breakpoint of a function.
371                    is_epilogue_begin:1,        ///< Indicates this entry is one (of possibly many) where execution should be suspended for an exit breakpoint of a function.
372                    is_terminal_entry:1;        ///< Indicates this entry is that of the first byte after the end of a sequence of target machine instructions.
373    };
374
375    struct EntrySearchInfo
376    {
377        LineTable* line_table;
378        lldb_private::Section *a_section;
379        Entry *a_entry;
380    };
381
382    //------------------------------------------------------------------
383    // Types
384    //------------------------------------------------------------------
385    typedef std::vector<lldb_private::Section*> section_collection; ///< The collection type for the sections.
386    typedef std::vector<Entry>                  entry_collection;   ///< The collection type for the line entries.
387    //------------------------------------------------------------------
388    // Member variables.
389    //------------------------------------------------------------------
390    CompileUnit* m_comp_unit;   ///< The compile unit that this line table belongs to.
391    entry_collection m_entries; ///< The collection of line entries in this line table.
392
393    //------------------------------------------------------------------
394    // Helper class
395    //------------------------------------------------------------------
396    class LineSequenceImpl : public LineSequence
397    {
398    public:
399        LineSequenceImpl() :
400            LineSequence()
401        {}
402
403        virtual
404        ~LineSequenceImpl()
405        {}
406
407        virtual void
408        Clear();
409
410        entry_collection m_entries; ///< The collection of line entries in this sequence.
411    };
412
413    bool
414    ConvertEntryAtIndexToLineEntry (uint32_t idx, LineEntry &line_entry);
415
416private:
417    DISALLOW_COPY_AND_ASSIGN (LineTable);
418};
419
420} // namespace lldb_private
421
422#endif  // liblldb_LineTable_h_
423