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 kWasmDebugInfoNumEntries | 23 kWasmDebugInfoNumEntries |
24 }; | 24 }; |
25 | 25 |
26 ByteArray *GetOrCreateFunctionOffsetTable(WasmDebugInfo *debug_info) { | 26 ByteArray *GetOrCreateFunctionOffsetTable(Handle<WasmDebugInfo> debug_info) { |
27 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); | 27 Object *offset_table = debug_info->get(kWasmDebugInfoFunctionByteOffsets); |
28 Isolate *isolate = debug_info->GetIsolate(); | 28 Isolate *isolate = debug_info->GetIsolate(); |
29 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); | 29 if (!offset_table->IsUndefined(isolate)) return ByteArray::cast(offset_table); |
30 | 30 |
31 FunctionOffsetsResult function_offsets; | 31 FunctionOffsetsResult function_offsets; |
32 { | 32 { |
33 DisallowHeapAllocation no_gc; | 33 DisallowHeapAllocation no_gc; |
34 SeqOneByteString *wasm_bytes = | 34 SeqOneByteString *wasm_bytes = |
35 wasm::GetWasmBytes(debug_info->wasm_object()); | 35 wasm::GetWasmBytes(debug_info->wasm_object()); |
36 const byte *bytes_start = wasm_bytes->GetChars(); | 36 const byte *bytes_start = wasm_bytes->GetChars(); |
37 const byte *bytes_end = bytes_start + wasm_bytes->length(); | 37 const byte *bytes_end = bytes_start + wasm_bytes->length(); |
38 function_offsets = wasm::DecodeWasmFunctionOffsets(bytes_start, bytes_end); | 38 function_offsets = wasm::DecodeWasmFunctionOffsets(bytes_start, bytes_end); |
39 } | 39 } |
40 DCHECK(function_offsets.ok()); | 40 DCHECK(function_offsets.ok()); |
41 size_t array_size = 2 * kIntSize * function_offsets.val.size(); | 41 size_t array_size = 2 * kIntSize * function_offsets.val.size(); |
42 CHECK_LE(array_size, static_cast<size_t>(kMaxInt)); | 42 CHECK_LE(array_size, static_cast<size_t>(kMaxInt)); |
43 ByteArray *arr = | 43 ByteArray *arr = |
44 *isolate->factory()->NewByteArray(static_cast<int>(array_size)); | 44 *isolate->factory()->NewByteArray(static_cast<int>(array_size)); |
45 int idx = 0; | 45 int idx = 0; |
46 for (std::pair<int, int> p : function_offsets.val) { | 46 for (std::pair<int, int> p : function_offsets.val) { |
47 arr->set_int(idx++, p.first); | 47 arr->set_int(idx++, p.first); |
48 arr->set_int(idx++, p.second); | 48 arr->set_int(idx++, p.second); |
49 } | 49 } |
50 DCHECK_EQ(arr->length(), idx * kIntSize); | 50 DCHECK_EQ(arr->length(), idx * kIntSize); |
51 debug_info->set(kWasmDebugInfoFunctionByteOffsets, arr); | 51 debug_info->set(kWasmDebugInfoFunctionByteOffsets, arr); |
52 | 52 |
53 return arr; | 53 return arr; |
54 } | 54 } |
55 | 55 |
56 std::pair<int, int> GetFunctionOffsetAndLength(WasmDebugInfo *debug_info, | 56 std::pair<int, int> GetFunctionOffsetAndLength(Handle<WasmDebugInfo> debug_info, |
57 int func_index) { | 57 int func_index) { |
58 ByteArray *arr = GetOrCreateFunctionOffsetTable(debug_info); | 58 ByteArray *arr = GetOrCreateFunctionOffsetTable(debug_info); |
59 DCHECK(func_index >= 0 && func_index < arr->length() / kIntSize / 2); | 59 DCHECK(func_index >= 0 && func_index < arr->length() / kIntSize / 2); |
60 | 60 |
61 int offset = arr->get_int(2 * func_index); | 61 int offset = arr->get_int(2 * func_index); |
62 int length = arr->get_int(2 * func_index + 1); | 62 int length = arr->get_int(2 * func_index + 1); |
63 // Assert that it's distinguishable from the "illegal function index" return. | 63 // Assert that it's distinguishable from the "illegal function index" return. |
64 DCHECK(offset > 0 && length > 0); | 64 DCHECK(offset > 0 && length > 0); |
65 return {offset, length}; | 65 return {offset, length}; |
66 } | 66 } |
67 | 67 |
68 Vector<const uint8_t> GetFunctionBytes(WasmDebugInfo *debug_info, | 68 Vector<const uint8_t> GetFunctionBytes(Handle<WasmDebugInfo> debug_info, |
69 int func_index) { | 69 int func_index) { |
70 DCHECK(!AllowHeapAllocation::IsAllowed()); | |
71 SeqOneByteString *module_bytes = | 70 SeqOneByteString *module_bytes = |
72 wasm::GetWasmBytes(debug_info->wasm_object()); | 71 wasm::GetWasmBytes(debug_info->wasm_object()); |
73 std::pair<int, int> offset_and_length = | 72 std::pair<int, int> offset_and_length = |
74 GetFunctionOffsetAndLength(debug_info, func_index); | 73 GetFunctionOffsetAndLength(debug_info, func_index); |
75 return Vector<const uint8_t>( | 74 return Vector<const uint8_t>( |
76 module_bytes->GetChars() + offset_and_length.first, | 75 module_bytes->GetChars() + offset_and_length.first, |
77 offset_and_length.second); | 76 offset_and_length.second); |
78 } | 77 } |
79 | 78 |
80 } // namespace | 79 } // namespace |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 115 |
117 JSObject *WasmDebugInfo::wasm_object() { | 116 JSObject *WasmDebugInfo::wasm_object() { |
118 return JSObject::cast(get(kWasmDebugInfoWasmObj)); | 117 return JSObject::cast(get(kWasmDebugInfoWasmObj)); |
119 } | 118 } |
120 | 119 |
121 bool WasmDebugInfo::SetBreakPoint(int byte_offset) { | 120 bool WasmDebugInfo::SetBreakPoint(int byte_offset) { |
122 // TODO(clemensh): Implement this. | 121 // TODO(clemensh): Implement this. |
123 return false; | 122 return false; |
124 } | 123 } |
125 | 124 |
126 Handle<String> WasmDebugInfo::DisassembleFunction(int func_index) { | 125 Handle<String> WasmDebugInfo::DisassembleFunction( |
| 126 Handle<WasmDebugInfo> debug_info, int func_index) { |
127 std::ostringstream disassembly_os; | 127 std::ostringstream disassembly_os; |
128 | 128 |
129 { | 129 { |
| 130 Vector<const uint8_t> bytes_vec = GetFunctionBytes(debug_info, func_index); |
130 DisallowHeapAllocation no_gc; | 131 DisallowHeapAllocation no_gc; |
131 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); | |
132 | 132 |
133 base::AccountingAllocator allocator; | 133 base::AccountingAllocator allocator; |
134 bool ok = PrintAst( | 134 bool ok = PrintAst( |
135 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), | 135 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), |
136 disassembly_os, nullptr); | 136 disassembly_os, nullptr); |
137 DCHECK(ok); | 137 DCHECK(ok); |
138 USE(ok); | 138 USE(ok); |
139 } | 139 } |
140 | 140 |
141 // Unfortunately, we have to copy the string here. | 141 // Unfortunately, we have to copy the string here. |
142 std::string code_str = disassembly_os.str(); | 142 std::string code_str = disassembly_os.str(); |
143 CHECK_LE(code_str.length(), static_cast<size_t>(kMaxInt)); | 143 CHECK_LE(code_str.length(), static_cast<size_t>(kMaxInt)); |
144 return GetIsolate() | 144 Factory *factory = debug_info->GetIsolate()->factory(); |
145 ->factory() | 145 Vector<const char> code_vec(code_str.data(), |
146 ->NewStringFromAscii(Vector<const char>( | 146 static_cast<int>(code_str.length())); |
147 code_str.data(), static_cast<int>(code_str.length()))) | 147 return factory->NewStringFromAscii(code_vec).ToHandleChecked(); |
148 .ToHandleChecked(); | |
149 } | 148 } |
150 | 149 |
151 Handle<FixedArray> WasmDebugInfo::GetFunctionOffsetTable(int func_index) { | 150 Handle<FixedArray> WasmDebugInfo::GetFunctionOffsetTable( |
| 151 Handle<WasmDebugInfo> debug_info, int func_index) { |
152 class NullBuf : public std::streambuf {}; | 152 class NullBuf : public std::streambuf {}; |
153 NullBuf null_buf; | 153 NullBuf null_buf; |
154 std::ostream null_stream(&null_buf); | 154 std::ostream null_stream(&null_buf); |
155 | 155 |
156 std::vector<std::tuple<uint32_t, int, int>> offset_table_vec; | 156 std::vector<std::tuple<uint32_t, int, int>> offset_table_vec; |
157 | 157 |
158 { | 158 { |
| 159 Vector<const uint8_t> bytes_vec = GetFunctionBytes(debug_info, func_index); |
159 DisallowHeapAllocation no_gc; | 160 DisallowHeapAllocation no_gc; |
160 Vector<const uint8_t> bytes_vec = GetFunctionBytes(this, func_index); | |
161 | 161 |
162 v8::base::AccountingAllocator allocator; | 162 v8::base::AccountingAllocator allocator; |
163 bool ok = PrintAst( | 163 bool ok = PrintAst( |
164 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), | 164 &allocator, FunctionBodyForTesting(bytes_vec.start(), bytes_vec.end()), |
165 null_stream, &offset_table_vec); | 165 null_stream, &offset_table_vec); |
166 DCHECK(ok); | 166 DCHECK(ok); |
167 USE(ok); | 167 USE(ok); |
168 } | 168 } |
169 | 169 |
170 size_t arr_size = 3 * offset_table_vec.size(); | 170 size_t arr_size = 3 * offset_table_vec.size(); |
171 CHECK_LE(arr_size, static_cast<size_t>(kMaxInt)); | 171 CHECK_LE(arr_size, static_cast<size_t>(kMaxInt)); |
172 Handle<FixedArray> offset_table = GetIsolate()->factory()->NewFixedArray( | 172 Factory *factory = debug_info->GetIsolate()->factory(); |
173 static_cast<int>(arr_size), TENURED); | 173 Handle<FixedArray> offset_table = |
| 174 factory->NewFixedArray(static_cast<int>(arr_size), TENURED); |
174 | 175 |
175 int idx = 0; | 176 int idx = 0; |
176 for (std::tuple<uint32_t, int, int> elem : offset_table_vec) { | 177 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<0>(elem))); |
178 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); | 179 offset_table->set(idx++, Smi::FromInt(std::get<1>(elem))); |
179 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); | 180 offset_table->set(idx++, Smi::FromInt(std::get<2>(elem))); |
180 } | 181 } |
181 DCHECK_EQ(idx, offset_table->length()); | 182 DCHECK_EQ(idx, offset_table->length()); |
182 | 183 |
183 return offset_table; | 184 return offset_table; |
184 } | 185 } |
OLD | NEW |