From 6c273763a366de3b1e22348b638795795fb7e005 Mon Sep 17 00:00:00 2001 From: Greg Clayton Date: Thu, 27 Oct 2016 16:32:04 +0000 Subject: [PATCH] Switch all DWARF variables for tags, attributes and forms over to use the llvm::dwarf enumerations instead of using raw uint16_t values. This allows easier debugging as users can see the values of the enumerations in the variables view that will show the enumeration string instead of just a number. https://reviews.llvm.org/D26013 llvm-svn: 285309 --- .../DebugInfo/DWARF/DWARFAbbreviationDeclaration.h | 19 ++++++---- .../llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h | 3 +- .../llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h | 18 +++++---- llvm/include/llvm/DebugInfo/DWARF/DWARFFormValue.h | 11 +++--- llvm/include/llvm/Support/Dwarf.def | 2 +- llvm/include/llvm/Support/Dwarf.h | 3 +- .../DWARF/DWARFAbbreviationDeclaration.cpp | 43 +++++++++++----------- llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp | 2 +- llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp | 24 +++++++----- llvm/lib/DebugInfo/DWARF/DWARFFormValue.cpp | 15 +++++--- llvm/tools/llvm-dwp/llvm-dwp.cpp | 10 ++--- .../DebugInfo/DWARF/DWARFFormValueTest.cpp | 4 +- 12 files changed, 87 insertions(+), 67 deletions(-) diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h index 6ab5d5c..c9cba3b 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h @@ -12,6 +12,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Support/DataExtractor.h" +#include "llvm/Support/Dwarf.h" namespace llvm { @@ -20,16 +21,16 @@ class raw_ostream; class DWARFAbbreviationDeclaration { public: struct AttributeSpec { - AttributeSpec(uint16_t Attr, uint16_t Form) : Attr(Attr), Form(Form) {} - uint16_t Attr; - uint16_t Form; + AttributeSpec(dwarf::Attribute A, dwarf::Form F) : Attr(A), Form(F) {} + dwarf::Attribute Attr; + dwarf::Form Form; }; typedef SmallVector AttributeSpecVector; DWARFAbbreviationDeclaration(); uint32_t getCode() const { return Code; } - uint32_t getTag() const { return Tag; } + dwarf::Tag getTag() const { return Tag; } bool hasChildren() const { return HasChildren; } typedef iterator_range @@ -39,11 +40,13 @@ public: return attr_iterator_range(AttributeSpecs.begin(), AttributeSpecs.end()); } - uint16_t getFormByIndex(uint32_t idx) const { - return idx < AttributeSpecs.size() ? AttributeSpecs[idx].Form : 0; + dwarf::Form getFormByIndex(uint32_t idx) const { + if (idx < AttributeSpecs.size()) + return AttributeSpecs[idx].Form; + return dwarf::Form(0); } - uint32_t findAttributeIndex(uint16_t attr) const; + uint32_t findAttributeIndex(dwarf::Attribute attr) const; bool extract(DataExtractor Data, uint32_t* OffsetPtr); void dump(raw_ostream &OS) const; @@ -51,7 +54,7 @@ private: void clear(); uint32_t Code; - uint32_t Tag; + dwarf::Tag Tag; bool HasChildren; AttributeSpecVector AttributeSpecs; diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h index 47dbf5f..6334372 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h @@ -13,6 +13,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" +#include "llvm/Support/Dwarf.h" #include namespace llvm { @@ -30,7 +31,7 @@ class DWARFAcceleratorTable { struct HeaderData { typedef uint16_t AtomType; - typedef uint16_t Form; + typedef dwarf::Form Form; uint32_t DIEOffsetBase; SmallVector, 3> Atoms; }; diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h index f29d5fe9..9cfa3e1 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h @@ -41,7 +41,8 @@ public: void dump(raw_ostream &OS, DWARFUnit *u, unsigned recurseDepth, unsigned indent = 0) const; void dumpAttribute(raw_ostream &OS, DWARFUnit *u, uint32_t *offset_ptr, - uint16_t attr, uint16_t form, unsigned indent = 0) const; + dwarf::Attribute attr, dwarf::Form form, + unsigned indent = 0) const; /// Extracts a debug info entry, which is a child of a given unit, /// starting at a given offset. If DIE can't be extracted, returns false and @@ -86,24 +87,27 @@ public: return AbbrevDecl; } - bool getAttributeValue(const DWARFUnit *U, const uint16_t Attr, + bool getAttributeValue(const DWARFUnit *U, dwarf::Attribute Attr, DWARFFormValue &FormValue) const; - const char *getAttributeValueAsString(const DWARFUnit *U, const uint16_t Attr, + const char *getAttributeValueAsString(const DWARFUnit *U, + dwarf::Attribute Attr, const char *FailValue) const; - uint64_t getAttributeValueAsAddress(const DWARFUnit *U, const uint16_t Attr, + uint64_t getAttributeValueAsAddress(const DWARFUnit *U, + dwarf::Attribute Attr, uint64_t FailValue) const; uint64_t getAttributeValueAsUnsignedConstant(const DWARFUnit *U, - const uint16_t Attr, + dwarf::Attribute Attr, uint64_t FailValue) const; - uint64_t getAttributeValueAsReference(const DWARFUnit *U, const uint16_t Attr, + uint64_t getAttributeValueAsReference(const DWARFUnit *U, + dwarf::Attribute Attr, uint64_t FailValue) const; uint64_t getAttributeValueAsSectionOffset(const DWARFUnit *U, - const uint16_t Attr, + dwarf::Attribute Attr, uint64_t FailValue) const; uint64_t getRangesBaseAttribute(const DWARFUnit *U, uint64_t FailValue) const; diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFFormValue.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFFormValue.h index b2f750d..19efd93 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFFormValue.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFFormValue.h @@ -12,6 +12,7 @@ #include "llvm/ADT/Optional.h" #include "llvm/Support/DataExtractor.h" +#include "llvm/Support/Dwarf.h" namespace llvm { @@ -48,12 +49,12 @@ private: const uint8_t* data; }; - uint16_t Form; // Form for this value. + dwarf::Form Form; // Form for this value. ValueType Value; // Contains all data for the form. public: - DWARFFormValue(uint16_t Form = 0) : Form(Form) {} - uint16_t getForm() const { return Form; } + DWARFFormValue(dwarf::Form F = dwarf::Form(0)) : Form(F) {} + dwarf::Form getForm() const { return Form; } bool isFormClass(FormClass FC) const; void dump(raw_ostream &OS, const DWARFUnit *U) const; @@ -82,9 +83,9 @@ public: bool skipValue(DataExtractor debug_info_data, uint32_t *offset_ptr, const DWARFUnit *u) const; - static bool skipValue(uint16_t form, DataExtractor debug_info_data, + static bool skipValue(dwarf::Form form, DataExtractor debug_info_data, uint32_t *offset_ptr, const DWARFUnit *u); - static bool skipValue(uint16_t form, DataExtractor debug_info_data, + static bool skipValue(dwarf::Form form, DataExtractor debug_info_data, uint32_t *offset_ptr, uint16_t Version, uint8_t AddrSize); diff --git a/llvm/include/llvm/Support/Dwarf.def b/llvm/include/llvm/Support/Dwarf.def index b73f2ae..13984d2 100644 --- a/llvm/include/llvm/Support/Dwarf.def +++ b/llvm/include/llvm/Support/Dwarf.def @@ -42,7 +42,7 @@ #define HANDLE_DW_CC(ID, NAME) #endif - +HANDLE_DW_TAG(0x0000, null) HANDLE_DW_TAG(0x0001, array_type) HANDLE_DW_TAG(0x0002, class_type) HANDLE_DW_TAG(0x0003, entry_point) diff --git a/llvm/include/llvm/Support/Dwarf.h b/llvm/include/llvm/Support/Dwarf.h index 8448dae..6e32ffd 100644 --- a/llvm/include/llvm/Support/Dwarf.h +++ b/llvm/include/llvm/Support/Dwarf.h @@ -312,7 +312,8 @@ enum Form : uint16_t { DW_FORM_exprloc = 0x18, DW_FORM_flag_present = 0x19, DW_FORM_ref_sig8 = 0x20, - + + DW_FORM_lo_user = 0x1f00, // Extensions for Fission proposal DW_FORM_GNU_addr_index = 0x1f01, DW_FORM_GNU_str_index = 0x1f02, diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp index afae00e..637d12d 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp @@ -16,7 +16,7 @@ using namespace dwarf; void DWARFAbbreviationDeclaration::clear() { Code = 0; - Tag = 0; + Tag = DW_TAG_null; HasChildren = false; AttributeSpecs.clear(); } @@ -26,37 +26,38 @@ DWARFAbbreviationDeclaration::DWARFAbbreviationDeclaration() { } bool -DWARFAbbreviationDeclaration::extract(DataExtractor Data, uint32_t* OffsetPtr) { +DWARFAbbreviationDeclaration::extract(DataExtractor Data, + uint32_t* OffsetPtr) { clear(); Code = Data.getULEB128(OffsetPtr); if (Code == 0) { return false; } - Tag = Data.getULEB128(OffsetPtr); + Tag = static_cast(Data.getULEB128(OffsetPtr)); + if (Tag == DW_TAG_null) { + clear(); + return false; + } uint8_t ChildrenByte = Data.getU8(OffsetPtr); HasChildren = (ChildrenByte == DW_CHILDREN_yes); while (true) { - uint32_t CurOffset = *OffsetPtr; - uint16_t Attr = Data.getULEB128(OffsetPtr); - if (CurOffset == *OffsetPtr) { - clear(); - return false; - } - CurOffset = *OffsetPtr; - uint16_t Form = Data.getULEB128(OffsetPtr); - if (CurOffset == *OffsetPtr) { + auto A = static_cast(Data.getULEB128(OffsetPtr)); + auto F = static_cast
(Data.getULEB128(OffsetPtr)); + if (A && F) { + AttributeSpecs.push_back(AttributeSpec(A, F)); + } else if (A == 0 && F == 0) { + // We successfully reached the end of this abbreviation declaration + // since both attribute and form are zero. + break; + } else { + // Attribute and form pairs must either both be non-zero, in which case + // they are added to the abbreviation declaration, or both be zero to + // terminate the abbrevation declaration. In this case only one was + // zero which is an error. clear(); return false; } - if (Attr == 0 && Form == 0) - break; - AttributeSpecs.push_back(AttributeSpec(Attr, Form)); - } - - if (Tag == 0) { - clear(); - return false; } return true; } @@ -88,7 +89,7 @@ void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const { } uint32_t -DWARFAbbreviationDeclaration::findAttributeIndex(uint16_t attr) const { +DWARFAbbreviationDeclaration::findAttributeIndex(dwarf::Attribute attr) const { for (uint32_t i = 0, e = AttributeSpecs.size(); i != e; ++i) { if (AttributeSpecs[i].Attr == attr) return i; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp index 9aa3a2b..cef4eab 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp @@ -39,7 +39,7 @@ bool DWARFAcceleratorTable::extract() { for (unsigned i = 0; i < NumAtoms; ++i) { uint16_t AtomType = AccelSection.getU16(&Offset); - uint16_t AtomForm = AccelSection.getU16(&Offset); + auto AtomForm = static_cast(AccelSection.getU16(&Offset)); HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm)); } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp index c43456b..e4577dc 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp @@ -112,7 +112,8 @@ static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges, void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS, DWARFUnit *u, uint32_t *offset_ptr, - uint16_t attr, uint16_t form, + dwarf::Attribute attr, + dwarf::Form form, unsigned indent) const { const char BaseIndent[] = " "; OS << BaseIndent; @@ -207,7 +208,7 @@ bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U, // Skip all data in the .debug_info for the attributes for (const auto &AttrSpec : AbbrevDecl->attributes()) { - uint16_t Form = AttrSpec.Form; + auto Form = AttrSpec.Form; uint8_t FixedFormSize = (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0; @@ -232,8 +233,8 @@ bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const { Tag == DW_TAG_inlined_subroutine; } -bool DWARFDebugInfoEntryMinimal::getAttributeValue( - const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const { +bool DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFUnit *U, + dwarf::Attribute Attr, DWARFFormValue &FormValue) const { if (!AbbrevDecl) return false; @@ -258,7 +259,8 @@ bool DWARFDebugInfoEntryMinimal::getAttributeValue( } const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString( - const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const { + const DWARFUnit *U, dwarf::Attribute Attr, + const char *FailValue) const { DWARFFormValue FormValue; if (!getAttributeValue(U, Attr, FormValue)) return FailValue; @@ -267,7 +269,8 @@ const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString( } uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress( - const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const { + const DWARFUnit *U, dwarf::Attribute Attr, + uint64_t FailValue) const { DWARFFormValue FormValue; if (!getAttributeValue(U, Attr, FormValue)) return FailValue; @@ -276,7 +279,8 @@ uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress( } uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant( - const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const { + const DWARFUnit *U, dwarf::Attribute Attr, + uint64_t FailValue) const { DWARFFormValue FormValue; if (!getAttributeValue(U, Attr, FormValue)) return FailValue; @@ -285,7 +289,8 @@ uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant( } uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference( - const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const { + const DWARFUnit *U, dwarf::Attribute Attr, + uint64_t FailValue) const { DWARFFormValue FormValue; if (!getAttributeValue(U, Attr, FormValue)) return FailValue; @@ -294,7 +299,8 @@ uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference( } uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset( - const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const { + const DWARFUnit *U, dwarf::Attribute Attr, + uint64_t FailValue) const { DWARFFormValue FormValue; if (!getAttributeValue(U, Attr, FormValue)) return FailValue; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFFormValue.cpp b/llvm/lib/DebugInfo/DWARF/DWARFFormValue.cpp index 3dc5842..a2d625a 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFFormValue.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFFormValue.cpp @@ -123,6 +123,8 @@ bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const { case DW_FORM_GNU_str_index: case DW_FORM_GNU_strp_alt: return (FC == FC_String); + default: + break; } // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section offset. // Don't check for DWARF version here, as some producers may still do this @@ -208,7 +210,7 @@ bool DWARFFormValue::extractValue(DataExtractor data, uint32_t *offset_ptr, Value.cstr = data.getCStr(offset_ptr); break; case DW_FORM_indirect: - Form = data.getULEB128(offset_ptr); + Form = static_cast(data.getULEB128(offset_ptr)); indirect = true; break; case DW_FORM_sec_offset: @@ -259,14 +261,15 @@ DWARFFormValue::skipValue(DataExtractor debug_info_data, uint32_t* offset_ptr, } bool -DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data, +DWARFFormValue::skipValue(dwarf::Form form, DataExtractor debug_info_data, uint32_t *offset_ptr, const DWARFUnit *cu) { return skipValue(form, debug_info_data, offset_ptr, cu->getVersion(), cu->getAddressByteSize()); } -bool DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data, - uint32_t *offset_ptr, uint16_t Version, - uint8_t AddrSize) { +bool +DWARFFormValue::skipValue(dwarf::Form form, DataExtractor debug_info_data, + uint32_t *offset_ptr, uint16_t Version, + uint8_t AddrSize) { bool indirect = false; do { switch (form) { @@ -349,7 +352,7 @@ bool DWARFFormValue::skipValue(uint16_t form, DataExtractor debug_info_data, case DW_FORM_indirect: indirect = true; - form = debug_info_data.getULEB128(offset_ptr); + form = static_cast(debug_info_data.getULEB128(offset_ptr)); break; // FIXME: 4 for DWARF32, 8 for DWARF64. diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp index 57cdb52..b1ec503 100644 --- a/llvm/tools/llvm-dwp/llvm-dwp.cpp +++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp @@ -112,8 +112,8 @@ struct CompileUnitIdentifiers { }; static Expected -getIndexedString(uint32_t Form, DataExtractor InfoData, uint32_t &InfoOffset, - StringRef StrOffsets, StringRef Str) { +getIndexedString(dwarf::Form Form, DataExtractor InfoData, + uint32_t &InfoOffset, StringRef StrOffsets, StringRef Str) { if (Form == dwarf::DW_FORM_string) return InfoData.getCStr(&InfoOffset); if (Form != dwarf::DW_FORM_GNU_str_index) @@ -142,16 +142,16 @@ static Expected getCUIdentifiers(StringRef Abbrev, DataExtractor AbbrevData(Abbrev, true, 0); uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); - uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); + auto Tag = static_cast(AbbrevData.getULEB128(&AbbrevOffset)); if (Tag != dwarf::DW_TAG_compile_unit) return make_error("top level DIE is not a compile unit"); // DW_CHILDREN AbbrevData.getU8(&AbbrevOffset); uint32_t Name; - uint32_t Form; + dwarf::Form Form; CompileUnitIdentifiers ID; while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | - (Form = AbbrevData.getULEB128(&AbbrevOffset)) && + (Form = static_cast(AbbrevData.getULEB128(&AbbrevOffset))) && (Name != 0 || Form != 0)) { switch (Name) { case dwarf::DW_AT_name: { diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp index eb9607a..c751bff 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFFormValueTest.cpp @@ -33,7 +33,7 @@ TEST(DWARFFormValue, FixedFormSizes) { EXPECT_EQ(0U, DWARFFormValue::getFixedFormSizes(16, 2).size()); } -bool isFormClass(uint16_t Form, DWARFFormValue::FormClass FC) { +bool isFormClass(dwarf::Form Form, DWARFFormValue::FormClass FC) { return DWARFFormValue(Form).isFormClass(FC); } @@ -52,7 +52,7 @@ TEST(DWARFFormValue, FormClass) { } template -DWARFFormValue createDataXFormValue(uint16_t Form, RawTypeT Value) { +DWARFFormValue createDataXFormValue(dwarf::Form Form, RawTypeT Value) { char Raw[sizeof(RawTypeT)]; memcpy(Raw, &Value, sizeof(RawTypeT)); uint32_t Offset = 0; -- 2.7.4