diff options
author | alx32 <103613512+alx32@users.noreply.github.com> | 2025-02-06 15:41:29 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-06 15:41:29 -0800 |
commit | 464f65adacd812aead775308ab7f1952fee774db (patch) | |
tree | f4fefa4854e8f3cedb1e28a2cafe5a45f89d0eb4 /llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp | |
parent | a15618f18cb000cce59df810c9e75153aa5e0aca (diff) | |
download | llvm-464f65adacd812aead775308ab7f1952fee774db.zip llvm-464f65adacd812aead775308ab7f1952fee774db.tar.gz llvm-464f65adacd812aead775308ab7f1952fee774db.tar.bz2 |
[DebugInfo][DWARF] Utilize DW_AT_LLVM_stmt_sequence attr in line table lookups (#123391)
**Summary**
Add support for filtering line table entries based on
`DW_AT_LLVM_stmt_sequence` attribute when looking up address ranges.
This ensures that line entries are correctly attributed to their
corresponding functions, even when multiple functions share the same
address range due to optimizations.
**Background**
In https://github.com/llvm/llvm-project/pull/110192 we added support to
clang to generate the `DW_AT_LLVM_stmt_sequence` attribute for
`DW_TAG_subprogram`'s. Corresponding RFC: [New DWARF Attribute for
Symbolication of Merged
Functions](https://discourse.llvm.org/t/rfc-new-dwarf-attribute-for-symbolication-of-merged-functions/79434)
The `DW_AT_LLVM_stmt_sequence` attribute allows accurate attribution of
line number information to their corresponding functions, even in
scenarios where functions are merged or share the same address space due
to optimizations like Identical Code Folding (ICF) in the linker.
**Implementation Details**
The patch modifies `DWARFDebugLine::lookupAddressRange` to accept an
optional DWARFDie parameter. When provided, the function checks if the
`DIE` has a `DW_AT_LLVM_stmt_sequence` attribute. This attribute
contains an offset into the line table that marks where the line entries
for this DIE's function begin.
If the attribute is present, the function filters the results to only
include line entries from the sequence that starts at the specified
offset. This ensures that even when multiple functions share the same
address range, we return only the line entries that actually belong to
the function represented by the DIE.
The implementation:
- Adds an optional DWARFDie parameter to lookupAddressRange
- Extracts the `DW_AT_LLVM_stmt_sequence` offset if present
- Modifies the address range lookup logic to filter sequences based on
their offset
- Returns only line entries from the matching sequence
Diffstat (limited to 'llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp')
-rw-r--r-- | llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp | 115 |
1 files changed, 114 insertions, 1 deletions
diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp index e549128..2fe5260 100644 --- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp +++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp @@ -6,10 +6,10 @@ // //===----------------------------------------------------------------------===// +#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" #include "DwarfGenerator.h" #include "DwarfUtils.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" -#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" @@ -2035,4 +2035,117 @@ TEST_F(DebugLineBasicFixture, PrintPathsProperly) { EXPECT_THAT(Result.c_str(), MatchesRegex("a dir.b dir.b file")); } +/// Test that lookupAddressRange correctly filters rows based on +/// a statement-sequence offset (simulating DW_AT_LLVM_stmt_sequence). +/// +/// This test verifies that: +/// 1. When a statement-sequence offset is provided, lookupAddressRange +/// only returns rows from the sequence starting at that offset. +/// 2. When an invalid statement-sequence offset is provided, no rows +/// are returned. +/// 3. When no statement-sequence offset is provided, all matching rows +/// in the table are returned. +/// +/// We build a line table with two sequences at the same address range +/// but different line numbers. Then we try lookups with various statement- +/// sequence offsets to check the filtering logic. +TEST_F(DebugLineBasicFixture, LookupAddressRangeWithStmtSequenceOffset) { + if (!setupGenerator()) + GTEST_SKIP(); + + // Create a line table that has two sequences covering [0x1000, 0x1004). + // Each sequence has two rows: addresses at 0x1000 and 0x1004, but + // they differ by line numbers (100 vs. 200, etc.). + // + // We'll pretend the first sequence starts at offset 0x2e in the line table, + // the second at 0x42, and we'll also test an invalid offset 0x66. + + LineTable < = Gen->addLineTable(); + + // First sequence at offset 0x2e: addresses 0x1000(Ln=100), 0x1004(Ln=101) + LT.addExtendedOpcode(9, DW_LNE_set_address, {{0x1000U, LineTable::Quad}}); + LT.addStandardOpcode(DW_LNS_set_prologue_end, {}); + // Advance the line register by 99 (so line=100) and copy. + LT.addStandardOpcode(DW_LNS_advance_line, {{99, LineTable::SLEB}}); + LT.addStandardOpcode(DW_LNS_copy, {}); + // 0x4b is a special opcode: address += 4, line += 1 (so line=101). + LT.addByte(0x4b); + // End this sequence. + LT.addExtendedOpcode(1, DW_LNE_end_sequence, {}); + + // Second sequence at offset 0x42: addresses 0x1000(Ln=200), 0x1004(Ln=201) + LT.addExtendedOpcode(9, DW_LNE_set_address, {{0x1000U, LineTable::Quad}}); + LT.addStandardOpcode(DW_LNS_set_prologue_end, {}); + LT.addStandardOpcode(DW_LNS_advance_line, {{199, LineTable::SLEB}}); + LT.addStandardOpcode(DW_LNS_copy, {}); + // 0x4b again: address += 4, line += 1 (so line=201). + LT.addByte(0x4b); + // End this second sequence. + LT.addExtendedOpcode(1, DW_LNE_end_sequence, {}); + + // Generate the DWARF data. + generate(); + + // Parse the line table. + auto ExpectedLineTable = + Line.getOrParseLineTable(LineData, /*Offset=*/0, *Context, + /*DwarfUnit=*/nullptr, RecordRecoverable); + ASSERT_THAT_EXPECTED(ExpectedLineTable, Succeeded()); + const auto *Table = *ExpectedLineTable; + + // The table should have two sequences, each starting at our chosen offsets. + ASSERT_EQ(Table->Sequences.size(), 2u); + + // 1) Try looking up with an invalid offset (simulating an invalid + // DW_AT_LLVM_stmt_sequence). We expect no rows. + { + std::vector<uint32_t> Rows; + bool Found = Table->lookupAddressRange( + {0x1000, object::SectionedAddress::UndefSection}, /*Size=*/1, Rows, + /*StmtSequenceOffset=*/0x66); // invalid offset + EXPECT_FALSE(Found); + EXPECT_TRUE(Rows.empty()); + } + + // 2) Look up using the offset 0x2e (our first sequence). We expect + // to get the rows from that sequence only (which for 0x1000 is row #0). + { + std::vector<uint32_t> Rows; + bool Found = Table->lookupAddressRange( + {0x1000, object::SectionedAddress::UndefSection}, /*Size=*/1, Rows, + /*StmtSequenceOffset=*/0x2e); + EXPECT_TRUE(Found); + ASSERT_EQ(Rows.size(), 1u); + // The first sequence's first row is index 0. + EXPECT_EQ(Rows[0], 0u); + } + + // 3) Look up using the offset 0x42 (second sequence). For address 0x1000 + // in that second sequence, we should see row #2. + { + std::vector<uint32_t> Rows; + bool Found = Table->lookupAddressRange( + {0x1000, object::SectionedAddress::UndefSection}, /*Size=*/1, Rows, + /*StmtSequenceOffset=*/0x42); + EXPECT_TRUE(Found); + ASSERT_EQ(Rows.size(), 1u); + // The second sequence's first row is index 2 in the table. + EXPECT_EQ(Rows[0], 3u); + } + + // 4) Look up with no statement-sequence offset specified. + // We should get rows from both sequences for address 0x1000. + { + std::vector<uint32_t> Rows; + bool Found = Table->lookupAddressRange( + {0x1000, object::SectionedAddress::UndefSection}, /*Size=*/1, Rows, + std::nullopt /* no filter */); + EXPECT_TRUE(Found); + // The first sequence's row is #0, second's row is #2, so both should + // appear. + ASSERT_EQ(Rows.size(), 2u); + EXPECT_EQ(Rows[0], 0u); + EXPECT_EQ(Rows[1], 3u); + } +} } // end anonymous namespace |