Chromium Code Reviews| Index: src/wasm/wasm-debug.cc |
| diff --git a/src/wasm/wasm-debug.cc b/src/wasm/wasm-debug.cc |
| index aaca8622b88b4b962d9963cd4fe437c738ffeb51..ea7299515d2311b15a08c9b306faa30700697388 100644 |
| --- a/src/wasm/wasm-debug.cc |
| +++ b/src/wasm/wasm-debug.cc |
| @@ -21,6 +21,7 @@ enum { |
| kWasmDebugInfoWasmBytesHash, |
| kWasmDebugInfoFunctionByteOffsets, |
| kWasmDebugInfoFunctionScripts, |
| + kWasmDebugInfoAsmJsOffsets, |
| kWasmDebugInfoNumEntries |
| }; |
| @@ -236,3 +237,76 @@ Handle<FixedArray> WasmDebugInfo::GetFunctionOffsetTable( |
| return offset_table; |
| } |
| + |
| +int WasmDebugInfo::GetAsmJsSourcePosition(Handle<WasmDebugInfo> debug_info, |
| + int func_index, int byte_offset) { |
| + Object *offset_tables = debug_info->get(kWasmDebugInfoAsmJsOffsets); |
| + Isolate *isolate = debug_info->GetIsolate(); |
| + if (offset_tables->IsUndefined(isolate)) { |
|
titzer
2016/10/12 08:03:05
Can you extract a helper method to materialize the
Clemens Hammacher
2016/10/12 08:17:46
Done.
|
| + 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); |
| + } |
| + offset_tables = *all_tables; |
| + } |
| + |
| + FixedArray *offset_tables_arr = FixedArray::cast(offset_tables); |
| + DCHECK_LT(func_index, offset_tables_arr->length()); |
| + ByteArray *offset_table = ByteArray::cast(offset_tables_arr->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); |
| +} |