]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
Upgrade to Bzip2 version 1.0.8.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / SymbolFile / DWARF / DWARFUnit.h
1 //===-- DWARFUnit.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_DWARFUnit_h_
11 #define SymbolFileDWARF_DWARFUnit_h_
12
13 #include "DWARFDIE.h"
14 #include "DWARFDebugInfoEntry.h"
15 #include "lldb/lldb-enumerations.h"
16 #include "llvm/Support/RWMutex.h"
17 #include <atomic>
18
19 class DWARFUnit;
20 class DWARFCompileUnit;
21 class NameToDIE;
22 class SymbolFileDWARF;
23 class SymbolFileDWARFDwo;
24
25 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
26
27 enum DWARFProducer {
28   eProducerInvalid = 0,
29   eProducerClang,
30   eProducerGCC,
31   eProducerLLVMGCC,
32   eProcucerOther
33 };
34
35 class DWARFUnit {
36   using die_iterator_range =
37       llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
38
39 public:
40   virtual ~DWARFUnit();
41
42   void ExtractUnitDIEIfNeeded();
43   void ExtractDIEsIfNeeded();
44
45   class ScopedExtractDIEs {
46     DWARFUnit *m_cu;
47   public:
48     bool m_clear_dies = false;
49     ScopedExtractDIEs(DWARFUnit *cu);
50     ~ScopedExtractDIEs();
51     DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs);
52     ScopedExtractDIEs(ScopedExtractDIEs &&rhs);
53     ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);
54   };
55   ScopedExtractDIEs ExtractDIEsScoped();
56
57   DWARFDIE LookupAddress(const dw_addr_t address);
58   size_t AppendDIEsWithTag(const dw_tag_t tag,
59                            DWARFDIECollection &matching_dies,
60                            uint32_t depth = UINT32_MAX) const;
61   bool Verify(lldb_private::Stream *s) const;
62   virtual void Dump(lldb_private::Stream *s) const = 0;
63   //------------------------------------------------------------------
64   /// Get the data that contains the DIE information for this unit.
65   ///
66   /// This will return the correct bytes that contain the data for
67   /// this DWARFUnit. It could be .debug_info or .debug_types
68   /// depending on where the data for this unit originates.
69   ///
70   /// @return
71   ///   The correct data for the DIE information in this unit.
72   //------------------------------------------------------------------
73   virtual const lldb_private::DWARFDataExtractor &GetData() const = 0;
74   //------------------------------------------------------------------
75   /// Get the size in bytes of the compile unit header.
76   ///
77   /// @return
78   ///     Byte size of the compile unit header
79   //------------------------------------------------------------------
80   virtual uint32_t GetHeaderByteSize() const = 0;
81   // Offset of the initial length field.
82   dw_offset_t GetOffset() const { return m_offset; }
83   lldb::user_id_t GetID() const;
84   //------------------------------------------------------------------
85   /// Get the size in bytes of the length field in the header.
86   ///
87   /// In DWARF32 this is just 4 bytes, and DWARF64 it is 12 where 4
88   /// are 0xFFFFFFFF followed by the actual 64 bit length.
89   ///
90   /// @return
91   ///     Byte size of the compile unit header length field
92   //------------------------------------------------------------------
93   size_t GetLengthByteSize() const { return IsDWARF64() ? 12 : 4; }
94   
95   bool ContainsDIEOffset(dw_offset_t die_offset) const {
96     return die_offset >= GetFirstDIEOffset() &&
97            die_offset < GetNextCompileUnitOffset();
98   }
99   dw_offset_t GetFirstDIEOffset() const {
100     return m_offset + GetHeaderByteSize();
101   }
102   dw_offset_t GetNextCompileUnitOffset() const;
103   // Size of the CU data (without initial length and without header).
104   size_t GetDebugInfoSize() const;
105   // Size of the CU data incl. header but without initial length.
106   uint32_t GetLength() const { return m_length; }
107   uint16_t GetVersion() const { return m_version; }
108   const DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
109   dw_offset_t GetAbbrevOffset() const;
110   uint8_t GetAddressByteSize() const { return m_addr_size; }
111   dw_addr_t GetBaseAddress() const { return m_base_addr; }
112   dw_addr_t GetAddrBase() const { return m_addr_base; }
113   dw_addr_t GetRangesBase() const { return m_ranges_base; }
114   dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; }
115   void SetAddrBase(dw_addr_t addr_base);
116   void SetRangesBase(dw_addr_t ranges_base);
117   void SetBaseObjOffset(dw_offset_t base_obj_offset);
118   void SetStrOffsetsBase(dw_offset_t str_offsets_base);
119   void BuildAddressRangeTable(SymbolFileDWARF *dwarf,
120                               DWARFDebugAranges *debug_aranges);
121
122   lldb::ByteOrder GetByteOrder() const;
123
124   lldb_private::TypeSystem *GetTypeSystem();
125
126   const DWARFDebugAranges &GetFunctionAranges();
127
128   DWARFFormValue::FixedFormSizes GetFixedFormSizes();
129
130   void SetBaseAddress(dw_addr_t base_addr);
131
132   DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); }
133
134   DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
135
136   DWARFDIE GetDIE(dw_offset_t die_offset);
137
138   static uint8_t GetAddressByteSize(const DWARFUnit *cu);
139
140   static bool IsDWARF64(const DWARFUnit *cu);
141
142   static uint8_t GetDefaultAddressSize();
143
144   void *GetUserData() const;
145
146   void SetUserData(void *d);
147
148   bool Supports_DW_AT_APPLE_objc_complete_type();
149
150   bool DW_AT_decl_file_attributes_are_invalid();
151
152   bool Supports_unnamed_objc_bitfields();
153
154   SymbolFileDWARF *GetSymbolFileDWARF() const;
155
156   DWARFProducer GetProducer();
157
158   uint32_t GetProducerVersionMajor();
159
160   uint32_t GetProducerVersionMinor();
161
162   uint32_t GetProducerVersionUpdate();
163
164   static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
165
166   lldb::LanguageType GetLanguageType();
167
168   bool IsDWARF64() const { return m_is_dwarf64; }
169
170   bool GetIsOptimized();
171
172   SymbolFileDWARFDwo *GetDwoSymbolFile() const;
173
174   dw_offset_t GetBaseObjOffset() const;
175
176   die_iterator_range dies() {
177     ExtractDIEsIfNeeded();
178     return die_iterator_range(m_die_array.begin(), m_die_array.end());
179   }
180
181 protected:
182   DWARFUnit(SymbolFileDWARF *dwarf);
183
184   SymbolFileDWARF *m_dwarf = nullptr;
185   std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file;
186   const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
187   void *m_user_data = nullptr;
188   // The compile unit debug information entry item
189   DWARFDebugInfoEntry::collection m_die_array;
190   mutable llvm::sys::RWMutex m_die_array_mutex;
191   // It is used for tracking of ScopedExtractDIEs instances.
192   mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
193   // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
194   // as someone called ExtractDIEsIfNeeded().
195   std::atomic<bool> m_cancel_scopes;
196   // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
197   // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
198   // would possibly move in memory after later ExtractDIEsIfNeeded().
199   DWARFDebugInfoEntry m_first_die;
200   llvm::sys::RWMutex m_first_die_mutex;
201   // A table similar to the .debug_aranges table, but this one points to the
202   // exact DW_TAG_subprogram DIEs
203   std::unique_ptr<DWARFDebugAranges> m_func_aranges_ap;
204   dw_addr_t m_base_addr = 0;
205   dw_offset_t m_length = 0;
206   uint16_t m_version = 0;
207   uint8_t m_addr_size = 0;
208   uint8_t m_unit_type = 0;
209   uint64_t m_dwo_id = 0;
210   DWARFProducer m_producer = eProducerInvalid;
211   uint32_t m_producer_version_major = 0;
212   uint32_t m_producer_version_minor = 0;
213   uint32_t m_producer_version_update = 0;
214   lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown;
215   bool m_is_dwarf64 = false;
216   lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate;
217   dw_addr_t m_addr_base = 0;   // Value of DW_AT_addr_base
218   dw_addr_t m_ranges_base = 0; // Value of DW_AT_ranges_base
219   // If this is a dwo compile unit this is the offset of the base compile unit
220   // in the main object file
221   dw_offset_t m_base_obj_offset = DW_INVALID_OFFSET;
222   dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
223   // Offset of the initial length field.
224   dw_offset_t m_offset;
225
226 private:
227   void ParseProducerInfo();
228   void ExtractDIEsRWLocked();
229   void ClearDIEsRWLocked();
230
231   // Get the DWARF unit DWARF debug informration entry. Parse the single DIE
232   // if needed.
233   const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
234     ExtractUnitDIEIfNeeded();
235     // m_first_die_mutex is not required as m_first_die is never cleared.
236     if (!m_first_die)
237       return NULL;
238     return &m_first_die;
239   }
240
241   // Get all DWARF debug informration entries. Parse all DIEs if needed.
242   const DWARFDebugInfoEntry *DIEPtr() {
243     ExtractDIEsIfNeeded();
244     if (m_die_array.empty())
245       return NULL;
246     return &m_die_array[0];
247   }
248
249   void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
250   void ExtractDIEsEndCheck(lldb::offset_t offset) const;
251
252   DISALLOW_COPY_AND_ASSIGN(DWARFUnit);
253 };
254
255 #endif // SymbolFileDWARF_DWARFUnit_h_