| 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 kWasmDebugInfoNumEntries | 24 kWasmDebugInfoNumEntries |
| 24 }; | 25 }; |
| 25 | 26 |
| 26 ByteArray *GetOrCreateFunctionOffsetTable(WasmDebugInfo *debug_info) { | 27 ByteArray *GetOrCreateFunctionOffsetTable(WasmDebugInfo *debug_info) { |
| 27 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); | 28 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); |
| 28 Isolate *isolate = debug_info->GetIsolate(); | 29 Isolate *isolate = debug_info->GetIsolate(); |
| 29 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); | 30 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); |
| 30 | 31 |
| 31 FunctionOffsetsResult function_offsets; | 32 FunctionOffsetsResult function_offsets; |
| 32 { | 33 { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 int hash = 0; | 91 int hash = 0; |
| 91 Handle<SeqOneByteString> wasm_bytes(GetWasmBytes(*wasm), isolate); | 92 Handle<SeqOneByteString> wasm_bytes(GetWasmBytes(*wasm), isolate); |
| 92 { | 93 { |
| 93 DisallowHeapAllocation no_gc; | 94 DisallowHeapAllocation no_gc; |
| 94 hash = StringHasher::HashSequentialString( | 95 hash = StringHasher::HashSequentialString( |
| 95 wasm_bytes->GetChars(), wasm_bytes->length(), kZeroHashSeed); | 96 wasm_bytes->GetChars(), wasm_bytes->length(), kZeroHashSeed); |
| 96 } | 97 } |
| 97 Handle<Object> hash_obj = factory->NewNumberFromInt(hash, TENURED); | 98 Handle<Object> hash_obj = factory->NewNumberFromInt(hash, TENURED); |
| 98 arr->set(kWasmDebugInfoWasmBytesHash, *hash_obj); | 99 arr->set(kWasmDebugInfoWasmBytesHash, *hash_obj); |
| 99 | 100 |
| 101 int num_functions = wasm::GetNumberOfFunctions(*wasm); |
| 102 Handle<FixedArray> functionScripts = |
| 103 factory->NewFixedArray(num_functions, TENURED); |
| 104 arr->set(kWasmDebugInfoFunctionScripts, *functionScripts); |
| 105 |
| 100 return Handle<WasmDebugInfo>::cast(arr); | 106 return Handle<WasmDebugInfo>::cast(arr); |
| 101 } | 107 } |
| 102 | 108 |
| 103 bool WasmDebugInfo::IsDebugInfo(Object *object) { | 109 bool WasmDebugInfo::IsDebugInfo(Object *object) { |
| 104 if (!object->IsFixedArray()) return false; | 110 if (!object->IsFixedArray()) return false; |
| 105 FixedArray *arr = FixedArray::cast(object); | 111 FixedArray *arr = FixedArray::cast(object); |
| 106 Isolate *isolate = arr->GetIsolate(); | 112 Isolate *isolate = arr->GetIsolate(); |
| 107 return arr->length() == kWasmDebugInfoNumEntries && | 113 return arr->length() == kWasmDebugInfoNumEntries && |
| 108 IsWasmObject(arr->get(kWasmDebugInfoWasmObj)) && | 114 IsWasmObject(arr->get(kWasmDebugInfoWasmObj)) && |
| 109 arr->get(kWasmDebugInfoWasmBytesHash)->IsNumber() && | 115 arr->get(kWasmDebugInfoWasmBytesHash)->IsNumber() && |
| 110 (arr->get(kWasmDebugInfoFunctionByteOffsets)->IsUndefined(isolate) || | 116 (arr->get(kWasmDebugInfoFunctionByteOffsets)->IsUndefined(isolate) || |
| 111 arr->get(kWasmDebugInfoFunctionByteOffsets)->IsByteArray()); | 117 arr->get(kWasmDebugInfoFunctionByteOffsets)->IsByteArray()) && |
| 118 (arr->get(kWasmDebugInfoFunctionScripts)->IsUndefined(isolate) || |
| 119 arr->get(kWasmDebugInfoFunctionScripts)->IsFixedArray()); |
| 112 } | 120 } |
| 113 | 121 |
| 114 WasmDebugInfo *WasmDebugInfo::cast(Object *object) { | 122 WasmDebugInfo *WasmDebugInfo::cast(Object *object) { |
| 115 DCHECK(IsDebugInfo(object)); | 123 DCHECK(IsDebugInfo(object)); |
| 116 return static_cast<WasmDebugInfo *>(object); | 124 return static_cast<WasmDebugInfo *>(object); |
| 117 } | 125 } |
| 118 | 126 |
| 119 JSObject *WasmDebugInfo::wasm_object() { | 127 JSObject *WasmDebugInfo::wasm_object() { |
| 120 return JSObject::cast(get(kWasmDebugInfoWasmObj)); | 128 return JSObject::cast(get(kWasmDebugInfoWasmObj)); |
| 121 } | 129 } |
| 122 | 130 |
| 131 Script *WasmDebugInfo::GetFunctionScript(int func_index) { |
| 132 Isolate *isolate = GetIsolate(); |
| 133 FixedArray *scripts = FixedArray::cast(get(kWasmDebugInfoFunctionScripts)); |
| 134 DCHECK(func_index >= 0 && func_index < scripts->length()); |
| 135 Object *script_or_undef = scripts->get(func_index); |
| 136 if (!script_or_undef->IsUndefined(isolate)) { |
| 137 return Script::cast(script_or_undef); |
| 138 } |
| 139 |
| 140 JSObject *wasm0 = wasm_object(); |
| 141 Handle<JSObject> wasm(wasm0, isolate); |
| 142 |
| 143 Handle<Script> script = |
| 144 isolate->factory()->NewScript(isolate->factory()->empty_string()); |
| 145 scripts->set(func_index, *script); |
| 146 |
| 147 script->set_type(Script::TYPE_WASM); |
| 148 |
| 149 Handle<ByteArray> offset_arr(GetOrCreateFunctionOffsetTable(this), isolate); |
| 150 int func_bytes_len = offset_arr->get_int(2 * func_index + 1); |
| 151 Handle<FixedArray> line_ends = isolate->factory()->NewFixedArray(1, TENURED); |
| 152 line_ends->set(0, Smi::FromInt(func_bytes_len)); |
| 153 line_ends->set_map(isolate->heap()->fixed_cow_array_map()); |
| 154 script->set_line_ends(*line_ends); |
| 155 |
| 156 int hash = 0; |
| 157 get(kWasmDebugInfoWasmBytesHash)->ToInt32(&hash); |
| 158 |
| 159 // TODO(clemensh): Register this new script at the debugger. |
| 160 USE(hash); |
| 161 |
| 162 return *script; |
| 163 } |
| 164 |
| 123 bool WasmDebugInfo::SetBreakPoint(int byte_offset) { return false; } | 165 bool WasmDebugInfo::SetBreakPoint(int byte_offset) { return false; } |
| 124 | 166 |
| 125 Handle<String> WasmDebugInfo::DisassembleFunction(int func_index) { | 167 Handle<String> WasmDebugInfo::DisassembleFunction(int func_index) { |
| 126 std::ostringstream disassembly_os; | 168 std::ostringstream disassembly_os; |
| 127 | 169 |
| 128 { | 170 { |
| 129 DisallowHeapAllocation no_gc; | 171 DisallowHeapAllocation no_gc; |
| 130 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); | 172 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); |
| 131 | 173 |
| 132 base::AccountingAllocator allocator; | 174 base::AccountingAllocator allocator; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 int idx = 0; | 217 int idx = 0; |
| 176 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { | 218 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { |
| 177 offset_table->set(idx++, Smi::FromInt(std::get<0>(elem))); | 219 offset_table->set(idx++, Smi::FromInt(std::get<0>(elem))); |
| 178 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); | 220 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); |
| 179 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); | 221 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); |
| 180 } | 222 } |
| 181 DCHECK_EQ(idx, offset_table->length()); | 223 DCHECK_EQ(idx, offset_table->length()); |
| 182 | 224 |
| 183 return offset_table; | 225 return offset_table; |
| 184 } | 226 } |
| OLD | NEW |