[BOLT][NFC] Fix debug info printouts for inlined functions

Summary:
While printing debug info for instructions, we should use line tables
from the corresponding DWARF CU which could be different from the
containing function CU in case of inlined instructions.

(cherry picked from FBD28908324)
This commit is contained in:
Maksim Panchenko 2021-06-04 12:31:31 -07:00
parent 65d227c035
commit 7bccf8d25d
5 changed files with 45 additions and 9 deletions

View file

@ -1689,25 +1689,31 @@ void BinaryContext::printInstruction(raw_ostream &OS,
MIB->printAnnotations(Instruction, OS);
const DWARFDebugLine::LineTable *LineTable =
Function && opts::PrintDebugInfo ? Function->getDWARFLineTable()
: nullptr;
if (LineTable) {
if (opts::PrintDebugInfo) {
DebugLineTableRowRef RowRef =
DebugLineTableRowRef::fromSMLoc(Instruction.getLoc());
if (RowRef != DebugLineTableRowRef::NULL_ROW) {
const DWARFDebugLine::LineTable *LineTable;
if (Function && Function->getDWARFUnit() &&
Function->getDWARFUnit()->getOffset() == RowRef.DwCompileUnitIndex) {
LineTable = Function->getDWARFLineTable();
} else {
LineTable = DwCtx->getLineTableForUnit(
DwCtx->getCompileUnitForOffset(RowRef.DwCompileUnitIndex));
}
assert(LineTable &&
"line table expected for instruction with debug info");
const DWARFDebugLine::Row &Row = LineTable->Rows[RowRef.RowIndex - 1];
StringRef FileName = "";
if (Optional<const char *> FName =
dwarf::toString(LineTable->Prologue.FileNames[Row.File - 1].Name))
FileName = *FName;
OS << " # debug line " << FileName << ":" << Row.Line;
if (Row.Column) {
if (Row.Column)
OS << ":" << Row.Column;
}
if (Row.Discriminator)
OS << " discriminator:" << Row.Discriminator;
}
}

View file

@ -0,0 +1,5 @@
#include <stdio.h>
void foo() {
puts("Hello world!\n");
}

View file

@ -0,0 +1,5 @@
extern void foo();
int main() {
foo();
return 0;
}

View file

@ -0,0 +1,19 @@
## Check that BOLT correctly prints and updates debug info for inlined
## functions.
# REQUIRES: system-linux
# RUN: %host_cc %cflags -O1 -g %p/Inputs/inline-main.c %p/Inputs/inline-foo.c \
# RUN: -o %t.exe -Wl,-q
# RUN: llvm-bolt %t.exe -update-debug-sections -print-debug-info \
# RUN: -print-only=main -print-after-lowering -force-inline=foo -o %t.bolt \
# RUN: | FileCheck %s
## The call to puts() should come from inline-foo.c:
# CHECK: callq puts@PLT # debug line {{.*}}inline-foo.c:4:3
# RUN: llvm-objdump --disassemble-symbols=main -d --line-numbers %t.bolt \
# RUN: | FileCheck %s -check-prefix=CHECK-OBJDUMP
## Dump of main() should include debug info from inline-foo.c after inlining:
# CHECK-OBJDUMP: inline-foo.c:4

View file

@ -57,6 +57,7 @@ tools = [
ToolSubst('yaml2obj', unresolved='fatal'),
ToolSubst('llvm-mc', unresolved='fatal'),
ToolSubst('llvm-nm', unresolved='fatal'),
ToolSubst('llvm-objdump', unresolved='fatal'),
ToolSubst('llvm-strip', unresolved='fatal'),
ToolSubst('link_fdata', command=FindTool('link_fdata.sh'), unresolved='fatal'),
]