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_PACKET_H_
6#define V8_DEBUG_WASM_GDB_SERVER_PACKET_H_
7
8#include <string>
9
10#include "src/base/macros.h"
11
12namespace v8 {
13namespace internal {
14namespace wasm {
15namespace gdb_server {
16
17class V8_EXPORT_PRIVATE Packet {
18 public:
19  Packet();
20
21  // Empty the vector and reset the read/write pointers.
22  void Clear();
23
24  // Reset the read pointer, allowing the packet to be re-read.
25  void Rewind();
26
27  // Return true of the read pointer has reached the write pointer.
28  bool EndOfPacket() const;
29
30  // Store a single raw 8 bit value
31  void AddRawChar(char ch);
32
33  // Store a block of data as hex pairs per byte
34  void AddBlock(const void* ptr, uint32_t len);
35
36  // Store a byte as a 2 chars block.
37  void AddWord8(uint8_t val);
38
39  // Store a number up to 64 bits, formatted as a big-endian hex string with
40  // preceeding zeros removed.  Since zeros can be removed, the width of this
41  // number is unknown, and the number is always followed by a NULL or a
42  // separator (non hex digit).
43  void AddNumberSep(uint64_t val, char sep);
44
45  // Add a raw string.
46  void AddString(const char* str);
47
48  // Add a string stored as a stream of ASCII hex digit pairs.  It is safe
49  // to use any non-null character in this stream.  If this does not terminate
50  // the packet, there should be a separator (non hex digit) immediately
51  // following.
52  void AddHexString(const char* str);
53
54  // Retrieve a single character if available
55  bool GetRawChar(char* ch);
56
57  // Retrieve "len" ASCII character pairs.
58  bool GetBlock(void* ptr, uint32_t len);
59
60  // Retrieve a 8, 16, 32, or 64 bit word as pairs of hex digits.  These
61  // functions will always consume bits/4 characters from the stream.
62  bool GetWord8(uint8_t* val);
63
64  // Retrieve a number (formatted as a big-endian hex string) and a separator.
65  // If 'sep' is null, the separator is consumed but thrown away.
66  bool GetNumberSep(uint64_t* val, char* sep);
67
68  // Get a string from the stream
69  bool GetString(std::string* str);
70  bool GetHexString(std::string* str);
71
72  // Return a pointer to the entire packet payload
73  const char* GetPayload() const;
74  size_t GetPayloadSize() const;
75
76  // Returns true and the sequence number, or false if it is unset.
77  bool GetSequence(int32_t* seq) const;
78
79  // Parses sequence number in package data and moves read pointer past it.
80  void ParseSequence();
81
82  // Set the sequence number.
83  void SetSequence(int32_t seq);
84
85  enum class ErrDef { None = 0, BadFormat = 1, BadArgs = 2, Failed = 3 };
86  void SetError(ErrDef);
87
88  // Returns the full content of a GDB-remote packet, in the format:
89  //    $payload#checksum
90  // where the two-digit checksum is computed as the modulo 256 sum of all
91  // characters between the leading ‘$’ and the trailing ‘#’.
92  std::string GetPacketData() const;
93
94 private:
95  int32_t seq_;
96  std::string data_;
97  size_t read_index_;
98};
99
100}  // namespace gdb_server
101}  // namespace wasm
102}  // namespace internal
103}  // namespace v8
104
105#endif  // V8_DEBUG_WASM_GDB_SERVER_PACKET_H_
106