Skip to content

DWARF debug line updating #2545

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 21 commits into from
Dec 21, 2019
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
8 changes: 0 additions & 8 deletions src/passes/DWARF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ struct DWARFDump : public Pass {
}
};

struct DWARFUpdate : public Pass {
void run(PassRunner* runner, Module* module) override {
Debug::writeDWARFSections(*module);
}
};

Pass* createDWARFDumpPass() { return new DWARFDump(); }

Pass* createDWARFUpdatePass() { return new DWARFUpdate(); }

} // namespace wasm
3 changes: 2 additions & 1 deletion src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,8 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
auto iter = currFunction->binaryLocations.find(curr);
if (iter != currFunction->binaryLocations.end()) {
Colors::grey(o);
o << ";; code offset: 0x" << iter->second << '\n';
o << ";; code offset: 0x" << std::hex << iter->second << std::dec
<< '\n';
restoreNormalColor(o);
doIndent(o, indent);
}
Expand Down
2 changes: 1 addition & 1 deletion src/passes/RoundTrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct RoundTrip : public Pass {
// Read
ModuleUtils::clearModule(*module);
ModuleReader reader;
// TODO: enable debug info when relevant
reader.setDWARF(runner->options.debugInfo);
reader.read(tempName, *module);
// Clean up
std::remove(tempName.c_str());
Expand Down
2 changes: 0 additions & 2 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ void PassRegistry::registerPasses() {
registerPass("dwarfdump",
"dump DWARF debug info sections from the read binary",
createDWARFDumpPass);
registerPass(
"dwarfupdate", "update DWARF debug info sections", createDWARFUpdatePass);
registerPass("duplicate-import-elimination",
"removes duplicate imports",
createDuplicateImportEliminationPass);
Expand Down
1 change: 0 additions & 1 deletion src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Pass* createDataFlowOptsPass();
Pass* createDeadCodeEliminationPass();
Pass* createDirectizePass();
Pass* createDWARFDumpPass();
Pass* createDWARFUpdatePass();
Pass* createDuplicateImportEliminationPass();
Pass* createDuplicateFunctionEliminationPass();
Pass* createEmitTargetFeaturesPass();
Expand Down
13 changes: 13 additions & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,19 @@ class WasmBinaryWriter {

std::unique_ptr<ImportInfo> importInfo;

// General debugging info: map every instruction to its original position in
// the binary, relative to the beginning of the code section. This is similar
// to binaryLocations on Function objects, which are filled as we load the
// functions from the binary. Here we track them as we write, and then
// the combination of the two can be used to update DWARF info for the new
// locations of things.
BinaryLocationsMap binaryLocations;
size_t binaryLocationsSizeAtSectionStart;
// Track the expressions that we added for the current function being
// written, so that we can update those specific binary locations when
// the function is written out.
std::vector<Expression*> binaryLocationTrackedExpressionsForFunc;

void prepare();
};

Expand Down
2 changes: 1 addition & 1 deletion src/wasm-debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool hasDWARFSections(const Module& wasm);
void dumpDWARF(const Module& wasm);

// Update the DWARF sections.
void writeDWARFSections(Module& wasm);
void writeDWARFSections(Module& wasm, const BinaryLocationsMap& newLocations);

} // namespace Debug

Expand Down
18 changes: 14 additions & 4 deletions src/wasm-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ class BinaryInstWriter : public OverriddenVisitor<BinaryInstWriter> {
public:
BinaryInstWriter(WasmBinaryWriter& parent,
BufferWithRandomAccess& o,
Function* func)
: parent(parent), o(o), func(func) {}
Function* func,
bool sourceMap)
: parent(parent), o(o), func(func), sourceMap(sourceMap) {}

void visit(Expression* curr) {
if (func && !sourceMap) {
parent.writeDebugLocation(curr, func);
}
OverriddenVisitor<BinaryInstWriter>::visit(curr);
}

void visitBlock(Block* curr);
void visitIf(If* curr);
Expand Down Expand Up @@ -144,6 +152,8 @@ class BinaryInstWriter : public OverriddenVisitor<BinaryInstWriter> {
WasmBinaryWriter& parent;
BufferWithRandomAccess& o;
Function* func = nullptr;
bool sourceMap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just use for source maps for for dwarf debug_line too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for source maps - when source maps are enabled, we don't update debug info in the way that DWARF needs, and vice versa. So we need to know if source maps are used (previously there was just one way to do debug locations).


std::vector<Name> breakStack;

// type => number of locals of that type in the compact form
Expand Down Expand Up @@ -758,7 +768,7 @@ class BinaryenIRToBinaryWriter
Function* func = nullptr,
bool sourceMap = false)
: BinaryenIRWriter<BinaryenIRToBinaryWriter>(func), parent(parent),
writer(parent, o, func), sourceMap(sourceMap) {}
writer(parent, o, func, sourceMap), sourceMap(sourceMap) {}

void visit(Expression* curr) {
BinaryenIRWriter<BinaryenIRToBinaryWriter>::visit(curr);
Expand Down Expand Up @@ -833,7 +843,7 @@ class StackIRToBinaryWriter {
StackIRToBinaryWriter(WasmBinaryWriter& parent,
BufferWithRandomAccess& o,
Function* func)
: writer(parent, o, func), func(func) {}
: writer(parent, o, func, false /* sourceMap */), func(func) {}

void write();

Expand Down
7 changes: 5 additions & 2 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,10 @@ struct Importable {
// Stack IR is a secondary IR to the main IR defined in this file (Binaryen
// IR). See wasm-stack.h.
class StackInst;
typedef std::vector<StackInst*> StackIR;

using StackIR = std::vector<StackInst*>;

using BinaryLocationsMap = std::unordered_map<Expression*, uint32_t>;

class Function : public Importable {
public:
Expand Down Expand Up @@ -1178,7 +1181,7 @@ class Function : public Importable {

// General debugging info: map every instruction to its original position in
// the binary, relative to the beginning of the code section.
std::unordered_map<Expression*, uint32_t> binaryLocations;
BinaryLocationsMap binaryLocations;

size_t getNumParams();
size_t getNumVars();
Expand Down
80 changes: 65 additions & 15 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ void WasmBinaryWriter::write() {
writeSourceMapEpilog();
}

#ifdef BUILD_LLVM_DWARF
// Update DWARF user sections after writing the data referred to by them
// (function bodies), and before writing the user sections themselves.
if (Debug::hasDWARFSections(*wasm)) {
Debug::writeDWARFSections(*wasm, binaryLocations);
}
#endif

writeLateUserSections();
writeFeaturesSection();

Expand Down Expand Up @@ -109,29 +117,52 @@ template<typename T> int32_t WasmBinaryWriter::startSection(T code) {
if (sourceMap) {
sourceMapLocationsSizeAtSectionStart = sourceMapLocations.size();
}
binaryLocationsSizeAtSectionStart = binaryLocations.size();
return writeU32LEBPlaceholder(); // section size to be filled in later
}

void WasmBinaryWriter::finishSection(int32_t start) {
// section size does not include the reserved bytes of the size field itself
int32_t size = o.size() - start - MaxLEB32Bytes;
auto sizeFieldSize = o.writeAt(start, U32LEB(size));
if (sizeFieldSize != MaxLEB32Bytes) {
// We can move things back if the actual LEB for the size doesn't use the
// maximum 5 bytes. In that case we need to adjust offsets after we move
// things backwards.
auto adjustmentForLEBShrinking = MaxLEB32Bytes - sizeFieldSize;
if (adjustmentForLEBShrinking) {
// we can save some room, nice
assert(sizeFieldSize < MaxLEB32Bytes);
std::move(&o[start] + MaxLEB32Bytes,
&o[start] + MaxLEB32Bytes + size,
&o[start] + sizeFieldSize);
auto adjustment = MaxLEB32Bytes - sizeFieldSize;
o.resize(o.size() - adjustment);
o.resize(o.size() - adjustmentForLEBShrinking);
if (sourceMap) {
for (auto i = sourceMapLocationsSizeAtSectionStart;
i < sourceMapLocations.size();
++i) {
sourceMapLocations[i].first -= adjustment;
sourceMapLocations[i].first -= adjustmentForLEBShrinking;
}
}
}

if (binaryLocationsSizeAtSectionStart != binaryLocations.size()) {
// We added the binary locations, adjust them: they must be relative
// to the code section.
assert(binaryLocationsSizeAtSectionStart == 0);
// The section type byte is right before the LEB for the size; we want
// offsets that are relative to the body, which is after that section type
// byte and the the size LEB.
auto body = start + sizeFieldSize;
for (auto& pair : binaryLocations) {
// Offsets are relative to the body of the code section: after the
// section type byte and the size.
// Everything was moved by the adjustment, track that. After this,
// we are at the right absolute address.
pair.second -= adjustmentForLEBShrinking;
// We are relative to the section start.
pair.second -= body;
}
}
}

int32_t
Expand Down Expand Up @@ -266,6 +297,7 @@ void WasmBinaryWriter::writeFunctions() {
auto start = startSection(BinaryConsts::Section::Code);
o << U32LEB(importInfo->getNumDefinedFunctions());
ModuleUtils::iterDefinedFunctions(*wasm, [&](Function* func) {
assert(binaryLocationTrackedExpressionsForFunc.empty());
size_t sourceMapLocationsSizeAtFunctionStart = sourceMapLocations.size();
BYN_TRACE("write one at" << o.size() << std::endl);
size_t sizePos = writeU32LEBPlaceholder();
Expand All @@ -284,22 +316,31 @@ void WasmBinaryWriter::writeFunctions() {
BYN_TRACE("body size: " << size << ", writing at " << sizePos
<< ", next starts at " << o.size() << "\n");
auto sizeFieldSize = o.writeAt(sizePos, U32LEB(size));
if (sizeFieldSize != MaxLEB32Bytes) {
// We can move things back if the actual LEB for the size doesn't use the
// maximum 5 bytes. In that case we need to adjust offsets after we move
// things backwards.
auto adjustmentForLEBShrinking = MaxLEB32Bytes - sizeFieldSize;
if (adjustmentForLEBShrinking) {
// we can save some room, nice
assert(sizeFieldSize < MaxLEB32Bytes);
std::move(&o[start], &o[start] + size, &o[sizePos] + sizeFieldSize);
auto adjustment = MaxLEB32Bytes - sizeFieldSize;
o.resize(o.size() - adjustment);
o.resize(o.size() - adjustmentForLEBShrinking);
if (sourceMap) {
for (auto i = sourceMapLocationsSizeAtFunctionStart;
i < sourceMapLocations.size();
++i) {
sourceMapLocations[i].first -= adjustment;
sourceMapLocations[i].first -= adjustmentForLEBShrinking;
}
}
for (auto* curr : binaryLocationTrackedExpressionsForFunc) {
// We added the binary locations, adjust them: they must be relative
// to the code section.
binaryLocations[curr] -= adjustmentForLEBShrinking;
}
}
tableOfContents.functionBodies.emplace_back(
func->name, sizePos + sizeFieldSize, size);
binaryLocationTrackedExpressionsForFunc.clear();
});
finishSection(start);
}
Expand Down Expand Up @@ -649,10 +690,19 @@ void WasmBinaryWriter::writeDebugLocation(const Function::DebugLocation& loc) {
}

void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) {
auto& debugLocations = func->debugLocations;
auto iter = debugLocations.find(curr);
if (iter != debugLocations.end()) {
writeDebugLocation(iter->second);
if (sourceMap) {
auto& debugLocations = func->debugLocations;
auto iter = debugLocations.find(curr);
if (iter != debugLocations.end()) {
writeDebugLocation(iter->second);
}
}
// TODO: remove source map debugging support and refactor this method
// to something that directly thinks about DWARF, instead of indirectly
// looking at func->binaryLocations as a proxy for that etc.
if (func && !func->binaryLocations.empty()) {
binaryLocations[curr] = o.size();
binaryLocationTrackedExpressionsForFunc.push_back(curr);
}
}

Expand Down Expand Up @@ -809,6 +859,9 @@ void WasmBinaryBuilder::read() {
readFunctionSignatures();
break;
case BinaryConsts::Section::Code:
if (DWARF) {
codeSectionLocation = pos;
}
readFunctions();
break;
case BinaryConsts::Section::Export:
Expand Down Expand Up @@ -1288,9 +1341,6 @@ void WasmBinaryBuilder::readFunctionSignatures() {

void WasmBinaryBuilder::readFunctions() {
BYN_TRACE("== readFunctions\n");
if (DWARF) {
codeSectionLocation = pos;
}
size_t total = getU32LEB();
if (total != functionSignatures.size()) {
throwError("invalid function section size, must equal types");
Expand Down
Loading