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_SESSION_H_
61cb0ef41Sopenharmony_ci#define V8_DEBUG_WASM_GDB_SERVER_SESSION_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/base/macros.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace v8 {
111cb0ef41Sopenharmony_cinamespace internal {
121cb0ef41Sopenharmony_cinamespace wasm {
131cb0ef41Sopenharmony_cinamespace gdb_server {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciclass Packet;
161cb0ef41Sopenharmony_ciclass TransportBase;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Represents a gdb-remote debugging session.
191cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE Session {
201cb0ef41Sopenharmony_ci public:
211cb0ef41Sopenharmony_ci  explicit Session(TransportBase* transport);
221cb0ef41Sopenharmony_ci  Session(const Session&) = delete;
231cb0ef41Sopenharmony_ci  Session& operator=(const Session&) = delete;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // Attempt to send a packet and optionally wait for an ACK from the receiver.
261cb0ef41Sopenharmony_ci  bool SendPacket(Packet* packet, bool expect_ack = true);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  // Attempt to receive a packet.
291cb0ef41Sopenharmony_ci  bool GetPacket(Packet* packet);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Return true if there is data to read.
321cb0ef41Sopenharmony_ci  bool IsDataAvailable() const;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  // Return true if the connection is still valid.
351cb0ef41Sopenharmony_ci  bool IsConnected() const;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // Shutdown the connection.
381cb0ef41Sopenharmony_ci  void Disconnect();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  // When a debugging session is active, the GDB-remote thread can block waiting
411cb0ef41Sopenharmony_ci  // for events and it will resume execution when one of these two events arise:
421cb0ef41Sopenharmony_ci  // - A network event (a new packet arrives, or the connection is dropped)
431cb0ef41Sopenharmony_ci  // - A thread event (the execution stopped because of a trap or breakpoint).
441cb0ef41Sopenharmony_ci  void WaitForDebugStubEvent();
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // Signal that the debuggee execution stopped because of a trap or breakpoint.
471cb0ef41Sopenharmony_ci  bool SignalThreadEvent();
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // By default, when either the debugger or the GDB-stub sends a packet,
501cb0ef41Sopenharmony_ci  // the first response expected is an acknowledgment: either '+' (to indicate
511cb0ef41Sopenharmony_ci  // the packet was received correctly) or '-' (to request retransmission).
521cb0ef41Sopenharmony_ci  // When a transport is reliable, the debugger may request that acknowledgement
531cb0ef41Sopenharmony_ci  // be disabled by means of the 'QStartNoAckMode' packet.
541cb0ef41Sopenharmony_ci  void EnableAck(bool ack_enabled) { ack_enabled_ = ack_enabled; }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci private:
571cb0ef41Sopenharmony_ci  // Read a single character from the transport.
581cb0ef41Sopenharmony_ci  bool GetChar(char* ch);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  // Read the content of a packet, from a leading '$' to a trailing '#'.
611cb0ef41Sopenharmony_ci  bool GetPayload(Packet* pkt, uint8_t* checksum);
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  TransportBase* io_;  // Transport object not owned by the Session.
641cb0ef41Sopenharmony_ci  bool connected_;     // Is the connection still valid.
651cb0ef41Sopenharmony_ci  bool ack_enabled_;   // If true, emit or wait for '+' from RSP stream.
661cb0ef41Sopenharmony_ci};
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci}  // namespace gdb_server
691cb0ef41Sopenharmony_ci}  // namespace wasm
701cb0ef41Sopenharmony_ci}  // namespace internal
711cb0ef41Sopenharmony_ci}  // namespace v8
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci#endif  // V8_DEBUG_WASM_GDB_SERVER_SESSION_H_
74