ea8b7d7e22d8aa4903f8bb14094759c8d9c96342
6 #include <sys/socket.h>
17 #include "gdbserver.h"
21 unsigned int circular_buffer_t
<T
>::size() const
26 return end
+ capacity
- start
;
30 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
32 start
= (start
+ bytes
) % capacity
;
36 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
40 return capacity
- end
- 1;
42 return capacity
- end
;
44 return start
- end
- 1;
48 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
53 return capacity
- start
;
57 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
60 assert(end
<= capacity
);
66 void circular_buffer_t
<T
>::reset()
73 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
75 unsigned int copy
= std::min(count
, contiguous_empty_size());
76 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
80 assert(count
< contiguous_empty_size());
81 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
86 // Code inspired by/copied from OpenOCD server/server.c.
88 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
91 recv_buf(64 * 1024), send_buf(64 * 1024)
93 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
94 if (socket_fd
== -1) {
95 fprintf(stderr
, "error creating socket: %s\n", strerror(errno
));
99 int so_reuseaddr_option
= 1;
100 setsockopt(socket_fd
,
103 (void *)&so_reuseaddr_option
,
106 int oldopts
= fcntl(socket_fd
, F_GETFL
, 0);
107 fcntl(socket_fd
, F_SETFL
, oldopts
| O_NONBLOCK
);
109 struct sockaddr_in sin
;
110 memset(&sin
, 0, sizeof(sin
));
111 sin
.sin_family
= AF_INET
;
112 sin
.sin_addr
.s_addr
= INADDR_ANY
;
113 sin
.sin_port
= htons(port
);
115 if (bind(socket_fd
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
116 fprintf(stderr
, "couldn't bind to socket: %s\n", strerror(errno
));
120 /* These setsockopt()s must happen before the listen() */
121 int window_size
= 128 * 1024;
122 setsockopt(socket_fd
, SOL_SOCKET
, SO_SNDBUF
,
123 (char *)&window_size
, sizeof(window_size
));
124 setsockopt(socket_fd
, SOL_SOCKET
, SO_RCVBUF
,
125 (char *)&window_size
, sizeof(window_size
));
127 if (listen(socket_fd
, 1) == -1) {
128 fprintf(stderr
, "couldn't listen on socket: %s\n", strerror(errno
));
133 void gdbserver_t::accept()
135 struct sockaddr client_addr
;
136 socklen_t address_size
= sizeof(client_addr
);
137 client_fd
= ::accept(socket_fd
, &client_addr
, &address_size
);
138 if (client_fd
== -1) {
139 if (errno
== EAGAIN
) {
140 // We'll try again in the next call.
142 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
), errno
);
146 int oldopts
= fcntl(client_fd
, F_GETFL
, 0);
147 fcntl(client_fd
, F_SETFL
, oldopts
| O_NONBLOCK
);
149 extended_mode
= false;
151 // gdb wants the core to be halted when it attaches.
152 processor_t
*p
= sim
->get_core(0);
157 void gdbserver_t::read()
159 // Reading from a non-blocking socket still blocks if there is no data
162 size_t count
= recv_buf
.contiguous_empty_size();
164 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
166 if (errno
== EAGAIN
) {
167 // We'll try again the next call.
169 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
172 } else if (bytes
== 0) {
173 // The remote disconnected.
175 processor_t
*p
= sim
->get_core(0);
176 p
->set_halted(false);
180 recv_buf
.data_added(bytes
);
184 void gdbserver_t::write()
186 if (send_buf
.empty())
189 while (!send_buf
.empty()) {
190 unsigned int count
= send_buf
.contiguous_data_size();
192 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
194 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
196 } else if (bytes
== 0) {
197 // Client can't take any more data right now.
200 printf("wrote %ld bytes: ", bytes
);
201 for (unsigned int i
= 0; i
< bytes
; i
++) {
202 printf("%c", send_buf
[i
]);
205 send_buf
.consume(bytes
);
210 void print_packet(const std::vector
<uint8_t> &packet
)
212 for (uint8_t c
: packet
) {
213 if (c
>= ' ' and c
<= '~')
214 fprintf(stderr
, "%c", c
);
216 fprintf(stderr
, "\\x%x", c
);
218 fprintf(stderr
, "\n");
221 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
223 uint8_t checksum
= 0;
224 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
230 uint8_t character_hex_value(uint8_t character
)
232 if (character
>= '0' && character
<= '9')
233 return character
- '0';
234 if (character
>= 'a' && character
<= 'f')
235 return 10 + character
- 'a';
236 if (character
>= 'A' && character
<= 'F')
237 return 10 + character
- 'A';
241 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
243 return character_hex_value(*(packet
.end() - 1)) +
244 16 * character_hex_value(*(packet
.end() - 2));
247 void gdbserver_t::process_requests()
249 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
251 while (!recv_buf
.empty()) {
252 std::vector
<uint8_t> packet
;
253 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
254 uint8_t b
= recv_buf
[i
];
256 if (packet
.empty() && expect_ack
&& b
== '+') {
261 if (packet
.empty() && b
== 3) {
262 fprintf(stderr
, "Received interrupt\n");
269 // Start of new packet.
270 if (!packet
.empty()) {
271 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ", packet
.size());
272 print_packet(packet
);
280 // Packets consist of $<packet-data>#<checksum>
281 // where <checksum> is
282 if (packet
.size() >= 4 &&
283 packet
[packet
.size()-3] == '#') {
284 handle_packet(packet
);
285 recv_buf
.consume(i
+1);
289 // There's a partial packet in the buffer. Wait until we get more data to
297 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
302 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
304 // Register order that gdb expects is:
305 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
306 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
307 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
308 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
310 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
311 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
312 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
313 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
315 // Each byte of register data is described by two hex digits. The bytes with
316 // the register are transmitted in target byte order. The size of each
317 // register and their position within the ‘g’ packet are determined by the
318 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
319 // gdbarch_register_name.
322 running_checksum
= 0;
323 processor_t
*p
= sim
->get_core(0);
324 for (int r
= 0; r
< 32; r
++) {
325 send(p
->state
.XPR
[r
]);
327 send_running_checksum();
331 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
332 std::vector
<uint8_t>::const_iterator end
)
336 while (iter
!= end
) {
338 uint64_t c_value
= character_hex_value(c
);
348 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
352 // Register order that gdb expects is:
353 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
354 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
355 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
356 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
358 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
359 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
360 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
361 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
363 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
364 unsigned int n
= consume_hex_number(iter
, packet
.end());
366 return send_packet("E16"); // EINVAL
368 processor_t
*p
= sim
->get_core(0);
370 running_checksum
= 0;
372 send(p
->state
.XPR
[n
]);
373 } else if (n
== 0x20) {
376 send("E16"); // EINVAL
379 send_running_checksum();
383 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
386 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
387 reg_t address
= consume_hex_number(iter
, packet
.end());
389 return send_packet("E16"); // EINVAL
391 reg_t length
= consume_hex_number(iter
, packet
.end());
393 return send_packet("E16"); // EINVAL
396 running_checksum
= 0;
398 processor_t
*p
= sim
->get_core(0);
399 mmu_t
* mmu
= sim
->debug_mmu
;
401 for (reg_t i
= 0; i
< length
; i
++) {
402 sprintf(buffer
, "%02x", mmu
->load_uint8(address
+ i
));
405 send_running_checksum();
408 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
410 // X addr,length:XX...
411 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
412 reg_t address
= consume_hex_number(iter
, packet
.end());
414 return send_packet("E16"); // EINVAL
416 reg_t length
= consume_hex_number(iter
, packet
.end());
418 return send_packet("E16"); // EINVAL
421 processor_t
*p
= sim
->get_core(0);
422 mmu_t
* mmu
= sim
->debug_mmu
;
423 for (unsigned int i
= 0; i
< length
; i
++) {
424 if (iter
== packet
.end()) {
425 return send_packet("E16"); // EINVAL
427 mmu
->store_uint8(address
+ i
, *iter
);
431 return send_packet("E4b"); // EOVERFLOW
436 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
439 processor_t
*p
= sim
->get_core(0);
440 if (packet
[2] != '#') {
441 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
442 p
->state
.pc
= consume_hex_number(iter
, packet
.end());
444 return send_packet("E16"); // EINVAL
447 p
->set_halted(false);
450 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
453 // The exact effect of this packet is not specified.
454 // Looks like OpenOCD disconnects?
458 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
460 // Enable extended mode. In extended mode, the remote server is made
461 // persistent. The ‘R’ packet is used to restart the program being debugged.
463 extended_mode
= true;
466 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
468 if (compute_checksum(packet
) != extract_checksum(packet
)) {
469 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
470 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
471 print_packet(packet
);
476 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
477 print_packet(packet
);
482 return handle_extended(packet
);
484 return handle_halt_reason(packet
);
486 return handle_general_registers_read(packet
);
488 return handle_kill(packet
);
490 return handle_memory_read(packet
);
492 // return handle_memory_write(packet);
494 return handle_memory_binary_write(packet
);
496 return handle_register_read(packet
);
498 return handle_continue(packet
);
502 fprintf(stderr
, "** Unsupported packet: ");
503 print_packet(packet
);
507 void gdbserver_t::handle_interrupt()
509 processor_t
*p
= sim
->get_core(0);
511 send_packet("S02"); // Pretend program received SIGINT.
514 void gdbserver_t::handle()
524 this->process_requests();
527 void gdbserver_t::send(const char* msg
)
529 unsigned int length
= strlen(msg
);
530 for (const char *c
= msg
; *c
; c
++)
531 running_checksum
+= *c
;
532 send_buf
.append((const uint8_t *) msg
, length
);
535 void gdbserver_t::send(uint64_t value
)
538 for (unsigned int i
= 0; i
< 8; i
++) {
539 sprintf(buffer
, "%02x", (int) (value
& 0xff));
545 void gdbserver_t::send(uint32_t value
)
548 for (unsigned int i
= 0; i
< 4; i
++) {
549 sprintf(buffer
, "%02x", (int) (value
& 0xff));
555 void gdbserver_t::send_packet(const char* data
)
558 running_checksum
= 0;
560 send_running_checksum();
564 void gdbserver_t::send_running_checksum()
566 char checksum_string
[4];
567 sprintf(checksum_string
, "#%02x", running_checksum
);
568 send(checksum_string
);