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
12namespace v8 {
13namespace internal {
14namespace wasm {
15
16class WasmValue;
17
18namespace 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.
23class WasmModuleDebug {
24 public:
25  WasmModuleDebug(v8::Isolate* isolate, Local<debug::WasmScript> script);
26
27  std::string GetModuleName() const;
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  uint32_t GetWasmMemory(Isolate* isolate, uint32_t offset, uint8_t* buffer,
53                         uint32_t size);
54
55  // Reads {size} bytes, starting from {offset}, from the first segment
56  // associated to this module.
57  // Returns the number of byte copied to {buffer}, or 0 is case of error.
58  // Note: only one Memory for Module is currently supported.
59  uint32_t GetWasmData(Isolate* isolate, uint32_t offset, uint8_t* buffer,
60                       uint32_t size);
61
62  // Gets {size} bytes, starting from {offset}, from the Code space of this
63  // module.
64  // Returns the number of byte copied to {buffer}, or 0 is case of error.
65  uint32_t GetWasmModuleBytes(wasm_addr_t wasm_addr, uint8_t* buffer,
66                              uint32_t size);
67
68  // Inserts a breakpoint at the offset {offset} of this module.
69  // Returns {true} if the breakpoint was successfully added.
70  bool AddBreakpoint(uint32_t offset, int* breakpoint_id);
71
72  // Removes a breakpoint at the offset {offset} of the this module.
73  void RemoveBreakpoint(uint32_t offset, int breakpoint_id);
74
75  // Handle stepping in wasm functions via the wasm interpreter.
76  void PrepareStep();
77
78  // Returns the current stack trace as a vector of instruction pointers.
79  static std::vector<wasm_addr_t> GetCallStack(uint32_t debug_context_id,
80                                               Isolate* isolate);
81
82 private:
83  // Returns the module WasmInstance associated to the {frame_index}th frame
84  // in the call stack.
85  static Handle<WasmInstanceObject> GetWasmInstance(Isolate* isolate,
86                                                    uint32_t frame_index);
87
88  // Returns its first WasmInstance for this Wasm module.
89  Handle<WasmInstanceObject> GetFirstWasmInstance();
90
91  // Iterates on current stack frames and return frame information for the
92  // {frame_index} specified.
93  // Returns an empty array if the frame specified does not correspond to a Wasm
94  // stack frame.
95  static std::vector<FrameSummary> FindWasmFrame(
96      StackTraceFrameIterator* frame_it, uint32_t* frame_index);
97
98  // Converts a WasmValue into an array of bytes.
99  static bool GetWasmValue(const wasm::WasmValue& wasm_value, uint8_t* buffer,
100                           uint32_t buffer_size, uint32_t* size);
101
102  v8::Isolate* isolate_;
103  Global<debug::WasmScript> wasm_script_;
104};
105
106}  // namespace gdb_server
107}  // namespace wasm
108}  // namespace internal
109}  // namespace v8
110
111#endif  // V8_DEBUG_WASM_GDB_SERVER_WASM_MODULE_DEBUG_H_
112