v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
target.h
Go to the documentation of this file.
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_TARGET_H_
6#define V8_DEBUG_WASM_GDB_SERVER_TARGET_H_
7
8#include <atomic>
9#include <map>
10
14
15namespace v8 {
16namespace internal {
17namespace wasm {
18namespace gdb_server {
19
20class GdbServer;
21class Packet;
22class Session;
23
24// Class Target represents a debugging target. It contains the logic to decode
25// incoming GDB-remote packets, execute them forwarding the debugger commands
26// and queries to the Wasm engine, and send back GDB-remote packets.
27class Target {
28 public:
29 // Contruct a Target object.
30 explicit Target(GdbServer* gdb_server);
31 Target(const Target&) = delete;
32 Target& operator=(const Target&) = delete;
33
34 // This function spin on a debugging session, until it closes.
35 void Run(Session* ses);
36
37 void Terminate();
38 bool IsTerminated() const { return status_ == Status::Terminated; }
39
40 // Notifies that the debuggee thread suspended at a breakpoint.
41 void OnProgramBreak(Isolate* isolate,
42 const std::vector<wasm_addr_t>& call_frames);
43 // Notifies that the debuggee thread suspended because of an unhandled
44 // exception.
45 void OnException(Isolate* isolate,
46 const std::vector<wasm_addr_t>& call_frames);
47
48 // Returns the state at the moment of the thread suspension.
49 const std::vector<wasm_addr_t> GetCallStack() const;
52
53 private:
54 void OnSuspended(Isolate* isolate, int signal,
55 const std::vector<wasm_addr_t>& call_frames);
56
57 // Initializes a map used to make fast lookups when handling query packets
58 // that have a constant response.
60
61 // Blocks waiting for one of these two events to occur:
62 // - A network packet arrives from the debugger, or the debugger connection is
63 // closed;
64 // - The debuggee suspends execution because of a trap or breakpoint.
65 void WaitForDebugEvent();
66 void ProcessDebugEvent();
67
68 // Processes GDB-remote packets that arrive from the debugger.
69 // This method should be called when the debuggee has suspended its execution.
70 void ProcessCommands();
71
72 // Requests that the thread suspends execution at the next Wasm instruction.
73 void Suspend();
74
75 enum class ErrorCode { None = 0, BadFormat = 1, BadArgs = 2, Failed = 3 };
76
78 Paused, // The command was processed, debuggee still paused.
79 Continue, // The debuggee should resume execution.
80 Detach, // Request to detach from the debugger.
81 Kill // Request to terminate the debuggee process.
82 };
83 // This function always succeedes, since all errors are reported as an error
84 // string "Exx" where xx is a two digit number.
85 // The return value indicates if the target can resume execution or it is
86 // still paused.
88
89 // Processes a general query packet
90 ErrorCode ProcessQueryPacket(const Packet* pkt_in, Packet* pkt_out);
91
92 // Formats a 'Stop-reply' packet, which is sent in response of a 'c'
93 // (continue), 's' (step) and '?' (query halt reason) commands.
94 void SetStopReply(Packet* pkt_out) const;
95
97
98 void SetStatus(Status status, int8_t signal = 0,
99 std::vector<wasm_addr_t> call_frames_ = {},
100 Isolate* isolate = nullptr);
101
103
104 std::atomic<Status> status_;
105
106 // Signal being processed.
107 std::atomic<int8_t> cur_signal_;
108
109 // Session object not owned by the Target.
111
112 // Map used to make fast lookups when handling query packets.
113 typedef std::map<std::string, std::string> QueryPropertyMap;
115
117
118 // Used to block waiting for suspension
120
123 // Protected by {mutex_}:
124
125 // Current isolate. This is not null only when the target is in a Suspended
126 // state and it is the isolate associated to the current call stack and used
127 // for all debugging activities.
129
130 // Call stack when the execution is suspended.
131 std::vector<wasm_addr_t> call_frames_;
132
133 // End of fields protected by {mutex_}.
135};
136
137} // namespace gdb_server
138} // namespace wasm
139} // namespace internal
140} // namespace v8
141
142#endif // V8_DEBUG_WASM_GDB_SERVER_TARGET_H_
std::vector< wasm_addr_t > call_frames_
Definition target.h:131
std::map< std::string, std::string > QueryPropertyMap
Definition target.h:113
const std::vector< wasm_addr_t > GetCallStack() const
Definition target.cc:685
Target(GdbServer *gdb_server)
Definition target.cc:26
void OnSuspended(Isolate *isolate, int signal, const std::vector< wasm_addr_t > &call_frames)
Definition target.cc:75
Target & operator=(const Target &)=delete
ErrorCode ProcessQueryPacket(const Packet *pkt_in, Packet *pkt_out)
Definition target.cc:453
void SetStatus(Status status, int8_t signal=0, std::vector< wasm_addr_t > call_frames_={}, Isolate *isolate=nullptr)
Definition target.cc:670
ProcessPacketResult ProcessPacket(Packet *pkt_in, Packet *pkt_out)
Definition target.cc:204
Isolate * GetCurrentIsolate() const
Definition target.h:51
void SetStopReply(Packet *pkt_out) const
Definition target.cc:650
void OnException(Isolate *isolate, const std::vector< wasm_addr_t > &call_frames)
Definition target.cc:71
std::atomic< int8_t > cur_signal_
Definition target.h:107
std::atomic< Status > status_
Definition target.h:104
void OnProgramBreak(Isolate *isolate, const std::vector< wasm_addr_t > &call_frames)
Definition target.cc:67
Definition c-api.cc:87