Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
39645 views
//===-- DWARFUnit.h ---------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H9#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H1011#include "DWARFDIE.h"12#include "DWARFDebugInfoEntry.h"13#include "lldb/Utility/XcodeSDK.h"14#include "lldb/lldb-enumerations.h"15#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"16#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"17#include "llvm/Support/RWMutex.h"18#include <atomic>19#include <optional>2021namespace lldb_private::plugin {22namespace dwarf {23class DWARFUnit;24class DWARFCompileUnit;25class NameToDIE;26class SymbolFileDWARF;27class SymbolFileDWARFDwo;2829typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;3031enum DWARFProducer {32eProducerInvalid = 0,33eProducerClang,34eProducerGCC,35eProducerLLVMGCC,36eProducerSwift,37eProducerOther38};3940class DWARFUnit : public UserID {41using die_iterator_range =42llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;4344public:45static llvm::Expected<DWARFUnitSP>46extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid,47const DWARFDataExtractor &debug_info, DIERef::Section section,48lldb::offset_t *offset_ptr);49virtual ~DWARFUnit();5051bool IsDWOUnit() { return m_is_dwo; }52/// Get the DWO ID from the DWARFUnitHeader for DWARF5, or from the unit DIE's53/// DW_AT_dwo_id or DW_AT_GNU_dwo_id for DWARF4 and earlier.54std::optional<uint64_t> GetDWOId();55/// Get the DWO ID from the DWARFUnitHeader only. DWARF5 skeleton units have56/// the DWO ID in the compile unit header and we sometimes only want to access57/// this cheap value without causing the more expensive attribute fetches that58/// GetDWOId() uses.59std::optional<uint64_t> GetHeaderDWOId() { return m_header.getDWOId(); }60void ExtractUnitDIEIfNeeded();61void ExtractUnitDIENoDwoIfNeeded();62void ExtractDIEsIfNeeded();6364class ScopedExtractDIEs {65DWARFUnit *m_cu;6667public:68bool m_clear_dies = false;69ScopedExtractDIEs(DWARFUnit &cu);70~ScopedExtractDIEs();71ScopedExtractDIEs(const ScopedExtractDIEs &) = delete;72const ScopedExtractDIEs &operator=(const ScopedExtractDIEs &) = delete;73ScopedExtractDIEs(ScopedExtractDIEs &&rhs);74ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);75};76ScopedExtractDIEs ExtractDIEsScoped();7778bool Verify(Stream *s) const;79virtual void Dump(Stream *s) const = 0;80/// Get the data that contains the DIE information for this unit.81///82/// This will return the correct bytes that contain the data for83/// this DWARFUnit. It could be .debug_info or .debug_types84/// depending on where the data for this unit originates.85///86/// \return87/// The correct data for the DIE information in this unit.88const DWARFDataExtractor &GetData() const;8990/// Get the size in bytes of the unit header.91///92/// \return93/// Byte size of the unit header94uint32_t GetHeaderByteSize() const;9596// Offset of the initial length field.97dw_offset_t GetOffset() const { return m_header.getOffset(); }98/// Get the size in bytes of the length field in the header.99///100/// In DWARF32 this is just 4 bytes101///102/// \return103/// Byte size of the compile unit header length field104size_t GetLengthByteSize() const { return 4; }105106bool ContainsDIEOffset(dw_offset_t die_offset) const {107return die_offset >= GetFirstDIEOffset() &&108die_offset < GetNextUnitOffset();109}110dw_offset_t GetFirstDIEOffset() const {111return GetOffset() + GetHeaderByteSize();112}113dw_offset_t GetNextUnitOffset() const { return m_header.getNextUnitOffset(); }114// Size of the CU data (without initial length and without header).115size_t GetDebugInfoSize() const;116// Size of the CU data incl. header but without initial length.117dw_offset_t GetLength() const { return m_header.getLength(); }118uint16_t GetVersion() const { return m_header.getVersion(); }119const llvm::DWARFAbbreviationDeclarationSet *GetAbbreviations() const;120dw_offset_t GetAbbrevOffset() const;121uint8_t GetAddressByteSize() const { return m_header.getAddressByteSize(); }122dw_addr_t GetAddrBase() const { return m_addr_base.value_or(0); }123dw_addr_t GetBaseAddress() const { return m_base_addr; }124dw_offset_t GetLineTableOffset();125dw_addr_t GetRangesBase() const { return m_ranges_base; }126dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; }127void SetAddrBase(dw_addr_t addr_base);128void SetLoclistsBase(dw_addr_t loclists_base);129void SetRangesBase(dw_addr_t ranges_base);130void SetStrOffsetsBase(dw_offset_t str_offsets_base);131virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0;132133dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const;134135lldb::ByteOrder GetByteOrder() const;136137const DWARFDebugAranges &GetFunctionAranges();138139void SetBaseAddress(dw_addr_t base_addr);140141DWARFBaseDIE GetUnitDIEOnly() { return {this, GetUnitDIEPtrOnly()}; }142143DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }144145DWARFDIE GetDIE(dw_offset_t die_offset);146147/// Returns the AT_Name of the DIE at `die_offset`, if it exists, without148/// parsing the entire compile unit. An empty is string is returned upon149/// error or if the attribute is not present.150llvm::StringRef PeekDIEName(dw_offset_t die_offset);151152DWARFUnit &GetNonSkeletonUnit();153154static uint8_t GetAddressByteSize(const DWARFUnit *cu);155156static uint8_t GetDefaultAddressSize();157158lldb_private::CompileUnit *GetLLDBCompUnit() const { return m_lldb_cu; }159160void SetLLDBCompUnit(lldb_private::CompileUnit *cu) { m_lldb_cu = cu; }161162/// Get the skeleton compile unit for a DWO file.163///164/// We need to keep track of the skeleton compile unit for a DWO file so165/// we can access it. Sometimes this value is cached when the skeleton166/// compile unit is first parsed, but if a .dwp file parses all of the167/// DWARFUnits in the file, the skeleton compile unit might not have been168/// parsed yet, to there might not be a backlink. This accessor handles169/// both cases correctly and avoids crashes.170DWARFCompileUnit *GetSkeletonUnit();171172void SetSkeletonUnit(DWARFUnit *skeleton_unit);173174bool Supports_DW_AT_APPLE_objc_complete_type();175176bool DW_AT_decl_file_attributes_are_invalid();177178bool Supports_unnamed_objc_bitfields();179180SymbolFileDWARF &GetSymbolFileDWARF() const { return m_dwarf; }181182DWARFProducer GetProducer();183184llvm::VersionTuple GetProducerVersion();185186uint64_t GetDWARFLanguageType();187188bool GetIsOptimized();189190const FileSpec &GetCompilationDirectory();191const FileSpec &GetAbsolutePath();192FileSpec GetFile(size_t file_idx);193FileSpec::Style GetPathStyle();194195SymbolFileDWARFDwo *GetDwoSymbolFile(bool load_all_debug_info = true);196197die_iterator_range dies() {198ExtractDIEsIfNeeded();199return die_iterator_range(m_die_array.begin(), m_die_array.end());200}201202DIERef::Section GetDebugSection() const { return m_section; }203204uint8_t GetUnitType() const { return m_header.getUnitType(); }205bool IsTypeUnit() const { return m_header.isTypeUnit(); }206/// Note that this check only works for DWARF5+.207bool IsSkeletonUnit() const {208return GetUnitType() == llvm::dwarf::DW_UT_skeleton;209}210211std::optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const;212213/// Return a list of address ranges resulting from a (possibly encoded)214/// range list starting at a given offset in the appropriate ranges section.215llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset);216217/// Return a list of address ranges retrieved from an encoded range218/// list whose offset is found via a table lookup given an index (DWARF v5219/// and later).220llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index);221222/// Return a rangelist's offset based on an index. The index designates223/// an entry in the rangelist table's offset array and is supplied by224/// DW_FORM_rnglistx.225llvm::Expected<uint64_t> GetRnglistOffset(uint32_t Index);226227std::optional<uint64_t> GetLoclistOffset(uint32_t Index) {228if (!m_loclist_table_header)229return std::nullopt;230231std::optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(232m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(), Index);233if (!Offset)234return std::nullopt;235return *Offset + m_loclists_base;236}237238/// Return the location table for parsing the given location list data. The239/// format is chosen according to the unit type. Never returns null.240std::unique_ptr<llvm::DWARFLocationTable>241GetLocationTable(const DataExtractor &data) const;242243DWARFDataExtractor GetLocationData() const;244245/// Returns true if any DIEs in the unit match any DW_TAG values in \a tags.246///247/// \param[in] tags248/// An array of dw_tag_t values to check all abbrevitions for.249///250/// \returns251/// True if any DIEs match any tag in \a tags, false otherwise.252bool HasAny(llvm::ArrayRef<dw_tag_t> tags);253254/// Get the fission .dwo file specific error for this compile unit.255///256/// The skeleton compile unit only can have a DWO error. Any other type257/// of DWARFUnit will not have a valid DWO error.258///259/// \returns260/// A valid DWO error if there is a problem with anything in the261/// locating or parsing inforamtion in the .dwo file262const Status &GetDwoError() const { return m_dwo_error; }263264/// Set the fission .dwo file specific error for this compile unit.265///266/// This helps tracks issues that arise when trying to locate or parse a267/// .dwo file. Things like a missing .dwo file, DWO ID mismatch, and other268/// .dwo errors can be stored in each compile unit so the issues can be269/// communicated to the user.270void SetDwoError(const Status &error) { m_dwo_error = error; }271272protected:273DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,274const llvm::DWARFUnitHeader &header,275const llvm::DWARFAbbreviationDeclarationSet &abbrevs,276DIERef::Section section, bool is_dwo);277278llvm::Error ExtractHeader(SymbolFileDWARF &dwarf,279const DWARFDataExtractor &data,280lldb::offset_t *offset_ptr);281282// Get the DWARF unit DWARF debug information entry. Parse the single DIE283// if needed.284const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {285ExtractUnitDIENoDwoIfNeeded();286// m_first_die_mutex is not required as m_first_die is never cleared.287if (!m_first_die)288return nullptr;289return &m_first_die;290}291292// Get all DWARF debug informration entries. Parse all DIEs if needed.293const DWARFDebugInfoEntry *DIEPtr() {294ExtractDIEsIfNeeded();295if (m_die_array.empty())296return nullptr;297return &m_die_array[0];298}299300const std::optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable();301302DWARFDataExtractor GetRnglistData() const;303304SymbolFileDWARF &m_dwarf;305std::shared_ptr<DWARFUnit> m_dwo;306llvm::DWARFUnitHeader m_header;307const llvm::DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;308lldb_private::CompileUnit *m_lldb_cu = nullptr;309// If this is a DWO file, we have a backlink to our skeleton compile unit.310DWARFUnit *m_skeleton_unit = nullptr;311// The compile unit debug information entry item312DWARFDebugInfoEntry::collection m_die_array;313mutable llvm::sys::RWMutex m_die_array_mutex;314// It is used for tracking of ScopedExtractDIEs instances.315mutable llvm::sys::RWMutex m_die_array_scoped_mutex;316// ScopedExtractDIEs instances should not call ClearDIEsRWLocked()317// as someone called ExtractDIEsIfNeeded().318std::atomic<bool> m_cancel_scopes;319// GetUnitDIEPtrOnly() needs to return pointer to the first DIE.320// But the first element of m_die_array after ExtractUnitDIEIfNeeded()321// would possibly move in memory after later ExtractDIEsIfNeeded().322DWARFDebugInfoEntry m_first_die;323llvm::sys::RWMutex m_first_die_mutex;324// A table similar to the .debug_aranges table, but this one points to the325// exact DW_TAG_subprogram DIEs326std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;327dw_addr_t m_base_addr = 0;328DWARFProducer m_producer = eProducerInvalid;329llvm::VersionTuple m_producer_version;330std::optional<uint64_t> m_language_type;331LazyBool m_is_optimized = eLazyBoolCalculate;332std::optional<FileSpec> m_comp_dir;333std::optional<FileSpec> m_file_spec;334std::optional<dw_addr_t> m_addr_base; ///< Value of DW_AT_addr_base.335dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base.336dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base.337std::optional<uint64_t> m_gnu_addr_base;338std::optional<uint64_t> m_gnu_ranges_base;339340/// Value of DW_AT_stmt_list.341dw_offset_t m_line_table_offset = DW_INVALID_OFFSET;342343dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.344345std::optional<llvm::DWARFDebugRnglistTable> m_rnglist_table;346bool m_rnglist_table_done = false;347std::optional<llvm::DWARFListTableHeader> m_loclist_table_header;348349const DIERef::Section m_section;350bool m_is_dwo;351bool m_has_parsed_non_skeleton_unit;352/// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).353std::optional<uint64_t> m_dwo_id;354/// If we get an error when trying to load a .dwo file, save that error here.355/// Errors include .dwo/.dwp file not found, or the .dwp/.dwp file was found356/// but DWO ID doesn't match, etc.357Status m_dwo_error;358359private:360void ParseProducerInfo();361void ExtractDIEsRWLocked();362void ClearDIEsRWLocked();363364void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);365void SetDwoStrOffsetsBase();366367void ComputeCompDirAndGuessPathStyle();368void ComputeAbsolutePath();369370DWARFUnit(const DWARFUnit &) = delete;371const DWARFUnit &operator=(const DWARFUnit &) = delete;372};373} // namespace dwarf374} // namespace lldb_private::plugin375376#endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H377378379