1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
26 #include "gdb_server.h"
30 #include "binarybuffer.h"
31 #include "breakpoints.h"
33 #include "target_request.h"
42 #define _DEBUG_GDB_IO_
45 static unsigned short gdb_port;
55 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
57 int gdb_last_signal(target_t *target)
59 switch (target->debug_reason)
61 case DBG_REASON_DBGRQ:
62 return 0x2; /* SIGINT */
63 case DBG_REASON_BREAKPOINT:
64 case DBG_REASON_WATCHPOINT:
65 case DBG_REASON_WPTANDBKPT:
66 return 0x05; /* SIGTRAP */
67 case DBG_REASON_SINGLESTEP:
68 return 0x05; /* SIGTRAP */
69 case DBG_REASON_NOTHALTED:
70 return 0x0; /* no signal... shouldn't happen */
72 ERROR("BUG: undefined debug reason");
77 int gdb_get_char(connection_t *connection, int* next_char)
79 gdb_connection_t *gdb_con = connection->priv;
82 if (gdb_con->buf_cnt-- > 0)
84 *next_char = *(gdb_con->buf_p++);
85 if (gdb_con->buf_cnt > 0)
86 connection->input_pending = 1;
88 connection->input_pending = 0;
91 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
97 while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
99 if (gdb_con->buf_cnt == 0)
100 return ERROR_SERVER_REMOTE_CLOSED;
103 errno = WSAGetLastError();
110 case WSAECONNABORTED:
111 return ERROR_SERVER_REMOTE_CLOSED;
113 ERROR("read: %d", errno);
123 return ERROR_SERVER_REMOTE_CLOSED;
125 return ERROR_SERVER_REMOTE_CLOSED;
127 ERROR("read: %s", strerror(errno));
133 debug_buffer = malloc(gdb_con->buf_cnt + 1);
134 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
135 debug_buffer[gdb_con->buf_cnt] = 0;
136 DEBUG("received '%s'", debug_buffer);
139 gdb_con->buf_p = gdb_con->buffer;
141 *next_char = *(gdb_con->buf_p++);
142 if (gdb_con->buf_cnt > 0)
143 connection->input_pending = 1;
145 connection->input_pending = 0;
146 #ifdef _DEBUG_GDB_IO_
147 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
153 int gdb_putback_char(connection_t *connection, int last_char)
155 gdb_connection_t *gdb_con = connection->priv;
157 if (gdb_con->buf_p > gdb_con->buffer)
159 *(--gdb_con->buf_p) = last_char;
164 ERROR("BUG: couldn't put character back");
170 int gdb_put_packet(connection_t *connection, char *buffer, int len)
173 unsigned char my_checksum = 0;
178 gdb_connection_t *gdb_con = connection->priv;
180 for (i = 0; i < len; i++)
181 my_checksum += buffer[i];
185 debug_buffer = malloc(len + 1);
186 memcpy(debug_buffer, buffer, len);
187 debug_buffer[len] = 0;
188 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
191 write_socket(connection->fd, "$", 1);
193 write_socket(connection->fd, buffer, len);
194 write_socket(connection->fd, "#", 1);
196 snprintf(checksum, 3, "%2.2x", my_checksum);
198 write_socket(connection->fd, checksum, 2);
200 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
205 else if (reply == '-')
206 WARNING("negative reply, retrying");
207 else if (reply == 0x3)
210 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
214 else if (reply == '-')
215 WARNING("negative reply, retrying");
218 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
219 return ERROR_SERVER_REMOTE_CLOSED;
224 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
225 return ERROR_SERVER_REMOTE_CLOSED;
232 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
238 unsigned char my_checksum = 0;
239 gdb_connection_t *gdb_con = connection->priv;
245 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
248 DEBUG("character: '%c'", character);
255 WARNING("acknowledgment received, but no packet pending");
258 WARNING("negative acknowledgment, but no packet pending");
265 WARNING("ignoring character 0x%x", character);
268 } while (character != '$');
274 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
277 if (character == '#') break;
279 if (character == '}')
281 /* data transmitted in binary mode (X packet)
282 * uses 0x7d as escape character */
283 my_checksum += character & 0xff;
284 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
286 my_checksum += character & 0xff;
287 buffer[count++] = (character ^ 0x20) & 0xff;
291 my_checksum += character & 0xff;
292 buffer[count++] = character & 0xff;
297 ERROR("packet buffer too small");
298 return ERROR_GDB_BUFFER_TOO_SMALL;
304 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
306 checksum[0] = character;
307 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
309 checksum[1] = character;
312 if (my_checksum == strtoul(checksum, NULL, 16))
314 write_socket(connection->fd, "+", 1);
318 WARNING("checksum error, requesting retransmission");
319 write_socket(connection->fd, "-", 1);
325 int gdb_output(struct command_context_s *context, char* line)
327 connection_t *connection = context->output_handler_priv;
331 bin_size = strlen(line);
333 hex_buffer = malloc(bin_size*2 + 4);
336 for (i=0; i<bin_size; i++)
337 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
338 hex_buffer[bin_size*2+1] = '0';
339 hex_buffer[bin_size*2+2] = 'a';
340 hex_buffer[bin_size*2+3] = 0x0;
342 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
348 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
350 connection_t *connection = priv;
351 gdb_connection_t *gdb_connection = connection->priv;
357 case TARGET_EVENT_HALTED:
358 if (gdb_connection->frontend_state == TARGET_RUNNING)
360 if (gdb_connection->ctrl_c)
363 gdb_connection->ctrl_c = 0;
367 signal = gdb_last_signal(target);
370 snprintf(sig_reply, 4, "T%2.2x", signal);
371 gdb_put_packet(connection, sig_reply, 3);
372 gdb_connection->frontend_state = TARGET_HALTED;
375 case TARGET_EVENT_RESUMED:
376 if (gdb_connection->frontend_state == TARGET_HALTED)
378 gdb_connection->frontend_state = TARGET_RUNNING;
388 int gdb_new_connection(connection_t *connection)
390 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
391 gdb_service_t *gdb_service = connection->service->priv;
395 connection->priv = gdb_connection;
397 /* initialize gdb connection information */
398 gdb_connection->buf_p = gdb_connection->buffer;
399 gdb_connection->buf_cnt = 0;
400 gdb_connection->ctrl_c = 0;
401 gdb_connection->frontend_state = TARGET_HALTED;
402 gdb_connection->vflash_image = NULL;
404 /* output goes through gdb connection */
405 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
407 /* register callback to be informed about target events */
408 target_register_event_callback(gdb_target_callback_event_handler, connection);
410 /* a gdb session just attached, put the target in halt mode */
411 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
412 (retval != ERROR_TARGET_ALREADY_HALTED))
414 ERROR("error when trying to halt target");
418 while (gdb_service->target->state != TARGET_HALTED)
420 gdb_service->target->type->poll(gdb_service->target);
423 /* remove the initial ACK from the incoming buffer */
424 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
427 if (initial_ack != '+')
428 gdb_putback_char(connection, initial_ack);
433 int gdb_connection_closed(connection_t *connection)
435 gdb_service_t *gdb_service = connection->service->priv;
436 gdb_connection_t *gdb_connection = connection->priv;
438 /* see if an image built with vFlash commands is left */
439 if (gdb_connection->vflash_image)
441 image_close(gdb_connection->vflash_image);
442 free(gdb_connection->vflash_image);
443 gdb_connection->vflash_image = NULL;
446 /* if this connection registered a debug-message receiver delete it */
447 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
449 if (connection->priv)
450 free(connection->priv);
452 ERROR("BUG: connection->priv == NULL");
454 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
459 void gdb_send_error(connection_t *connection, u8 the_error)
462 snprintf(err, 4, "E%2.2X", the_error );
463 gdb_put_packet(connection, err, 3);
466 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
471 signal = gdb_last_signal(target);
473 snprintf(sig_reply, 4, "S%2.2x", signal);
474 gdb_put_packet(connection, sig_reply, 3);
479 void gdb_str_to_target(target_t *target, char *str, char *tstr)
481 int str_len = strlen(str);
486 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str);
490 if (target->endianness == TARGET_LITTLE_ENDIAN)
492 for (i = 0; i < str_len; i+=2)
494 tstr[str_len - i - 1] = str[i + 1];
495 tstr[str_len - i - 2] = str[i];
500 for (i = 0; i < str_len; i++)
507 void gdb_target_to_str(target_t *target, char *tstr, char *str)
509 int str_len = strlen(tstr);
514 ERROR("BUG: gdb value with uneven number of characters encountered");
518 if (target->endianness == TARGET_LITTLE_ENDIAN)
520 for (i = 0; i < str_len; i+=2)
522 str[str_len - i - 1] = tstr[i + 1];
523 str[str_len - i - 2] = tstr[i];
528 for (i = 0; i < str_len; i++)
535 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
540 int reg_packet_size = 0;
547 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
551 case ERROR_TARGET_NOT_HALTED:
552 ERROR("gdb requested registers but we're not halted, dropping connection");
553 return ERROR_SERVER_REMOTE_CLOSED;
555 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
556 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
561 for (i = 0; i < reg_list_size; i++)
563 reg_packet_size += reg_list[i]->size;
566 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
567 reg_packet_p = reg_packet;
569 for (i = 0; i < reg_list_size; i++)
571 char *hex_buf = buf_to_str(reg_list[i]->value, reg_list[i]->size, 16);
572 DEBUG("hex_buf: %s", hex_buf);
573 gdb_str_to_target(target, hex_buf, reg_packet_p);
574 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
578 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
579 DEBUG("reg_packet: %s", reg_packet_p);
582 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
590 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
600 /* skip command character */
606 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
607 return ERROR_SERVER_REMOTE_CLOSED;
610 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
614 case ERROR_TARGET_NOT_HALTED:
615 ERROR("gdb tried to registers but we're not halted, dropping connection");
616 return ERROR_SERVER_REMOTE_CLOSED;
618 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
619 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
625 for (i = 0; i < reg_list_size; i++)
629 reg_arch_type_t *arch_type;
631 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
632 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
633 gdb_target_to_str(target, packet_p, hex_buf);
635 /* convert hex-string to binary buffer */
636 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
637 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
639 /* get register arch_type, and call set method */
640 arch_type = register_get_arch_type(reg_list[i]->arch_type);
641 if (arch_type == NULL)
643 ERROR("BUG: encountered unregistered arch type");
646 arch_type->set(reg_list[i], bin_buf);
648 /* advance packet pointer */
649 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
655 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
658 gdb_put_packet(connection, "OK", 2);
663 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
666 int reg_num = strtoul(packet + 1, NULL, 16);
674 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
678 case ERROR_TARGET_NOT_HALTED:
679 ERROR("gdb requested registers but we're not halted, dropping connection");
680 return ERROR_SERVER_REMOTE_CLOSED;
682 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
683 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
688 if (reg_list_size <= reg_num)
690 ERROR("gdb requested a non-existing register");
694 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
696 hex_buf = buf_to_str(reg_list[reg_num]->value, reg_list[reg_num]->size, 16);
698 gdb_str_to_target(target, hex_buf, reg_packet);
700 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
709 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
714 int reg_num = strtoul(packet + 1, &separator, 16);
718 reg_arch_type_t *arch_type;
722 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
726 case ERROR_TARGET_NOT_HALTED:
727 ERROR("gdb tried to set a register but we're not halted, dropping connection");
728 return ERROR_SERVER_REMOTE_CLOSED;
730 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
731 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
736 if (reg_list_size < reg_num)
738 ERROR("gdb requested a non-existing register");
739 return ERROR_SERVER_REMOTE_CLOSED;
742 if (*separator != '=')
744 ERROR("GDB 'set register packet', but no '=' following the register number");
745 return ERROR_SERVER_REMOTE_CLOSED;
748 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
749 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
750 gdb_target_to_str(target, separator + 1, hex_buf);
752 /* convert hex-string to binary buffer */
753 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
754 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
756 /* get register arch_type, and call set method */
757 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
758 if (arch_type == NULL)
760 ERROR("BUG: encountered unregistered arch type");
763 arch_type->set(reg_list[reg_num], bin_buf);
765 gdb_put_packet(connection, "OK", 2);
774 int gdb_memory_packet_error(connection_t *connection, int retval)
778 case ERROR_TARGET_NOT_HALTED:
779 ERROR("gdb tried to read memory but we're not halted, dropping connection");
780 return ERROR_SERVER_REMOTE_CLOSED;
782 case ERROR_TARGET_DATA_ABORT:
783 gdb_send_error(connection, EIO);
785 case ERROR_TARGET_TRANSLATION_FAULT:
786 gdb_send_error(connection, EFAULT);
788 case ERROR_TARGET_UNALIGNED_ACCESS:
789 gdb_send_error(connection, EFAULT);
792 ERROR("BUG: unexpected error %i", retval);
799 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
811 /* skip command character */
814 addr = strtoul(packet, &separator, 16);
816 if (*separator != ',')
818 ERROR("incomplete read memory packet received, dropping connection");
819 return ERROR_SERVER_REMOTE_CLOSED;
822 len = strtoul(separator+1, NULL, 16);
824 buffer = malloc(len);
826 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
832 retval = target->type->read_memory(target, addr, 4, 1, buffer);
834 retval = target->type->read_memory(target, addr, 1, len, buffer);
838 retval = target->type->read_memory(target, addr, 2, 1, buffer);
840 retval = target->type->read_memory(target, addr, 1, len, buffer);
843 if (((addr % 4) == 0) && ((len % 4) == 0))
844 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
846 retval = target->type->read_memory(target, addr, 1, len, buffer);
849 if (retval == ERROR_OK)
851 hex_buffer = malloc(len * 2 + 1);
853 for (i=0; i<len; i++)
854 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
856 gdb_put_packet(connection, hex_buffer, len * 2);
862 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
871 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
882 /* skip command character */
885 addr = strtoul(packet, &separator, 16);
887 if (*separator != ',')
889 ERROR("incomplete write memory packet received, dropping connection");
890 return ERROR_SERVER_REMOTE_CLOSED;
893 len = strtoul(separator+1, &separator, 16);
895 if (*(separator++) != ':')
897 ERROR("incomplete write memory packet received, dropping connection");
898 return ERROR_SERVER_REMOTE_CLOSED;
901 buffer = malloc(len);
903 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
905 for (i=0; i<len; i++)
908 sscanf(separator + 2*i, "%2x", &tmp);
915 /* handle sized writes */
918 retval = target->type->write_memory(target, addr, 4, 1, buffer);
920 retval = target->type->write_memory(target, addr, 1, len, buffer);
924 retval = target->type->write_memory(target, addr, 2, 1, buffer);
926 retval = target->type->write_memory(target, addr, 1, len, buffer);
930 retval = target->type->write_memory(target, addr, 1, len, buffer);
932 /* handle bulk writes */
934 retval = target_write_buffer(target, addr, len, buffer);
938 if (retval == ERROR_OK)
940 gdb_put_packet(connection, "OK", 2);
944 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
953 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
962 /* skip command character */
965 addr = strtoul(packet, &separator, 16);
967 if (*separator != ',')
969 ERROR("incomplete write memory binary packet received, dropping connection");
970 return ERROR_SERVER_REMOTE_CLOSED;
973 len = strtoul(separator+1, &separator, 16);
975 if (*(separator++) != ':')
977 ERROR("incomplete write memory binary packet received, dropping connection");
978 return ERROR_SERVER_REMOTE_CLOSED;
984 buffer = malloc(len);
986 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
988 memcpy( buffer, separator, len );
994 retval = target->type->write_memory(target, addr, 4, 1, buffer);
996 retval = target->type->write_memory(target, addr, 1, len, buffer);
1000 retval = target->type->write_memory(target, addr, 2, 1, buffer);
1002 retval = target->type->write_memory(target, addr, 1, len, buffer);
1006 retval = target->type->write_memory(target, addr, 1, len, buffer);
1009 retval = target_write_buffer(target, addr, len, buffer);
1016 if (retval == ERROR_OK)
1018 gdb_put_packet(connection, "OK", 2);
1022 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1029 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1036 if (packet_size > 1)
1038 packet[packet_size] = 0;
1039 address = strtoul(packet + 1, NULL, 16);
1046 if (packet[0] == 'c')
1049 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1051 else if (packet[0] == 's')
1054 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1058 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1062 case ERROR_TARGET_NOT_HALTED:
1063 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1064 return ERROR_SERVER_REMOTE_CLOSED;
1066 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1067 gdb_send_error(connection, EBUSY);
1070 ERROR("BUG: unexpected error %i", retval);
1077 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1080 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1081 enum watchpoint_rw wp_type;
1089 type = strtoul(packet + 1, &separator, 16);
1091 if (type == 0) /* memory breakpoint */
1092 bp_type = BKPT_SOFT;
1093 else if (type == 1) /* hardware breakpoint */
1094 bp_type = BKPT_HARD;
1095 else if (type == 2) /* write watchpoint */
1096 wp_type = WPT_WRITE;
1097 else if (type == 3) /* read watchpoint */
1099 else if (type == 4) /* access watchpoint */
1100 wp_type = WPT_ACCESS;
1102 if (*separator != ',')
1104 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1105 return ERROR_SERVER_REMOTE_CLOSED;
1108 address = strtoul(separator+1, &separator, 16);
1110 if (*separator != ',')
1112 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1113 return ERROR_SERVER_REMOTE_CLOSED;
1116 size = strtoul(separator+1, &separator, 16);
1122 if (packet[0] == 'Z')
1124 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1126 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1131 gdb_put_packet(connection, "OK", 2);
1136 breakpoint_remove(target, address);
1137 gdb_put_packet(connection, "OK", 2);
1144 if (packet[0] == 'Z')
1146 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1148 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1153 gdb_put_packet(connection, "OK", 2);
1158 watchpoint_remove(target, address);
1159 gdb_put_packet(connection, "OK", 2);
1170 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1172 command_context_t *cmd_ctx = connection->cmd_ctx;
1174 if (strstr(packet, "qRcmd,"))
1176 if (packet_size > 6)
1180 cmd = malloc((packet_size - 6)/2 + 1);
1181 for (i=0; i < (packet_size - 6)/2; i++)
1184 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1187 cmd[(packet_size - 6)/2] = 0x0;
1188 command_run_line(cmd_ctx, cmd);
1191 gdb_put_packet(connection, "OK", 2);
1195 if (strstr(packet, "qCRC:"))
1197 if (packet_size > 5)
1206 /* skip command character */
1209 addr = strtoul(packet, &separator, 16);
1211 if (*separator != ',')
1213 ERROR("incomplete read memory packet received, dropping connection");
1214 return ERROR_SERVER_REMOTE_CLOSED;
1217 len = strtoul(separator+1, NULL, 16);
1219 retval = target_checksum_memory(target, addr, len, &checksum);
1221 if (retval == ERROR_OK)
1223 snprintf(gdb_reply, 9, "C%2.2x", checksum);
1224 gdb_put_packet(connection, gdb_reply, 9);
1228 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1236 gdb_put_packet(connection, "", 0);
1240 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1242 gdb_connection_t *gdb_connection = connection->priv;
1243 gdb_service_t *gdb_service = connection->service->priv;
1246 if (strstr(packet, "vFlashErase:"))
1249 unsigned long length;
1250 char *parse = packet + 12;
1253 ERROR("incomplete vFlashErase packet received, dropping connection");
1254 return ERROR_SERVER_REMOTE_CLOSED;
1257 addr = strtoul(parse, &parse, 16);
1259 if (*(parse++) != ',' || *parse == '\0')
1261 ERROR("incomplete vFlashErase packet received, dropping connection");
1262 return ERROR_SERVER_REMOTE_CLOSED;
1265 length = strtoul(parse, &parse, 16);
1269 ERROR("incomplete vFlashErase packet received, dropping connection");
1270 return ERROR_SERVER_REMOTE_CLOSED;
1274 if ((result = flash_erase(gdb_service->target, addr, length)) != ERROR_OK)
1276 /* GDB doesn't evaluate the actual error number returned,
1277 * treat a failed erase as an I/O error
1279 gdb_send_error(connection, EIO);
1280 ERROR("flash_erase returned %i", result);
1283 gdb_put_packet(connection, "OK", 2);
1288 if (strstr(packet, "vFlashWrite:"))
1291 unsigned long length;
1292 char *parse = packet + 12;
1296 ERROR("incomplete vFlashErase packet received, dropping connection");
1297 return ERROR_SERVER_REMOTE_CLOSED;
1299 addr = strtoul(parse, &parse, 16);
1300 if (*(parse++) != ':')
1302 ERROR("incomplete vFlashErase packet received, dropping connection");
1303 return ERROR_SERVER_REMOTE_CLOSED;
1305 length = packet_size - (parse - packet);
1307 /* create a new image if there isn't already one */
1308 if (gdb_connection->vflash_image == NULL)
1310 gdb_connection->vflash_image = malloc(sizeof(image_t));
1311 image_open(gdb_connection->vflash_image, "", "build");
1314 /* create new section with content from packet buffer */
1315 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1317 gdb_put_packet(connection, "OK", 2);
1322 if (!strcmp(packet, "vFlashDone"))
1327 /* process the flashing buffer */
1328 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1330 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1331 gdb_put_packet(connection, "E.memtype", 9);
1333 gdb_send_error(connection, EIO);
1337 ERROR("flash writing failed: %s", error_str);
1343 DEBUG("wrote %u bytes from vFlash image to flash", written);
1344 gdb_put_packet(connection, "OK", 2);
1347 image_close(gdb_connection->vflash_image);
1348 free(gdb_connection->vflash_image);
1349 gdb_connection->vflash_image = NULL;
1354 gdb_put_packet(connection, "", 0);
1358 int gdb_detach(connection_t *connection, target_t *target)
1360 switch( detach_mode )
1362 case GDB_DETACH_RESUME:
1363 target->type->resume(target, 1, 0, 1, 0);
1366 case GDB_DETACH_RESET:
1367 target_process_reset(connection->cmd_ctx);
1370 case GDB_DETACH_HALT:
1371 target->type->halt(target);
1374 case GDB_DETACH_NOTHING:
1378 gdb_put_packet(connection, "OK", 2);
1383 int gdb_input(connection_t *connection)
1385 gdb_service_t *gdb_service = connection->service->priv;
1386 target_t *target = gdb_service->target;
1387 char packet[GDB_BUFFER_SIZE];
1390 gdb_connection_t *gdb_con = connection->priv;
1392 /* drain input buffer */
1395 packet_size = GDB_BUFFER_SIZE-1;
1396 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1400 case ERROR_GDB_BUFFER_TOO_SMALL:
1401 ERROR("BUG: buffer supplied for gdb packet was too small");
1403 case ERROR_SERVER_REMOTE_CLOSED:
1404 return ERROR_SERVER_REMOTE_CLOSED;
1406 ERROR("BUG: unexpected error");
1411 /* terminate with zero */
1412 packet[packet_size] = 0;
1414 DEBUG("recevied packet: '%s'", packet);
1416 if (packet_size > 0)
1422 /* Hct... -- set thread
1423 * we don't have threads, send empty reply */
1424 gdb_put_packet(connection, NULL, 0);
1427 retval = gdb_query_packet(connection, target, packet, packet_size);
1430 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1433 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1436 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1439 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1442 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1445 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1449 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1452 gdb_last_signal_packet(connection, target, packet, packet_size);
1456 gdb_step_continue_packet(connection, target, packet, packet_size);
1459 retval = gdb_v_packet(connection, target, packet, packet_size);
1462 retval = gdb_detach(connection, target);
1465 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1469 gdb_put_packet(connection, "OK", 2);
1470 return ERROR_SERVER_REMOTE_CLOSED;
1472 /* ignore unkown packets */
1473 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1474 gdb_put_packet(connection, NULL, 0);
1478 /* if a packet handler returned an error, exit input loop */
1479 if (retval != ERROR_OK)
1483 if (gdb_con->ctrl_c)
1485 if (target->state == TARGET_RUNNING)
1487 target->type->halt(target);
1488 gdb_con->ctrl_c = 0;
1492 } while (gdb_con->buf_cnt > 0);
1499 gdb_service_t *gdb_service;
1500 target_t *target = targets;
1505 WARNING("no gdb ports allocated as no target has been specified");
1511 WARNING("no gdb port specified, using default port 3333");
1517 char service_name[8];
1519 snprintf(service_name, 8, "gdb-%2.2i", i);
1521 gdb_service = malloc(sizeof(gdb_service_t));
1522 gdb_service->target = target;
1524 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1526 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1529 target = target->next;
1535 /* daemon configuration command gdb_port */
1536 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1541 /* only if the port wasn't overwritten by cmdline */
1543 gdb_port = strtoul(args[0], NULL, 0);
1548 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1552 if (strcmp(args[0], "resume") == 0)
1554 detach_mode = GDB_DETACH_RESUME;
1557 else if (strcmp(args[0], "reset") == 0)
1559 detach_mode = GDB_DETACH_RESET;
1562 else if (strcmp(args[0], "halt") == 0)
1564 detach_mode = GDB_DETACH_HALT;
1567 else if (strcmp(args[0], "nothing") == 0)
1569 detach_mode = GDB_DETACH_NOTHING;
1574 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1578 int gdb_register_commands(command_context_t *command_context)
1580 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1581 COMMAND_CONFIG, "");
1582 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
1583 COMMAND_CONFIG, "");