fe553dc93888d603ec9c9e78740ac921942d9902
[riscv-isa-sim.git] / riscv / gdbserver.cc
1 #include <arpa/inet.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include <algorithm>
11 #include <cassert>
12 #include <cstdio>
13 #include <vector>
14
15 #include "disasm.h"
16 #include "sim.h"
17 #include "gdbserver.h"
18
19 template <typename T>
20 unsigned int circular_buffer_t<T>::size() const
21 {
22 if (end >= start)
23 return end - start;
24 else
25 return end + capacity - start;
26 }
27
28 template <typename T>
29 void circular_buffer_t<T>::consume(unsigned int bytes)
30 {
31 start = (start + bytes) % capacity;
32 }
33
34 template <typename T>
35 unsigned int circular_buffer_t<T>::contiguous_empty_size() const
36 {
37 if (end >= start)
38 if (start == 0)
39 return capacity - end - 1;
40 else
41 return capacity - end;
42 else
43 return start - end - 1;
44 }
45
46 template <typename T>
47 unsigned int circular_buffer_t<T>::contiguous_data_size() const
48 {
49 if (end >= start)
50 return end - start;
51 else
52 return capacity - start;
53 }
54
55 template <typename T>
56 void circular_buffer_t<T>::data_added(unsigned int bytes)
57 {
58 end += bytes;
59 assert(end <= capacity);
60 if (end == capacity)
61 end = 0;
62 }
63
64 template <typename T>
65 void circular_buffer_t<T>::reset()
66 {
67 start = 0;
68 end = 0;
69 }
70
71 template <typename T>
72 void circular_buffer_t<T>::append(const T *src, unsigned int count)
73 {
74 unsigned int copy = std::min(count, contiguous_empty_size());
75 memcpy(contiguous_empty(), src, copy * sizeof(T));
76 data_added(copy);
77 count -= copy;
78 if (count > 0) {
79 assert(count < contiguous_empty_size());
80 memcpy(contiguous_empty(), src, count * sizeof(T));
81 data_added(count);
82 }
83 }
84
85 // Code inspired by/copied from OpenOCD server/server.c.
86
87 gdbserver_t::gdbserver_t(uint16_t port, sim_t *sim) :
88 sim(sim),
89 client_fd(0),
90 recv_buf(64 * 1024), send_buf(64 * 1024)
91 {
92 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
93 if (socket_fd == -1) {
94 fprintf(stderr, "error creating socket: %s\n", strerror(errno));
95 abort();
96 }
97
98 int so_reuseaddr_option = 1;
99 setsockopt(socket_fd,
100 SOL_SOCKET,
101 SO_REUSEADDR,
102 (void *)&so_reuseaddr_option,
103 sizeof(int));
104
105 int oldopts = fcntl(socket_fd, F_GETFL, 0);
106 fcntl(socket_fd, F_SETFL, oldopts | O_NONBLOCK);
107
108 struct sockaddr_in sin;
109 memset(&sin, 0, sizeof(sin));
110 sin.sin_family = AF_INET;
111 sin.sin_addr.s_addr = INADDR_ANY;
112 sin.sin_port = htons(port);
113
114 if (bind(socket_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
115 fprintf(stderr, "couldn't bind to socket: %s\n", strerror(errno));
116 abort();
117 }
118
119 /* These setsockopt()s must happen before the listen() */
120 int window_size = 128 * 1024;
121 setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF,
122 (char *)&window_size, sizeof(window_size));
123 setsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF,
124 (char *)&window_size, sizeof(window_size));
125
126 if (listen(socket_fd, 1) == -1) {
127 fprintf(stderr, "couldn't listen on socket: %s\n", strerror(errno));
128 abort();
129 }
130 }
131
132 void gdbserver_t::accept()
133 {
134 struct sockaddr client_addr;
135 socklen_t address_size = sizeof(client_addr);
136 client_fd = ::accept(socket_fd, &client_addr, &address_size);
137 if (client_fd == -1) {
138 if (errno == EAGAIN) {
139 // We'll try again in the next call.
140 } else {
141 fprintf(stderr, "failed to accept on socket: %s (%d)\n", strerror(errno), errno);
142 abort();
143 }
144 } else {
145 int oldopts = fcntl(client_fd, F_GETFL, 0);
146 fcntl(client_fd, F_SETFL, oldopts | O_NONBLOCK);
147 expect_ack = false;
148 }
149 }
150
151 void gdbserver_t::read()
152 {
153 // Reading from a non-blocking socket still blocks if there is no data
154 // available.
155
156 size_t count = recv_buf.contiguous_empty_size();
157 assert(count > 0);
158 ssize_t bytes = ::read(client_fd, recv_buf.contiguous_empty(), count);
159 if (bytes == -1) {
160 if (errno == EAGAIN) {
161 // We'll try again the next call.
162 } else {
163 fprintf(stderr, "failed to read on socket: %s (%d)\n", strerror(errno), errno);
164 abort();
165 }
166 } else if (bytes == 0) {
167 // The remote disconnected.
168 client_fd = 0;
169 recv_buf.reset();
170 send_buf.reset();
171 } else {
172 recv_buf.data_added(bytes);
173 }
174 }
175
176 void gdbserver_t::write()
177 {
178 if (send_buf.empty())
179 return;
180
181 while (!send_buf.empty()) {
182 unsigned int count = send_buf.contiguous_data_size();
183 assert(count > 0);
184 ssize_t bytes = ::write(client_fd, send_buf.contiguous_data(), count);
185 if (bytes == -1) {
186 fprintf(stderr, "failed to write to socket: %s (%d)\n", strerror(errno), errno);
187 abort();
188 } else if (bytes == 0) {
189 // Client can't take any more data right now.
190 break;
191 } else {
192 printf("wrote %ld bytes: ", bytes);
193 for (unsigned int i = 0; i < bytes; i++) {
194 printf("%c", send_buf[i]);
195 }
196 printf("\n");
197 send_buf.consume(bytes);
198 }
199 }
200 }
201
202 void print_packet(const std::vector<uint8_t> &packet)
203 {
204 for (uint8_t c : packet) {
205 fprintf(stderr, "%c", c);
206 }
207 fprintf(stderr, "\n");
208 }
209
210 uint8_t compute_checksum(const std::vector<uint8_t> &packet)
211 {
212 uint8_t checksum = 0;
213 for (auto i = packet.begin() + 1; i != packet.end() - 3; i++ ) {
214 checksum += *i;
215 }
216 return checksum;
217 }
218
219 uint8_t character_hex_value(uint8_t character)
220 {
221 if (character >= '0' && character <= '9')
222 return character - '0';
223 if (character >= 'a' && character <= 'f')
224 return 10 + character - 'a';
225 if (character >= 'A' && character <= 'F')
226 return 10 + character - 'A';
227 return 0xff;
228 }
229
230 uint8_t extract_checksum(const std::vector<uint8_t> &packet)
231 {
232 return character_hex_value(*(packet.end() - 1)) +
233 16 * character_hex_value(*(packet.end() - 2));
234 }
235
236 void gdbserver_t::process_requests()
237 {
238 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
239
240 while (!recv_buf.empty()) {
241 std::vector<uint8_t> packet;
242 for (unsigned int i = 0; i < recv_buf.size(); i++) {
243 uint8_t b = recv_buf[i];
244
245 if (packet.empty() && expect_ack && b == '+') {
246 fprintf(stderr, "Received ack\n");
247 recv_buf.consume(1);
248 break;
249 }
250
251 if (b == '$') {
252 // Start of new packet.
253 if (!packet.empty()) {
254 fprintf(stderr, "Received malformed %ld-byte packet from debug client: ", packet.size());
255 print_packet(packet);
256 recv_buf.consume(i);
257 break;
258 }
259 }
260
261 packet.push_back(b);
262
263 // Packets consist of $<packet-data>#<checksum>
264 // where <checksum> is
265 if (packet.size() >= 4 &&
266 packet[packet.size()-3] == '#') {
267 handle_packet(packet);
268 recv_buf.consume(i+1);
269 break;
270 }
271 }
272 // There's a partial packet in the buffer. Wait until we get more data to
273 // process it.
274 if (packet.size())
275 break;
276 }
277 }
278
279 void gdbserver_t::handle_halt_reason(const std::vector<uint8_t> &packet)
280 {
281 send_packet("S00");
282 }
283
284 void gdbserver_t::handle_read_general_registers(const std::vector<uint8_t> &packet)
285 {
286 // Register order that gdb expects is:
287 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
288 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
289 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
290 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
291 // "pc",
292 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
293 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
294 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
295 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
296
297 // Each byte of register data is described by two hex digits. The bytes with
298 // the register are transmitted in target byte order. The size of each
299 // register and their position within the ā€˜gā€™ packet are determined by the
300 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
301 // gdbarch_register_name.
302
303 send("$");
304 running_checksum = 0;
305 processor_t *p = sim->get_core(0);
306 for (int r = 0; r < 32; r++) {
307 send(p->state.XPR[r]);
308 }
309 send_running_checksum();
310 expect_ack = true;
311 }
312
313 uint64_t consume_hex_number(std::vector<uint8_t>::const_iterator &iter,
314 std::vector<uint8_t>::const_iterator end)
315 {
316 uint64_t value = 0;
317
318 while (iter != end) {
319 uint8_t c = *iter;
320 uint64_t c_value = character_hex_value(c);
321 if (c_value > 15)
322 break;
323 iter++;
324 value <<= 4;
325 value += c_value;
326 }
327 return value;
328 }
329
330 void gdbserver_t::handle_read_register(const std::vector<uint8_t> &packet)
331 {
332 // p n
333
334 // Register order that gdb expects is:
335 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
336 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
337 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
338 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
339 // "pc",
340 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
341 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
342 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
343 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
344
345 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
346 unsigned int n = consume_hex_number(iter, packet.end());
347 if (*iter != '#')
348 return send_packet("E16"); // EINVAL
349
350 processor_t *p = sim->get_core(0);
351 send("$");
352 running_checksum = 0;
353 if (n < 32) {
354 send(p->state.XPR[n]);
355 } else if (n == 0x20) {
356 send(p->state.pc);
357 } else {
358 send("E16"); // EINVAL
359 }
360
361 send_running_checksum();
362 expect_ack = true;
363 }
364
365 void gdbserver_t::handle_read_memory(const std::vector<uint8_t> &packet)
366 {
367 // m addr,length
368 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
369 reg_t address = consume_hex_number(iter, packet.end());
370 printf("address=%lx %c\n", address, *iter);
371 if (*iter != ',')
372 return send_packet("E16"); // EINVAL
373 iter++;
374 reg_t length = consume_hex_number(iter, packet.end());
375 printf("length=%lx %c\n", length, *iter);
376 if (*iter != '#')
377 return send_packet("E16"); // EINVAL
378
379 send("$");
380 running_checksum = 0;
381 char buffer[3];
382 processor_t *p = sim->get_core(0);
383 mmu_t* mmu = sim->debug_mmu;
384
385 for (reg_t i = 0; i < length; i++) {
386 sprintf(buffer, "%02x", mmu->load_uint8(address + i));
387 send(buffer);
388 }
389 send_running_checksum();
390 }
391
392 void gdbserver_t::handle_packet(const std::vector<uint8_t> &packet)
393 {
394 if (compute_checksum(packet) != extract_checksum(packet)) {
395 fprintf(stderr, "Received %ld-byte packet with invalid checksum\n", packet.size());
396 fprintf(stderr, "Computed checksum: %x\n", compute_checksum(packet));
397 print_packet(packet);
398 send("-");
399 return;
400 }
401
402 fprintf(stderr, "Received %ld-byte packet from debug client: ", packet.size());
403 print_packet(packet);
404 send("+");
405
406 switch (packet[1]) {
407 case '?':
408 return handle_halt_reason(packet);
409 case 'g':
410 return handle_read_general_registers(packet);
411 case 'm':
412 return handle_read_memory(packet);
413 case 'p':
414 return handle_read_register(packet);
415 }
416
417 // Not supported.
418 send_packet("");
419 }
420
421 void gdbserver_t::handle()
422 {
423 if (client_fd > 0) {
424 this->read();
425 this->write();
426
427 } else {
428 this->accept();
429 }
430
431 this->process_requests();
432 }
433
434 void gdbserver_t::send(const char* msg)
435 {
436 unsigned int length = strlen(msg);
437 for (const char *c = msg; *c; c++)
438 running_checksum += *c;
439 send_buf.append((const uint8_t *) msg, length);
440 }
441
442 void gdbserver_t::send(uint64_t value)
443 {
444 char buffer[3];
445 for (unsigned int i = 0; i < 8; i++) {
446 sprintf(buffer, "%02x", value & 0xff);
447 send(buffer);
448 value >>= 8;
449 }
450 }
451
452 void gdbserver_t::send(uint32_t value)
453 {
454 char buffer[3];
455 for (unsigned int i = 0; i < 4; i++) {
456 sprintf(buffer, "%02x", value & 0xff);
457 send(buffer);
458 value >>= 8;
459 }
460 }
461
462 void gdbserver_t::send_packet(const char* data)
463 {
464 send("$");
465 running_checksum = 0;
466 send(data);
467 send_running_checksum();
468 expect_ack = true;
469 }
470
471 void gdbserver_t::send_running_checksum()
472 {
473 char checksum_string[4];
474 sprintf(checksum_string, "#%02x", running_checksum);
475 send(checksum_string);
476 }