OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/wasm/wasm-debug.h" | 5 #include "src/wasm/wasm-debug.h" |
6 | 6 |
7 #include "src/assert-scope.h" | 7 #include "src/assert-scope.h" |
8 #include "src/debug/debug.h" | 8 #include "src/debug/debug.h" |
9 #include "src/factory.h" | 9 #include "src/factory.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
11 #include "src/wasm/module-decoder.h" | 11 #include "src/wasm/module-decoder.h" |
12 #include "src/wasm/wasm-module.h" | 12 #include "src/wasm/wasm-module.h" |
13 | 13 |
14 using namespace v8::internal; | 14 using namespace v8::internal; |
15 using namespace v8::internal::wasm; | 15 using namespace v8::internal::wasm; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 enum { | 19 enum { |
20 kWasmDebugInfoWasmObj, | 20 kWasmDebugInfoWasmObj, |
21 kWasmDebugInfoWasmBytesHash, | 21 kWasmDebugInfoWasmBytesHash, |
22 kWasmDebugInfoFunctionByteOffsets, | 22 kWasmDebugInfoFunctionByteOffsets, |
23 kWasmDebugInfoFunctionScripts, | 23 kWasmDebugInfoFunctionScripts, |
| 24 kWasmDebugInfoAsmJsOffsets, |
24 kWasmDebugInfoNumEntries | 25 kWasmDebugInfoNumEntries |
25 }; | 26 }; |
26 | 27 |
27 ByteArray *GetOrCreateFunctionOffsetTable(Handle<WasmDebugInfo> debug_info) { | 28 ByteArray *GetOrCreateFunctionOffsetTable(Handle<WasmDebugInfo> debug_info) { |
28 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); | 29 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); |
29 Isolate *isolate = debug_info->GetIsolate(); | 30 Isolate *isolate = debug_info->GetIsolate(); |
30 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); | 31 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); |
31 | 32 |
32 FunctionOffsetsResult function_offsets; | 33 FunctionOffsetsResult function_offsets; |
33 { | 34 { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 int func_index) { | 74 int func_index) { |
74 Handle<JSObject> wasm_object(debug_info->wasm_object()); | 75 Handle<JSObject> wasm_object(debug_info->wasm_object()); |
75 Handle<SeqOneByteString> module_bytes = wasm::GetWasmBytes(wasm_object); | 76 Handle<SeqOneByteString> module_bytes = wasm::GetWasmBytes(wasm_object); |
76 std::pair<int, int> offset_and_length = | 77 std::pair<int, int> offset_and_length = |
77 GetFunctionOffsetAndLength(debug_info, func_index); | 78 GetFunctionOffsetAndLength(debug_info, func_index); |
78 return Vector<const uint8_t>( | 79 return Vector<const uint8_t>( |
79 module_bytes->GetChars() + offset_and_length.first, | 80 module_bytes->GetChars() + offset_and_length.first, |
80 offset_and_length.second); | 81 offset_and_length.second); |
81 } | 82 } |
82 | 83 |
| 84 FixedArray *GetOffsetTables(Handle<WasmDebugInfo> debug_info, |
| 85 Isolate *isolate) { |
| 86 Object *offset_tables = debug_info->get(kWasmDebugInfoAsmJsOffsets); |
| 87 if (!offset_tables->IsUndefined(isolate)) { |
| 88 return FixedArray::cast(offset_tables); |
| 89 } |
| 90 |
| 91 AsmJsOffsetsResult asm_offsets; |
| 92 { |
| 93 Handle<JSObject> wasm_object(debug_info->wasm_object(), isolate); |
| 94 Handle<WasmCompiledModule> compiled_module = |
| 95 handle(GetCompiledModule(*wasm_object), isolate); |
| 96 DCHECK(compiled_module->has_asm_js_offset_tables()); |
| 97 Handle<ByteArray> asm_offset_tables = |
| 98 compiled_module->asm_js_offset_tables(); |
| 99 uint32_t num_imported_functions = |
| 100 static_cast<uint32_t>(wasm::GetNumImportedFunctions(wasm_object)); |
| 101 DisallowHeapAllocation no_gc; |
| 102 const byte *bytes_start = asm_offset_tables->GetDataStartAddress(); |
| 103 const byte *bytes_end = bytes_start + asm_offset_tables->length(); |
| 104 asm_offsets = wasm::DecodeAsmJsOffsets(bytes_start, bytes_end, |
| 105 num_imported_functions); |
| 106 } |
| 107 // Wasm bytes must be valid and must contain asm.js offset table. |
| 108 DCHECK(asm_offsets.ok()); |
| 109 DCHECK_GE(static_cast<size_t>(kMaxInt), asm_offsets.val.size()); |
| 110 int num_functions = static_cast<int>(asm_offsets.val.size()); |
| 111 DCHECK_EQ(wasm::GetNumberOfFunctions(handle(debug_info->wasm_object())), |
| 112 num_functions); |
| 113 Handle<FixedArray> all_tables = |
| 114 isolate->factory()->NewFixedArray(num_functions); |
| 115 debug_info->set(kWasmDebugInfoAsmJsOffsets, *all_tables); |
| 116 for (int func = 0; func < num_functions; ++func) { |
| 117 std::vector<std::pair<int, int>> &func_asm_offsets = asm_offsets.val[func]; |
| 118 if (func_asm_offsets.empty()) continue; |
| 119 size_t array_size = 2 * kIntSize * func_asm_offsets.size(); |
| 120 CHECK_LE(array_size, static_cast<size_t>(kMaxInt)); |
| 121 ByteArray *arr = |
| 122 *isolate->factory()->NewByteArray(static_cast<int>(array_size)); |
| 123 all_tables->set(func, arr); |
| 124 int idx = 0; |
| 125 for (std::pair<int, int> p : func_asm_offsets) { |
| 126 // Byte offsets must be strictly monotonously increasing: |
| 127 DCHECK(idx == 0 || p.first > arr->get_int(idx - 2)); |
| 128 arr->set_int(idx++, p.first); |
| 129 arr->set_int(idx++, p.second); |
| 130 } |
| 131 DCHECK_EQ(arr->length(), idx * kIntSize); |
| 132 } |
| 133 return *all_tables; |
| 134 } |
83 } // namespace | 135 } // namespace |
84 | 136 |
85 Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) { | 137 Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) { |
86 Isolate *isolate = wasm->GetIsolate(); | 138 Isolate *isolate = wasm->GetIsolate(); |
87 Factory *factory = isolate->factory(); | 139 Factory *factory = isolate->factory(); |
88 Handle<FixedArray> arr = | 140 Handle<FixedArray> arr = |
89 factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED); | 141 factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED); |
90 arr->set(kWasmDebugInfoWasmObj, *wasm); | 142 arr->set(kWasmDebugInfoWasmObj, *wasm); |
91 int hash = 0; | 143 int hash = 0; |
92 Handle<SeqOneByteString> wasm_bytes = GetWasmBytes(wasm); | 144 Handle<SeqOneByteString> wasm_bytes = GetWasmBytes(wasm); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 int idx = 0; | 281 int idx = 0; |
230 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { | 282 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { |
231 offset_table->set(idx++, Smi::FromInt(std::get<0>(elem))); | 283 offset_table->set(idx++, Smi::FromInt(std::get<0>(elem))); |
232 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); | 284 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); |
233 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); | 285 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); |
234 } | 286 } |
235 DCHECK_EQ(idx, offset_table->length()); | 287 DCHECK_EQ(idx, offset_table->length()); |
236 | 288 |
237 return offset_table; | 289 return offset_table; |
238 } | 290 } |
| 291 |
| 292 int WasmDebugInfo::GetAsmJsSourcePosition(Handle<WasmDebugInfo> debug_info, |
| 293 int func_index, int byte_offset) { |
| 294 Isolate *isolate = debug_info->GetIsolate(); |
| 295 FixedArray *offset_tables = GetOffsetTables(debug_info, isolate); |
| 296 |
| 297 DCHECK_LT(func_index, offset_tables->length()); |
| 298 ByteArray *offset_table = ByteArray::cast(offset_tables->get(func_index)); |
| 299 |
| 300 // Binary search for the current byte offset. |
| 301 int left = 0; // inclusive |
| 302 int right = offset_table->length() / kIntSize / 2; // exclusive |
| 303 DCHECK_LT(left, right); |
| 304 while (right - left > 1) { |
| 305 int mid = left + (right - left) / 2; |
| 306 if (offset_table->get_int(2 * mid) < byte_offset) { |
| 307 left = mid; |
| 308 } else { |
| 309 right = mid; |
| 310 } |
| 311 } |
| 312 // There should be an entry for each position that could show up on the stack |
| 313 // trace: |
| 314 DCHECK_EQ(byte_offset, offset_table->get_int(2 * left)); |
| 315 return offset_table->get_int(2 * left + 1); |
| 316 } |
OLD | NEW |