LLDB  mainline
DWARFUnit.h
Go to the documentation of this file.
1 //===-- DWARFUnit.h ---------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SymbolFileDWARF_DWARFUnit_h_
10 #define SymbolFileDWARF_DWARFUnit_h_
11 
12 #include "DWARFDIE.h"
13 #include "DWARFDebugInfoEntry.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "llvm/Support/RWMutex.h"
16 #include <atomic>
17 
18 class DWARFUnit;
19 class DWARFCompileUnit;
20 class NameToDIE;
21 class SymbolFileDWARF;
23 
24 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
25 
32 };
33 
35  using die_iterator_range =
36  llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
37 
38 public:
39  virtual ~DWARFUnit();
40 
42  void ExtractDIEsIfNeeded();
43 
45  DWARFUnit *m_cu;
46  public:
47  bool m_clear_dies = false;
53  };
55 
56  DWARFDIE LookupAddress(const dw_addr_t address);
57  size_t AppendDIEsWithTag(const dw_tag_t tag, std::vector<DWARFDIE> &dies,
58  uint32_t depth = UINT32_MAX) const;
59  bool Verify(lldb_private::Stream *s) const;
60  virtual void Dump(lldb_private::Stream *s) const = 0;
61  /// Get the data that contains the DIE information for this unit.
62  ///
63  /// This will return the correct bytes that contain the data for
64  /// this DWARFUnit. It could be .debug_info or .debug_types
65  /// depending on where the data for this unit originates.
66  ///
67  /// \return
68  /// The correct data for the DIE information in this unit.
69  virtual const lldb_private::DWARFDataExtractor &GetData() const = 0;
70  /// Get the size in bytes of the compile unit header.
71  ///
72  /// \return
73  /// Byte size of the compile unit header
74  virtual uint32_t GetHeaderByteSize() const = 0;
75  // Offset of the initial length field.
76  dw_offset_t GetOffset() const { return m_offset; }
77  /// Get the size in bytes of the length field in the header.
78  ///
79  /// In DWARF32 this is just 4 bytes
80  ///
81  /// \return
82  /// Byte size of the compile unit header length field
83  size_t GetLengthByteSize() const { return 4; }
84 
85  bool ContainsDIEOffset(dw_offset_t die_offset) const {
86  return die_offset >= GetFirstDIEOffset() &&
87  die_offset < GetNextCompileUnitOffset();
88  }
90  return m_offset + GetHeaderByteSize();
91  }
93  // Size of the CU data (without initial length and without header).
94  size_t GetDebugInfoSize() const;
95  // Size of the CU data incl. header but without initial length.
96  uint32_t GetLength() const { return m_length; }
97  uint16_t GetVersion() const { return m_version; }
100  uint8_t GetAddressByteSize() const { return m_addr_size; }
102  dw_addr_t GetAddrBase() const { return m_addr_base; }
105  void SetAddrBase(dw_addr_t addr_base);
106  void SetRangesBase(dw_addr_t ranges_base);
107  void SetBaseObjOffset(dw_offset_t base_obj_offset);
108  void SetStrOffsetsBase(dw_offset_t str_offsets_base);
110  DWARFDebugAranges *debug_aranges);
111 
113 
115 
117 
119 
120  void SetBaseAddress(dw_addr_t base_addr);
121 
122  DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); }
123 
124  DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
125 
126  DWARFDIE GetDIE(dw_offset_t die_offset);
127 
128  static uint8_t GetAddressByteSize(const DWARFUnit *cu);
129 
130  static uint8_t GetDefaultAddressSize();
131 
132  void *GetUserData() const;
133 
134  void SetUserData(void *d);
135 
137 
139 
141 
143 
145 
147 
149 
151 
152  static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
153 
155 
156  bool GetIsOptimized();
157 
160 
162 
164 
165  die_iterator_range dies() {
167  return die_iterator_range(m_die_array.begin(), m_die_array.end());
168  }
169 
170 protected:
172 
174  std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file;
176  void *m_user_data = nullptr;
177  // The compile unit debug information entry item
179  mutable llvm::sys::RWMutex m_die_array_mutex;
180  // It is used for tracking of ScopedExtractDIEs instances.
181  mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
182  // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
183  // as someone called ExtractDIEsIfNeeded().
184  std::atomic<bool> m_cancel_scopes;
185  // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
186  // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
187  // would possibly move in memory after later ExtractDIEsIfNeeded().
189  llvm::sys::RWMutex m_first_die_mutex;
190  // A table similar to the .debug_aranges table, but this one points to the
191  // exact DW_TAG_subprogram DIEs
192  std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
196  uint8_t m_addr_size = 0;
197  uint8_t m_unit_type = 0;
198  uint64_t m_dwo_id = 0;
205  llvm::Optional<lldb_private::FileSpec> m_comp_dir;
206  dw_addr_t m_addr_base = 0; // Value of DW_AT_addr_base
207  dw_addr_t m_ranges_base = 0; // Value of DW_AT_ranges_base
208  // If this is a dwo compile unit this is the offset of the base compile unit
209  // in the main object file
211  dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
212  // Offset of the initial length field.
214 
215 private:
216  void ParseProducerInfo();
217  void ExtractDIEsRWLocked();
218  void ClearDIEsRWLocked();
219 
220  // Get the DWARF unit DWARF debug informration entry. Parse the single DIE
221  // if needed.
222  const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
224  // m_first_die_mutex is not required as m_first_die is never cleared.
225  if (!m_first_die)
226  return NULL;
227  return &m_first_die;
228  }
229 
230  // Get all DWARF debug informration entries. Parse all DIEs if needed.
231  const DWARFDebugInfoEntry *DIEPtr() {
233  if (m_die_array.empty())
234  return NULL;
235  return &m_die_array[0];
236  }
237 
238  void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
239 
240  void ComputeCompDirAndGuessPathStyle();
241 
243 };
244 
245 #endif // SymbolFileDWARF_DWARFUnit_h_
ScopedExtractDIEs & operator=(ScopedExtractDIEs &&rhs)
Definition: DWARFUnit.cpp:134
DWARFBaseDIE GetUnitDIEOnly()
Definition: DWARFUnit.h:122
uint32_t m_producer_version_update
Definition: DWARFUnit.h:202
dw_addr_t GetBaseAddress() const
Definition: DWARFUnit.h:101
lldb_private::LazyBool m_is_optimized
Definition: DWARFUnit.h:204
uint64_t m_dwo_id
Definition: DWARFUnit.h:198
void * m_user_data
Definition: DWARFUnit.h:176
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
bool ContainsDIEOffset(dw_offset_t die_offset) const
Definition: DWARFUnit.h:85
static uint8_t GetDefaultAddressSize()
Definition: DWARFUnit.cpp:560
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:530
uint8_t m_unit_type
Definition: DWARFUnit.h:197
void SetUserData(void *d)
Definition: DWARFUnit.cpp:564
size_t AppendDIEsWithTag(const dw_tag_t tag, std::vector< DWARFDIE > &dies, uint32_t depth=UINT32_MAX) const
Definition: DWARFUnit.cpp:351
A mix in class that contains a generic user ID.
Definition: UserID.h:33
A file utility class.
Definition: FileSpec.h:55
uint32_t m_producer_version_minor
Definition: DWARFUnit.h:201
std::unique_ptr< SymbolFileDWARFDwo > m_dwo_symbol_file
Definition: DWARFUnit.h:174
uint64_t dw_addr_t
Definition: dwarf.h:26
dw_offset_t m_base_obj_offset
Definition: DWARFUnit.h:210
uint16_t GetVersion() const
Definition: DWARFUnit.h:97
const DWARFAbbreviationDeclarationSet * m_abbrevs
Definition: DWARFUnit.h:175
lldb::LanguageType GetLanguageType()
Definition: DWARFUnit.cpp:671
DWARFProducer
Definition: DWARFUnit.h:26
lldb_private::FileSpec::Style GetPathStyle()
Definition: DWARFUnit.cpp:696
dw_addr_t GetAddrBase() const
Definition: DWARFUnit.h:102
lldb::LanguageType m_language_type
Definition: DWARFUnit.h:203
void SetRangesBase(dw_addr_t ranges_base)
Definition: DWARFUnit.cpp:387
bool DW_AT_decl_file_attributes_are_invalid()
Definition: DWARFUnit.cpp:574
DWARFDIE DIE()
Definition: DWARFUnit.h:124
uint8_t m_addr_size
Definition: DWARFUnit.h:196
const DWARFAbbreviationDeclarationSet * GetAbbreviations() const
Definition: DWARFUnit.cpp:377
uint32_t GetProducerVersionMinor()
Definition: DWARFUnit.cpp:647
#define UINT32_MAX
Definition: lldb-defines.h:31
dw_offset_t m_str_offsets_base
Definition: DWARFUnit.h:211
static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val)
Definition: DWARFUnit.cpp:658
dw_addr_t GetRangesBase() const
Definition: DWARFUnit.h:103
uint32_t m_producer_version_major
Definition: DWARFUnit.h:200
uint64_t user_id_t
Definition: lldb-types.h:84
LanguageType
Programming language type.
ScopedExtractDIEs ExtractDIEsScoped()
Definition: DWARFUnit.cpp:90
virtual ~DWARFUnit()
Definition: DWARFUnit.cpp:35
dw_offset_t GetNextCompileUnitOffset() const
Definition: DWARFUnit.cpp:369
dw_addr_t GetStrOffsetsBase() const
Definition: DWARFUnit.h:104
DWARFDebugInfoEntry::collection m_die_array
Definition: DWARFUnit.h:178
die_iterator_range dies()
Definition: DWARFUnit.h:165
bool Supports_unnamed_objc_bitfields()
Definition: DWARFUnit.cpp:580
void SetBaseObjOffset(dw_offset_t base_obj_offset)
Definition: DWARFUnit.cpp:391
dw_offset_t m_offset
Definition: DWARFUnit.h:213
virtual const lldb_private::DWARFDataExtractor & GetData() const =0
Get the data that contains the DIE information for this unit.
llvm::Optional< lldb_private::FileSpec > m_comp_dir
Definition: DWARFUnit.h:205
uint16_t m_version
Definition: DWARFUnit.h:195
dw_offset_t GetAbbrevOffset() const
Definition: DWARFUnit.cpp:381
dw_offset_t m_length
Definition: DWARFUnit.h:194
ScopedExtractDIEs(DWARFUnit *cu)
Definition: DWARFUnit.cpp:110
uint8_t GetAddressByteSize() const
Definition: DWARFUnit.h:100
std::vector< DWARFDebugInfoEntry > collection
const lldb_private::FileSpec & GetCompilationDirectory()
Definition: DWARFUnit.cpp:702
dw_offset_t GetOffset() const
Definition: DWARFUnit.h:76
dw_addr_t m_addr_base
Definition: DWARFUnit.h:206
llvm::sys::path::Style Style
Definition: FileSpec.h:57
DWARFUnit(SymbolFileDWARF *dwarf, lldb::user_id_t uid)
Definition: DWARFUnit.cpp:32
virtual void Dump(lldb_private::Stream *s) const =0
void ExtractUnitDIEIfNeeded()
Definition: DWARFUnit.cpp:38
void SetStrOffsetsBase(dw_offset_t str_offsets_base)
Definition: DWARFUnit.cpp:395
llvm::sys::RWMutex m_die_array_mutex
Definition: DWARFUnit.h:179
std::atomic< bool > m_cancel_scopes
Definition: DWARFUnit.h:184
bool GetIsOptimized()
Definition: DWARFUnit.cpp:682
void BuildAddressRangeTable(SymbolFileDWARF *dwarf, DWARFDebugAranges *debug_aranges)
Definition: DWARFUnit.cpp:408
void SetBaseAddress(dw_addr_t base_addr)
Definition: DWARFUnit.cpp:516
bool Supports_DW_AT_APPLE_objc_complete_type()
Definition: DWARFUnit.cpp:570
llvm::sys::RWMutex m_die_array_scoped_mutex
Definition: DWARFUnit.h:181
std::shared_ptr< DWARFUnit > DWARFUnitSP
Definition: DWARFUnit.h:22
Unknown or invalid language value.
SymbolFileDWARF * GetSymbolFileDWARF() const
Definition: DWARFUnit.cpp:590
uint32_t GetProducerVersionMajor()
Definition: DWARFUnit.cpp:641
void * GetUserData() const
Definition: DWARFUnit.cpp:562
virtual uint32_t GetHeaderByteSize() const =0
Get the size in bytes of the compile unit header.
std::unique_ptr< DWARFDebugAranges > m_func_aranges_up
Definition: DWARFUnit.h:192
void ExtractDIEsIfNeeded()
Definition: DWARFUnit.cpp:70
DWARFFormValue::FixedFormSizes GetFixedFormSizes()
Definition: DWARFUnit.cpp:512
#define DW_INVALID_OFFSET
Definition: dwarf.h:34
size_t GetDebugInfoSize() const
Definition: DWARFUnit.cpp:373
lldb::ByteOrder GetByteOrder() const
Definition: DWARFUnit.cpp:501
DWARFDebugInfoEntry m_first_die
Definition: DWARFUnit.h:188
dw_offset_t GetBaseObjOffset() const
Definition: DWARFUnit.cpp:771
SymbolFileDWARF * m_dwarf
Definition: DWARFUnit.h:173
void SetAddrBase(dw_addr_t addr_base)
Definition: DWARFUnit.cpp:385
DWARFProducer m_producer
Definition: DWARFUnit.h:199
size_t GetLengthByteSize() const
Get the size in bytes of the length field in the header.
Definition: DWARFUnit.h:83
DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs)
DWARFProducer GetProducer()
Definition: DWARFUnit.cpp:635
uint32_t GetLength() const
Definition: DWARFUnit.h:96
SymbolFileDWARFDwo * GetDwoSymbolFile() const
Definition: DWARFUnit.cpp:767
dw_addr_t m_base_addr
Definition: DWARFUnit.h:193
const DWARFDebugAranges & GetFunctionAranges()
Definition: DWARFUnit.cpp:773
bool Verify(lldb_private::Stream *s) const
lldb_private::TypeSystem * GetTypeSystem()
Definition: DWARFUnit.cpp:505
dw_addr_t m_ranges_base
Definition: DWARFUnit.h:207
llvm::sys::RWMutex m_first_die_mutex
Definition: DWARFUnit.h:189
DWARFDIE LookupAddress(const dw_addr_t address)
Definition: DWARFUnit.cpp:340
dw_offset_t GetFirstDIEOffset() const
Definition: DWARFUnit.h:89
uint32_t GetProducerVersionUpdate()
Definition: DWARFUnit.cpp:653