1 // Copyright 2020 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 #ifndef V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_ 6 #define V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_ 7 8 #include "src/debug/debug.h" 9 #include "src/debug/wasm/gdb-server/gdb-remote-util.h" 10 #include "src/execution/frames.h" 11 12 namespace v8 { 13 namespace internal { 14 namespace wasm { 15 16 class WasmValue; 17 18 namespace gdb_server { 19 20 // Represents the interface to access the Wasm engine state for a given module. 21 // For the moment it only works with interpreted functions, in the future it 22 // could be extended to also support Liftoff. 23 class WasmModuleDebug { 24 public: 25 WasmModuleDebug(v8::Isolate* isolate, Local<debug::WasmScript> script); 26 27 std::string GetModuleName() const; GetIsolate()28 i::Isolate* GetIsolate() const { 29 return reinterpret_cast<i::Isolate*>(isolate_); 30 } 31 32 // Gets the value of the {index}th global value. 33 static bool GetWasmGlobal(Isolate* isolate, uint32_t frame_index, 34 uint32_t index, uint8_t* buffer, 35 uint32_t buffer_size, uint32_t* size); 36 37 // Gets the value of the {index}th local value in the {frame_index}th stack 38 // frame. 39 static bool GetWasmLocal(Isolate* isolate, uint32_t frame_index, 40 uint32_t index, uint8_t* buffer, 41 uint32_t buffer_size, uint32_t* size); 42 43 // Gets the value of the {index}th value in the operand stack. 44 static bool GetWasmStackValue(Isolate* isolate, uint32_t frame_index, 45 uint32_t index, uint8_t* buffer, 46 uint32_t buffer_size, uint32_t* size); 47 48 // Reads {size} bytes, starting from {offset}, from the Memory instance 49 // associated to this module. 50 // Returns the number of byte copied to {buffer}, or 0 is case of error. 51 // Note: only one Memory for Module is currently supported. 52 static uint32_t GetWasmMemory(Isolate* isolate, uint32_t frame_index, 53 uint32_t offset, uint8_t* buffer, 54 uint32_t size); 55 56 // Gets {size} bytes, starting from {offset}, from the Code space of this 57 // module. 58 // Returns the number of byte copied to {buffer}, or 0 is case of error. 59 uint32_t GetWasmModuleBytes(wasm_addr_t wasm_addr, uint8_t* buffer, 60 uint32_t size); 61 62 // Inserts a breakpoint at the offset {offset} of this module. 63 // Returns {true} if the breakpoint was successfully added. 64 bool AddBreakpoint(uint32_t offset, int* breakpoint_id); 65 66 // Removes a breakpoint at the offset {offset} of the this module. 67 void RemoveBreakpoint(uint32_t offset, int breakpoint_id); 68 69 // Handle stepping in wasm functions via the wasm interpreter. 70 void PrepareStep(); 71 72 // Returns the current stack trace as a vector of instruction pointers. 73 static std::vector<wasm_addr_t> GetCallStack(uint32_t debug_context_id, 74 Isolate* isolate); 75 76 private: 77 // Returns the module WasmInstance associated to the {frame_index}th frame 78 // in the call stack. 79 static Handle<WasmInstanceObject> GetWasmInstance(Isolate* isolate, 80 uint32_t frame_index); 81 82 // Returns its first WasmInstance for this Wasm module. 83 Handle<WasmInstanceObject> GetFirstWasmInstance(); 84 85 // Iterates on current stack frames and return frame information for the 86 // {frame_index} specified. 87 // Returns an empty array if the frame specified does not correspond to a Wasm 88 // stack frame. 89 static std::vector<FrameSummary> FindWasmFrame( 90 StackTraceFrameIterator* frame_it, uint32_t* frame_index); 91 92 // Converts a WasmValue into an array of bytes. 93 static bool GetWasmValue(const wasm::WasmValue& wasm_value, uint8_t* buffer, 94 uint32_t buffer_size, uint32_t* size); 95 96 v8::Isolate* isolate_; 97 Global<debug::WasmScript> wasm_script_; 98 }; 99 100 } // namespace gdb_server 101 } // namespace wasm 102 } // namespace internal 103 } // namespace v8 104 105 #endif // V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_ 106