DWARFCompileUnit.h revision c5b3f5025871a8f359c08f242894af00178d08dd
1//===-- DWARFCompileUnit.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 SymbolFileDWARF_DWARFCompileUnit_h_
11#define SymbolFileDWARF_DWARFCompileUnit_h_
12
13#include "DWARFDebugInfoEntry.h"
14#include "SymbolFileDWARF.h"
15
16class NameToDIE;
17
18class DWARFCompileUnit
19{
20public:
21    DWARFCompileUnit(SymbolFileDWARF* dwarf2Data);
22
23    bool        Extract(const lldb_private::DataExtractor &debug_info, uint32_t* offset_ptr);
24    dw_offset_t Extract(dw_offset_t offset, const lldb_private::DataExtractor& debug_info_data, const DWARFAbbreviationDeclarationSet* abbrevs);
25    size_t      ExtractDIEsIfNeeded (bool cu_die_only);
26    bool        LookupAddress(
27                    const dw_addr_t address,
28                    DWARFDebugInfoEntry** function_die,
29                    DWARFDebugInfoEntry** block_die);
30
31    size_t      AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& matching_dies, uint32_t depth = UINT32_MAX) const;
32    void        Clear();
33    bool        Verify(lldb_private::Stream *s) const;
34    void        Dump(lldb_private::Stream *s) const;
35    dw_offset_t GetOffset() const { return m_offset; }
36    uint32_t    Size() const { return 11; /* Size in bytes of the compile unit header */ }
37    bool        ContainsDIEOffset(dw_offset_t die_offset) const { return die_offset >= GetFirstDIEOffset() && die_offset < GetNextCompileUnitOffset(); }
38    dw_offset_t GetFirstDIEOffset() const { return m_offset + Size(); }
39    dw_offset_t GetNextCompileUnitOffset() const { return m_offset + m_length + 4; }
40    size_t      GetDebugInfoSize() const { return m_length + 4 - Size(); /* Size in bytes of the .debug_info data associated with this compile unit. */ }
41    uint32_t    GetLength() const { return m_length; }
42    uint16_t    GetVersion() const { return m_version; }
43    const DWARFAbbreviationDeclarationSet*  GetAbbreviations() const { return m_abbrevs; }
44    dw_offset_t GetAbbrevOffset() const;
45    uint8_t     GetAddressByteSize() const { return m_addr_size; }
46    dw_addr_t   GetBaseAddress() const { return m_base_addr; }
47    void        ClearDIEs(bool keep_compile_unit_die);
48    void        BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data,
49                                        DWARFDebugAranges* debug_aranges,
50                                        bool clear_dies_if_already_not_parsed);
51
52    void
53    SetBaseAddress(dw_addr_t base_addr)
54    {
55        m_base_addr = base_addr;
56    }
57
58    const DWARFDebugInfoEntry*
59    GetCompileUnitDIEOnly()
60    {
61        ExtractDIEsIfNeeded (true);
62        if (m_die_array.empty())
63            return NULL;
64        return &m_die_array[0];
65    }
66
67    const DWARFDebugInfoEntry*
68    DIE()
69    {
70        ExtractDIEsIfNeeded (false);
71        if (m_die_array.empty())
72            return NULL;
73        return &m_die_array[0];
74    }
75
76    void
77    AddDIE (DWARFDebugInfoEntry& die)
78    {
79        // The average bytes per DIE entry has been seen to be
80        // around 14-20 so lets pre-reserve half of that since
81        // we are now stripping the NULL tags.
82
83        // Only reserve the memory if we are adding children of
84        // the main compile unit DIE. The compile unit DIE is always
85        // the first entry, so if our size is 1, then we are adding
86        // the first compile unit child DIE and should reserve
87        // the memory.
88        if (m_die_array.empty())
89            m_die_array.reserve(GetDebugInfoSize() / 24);
90        m_die_array.push_back(die);
91    }
92
93    DWARFDebugInfoEntry*
94    GetDIEAtIndexUnchecked (uint32_t idx)
95    {
96        return &m_die_array[idx];
97    }
98
99    DWARFDebugInfoEntry*
100    GetDIEPtr (dw_offset_t die_offset);
101
102    const DWARFDebugInfoEntry*
103    GetDIEPtrContainingOffset (dw_offset_t die_offset);
104
105    static uint8_t
106    GetAddressByteSize(const DWARFCompileUnit* cu);
107
108    static uint8_t
109    GetDefaultAddressSize();
110
111    static void
112    SetDefaultAddressSize(uint8_t addr_size);
113
114    void *
115    GetUserData() const
116    {
117        return m_user_data;
118    }
119
120    void
121    SetUserData(void *d)
122    {
123        m_user_data = d;
124    }
125
126
127//    void
128//    AddGlobalDIEByIndex (uint32_t die_idx);
129//
130//    void
131//    AddGlobal (const DWARFDebugInfoEntry* die);
132//
133    void
134    Index (const uint32_t cu_idx,
135           NameToDIE& func_basenames,
136           NameToDIE& func_fullnames,
137           NameToDIE& func_methods,
138           NameToDIE& func_selectors,
139           NameToDIE& objc_class_selectors,
140           NameToDIE& globals,
141           NameToDIE& types,
142           NameToDIE& namespaces);
143
144    const DWARFDebugAranges &
145    GetFunctionAranges ();
146
147protected:
148    SymbolFileDWARF*    m_dwarf2Data;
149    const DWARFAbbreviationDeclarationSet *m_abbrevs;
150    void *              m_user_data;
151    DWARFDebugInfoEntry::collection m_die_array;    // The compile unit debug information entry item
152    std::auto_ptr<DWARFDebugAranges> m_func_aranges_ap;   // A table similar to the .debug_aranges table, but this one points to the exact DW_TAG_subprogram DIEs
153    dw_addr_t           m_base_addr;
154    dw_offset_t         m_offset;
155    uint32_t            m_length;
156    uint16_t            m_version;
157    uint8_t             m_addr_size;
158private:
159    DISALLOW_COPY_AND_ASSIGN (DWARFCompileUnit);
160};
161
162#endif  // SymbolFileDWARF_DWARFCompileUnit_h_
163