Correctly read PC on halt.
[riscv-isa-sim.git] / riscv / gdbserver.cc
1 #include <arpa/inet.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/socket.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include <algorithm>
11 #include <cassert>
12 #include <cstdio>
13 #include <vector>
14
15 #include "disasm.h"
16 #include "sim.h"
17 #include "gdbserver.h"
18 #include "mmu.h"
19
20 #define C_EBREAK 0x9002
21 #define EBREAK 0x00100073
22
23 // Functions to generate RISC-V opcodes.
24 // TODO: Does this already exist somewhere?
25
26 // Using regnames.cc as source. The RVG Calling Convention of the 2.0 RISC-V
27 // spec says it should be 2 and 3.
28 #define S0 8
29 #define S1 9
30 static uint32_t bits(uint32_t value, unsigned int hi, unsigned int lo) {
31 return (value >> lo) & ((1 << (hi+1-lo)) - 1);
32 }
33
34 static uint32_t bit(uint32_t value, unsigned int b) {
35 return (value >> b) & 1;
36 }
37
38 static uint32_t jal(unsigned int rd, uint32_t imm) {
39 return (bit(imm, 20) << 31) |
40 (bits(imm, 10, 1) << 21) |
41 (bit(imm, 11) << 20) |
42 (bits(imm, 19, 12) << 12) |
43 (rd << 7) |
44 MATCH_JAL;
45 }
46
47 static uint32_t csrsi(unsigned int csr, uint8_t imm) {
48 return (csr << 20) |
49 (bits(imm, 4, 0) << 15) |
50 MATCH_CSRRSI;
51 }
52
53 static uint32_t csrr(unsigned int rd, unsigned int csr) {
54 return (csr << 20) | (rd << 7) | MATCH_CSRRS;
55 }
56
57 static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset)
58 {
59 return (bits(offset, 11, 5) << 25) |
60 (src << 20) |
61 (base << 15) |
62 (bits(offset, 4, 0) << 7) |
63 MATCH_SW;
64 }
65
66 template <typename T>
67 unsigned int circular_buffer_t<T>::size() const
68 {
69 if (end >= start)
70 return end - start;
71 else
72 return end + capacity - start;
73 }
74
75 template <typename T>
76 void circular_buffer_t<T>::consume(unsigned int bytes)
77 {
78 start = (start + bytes) % capacity;
79 }
80
81 template <typename T>
82 unsigned int circular_buffer_t<T>::contiguous_empty_size() const
83 {
84 if (end >= start)
85 if (start == 0)
86 return capacity - end - 1;
87 else
88 return capacity - end;
89 else
90 return start - end - 1;
91 }
92
93 template <typename T>
94 unsigned int circular_buffer_t<T>::contiguous_data_size() const
95 {
96 if (end >= start)
97 return end - start;
98 else
99 return capacity - start;
100 }
101
102 template <typename T>
103 void circular_buffer_t<T>::data_added(unsigned int bytes)
104 {
105 end += bytes;
106 assert(end <= capacity);
107 if (end == capacity)
108 end = 0;
109 }
110
111 template <typename T>
112 void circular_buffer_t<T>::reset()
113 {
114 start = 0;
115 end = 0;
116 }
117
118 template <typename T>
119 void circular_buffer_t<T>::append(const T *src, unsigned int count)
120 {
121 unsigned int copy = std::min(count, contiguous_empty_size());
122 memcpy(contiguous_empty(), src, copy * sizeof(T));
123 data_added(copy);
124 count -= copy;
125 if (count > 0) {
126 assert(count < contiguous_empty_size());
127 memcpy(contiguous_empty(), src, count * sizeof(T));
128 data_added(count);
129 }
130 }
131
132 gdbserver_t::gdbserver_t(uint16_t port, sim_t *sim) :
133 sim(sim),
134 client_fd(0),
135 recv_buf(64 * 1024), send_buf(64 * 1024)
136 {
137 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
138 if (socket_fd == -1) {
139 fprintf(stderr, "failed to make socket: %s (%d)\n", strerror(errno), errno);
140 abort();
141 }
142
143 fcntl(socket_fd, F_SETFL, O_NONBLOCK);
144 int reuseaddr = 1;
145 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
146 sizeof(int)) == -1) {
147 fprintf(stderr, "failed setsockopt: %s (%d)\n", strerror(errno), errno);
148 abort();
149 }
150
151 struct sockaddr_in addr;
152 memset(&addr, 0, sizeof(addr));
153 addr.sin_family = AF_INET;
154 addr.sin_addr.s_addr = INADDR_ANY;
155 addr.sin_port = htons(port);
156
157 if (bind(socket_fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
158 fprintf(stderr, "failed to bind socket: %s (%d)\n", strerror(errno), errno);
159 abort();
160 }
161
162 if (listen(socket_fd, 1) == -1) {
163 fprintf(stderr, "failed to listen on socket: %s (%d)\n", strerror(errno), errno);
164 abort();
165 }
166 }
167
168 void gdbserver_t::write_debug_ram(unsigned int index, uint32_t value)
169 {
170 sim->debug_module.ram_write32(index, value);
171 }
172
173 uint32_t gdbserver_t::read_debug_ram(unsigned int index)
174 {
175 return sim->debug_module.ram_read32(index);
176 }
177
178 void gdbserver_t::halt()
179 {
180 processor_t *p = sim->get_core(0);
181 write_debug_ram(0, csrsi(DCSR_ADDRESS, DCSR_HALT_OFFSET));
182 write_debug_ram(1, csrr(S0, DPC_ADDRESS));
183 write_debug_ram(2, sw(S0, 0, (uint16_t) DEBUG_RAM_START));
184 write_debug_ram(3, csrr(S0, DCSR_ADDRESS));
185 write_debug_ram(4, sw(S0, 0, (uint16_t) DEBUG_RAM_START + 8));
186 write_debug_ram(5, jal(0, (uint32_t) (DEBUG_ROM_RESUME - (DEBUG_RAM_START + 4*5))));
187 sim->debug_module.set_interrupt(p->id);
188 state = STATE_HALTING;
189 }
190
191 void gdbserver_t::accept()
192 {
193 client_fd = ::accept(socket_fd, NULL, NULL);
194 if (client_fd == -1) {
195 if (errno == EAGAIN) {
196 // No client waiting to connect right now.
197 } else {
198 fprintf(stderr, "failed to accept on socket: %s (%d)\n", strerror(errno),
199 errno);
200 abort();
201 }
202 } else {
203 fcntl(client_fd, F_SETFL, O_NONBLOCK);
204
205 expect_ack = false;
206 extended_mode = false;
207
208 // gdb wants the core to be halted when it attaches.
209 halt();
210 }
211 }
212
213 void gdbserver_t::read()
214 {
215 // Reading from a non-blocking socket still blocks if there is no data
216 // available.
217
218 size_t count = recv_buf.contiguous_empty_size();
219 assert(count > 0);
220 ssize_t bytes = ::read(client_fd, recv_buf.contiguous_empty(), count);
221 if (bytes == -1) {
222 if (errno == EAGAIN) {
223 // We'll try again the next call.
224 } else {
225 fprintf(stderr, "failed to read on socket: %s (%d)\n", strerror(errno), errno);
226 abort();
227 }
228 } else if (bytes == 0) {
229 // The remote disconnected.
230 client_fd = 0;
231 processor_t *p = sim->get_core(0);
232 // TODO p->set_halted(false, HR_NONE);
233 recv_buf.reset();
234 send_buf.reset();
235 } else {
236 recv_buf.data_added(bytes);
237 }
238 }
239
240 void gdbserver_t::write()
241 {
242 if (send_buf.empty())
243 return;
244
245 while (!send_buf.empty()) {
246 unsigned int count = send_buf.contiguous_data_size();
247 assert(count > 0);
248 ssize_t bytes = ::write(client_fd, send_buf.contiguous_data(), count);
249 if (bytes == -1) {
250 fprintf(stderr, "failed to write to socket: %s (%d)\n", strerror(errno), errno);
251 abort();
252 } else if (bytes == 0) {
253 // Client can't take any more data right now.
254 break;
255 } else {
256 fprintf(stderr, "wrote %ld bytes: ", bytes);
257 for (unsigned int i = 0; i < bytes; i++) {
258 fprintf(stderr, "%c", send_buf[i]);
259 }
260 fprintf(stderr, "\n");
261 send_buf.consume(bytes);
262 }
263 }
264 }
265
266 void print_packet(const std::vector<uint8_t> &packet)
267 {
268 for (uint8_t c : packet) {
269 if (c >= ' ' and c <= '~')
270 fprintf(stderr, "%c", c);
271 else
272 fprintf(stderr, "\\x%x", c);
273 }
274 fprintf(stderr, "\n");
275 }
276
277 uint8_t compute_checksum(const std::vector<uint8_t> &packet)
278 {
279 uint8_t checksum = 0;
280 for (auto i = packet.begin() + 1; i != packet.end() - 3; i++ ) {
281 checksum += *i;
282 }
283 return checksum;
284 }
285
286 uint8_t character_hex_value(uint8_t character)
287 {
288 if (character >= '0' && character <= '9')
289 return character - '0';
290 if (character >= 'a' && character <= 'f')
291 return 10 + character - 'a';
292 if (character >= 'A' && character <= 'F')
293 return 10 + character - 'A';
294 return 0xff;
295 }
296
297 uint8_t extract_checksum(const std::vector<uint8_t> &packet)
298 {
299 return character_hex_value(*(packet.end() - 1)) +
300 16 * character_hex_value(*(packet.end() - 2));
301 }
302
303 void gdbserver_t::process_requests()
304 {
305 // See https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
306
307 while (!recv_buf.empty()) {
308 std::vector<uint8_t> packet;
309 for (unsigned int i = 0; i < recv_buf.size(); i++) {
310 uint8_t b = recv_buf[i];
311
312 if (packet.empty() && expect_ack && b == '+') {
313 recv_buf.consume(1);
314 break;
315 }
316
317 if (packet.empty() && b == 3) {
318 fprintf(stderr, "Received interrupt\n");
319 recv_buf.consume(1);
320 handle_interrupt();
321 break;
322 }
323
324 if (b == '$') {
325 // Start of new packet.
326 if (!packet.empty()) {
327 fprintf(stderr, "Received malformed %ld-byte packet from debug client: ",
328 packet.size());
329 print_packet(packet);
330 recv_buf.consume(i);
331 break;
332 }
333 }
334
335 packet.push_back(b);
336
337 // Packets consist of $<packet-data>#<checksum>
338 // where <checksum> is
339 if (packet.size() >= 4 &&
340 packet[packet.size()-3] == '#') {
341 handle_packet(packet);
342 recv_buf.consume(i+1);
343 break;
344 }
345 }
346 // There's a partial packet in the buffer. Wait until we get more data to
347 // process it.
348 if (packet.size()) {
349 break;
350 }
351 }
352 }
353
354 void gdbserver_t::handle_halt_reason(const std::vector<uint8_t> &packet)
355 {
356 send_packet("S00");
357 }
358
359 void gdbserver_t::handle_general_registers_read(const std::vector<uint8_t> &packet)
360 {
361 // Register order that gdb expects is:
362 // "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
363 // "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
364 // "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
365 // "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31",
366
367 // Each byte of register data is described by two hex digits. The bytes with
368 // the register are transmitted in target byte order. The size of each
369 // register and their position within the ‘g’ packet are determined by the
370 // gdb internal gdbarch functions DEPRECATED_REGISTER_RAW_SIZE and
371 // gdbarch_register_name.
372
373 send("$");
374 running_checksum = 0;
375 processor_t *p = sim->get_core(0);
376 for (int r = 0; r < 32; r++) {
377 send(p->state.XPR[r]);
378 }
379 send_running_checksum();
380 expect_ack = true;
381 }
382
383 // First byte is the most-significant one.
384 // Eg. "08675309" becomes 0x08675309.
385 uint64_t consume_hex_number(std::vector<uint8_t>::const_iterator &iter,
386 std::vector<uint8_t>::const_iterator end)
387 {
388 uint64_t value = 0;
389
390 while (iter != end) {
391 uint8_t c = *iter;
392 uint64_t c_value = character_hex_value(c);
393 if (c_value > 15)
394 break;
395 iter++;
396 value <<= 4;
397 value += c_value;
398 }
399 return value;
400 }
401
402 // First byte is the least-significant one.
403 // Eg. "08675309" becomes 0x09536708
404 uint64_t consume_hex_number_le(std::vector<uint8_t>::const_iterator &iter,
405 std::vector<uint8_t>::const_iterator end)
406 {
407 uint64_t value = 0;
408 unsigned int shift = 4;
409
410 while (iter != end) {
411 uint8_t c = *iter;
412 uint64_t c_value = character_hex_value(c);
413 if (c_value > 15)
414 break;
415 iter++;
416 value |= c_value << shift;
417 if ((shift % 8) == 0)
418 shift += 12;
419 else
420 shift -= 4;
421 }
422 return value;
423 }
424
425 void consume_string(std::string &str, std::vector<uint8_t>::const_iterator &iter,
426 std::vector<uint8_t>::const_iterator end, uint8_t separator)
427 {
428 while (iter != end && *iter != separator) {
429 str.append(1, (char) *iter);
430 iter++;
431 }
432 }
433
434 // gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
435 // its source tree. We must interpret the numbers the same here.
436 enum {
437 REG_XPR0 = 0,
438 REG_XPR31 = 31,
439 REG_PC = 32,
440 REG_FPR0 = 33,
441 REG_FPR31 = 64,
442 REG_CSR0 = 65,
443 REG_CSR4095 = 4160,
444 REG_END = 4161
445 };
446
447 void gdbserver_t::handle_register_read(const std::vector<uint8_t> &packet)
448 {
449 // p n
450
451 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
452 unsigned int n = consume_hex_number(iter, packet.end());
453 if (*iter != '#')
454 return send_packet("E01");
455
456 processor_t *p = sim->get_core(0);
457 send("$");
458 running_checksum = 0;
459
460 if (n >= REG_XPR0 && n <= REG_XPR31) {
461 send(p->state.XPR[n - REG_XPR0]);
462 } else if (n == REG_PC) {
463 send(p->state.pc);
464 } else if (n >= REG_FPR0 && n <= REG_FPR31) {
465 send(p->state.FPR[n - REG_FPR0]);
466 } else if (n >= REG_CSR0 && n <= REG_CSR4095) {
467 try {
468 send(p->get_csr(n - REG_CSR0));
469 } catch(trap_t& t) {
470 // It would be nicer to return an error here, but if you do that then gdb
471 // exits out of 'info registers all' as soon as it encounters a register
472 // that can't be read.
473 send((reg_t) 0);
474 }
475 } else {
476 return send_packet("E02");
477 }
478
479 send_running_checksum();
480 expect_ack = true;
481 }
482
483 void gdbserver_t::handle_register_write(const std::vector<uint8_t> &packet)
484 {
485 // P n...=r...
486
487 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
488 unsigned int n = consume_hex_number(iter, packet.end());
489 if (*iter != '=')
490 return send_packet("E05");
491 iter++;
492
493 reg_t value = consume_hex_number_le(iter, packet.end());
494 if (*iter != '#')
495 return send_packet("E06");
496
497 processor_t *p = sim->get_core(0);
498
499 if (n >= REG_XPR0 && n <= REG_XPR31) {
500 p->state.XPR.write(n - REG_XPR0, value);
501 } else if (n == REG_PC) {
502 p->state.pc = value;
503 } else if (n >= REG_FPR0 && n <= REG_FPR31) {
504 p->state.FPR.write(n - REG_FPR0, value);
505 } else if (n >= REG_CSR0 && n <= REG_CSR4095) {
506 try {
507 p->set_csr(n - REG_CSR0, value);
508 } catch(trap_t& t) {
509 return send_packet("EFF");
510 }
511 } else {
512 return send_packet("E07");
513 }
514
515 return send_packet("OK");
516 }
517
518 void gdbserver_t::handle_memory_read(const std::vector<uint8_t> &packet)
519 {
520 // m addr,length
521 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
522 reg_t address = consume_hex_number(iter, packet.end());
523 if (*iter != ',')
524 return send_packet("E10");
525 iter++;
526 reg_t length = consume_hex_number(iter, packet.end());
527 if (*iter != '#')
528 return send_packet("E11");
529
530 send("$");
531 running_checksum = 0;
532 char buffer[3];
533 processor_t *p = sim->get_core(0);
534 mmu_t* mmu = sim->debug_mmu;
535
536 for (reg_t i = 0; i < length; i++) {
537 sprintf(buffer, "%02x", mmu->load_uint8(address + i));
538 send(buffer);
539 }
540 send_running_checksum();
541 }
542
543 void gdbserver_t::handle_memory_binary_write(const std::vector<uint8_t> &packet)
544 {
545 // X addr,length:XX...
546 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
547 reg_t address = consume_hex_number(iter, packet.end());
548 if (*iter != ',')
549 return send_packet("E20");
550 iter++;
551 reg_t length = consume_hex_number(iter, packet.end());
552 if (*iter != ':')
553 return send_packet("E21");
554 iter++;
555
556 processor_t *p = sim->get_core(0);
557 mmu_t* mmu = sim->debug_mmu;
558 for (unsigned int i = 0; i < length; i++) {
559 if (iter == packet.end()) {
560 return send_packet("E22");
561 }
562 mmu->store_uint8(address + i, *iter);
563 iter++;
564 }
565 if (*iter != '#')
566 return send_packet("E4b"); // EOVERFLOW
567
568 send_packet("OK");
569 }
570
571 void gdbserver_t::handle_continue(const std::vector<uint8_t> &packet)
572 {
573 // c [addr]
574 processor_t *p = sim->get_core(0);
575 if (packet[2] != '#') {
576 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
577 p->state.pc = consume_hex_number(iter, packet.end());
578 if (*iter != '#')
579 return send_packet("E30");
580 }
581
582 // TODO p->set_halted(false, HR_NONE);
583 // TODO running = true;
584 }
585
586 void gdbserver_t::handle_step(const std::vector<uint8_t> &packet)
587 {
588 // s [addr]
589 processor_t *p = sim->get_core(0);
590 if (packet[2] != '#') {
591 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
592 p->state.pc = consume_hex_number(iter, packet.end());
593 if (*iter != '#')
594 return send_packet("E40");
595 }
596
597 // TODO: p->set_single_step(true);
598 // TODO running = true;
599 }
600
601 void gdbserver_t::handle_kill(const std::vector<uint8_t> &packet)
602 {
603 // k
604 // The exact effect of this packet is not specified.
605 // Looks like OpenOCD disconnects?
606 // TODO
607 }
608
609 void gdbserver_t::handle_extended(const std::vector<uint8_t> &packet)
610 {
611 // Enable extended mode. In extended mode, the remote server is made
612 // persistent. The ‘R’ packet is used to restart the program being debugged.
613 send_packet("OK");
614 extended_mode = true;
615 }
616
617 void software_breakpoint_t::insert(mmu_t* mmu)
618 {
619 if (size == 2) {
620 instruction = mmu->load_uint16(address);
621 mmu->store_uint16(address, C_EBREAK);
622 } else {
623 instruction = mmu->load_uint32(address);
624 mmu->store_uint32(address, EBREAK);
625 }
626 fprintf(stderr, ">>> Read %x from %lx\n", instruction, address);
627 }
628
629 void software_breakpoint_t::remove(mmu_t* mmu)
630 {
631 fprintf(stderr, ">>> write %x to %lx\n", instruction, address);
632 if (size == 2) {
633 mmu->store_uint16(address, instruction);
634 } else {
635 mmu->store_uint32(address, instruction);
636 }
637 }
638
639 void gdbserver_t::handle_breakpoint(const std::vector<uint8_t> &packet)
640 {
641 // insert: Z type,addr,kind
642 // remove: z type,addr,kind
643
644 software_breakpoint_t bp;
645 bool insert = (packet[1] == 'Z');
646 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
647 int type = consume_hex_number(iter, packet.end());
648 if (*iter != ',')
649 return send_packet("E50");
650 iter++;
651 bp.address = consume_hex_number(iter, packet.end());
652 if (*iter != ',')
653 return send_packet("E51");
654 iter++;
655 bp.size = consume_hex_number(iter, packet.end());
656 // There may be more options after a ; here, but we don't support that.
657 if (*iter != '#')
658 return send_packet("E52");
659
660 if (bp.size != 2 && bp.size != 4) {
661 return send_packet("E53");
662 }
663
664 processor_t *p = sim->get_core(0);
665 mmu_t* mmu = p->mmu;
666 if (insert) {
667 bp.insert(mmu);
668 breakpoints[bp.address] = bp;
669
670 } else {
671 bp = breakpoints[bp.address];
672 bp.remove(mmu);
673 breakpoints.erase(bp.address);
674 }
675 mmu->flush_icache();
676 sim->debug_mmu->flush_icache();
677 return send_packet("OK");
678 }
679
680 void gdbserver_t::handle_query(const std::vector<uint8_t> &packet)
681 {
682 std::string name;
683 std::vector<uint8_t>::const_iterator iter = packet.begin() + 2;
684
685 consume_string(name, iter, packet.end(), ':');
686 if (iter != packet.end())
687 iter++;
688 if (name == "Supported") {
689 send("$");
690 running_checksum = 0;
691 while (iter != packet.end()) {
692 std::string feature;
693 consume_string(feature, iter, packet.end(), ';');
694 if (iter != packet.end())
695 iter++;
696 if (feature == "swbreak+") {
697 send("swbreak+;");
698 }
699 }
700 return send_running_checksum();
701 }
702
703 fprintf(stderr, "Unsupported query %s\n", name.c_str());
704 return send_packet("");
705 }
706
707 void gdbserver_t::handle_packet(const std::vector<uint8_t> &packet)
708 {
709 if (compute_checksum(packet) != extract_checksum(packet)) {
710 fprintf(stderr, "Received %ld-byte packet with invalid checksum\n", packet.size());
711 fprintf(stderr, "Computed checksum: %x\n", compute_checksum(packet));
712 print_packet(packet);
713 send("-");
714 return;
715 }
716
717 fprintf(stderr, "Received %ld-byte packet from debug client: ", packet.size());
718 print_packet(packet);
719 send("+");
720
721 switch (packet[1]) {
722 case '!':
723 return handle_extended(packet);
724 case '?':
725 return handle_halt_reason(packet);
726 case 'g':
727 return handle_general_registers_read(packet);
728 case 'k':
729 return handle_kill(packet);
730 case 'm':
731 return handle_memory_read(packet);
732 // case 'M':
733 // return handle_memory_write(packet);
734 case 'X':
735 return handle_memory_binary_write(packet);
736 case 'p':
737 return handle_register_read(packet);
738 case 'P':
739 return handle_register_write(packet);
740 case 'c':
741 return handle_continue(packet);
742 case 's':
743 return handle_step(packet);
744 case 'z':
745 case 'Z':
746 return handle_breakpoint(packet);
747 case 'q':
748 case 'Q':
749 return handle_query(packet);
750 }
751
752 // Not supported.
753 fprintf(stderr, "** Unsupported packet: ");
754 print_packet(packet);
755 send_packet("");
756 }
757
758 void gdbserver_t::handle_interrupt()
759 {
760 processor_t *p = sim->get_core(0);
761 // TODO p->set_halted(true, HR_INTERRUPT);
762 send_packet("S02"); // Pretend program received SIGINT.
763 // TODO running = false;
764 }
765
766 void gdbserver_t::handle()
767 {
768 if (client_fd > 0) {
769 processor_t *p = sim->get_core(0);
770
771 if (state == STATE_HALTING && sim->debug_module.get_interrupt(p->id) == 0) {
772 // gdb requested a halt and now it's done.
773 send_packet("T05");
774 fprintf(stderr, "DPC: 0x%x\n", read_debug_ram(0));
775 fprintf(stderr, "DCSR: 0x%x\n", read_debug_ram(2));
776 state = STATE_HALTED;
777 }
778
779 /* TODO
780 if (running && p->halted) {
781 // The core was running, but now it's halted. Better tell gdb.
782 switch (p->halt_reason) {
783 case HR_NONE:
784 fprintf(stderr, "Internal error. Processor halted without reason.\n");
785 abort();
786 case HR_STEPPED:
787 case HR_INTERRUPT:
788 case HR_CMDLINE:
789 case HR_ATTACHED:
790 // There's no gdb code for this.
791 send_packet("T05");
792 break;
793 case HR_SWBP:
794 send_packet("T05swbreak:;");
795 break;
796 }
797 send_packet("T00");
798 // TODO: Actually include register values here
799 running = false;
800 }
801 */
802
803 this->read();
804 this->write();
805
806 } else {
807 this->accept();
808 }
809
810 this->process_requests();
811 }
812
813 void gdbserver_t::send(const char* msg)
814 {
815 unsigned int length = strlen(msg);
816 for (const char *c = msg; *c; c++)
817 running_checksum += *c;
818 send_buf.append((const uint8_t *) msg, length);
819 }
820
821 void gdbserver_t::send(uint64_t value)
822 {
823 char buffer[3];
824 for (unsigned int i = 0; i < 8; i++) {
825 sprintf(buffer, "%02x", (int) (value & 0xff));
826 send(buffer);
827 value >>= 8;
828 }
829 }
830
831 void gdbserver_t::send(uint32_t value)
832 {
833 char buffer[3];
834 for (unsigned int i = 0; i < 4; i++) {
835 sprintf(buffer, "%02x", (int) (value & 0xff));
836 send(buffer);
837 value >>= 8;
838 }
839 }
840
841 void gdbserver_t::send_packet(const char* data)
842 {
843 send("$");
844 running_checksum = 0;
845 send(data);
846 send_running_checksum();
847 expect_ack = true;
848 }
849
850 void gdbserver_t::send_running_checksum()
851 {
852 char checksum_string[4];
853 sprintf(checksum_string, "#%02x", running_checksum);
854 send(checksum_string);
855 }