diff --git a/llvm/include/llvm/DebugInfo/BTF/BTFContext.h b/llvm/include/llvm/DebugInfo/BTF/BTFContext.h index c16bee6133220..e3f7f57749ff1 100644 --- a/llvm/include/llvm/DebugInfo/BTF/BTFContext.h +++ b/llvm/include/llvm/DebugInfo/BTF/BTFContext.h @@ -30,11 +30,11 @@ class BTFContext final : public DIContext { // BTF is no DWARF, so ignore this operation for now. } - DILineInfo getLineInfoForAddress( + std::optional getLineInfoForAddress( object::SectionedAddress Address, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; - DILineInfo + std::optional getLineInfoForDataAddress(object::SectionedAddress Address) override; DILineInfoTable getLineInfoForAddressRange( diff --git a/llvm/include/llvm/DebugInfo/DIContext.h b/llvm/include/llvm/DebugInfo/DIContext.h index 71685ba09d8db..c90b99987f1db 100644 --- a/llvm/include/llvm/DebugInfo/DIContext.h +++ b/llvm/include/llvm/DebugInfo/DIContext.h @@ -252,10 +252,12 @@ class DIContext { return true; } - virtual DILineInfo getLineInfoForAddress( + // For getLineInfoForAddress and getLineInfoForDataAddress, std::nullopt is + // returned when debug info is missing for the given address. + virtual std::optional getLineInfoForAddress( object::SectionedAddress Address, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0; - virtual DILineInfo + virtual std::optional getLineInfoForDataAddress(object::SectionedAddress Address) = 0; virtual DILineInfoTable getLineInfoForAddressRange( object::SectionedAddress Address, uint64_t Size, diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h index 0d6e2b076cc34..6df3f5066e327 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -386,10 +386,10 @@ class DWARFContext : public DIContext { /// executable's debug info. DIEsForAddress getDIEsForAddress(uint64_t Address, bool CheckDWO = false); - DILineInfo getLineInfoForAddress( + std::optional getLineInfoForAddress( object::SectionedAddress Address, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; - DILineInfo + std::optional getLineInfoForDataAddress(object::SectionedAddress Address) override; DILineInfoTable getLineInfoForAddressRange( object::SectionedAddress Address, uint64_t Size, diff --git a/llvm/include/llvm/DebugInfo/PDB/PDBContext.h b/llvm/include/llvm/DebugInfo/PDB/PDBContext.h index 3163c0a1dae03..fedfcd483dcc3 100644 --- a/llvm/include/llvm/DebugInfo/PDB/PDBContext.h +++ b/llvm/include/llvm/DebugInfo/PDB/PDBContext.h @@ -42,10 +42,10 @@ namespace pdb { void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override; - DILineInfo getLineInfoForAddress( + std::optional getLineInfoForAddress( object::SectionedAddress Address, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override; - DILineInfo + std::optional getLineInfoForDataAddress(object::SectionedAddress Address) override; DILineInfoTable getLineInfoForAddressRange( object::SectionedAddress Address, uint64_t Size, diff --git a/llvm/lib/DebugInfo/BTF/BTFContext.cpp b/llvm/lib/DebugInfo/BTF/BTFContext.cpp index 2e651cb378dbf..412bd42ee0b67 100644 --- a/llvm/lib/DebugInfo/BTF/BTFContext.cpp +++ b/llvm/lib/DebugInfo/BTF/BTFContext.cpp @@ -20,12 +20,13 @@ using namespace llvm; using object::ObjectFile; using object::SectionedAddress; -DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address, - DILineInfoSpecifier Specifier) { +std::optional +BTFContext::getLineInfoForAddress(SectionedAddress Address, + DILineInfoSpecifier Specifier) { const BTF::BPFLineInfo *LineInfo = BTF.findLineInfo(Address); DILineInfo Result; if (!LineInfo) - return Result; + return std::nullopt; Result.LineSource = BTF.findString(LineInfo->LineOff); Result.FileName = BTF.findString(LineInfo->FileNameOff); @@ -34,9 +35,10 @@ DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address, return Result; } -DILineInfo BTFContext::getLineInfoForDataAddress(SectionedAddress Address) { +std::optional +BTFContext::getLineInfoForDataAddress(SectionedAddress Address) { // BTF does not convey such information. - return {}; + return std::nullopt; } DILineInfoTable diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 99e1642ff23ad..5e5dcb1ae941d 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1730,8 +1730,9 @@ DWARFContext::getLocalsForAddress(object::SectionedAddress Address) { return Result; } -DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, - DILineInfoSpecifier Spec) { +std::optional +DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, + DILineInfoSpecifier Spec) { DILineInfo Result; DWARFCompileUnit *CU = getCompileUnitForCodeAddress(Address.Address); if (!CU) @@ -1751,7 +1752,7 @@ DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, return Result; } -DILineInfo +std::optional DWARFContext::getLineInfoForDataAddress(object::SectionedAddress Address) { DILineInfo Result; DWARFCompileUnit *CU = getCompileUnitForDataAddress(Address.Address); diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp index 568af5ee8e3ae..4107f843123f8 100644 --- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp +++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp @@ -729,7 +729,7 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath, uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames(); if (NumDwarfInlineInfos == 0) { DwarfInlineInfos.addFrame( - DICtx.getLineInfoForAddress(SectAddr, DLIS)); + DICtx.getLineInfoForAddress(SectAddr, DLIS).value_or(DILineInfo())); } // Check for 1 entry that has no file and line info diff --git a/llvm/lib/DebugInfo/PDB/PDBContext.cpp b/llvm/lib/DebugInfo/PDB/PDBContext.cpp index e600fb7385f13..5fff484a7302f 100644 --- a/llvm/lib/DebugInfo/PDB/PDBContext.cpp +++ b/llvm/lib/DebugInfo/PDB/PDBContext.cpp @@ -32,8 +32,9 @@ PDBContext::PDBContext(const COFFObjectFile &Object, void PDBContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts){} -DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address, - DILineInfoSpecifier Specifier) { +std::optional +PDBContext::getLineInfoForAddress(object::SectionedAddress Address, + DILineInfoSpecifier Specifier) { DILineInfo Result; Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind); @@ -64,7 +65,7 @@ DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address, return Result; } -DILineInfo +std::optional PDBContext::getLineInfoForDataAddress(object::SectionedAddress Address) { // Unimplemented. S_GDATA and S_LDATA in CodeView (used to describe global // variables) aren't capable of carrying line information. @@ -84,9 +85,10 @@ PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address, return Table; while (auto LineInfo = LineNumbers->getNext()) { - DILineInfo LineEntry = getLineInfoForAddress( - {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier); - Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry)); + if (std::optional LineEntry = getLineInfoForAddress( + {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier)) + Table.push_back( + std::make_pair(LineInfo->getVirtualAddress(), *LineEntry)); } return Table; } @@ -95,7 +97,8 @@ DIInliningInfo PDBContext::getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier) { DIInliningInfo InlineInfo; - DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier); + DILineInfo CurrentLine = + getLineInfoForAddress(Address, Specifier).value_or(DILineInfo()); // Find the function at this address. std::unique_ptr ParentFunc = diff --git a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp index d5e1dc759df5c..dcd6188daf580 100644 --- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp @@ -276,8 +276,11 @@ SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset, if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection) ModuleOffset.SectionIndex = getModuleSectionIndexForAddress(ModuleOffset.Address); - DILineInfo LineInfo = - DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier); + DILineInfo LineInfo; + if (std::optional DBGLineInfo = + DebugInfoContext->getLineInfoForAddress(ModuleOffset, + LineInfoSpecifier)) + LineInfo = *DBGLineInfo; // Override function name from symbol table if necessary. if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) { @@ -334,10 +337,11 @@ DIGlobal SymbolizableObjectFile::symbolizeData( Res.DeclFile = FileName; // Try and get a better filename:lineno pair from the debuginfo, if present. - DILineInfo DL = DebugInfoContext->getLineInfoForDataAddress(ModuleOffset); - if (DL.Line != 0) { - Res.DeclFile = DL.FileName; - Res.DeclLine = DL.Line; + std::optional DL = + DebugInfoContext->getLineInfoForDataAddress(ModuleOffset); + if (DL && DL->Line != 0) { + Res.DeclFile = DL->FileName; + Res.DeclLine = DL->Line; } return Res; } diff --git a/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp b/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp index 1f4557217cf24..6a00b87dd0a6b 100644 --- a/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp @@ -71,7 +71,8 @@ static VTuneMethodBatch getMethodBatch(LinkGraph &G, bool EmitDebugInfo) { SAddr, Sym->getSize(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath); Method.SourceFileSI = Batch.Strings.size(); - Batch.Strings.push_back(DC->getLineInfoForAddress(SAddr).FileName); + Batch.Strings.push_back( + DC->getLineInfoForAddress(SAddr).value_or(DILineInfo()).FileName); for (auto &LInfo : LinesInfo) { Method.LineTable.push_back( std::pair{/*unsigned*/ Sym->getOffset(), diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index d63c51566e80c..0e0394f606f92 100644 --- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -565,9 +565,13 @@ static bool lookup(ObjectFile &Obj, DWARFContext &DICtx, uint64_t Address, // TODO: it is neccessary to set proper SectionIndex here. // object::SectionedAddress::UndefSection works for only absolute addresses. - if (DILineInfo LineInfo = DICtx.getLineInfoForAddress( - {Lookup, object::SectionedAddress::UndefSection})) + if (DILineInfo LineInfo = + DICtx + .getLineInfoForAddress( + {Lookup, object::SectionedAddress::UndefSection}) + .value_or(DILineInfo())) { LineInfo.dump(OS); + } return true; } diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp index ab6f65cd41a36..d0b4ed6705459 100644 --- a/llvm/tools/llvm-objdump/MachODump.cpp +++ b/llvm/tools/llvm-objdump/MachODump.cpp @@ -7646,7 +7646,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, // Print debug info. if (diContext) { - DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}); + DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}) + .value_or(DILineInfo()); // Print valid line info if it changed. if (dli != lastLine && dli.Line != 0) outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' diff --git a/llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp b/llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp index 5b203adfeb284..7bcc709bffbf0 100644 --- a/llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp +++ b/llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp @@ -343,17 +343,15 @@ TEST(BTFParserTest, btfContext) { BTFParser BTF; std::unique_ptr Ctx = BTFContext::create(Mock.makeObj()); - DILineInfo I1 = Ctx->getLineInfoForAddress({16, 1}); - EXPECT_EQ(I1.Line, 7u); - EXPECT_EQ(I1.Column, 1u); - EXPECT_EQ(I1.FileName, "a.c"); - EXPECT_EQ(I1.LineSource, "first line"); - - DILineInfo I2 = Ctx->getLineInfoForAddress({24, 1}); - EXPECT_EQ(I2.Line, 0u); - EXPECT_EQ(I2.Column, 0u); - EXPECT_EQ(I2.FileName, DILineInfo::BadString); - EXPECT_EQ(I2.LineSource, std::nullopt); + std::optional I1 = Ctx->getLineInfoForAddress({16, 1}); + EXPECT_TRUE(I1.has_value()); + EXPECT_EQ(I1->Line, 7u); + EXPECT_EQ(I1->Column, 1u); + EXPECT_EQ(I1->FileName, "a.c"); + EXPECT_EQ(I1->LineSource, "first line"); + + std::optional I2 = Ctx->getLineInfoForAddress({24, 1}); + EXPECT_FALSE(I2.has_value()); } static uint32_t mkInfo(uint32_t Kind) { return Kind << 24; }