LineTable.h revision 924910ce9f0b85e54a5c5c75cdaebc7607ffe117
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#include <vector>
14
15#include "lldb/lldb-private.h"
16#include "lldb/Symbol/LineEntry.h"
17#include "lldb/Core/ModuleChild.h"
18#include "lldb/Core/Section.h"
19#include "lldb/Core/RangeMap.h"
20
21namespace lldb_private {
22
23//----------------------------------------------------------------------
24/// @class LineSequence LineTable.h "lldb/Symbol/LineTable.h"
25/// @brief An abstract base class used during symbol table creation.
26//----------------------------------------------------------------------
27class LineSequence
28{
29public:
30    LineSequence ();
31
32    virtual
33    ~LineSequence() {}
34
35    virtual void
36    Clear() = 0;
37
38private:
39    DISALLOW_COPY_AND_ASSIGN (LineSequence);
40};
41
42//----------------------------------------------------------------------
43/// @class LineTable LineTable.h "lldb/Symbol/LineTable.h"
44/// @brief A line table class.
45//----------------------------------------------------------------------
46class LineTable
47{
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
78    InsertLineEntry (const lldb::SectionSP& section_sp,
79                     lldb::addr_t section_offset,
80                     uint32_t line,
81                     uint16_t column,
82                     uint16_t file_idx,
83                     bool is_start_of_statement,
84                     bool is_start_of_basic_block,
85                     bool is_prologue_end,
86                     bool is_epilogue_begin,
87                     bool is_terminal_entry);
88
89    // Used to instantiate the LineSequence helper classw
90    LineSequence*
91    CreateLineSequenceContainer ();
92
93    // Append an entry to a caller-provided collection that will later be
94    // inserted in this line table.
95    void
96    AppendLineEntryToSequence (LineSequence* sequence,
97                               const lldb::SectionSP& section_sp,
98                               lldb::addr_t section_offset,
99                               uint32_t line,
100                               uint16_t column,
101                               uint16_t file_idx,
102                               bool is_start_of_statement,
103                               bool is_start_of_basic_block,
104                               bool is_prologue_end,
105                               bool is_epilogue_begin,
106                               bool is_terminal_entry);
107
108    // Insert a sequence of entries into this line table.
109    void
110    InsertSequence (LineSequence* sequence);
111
112    //------------------------------------------------------------------
113    /// Dump all line entries in this line table to the stream \a s.
114    ///
115    /// @param[in] s
116    ///     The stream to which to dump the object descripton.
117    ///
118    /// @param[in] style
119    ///     The display style for the address.
120    ///
121    /// @see Address::DumpStyle
122    //------------------------------------------------------------------
123    void
124    Dump (Stream *s, Target *target,
125          Address::DumpStyle style,
126          Address::DumpStyle fallback_style,
127          bool show_line_ranges);
128
129    void
130    GetDescription (Stream *s,
131                    Target *target,
132                    lldb::DescriptionLevel level);
133
134    //------------------------------------------------------------------
135    /// Find a line entry that contains the section offset address \a
136    /// so_addr.
137    ///
138    /// @param[in] so_addr
139    ///     A section offset address object containing the address we
140    ///     are searching for.
141    ///
142    /// @param[out] line_entry
143    ///     A copy of the line entry that was found if \b true is
144    ///     returned, otherwise \a entry is left unmodified.
145    ///
146    /// @param[out] index_ptr
147    ///     A pointer to a 32 bit integer that will get the actual line
148    ///     entry index if it is not NULL.
149    ///
150    /// @return
151    ///     Returns \b true if \a so_addr is contained in a line entry
152    ///     in this line table, \b false otherwise.
153    //------------------------------------------------------------------
154    bool
155    FindLineEntryByAddress (const Address &so_addr, LineEntry& line_entry, uint32_t *index_ptr = NULL);
156
157    //------------------------------------------------------------------
158    /// Find a line entry index that has a matching file index and
159    /// source line number.
160    ///
161    /// Finds the next line entry that has a matching \a file_idx and
162    /// source line number \a line starting at the \a start_idx entries
163    /// into the line entry collection.
164    ///
165    /// @param[in] start_idx
166    ///     The number of entries to skip when starting the search.
167    ///
168    /// @param[out] file_idx
169    ///     The file index to search for that should be found prior
170    ///     to calling this function using the following functions:
171    ///     CompileUnit::GetSupportFiles()
172    ///     FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
173    ///
174    /// @param[in] line
175    ///     The source line to match.
176    ///
177    /// @param[in] exact
178    ///     If true, match only if you find a line entry exactly matching \a line.
179    ///     If false, return the closest line entry greater than \a line.
180    ///
181    /// @param[out] line_entry
182    ///     A reference to a line entry object that will get a copy of
183    ///     the line entry if \b true is returned, otherwise \a
184    ///     line_entry is left untouched.
185    ///
186    /// @return
187    ///     Returns \b true if a matching line entry is found in this
188    ///     line table, \b false otherwise.
189    ///
190    /// @see CompileUnit::GetSupportFiles()
191    /// @see FileSpecList::FindFileIndex (uint32_t, const FileSpec &) const
192    //------------------------------------------------------------------
193    uint32_t
194    FindLineEntryIndexByFileIndex (uint32_t start_idx,
195                                   uint32_t file_idx,
196                                   uint32_t line,
197                                   bool exact,
198                                   LineEntry* line_entry_ptr);
199
200    uint32_t
201    FindLineEntryIndexByFileIndex (uint32_t start_idx,
202                                   const std::vector<uint32_t> &file_indexes,
203                                   uint32_t line,
204                                   bool exact,
205                                   LineEntry* line_entry_ptr);
206
207    size_t
208    FineLineEntriesForFileIndex (uint32_t file_idx,
209                                 bool append,
210                                 SymbolContextList &sc_list);
211
212    //------------------------------------------------------------------
213    /// Get the line entry from the line table at index \a idx.
214    ///
215    /// @param[in] idx
216    ///     An index into the line table entry collection.
217    ///
218    /// @return
219    ///     A valid line entry if \a idx is a valid index, or an invalid
220    ///     line entry if \a idx is not valid.
221    ///
222    /// @see LineTable::GetSize()
223    /// @see LineEntry::IsValid() const
224    //------------------------------------------------------------------
225    bool
226    GetLineEntryAtIndex(uint32_t idx, LineEntry& line_entry);
227
228    //------------------------------------------------------------------
229    /// Gets the size of the line table in number of line table entries.
230    ///
231    /// @return
232    ///     The number of line table entries in this line table.
233    //------------------------------------------------------------------
234    uint32_t
235    GetSize () const;
236
237    typedef lldb_private::RangeArray<lldb::addr_t, lldb::addr_t, 32> FileAddressRanges;
238
239    //------------------------------------------------------------------
240    /// Gets all contiguous file address ranges for the entire line table.
241    ///
242    /// @param[out] file_ranges
243    ///     A collection of file address ranges that will be filled in
244    ///     by this function.
245    ///
246    /// @param[out] append
247    ///     If \b true, then append to \a file_ranges, otherwise clear
248    ///     \a file_ranges prior to adding any ranges.
249    ///
250    /// @return
251    ///     The number of address ranges added to \a file_ranges
252    //------------------------------------------------------------------
253    size_t
254    GetContiguousFileAddressRanges (FileAddressRanges &file_ranges, bool append);
255
256protected:
257
258    struct Entry
259    {
260        enum { kInvalidSectIdx = UINT32_MAX };
261
262        Entry () :
263            sect_idx (kInvalidSectIdx),
264            sect_offset (0),
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
276        Entry ( uint32_t _sect_idx,
277                lldb::addr_t _sect_offset,
278                uint32_t _line,
279                uint16_t _column,
280                uint16_t _file_idx,
281                bool _is_start_of_statement,
282                bool _is_start_of_basic_block,
283                bool _is_prologue_end,
284                bool _is_epilogue_begin,
285                bool _is_terminal_entry) :
286            sect_idx (_sect_idx),
287            sect_offset (_sect_offset),
288            line (_line),
289            column (_column),
290            file_idx (_file_idx),
291            is_start_of_statement (_is_start_of_statement),
292            is_start_of_basic_block (_is_start_of_basic_block),
293            is_prologue_end (_is_prologue_end),
294            is_epilogue_begin (_is_epilogue_begin),
295            is_terminal_entry (_is_terminal_entry)
296        {
297            // We have reserved 32 bits for the section offset which should
298            // be enough, but if it isn't then we need to make m_section_offset
299            // bigger
300            assert(_sect_offset <= UINT32_MAX);
301        }
302
303        int
304        bsearch_compare (const void *key, const void *arrmem);
305
306        void
307        Clear ()
308        {
309            sect_idx = kInvalidSectIdx;
310            sect_offset = 0;
311            line = 0;
312            column = 0;
313            file_idx = 0;
314            is_start_of_statement = false;
315            is_start_of_basic_block = false;
316            is_prologue_end = false;
317            is_epilogue_begin = false;
318            is_terminal_entry = false;
319        }
320
321        static int
322        Compare (const Entry& lhs, const Entry& rhs)
323        {
324            // Compare the sections before calling
325            #define SCALAR_COMPARE(a,b) if (a < b) return -1; if (a > b) return +1
326            SCALAR_COMPARE (lhs.sect_offset, rhs.sect_offset);
327            SCALAR_COMPARE (lhs.line, rhs.line);
328            SCALAR_COMPARE (lhs.column, rhs.column);
329            SCALAR_COMPARE (lhs.is_start_of_statement, rhs.is_start_of_statement);
330            SCALAR_COMPARE (lhs.is_start_of_basic_block, rhs.is_start_of_basic_block);
331            // rhs and lhs reversed on purpose below.
332            SCALAR_COMPARE (rhs.is_prologue_end, lhs.is_prologue_end);
333            SCALAR_COMPARE (lhs.is_epilogue_begin, rhs.is_epilogue_begin);
334            // rhs and lhs reversed on purpose below.
335            SCALAR_COMPARE (rhs.is_terminal_entry, lhs.is_terminal_entry);
336            SCALAR_COMPARE (lhs.file_idx, rhs.file_idx);
337            #undef SCALAR_COMPARE
338            return 0;
339        }
340
341
342        class LessThanBinaryPredicate
343        {
344        public:
345            LessThanBinaryPredicate(LineTable *line_table);
346            bool operator() (const LineTable::Entry&, const LineTable::Entry&) const;
347        protected:
348            LineTable *m_line_table;
349        };
350
351        static bool EntryAddressLessThan (const Entry& lhs, const Entry& rhs)
352        {
353            if (lhs.sect_idx == rhs.sect_idx)
354                return lhs.sect_offset < rhs.sect_offset;
355            return lhs.sect_idx < rhs.sect_idx;
356        }
357
358        //------------------------------------------------------------------
359        // Member variables.
360        //------------------------------------------------------------------
361        uint32_t    sect_idx;                   ///< The section index for this line entry.
362        uint32_t    sect_offset;                ///< The offset into the section for this line entry.
363        uint32_t    line;                       ///< The source line number, or zero if there is no line number information.
364        uint16_t    column;                     ///< The column number of the source line, or zero if there is no column information.
365        uint16_t    file_idx:11,                ///< The file index into CompileUnit's file table, or zero if there is no file information.
366                    is_start_of_statement:1,    ///< Indicates this entry is the beginning of a statement.
367                    is_start_of_basic_block:1,  ///< Indicates this entry is the beginning of a basic block.
368                    is_prologue_end:1,          ///< Indicates this entry is one (of possibly many) where execution should be suspended for an entry breakpoint of a function.
369                    is_epilogue_begin:1,        ///< Indicates this entry is one (of possibly many) where execution should be suspended for an exit breakpoint of a function.
370                    is_terminal_entry:1;        ///< Indicates this entry is that of the first byte after the end of a sequence of target machine instructions.
371    };
372
373    struct EntrySearchInfo
374    {
375        LineTable* line_table;
376        lldb_private::Section *a_section;
377        Entry *a_entry;
378    };
379
380    //------------------------------------------------------------------
381    // Types
382    //------------------------------------------------------------------
383    typedef std::vector<lldb_private::Section*> section_collection; ///< The collection type for the sections.
384    typedef std::vector<Entry>                  entry_collection;   ///< The collection type for the line entries.
385    //------------------------------------------------------------------
386    // Member variables.
387    //------------------------------------------------------------------
388    CompileUnit* m_comp_unit;   ///< The compile unit that this line table belongs to.
389    SectionList m_section_list; ///< The list of sections that at least one of the line entries exists in.
390    entry_collection m_entries; ///< The collection of line entries in this line table.
391
392    //------------------------------------------------------------------
393    // Helper class
394    //------------------------------------------------------------------
395    class LineSequenceImpl : public LineSequence
396    {
397    public:
398        LineSequenceImpl() :
399            LineSequence()
400        {}
401
402        virtual
403        ~LineSequenceImpl()
404        {}
405
406        virtual void
407        Clear();
408
409        entry_collection m_seq_entries; ///< The collection of line entries in this sequence.
410    };
411
412    bool
413    ConvertEntryAtIndexToLineEntry (uint32_t idx, LineEntry &line_entry);
414
415    lldb_private::Section *
416    GetSectionForEntryIndex (uint32_t idx);
417private:
418    DISALLOW_COPY_AND_ASSIGN (LineTable);
419};
420
421} // namespace lldb_private
422
423#endif  // liblldb_LineTable_h_
424