11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_
61cb0ef41Sopenharmony_ci#define V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/debug/debug.h"
91cb0ef41Sopenharmony_ci#include "src/debug/wasm/gdb-server/gdb-remote-util.h"
101cb0ef41Sopenharmony_ci#include "src/execution/frames.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace v8 {
131cb0ef41Sopenharmony_cinamespace internal {
141cb0ef41Sopenharmony_cinamespace wasm {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciclass WasmValue;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cinamespace gdb_server {
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Represents the interface to access the Wasm engine state for a given module.
211cb0ef41Sopenharmony_ci// For the moment it only works with interpreted functions, in the future it
221cb0ef41Sopenharmony_ci// could be extended to also support Liftoff.
231cb0ef41Sopenharmony_ciclass WasmModuleDebug {
241cb0ef41Sopenharmony_ci public:
251cb0ef41Sopenharmony_ci  WasmModuleDebug(v8::Isolate* isolate, Local<debug::WasmScript> script);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  std::string GetModuleName() const;
281cb0ef41Sopenharmony_ci  i::Isolate* GetIsolate() const {
291cb0ef41Sopenharmony_ci    return reinterpret_cast<i::Isolate*>(isolate_);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  // Gets the value of the {index}th global value.
331cb0ef41Sopenharmony_ci  static bool GetWasmGlobal(Isolate* isolate, uint32_t frame_index,
341cb0ef41Sopenharmony_ci                            uint32_t index, uint8_t* buffer,
351cb0ef41Sopenharmony_ci                            uint32_t buffer_size, uint32_t* size);
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // Gets the value of the {index}th local value in the {frame_index}th stack
381cb0ef41Sopenharmony_ci  // frame.
391cb0ef41Sopenharmony_ci  static bool GetWasmLocal(Isolate* isolate, uint32_t frame_index,
401cb0ef41Sopenharmony_ci                           uint32_t index, uint8_t* buffer,
411cb0ef41Sopenharmony_ci                           uint32_t buffer_size, uint32_t* size);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  // Gets the value of the {index}th value in the operand stack.
441cb0ef41Sopenharmony_ci  static bool GetWasmStackValue(Isolate* isolate, uint32_t frame_index,
451cb0ef41Sopenharmony_ci                                uint32_t index, uint8_t* buffer,
461cb0ef41Sopenharmony_ci                                uint32_t buffer_size, uint32_t* size);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // Reads {size} bytes, starting from {offset}, from the Memory instance
491cb0ef41Sopenharmony_ci  // associated to this module.
501cb0ef41Sopenharmony_ci  // Returns the number of byte copied to {buffer}, or 0 is case of error.
511cb0ef41Sopenharmony_ci  // Note: only one Memory for Module is currently supported.
521cb0ef41Sopenharmony_ci  uint32_t GetWasmMemory(Isolate* isolate, uint32_t offset, uint8_t* buffer,
531cb0ef41Sopenharmony_ci                         uint32_t size);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  // Reads {size} bytes, starting from {offset}, from the first segment
561cb0ef41Sopenharmony_ci  // associated to this module.
571cb0ef41Sopenharmony_ci  // Returns the number of byte copied to {buffer}, or 0 is case of error.
581cb0ef41Sopenharmony_ci  // Note: only one Memory for Module is currently supported.
591cb0ef41Sopenharmony_ci  uint32_t GetWasmData(Isolate* isolate, uint32_t offset, uint8_t* buffer,
601cb0ef41Sopenharmony_ci                       uint32_t size);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  // Gets {size} bytes, starting from {offset}, from the Code space of this
631cb0ef41Sopenharmony_ci  // module.
641cb0ef41Sopenharmony_ci  // Returns the number of byte copied to {buffer}, or 0 is case of error.
651cb0ef41Sopenharmony_ci  uint32_t GetWasmModuleBytes(wasm_addr_t wasm_addr, uint8_t* buffer,
661cb0ef41Sopenharmony_ci                              uint32_t size);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  // Inserts a breakpoint at the offset {offset} of this module.
691cb0ef41Sopenharmony_ci  // Returns {true} if the breakpoint was successfully added.
701cb0ef41Sopenharmony_ci  bool AddBreakpoint(uint32_t offset, int* breakpoint_id);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  // Removes a breakpoint at the offset {offset} of the this module.
731cb0ef41Sopenharmony_ci  void RemoveBreakpoint(uint32_t offset, int breakpoint_id);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  // Handle stepping in wasm functions via the wasm interpreter.
761cb0ef41Sopenharmony_ci  void PrepareStep();
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  // Returns the current stack trace as a vector of instruction pointers.
791cb0ef41Sopenharmony_ci  static std::vector<wasm_addr_t> GetCallStack(uint32_t debug_context_id,
801cb0ef41Sopenharmony_ci                                               Isolate* isolate);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci private:
831cb0ef41Sopenharmony_ci  // Returns the module WasmInstance associated to the {frame_index}th frame
841cb0ef41Sopenharmony_ci  // in the call stack.
851cb0ef41Sopenharmony_ci  static Handle<WasmInstanceObject> GetWasmInstance(Isolate* isolate,
861cb0ef41Sopenharmony_ci                                                    uint32_t frame_index);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  // Returns its first WasmInstance for this Wasm module.
891cb0ef41Sopenharmony_ci  Handle<WasmInstanceObject> GetFirstWasmInstance();
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  // Iterates on current stack frames and return frame information for the
921cb0ef41Sopenharmony_ci  // {frame_index} specified.
931cb0ef41Sopenharmony_ci  // Returns an empty array if the frame specified does not correspond to a Wasm
941cb0ef41Sopenharmony_ci  // stack frame.
951cb0ef41Sopenharmony_ci  static std::vector<FrameSummary> FindWasmFrame(
961cb0ef41Sopenharmony_ci      StackTraceFrameIterator* frame_it, uint32_t* frame_index);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  // Converts a WasmValue into an array of bytes.
991cb0ef41Sopenharmony_ci  static bool GetWasmValue(const wasm::WasmValue& wasm_value, uint8_t* buffer,
1001cb0ef41Sopenharmony_ci                           uint32_t buffer_size, uint32_t* size);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  v8::Isolate* isolate_;
1031cb0ef41Sopenharmony_ci  Global<debug::WasmScript> wasm_script_;
1041cb0ef41Sopenharmony_ci};
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci}  // namespace gdb_server
1071cb0ef41Sopenharmony_ci}  // namespace wasm
1081cb0ef41Sopenharmony_ci}  // namespace internal
1091cb0ef41Sopenharmony_ci}  // namespace v8
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci#endif  // V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_
112