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_PACKET_H_
61cb0ef41Sopenharmony_ci#define V8_DEBUG_WASM_GDB_SERVER_PACKET_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <string>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "src/base/macros.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace v8 {
131cb0ef41Sopenharmony_cinamespace internal {
141cb0ef41Sopenharmony_cinamespace wasm {
151cb0ef41Sopenharmony_cinamespace gdb_server {
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE Packet {
181cb0ef41Sopenharmony_ci public:
191cb0ef41Sopenharmony_ci  Packet();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  // Empty the vector and reset the read/write pointers.
221cb0ef41Sopenharmony_ci  void Clear();
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  // Reset the read pointer, allowing the packet to be re-read.
251cb0ef41Sopenharmony_ci  void Rewind();
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  // Return true of the read pointer has reached the write pointer.
281cb0ef41Sopenharmony_ci  bool EndOfPacket() const;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Store a single raw 8 bit value
311cb0ef41Sopenharmony_ci  void AddRawChar(char ch);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // Store a block of data as hex pairs per byte
341cb0ef41Sopenharmony_ci  void AddBlock(const void* ptr, uint32_t len);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  // Store a byte as a 2 chars block.
371cb0ef41Sopenharmony_ci  void AddWord8(uint8_t val);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // Store a number up to 64 bits, formatted as a big-endian hex string with
401cb0ef41Sopenharmony_ci  // preceeding zeros removed.  Since zeros can be removed, the width of this
411cb0ef41Sopenharmony_ci  // number is unknown, and the number is always followed by a NULL or a
421cb0ef41Sopenharmony_ci  // separator (non hex digit).
431cb0ef41Sopenharmony_ci  void AddNumberSep(uint64_t val, char sep);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  // Add a raw string.
461cb0ef41Sopenharmony_ci  void AddString(const char* str);
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  // Add a string stored as a stream of ASCII hex digit pairs.  It is safe
491cb0ef41Sopenharmony_ci  // to use any non-null character in this stream.  If this does not terminate
501cb0ef41Sopenharmony_ci  // the packet, there should be a separator (non hex digit) immediately
511cb0ef41Sopenharmony_ci  // following.
521cb0ef41Sopenharmony_ci  void AddHexString(const char* str);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // Retrieve a single character if available
551cb0ef41Sopenharmony_ci  bool GetRawChar(char* ch);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  // Retrieve "len" ASCII character pairs.
581cb0ef41Sopenharmony_ci  bool GetBlock(void* ptr, uint32_t len);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  // Retrieve a 8, 16, 32, or 64 bit word as pairs of hex digits.  These
611cb0ef41Sopenharmony_ci  // functions will always consume bits/4 characters from the stream.
621cb0ef41Sopenharmony_ci  bool GetWord8(uint8_t* val);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  // Retrieve a number (formatted as a big-endian hex string) and a separator.
651cb0ef41Sopenharmony_ci  // If 'sep' is null, the separator is consumed but thrown away.
661cb0ef41Sopenharmony_ci  bool GetNumberSep(uint64_t* val, char* sep);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  // Get a string from the stream
691cb0ef41Sopenharmony_ci  bool GetString(std::string* str);
701cb0ef41Sopenharmony_ci  bool GetHexString(std::string* str);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  // Return a pointer to the entire packet payload
731cb0ef41Sopenharmony_ci  const char* GetPayload() const;
741cb0ef41Sopenharmony_ci  size_t GetPayloadSize() const;
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  // Returns true and the sequence number, or false if it is unset.
771cb0ef41Sopenharmony_ci  bool GetSequence(int32_t* seq) const;
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  // Parses sequence number in package data and moves read pointer past it.
801cb0ef41Sopenharmony_ci  void ParseSequence();
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  // Set the sequence number.
831cb0ef41Sopenharmony_ci  void SetSequence(int32_t seq);
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  enum class ErrDef { None = 0, BadFormat = 1, BadArgs = 2, Failed = 3 };
861cb0ef41Sopenharmony_ci  void SetError(ErrDef);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  // Returns the full content of a GDB-remote packet, in the format:
891cb0ef41Sopenharmony_ci  //    $payload#checksum
901cb0ef41Sopenharmony_ci  // where the two-digit checksum is computed as the modulo 256 sum of all
911cb0ef41Sopenharmony_ci  // characters between the leading ‘$’ and the trailing ‘#’.
921cb0ef41Sopenharmony_ci  std::string GetPacketData() const;
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci private:
951cb0ef41Sopenharmony_ci  int32_t seq_;
961cb0ef41Sopenharmony_ci  std::string data_;
971cb0ef41Sopenharmony_ci  size_t read_index_;
981cb0ef41Sopenharmony_ci};
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci}  // namespace gdb_server
1011cb0ef41Sopenharmony_ci}  // namespace wasm
1021cb0ef41Sopenharmony_ci}  // namespace internal
1031cb0ef41Sopenharmony_ci}  // namespace v8
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci#endif  // V8_DEBUG_WASM_GDB_SERVER_PACKET_H_
106