d188f9239afa2dd2954b88c64bd45372ed27ae5d
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
);
148 extended_mode
= false;
150 // gdb wants the core to be halted when it attaches.
151 processor_t
*p
= sim
->get_core(0);
156 void gdbserver_t::read()
158 // Reading from a non-blocking socket still blocks if there is no data
161 size_t count
= recv_buf
.contiguous_empty_size();
163 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
165 if (errno
== EAGAIN
) {
166 // We'll try again the next call.
168 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
171 } else if (bytes
== 0) {
172 // The remote disconnected.
174 processor_t
*p
= sim
->get_core(0);
175 p
->set_halted(false);
179 recv_buf
.data_added(bytes
);
183 void gdbserver_t::write()
185 if (send_buf
.empty())
188 while (!send_buf
.empty()) {
189 unsigned int count
= send_buf
.contiguous_data_size();
191 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
193 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
195 } else if (bytes
== 0) {
196 // Client can't take any more data right now.
199 printf("wrote %ld bytes: ", bytes
);
200 for (unsigned int i
= 0; i
< bytes
; i
++) {
201 printf("%c", send_buf
[i
]);
204 send_buf
.consume(bytes
);
209 void print_packet(const std::vector
<uint8_t> &packet
)
211 for (uint8_t c
: packet
) {
212 if (c
>= ' ' and c
<= '~')
213 fprintf(stderr
, "%c", c
);
215 fprintf(stderr
, "\\x%x", c
);
217 fprintf(stderr
, "\n");
220 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
222 uint8_t checksum
= 0;
223 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
229 uint8_t character_hex_value(uint8_t character
)
231 if (character
>= '0' && character
<= '9')
232 return character
- '0';
233 if (character
>= 'a' && character
<= 'f')
234 return 10 + character
- 'a';
235 if (character
>= 'A' && character
<= 'F')
236 return 10 + character
- 'A';
240 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
242 return character_hex_value(*(packet
.end() - 1)) +
243 16 * character_hex_value(*(packet
.end() - 2));
246 void gdbserver_t::process_requests()
248 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
250 while (!recv_buf
.empty()) {
251 std::vector
<uint8_t> packet
;
252 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
253 uint8_t b
= recv_buf
[i
];
255 if (packet
.empty() && expect_ack
&& b
== '+') {
260 if (packet
.empty() && b
== 3) {
261 fprintf(stderr
, "Received interrupt\n");
268 // Start of new packet.
269 if (!packet
.empty()) {
270 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ", packet
.size());
271 print_packet(packet
);
279 // Packets consist of $<packet-data>#<checksum>
280 // where <checksum> is
281 if (packet
.size() >= 4 &&
282 packet
[packet
.size()-3] == '#') {
283 handle_packet(packet
);
284 recv_buf
.consume(i
+1);
288 // There's a partial packet in the buffer. Wait until we get more data to
296 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
301 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
303 // Register order that gdb expects is:
304 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
305 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
306 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
307 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
309 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
310 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
311 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
312 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
314 // Each byte of register data is described by two hex digits. The bytes with
315 // the register are transmitted in target byte order. The size of each
316 // register and their position within the ‘g’ packet are determined by the
317 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
318 // gdbarch_register_name.
321 running_checksum
= 0;
322 processor_t
*p
= sim
->get_core(0);
323 for (int r
= 0; r
< 32; r
++) {
324 send(p
->state
.XPR
[r
]);
326 send_running_checksum();
330 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
331 std::vector
<uint8_t>::const_iterator end
)
335 while (iter
!= end
) {
337 uint64_t c_value
= character_hex_value(c
);
347 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
351 // Register order that gdb expects is:
352 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
353 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
354 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
355 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
357 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
358 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
359 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
360 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
362 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
363 unsigned int n
= consume_hex_number(iter
, packet
.end());
365 return send_packet("E16"); // EINVAL
367 processor_t
*p
= sim
->get_core(0);
369 running_checksum
= 0;
371 send(p
->state
.XPR
[n
]);
372 } else if (n
== 0x20) {
375 send("E16"); // EINVAL
378 send_running_checksum();
382 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
385 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
386 reg_t address
= consume_hex_number(iter
, packet
.end());
388 return send_packet("E16"); // EINVAL
390 reg_t length
= consume_hex_number(iter
, packet
.end());
392 return send_packet("E16"); // EINVAL
395 running_checksum
= 0;
397 processor_t
*p
= sim
->get_core(0);
398 mmu_t
* mmu
= sim
->debug_mmu
;
400 for (reg_t i
= 0; i
< length
; i
++) {
401 sprintf(buffer
, "%02x", mmu
->load_uint8(address
+ i
));
404 send_running_checksum();
407 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
409 // X addr,length:XX...
410 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
411 reg_t address
= consume_hex_number(iter
, packet
.end());
413 return send_packet("E16"); // EINVAL
415 reg_t length
= consume_hex_number(iter
, packet
.end());
417 return send_packet("E16"); // EINVAL
420 processor_t
*p
= sim
->get_core(0);
421 mmu_t
* mmu
= sim
->debug_mmu
;
422 for (unsigned int i
= 0; i
< length
; i
++) {
423 if (iter
== packet
.end()) {
424 return send_packet("E16"); // EINVAL
426 mmu
->store_uint8(address
+ i
, *iter
);
430 return send_packet("E4b"); // EOVERFLOW
435 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
438 processor_t
*p
= sim
->get_core(0);
439 if (packet
[2] != '#') {
440 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
441 p
->state
.pc
= consume_hex_number(iter
, packet
.end());
443 return send_packet("E16"); // EINVAL
446 p
->set_halted(false);
449 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
452 // The exact effect of this packet is not specified.
453 // Looks like OpenOCD disconnects?
457 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
459 // Enable extended mode. In extended mode, the remote server is made
460 // persistent. The ‘R’ packet is used to restart the program being debugged.
462 extended_mode
= true;
465 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
467 if (compute_checksum(packet
) != extract_checksum(packet
)) {
468 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
469 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
470 print_packet(packet
);
475 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
476 print_packet(packet
);
481 return handle_extended(packet
);
483 return handle_halt_reason(packet
);
485 return handle_general_registers_read(packet
);
487 return handle_kill(packet
);
489 return handle_memory_read(packet
);
491 // return handle_memory_write(packet);
493 return handle_memory_binary_write(packet
);
495 return handle_register_read(packet
);
497 return handle_continue(packet
);
501 fprintf(stderr
, "** Unsupported packet: ");
502 print_packet(packet
);
506 void gdbserver_t::handle_interrupt()
508 processor_t
*p
= sim
->get_core(0);
510 send_packet("S02"); // Pretend program received SIGINT.
513 void gdbserver_t::handle()
523 this->process_requests();
526 void gdbserver_t::send(const char* msg
)
528 unsigned int length
= strlen(msg
);
529 for (const char *c
= msg
; *c
; c
++)
530 running_checksum
+= *c
;
531 send_buf
.append((const uint8_t *) msg
, length
);
534 void gdbserver_t::send(uint64_t value
)
537 for (unsigned int i
= 0; i
< 8; i
++) {
538 sprintf(buffer
, "%02x", (int) (value
& 0xff));
544 void gdbserver_t::send(uint32_t value
)
547 for (unsigned int i
= 0; i
< 4; i
++) {
548 sprintf(buffer
, "%02x", (int) (value
& 0xff));
554 void gdbserver_t::send_packet(const char* data
)
557 running_checksum
= 0;
559 send_running_checksum();
563 void gdbserver_t::send_running_checksum()
565 char checksum_string
[4];
566 sprintf(checksum_string
, "#%02x", running_checksum
);
567 send(checksum_string
);