Skip to content

DWARF support for multiple line tables #2557

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 22 commits into from
Jan 9, 2020
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
66 changes: 43 additions & 23 deletions src/wasm/wasm-debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ struct BinaryenDWARFInfo {
}
// Parse debug sections.
uint8_t addrSize = 4;
context = llvm::DWARFContext::create(sections, addrSize);
bool isLittleEndian = true;
context = llvm::DWARFContext::create(sections, addrSize, isLittleEndian);
}
};

Expand All @@ -76,6 +77,8 @@ void dumpDWARF(const Module& wasm) {
}
}
llvm::DIDumpOptions options;
options.DumpType = llvm::DIDT_All;
options.ShowChildren = true;
options.Verbose = true;
info.context->dump(llvm::outs(), options);
}
Expand Down Expand Up @@ -111,8 +114,8 @@ struct LineState {
uint32_t line = 1;
uint32_t col = 0;
uint32_t file = 1;
// TODO uint32_t isa = 0;
// TODO Discriminator = 0;
uint32_t isa = 0;
uint32_t discriminator = 0;
bool isStmt;
bool basicBlock = false;
// XXX these two should be just prologue, epilogue?
Expand All @@ -139,9 +142,17 @@ struct LineState {
case llvm::dwarf::DW_LNE_end_sequence: {
return true;
}
case llvm::dwarf::DW_LNE_set_discriminator: {
discriminator = opcode.Data;
break;
}
case llvm::dwarf::DW_LNE_define_file: {
Fatal() << "TODO: DW_LNE_define_file";
}
default: {
Fatal() << "unknown debug line sub-opcode: " << std::hex
<< opcode.SubOpcode;
// An unknown opcode, ignore.
std::cerr << "warning: unknown subopcopde " << opcode.SubOpcode
<< '\n';
}
}
break;
Expand Down Expand Up @@ -174,13 +185,25 @@ struct LineState {
isStmt = !isStmt;
break;
}
case llvm::dwarf::DW_LNS_set_basic_block: {
basicBlock = true;
break;
}
case llvm::dwarf::DW_LNS_const_add_pc: {
uint8_t AdjustOpcode = 255 - table.OpcodeBase;
uint64_t AddrOffset =
(AdjustOpcode / table.LineRange) * table.MinInstLength;
addr += AddrOffset;
break;
}
case llvm::dwarf::DW_LNS_fixed_advance_pc: {
addr += opcode.Data;
break;
}
case llvm::dwarf::DW_LNS_set_isa: {
isa = opcode.Data;
break;
}
default: {
if (opcode.Opcode >= table.OpcodeBase) {
// Special opcode: adjust line and addr, using some math.
Expand Down Expand Up @@ -239,11 +262,23 @@ struct LineState {
item.Data = file;
newOpcodes.push_back(item);
}
if (isa != old.isa) {
auto item = makeItem(llvm::dwarf::DW_LNS_set_isa);
item.Data = isa;
newOpcodes.push_back(item);
}
if (discriminator != old.discriminator) {
// len = 1 (subopcode) + 4 (wasm32 address)
auto item = makeItem(llvm::dwarf::DW_LNE_set_discriminator, 5);
item.Data = discriminator;
newOpcodes.push_back(item);
}
if (isStmt != old.isStmt) {
newOpcodes.push_back(makeItem(llvm::dwarf::DW_LNS_negate_stmt));
}
if (basicBlock != old.basicBlock) {
Fatal() << "bb";
assert(basicBlock);
newOpcodes.push_back(makeItem(llvm::dwarf::DW_LNS_set_basic_block));
}
if (prologueEnd != old.prologueEnd) {
assert(prologueEnd);
Expand Down Expand Up @@ -382,21 +417,6 @@ static void updateDebugLines(const Module& wasm,
}
}

static void fixEmittedSection(const std::string& name,
std::vector<char>& data) {
if (name == ".debug_line") {
// The YAML code does not update the line section size. However, it is
// trivial to do so after the fact, as the wasm section's additional size is
// easy to compute: it is the emitted size - the 4 bytes of the size itself.
uint32_t size = data.size() - 4;
BufferWithRandomAccess buf;
buf << size;
for (int i = 0; i < 4; i++) {
data[i] = buf[i];
}
}
}

void writeDWARFSections(Module& wasm, const BinaryLocationsMap& newLocations) {
BinaryenDWARFInfo info(wasm);

Expand All @@ -411,7 +431,8 @@ void writeDWARFSections(Module& wasm, const BinaryLocationsMap& newLocations) {
// TODO: Actually update, and remove sections we don't know how to update yet?

// Convert to binary sections.
auto newSections = EmitDebugSections(data, true);
auto newSections =
EmitDebugSections(data, false /* EmitFixups for debug_info */);

// Update the custom sections in the wasm.
// TODO: efficiency
Expand All @@ -422,7 +443,6 @@ void writeDWARFSections(Module& wasm, const BinaryLocationsMap& newLocations) {
auto llvmData = newSections[llvmName]->getBuffer();
section.data.resize(llvmData.size());
std::copy(llvmData.begin(), llvmData.end(), section.data.data());
fixEmittedSection(section.name, section.data);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/passes/dwarfdump.bin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ Abbrev table for offset: 0x00000000
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
DW_AT_high_pc [DW_FORM_data4] (0x00000002)

0x00000026: DW_TAG_subprogram [2]
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
DW_AT_high_pc [DW_FORM_data4] (0x00000002)
DW_AT_linkage_name [DW_FORM_strp] ( .debug_str[0x000000c8] = "_Z3foov")
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000d0] = "foo")
DW_AT_decl_file [DW_FORM_data1] ("/usr/local/google/home/azakai/Dev/emscripten/a.cpp")
DW_AT_decl_line [DW_FORM_data1] (3)
DW_AT_external [DW_FORM_flag_present] (true)

0x00000039: NULL

.debug_line contents:
debug_line[0x00000000]
Line table prologue:
Expand Down
22 changes: 22 additions & 0 deletions test/passes/dwarfdump_roundtrip_dwarfdump.bin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ Abbrev table for offset: 0x00000000
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
DW_AT_high_pc [DW_FORM_data4] (0x00000002)

0x00000026: DW_TAG_subprogram [2]
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
DW_AT_high_pc [DW_FORM_data4] (0x00000002)
DW_AT_linkage_name [DW_FORM_strp] ( .debug_str[0x000000c8] = "_Z3foov")
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000d0] = "foo")
DW_AT_decl_file [DW_FORM_data1] ("/usr/local/google/home/azakai/Dev/emscripten/a.cpp")
DW_AT_decl_line [DW_FORM_data1] (3)
DW_AT_external [DW_FORM_flag_present] (true)

0x00000039: NULL

.debug_line contents:
debug_line[0x00000000]
Line table prologue:
Expand Down Expand Up @@ -129,6 +140,17 @@ Abbrev table for offset: 0x00000000
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
DW_AT_high_pc [DW_FORM_data4] (0x00000002)

0x00000026: DW_TAG_subprogram [2]
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
DW_AT_high_pc [DW_FORM_data4] (0x00000002)
DW_AT_linkage_name [DW_FORM_strp] ( .debug_str[0x000000c8] = "_Z3foov")
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000d0] = "foo")
DW_AT_decl_file [DW_FORM_data1] ("/usr/local/google/home/azakai/Dev/emscripten/a.cpp")
DW_AT_decl_line [DW_FORM_data1] (3)
DW_AT_external [DW_FORM_flag_present] (true)

0x00000039: NULL

.debug_line contents:
debug_line[0x00000000]
Line table prologue:
Expand Down
Loading