6 #include <sys/socket.h>
17 #include "gdbserver.h"
20 unsigned int circular_buffer_t
<T
>::size() const
25 return end
+ capacity
- start
;
29 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
31 start
= (start
+ bytes
) % capacity
;
35 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
39 return capacity
- end
- 1;
41 return capacity
- end
;
43 return start
- end
- 1;
47 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
52 return capacity
- start
;
56 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
59 assert(end
<= capacity
);
65 void circular_buffer_t
<T
>::reset()
72 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
74 unsigned int copy
= std::min(count
, contiguous_empty_size());
75 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
79 assert(count
< contiguous_empty_size());
80 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
85 // Code inspired by/copied from OpenOCD server/server.c.
87 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
90 recv_buf(64 * 1024), send_buf(64 * 1024)
92 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
93 if (socket_fd
== -1) {
94 fprintf(stderr
, "error creating socket: %s\n", strerror(errno
));
98 int so_reuseaddr_option
= 1;
102 (void *)&so_reuseaddr_option
,
105 int oldopts
= fcntl(socket_fd
, F_GETFL
, 0);
106 fcntl(socket_fd
, F_SETFL
, oldopts
| O_NONBLOCK
);
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
);
114 if (bind(socket_fd
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
115 fprintf(stderr
, "couldn't bind to socket: %s\n", strerror(errno
));
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
));
126 if (listen(socket_fd
, 1) == -1) {
127 fprintf(stderr
, "couldn't listen on socket: %s\n", strerror(errno
));
132 void gdbserver_t::accept()
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.
141 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
), errno
);
145 int oldopts
= fcntl(client_fd
, F_GETFL
, 0);
146 fcntl(client_fd
, F_SETFL
, oldopts
| O_NONBLOCK
);
149 // gdb wants the core to be halted when it attaches.
150 processor_t
*p
= sim
->get_core(0);
155 void gdbserver_t::read()
157 // Reading from a non-blocking socket still blocks if there is no data
160 size_t count
= recv_buf
.contiguous_empty_size();
162 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
164 if (errno
== EAGAIN
) {
165 // We'll try again the next call.
167 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
170 } else if (bytes
== 0) {
171 // The remote disconnected.
176 recv_buf
.data_added(bytes
);
177 printf("Read %d bytes.\n", bytes
);
181 void gdbserver_t::write()
183 if (send_buf
.empty())
186 while (!send_buf
.empty()) {
187 unsigned int count
= send_buf
.contiguous_data_size();
189 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
191 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
193 } else if (bytes
== 0) {
194 // Client can't take any more data right now.
197 printf("wrote %ld bytes: ", bytes
);
198 for (unsigned int i
= 0; i
< bytes
; i
++) {
199 printf("%c", send_buf
[i
]);
202 send_buf
.consume(bytes
);
207 void print_packet(const std::vector
<uint8_t> &packet
)
209 for (uint8_t c
: packet
) {
210 if (c
>= ' ' and c
<= '~')
211 fprintf(stderr
, "%c", c
);
213 fprintf(stderr
, "\\x%x", c
);
215 fprintf(stderr
, "\n");
218 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
220 uint8_t checksum
= 0;
221 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
227 uint8_t character_hex_value(uint8_t character
)
229 if (character
>= '0' && character
<= '9')
230 return character
- '0';
231 if (character
>= 'a' && character
<= 'f')
232 return 10 + character
- 'a';
233 if (character
>= 'A' && character
<= 'F')
234 return 10 + character
- 'A';
238 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
240 return character_hex_value(*(packet
.end() - 1)) +
241 16 * character_hex_value(*(packet
.end() - 2));
244 void gdbserver_t::process_requests()
246 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
248 while (!recv_buf
.empty()) {
249 std::vector
<uint8_t> packet
;
250 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
251 uint8_t b
= recv_buf
[i
];
253 if (packet
.empty() && expect_ack
&& b
== '+') {
254 fprintf(stderr
, "Received ack\n");
259 if (packet
.empty() && b
== 3) {
260 fprintf(stderr
, "Received interrupt\n");
267 // Start of new packet.
268 if (!packet
.empty()) {
269 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ", packet
.size());
270 print_packet(packet
);
278 // Packets consist of $<packet-data>#<checksum>
279 // where <checksum> is
280 if (packet
.size() >= 4 &&
281 packet
[packet
.size()-3] == '#') {
282 handle_packet(packet
);
283 recv_buf
.consume(i
+1);
287 // There's a partial packet in the buffer. Wait until we get more data to
290 fprintf(stderr
, "Partial packet: ");
291 print_packet(packet
);
297 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
302 void gdbserver_t::handle_read_general_registers(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_read_register(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_read_memory(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());
388 printf("address=%lx %c\n", address
, *iter
);
390 return send_packet("E16"); // EINVAL
392 reg_t length
= consume_hex_number(iter
, packet
.end());
393 printf("length=%lx %c\n", length
, *iter
);
395 return send_packet("E16"); // EINVAL
398 running_checksum
= 0;
400 processor_t
*p
= sim
->get_core(0);
401 mmu_t
* mmu
= sim
->debug_mmu
;
403 for (reg_t i
= 0; i
< length
; i
++) {
404 sprintf(buffer
, "%02x", mmu
->load_uint8(address
+ i
));
407 send_running_checksum();
410 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
412 if (compute_checksum(packet
) != extract_checksum(packet
)) {
413 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
414 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
415 print_packet(packet
);
420 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
421 print_packet(packet
);
426 return handle_halt_reason(packet
);
428 return handle_read_general_registers(packet
);
430 return handle_read_memory(packet
);
432 return handle_read_register(packet
);
434 return handle_continue(packet
);
441 void gdbserver_t::handle_interrupt()
443 processor_t
*p
= sim
->get_core(0);
445 send_packet("S02"); // Pretend program received SIGINT.
448 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
451 processor_t
*p
= sim
->get_core(0);
452 if (packet
[2] != '#') {
453 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
454 p
->state
.pc
= consume_hex_number(iter
, packet
.end());
456 return send_packet("E16"); // EINVAL
459 p
->set_halted(false);
462 void gdbserver_t::handle()
472 this->process_requests();
475 void gdbserver_t::send(const char* msg
)
477 unsigned int length
= strlen(msg
);
478 for (const char *c
= msg
; *c
; c
++)
479 running_checksum
+= *c
;
480 send_buf
.append((const uint8_t *) msg
, length
);
483 void gdbserver_t::send(uint64_t value
)
486 for (unsigned int i
= 0; i
< 8; i
++) {
487 sprintf(buffer
, "%02x", value
& 0xff);
493 void gdbserver_t::send(uint32_t value
)
496 for (unsigned int i
= 0; i
< 4; i
++) {
497 sprintf(buffer
, "%02x", value
& 0xff);
503 void gdbserver_t::send_packet(const char* data
)
506 running_checksum
= 0;
508 send_running_checksum();
512 void gdbserver_t::send_running_checksum()
514 char checksum_string
[4];
515 sprintf(checksum_string
, "#%02x", running_checksum
);
516 send(checksum_string
);