OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/wasm/wasm-debug.h" |
| 6 |
| 7 #include "src/assert-scope.h" |
| 8 #include "src/debug/debug.h" |
| 9 #include "src/factory.h" |
| 10 #include "src/isolate.h" |
| 11 #include "src/wasm/module-decoder.h" |
| 12 #include "src/wasm/wasm-module.h" |
| 13 |
| 14 using namespace v8::internal; |
| 15 using namespace v8::internal::wasm; |
| 16 |
| 17 namespace { |
| 18 |
| 19 enum { |
| 20 kWasmDebugInfoWasmObj, |
| 21 kWasmDebugInfoWasmBytesHash, |
| 22 kWasmDebugInfoFunctionByteOffsets, |
| 23 kWasmDebugInfoNumEntries |
| 24 }; |
| 25 |
| 26 ByteArray *GetOrCreateFunctionOffsetTable(WasmDebugInfo *debug_info) { |
| 27 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); |
| 28 Isolate *isolate = debug_info->GetIsolate(); |
| 29 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); |
| 30 |
| 31 FunctionOffsetsResult function_offsets; |
| 32 { |
| 33 DisallowHeapAllocation no_gc; |
| 34 SeqOneByteString *wasm_bytes = |
| 35 wasm::GetWasmBytes(debug_info->wasm_object()); |
| 36 const byte *bytes_start = wasm_bytes->GetChars(); |
| 37 const byte *bytes_end = bytes_start + wasm_bytes->length(); |
| 38 function_offsets = wasm::DecodeWasmFunctionOffsets(bytes_start, bytes_end); |
| 39 } |
| 40 DCHECK(function_offsets.ok()); |
| 41 size_t array_size = 2 * kIntSize * function_offsets.val.size(); |
| 42 CHECK_LE(array_size, static_cast<size_t>(kMaxInt)); |
| 43 ByteArray *arr = |
| 44 *isolate->factory()->NewByteArray(static_cast<int>(array_size)); |
| 45 int idx = 0; |
| 46 for (std::pair<int, int> p : function_offsets.val) { |
| 47 arr->set_int(idx++, p.first); |
| 48 arr->set_int(idx++, p.second); |
| 49 } |
| 50 DCHECK_EQ(arr->length(), idx * kIntSize); |
| 51 debug_info->set(kWasmDebugInfoFunctionByteOffsets, arr); |
| 52 |
| 53 return arr; |
| 54 } |
| 55 |
| 56 std::pair<int, int> GetFunctionOffsetAndLength(WasmDebugInfo *debug_info, |
| 57 int func_index) { |
| 58 ByteArray *arr = GetOrCreateFunctionOffsetTable(debug_info); |
| 59 DCHECK(func_index >= 0 && func_index < arr->length() / kIntSize / 2); |
| 60 |
| 61 int offset = arr->get_int(2 * func_index); |
| 62 int length = arr->get_int(2 * func_index + 1); |
| 63 // Assert that it's distinguishable from the "illegal function index" return. |
| 64 DCHECK(offset > 0 && length > 0); |
| 65 return {offset, length}; |
| 66 } |
| 67 |
| 68 Vector<const uint8_t> GetFunctionBytes(WasmDebugInfo *debug_info, |
| 69 int func_index) { |
| 70 DCHECK(!AllowHeapAllocation::IsAllowed()); |
| 71 SeqOneByteString *module_bytes = |
| 72 wasm::GetWasmBytes(debug_info->wasm_object()); |
| 73 std::pair<int, int> offset_and_length = |
| 74 GetFunctionOffsetAndLength(debug_info, func_index); |
| 75 return Vector<const uint8_t>( |
| 76 module_bytes->GetChars() + offset_and_length.first, |
| 77 offset_and_length.second); |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) { |
| 83 Isolate *isolate = wasm->GetIsolate(); |
| 84 Factory *factory = isolate->factory(); |
| 85 Handle<FixedArray> arr = |
| 86 factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED); |
| 87 arr->set(kWasmDebugInfoWasmObj, *wasm); |
| 88 int hash = 0; |
| 89 Handle<SeqOneByteString> wasm_bytes(GetWasmBytes(*wasm), isolate); |
| 90 { |
| 91 DisallowHeapAllocation no_gc; |
| 92 hash = StringHasher::HashSequentialString( |
| 93 wasm_bytes->GetChars(), wasm_bytes->length(), kZeroHashSeed); |
| 94 } |
| 95 Handle<Object> hash_obj = factory->NewNumberFromInt(hash, TENURED); |
| 96 arr->set(kWasmDebugInfoWasmBytesHash, *hash_obj); |
| 97 |
| 98 return Handle<WasmDebugInfo>::cast(arr); |
| 99 } |
| 100 |
| 101 bool WasmDebugInfo::IsDebugInfo(Object *object) { |
| 102 if (!object->IsFixedArray()) return false; |
| 103 FixedArray *arr = FixedArray::cast(object); |
| 104 Isolate *isolate = arr->GetIsolate(); |
| 105 return arr->length() == kWasmDebugInfoNumEntries && |
| 106 IsWasmObject(arr->get(kWasmDebugInfoWasmObj)) && |
| 107 arr->get(kWasmDebugInfoWasmBytesHash)->IsNumber() && |
| 108 (arr->get(kWasmDebugInfoFunctionByteOffsets)->IsUndefined(isolate) || |
| 109 arr->get(kWasmDebugInfoFunctionByteOffsets)->IsByteArray()); |
| 110 } |
| 111 |
| 112 WasmDebugInfo *WasmDebugInfo::cast(Object *object) { |
| 113 DCHECK(IsDebugInfo(object)); |
| 114 return reinterpret_cast<WasmDebugInfo *>(object); |
| 115 } |
| 116 |
| 117 JSObject *WasmDebugInfo::wasm_object() { |
| 118 return JSObject::cast(get(kWasmDebugInfoWasmObj)); |
| 119 } |
| 120 |
| 121 bool WasmDebugInfo::SetBreakPoint(int byte_offset) { |
| 122 // TODO(clemensh): Implement this. |
| 123 return false; |
| 124 } |
| 125 |
| 126 Handle<String> WasmDebugInfo::DisassembleFunction(int func_index) { |
| 127 std::ostringstream disassembly_os; |
| 128 |
| 129 { |
| 130 DisallowHeapAllocation no_gc; |
| 131 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); |
| 132 |
| 133 base::AccountingAllocator allocator; |
| 134 bool ok = PrintAst( |
| 135 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), |
| 136 disassembly_os, nullptr); |
| 137 DCHECK(ok); |
| 138 USE(ok); |
| 139 } |
| 140 |
| 141 // Unfortunately, we have to copy the string here. |
| 142 std::string code_str = disassembly_os.str(); |
| 143 CHECK_LE(code_str.length(), static_cast<size_t>(kMaxInt)); |
| 144 return GetIsolate() |
| 145 ->factory() |
| 146 ->NewStringFromAscii(Vector<const char>( |
| 147 code_str.data(), static_cast<int>(code_str.length()))) |
| 148 .ToHandleChecked(); |
| 149 } |
| 150 |
| 151 Handle<FixedArray> WasmDebugInfo::GetFunctionOffsetTable(int func_index) { |
| 152 class NullBuf : public std::streambuf {}; |
| 153 NullBuf null_buf; |
| 154 std::ostream null_stream(&null_buf); |
| 155 |
| 156 std::vector<std::tuple<uint32_t, int, int>> offset_table_vec; |
| 157 |
| 158 { |
| 159 DisallowHeapAllocation no_gc; |
| 160 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); |
| 161 |
| 162 v8::base::AccountingAllocator allocator; |
| 163 bool ok = PrintAst( |
| 164 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), |
| 165 null_stream, &offset_table_vec); |
| 166 DCHECK(ok); |
| 167 USE(ok); |
| 168 } |
| 169 |
| 170 size_t arr_size = 3 * offset_table_vec.size(); |
| 171 CHECK_LE(arr_size, static_cast<size_t>(kMaxInt)); |
| 172 Handle<FixedArray> offset_table = GetIsolate()->factory()->NewFixedArray( |
| 173 static_cast<int>(arr_size), TENURED); |
| 174 |
| 175 int idx = 0; |
| 176 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { |
| 177 offset_table->set(idx++, Smi::FromInt(std::get<0>(elem))); |
| 178 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); |
| 179 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); |
| 180 } |
| 181 DCHECK_EQ(idx, offset_table->length()); |
| 182 |
| 183 return offset_table; |
| 184 } |
OLD | NEW |