From e587f0e12d1d9c04d2179198262092c3588fadcc Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Tue, 4 Mar 2025 14:45:27 -0800 Subject: [PATCH 1/3] [NFC][DebugInfo] Wrap DILineInfo return type with std::optional to handle missing debug info. --- llvm/include/llvm/DebugInfo/BTF/BTFContext.h | 4 +-- llvm/include/llvm/DebugInfo/DIContext.h | 6 +++-- .../llvm/DebugInfo/DWARF/DWARFContext.h | 4 +-- llvm/include/llvm/DebugInfo/PDB/PDBContext.h | 4 +-- llvm/lib/DebugInfo/BTF/BTFContext.cpp | 12 +++++---- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 24 ++++++++++------- llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp | 7 +++-- llvm/lib/DebugInfo/PDB/PDBContext.cpp | 27 ++++++++++++------- .../Symbolize/SymbolizableObjectFile.cpp | 16 ++++++----- .../Orc/Debugging/VTuneSupportPlugin.cpp | 5 +++- llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 6 +++-- llvm/tools/llvm-objdump/MachODump.cpp | 11 ++++---- .../unittests/DebugInfo/BTF/BTFParserTest.cpp | 20 +++++++------- 13 files changed, 86 insertions(+), 60 deletions(-) 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..afa91b098efe5 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1730,40 +1730,44 @@ DWARFContext::getLocalsForAddress(object::SectionedAddress Address) { return Result; } -DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, - DILineInfoSpecifier Spec) { - DILineInfo Result; +std::optional +DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, + DILineInfoSpecifier Spec) { DWARFCompileUnit *CU = getCompileUnitForCodeAddress(Address.Address); if (!CU) - return Result; + return std::nullopt; - getFunctionNameAndStartLineForAddress( + DILineInfo Result; + bool HasDebugInfoForAddress = getFunctionNameAndStartLineForAddress( CU, Address.Address, Spec.FNKind, Spec.FLIKind, Result.FunctionName, Result.StartFileName, Result.StartLine, Result.StartAddress); if (Spec.FLIKind != FileLineInfoKind::None) { if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) { - LineTable->getFileLineInfoForAddress( + HasDebugInfoForAddress |= LineTable->getFileLineInfoForAddress( {Address.Address, Address.SectionIndex}, Spec.ApproximateLine, CU->getCompilationDir(), Spec.FLIKind, Result); } } + if (!HasDebugInfoForAddress) + return std::nullopt; return Result; } -DILineInfo +std::optional DWARFContext::getLineInfoForDataAddress(object::SectionedAddress Address) { - DILineInfo Result; DWARFCompileUnit *CU = getCompileUnitForDataAddress(Address.Address); if (!CU) - return Result; + return std::nullopt; if (DWARFDie Die = CU->getVariableForAddress(Address.Address)) { + DILineInfo Result; Result.FileName = Die.getDeclFile(FileLineInfoKind::AbsoluteFilePath); Result.Line = Die.getDeclLine(); + return Result; } - return Result; + return std::nullopt; } DILineInfoTable DWARFContext::getLineInfoForAddressRange( diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp index 568af5ee8e3ae..23d9c1f5ddf2c 100644 --- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp +++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp @@ -728,8 +728,11 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath, DICtx.getInliningInfoForAddress(SectAddr, DLIS); uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames(); if (NumDwarfInlineInfos == 0) { - DwarfInlineInfos.addFrame( - DICtx.getLineInfoForAddress(SectAddr, DLIS)); + if (std::optional DwarfLineInfo = + DICtx.getLineInfoForAddress(SectAddr, DLIS)) + DwarfInlineInfos.addFrame(*DwarfLineInfo); + else + DwarfInlineInfos.addFrame(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..dd38295ac62b5 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,11 +65,11 @@ 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. - return DILineInfo(); + return std::nullopt; } DILineInfoTable @@ -84,9 +85,11 @@ PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address, return Table; while (auto LineInfo = LineNumbers->getNext()) { - DILineInfo LineEntry = getLineInfoForAddress( + std::optional LineEntry = getLineInfoForAddress( {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier); - Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry)); + if (LineEntry) + Table.push_back( + std::make_pair(LineInfo->getVirtualAddress(), *LineEntry)); } return Table; } @@ -95,19 +98,22 @@ DIInliningInfo PDBContext::getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier) { DIInliningInfo InlineInfo; - DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier); + std::optional CurrentLine = + getLineInfoForAddress(Address, Specifier); // Find the function at this address. std::unique_ptr ParentFunc = Session->findSymbolByAddress(Address.Address, PDB_SymType::Function); if (!ParentFunc) { - InlineInfo.addFrame(CurrentLine); + if (CurrentLine) + InlineInfo.addFrame(*CurrentLine); return InlineInfo; } auto Frames = ParentFunc->findInlineFramesByVA(Address.Address); if (!Frames || Frames->getChildCount() == 0) { - InlineInfo.addFrame(CurrentLine); + if (CurrentLine) + InlineInfo.addFrame(*CurrentLine); return InlineInfo; } @@ -131,7 +137,8 @@ PDBContext::getInliningInfoForAddress(object::SectionedAddress Address, InlineInfo.addFrame(LineInfo); } - InlineInfo.addFrame(CurrentLine); + if (CurrentLine) + InlineInfo.addFrame(*CurrentLine); return InlineInfo; } 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..086a09b08db19 100644 --- a/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp @@ -71,7 +71,10 @@ 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); + if (std::optional LineInfo = DC->getLineInfoForAddress(SAddr)) + Batch.Strings.push_back(LineInfo->FileName); + else + Batch.Strings.push_back(DILineInfo::BadString); 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..d35cae29c9aa2 100644 --- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -565,9 +565,11 @@ 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( + DILineInfo LineInfo; + if (std::optional DBGLineInfo = DICtx.getLineInfoForAddress( {Lookup, object::SectionedAddress::UndefSection})) - LineInfo.dump(OS); + LineInfo = *DBGLineInfo; + LineInfo.dump(OS); return true; } diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp index ab6f65cd41a36..09b684f11839f 100644 --- a/llvm/tools/llvm-objdump/MachODump.cpp +++ b/llvm/tools/llvm-objdump/MachODump.cpp @@ -7594,7 +7594,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, else outs() << SymName << ":\n"; - DILineInfo lastLine; + std::optional lastLine; for (uint64_t Index = Start; Index < End; Index += Size) { MCInst Inst; @@ -7646,11 +7646,12 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, // Print debug info. if (diContext) { - DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}); + std::optional dli = + diContext->getLineInfoForAddress({PC, SectIdx}); // Print valid line info if it changed. - if (dli != lastLine && dli.Line != 0) - outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' - << dli.Column; + if (dli && dli != lastLine && dli->Line != 0) + outs() << "\t## " << dli->FileName << ':' << dli->Line << ':' + << dli->Column; lastLine = dli; } outs() << "\n"; 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; } From 6b18a46f9011fcb8d51badb7313946bf588c9d84 Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Wed, 12 Mar 2025 16:56:25 -0700 Subject: [PATCH 2/3] Address comments --- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 17 +++++++---------- llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp | 7 ++----- llvm/lib/DebugInfo/PDB/PDBContext.cpp | 20 ++++++++------------ llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 12 +++++++----- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index afa91b098efe5..5e5dcb1ae941d 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1733,41 +1733,38 @@ DWARFContext::getLocalsForAddress(object::SectionedAddress Address) { std::optional DWARFContext::getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Spec) { + DILineInfo Result; DWARFCompileUnit *CU = getCompileUnitForCodeAddress(Address.Address); if (!CU) - return std::nullopt; + return Result; - DILineInfo Result; - bool HasDebugInfoForAddress = getFunctionNameAndStartLineForAddress( + getFunctionNameAndStartLineForAddress( CU, Address.Address, Spec.FNKind, Spec.FLIKind, Result.FunctionName, Result.StartFileName, Result.StartLine, Result.StartAddress); if (Spec.FLIKind != FileLineInfoKind::None) { if (const DWARFLineTable *LineTable = getLineTableForUnit(CU)) { - HasDebugInfoForAddress |= LineTable->getFileLineInfoForAddress( + LineTable->getFileLineInfoForAddress( {Address.Address, Address.SectionIndex}, Spec.ApproximateLine, CU->getCompilationDir(), Spec.FLIKind, Result); } } - if (!HasDebugInfoForAddress) - return std::nullopt; return Result; } std::optional DWARFContext::getLineInfoForDataAddress(object::SectionedAddress Address) { + DILineInfo Result; DWARFCompileUnit *CU = getCompileUnitForDataAddress(Address.Address); if (!CU) - return std::nullopt; + return Result; if (DWARFDie Die = CU->getVariableForAddress(Address.Address)) { - DILineInfo Result; Result.FileName = Die.getDeclFile(FileLineInfoKind::AbsoluteFilePath); Result.Line = Die.getDeclLine(); - return Result; } - return std::nullopt; + return Result; } DILineInfoTable DWARFContext::getLineInfoForAddressRange( diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp index 23d9c1f5ddf2c..4107f843123f8 100644 --- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp +++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp @@ -728,11 +728,8 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath, DICtx.getInliningInfoForAddress(SectAddr, DLIS); uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames(); if (NumDwarfInlineInfos == 0) { - if (std::optional DwarfLineInfo = - DICtx.getLineInfoForAddress(SectAddr, DLIS)) - DwarfInlineInfos.addFrame(*DwarfLineInfo); - else - DwarfInlineInfos.addFrame(DILineInfo()); + DwarfInlineInfos.addFrame( + 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 dd38295ac62b5..5fff484a7302f 100644 --- a/llvm/lib/DebugInfo/PDB/PDBContext.cpp +++ b/llvm/lib/DebugInfo/PDB/PDBContext.cpp @@ -69,7 +69,7 @@ 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. - return std::nullopt; + return DILineInfo(); } DILineInfoTable @@ -85,9 +85,8 @@ PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address, return Table; while (auto LineInfo = LineNumbers->getNext()) { - std::optional LineEntry = getLineInfoForAddress( - {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier); - if (LineEntry) + if (std::optional LineEntry = getLineInfoForAddress( + {LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier)) Table.push_back( std::make_pair(LineInfo->getVirtualAddress(), *LineEntry)); } @@ -98,22 +97,20 @@ DIInliningInfo PDBContext::getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier) { DIInliningInfo InlineInfo; - std::optional CurrentLine = - getLineInfoForAddress(Address, Specifier); + DILineInfo CurrentLine = + getLineInfoForAddress(Address, Specifier).value_or(DILineInfo()); // Find the function at this address. std::unique_ptr ParentFunc = Session->findSymbolByAddress(Address.Address, PDB_SymType::Function); if (!ParentFunc) { - if (CurrentLine) - InlineInfo.addFrame(*CurrentLine); + InlineInfo.addFrame(CurrentLine); return InlineInfo; } auto Frames = ParentFunc->findInlineFramesByVA(Address.Address); if (!Frames || Frames->getChildCount() == 0) { - if (CurrentLine) - InlineInfo.addFrame(*CurrentLine); + InlineInfo.addFrame(CurrentLine); return InlineInfo; } @@ -137,8 +134,7 @@ PDBContext::getInliningInfoForAddress(object::SectionedAddress Address, InlineInfo.addFrame(LineInfo); } - if (CurrentLine) - InlineInfo.addFrame(*CurrentLine); + InlineInfo.addFrame(CurrentLine); return InlineInfo; } diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index d35cae29c9aa2..0e0394f606f92 100644 --- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -565,11 +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. - DILineInfo LineInfo; - if (std::optional DBGLineInfo = DICtx.getLineInfoForAddress( - {Lookup, object::SectionedAddress::UndefSection})) - LineInfo = *DBGLineInfo; - LineInfo.dump(OS); + if (DILineInfo LineInfo = + DICtx + .getLineInfoForAddress( + {Lookup, object::SectionedAddress::UndefSection}) + .value_or(DILineInfo())) { + LineInfo.dump(OS); + } return true; } From d1aa867f41f5e67ddd8a5469f2fdb5cc40fa5f7b Mon Sep 17 00:00:00 2001 From: Zequan Wu Date: Mon, 17 Mar 2025 13:57:20 -0700 Subject: [PATCH 3/3] restore behavior in MachODump.cpp and VTuneSupportPlugin.cpp --- .../Orc/Debugging/VTuneSupportPlugin.cpp | 6 ++---- llvm/tools/llvm-objdump/MachODump.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp b/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp index 086a09b08db19..6a00b87dd0a6b 100644 --- a/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp @@ -71,10 +71,8 @@ static VTuneMethodBatch getMethodBatch(LinkGraph &G, bool EmitDebugInfo) { SAddr, Sym->getSize(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath); Method.SourceFileSI = Batch.Strings.size(); - if (std::optional LineInfo = DC->getLineInfoForAddress(SAddr)) - Batch.Strings.push_back(LineInfo->FileName); - else - Batch.Strings.push_back(DILineInfo::BadString); + 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-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp index 09b684f11839f..d0b4ed6705459 100644 --- a/llvm/tools/llvm-objdump/MachODump.cpp +++ b/llvm/tools/llvm-objdump/MachODump.cpp @@ -7594,7 +7594,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, else outs() << SymName << ":\n"; - std::optional lastLine; + DILineInfo lastLine; for (uint64_t Index = Start; Index < End; Index += Size) { MCInst Inst; @@ -7646,12 +7646,12 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, // Print debug info. if (diContext) { - std::optional dli = - diContext->getLineInfoForAddress({PC, SectIdx}); + DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx}) + .value_or(DILineInfo()); // Print valid line info if it changed. - if (dli && dli != lastLine && dli->Line != 0) - outs() << "\t## " << dli->FileName << ':' << dli->Line << ':' - << dli->Column; + if (dli != lastLine && dli.Line != 0) + outs() << "\t## " << dli.FileName << ':' << dli.Line << ':' + << dli.Column; lastLine = dli; } outs() << "\n";