6 #include <sys/socket.h>
17 #include "gdbserver.h"
20 #define C_EBREAK 0x9002
21 #define EBREAK 0x00100073
24 unsigned int circular_buffer_t
<T
>::size() const
29 return end
+ capacity
- start
;
33 void circular_buffer_t
<T
>::consume(unsigned int bytes
)
35 start
= (start
+ bytes
) % capacity
;
39 unsigned int circular_buffer_t
<T
>::contiguous_empty_size() const
43 return capacity
- end
- 1;
45 return capacity
- end
;
47 return start
- end
- 1;
51 unsigned int circular_buffer_t
<T
>::contiguous_data_size() const
56 return capacity
- start
;
60 void circular_buffer_t
<T
>::data_added(unsigned int bytes
)
63 assert(end
<= capacity
);
69 void circular_buffer_t
<T
>::reset()
76 void circular_buffer_t
<T
>::append(const T
*src
, unsigned int count
)
78 unsigned int copy
= std::min(count
, contiguous_empty_size());
79 memcpy(contiguous_empty(), src
, copy
* sizeof(T
));
83 assert(count
< contiguous_empty_size());
84 memcpy(contiguous_empty(), src
, count
* sizeof(T
));
89 gdbserver_t::gdbserver_t(uint16_t port
, sim_t
*sim
) :
92 recv_buf(64 * 1024), send_buf(64 * 1024)
94 // TODO: listen on socket
95 socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0);
96 if (socket_fd
== -1) {
97 fprintf(stderr
, "failed to make socket: %s (%d)\n", strerror(errno
), errno
);
101 fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
);
103 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
104 sizeof(int)) == -1) {
105 fprintf(stderr
, "failed setsockopt: %s (%d)\n", strerror(errno
), errno
);
109 struct sockaddr_in addr
;
110 memset(&addr
, 0, sizeof(addr
));
111 addr
.sin_family
= AF_INET
;
112 addr
.sin_addr
.s_addr
= INADDR_ANY
;
113 addr
.sin_port
= htons(port
);
115 if (bind(socket_fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) == -1) {
116 fprintf(stderr
, "failed to bind socket: %s (%d)\n", strerror(errno
), errno
);
120 if (listen(socket_fd
, 1) == -1) {
121 fprintf(stderr
, "failed to listen on socket: %s (%d)\n", strerror(errno
), errno
);
126 void gdbserver_t::accept()
128 client_fd
= ::accept(socket_fd
, NULL
, NULL
);
129 if (client_fd
== -1) {
130 if (errno
== EAGAIN
) {
131 // No client waiting to connect right now.
133 fprintf(stderr
, "failed to accept on socket: %s (%d)\n", strerror(errno
),
138 fcntl(client_fd
, F_SETFL
, O_NONBLOCK
);
141 extended_mode
= false;
143 // gdb wants the core to be halted when it attaches.
144 processor_t
*p
= sim
->get_core(0);
145 p
->set_halted(true, HR_ATTACHED
);
149 void gdbserver_t::read()
151 // Reading from a non-blocking socket still blocks if there is no data
154 size_t count
= recv_buf
.contiguous_empty_size();
156 ssize_t bytes
= ::read(client_fd
, recv_buf
.contiguous_empty(), count
);
158 if (errno
== EAGAIN
) {
159 // We'll try again the next call.
161 fprintf(stderr
, "failed to read on socket: %s (%d)\n", strerror(errno
), errno
);
164 } else if (bytes
== 0) {
165 // The remote disconnected.
167 processor_t
*p
= sim
->get_core(0);
168 p
->set_halted(false, HR_NONE
);
172 recv_buf
.data_added(bytes
);
176 void gdbserver_t::write()
178 if (send_buf
.empty())
181 while (!send_buf
.empty()) {
182 unsigned int count
= send_buf
.contiguous_data_size();
184 ssize_t bytes
= ::write(client_fd
, send_buf
.contiguous_data(), count
);
186 fprintf(stderr
, "failed to write to socket: %s (%d)\n", strerror(errno
), errno
);
188 } else if (bytes
== 0) {
189 // Client can't take any more data right now.
192 printf("wrote %ld bytes: ", bytes
);
193 for (unsigned int i
= 0; i
< bytes
; i
++) {
194 printf("%c", send_buf
[i
]);
197 send_buf
.consume(bytes
);
202 void print_packet(const std::vector
<uint8_t> &packet
)
204 for (uint8_t c
: packet
) {
205 if (c
>= ' ' and c
<= '~')
206 fprintf(stderr
, "%c", c
);
208 fprintf(stderr
, "\\x%x", c
);
210 fprintf(stderr
, "\n");
213 uint8_t compute_checksum(const std::vector
<uint8_t> &packet
)
215 uint8_t checksum
= 0;
216 for (auto i
= packet
.begin() + 1; i
!= packet
.end() - 3; i
++ ) {
222 uint8_t character_hex_value(uint8_t character
)
224 if (character
>= '0' && character
<= '9')
225 return character
- '0';
226 if (character
>= 'a' && character
<= 'f')
227 return 10 + character
- 'a';
228 if (character
>= 'A' && character
<= 'F')
229 return 10 + character
- 'A';
233 uint8_t extract_checksum(const std::vector
<uint8_t> &packet
)
235 return character_hex_value(*(packet
.end() - 1)) +
236 16 * character_hex_value(*(packet
.end() - 2));
239 void gdbserver_t::process_requests()
241 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
243 while (!recv_buf
.empty()) {
244 std::vector
<uint8_t> packet
;
245 for (unsigned int i
= 0; i
< recv_buf
.size(); i
++) {
246 uint8_t b
= recv_buf
[i
];
248 if (packet
.empty() && expect_ack
&& b
== '+') {
253 if (packet
.empty() && b
== 3) {
254 fprintf(stderr
, "Received interrupt\n");
261 // Start of new packet.
262 if (!packet
.empty()) {
263 fprintf(stderr
, "Received malformed %ld-byte packet from debug client: ",
265 print_packet(packet
);
273 // Packets consist of $<packet-data>#<checksum>
274 // where <checksum> is
275 if (packet
.size() >= 4 &&
276 packet
[packet
.size()-3] == '#') {
277 handle_packet(packet
);
278 recv_buf
.consume(i
+1);
282 // There's a partial packet in the buffer. Wait until we get more data to
290 void gdbserver_t::handle_halt_reason(const std::vector
<uint8_t> &packet
)
295 void gdbserver_t::handle_general_registers_read(const std::vector
<uint8_t> &packet
)
297 // Register order that gdb expects is:
298 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
299 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
300 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
301 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
303 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
304 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
305 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
306 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
308 // Each byte of register data is described by two hex digits. The bytes with
309 // the register are transmitted in target byte order. The size of each
310 // register and their position within the ‘g’ packet are determined by the
311 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
312 // gdbarch_register_name.
315 running_checksum
= 0;
316 processor_t
*p
= sim
->get_core(0);
317 for (int r
= 0; r
< 32; r
++) {
318 send(p
->state
.XPR
[r
]);
320 send_running_checksum();
324 uint64_t consume_hex_number(std::vector
<uint8_t>::const_iterator
&iter
,
325 std::vector
<uint8_t>::const_iterator end
)
329 while (iter
!= end
) {
331 uint64_t c_value
= character_hex_value(c
);
341 void consume_string(std::string
&str
, std::vector
<uint8_t>::const_iterator
&iter
,
342 std::vector
<uint8_t>::const_iterator end
, uint8_t separator
)
344 while (iter
!= end
&& *iter
!= separator
) {
345 str
.append(1, (char) *iter
);
351 void gdbserver_t::handle_register_read(const std::vector
<uint8_t> &packet
)
355 // Register order that gdb expects is:
356 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
357 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
358 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
359 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
361 // "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
362 // "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
363 // "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
364 // "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
366 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
367 unsigned int n
= consume_hex_number(iter
, packet
.end());
369 return send_packet("E01");
371 processor_t
*p
= sim
->get_core(0);
373 running_checksum
= 0;
375 send(p
->state
.XPR
[n
]);
376 } else if (n
== 0x20) {
382 send_running_checksum();
386 void gdbserver_t::handle_memory_read(const std::vector
<uint8_t> &packet
)
389 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
390 reg_t address
= consume_hex_number(iter
, packet
.end());
392 return send_packet("E10");
394 reg_t length
= consume_hex_number(iter
, packet
.end());
396 return send_packet("E11");
399 running_checksum
= 0;
401 processor_t
*p
= sim
->get_core(0);
402 mmu_t
* mmu
= sim
->debug_mmu
;
404 for (reg_t i
= 0; i
< length
; i
++) {
405 sprintf(buffer
, "%02x", mmu
->load_uint8(address
+ i
));
408 send_running_checksum();
411 void gdbserver_t::handle_memory_binary_write(const std::vector
<uint8_t> &packet
)
413 // X addr,length:XX...
414 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
415 reg_t address
= consume_hex_number(iter
, packet
.end());
417 return send_packet("E20");
419 reg_t length
= consume_hex_number(iter
, packet
.end());
421 return send_packet("E21");
424 processor_t
*p
= sim
->get_core(0);
425 mmu_t
* mmu
= sim
->debug_mmu
;
426 for (unsigned int i
= 0; i
< length
; i
++) {
427 if (iter
== packet
.end()) {
428 return send_packet("E22");
430 mmu
->store_uint8(address
+ i
, *iter
);
434 return send_packet("E4b"); // EOVERFLOW
439 void gdbserver_t::handle_continue(const std::vector
<uint8_t> &packet
)
442 processor_t
*p
= sim
->get_core(0);
443 if (packet
[2] != '#') {
444 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
445 p
->state
.pc
= consume_hex_number(iter
, packet
.end());
447 return send_packet("E30");
450 p
->set_halted(false, HR_NONE
);
454 void gdbserver_t::handle_step(const std::vector
<uint8_t> &packet
)
457 processor_t
*p
= sim
->get_core(0);
458 if (packet
[2] != '#') {
459 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
460 p
->state
.pc
= consume_hex_number(iter
, packet
.end());
462 return send_packet("E40");
465 p
->set_single_step(true);
469 void gdbserver_t::handle_kill(const std::vector
<uint8_t> &packet
)
472 // The exact effect of this packet is not specified.
473 // Looks like OpenOCD disconnects?
477 void gdbserver_t::handle_extended(const std::vector
<uint8_t> &packet
)
479 // Enable extended mode. In extended mode, the remote server is made
480 // persistent. The ‘R’ packet is used to restart the program being debugged.
482 extended_mode
= true;
485 void software_breakpoint_t::insert(mmu_t
* mmu
)
488 instruction
= mmu
->load_uint16(address
);
489 mmu
->store_uint16(address
, C_EBREAK
);
491 instruction
= mmu
->load_uint32(address
);
492 mmu
->store_uint32(address
, EBREAK
);
494 printf(">>> Read %x from %lx\n", instruction
, address
);
497 void software_breakpoint_t::remove(mmu_t
* mmu
)
499 printf(">>> write %x to %lx\n", instruction
, address
);
501 mmu
->store_uint16(address
, instruction
);
503 mmu
->store_uint32(address
, instruction
);
507 void gdbserver_t::handle_breakpoint(const std::vector
<uint8_t> &packet
)
509 // insert: Z type,addr,kind
510 // remove: z type,addr,kind
512 software_breakpoint_t bp
;
513 bool insert
= (packet
[1] == 'Z');
514 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
515 int type
= consume_hex_number(iter
, packet
.end());
517 return send_packet("E50");
519 bp
.address
= consume_hex_number(iter
, packet
.end());
521 return send_packet("E51");
523 bp
.size
= consume_hex_number(iter
, packet
.end());
524 // There may be more options after a ; here, but we don't support that.
526 return send_packet("E52");
528 if (bp
.size
!= 2 && bp
.size
!= 4) {
529 return send_packet("E53");
532 mmu_t
* mmu
= sim
->debug_mmu
;
535 breakpoints
[bp
.address
] = bp
;
538 bp
= breakpoints
[bp
.address
];
540 breakpoints
.erase(bp
.address
);
543 processor_t
*p
= sim
->get_core(0);
544 p
->mmu
->flush_icache();
545 return send_packet("OK");
548 void gdbserver_t::handle_query(const std::vector
<uint8_t> &packet
)
551 std::vector
<uint8_t>::const_iterator iter
= packet
.begin() + 2;
553 consume_string(name
, iter
, packet
.end(), ':');
554 if (iter
!= packet
.end())
556 if (name
== "Supported") {
558 running_checksum
= 0;
559 while (iter
!= packet
.end()) {
561 consume_string(feature
, iter
, packet
.end(), ';');
562 if (iter
!= packet
.end())
564 printf("is %s supported?\n", feature
.c_str());
565 if (feature
== "swbreak+") {
569 return send_running_checksum();
572 printf("Unsupported query %s\n", name
.c_str());
573 return send_packet("");
576 void gdbserver_t::handle_packet(const std::vector
<uint8_t> &packet
)
578 if (compute_checksum(packet
) != extract_checksum(packet
)) {
579 fprintf(stderr
, "Received %ld-byte packet with invalid checksum\n", packet
.size());
580 fprintf(stderr
, "Computed checksum: %x\n", compute_checksum(packet
));
581 print_packet(packet
);
586 fprintf(stderr
, "Received %ld-byte packet from debug client: ", packet
.size());
587 print_packet(packet
);
592 return handle_extended(packet
);
594 return handle_halt_reason(packet
);
596 return handle_general_registers_read(packet
);
598 return handle_kill(packet
);
600 return handle_memory_read(packet
);
602 // return handle_memory_write(packet);
604 return handle_memory_binary_write(packet
);
606 return handle_register_read(packet
);
608 return handle_continue(packet
);
610 return handle_step(packet
);
613 return handle_breakpoint(packet
);
616 return handle_query(packet
);
620 fprintf(stderr
, "** Unsupported packet: ");
621 print_packet(packet
);
625 void gdbserver_t::handle_interrupt()
627 processor_t
*p
= sim
->get_core(0);
628 p
->set_halted(true, HR_INTERRUPT
);
629 send_packet("S02"); // Pretend program received SIGINT.
633 void gdbserver_t::handle()
635 processor_t
*p
= sim
->get_core(0);
636 if (running
&& p
->halted
) {
637 // The core was running, but now it's halted. Better tell gdb.
638 switch (p
->halt_reason
) {
640 fprintf(stderr
, "Internal error. Processor halted without reason.\n");
646 // There's no gdb code for this.
650 send_packet("T05swbreak:;");
654 // TODO: Actually include register values here
666 this->process_requests();
669 void gdbserver_t::send(const char* msg
)
671 unsigned int length
= strlen(msg
);
672 for (const char *c
= msg
; *c
; c
++)
673 running_checksum
+= *c
;
674 send_buf
.append((const uint8_t *) msg
, length
);
677 void gdbserver_t::send(uint64_t value
)
680 for (unsigned int i
= 0; i
< 8; i
++) {
681 sprintf(buffer
, "%02x", (int) (value
& 0xff));
687 void gdbserver_t::send(uint32_t value
)
690 for (unsigned int i
= 0; i
< 4; i
++) {
691 sprintf(buffer
, "%02x", (int) (value
& 0xff));
697 void gdbserver_t::send_packet(const char* data
)
700 running_checksum
= 0;
702 send_running_checksum();
706 void gdbserver_t::send_running_checksum()
708 char checksum_string
[4];
709 sprintf(checksum_string
, "#%02x", running_checksum
);
710 send(checksum_string
);