| Issue |
244617
|
| Summary |
[BOLT] `--update-debug-sections` drops line-table rows that share an address, so breakpoints on lines and functions move or fail
|
| Labels |
BOLT
|
| Assignees |
|
| Reporter |
not4juu
|
## Description
When several rows of a line-table sequence share an address, BOLT reduces them to one in every function it rewrites: it keeps only the last row, and at the first instruction of a function fragment marks that row `is_stmt`. Effects of this include:
- `break <function>` stops at the wrong line, or at an address with no line (a line-0 or non-statement row that BOLT marks `is_stmt`);
- `break <file>:<line>` fails with `No compiled code for line …`, or lands on another line, when the line's only `is_stmt` rows were not last at their address.
The rows are missing from the table itself (`llvm-dwarfdump --debug-line`), so this does not depend on the debugger.
**Environment:**
- **LLVM:** `llvm-bolt`, `clang`, `lld`, `llvm-objdump` and `llvm-dwarfdump` built at `72f18f704c03c8bdac29ba54d387e7d08fd1bad4`
- **x86-64:** `g++ (GCC) 14.2.0`, `GNU gdb 16.3`
## Reproducer
The examples below are for x86-64. With the same reproducer built for AArch64, the `llvm-dwarfdump --debug-line` output confirms that rows are lost the same way with both Clang and GCC, and proposed fix restores them.
```cpp
// main.cpp
int G = 5;
__attribute__((noinline)) void a(int c, int v) { G += v + c; }
__attribute__((noinline)) void b(int c, int v) { G -= v + c; }
__attribute__((noinline)) void pick(int c)
{
if (c == 1)
a(c, G + 1);
else
b(c, G + 2);
}
__attribute__((noinline)) int sum(const int *p, int n) {
int s = 0;
for (int i = 0; i < n; ++i)
s += p[i];
return s;
}
int main(int argc, char **) {
int a[4] = {argc, 2, 3, 4};
pick(argc);
return sum(a, 4);
}
```
### Clang
```bash
clang++ -O2 -gz=none -g main.cpp -o main -fuse-ld=lld -Wl,-q
llvm-bolt main -o main.bolt --update-debug-sections
```
`pick` starts at `0x1750` in the input and at `0x400114` in the BOLT output:
```bash
llvm-objdump -d --no-show-raw-insn --disassemble-symbols=_Z4picki main main.bolt
0000000000001750 <_Z4picki>:
1750: movl 0x2352(%rip), %esi # 0x3aa8 <G>
0000000000400114 <_Z4picki>:
400114: movl -0x3fc672(%rip), %esi # 0x3aa8 <G>
```
```bash
llvm-dwarfdump --debug-line main
llvm-dwarfdump --debug-line main.bolt
```
This instruction loads `G`, hoisted out of both arms of the `if`, so it has line 0. The line table of `pick` (address, line:column, flags):
| input | BOLT | with the fix below |
| ------------------------- | ----------------------------- | ------------------------- |
| `0x1750` 7:0 `is_stmt` | | `0x400114` 7:0 `is_stmt` |
| `0x1750` 0:0 | `0x400114` 0:0 **`is_stmt`** | `0x400114` 0:0 |
| `0x1756` 8:9 `is_stmt` | `0x40011a` 8:9 `is_stmt` | `0x40011a` 8:9 `is_stmt` |
```bash
gdb -q -batch -ex 'break pick' -ex 'info line *pick' main
Breakpoint 1 at 0x1756: file main.cpp, line 8.
Line 7 of "main.cpp" starts at address 0x1750 <_Z4picki> and ends at 0x1756 <_Z4picki+6>.
gdb -q -batch -ex 'break pick' -ex 'info line *pick' main.bolt
Breakpoint 1 at 0x400114
No line number information available for address 0x400114 <_Z4picki>
```
| command | input | BOLT | with the fix below |
| ----------------- | ------------------------- | ------------------------------------------ | --------------------------- |
| `break pick` | line 8 | **`0x400114`, no file or line** | line 8 |
| `info line *pick` | line 7 starts at `0x1750` | **`No line number information available`** | line 7 starts at `0x400114` |
### GCC
```bash
g++ -O2 -gdwarf-4 -gz=none main.cpp -o main -fuse-ld=lld -Wl,-q
llvm-bolt main -o main.bolt --update-debug-sections
```
> [!NOTE]
> The GCC build uses DWARF 4 on purpose. With `-gdwarf-5` the same rows are lost, but #209184 (BOLT keeps only `file_names[0]` while the rows still name file 1) also breaks the lookup in the debugger and hides this effect.
GCC also stacks rows in the middle of functions. `llvm-objdump` shows the three instructions of `sum` where a line loses its only `is_stmt` row:
```bash
llvm-objdump -d --no-show-raw-insn --disassemble-symbols=_Z3sumPKii main main.bolt
00000000002016d0 <_Z3sumPKii>:
2016d0: testl %esi, %esi
...
2016e0: addl (%rdi), %eax
...
2016f2: retq
0000000000600116 <_Z3sumPKii>:
600116: testl %esi, %esi
...
600123: addl (%rdi), %eax
...
600131: retq
```
```bash
llvm-dwarfdump --debug-line main
llvm-dwarfdump --debug-line main.bolt
```
| input | BOLT | with the fix below |
| ----------------------------- | ----------------------------- | ----------------------------- |
| `0x2016d0` 14:56 `is_stmt` | | `0x600116` 14:56 `is_stmt` |
| `0x2016d0` 15:3 `is_stmt` | | `0x600116` 15:3 `is_stmt` |
| `0x2016d0` 16:3 `is_stmt` | | `0x600116` 16:3 `is_stmt` |
| `0x2016d0` 16:21 `is_stmt` | `0x600116` 16:21 `is_stmt` | `0x600116` 16:21 `is_stmt` |
| `0x2016e0` 17:5 `is_stmt` | | `0x600123` 17:5 `is_stmt` |
| `0x2016e0` 17:7 | `0x600123` 17:7 | `0x600123` 17:7 |
| `0x2016f2` 18:3 `is_stmt` | | `0x600131` 18:3 `is_stmt` |
| `0x2016f2` 19:1 | `0x600131` 19:1 | `0x600131` 19:1 |
`pick` and `main` lose rows the same way.
With gdb:
```bash
gdb -q -batch -ex 'break pick' -ex 'break main.cpp:14' -ex 'break main.cpp:15' \
-ex 'break main.cpp:17' -ex 'break main.cpp:18' main
gdb -q -batch -ex 'break pick' -ex 'break main.cpp:14' -ex 'break main.cpp:15' \
-ex 'break main.cpp:17' -ex 'break main.cpp:18' main.bolt
```
| command | input | BOLT | with the fix below |
| ------------------- | ------- | ----------------------------------- | ------------------ |
| `break pick` | line 8 | **line 9** | line 8 |
| `break main.cpp:14` | line 14 | **line 16** | line 14 |
| `break main.cpp:15` | line 15 | **line 16** | line 15 |
| `break main.cpp:17` | line 17 | **`No compiled code for line 17`** | line 17 |
| `break main.cpp:18` | line 18 | **`No compiled code for line 18`** | line 18 |
## DWARF conformance
- DWARF 5 section 6.2.5.2: `DW_LNS_copy` "appends a row to the matrix using the current values of the state machine registers" without advancing the address, and section 6.2.5.1: a special opcode appends a row after advancing the address by its operation advance, which may be 0. Both appear in the inputs above (`llvm-dwarfdump --debug-line --verbose`). Several rows at one address are a normal part of the line-number program.
- DWARF 5 section 6.2.2: `is_stmt` is "a boolean indicating that the current instruction is a recommended breakpoint location", and "a recommended breakpoint location is intended to 'represent' a line, a statement and/or a semantically distinct subpart of a statement". `is_stmt` is a column of the matrix, recorded in each row, so dropping a row also drops the breakpoint location it marks.
- DWARF 5 section 6.2.2: line 0 is for "cases where an instruction cannot be attributed to any source line". In the Clang case BOLT marks such a row `is_stmt`, so the output recommends a breakpoint location that represents no line.
## Cause
`bolt/lib/Core/BinaryFunction.cpp`, `findDebugLineInformationForInstructionAt()`, returns a single `DebugLineTableRowRef`: the index `lookupAddress()` finds, which is the last row whose address does not exceed the instruction's. `lookupAddress()` picks the last row on purpose (`findRowInSeq()` in `DWARFDebugLine.cpp`: "the compiler generates two entries, both with the same address. We want the last one."), which is right for symbolizing an address but drops the other rows when the table is rewritten. `disassemble()` attaches that one row to the instruction. `ClusteredRows` can already hold several rows per instruction, but it only does so for a function that spans several compile units.
`bolt/lib/Core/BinaryEmitter.cpp`, `emitLineInfo()`, sets `DWARF2_FLAG_IS_STMT` on every row of the first instruction of a fragment. Once several rows per instruction are kept, that would mark each of them `is_stmt`.
## Proposed fix
Attach every row the sequence starts at the instruction's address, in table order. Force `is_stmt` only on the first row of each compile unit at a fragment's start, and let the rest keep their flags. `printDebugInfo()` reads the last row, the one that describes the instruction; until now that was the only row.
```diff
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index d65fd4d72071..0e390b555661 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -2443,8 +2443,9 @@ static void printDebugInfo(raw_ostream &OS, const MCInst &Instruction,
return;
// File name and line number should be the same for all CUs.
- // So it is sufficient to check the first one.
- DebugLineTableRowRef RowRef = LineTableRows->getRows().front();
+ // So it is sufficient to check one. Of several rows at the instruction's
+ // address, the last one describes it.
+ DebugLineTableRowRef RowRef = LineTableRows->getRows().back();
const DWARFDebugLine::LineTable *LineTable = DwCtx->getLineTableForUnit(
DwCtx->getCompileUnitForOffset(RowRef.DwCompileUnitIndex));
diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp
index 72f1e378958b..a951c2a4e8de 100644
--- a/bolt/lib/Core/BinaryEmitter.cpp
+++ b/bolt/lib/Core/BinaryEmitter.cpp
@@ -732,6 +732,7 @@ SMLoc BinaryEmitter::emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc,
if (!InstrLabel)
InstrLabel = BC.Ctx->createTempSymbol();
+ std::optional<uint64_t> PrevUnitIndex;
for (DebugLineTableRowRef RowReference : Cluster->getRows()) {
const DWARFDebugLine::LineTable *CurrentLineTable =
BC.DwCtx->getLineTableForUnit(
@@ -743,9 +744,11 @@ SMLoc BinaryEmitter::emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc,
(DWARF2_FLAG_PROLOGUE_END * CurrentRow.PrologueEnd) |
(DWARF2_FLAG_EPILOGUE_BEGIN * CurrentRow.EpilogueBegin);
- // Always emit is_stmt at the beginning of function fragment.
- if (FirstInstr)
+ // Always emit is_stmt at the beginning of function fragment. Further rows
+ // the input started at the same instruction keep their own flags.
+ if (FirstInstr && PrevUnitIndex != RowReference.DwCompileUnitIndex)
Flags |= DWARF2_FLAG_IS_STMT;
+ PrevUnitIndex = RowReference.DwCompileUnitIndex;
const auto &FunctionDwarfUnits = BF.getDWARFUnits();
auto It = FunctionDwarfUnits.find(RowReference.DwCompileUnitIndex);
if (It != FunctionDwarfUnits.end()) {
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 2b314098c7eb..636b05ce7cc5 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -170,29 +170,38 @@ namespace llvm {
namespace bolt {
/// Gets debug line information for the instruction located at the given
-/// address in the original binary. Returns an optional DebugLineTableRowRef
-/// that references the corresponding row in the DWARF line table. Since binary
-/// functions can span multiple compilation units, this function helps
+/// address in the original binary. Appends to \p Rows the DebugLineTableRowRef
+/// of each row in the DWARF line table that describes the instruction. Since
+/// binary functions can span multiple compilation units, this function helps
/// associate instructions with their debug line information from the
-/// appropriate CU. Returns std::nullopt if no debug line information for
-/// this instruction was found.
-static std::optional<DebugLineTableRowRef>
-findDebugLineInformationForInstructionAt(
+/// appropriate CU. Appends nothing if no debug line information for this
+/// instruction was found. A sequence can start several rows at the address of
+/// an instruction (e.g. GCC location views, or a function's line followed by
+/// the line of its first statement); all of them are appended, in table order.
+static void findDebugLineInformationForInstructionAt(
uint64_t Address, DWARFUnit *Unit,
- const DWARFDebugLine::LineTable *LineTable) {
+ const DWARFDebugLine::LineTable *LineTable,
+ SmallVectorImpl<DebugLineTableRowRef> &Rows) {
uint32_t RowIndex = LineTable->lookupAddress(
{Address, object::SectionedAddress::UndefSection});
if (RowIndex == LineTable->UnknownRowIndex)
- return std::nullopt;
+ return;
assert(RowIndex < LineTable->Rows.size() &&
"Line Table lookup returned invalid index.");
- DebugLineTableRowRef InstructionLocation;
- InstructionLocation.DwCompileUnitIndex = Unit->getOffset();
- InstructionLocation.RowIndex = RowIndex + 1;
+ uint32_t FirstIndex = RowIndex;
+ if (LineTable->Rows[RowIndex].Address.Address == Address)
+ while (FirstIndex > 0 && !LineTable->Rows[FirstIndex - 1].EndSequence &&
+ LineTable->Rows[FirstIndex - 1].Address.Address == Address)
+ --FirstIndex;
- return InstructionLocation;
+ for (uint32_t Index = FirstIndex; Index <= RowIndex; ++Index) {
+ DebugLineTableRowRef InstructionLocation;
+ InstructionLocation.DwCompileUnitIndex = Unit->getOffset();
+ InstructionLocation.RowIndex = Index + 1;
+ Rows.emplace_back(InstructionLocation);
+ }
}
static std::string buildSectionName(StringRef Prefix, StringRef Name,
@@ -1505,10 +1514,8 @@ add_instruction:
getDWARFLineTableForUnit(Unit);
if (!LineTable)
continue;
- if (std::optional<DebugLineTableRowRef> RowRef =
- findDebugLineInformationForInstructionAt(AbsoluteInstrAddr,
- Unit, LineTable))
- Rows.emplace_back(*RowRef);
+ findDebugLineInformationForInstructionAt(AbsoluteInstrAddr, Unit,
+ LineTable, Rows);
}
if (!Rows.empty()) {
ClusteredRows *Cluster =
```
`dwarf5-split-dwarf4-monolithic.test` (D128232) tests a mix of DWARF 4 and DWARF 5 split units and, as a side check, the exact `DW_AT_stmt_list` of the `helper1` skeleton unit. The line table of `helper1` follows those of `main` and `helper0`, which have stacks at function entries (1:0 and 2:6 in `main`, 6:0 and 3:6 in `helper0`); without the fix BOLT keeps only 2:6 and 3:6. The fix keeps both rows of each stack, one byte more per table, so the expected offset changes from `0xfe` to `0x100`.
```diff
diff --git a/bolt/<truncated>Please see the issue for the entire body.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs