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
);
151 void gdbserver_t::read()
153 // Reading from a non-blocking socket still blocks if there is no data
156 size_t count
= recv_buf
.contiguous_empty_size();
158 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
160 if (errno
== EAGAIN
) {
161 // We'll try again the next call.
163 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
166 } else if (bytes
== 0) {
167 // The remote disconnected.
172 printf("read %ld bytes\n", bytes
);
173 recv_buf
.data_added(bytes
);
177 void gdbserver_t::write()
179 if (send_buf
.empty())
182 while (!send_buf
.empty()) {
183 unsigned int count
= send_buf
.contiguous_data_size();
185 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
187 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
189 } else if (bytes
== 0) {
190 // Client can't take any more data right now.
193 printf("wrote %ld bytes:\n", bytes
);
194 for (unsigned int i
= 0; i
< bytes
; i
++) {
195 printf("%c", send_buf
[i
]);
198 send_buf
.consume(bytes
);
203 void print_packet(const std::vector
<uint8_t> &packet
)
205 for (uint8_t c
: packet
) {
206 fprintf(stderr
, "%c", c
);
208 fprintf(stderr
, "\n");
211 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
213 uint8_t checksum
= 0;
214 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
220 uint8_t character_hex_value(uint8_t character
)
222 if (character
>= '0' && character
<= '9')
223 return character
- '0';
224 if (character
>= 'a' && character
<= 'f')
225 return 10 + character
- 'a';
226 if (character
>= 'A' && character
<= 'F')
227 return 10 + character
- 'A';
231 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
233 return character_hex_value(*(packet
.end() - 1)) +
234 16 * character_hex_value(*(packet
.end() - 2));
237 void gdbserver_t::process_requests()
239 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
241 while (!recv_buf
.empty()) {
242 std::vector
<uint8_t> packet
;
243 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
244 uint8_t b
= recv_buf
[i
];
246 if (packet
.empty() && expect_ack
&& b
== '+') {
247 fprintf(stderr
, "Received ack\n");
253 // Start of new packet.
254 if (!packet
.empty()) {
255 fprintf(stderr
, "Received malformed %ld-byte packet from debug client\n", packet
.size());
256 print_packet(packet
);
264 // Packets consist of $<packet-data>#<checksum>
265 // where <checksum> is
266 if (packet
.size() >= 4 &&
267 packet
[packet
.size()-3] == '#') {
268 handle_packet(packet
);
269 recv_buf
.consume(i
+1);
273 // There's a partial packet in the buffer. Wait until we get more data to
280 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
285 void gdbserver_t::handle_read_general_registers(const std::vector
<uint8_t> &packet
)
287 // Register order that gdb expects is:
288 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
289 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
290 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
291 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
293 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
294 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
295 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
296 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
299 running_checksum
= 0;
301 processor_t
*p
= sim
->get_core(0);
302 for (int r
= 0; r
< 32; r
++) {
303 sprintf(buffer
, "%08lx", p
->state
.XPR
[r
]);
306 send_running_checksum();
310 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
311 std::vector
<uint8_t>::const_iterator end
)
315 while (iter
!= end
) {
317 uint64_t c_value
= character_hex_value(c
);
327 void gdbserver_t::handle_read_memory(const std::vector
<uint8_t> &packet
)
330 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
331 reg_t address
= consume_hex_number(iter
, packet
.end());
332 printf("address=%lx %c\n", address
, *iter
);
334 return send_packet("E16"); // EINVAL
336 reg_t length
= consume_hex_number(iter
, packet
.end());
337 printf("length=%lx %c\n", length
, *iter
);
339 return send_packet("E16"); // EINVAL
342 running_checksum
= 0;
344 processor_t
*p
= sim
->get_core(0);
345 mmu_t
* mmu
= sim
->debug_mmu
;
347 for (reg_t i
= 0; i
< length
; i
++) {
348 sprintf(buffer
, "%02x", mmu
->load_uint8(address
+ i
));
351 send_running_checksum();
354 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
356 if (compute_checksum(packet
) != extract_checksum(packet
)) {
357 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
358 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
359 print_packet(packet
);
364 fprintf(stderr
, "Received %ld-byte packet from debug client\n", packet
.size());
365 print_packet(packet
);
370 return handle_halt_reason(packet
);
372 return handle_read_general_registers(packet
);
374 return handle_read_memory(packet
);
381 void gdbserver_t::handle()
391 this->process_requests();
394 void gdbserver_t::send(const char* msg
)
396 unsigned int length
= strlen(msg
);
397 for (const char *c
= msg
; *c
; c
++)
398 running_checksum
+= *c
;
399 send_buf
.append((const uint8_t *) msg
, length
);
402 void gdbserver_t::send_packet(const char* data
)
405 running_checksum
= 0;
407 send_running_checksum();
411 void gdbserver_t::send_running_checksum()
413 char checksum_string
[4];
414 sprintf(checksum_string
, "#%02x", running_checksum
);
415 send(checksum_string
);