Index: src/wasm/wasm-debug.cc |
diff --git a/src/wasm/wasm-debug.cc b/src/wasm/wasm-debug.cc |
index aaca8622b88b4b962d9963cd4fe437c738ffeb51..4f62f9581e0ff4ec0797dec1e30bead7b644da3d 100644 |
--- a/src/wasm/wasm-debug.cc |
+++ b/src/wasm/wasm-debug.cc |
@@ -21,6 +21,7 @@ enum { |
kWasmDebugInfoWasmBytesHash, |
kWasmDebugInfoFunctionByteOffsets, |
kWasmDebugInfoFunctionScripts, |
+ kWasmDebugInfoAsmJsOffsets, |
kWasmDebugInfoNumEntries |
}; |
@@ -80,6 +81,57 @@ Vector<const uint8_t> GetFunctionBytes(Handle<WasmDebugInfo> debug_info, |
offset_and_length.second); |
} |
+FixedArray *GetOffsetTables(Handle<WasmDebugInfo> debug_info, |
+ Isolate *isolate) { |
+ Object *offset_tables = debug_info->get(kWasmDebugInfoAsmJsOffsets); |
+ if (!offset_tables->IsUndefined(isolate)) { |
+ return FixedArray::cast(offset_tables); |
+ } |
+ |
+ AsmJsOffsetsResult asm_offsets; |
+ { |
+ Handle<JSObject> wasm_object(debug_info->wasm_object(), isolate); |
+ Handle<WasmCompiledModule> compiled_module = |
+ handle(GetCompiledModule(*wasm_object), isolate); |
+ DCHECK(compiled_module->has_asm_js_offset_tables()); |
+ Handle<ByteArray> asm_offset_tables = |
+ compiled_module->asm_js_offset_tables(); |
+ uint32_t num_imported_functions = |
+ static_cast<uint32_t>(wasm::GetNumImportedFunctions(wasm_object)); |
+ DisallowHeapAllocation no_gc; |
+ const byte *bytes_start = asm_offset_tables->GetDataStartAddress(); |
+ const byte *bytes_end = bytes_start + asm_offset_tables->length(); |
+ asm_offsets = wasm::DecodeAsmJsOffsets(bytes_start, bytes_end, |
+ num_imported_functions); |
+ } |
+ // Wasm bytes must be valid and must contain asm.js offset table. |
+ DCHECK(asm_offsets.ok()); |
+ DCHECK_GE(static_cast<size_t>(kMaxInt), asm_offsets.val.size()); |
+ int num_functions = static_cast<int>(asm_offsets.val.size()); |
+ DCHECK_EQ(wasm::GetNumberOfFunctions(handle(debug_info->wasm_object())), |
+ num_functions); |
+ Handle<FixedArray> all_tables = |
+ isolate->factory()->NewFixedArray(num_functions); |
+ debug_info->set(kWasmDebugInfoAsmJsOffsets, *all_tables); |
+ for (int func = 0; func < num_functions; ++func) { |
+ std::vector<std::pair<int, int>> &func_asm_offsets = asm_offsets.val[func]; |
+ if (func_asm_offsets.empty()) continue; |
+ size_t array_size = 2 * kIntSize * func_asm_offsets.size(); |
+ CHECK_LE(array_size, static_cast<size_t>(kMaxInt)); |
+ ByteArray *arr = |
+ *isolate->factory()->NewByteArray(static_cast<int>(array_size)); |
+ all_tables->set(func, arr); |
+ int idx = 0; |
+ for (std::pair<int, int> p : func_asm_offsets) { |
+ // Byte offsets must be strictly monotonously increasing: |
+ DCHECK(idx == 0 || p.first > arr->get_int(idx - 2)); |
+ arr->set_int(idx++, p.first); |
+ arr->set_int(idx++, p.second); |
+ } |
+ DCHECK_EQ(arr->length(), idx * kIntSize); |
+ } |
+ return *all_tables; |
+} |
} // namespace |
Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) { |
@@ -236,3 +288,29 @@ Handle<FixedArray> WasmDebugInfo::GetFunctionOffsetTable( |
return offset_table; |
} |
+ |
+int WasmDebugInfo::GetAsmJsSourcePosition(Handle<WasmDebugInfo> debug_info, |
+ int func_index, int byte_offset) { |
+ Isolate *isolate = debug_info->GetIsolate(); |
+ FixedArray *offset_tables = GetOffsetTables(debug_info, isolate); |
+ |
+ DCHECK_LT(func_index, offset_tables->length()); |
+ ByteArray *offset_table = ByteArray::cast(offset_tables->get(func_index)); |
+ |
+ // Binary search for the current byte offset. |
+ int left = 0; // inclusive |
+ int right = offset_table->length() / kIntSize / 2; // exclusive |
+ DCHECK_LT(left, right); |
+ while (right - left > 1) { |
+ int mid = left + (right - left) / 2; |
+ if (offset_table->get_int(2 * mid) < byte_offset) { |
+ left = mid; |
+ } else { |
+ right = mid; |
+ } |
+ } |
+ // There should be an entry for each position that could show up on the stack |
+ // trace: |
+ DCHECK_EQ(byte_offset, offset_table->get_int(2 * left)); |
+ return offset_table->get_int(2 * left + 1); |
+} |