From 617ed4f061999025287b424c39fd99c5fe1e5126 Mon Sep 17 00:00:00 2001 From: Shubham Sandeep Rastogi Date: Thu, 6 Oct 2022 12:15:07 -0700 Subject: [PATCH] Remove the dependency between lib/DebugInfoDWARF and MC. Differential Revision: https://reviews.llvm.org/D134817 --- lldb/source/Expression/DWARFExpression.cpp | 16 +++- lldb/source/Symbol/UnwindPlan.cpp | 2 +- lldb/unittests/Symbol/PostfixExpressionTest.cpp | 2 +- .../PdbFPOProgramToDWARFExpressionTests.cpp | 2 +- llvm/include/llvm/DebugInfo/DIContext.h | 3 + llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h | 11 --- .../include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h | 24 +++--- llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h | 11 +-- .../include/llvm/DebugInfo/DWARF/DWARFExpression.h | 13 ++- llvm/lib/DebugInfo/DWARF/CMakeLists.txt | 1 - llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 44 +++------- llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp | 91 ++++++++++---------- llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp | 26 +++--- llvm/lib/DebugInfo/DWARF/DWARFDie.cpp | 9 +- llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp | 97 ++++++++++------------ .../DebugInfo/LogicalView/Readers/LVELFReader.cpp | 14 +++- llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 70 ++++++++++++---- llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp | 1 - llvm/tools/llvm-objdump/SourcePrinter.cpp | 12 ++- llvm/tools/llvm-objdump/SourcePrinter.h | 1 + llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h | 5 +- .../DebugInfo/DWARF/DWARFDebugFrameTest.cpp | 8 +- .../DWARF/DWARFExpressionCompactPrinterTest.cpp | 12 ++- 23 files changed, 253 insertions(+), 222 deletions(-) diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp index d6029aa..a60ac41 100644 --- a/lldb/source/Expression/DWARFExpression.cpp +++ b/lldb/source/Expression/DWARFExpression.cpp @@ -69,9 +69,21 @@ void DWARFExpression::UpdateValue(uint64_t const_value, void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi) const { + auto *MCRegInfo = abi ? &abi->GetMCRegisterInfo() : nullptr; + auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, + bool IsEH) -> llvm::StringRef { + if (!MCRegInfo) + return {}; + if (std::optional LLVMRegNum = + MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH)) + if (const char *RegName = MCRegInfo->getName(*LLVMRegNum)) + return llvm::StringRef(RegName); + return {}; + }; + llvm::DIDumpOptions DumpOpts; + DumpOpts.GetNameForDWARFReg = GetRegName; llvm::DWARFExpression(m_data.GetAsLLVM(), m_data.GetAddressByteSize()) - .print(s->AsRawOstream(), llvm::DIDumpOptions(), - abi ? &abi->GetMCRegisterInfo() : nullptr, nullptr); + .print(s->AsRawOstream(), DumpOpts, nullptr); } RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; } diff --git a/lldb/source/Symbol/UnwindPlan.cpp b/lldb/source/Symbol/UnwindPlan.cpp index 51301e4..eace7c4 100644 --- a/lldb/source/Symbol/UnwindPlan.cpp +++ b/lldb/source/Symbol/UnwindPlan.cpp @@ -84,7 +84,7 @@ static void DumpDWARFExpr(Stream &s, llvm::ArrayRef expr, Thread *threa llvm::DataExtractor data(expr, order_and_width->first == eByteOrderLittle, order_and_width->second); llvm::DWARFExpression(data, order_and_width->second, llvm::dwarf::DWARF32) - .print(s.AsRawOstream(), llvm::DIDumpOptions(), nullptr, nullptr); + .print(s.AsRawOstream(), llvm::DIDumpOptions(), nullptr); } else s.PutCString("dwarf-expr"); } diff --git a/lldb/unittests/Symbol/PostfixExpressionTest.cpp b/lldb/unittests/Symbol/PostfixExpressionTest.cpp index 4d4706d..0eaa9d8 100644 --- a/lldb/unittests/Symbol/PostfixExpressionTest.cpp +++ b/lldb/unittests/Symbol/PostfixExpressionTest.cpp @@ -159,7 +159,7 @@ static std::string ParseAndGenerateDWARF(llvm::StringRef expr) { std::string result; llvm::raw_string_ostream os(result); llvm::DWARFExpression(extractor, addr_size, llvm::dwarf::DWARF32) - .print(os, llvm::DIDumpOptions(), nullptr, nullptr); + .print(os, llvm::DIDumpOptions(), nullptr); return std::move(os.str()); } diff --git a/lldb/unittests/SymbolFile/NativePDB/PdbFPOProgramToDWARFExpressionTests.cpp b/lldb/unittests/SymbolFile/NativePDB/PdbFPOProgramToDWARFExpressionTests.cpp index 730afb3..270cc8e 100644 --- a/lldb/unittests/SymbolFile/NativePDB/PdbFPOProgramToDWARFExpressionTests.cpp +++ b/lldb/unittests/SymbolFile/NativePDB/PdbFPOProgramToDWARFExpressionTests.cpp @@ -40,7 +40,7 @@ CheckValidProgramTranslation(llvm::StringRef fpo_program, std::string result; llvm::raw_string_ostream os(result); llvm::DWARFExpression(extractor, /*AddressSize=*/4, llvm::dwarf::DWARF32) - .print(os, llvm::DIDumpOptions(), nullptr, nullptr); + .print(os, llvm::DIDumpOptions(), nullptr); // actual check ASSERT_EQ(expected_dwarf_expression, os.str()); diff --git a/llvm/include/llvm/DebugInfo/DIContext.h b/llvm/include/llvm/DebugInfo/DIContext.h index afc2d51..6866a66 100644 --- a/llvm/include/llvm/DebugInfo/DIContext.h +++ b/llvm/include/llvm/DebugInfo/DIContext.h @@ -199,6 +199,9 @@ struct DIDumpOptions { bool SummarizeTypes = false; bool Verbose = false; bool DisplayRawContents = false; + bool IsEH = false; + std::function + GetNameForDWARFReg; /// Return default option set for printing a single DIE without children. static DIDumpOptions getForSingleDIE() { diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h index 5dc0001..5dbec26 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -27,7 +27,6 @@ namespace llvm { -class MCRegisterInfo; class MemoryBuffer; class AppleAcceleratorTable; class DWARFCompileUnit; @@ -81,9 +80,6 @@ class DWARFContext : public DIContext { std::weak_ptr DWP; bool CheckedForDWP = false; std::string DWPName; - - std::unique_ptr RegInfo; - std::function RecoverableErrorHandler = WithColor::defaultErrorHandler; std::function WarningHandler = WithColor::defaultWarningHandler; @@ -408,8 +404,6 @@ public: std::shared_ptr getDWOContext(StringRef AbsolutePath); - const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); } - function_ref getRecoverableErrorHandler() { return RecoverableErrorHandler; } @@ -435,11 +429,6 @@ public: std::function WarningHandler = WithColor::defaultWarningHandler); - /// Loads register info for the architecture of the provided object file. - /// Improves readability of dumped DWARF expressions. Requires the caller to - /// have initialized the relevant target descriptions. - Error loadRegisterInfo(const object::ObjectFile &Obj); - /// Get address size from CUs. /// TODO: refactor compile_units() to make this const. uint8_t getCUAddrSize(); diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h index 6f25964..8e1e619 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h @@ -164,7 +164,7 @@ public: /// instead of from .debug_frame. This is needed for register number /// conversion because some register numbers differ between the two sections /// for certain architectures like x86. - void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const; + void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const; bool operator==(const UnwindLocation &RHS) const; }; @@ -222,7 +222,7 @@ public: /// instead of from .debug_frame. This is needed for register number /// conversion because some register numbers differ between the two sections /// for certain architectures like x86. - void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH) const; + void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const; /// Returns true if we have any register locations in this object. bool hasLocations() const { return !Locations.empty(); } @@ -303,7 +303,7 @@ public: /// /// \param IndentLevel specify the indent level as an integer. The UnwindRow /// will be output to the stream preceded by 2 * IndentLevel number of spaces. - void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, + void dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel = 0) const; }; @@ -348,7 +348,7 @@ public: /// /// \param IndentLevel specify the indent level as an integer. The UnwindRow /// will be output to the stream preceded by 2 * IndentLevel number of spaces. - void dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, + void dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel = 0) const; /// Create an UnwindTable from a Common Information Entry (CIE). @@ -454,8 +454,8 @@ public: /// where a problem occurred in case an error is returned. Error parse(DWARFDataExtractor Data, uint64_t *Offset, uint64_t EndOffset); - void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI, - bool IsEH, unsigned IndentLevel = 1) const; + void dump(raw_ostream &OS, DIDumpOptions DumpOpts, + unsigned IndentLevel = 1) const; void addInstruction(const Instruction &I) { Instructions.push_back(I); } @@ -523,7 +523,6 @@ private: /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand. void printOperand(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, bool IsEH, const Instruction &Instr, unsigned OperandIdx, uint64_t Operand) const; }; @@ -548,8 +547,7 @@ public: CFIProgram &cfis() { return CFIs; } /// Dump the instructions in this CFI fragment - virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, bool IsEH) const = 0; + virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const = 0; protected: const FrameKind Kind; @@ -605,8 +603,7 @@ public: uint32_t getLSDAPointerEncoding() const { return LSDAPointerEncoding; } - void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI, - bool IsEH) const override; + void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override; private: /// The following fields are defined in section 6.4.1 of the DWARF standard v4 @@ -645,8 +642,7 @@ public: uint64_t getAddressRange() const { return AddressRange; } std::optional getLSDAAddress() const { return LSDAAddress; } - void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI, - bool IsEH) const override; + void dump(raw_ostream &OS, DIDumpOptions DumpOpts) const override; static bool classof(const FrameEntry *FE) { return FE->getKind() == FK_FDE; } @@ -688,7 +684,7 @@ public: ~DWARFDebugFrame(); /// Dump the section data into the given stream. - void dump(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *MRI, + void dump(raw_ostream &OS, DIDumpOptions DumpOpts, std::optional Offset) const; /// Parse the section from raw data. \p Data is assumed to contain the whole diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h index 74638fc..5a82e7b 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h @@ -66,9 +66,8 @@ public: /// updated to point past the end of the current list). bool dumpLocationList(uint64_t *Offset, raw_ostream &OS, std::optional BaseAddr, - const MCRegisterInfo *MRI, const DWARFObject &Obj, - DWARFUnit *U, DIDumpOptions DumpOpts, - unsigned Indent) const; + const DWARFObject &Obj, DWARFUnit *U, + DIDumpOptions DumpOpts, unsigned Indent) const; Error visitAbsoluteLocationList( uint64_t Offset, std::optional BaseAddr, @@ -109,8 +108,7 @@ public: : DWARFLocationTable(std::move(Data)) {} /// Print the location lists found within the debug_loc section. - void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo, - const DWARFObject &Obj, DIDumpOptions DumpOpts, + void dump(raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts, std::optional Offset) const; Error visitLocationList( @@ -134,8 +132,7 @@ public: /// Dump all location lists within the given range. void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS, - const MCRegisterInfo *MRI, const DWARFObject &Obj, - DIDumpOptions DumpOpts); + const DWARFObject &Obj, DIDumpOptions DumpOpts); protected: void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS, diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFExpression.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFExpression.h index 6e5a16f..7fde327 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFExpression.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFExpression.h @@ -93,8 +93,7 @@ public: uint64_t getEndOffset() const { return EndOffset; } bool isError() const { return Error; } bool print(raw_ostream &OS, DIDumpOptions DumpOpts, - const DWARFExpression *Expr, const MCRegisterInfo *RegInfo, - DWARFUnit *U, bool isEH) const; + const DWARFExpression *Expr, DWARFUnit *U) const; /// Verify \p Op. Does not affect the return of \a isError(). static bool verify(const Operation &Op, DWARFUnit *U); @@ -147,15 +146,16 @@ public: iterator begin() const { return iterator(this, 0); } iterator end() const { return iterator(this, Data.getData().size()); } - void print(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *RegInfo, DWARFUnit *U, + void print(raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH = false) const; /// Print the expression in a format intended to be compact and useful to a /// user, but not perfectly unambiguous, or capable of representing every /// valid DWARF expression. Returns true if the expression was sucessfully /// printed. - bool printCompact(raw_ostream &OS, const MCRegisterInfo &RegInfo); + bool printCompact(raw_ostream &OS, + std::function + GetNameForDWARFReg = nullptr); bool verify(DWARFUnit *U); @@ -165,8 +165,7 @@ public: static bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, uint8_t Opcode, - const uint64_t Operands[2], - const MCRegisterInfo *MRI, bool isEH); + const uint64_t Operands[2]); private: DataExtractor Data; diff --git a/llvm/lib/DebugInfo/DWARF/CMakeLists.txt b/llvm/lib/DebugInfo/DWARF/CMakeLists.txt index 5ff72ad..0a33513 100644 --- a/llvm/lib/DebugInfo/DWARF/CMakeLists.txt +++ b/llvm/lib/DebugInfo/DWARF/CMakeLists.txt @@ -36,6 +36,5 @@ add_llvm_component_library(LLVMDebugInfoDWARF LINK_COMPONENTS BinaryFormat Object - MC Support ) diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index d7118b8..184eeff 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -38,7 +38,6 @@ #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h" #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" #include "llvm/DebugInfo/DWARF/DWARFVerifier.h" -#include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Object/Decompressor.h" #include "llvm/Object/MachO.h" @@ -309,9 +308,7 @@ DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) { } static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts, - DWARFDataExtractor Data, - const MCRegisterInfo *MRI, - const DWARFObject &Obj, + DWARFDataExtractor Data, const DWARFObject &Obj, std::optional DumpOffset) { uint64_t Offset = 0; @@ -330,13 +327,13 @@ static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts, if (DumpOffset) { if (DumpOffset >= Offset && DumpOffset < EndOffset) { Offset = *DumpOffset; - Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, MRI, Obj, + Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, Obj, nullptr, DumpOpts, /*Indent=*/0); OS << "\n"; return; } } else { - Loc.dumpRange(Offset, EndOffset - Offset, OS, MRI, Obj, DumpOpts); + Loc.dumpRange(Offset, EndOffset - Offset, OS, Obj, DumpOpts); } Offset = EndOffset; } @@ -424,21 +421,21 @@ void DWARFContext::dump( if (const auto *Off = shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc, DObj->getLocSection().Data)) { - getDebugLoc()->dump(OS, getRegisterInfo(), *DObj, LLDumpOpts, *Off); + getDebugLoc()->dump(OS, *DObj, LLDumpOpts, *Off); } if (const auto *Off = shouldDump(Explicit, ".debug_loclists", DIDT_ID_DebugLoclists, DObj->getLoclistsSection().Data)) { DWARFDataExtractor Data(*DObj, DObj->getLoclistsSection(), isLittleEndian(), 0); - dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off); + dumpLoclistsSection(OS, LLDumpOpts, Data, *DObj, *Off); } if (const auto *Off = shouldDump(ExplicitDWO, ".debug_loclists.dwo", DIDT_ID_DebugLoclists, DObj->getLoclistsDWOSection().Data)) { DWARFDataExtractor Data(*DObj, DObj->getLoclistsDWOSection(), isLittleEndian(), 0); - dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off); + dumpLoclistsSection(OS, LLDumpOpts, Data, *DObj, *Off); } if (const auto *Off = @@ -450,12 +447,12 @@ void DWARFContext::dump( if (*Off) { uint64_t Offset = **Off; Loc.dumpLocationList(&Offset, OS, - /*BaseAddr=*/std::nullopt, getRegisterInfo(), *DObj, - nullptr, LLDumpOpts, /*Indent=*/0); + /*BaseAddr=*/std::nullopt, *DObj, nullptr, + LLDumpOpts, + /*Indent=*/0); OS << "\n"; } else { - Loc.dumpRange(0, Data.getData().size(), OS, getRegisterInfo(), *DObj, - LLDumpOpts); + Loc.dumpRange(0, Data.getData().size(), OS, *DObj, LLDumpOpts); } } @@ -463,7 +460,7 @@ void DWARFContext::dump( shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, DObj->getFrameSection().Data)) { if (Expected DF = getDebugFrame()) - (*DF)->dump(OS, DumpOpts, getRegisterInfo(), *Off); + (*DF)->dump(OS, DumpOpts, *Off); else RecoverableErrorHandler(DF.takeError()); } @@ -472,7 +469,7 @@ void DWARFContext::dump( shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, DObj->getEHFrameSection().Data)) { if (Expected DF = getEHFrame()) - (*DF)->dump(OS, DumpOpts, getRegisterInfo(), *Off); + (*DF)->dump(OS, DumpOpts, *Off); else RecoverableErrorHandler(DF.takeError()); } @@ -2020,23 +2017,6 @@ DWARFContext::create(const StringMap> &Sections, std::move(DObj), "", RecoverableErrorHandler, WarningHandler); } -Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) { - // Detect the architecture from the object file. We usually don't need OS - // info to lookup a target and create register info. - Triple TT; - TT.setArch(Triple::ArchType(Obj.getArch())); - TT.setVendor(Triple::UnknownVendor); - TT.setOS(Triple::UnknownOS); - std::string TargetLookupError; - const Target *TheTarget = - TargetRegistry::lookupTarget(TT.str(), TargetLookupError); - if (!TargetLookupError.empty()) - return createStringError(errc::invalid_argument, - TargetLookupError.c_str()); - RegInfo.reset(TheTarget->createMCRegInfo(TT.str())); - return Error::success(); -} - uint8_t DWARFContext::getCUAddrSize() { // In theory, different compile units may have different address byte // sizes, but for simplicity we just use the address byte size of the diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp index 5351213..1b71f4b 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp @@ -13,7 +13,6 @@ #include "llvm/BinaryFormat/Dwarf.h" #include "llvm/DebugInfo/DIContext.h" #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" -#include "llvm/MC/MCRegisterInfo.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/DataExtractor.h" #include "llvm/Support/Errc.h" @@ -29,14 +28,13 @@ using namespace llvm; using namespace dwarf; -static void printRegister(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, +static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned RegNum) { - if (MRI) { - if (std::optional LLVMRegNum = MRI->getLLVMRegNum(RegNum, IsEH)) { - if (const char *RegName = MRI->getName(*LLVMRegNum)) { - OS << RegName; - return; - } + if (DumpOpts.GetNameForDWARFReg) { + auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH); + if (!RegName.empty()) { + OS << RegName; + return; } } OS << "reg" << RegNum; @@ -80,8 +78,7 @@ UnwindLocation UnwindLocation::createAtDWARFExpression(DWARFExpression Expr) { return {Expr, true}; } -void UnwindLocation::dump(raw_ostream &OS, const MCRegisterInfo *MRI, - bool IsEH) const { +void UnwindLocation::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { if (Dereference) OS << '['; switch (Kind) { @@ -103,7 +100,7 @@ void UnwindLocation::dump(raw_ostream &OS, const MCRegisterInfo *MRI, OS << Offset; break; case RegPlusOffset: - printRegister(OS, MRI, IsEH, RegNum); + printRegister(OS, DumpOpts, RegNum); if (Offset == 0 && !AddrSpace) break; if (Offset >= 0) @@ -112,9 +109,10 @@ void UnwindLocation::dump(raw_ostream &OS, const MCRegisterInfo *MRI, if (AddrSpace) OS << " in addrspace" << *AddrSpace; break; - case DWARFExpr: - Expr->print(OS, DIDumpOptions(), MRI, nullptr, IsEH); + case DWARFExpr: { + Expr->print(OS, DumpOpts, nullptr); break; + } case Constant: OS << Offset; break; @@ -125,7 +123,8 @@ void UnwindLocation::dump(raw_ostream &OS, const MCRegisterInfo *MRI, raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindLocation &UL) { - UL.dump(OS, nullptr, false); + auto DumpOpts = DIDumpOptions(); + UL.dump(OS, DumpOpts); return OS; } @@ -150,53 +149,55 @@ bool UnwindLocation::operator==(const UnwindLocation &RHS) const { return false; } -void RegisterLocations::dump(raw_ostream &OS, const MCRegisterInfo *MRI, - bool IsEH) const { +void RegisterLocations::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { bool First = true; for (const auto &RegLocPair : Locations) { if (First) First = false; else OS << ", "; - printRegister(OS, MRI, IsEH, RegLocPair.first); + printRegister(OS, DumpOpts, RegLocPair.first); OS << '='; - RegLocPair.second.dump(OS, MRI, IsEH); + RegLocPair.second.dump(OS, DumpOpts); } } raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const RegisterLocations &RL) { - RL.dump(OS, nullptr, false); + auto DumpOpts = DIDumpOptions(); + RL.dump(OS, DumpOpts); return OS; } -void UnwindRow::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, +void UnwindRow::dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel) const { OS.indent(2 * IndentLevel); if (hasAddress()) OS << format("0x%" PRIx64 ": ", *Address); OS << "CFA="; - CFAValue.dump(OS, MRI, IsEH); + CFAValue.dump(OS, DumpOpts); if (RegLocs.hasLocations()) { OS << ": "; - RegLocs.dump(OS, MRI, IsEH); + RegLocs.dump(OS, DumpOpts); } OS << "\n"; } raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindRow &Row) { - Row.dump(OS, nullptr, false, 0); + auto DumpOpts = DIDumpOptions(); + Row.dump(OS, DumpOpts, 0); return OS; } -void UnwindTable::dump(raw_ostream &OS, const MCRegisterInfo *MRI, bool IsEH, +void UnwindTable::dump(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel) const { for (const UnwindRow &Row : Rows) - Row.dump(OS, MRI, IsEH, IndentLevel); + Row.dump(OS, DumpOpts, IndentLevel); } raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindTable &Rows) { - Rows.dump(OS, nullptr, false, 0); + auto DumpOpts = DIDumpOptions(); + Rows.dump(OS, DumpOpts, 0); return OS; } @@ -853,7 +854,6 @@ CFIProgram::getOperandTypes() { /// Print \p Opcode's operand number \p OperandIdx which has value \p Operand. void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, bool IsEH, const Instruction &Instr, unsigned OperandIdx, uint64_t Operand) const { assert(OperandIdx < MaxOperands); @@ -901,7 +901,7 @@ void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts, break; case OT_Register: OS << ' '; - printRegister(OS, MRI, IsEH, Operand); + printRegister(OS, DumpOpts, Operand); break; case OT_AddressSpace: OS << format(" in addrspace%" PRId64, Operand); @@ -909,20 +909,19 @@ void CFIProgram::printOperand(raw_ostream &OS, DIDumpOptions DumpOpts, case OT_Expression: assert(Instr.Expression && "missing DWARFExpression object"); OS << " "; - Instr.Expression->print(OS, DumpOpts, MRI, nullptr, IsEH); + Instr.Expression->print(OS, DumpOpts, nullptr); break; } } void CFIProgram::dump(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, bool IsEH, unsigned IndentLevel) const { for (const auto &Instr : Instructions) { uint8_t Opcode = Instr.Opcode; OS.indent(2 * IndentLevel); OS << callFrameString(Opcode) << ":"; for (unsigned i = 0; i < Instr.Ops.size(); ++i) - printOperand(OS, DumpOpts, MRI, IsEH, Instr, i, Instr.Ops[i]); + printOperand(OS, DumpOpts, Instr, i, Instr.Ops[i]); OS << '\n'; } } @@ -939,21 +938,20 @@ constexpr uint64_t getCIEId(bool IsDWARF64, bool IsEH) { return DW_CIE_ID; } -void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, bool IsEH) const { +void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { // A CIE with a zero length is a terminator entry in the .eh_frame section. - if (IsEH && Length == 0) { + if (DumpOpts.IsEH && Length == 0) { OS << format("%08" PRIx64, Offset) << " ZERO terminator\n"; return; } OS << format("%08" PRIx64, Offset) << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length) - << format(" %0*" PRIx64, IsDWARF64 && !IsEH ? 16 : 8, - getCIEId(IsDWARF64, IsEH)) + << format(" %0*" PRIx64, IsDWARF64 && !DumpOpts.IsEH ? 16 : 8, + getCIEId(IsDWARF64, DumpOpts.IsEH)) << " CIE\n" << " Format: " << FormatString(IsDWARF64) << "\n"; - if (IsEH && Version != 1) + if (DumpOpts.IsEH && Version != 1) OS << "WARNING: unsupported CIE version\n"; OS << format(" Version: %d\n", Version) << " Augmentation: \"" << Augmentation << "\"\n"; @@ -974,11 +972,11 @@ void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts, OS << "\n"; } OS << "\n"; - CFIs.dump(OS, DumpOpts, MRI, IsEH); + CFIs.dump(OS, DumpOpts); OS << "\n"; if (Expected RowsOrErr = UnwindTable::create(this)) - RowsOrErr->dump(OS, MRI, IsEH, 1); + RowsOrErr->dump(OS, DumpOpts, 1); else { DumpOpts.RecoverableErrorHandler(joinErrors( createStringError(errc::invalid_argument, @@ -988,11 +986,10 @@ void CIE::dump(raw_ostream &OS, DIDumpOptions DumpOpts, OS << "\n"; } -void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, bool IsEH) const { +void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { OS << format("%08" PRIx64, Offset) << format(" %0*" PRIx64, IsDWARF64 ? 16 : 8, Length) - << format(" %0*" PRIx64, IsDWARF64 && !IsEH ? 16 : 8, CIEPointer) + << format(" %0*" PRIx64, IsDWARF64 && !DumpOpts.IsEH ? 16 : 8, CIEPointer) << " FDE cie="; if (LinkedCIE) OS << format("%08" PRIx64, LinkedCIE->getOffset()); @@ -1003,11 +1000,11 @@ void FDE::dump(raw_ostream &OS, DIDumpOptions DumpOpts, OS << " Format: " << FormatString(IsDWARF64) << "\n"; if (LSDAAddress) OS << format(" LSDA Address: %016" PRIx64 "\n", *LSDAAddress); - CFIs.dump(OS, DumpOpts, MRI, IsEH); + CFIs.dump(OS, DumpOpts); OS << "\n"; if (Expected RowsOrErr = UnwindTable::create(this)) - RowsOrErr->dump(OS, MRI, IsEH, 1); + RowsOrErr->dump(OS, DumpOpts, 1); else { DumpOpts.RecoverableErrorHandler(joinErrors( createStringError(errc::invalid_argument, @@ -1242,15 +1239,15 @@ FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { } void DWARFDebugFrame::dump(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *MRI, std::optional Offset) const { + DumpOpts.IsEH = IsEH; if (Offset) { if (auto *Entry = getEntryAtOffset(*Offset)) - Entry->dump(OS, DumpOpts, MRI, IsEH); + Entry->dump(OS, DumpOpts); return; } OS << "\n"; for (const auto &Entry : Entries) - Entry->dump(OS, DumpOpts, MRI, IsEH); + Entry->dump(OS, DumpOpts); } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp index 58c3889..00c2823 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp @@ -111,20 +111,19 @@ DWARFLocationInterpreter::Interpret(const DWARFLocationEntry &E) { static void dumpExpression(raw_ostream &OS, DIDumpOptions DumpOpts, ArrayRef Data, bool IsLittleEndian, - unsigned AddressSize, const MCRegisterInfo *MRI, - DWARFUnit *U) { + unsigned AddressSize, DWARFUnit *U) { DWARFDataExtractor Extractor(Data, IsLittleEndian, AddressSize); // Note. We do not pass any format to DWARFExpression, even if the // corresponding unit is known. For now, there is only one operation, // DW_OP_call_ref, which depends on the format; it is rarely used, and // is unexpected in location tables. - DWARFExpression(Extractor, AddressSize).print(OS, DumpOpts, MRI, U); + DWARFExpression(Extractor, AddressSize).print(OS, DumpOpts, U); } bool DWARFLocationTable::dumpLocationList( uint64_t *Offset, raw_ostream &OS, std::optional BaseAddr, - const MCRegisterInfo *MRI, const DWARFObject &Obj, DWARFUnit *U, - DIDumpOptions DumpOpts, unsigned Indent) const { + const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts, + unsigned Indent) const { DWARFLocationInterpreter Interp( BaseAddr, [U](uint32_t Index) -> std::optional { if (U) @@ -157,7 +156,7 @@ bool DWARFLocationTable::dumpLocationList( E.Kind != dwarf::DW_LLE_end_of_list) { OS << ": "; dumpExpression(OS, DumpOpts, E.Loc, Data.isLittleEndian(), - Data.getAddressSize(), MRI, U); + Data.getAddressSize(), U); } return true; }); @@ -183,13 +182,13 @@ Error DWARFLocationTable::visitAbsoluteLocationList( }); } -void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, - const DWARFObject &Obj, DIDumpOptions DumpOpts, +void DWARFDebugLoc::dump(raw_ostream &OS, const DWARFObject &Obj, + DIDumpOptions DumpOpts, std::optional DumpOffset) const { auto BaseAddr = std::nullopt; unsigned Indent = 12; if (DumpOffset) { - dumpLocationList(&*DumpOffset, OS, BaseAddr, MRI, Obj, nullptr, DumpOpts, + dumpLocationList(&*DumpOffset, OS, BaseAddr, Obj, nullptr, DumpOpts, Indent); } else { uint64_t Offset = 0; @@ -199,7 +198,7 @@ void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, OS << Separator; Separator = "\n"; - CanContinue = dumpLocationList(&Offset, OS, BaseAddr, MRI, Obj, nullptr, + CanContinue = dumpLocationList(&Offset, OS, BaseAddr, Obj, nullptr, DumpOpts, Indent); OS << '\n'; } @@ -386,8 +385,7 @@ void DWARFDebugLoclists::dumpRawEntry(const DWARFLocationEntry &Entry, } void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size, - raw_ostream &OS, const MCRegisterInfo *MRI, - const DWARFObject &Obj, + raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts) { if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) { OS << "Invalid dump range\n"; @@ -400,8 +398,8 @@ void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size, OS << Separator; Separator = "\n"; - CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, MRI, - Obj, nullptr, DumpOpts, /*Indent=*/12); + CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, Obj, + nullptr, DumpOpts, /*Indent=*/12); OS << '\n'; } } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp index 93e7a7d..26cef87 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp @@ -74,7 +74,6 @@ static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue, assert(FormValue.isFormClass(DWARFFormValue::FC_SectionOffset) && "bad FORM for location list"); DWARFContext &Ctx = U->getContext(); - const MCRegisterInfo *MRI = Ctx.getRegisterInfo(); uint64_t Offset = *FormValue.getAsSectionOffset(); if (FormValue.getForm() == DW_FORM_loclistx) { @@ -85,9 +84,8 @@ static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue, else return; } - U->getLocationTable().dumpLocationList(&Offset, OS, U->getBaseAddress(), MRI, - Ctx.getDWARFObj(), U, DumpOpts, - Indent); + U->getLocationTable().dumpLocationList( + &Offset, OS, U->getBaseAddress(), Ctx.getDWARFObj(), U, DumpOpts, Indent); } static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue, @@ -97,12 +95,11 @@ static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue, FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) && "bad FORM for location expression"); DWARFContext &Ctx = U->getContext(); - const MCRegisterInfo *MRI = Ctx.getRegisterInfo(); ArrayRef Expr = *FormValue.getAsBlock(); DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()), Ctx.isLittleEndian(), 0); DWARFExpression(Data, U->getAddressByteSize(), U->getFormParams().Format) - .print(OS, DumpOpts, MRI, U); + .print(OS, DumpOpts, U); } static DWARFDie resolveReferencedType(DWARFDie D, DWARFFormValue F) { diff --git a/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp b/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp index 061b656..523dee4 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp @@ -8,7 +8,6 @@ #include "llvm/DebugInfo/DWARF/DWARFExpression.h" #include "llvm/DebugInfo/DWARF/DWARFUnit.h" -#include "llvm/MC/MCRegisterInfo.h" #include "llvm/Support/Format.h" #include #include @@ -225,10 +224,11 @@ static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS, } } -bool DWARFExpression::prettyPrintRegisterOp( - DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, uint8_t Opcode, - const uint64_t Operands[2], const MCRegisterInfo *MRI, bool isEH) { - if (!MRI) +bool DWARFExpression::prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS, + DIDumpOptions DumpOpts, + uint8_t Opcode, + const uint64_t Operands[2]) { + if (!DumpOpts.GetNameForDWARFReg) return false; uint64_t DwarfRegNum; @@ -242,19 +242,17 @@ bool DWARFExpression::prettyPrintRegisterOp( else DwarfRegNum = Opcode - DW_OP_reg0; - if (std::optional LLVMRegNum = - MRI->getLLVMRegNum(DwarfRegNum, isEH)) { - if (const char *RegName = MRI->getName(*LLVMRegNum)) { - if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) || - Opcode == DW_OP_bregx) - OS << format(" %s%+" PRId64, RegName, Operands[OpNum]); - else - OS << ' ' << RegName; - - if (Opcode == DW_OP_regval_type) - prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1); - return true; - } + auto RegName = DumpOpts.GetNameForDWARFReg(DwarfRegNum, DumpOpts.IsEH); + if (!RegName.empty()) { + if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) || + Opcode == DW_OP_bregx) + OS << ' ' << RegName << format("%+" PRId64, Operands[OpNum]); + else + OS << ' ' << RegName.data(); + + if (Opcode == DW_OP_regval_type) + prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1); + return true; } return false; @@ -262,8 +260,7 @@ bool DWARFExpression::prettyPrintRegisterOp( bool DWARFExpression::Operation::print(raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, - const MCRegisterInfo *RegInfo, - DWARFUnit *U, bool isEH) const { + DWARFUnit *U) const { if (Error) { OS << ""; return false; @@ -277,7 +274,7 @@ bool DWARFExpression::Operation::print(raw_ostream &OS, DIDumpOptions DumpOpts, (Opcode >= DW_OP_reg0 && Opcode <= DW_OP_reg31) || Opcode == DW_OP_bregx || Opcode == DW_OP_regx || Opcode == DW_OP_regval_type) - if (prettyPrintRegisterOp(U, OS, DumpOpts, Opcode, Operands, RegInfo, isEH)) + if (prettyPrintRegisterOp(U, OS, DumpOpts, Opcode, Operands)) return true; for (unsigned Operand = 0; Operand < 2; ++Operand) { @@ -323,15 +320,15 @@ bool DWARFExpression::Operation::print(raw_ostream &OS, DIDumpOptions DumpOpts, } void DWARFExpression::print(raw_ostream &OS, DIDumpOptions DumpOpts, - const MCRegisterInfo *RegInfo, DWARFUnit *U, - bool IsEH) const { + DWARFUnit *U, bool IsEH) const { uint32_t EntryValExprSize = 0; uint64_t EntryValStartOffset = 0; if (Data.getData().empty()) OS << ""; for (auto &Op : *this) { - if (!Op.print(OS, DumpOpts, this, RegInfo, U, IsEH)) { + DumpOpts.IsEH = IsEH; + if (!Op.print(OS, DumpOpts, this, U)) { uint64_t FailOffset = Op.getEndOffset(); while (FailOffset < Data.getData().size()) OS << format(" %02x", Data.getU8(&FailOffset)); @@ -402,9 +399,11 @@ struct PrintedExpr { PrintedExpr(ExprKind K = Address) : Kind(K) {} }; -static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, - const DWARFExpression::iterator E, - const MCRegisterInfo &MRI) { +static bool printCompactDWARFExpr( + raw_ostream &OS, DWARFExpression::iterator I, + const DWARFExpression::iterator E, + std::function GetNameForDWARFReg = + nullptr) { SmallVector Stack; while (I != E) { @@ -415,27 +414,21 @@ static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, // DW_OP_regx: A register, with the register num given as an operand. // Printed as the plain register name. uint64_t DwarfRegNum = Op.getRawOperand(0); - std::optional LLVMRegNum = - MRI.getLLVMRegNum(DwarfRegNum, false); - if (!LLVMRegNum) { - OS << ""; + auto RegName = GetNameForDWARFReg(DwarfRegNum, false); + if (RegName.empty()) return false; - } raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String); - S << MRI.getName(*LLVMRegNum); + S << RegName; break; } case dwarf::DW_OP_bregx: { int DwarfRegNum = Op.getRawOperand(0); int64_t Offset = Op.getRawOperand(1); - std::optional LLVMRegNum = - MRI.getLLVMRegNum(DwarfRegNum, false); - if (!LLVMRegNum) { - OS << ""; + auto RegName = GetNameForDWARFReg(DwarfRegNum, false); + if (RegName.empty()) return false; - } raw_svector_ostream S(Stack.emplace_back().String); - S << MRI.getName(*LLVMRegNum); + S << RegName; if (Offset) S << format("%+" PRId64, Offset); break; @@ -449,7 +442,7 @@ static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, ++I; raw_svector_ostream S(Stack.emplace_back().String); S << "entry("; - printCompactDWARFExpr(S, I, SubExprEnd, MRI); + printCompactDWARFExpr(S, I, SubExprEnd, GetNameForDWARFReg); S << ")"; I = SubExprEnd; continue; @@ -466,26 +459,20 @@ static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, // DW_OP_reg: A register, with the register num implied by the // opcode. Printed as the plain register name. uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0; - std::optional LLVMRegNum = - MRI.getLLVMRegNum(DwarfRegNum, false); - if (!LLVMRegNum) { - OS << ""; + auto RegName = GetNameForDWARFReg(DwarfRegNum, false); + if (RegName.empty()) return false; - } raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String); - S << MRI.getName(*LLVMRegNum); + S << RegName; } else if (Opcode >= dwarf::DW_OP_breg0 && Opcode <= dwarf::DW_OP_breg31) { int DwarfRegNum = Opcode - dwarf::DW_OP_breg0; int64_t Offset = Op.getRawOperand(0); - std::optional LLVMRegNum = - MRI.getLLVMRegNum(DwarfRegNum, false); - if (!LLVMRegNum) { - OS << ""; + auto RegName = GetNameForDWARFReg(DwarfRegNum, false); + if (RegName.empty()) return false; - } raw_svector_ostream S(Stack.emplace_back().String); - S << MRI.getName(*LLVMRegNum); + S << RegName; if (Offset) S << format("%+" PRId64, Offset); } else { @@ -510,8 +497,10 @@ static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, return true; } -bool DWARFExpression::printCompact(raw_ostream &OS, const MCRegisterInfo &MRI) { - return printCompactDWARFExpr(OS, begin(), end(), MRI); +bool DWARFExpression::printCompact( + raw_ostream &OS, + std::function GetNameForDWARFReg) { + return printCompactDWARFExpr(OS, begin(), end(), GetNameForDWARFReg); } bool DWARFExpression::operator==(const DWARFExpression &RHS) const { diff --git a/llvm/lib/DebugInfo/LogicalView/Readers/LVELFReader.cpp b/llvm/lib/DebugInfo/LogicalView/Readers/LVELFReader.cpp index 37bd535..1eac529 100644 --- a/llvm/lib/DebugInfo/LogicalView/Readers/LVELFReader.cpp +++ b/llvm/lib/DebugInfo/LogicalView/Readers/LVELFReader.cpp @@ -770,9 +770,19 @@ std::string LVELFReader::getRegisterName(LVSmall Opcode, uint64_t Operands[2]) { std::string string; raw_string_ostream Stream(string); DIDumpOptions DumpOpts; + auto *MCRegInfo = MRI.get(); + auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, bool IsEH) -> StringRef { + if (!MCRegInfo) + return {}; + if (std::optional LLVMRegNum = + MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH)) + if (const char *RegName = MCRegInfo->getName(*LLVMRegNum)) + return StringRef(RegName); + return {}; + }; + DumpOpts.GetNameForDWARFReg = GetRegName; DWARFExpression::prettyPrintRegisterOp(/*U=*/nullptr, Stream, DumpOpts, - Opcode, Operands, MRI.get(), - /*isEH=*/false); + Opcode, Operands); return Stream.str(); } diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index 59e7361..c151129 100644 --- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -18,6 +18,8 @@ #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h" #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/TargetRegistry.h" #include "llvm/Object/Archive.h" #include "llvm/Object/MachOUniversal.h" #include "llvm/Object/ObjectFile.h" @@ -340,9 +342,11 @@ using HandlerFn = std::function; /// Print only DIEs that have a certain name. -static bool filterByName(const StringSet<> &Names, DWARFDie Die, - StringRef NameRef, raw_ostream &OS) { +static bool filterByName( + const StringSet<> &Names, DWARFDie Die, StringRef NameRef, raw_ostream &OS, + std::function GetNameForDWARFReg) { DIDumpOptions DumpOpts = getDumpOpts(Die.getDwarfUnit()->getContext()); + DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg; std::string Name = (IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str(); if (UseRegex) { @@ -368,17 +372,18 @@ static bool filterByName(const StringSet<> &Names, DWARFDie Die, } /// Print only DIEs that have a certain name. -static void filterByName(const StringSet<> &Names, - DWARFContext::unit_iterator_range CUs, - raw_ostream &OS) { +static void filterByName( + const StringSet<> &Names, DWARFContext::unit_iterator_range CUs, + raw_ostream &OS, + std::function GetNameForDWARFReg) { for (const auto &CU : CUs) for (const auto &Entry : CU->dies()) { DWARFDie Die = {CU.get(), &Entry}; if (const char *Name = Die.getName(DINameKind::ShortName)) - if (filterByName(Names, Die, Name, OS)) + if (filterByName(Names, Die, Name, OS, GetNameForDWARFReg)) continue; if (const char *Name = Die.getName(DINameKind::LinkageName)) - filterByName(Names, Die, Name, OS); + filterByName(Names, Die, Name, OS, GetNameForDWARFReg); } } @@ -422,8 +427,9 @@ static void getDies(DWARFContext &DICtx, const DWARFDebugNames &Accel, } /// Print only DIEs that have a certain name. -static void filterByAccelName(ArrayRef Names, DWARFContext &DICtx, - raw_ostream &OS) { +static void filterByAccelName( + ArrayRef Names, DWARFContext &DICtx, raw_ostream &OS, + std::function GetNameForDWARFReg) { SmallVector Dies; for (const auto &Name : Names) { getDies(DICtx, DICtx.getAppleNames(), Name, Dies); @@ -435,6 +441,7 @@ static void filterByAccelName(ArrayRef Names, DWARFContext &DICtx, Dies.erase(std::unique(Dies.begin(), Dies.end()), Dies.end()); DIDumpOptions DumpOpts = getDumpOpts(DICtx); + DumpOpts.GetNameForDWARFReg = GetNameForDWARFReg; for (DWARFDie Die : Dies) Die.dump(OS, 0, DumpOpts); } @@ -551,10 +558,41 @@ static bool collectObjectSources(ObjectFile &Obj, DWARFContext &DICtx, return Result; } +static std::unique_ptr +createRegInfo(const object::ObjectFile &Obj) { + std::unique_ptr MCRegInfo; + Triple TT; + TT.setArch(Triple::ArchType(Obj.getArch())); + TT.setVendor(Triple::UnknownVendor); + TT.setOS(Triple::UnknownOS); + std::string TargetLookupError; + const Target *TheTarget = + TargetRegistry::lookupTarget(TT.str(), TargetLookupError); + if (!TargetLookupError.empty()) + return nullptr; + MCRegInfo.reset(TheTarget->createMCRegInfo(TT.str())); + return MCRegInfo; +} + static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, const Twine &Filename, raw_ostream &OS) { - logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(), - Filename.str() + ": "); + + auto MCRegInfo = createRegInfo(Obj); + if (!MCRegInfo) + logAllUnhandledErrors(createStringError(inconvertibleErrorCode(), + "Error in creating MCRegInfo"), + errs(), Filename.str() + ": "); + + auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum, bool IsEH) -> StringRef { + if (!MCRegInfo) + return {}; + if (std::optional LLVMRegNum = + MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH)) + if (const char *RegName = MCRegInfo->getName(*LLVMRegNum)) + return StringRef(RegName); + return {}; + }; + // The UUID dump already contains all the same information. if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All) OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n'; @@ -569,19 +607,21 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, for (auto name : Name) Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name); - filterByName(Names, DICtx.normal_units(), OS); - filterByName(Names, DICtx.dwo_units(), OS); + filterByName(Names, DICtx.normal_units(), OS, GetRegName); + filterByName(Names, DICtx.dwo_units(), OS, GetRegName); return true; } // Handle the --find option and lower it to --debug-info=. if (!Find.empty()) { - filterByAccelName(Find, DICtx, OS); + filterByAccelName(Find, DICtx, OS, GetRegName); return true; } // Dump the complete DWARF structure. - DICtx.dump(OS, getDumpOpts(DICtx), DumpOffsets); + auto DumpOpts = getDumpOpts(DICtx); + DumpOpts.GetNameForDWARFReg = GetRegName; + DICtx.dump(OS, DumpOpts, DumpOffsets); return true; } diff --git a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp index ac16ae8..943c056 100644 --- a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp +++ b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp @@ -286,7 +286,6 @@ static llvm::Error handleObjectFile(ObjectFile &Obj, if (!DICtx) return createStringError(std::errc::invalid_argument, "unable to create DWARF context"); - logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), OS, "DwarfTransformer: "); // Make a DWARF transformer object and populate the ranges of the code // so we don't end up adding invalid functions to GSYM data. diff --git a/llvm/tools/llvm-objdump/SourcePrinter.cpp b/llvm/tools/llvm-objdump/SourcePrinter.cpp index d0b80fd..6736cbc 100644 --- a/llvm/tools/llvm-objdump/SourcePrinter.cpp +++ b/llvm/tools/llvm-objdump/SourcePrinter.cpp @@ -42,7 +42,17 @@ void LiveVariable::print(raw_ostream &OS, const MCRegisterInfo &MRI) const { DataExtractor Data({LocExpr.Expr.data(), LocExpr.Expr.size()}, Unit->getContext().isLittleEndian(), 0); DWARFExpression Expression(Data, Unit->getAddressByteSize()); - Expression.printCompact(OS, MRI); + + auto GetRegName = [&MRI, &OS](uint64_t DwarfRegNum, bool IsEH) -> StringRef { + if (std::optional LLVMRegNum = + MRI.getLLVMRegNum(DwarfRegNum, IsEH)) + if (const char *RegName = MRI.getName(*LLVMRegNum)) + return StringRef(RegName); + OS << ""; + return {}; + }; + + Expression.printCompact(OS, GetRegName); } void LiveVariablePrinter::addVariable(DWARFDie FuncDie, DWARFDie VarDie) { diff --git a/llvm/tools/llvm-objdump/SourcePrinter.h b/llvm/tools/llvm-objdump/SourcePrinter.h index 29ef19c..6209bb0 100644 --- a/llvm/tools/llvm-objdump/SourcePrinter.h +++ b/llvm/tools/llvm-objdump/SourcePrinter.h @@ -13,6 +13,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/DebugInfo/Symbolize/Symbolize.h" +#include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCSubtargetInfo.h" #include "llvm/Support/FormattedStream.h" #include diff --git a/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h b/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h index 5dc947e..365a598 100644 --- a/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h +++ b/llvm/tools/llvm-readobj/DwarfCFIEHPrinter.h @@ -226,8 +226,9 @@ void PrinterContext::printEHFrame(const Elf_Shdr *EHFrameShdr) const { W.getOStream() << "\n"; W.startLine() << "Program:\n"; W.indent(); - Entry.cfis().dump(W.getOStream(), DIDumpOptions(), nullptr, - W.getIndentLevel()); + auto DumpOpts = DIDumpOptions(); + DumpOpts.IsEH = true; + Entry.cfis().dump(W.getOStream(), DumpOpts, W.getIndentLevel()); W.unindent(); W.unindent(); W.getOStream() << "\n"; diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp index 155b799..d061381 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp @@ -41,7 +41,9 @@ void expectDumpResult(const dwarf::CIE &TestCIE, bool IsEH, StringRef ExpectedFirstLine) { std::string Output; raw_string_ostream OS(Output); - TestCIE.dump(OS, DIDumpOptions(), /*MRI=*/nullptr, IsEH); + auto DumpOpts = DIDumpOptions(); + DumpOpts.IsEH = IsEH; + TestCIE.dump(OS, DumpOpts); OS.flush(); StringRef FirstLine = StringRef(Output).split('\n').first; EXPECT_EQ(FirstLine, ExpectedFirstLine); @@ -51,7 +53,9 @@ void expectDumpResult(const dwarf::FDE &TestFDE, bool IsEH, StringRef ExpectedFirstLine) { std::string Output; raw_string_ostream OS(Output); - TestFDE.dump(OS, DIDumpOptions(), /*MRI=*/nullptr, IsEH); + auto DumpOpts = DIDumpOptions(); + DumpOpts.IsEH = IsEH; + TestFDE.dump(OS, DumpOpts); OS.flush(); StringRef FirstLine = StringRef(Output).split('\n').first; EXPECT_EQ(FirstLine, ExpectedFirstLine); diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFExpressionCompactPrinterTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFExpressionCompactPrinterTest.cpp index 50b8b6b..e0c5918 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFExpressionCompactPrinterTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFExpressionCompactPrinterTest.cpp @@ -60,7 +60,17 @@ void DWARFExpressionCompactPrinterTest::TestExprPrinter( raw_string_ostream OS(Result); DataExtractor DE(ExprData, true, 8); DWARFExpression Expr(DE, 8); - Expr.printCompact(OS, *MRI); + + auto GetRegName = [&](uint64_t DwarfRegNum, bool IsEH) -> StringRef { + if (std::optional LLVMRegNum = + this->MRI->getLLVMRegNum(DwarfRegNum, IsEH)) + if (const char *RegName = this->MRI->getName(*LLVMRegNum)) + return llvm::StringRef(RegName); + OS << ""; + return {}; + }; + + Expr.printCompact(OS, GetRegName); EXPECT_EQ(OS.str(), Expected); } -- 2.7.4