Skip to content

[NFC][DebugInfo] Wrap DILineInfo return type with std::optional to handle missing debug info. #129792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/include/llvm/DebugInfo/BTF/BTFContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class BTFContext final : public DIContext {
// BTF is no DWARF, so ignore this operation for now.
}

DILineInfo getLineInfoForAddress(
std::optional<DILineInfo> getLineInfoForAddress(
object::SectionedAddress Address,
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;

DILineInfo
std::optional<DILineInfo>
getLineInfoForDataAddress(object::SectionedAddress Address) override;

DILineInfoTable getLineInfoForAddressRange(
Expand Down
6 changes: 4 additions & 2 deletions llvm/include/llvm/DebugInfo/DIContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<DILineInfo> getLineInfoForAddress(
object::SectionedAddress Address,
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
virtual DILineInfo
virtual std::optional<DILineInfo>
getLineInfoForDataAddress(object::SectionedAddress Address) = 0;
virtual DILineInfoTable getLineInfoForAddressRange(
object::SectionedAddress Address, uint64_t Size,
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ class DWARFContext : public DIContext {
/// executable's debug info.
DIEsForAddress getDIEsForAddress(uint64_t Address, bool CheckDWO = false);

DILineInfo getLineInfoForAddress(
std::optional<DILineInfo> getLineInfoForAddress(
object::SectionedAddress Address,
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
DILineInfo
std::optional<DILineInfo>
getLineInfoForDataAddress(object::SectionedAddress Address) override;
DILineInfoTable getLineInfoForAddressRange(
object::SectionedAddress Address, uint64_t Size,
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/DebugInfo/PDB/PDBContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ namespace pdb {

void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override;

DILineInfo getLineInfoForAddress(
std::optional<DILineInfo> getLineInfoForAddress(
object::SectionedAddress Address,
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
DILineInfo
std::optional<DILineInfo>
getLineInfoForDataAddress(object::SectionedAddress Address) override;
DILineInfoTable getLineInfoForAddressRange(
object::SectionedAddress Address, uint64_t Size,
Expand Down
12 changes: 7 additions & 5 deletions llvm/lib/DebugInfo/BTF/BTFContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ using namespace llvm;
using object::ObjectFile;
using object::SectionedAddress;

DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address,
DILineInfoSpecifier Specifier) {
std::optional<DILineInfo>
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);
Expand All @@ -34,9 +35,10 @@ DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address,
return Result;
}

DILineInfo BTFContext::getLineInfoForDataAddress(SectionedAddress Address) {
std::optional<DILineInfo>
BTFContext::getLineInfoForDataAddress(SectionedAddress Address) {
// BTF does not convey such information.
return {};
return std::nullopt;
}

DILineInfoTable
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,8 +1730,9 @@ DWARFContext::getLocalsForAddress(object::SectionedAddress Address) {
return Result;
}

DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
DILineInfoSpecifier Spec) {
std::optional<DILineInfo>
DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
DILineInfoSpecifier Spec) {
DILineInfo Result;
DWARFCompileUnit *CU = getCompileUnitForCodeAddress(Address.Address);
if (!CU)
Expand All @@ -1751,7 +1752,7 @@ DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
return Result;
}

DILineInfo
std::optional<DILineInfo>
DWARFContext::getLineInfoForDataAddress(object::SectionedAddress Address) {
DILineInfo Result;
DWARFCompileUnit *CU = getCompileUnitForDataAddress(Address.Address);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions llvm/lib/DebugInfo/PDB/PDBContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DILineInfo>
PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
DILineInfoSpecifier Specifier) {
DILineInfo Result;
Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind);

Expand Down Expand Up @@ -64,7 +65,7 @@ DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
return Result;
}

DILineInfo
std::optional<DILineInfo>
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.
Expand All @@ -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<DILineInfo> LineEntry = getLineInfoForAddress(
{LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier))
Table.push_back(
std::make_pair(LineInfo->getVirtualAddress(), *LineEntry));
}
return Table;
}
Expand All @@ -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<PDBSymbol> ParentFunc =
Expand Down
16 changes: 10 additions & 6 deletions llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DILineInfo> DBGLineInfo =
DebugInfoContext->getLineInfoForAddress(ModuleOffset,
LineInfoSpecifier))
LineInfo = *DBGLineInfo;

// Override function name from symbol table if necessary.
if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
Expand Down Expand Up @@ -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<DILineInfo> DL =
DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
if (DL && DL->Line != 0) {
Res.DeclFile = DL->FileName;
Res.DeclLine = DL->Line;
}
return Res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, unsigned>{/*unsigned*/ Sym->getOffset(),
Expand Down
8 changes: 6 additions & 2 deletions llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/tools/llvm-objdump/MachODump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << ':'
Expand Down
20 changes: 9 additions & 11 deletions llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,15 @@ TEST(BTFParserTest, btfContext) {
BTFParser BTF;
std::unique_ptr<BTFContext> 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<DILineInfo> 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<DILineInfo> I2 = Ctx->getLineInfoForAddress({24, 1});
EXPECT_FALSE(I2.has_value());
}

static uint32_t mkInfo(uint32_t Kind) { return Kind << 24; }
Expand Down
Loading