5a7a102797e85270f72ed8fc5506928ae0317c41
[riscv-isa-sim.git] / riscv / gdbserver.h
1 #ifndef _RISCV_GDBSERVER_H
2 #define _RISCV_GDBSERVER_H
3
4 #include <stdint.h>
5
6 class sim_t;
7
8 template <typename T>
9 class circular_buffer_t
10 {
11 public:
12 // The buffer can store capacity-1 data elements.
13 circular_buffer_t(unsigned int capacity) : data(new T[capacity]),
14 start(0), end(0), capacity(capacity) {}
15 circular_buffer_t() : start(0), end(0), capacity(0) {}
16 ~circular_buffer_t() { delete[] data; }
17
18 T *data;
19 unsigned int start; // Data start, inclusive.
20 unsigned int end; // Data end, exclusive.
21 unsigned int capacity; // Size of the buffer.
22 unsigned int size() const;
23 bool empty() const { return start == end; }
24 bool full() const { return ((end+1) % capacity) == start; }
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_empty_size() const;
29 T *contiguous_empty() { return data + end; }
30 void data_added(unsigned int bytes);
31
32 unsigned int contiguous_data_size() const;
33 T *contiguous_data() { return data + start; }
34 // Tell the buffer that some bytes were consumed from the start of the
35 // buffer.
36 void consume(unsigned int bytes);
37
38 void reset();
39
40 T operator[](unsigned int i) const { return data[(start + i) % capacity]; }
41
42 void append(const T *src, unsigned int count);
43 };
44
45 // Class to track software breakpoints that we set.
46 class software_breakpoint_t
47 {
48 public:
49 reg_t address;
50 unsigned int size;
51 uint32_t instruction;
52
53 void insert(mmu_t* mmu);
54 void remove(mmu_t* mmu);
55 };
56
57 class gdbserver_t
58 {
59 public:
60 // Create a new server, listening for connections from localhost on the given
61 // port.
62 gdbserver_t(uint16_t port, sim_t *sim);
63
64 // Process all pending messages from a client.
65 void handle();
66
67 void handle_packet(const std::vector<uint8_t> &packet);
68 void handle_interrupt();
69
70 void handle_breakpoint(const std::vector<uint8_t> &packet);
71 void handle_continue(const std::vector<uint8_t> &packet);
72 void handle_extended(const std::vector<uint8_t> &packet);
73 void handle_general_registers_read(const std::vector<uint8_t> &packet);
74 void handle_halt_reason(const std::vector<uint8_t> &packet);
75 void handle_kill(const std::vector<uint8_t> &packet);
76 void handle_memory_binary_write(const std::vector<uint8_t> &packet);
77 void handle_memory_read(const std::vector<uint8_t> &packet);
78 void handle_query(const std::vector<uint8_t> &packet);
79 void handle_register_read(const std::vector<uint8_t> &packet);
80 void handle_register_write(const std::vector<uint8_t> &packet);
81 void handle_step(const std::vector<uint8_t> &packet);
82
83 bool connected() const { return client_fd > 0; }
84
85 void halt();
86
87 private:
88 sim_t *sim;
89 int socket_fd;
90 int client_fd;
91 circular_buffer_t<uint8_t> recv_buf;
92 circular_buffer_t<uint8_t> send_buf;
93
94 bool expect_ack;
95 bool extended_mode;
96 // Used to track whether we think the target is running. If we think it is
97 // but it isn't, we need to tell gdb about it.
98 bool running;
99 enum {
100 STATE_UNKNOWN,
101 STATE_RUNNING,
102 STATE_HALTING,
103 STATE_HALTED
104 } state;
105
106 std::map <reg_t, software_breakpoint_t> breakpoints;
107
108 // Read pending data from the client.
109 void read();
110 void write();
111 // Accept a new client if there isn't one already connected.
112 void accept();
113 // Process all complete requests in recv_buf.
114 void process_requests();
115 // Add the given message to send_buf.
116 void send(const char* msg);
117 // Hex-encode a 64-bit value, and send it to gcc in target byte order (little
118 // endian).
119 void send(uint64_t value);
120 // Hex-encode a 32-bit value, and send it to gcc in target byte order (little
121 // endian).
122 void send(uint32_t value);
123 void send_packet(const char* data);
124 uint8_t running_checksum;
125 void send_running_checksum();
126
127 // Write value to the index'th word in Debug RAM.
128 void write_debug_ram(unsigned int index, uint32_t value);
129 uint32_t read_debug_ram(unsigned int index);
130 };
131
132 #endif