Listen on a socket for gdb to connect to.
[riscv-isa-sim.git] / riscv / gdbserver.h
1 #ifndef _RISCV_GDBSERVER_H
2 #define _RISCV_GDBSERVER_H
3
4 #include <stdint.h>
5
6 template <typename T>
7 class circular_buffer_t
8 {
9 public:
10 // The buffer can store capacity-1 data elements.
11 circular_buffer_t(unsigned int capacity) : data(new T[capacity]),
12 start(0), end(0), capacity(capacity) {}
13 circular_buffer_t() : start(0), end(0), capacity(0) {}
14 ~circular_buffer_t() { delete data; }
15
16 T *data;
17 unsigned int start; // Data start, inclusive.
18 unsigned int end; // Data end, exclusive.
19 unsigned int capacity; // Size of the buffer.
20 unsigned int size() const;
21 bool empty() const { return start == end; }
22 // Tell the buffer that some bytes were consumed from the start of the
23 // buffer.
24 void consume(unsigned int bytes);
25
26 // Return size and address of the block of RAM where more data can be copied
27 // to be added to the buffer.
28 unsigned int contiguous_space() const;
29 T *contiguous_data() { return data + end; }
30 void data_added(unsigned int bytes);
31
32 void reset();
33
34 T operator[](unsigned int i) {
35 return data[(start + i) % capacity];
36 }
37 };
38
39 class gdbserver_t
40 {
41 public:
42 // Create a new server, listening for connections from localhost on the given
43 // port.
44 gdbserver_t(uint16_t port);
45
46 // Process all pending messages from a client.
47 void handle();
48
49 void handle_packet(const std::vector<uint8_t> &packet);
50
51 private:
52 int socket_fd;
53 int client_fd;
54 circular_buffer_t<uint8_t> recv_buf;
55 uint8_t send_buf[64 * 1024]; // Circular buffer.
56 unsigned int send_start, send_end; // Data start (inclusive)/end (exclusive)pointers.
57
58 bool ack_mode;
59
60 // Read pending data from the client.
61 void read();
62 // Accept a new client if there isn't one already connected.
63 void accept();
64 // Process all complete requests in recv_buf.
65 void process_requests();
66 // Add the given message to send_buf.
67 void send(const char* msg);
68 };
69
70 #endif