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;
47 int gdb_last_signal(target_t *target)
49 switch (target->debug_reason)
51 case DBG_REASON_DBGRQ:
52 return 0x2; /* SIGINT */
53 case DBG_REASON_BREAKPOINT:
54 case DBG_REASON_WATCHPOINT:
55 case DBG_REASON_WPTANDBKPT:
56 return 0x05; /* SIGTRAP */
57 case DBG_REASON_SINGLESTEP:
58 return 0x05; /* SIGTRAP */
59 case DBG_REASON_NOTHALTED:
60 return 0x0; /* no signal... shouldn't happen */
62 ERROR("BUG: undefined debug reason");
67 int gdb_get_char(connection_t *connection, int* next_char)
69 gdb_connection_t *gdb_con = connection->priv;
72 if (gdb_con->buf_cnt-- > 0)
74 *next_char = *(gdb_con->buf_p++);
75 if (gdb_con->buf_cnt > 0)
76 connection->input_pending = 1;
78 connection->input_pending = 0;
81 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
87 while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
89 if (gdb_con->buf_cnt == 0)
90 return ERROR_SERVER_REMOTE_CLOSED;
93 errno = WSAGetLastError();
100 case WSAECONNABORTED:
101 return ERROR_SERVER_REMOTE_CLOSED;
103 ERROR("read: %d", errno);
113 return ERROR_SERVER_REMOTE_CLOSED;
115 return ERROR_SERVER_REMOTE_CLOSED;
117 ERROR("read: %s", strerror(errno));
123 debug_buffer = malloc(gdb_con->buf_cnt + 1);
124 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
125 debug_buffer[gdb_con->buf_cnt] = 0;
126 DEBUG("received '%s'", debug_buffer);
129 gdb_con->buf_p = gdb_con->buffer;
131 *next_char = *(gdb_con->buf_p++);
132 if (gdb_con->buf_cnt > 0)
133 connection->input_pending = 1;
135 connection->input_pending = 0;
136 #ifdef _DEBUG_GDB_IO_
137 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
143 int gdb_putback_char(connection_t *connection, int last_char)
145 gdb_connection_t *gdb_con = connection->priv;
147 if (gdb_con->buf_p > gdb_con->buffer)
149 *(--gdb_con->buf_p) = last_char;
154 ERROR("BUG: couldn't put character back");
160 int gdb_put_packet(connection_t *connection, char *buffer, int len)
163 unsigned char my_checksum = 0;
168 gdb_connection_t *gdb_con = connection->priv;
170 for (i = 0; i < len; i++)
171 my_checksum += buffer[i];
176 debug_buffer = malloc(len + 1);
177 memcpy(debug_buffer, buffer, len);
178 debug_buffer[len] = 0;
179 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
182 write_socket(connection->fd, "$", 1);
184 write_socket(connection->fd, buffer, len);
185 write_socket(connection->fd, "#", 1);
187 snprintf(checksum, 3, "%2.2x", my_checksum);
189 write_socket(connection->fd, checksum, 2);
191 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
196 else if (reply == '-')
197 WARNING("negative reply, retrying");
198 else if (reply == 0x3)
201 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
205 else if (reply == '-')
206 WARNING("negative reply, retrying");
209 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
210 return ERROR_SERVER_REMOTE_CLOSED;
215 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
216 return ERROR_SERVER_REMOTE_CLOSED;
223 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
229 unsigned char my_checksum = 0;
230 gdb_connection_t *gdb_con = connection->priv;
236 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
239 DEBUG("character: '%c'", character);
246 WARNING("acknowledgment received, but no packet pending");
249 WARNING("negative acknowledgment, but no packet pending");
256 WARNING("ignoring character 0x%x", character);
259 } while (character != '$');
265 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
268 if (character == '#') break;
270 if (character == '}')
272 /* data transmitted in binary mode (X packet)
273 * uses 0x7d as escape character */
274 my_checksum += character & 0xff;
275 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
277 my_checksum += character & 0xff;
278 buffer[count++] = (character ^ 0x20) & 0xff;
282 my_checksum += character & 0xff;
283 buffer[count++] = character & 0xff;
288 ERROR("packet buffer too small");
289 return ERROR_GDB_BUFFER_TOO_SMALL;
295 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
297 checksum[0] = character;
298 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
300 checksum[1] = character;
303 if (my_checksum == strtoul(checksum, NULL, 16))
305 write_socket(connection->fd, "+", 1);
309 WARNING("checksum error, requesting retransmission");
310 write_socket(connection->fd, "-", 1);
316 int gdb_output(struct command_context_s *context, char* line)
318 connection_t *connection = context->output_handler_priv;
322 bin_size = strlen(line);
324 hex_buffer = malloc(bin_size*2 + 4);
327 for (i=0; i<bin_size; i++)
328 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
329 hex_buffer[bin_size*2+1] = '0';
330 hex_buffer[bin_size*2+2] = 'a';
331 hex_buffer[bin_size*2+3] = 0x0;
333 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
339 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
341 connection_t *connection = priv;
342 gdb_connection_t *gdb_connection = connection->priv;
348 case TARGET_EVENT_HALTED:
349 if (gdb_connection->frontend_state == TARGET_RUNNING)
351 if (gdb_connection->ctrl_c)
354 gdb_connection->ctrl_c = 0;
358 signal = gdb_last_signal(target);
361 snprintf(sig_reply, 4, "T%2.2x", signal);
362 gdb_put_packet(connection, sig_reply, 3);
363 gdb_connection->frontend_state = TARGET_HALTED;
366 case TARGET_EVENT_RESUMED:
367 if (gdb_connection->frontend_state == TARGET_HALTED)
369 gdb_connection->frontend_state = TARGET_RUNNING;
379 int gdb_new_connection(connection_t *connection)
381 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
382 gdb_service_t *gdb_service = connection->service->priv;
386 connection->priv = gdb_connection;
388 /* initialize gdb connection information */
389 gdb_connection->buf_p = gdb_connection->buffer;
390 gdb_connection->buf_cnt = 0;
391 gdb_connection->ctrl_c = 0;
392 gdb_connection->frontend_state = TARGET_HALTED;
393 gdb_connection->vflash_image = NULL;
395 /* output goes through gdb connection */
396 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
398 /* register callback to be informed about target events */
399 target_register_event_callback(gdb_target_callback_event_handler, connection);
401 /* a gdb session just attached, put the target in halt mode */
402 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
403 (retval != ERROR_TARGET_ALREADY_HALTED))
405 ERROR("error when trying to halt target");
409 while (gdb_service->target->state != TARGET_HALTED)
411 gdb_service->target->type->poll(gdb_service->target);
414 /* remove the initial ACK from the incoming buffer */
415 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
418 if (initial_ack != '+')
419 gdb_putback_char(connection, initial_ack);
424 int gdb_connection_closed(connection_t *connection)
426 gdb_service_t *gdb_service = connection->service->priv;
427 gdb_connection_t *gdb_connection = connection->priv;
429 /* see if an image built with vFlash commands is left */
430 if (gdb_connection->vflash_image)
432 image_close(gdb_connection->vflash_image);
433 free(gdb_connection->vflash_image);
434 gdb_connection->vflash_image = NULL;
437 /* if this connection registered a debug-message receiver delete it */
438 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
440 if (connection->priv)
441 free(connection->priv);
443 ERROR("BUG: connection->priv == NULL");
445 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
450 void gdb_send_error(connection_t *connection, u8 the_error)
453 snprintf(err, 4, "E%2.2X", the_error );
454 gdb_put_packet(connection, err, 3);
457 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
462 signal = gdb_last_signal(target);
464 snprintf(sig_reply, 4, "S%2.2x", signal);
465 gdb_put_packet(connection, sig_reply, 3);
470 void gdb_str_to_target(target_t *target, char *str, char *tstr)
472 int str_len = strlen(str);
477 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str);
481 if (target->endianness == TARGET_LITTLE_ENDIAN)
483 for (i = 0; i < str_len; i+=2)
485 tstr[str_len - i - 1] = str[i + 1];
486 tstr[str_len - i - 2] = str[i];
491 for (i = 0; i < str_len; i++)
498 void gdb_target_to_str(target_t *target, char *tstr, char *str)
500 int str_len = strlen(tstr);
505 ERROR("BUG: gdb value with uneven number of characters encountered");
509 if (target->endianness == TARGET_LITTLE_ENDIAN)
511 for (i = 0; i < str_len; i+=2)
513 str[str_len - i - 1] = tstr[i + 1];
514 str[str_len - i - 2] = tstr[i];
519 for (i = 0; i < str_len; i++)
526 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
531 int reg_packet_size = 0;
538 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
542 case ERROR_TARGET_NOT_HALTED:
543 ERROR("gdb requested registers but we're not halted, dropping connection");
544 return ERROR_SERVER_REMOTE_CLOSED;
546 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
547 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
552 for (i = 0; i < reg_list_size; i++)
554 reg_packet_size += reg_list[i]->size;
557 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
558 reg_packet_p = reg_packet;
560 for (i = 0; i < reg_list_size; i++)
562 char *hex_buf = buf_to_str(reg_list[i]->value, reg_list[i]->size, 16);
563 DEBUG("hex_buf: %s", hex_buf);
564 gdb_str_to_target(target, hex_buf, reg_packet_p);
565 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
569 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
570 DEBUG("reg_packet: %s", reg_packet_p);
573 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
581 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
591 /* skip command character */
597 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
598 return ERROR_SERVER_REMOTE_CLOSED;
601 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
605 case ERROR_TARGET_NOT_HALTED:
606 ERROR("gdb tried to registers but we're not halted, dropping connection");
607 return ERROR_SERVER_REMOTE_CLOSED;
609 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
610 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
616 for (i = 0; i < reg_list_size; i++)
620 reg_arch_type_t *arch_type;
622 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
623 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
624 gdb_target_to_str(target, packet_p, hex_buf);
626 /* convert hex-string to binary buffer */
627 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
628 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
630 /* get register arch_type, and call set method */
631 arch_type = register_get_arch_type(reg_list[i]->arch_type);
632 if (arch_type == NULL)
634 ERROR("BUG: encountered unregistered arch type");
637 arch_type->set(reg_list[i], bin_buf);
639 /* advance packet pointer */
640 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
646 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
649 gdb_put_packet(connection, "OK", 2);
654 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
657 int reg_num = strtoul(packet + 1, NULL, 16);
665 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
669 case ERROR_TARGET_NOT_HALTED:
670 ERROR("gdb requested registers but we're not halted, dropping connection");
671 return ERROR_SERVER_REMOTE_CLOSED;
673 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
674 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
679 if (reg_list_size <= reg_num)
681 ERROR("gdb requested a non-existing register");
685 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
687 hex_buf = buf_to_str(reg_list[reg_num]->value, reg_list[reg_num]->size, 16);
689 gdb_str_to_target(target, hex_buf, reg_packet);
691 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
700 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
705 int reg_num = strtoul(packet + 1, &separator, 16);
709 reg_arch_type_t *arch_type;
713 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
717 case ERROR_TARGET_NOT_HALTED:
718 ERROR("gdb tried to set a register but we're not halted, dropping connection");
719 return ERROR_SERVER_REMOTE_CLOSED;
721 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
722 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
727 if (reg_list_size < reg_num)
729 ERROR("gdb requested a non-existing register");
730 return ERROR_SERVER_REMOTE_CLOSED;
733 if (*separator != '=')
735 ERROR("GDB 'set register packet', but no '=' following the register number");
736 return ERROR_SERVER_REMOTE_CLOSED;
739 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
740 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
741 gdb_target_to_str(target, separator + 1, hex_buf);
743 /* convert hex-string to binary buffer */
744 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
745 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
747 /* get register arch_type, and call set method */
748 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
749 if (arch_type == NULL)
751 ERROR("BUG: encountered unregistered arch type");
754 arch_type->set(reg_list[reg_num], bin_buf);
756 gdb_put_packet(connection, "OK", 2);
765 int gdb_memory_packet_error(connection_t *connection, int retval)
769 case ERROR_TARGET_NOT_HALTED:
770 ERROR("gdb tried to read memory but we're not halted, dropping connection");
771 return ERROR_SERVER_REMOTE_CLOSED;
773 case ERROR_TARGET_DATA_ABORT:
774 gdb_send_error(connection, EIO);
776 case ERROR_TARGET_TRANSLATION_FAULT:
777 gdb_send_error(connection, EFAULT);
779 case ERROR_TARGET_UNALIGNED_ACCESS:
780 gdb_send_error(connection, EFAULT);
783 ERROR("BUG: unexpected error %i", retval);
790 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
802 /* skip command character */
805 addr = strtoul(packet, &separator, 16);
807 if (*separator != ',')
809 ERROR("incomplete read memory packet received, dropping connection");
810 return ERROR_SERVER_REMOTE_CLOSED;
813 len = strtoul(separator+1, NULL, 16);
815 buffer = malloc(len);
817 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
823 retval = target->type->read_memory(target, addr, 4, 1, buffer);
825 retval = target->type->read_memory(target, addr, 1, len, buffer);
829 retval = target->type->read_memory(target, addr, 2, 1, buffer);
831 retval = target->type->read_memory(target, addr, 1, len, buffer);
834 if (((addr % 4) == 0) && ((len % 4) == 0))
835 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
837 retval = target->type->read_memory(target, addr, 1, len, buffer);
840 if (retval == ERROR_OK)
842 hex_buffer = malloc(len * 2 + 1);
844 for (i=0; i<len; i++)
845 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
847 gdb_put_packet(connection, hex_buffer, len * 2);
853 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
862 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
873 /* skip command character */
876 addr = strtoul(packet, &separator, 16);
878 if (*separator != ',')
880 ERROR("incomplete write memory packet received, dropping connection");
881 return ERROR_SERVER_REMOTE_CLOSED;
884 len = strtoul(separator+1, &separator, 16);
886 if (*(separator++) != ':')
888 ERROR("incomplete write memory packet received, dropping connection");
889 return ERROR_SERVER_REMOTE_CLOSED;
892 buffer = malloc(len);
894 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
896 for (i=0; i<len; i++)
899 sscanf(separator + 2*i, "%2x", &tmp);
906 /* handle sized writes */
909 retval = target->type->write_memory(target, addr, 4, 1, buffer);
911 retval = target->type->write_memory(target, addr, 1, len, buffer);
915 retval = target->type->write_memory(target, addr, 2, 1, buffer);
917 retval = target->type->write_memory(target, addr, 1, len, buffer);
921 retval = target->type->write_memory(target, addr, 1, len, buffer);
923 /* handle bulk writes */
925 retval = target_write_buffer(target, addr, len, buffer);
929 if (retval == ERROR_OK)
931 gdb_put_packet(connection, "OK", 2);
935 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
944 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
953 /* skip command character */
956 addr = strtoul(packet, &separator, 16);
958 if (*separator != ',')
960 ERROR("incomplete write memory binary packet received, dropping connection");
961 return ERROR_SERVER_REMOTE_CLOSED;
964 len = strtoul(separator+1, &separator, 16);
966 if (*(separator++) != ':')
968 ERROR("incomplete write memory binary packet received, dropping connection");
969 return ERROR_SERVER_REMOTE_CLOSED;
975 buffer = malloc(len);
977 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
979 memcpy( buffer, separator, len );
985 retval = target->type->write_memory(target, addr, 4, 1, buffer);
987 retval = target->type->write_memory(target, addr, 1, len, buffer);
991 retval = target->type->write_memory(target, addr, 2, 1, buffer);
993 retval = target->type->write_memory(target, addr, 1, len, buffer);
997 retval = target->type->write_memory(target, addr, 1, len, buffer);
1000 retval = target_write_buffer(target, addr, len, buffer);
1007 if (retval == ERROR_OK)
1009 gdb_put_packet(connection, "OK", 2);
1013 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1020 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1027 if (packet_size > 1)
1029 packet[packet_size] = 0;
1030 address = strtoul(packet + 1, NULL, 16);
1037 if (packet[0] == 'c')
1040 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1042 else if (packet[0] == 's')
1045 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1049 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1053 case ERROR_TARGET_NOT_HALTED:
1054 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1055 return ERROR_SERVER_REMOTE_CLOSED;
1057 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1058 gdb_send_error(connection, EBUSY);
1061 ERROR("BUG: unexpected error %i", retval);
1068 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1071 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1072 enum watchpoint_rw wp_type;
1080 type = strtoul(packet + 1, &separator, 16);
1082 if (type == 0) /* memory breakpoint */
1083 bp_type = BKPT_SOFT;
1084 else if (type == 1) /* hardware breakpoint */
1085 bp_type = BKPT_HARD;
1086 else if (type == 2) /* write watchpoint */
1087 wp_type = WPT_WRITE;
1088 else if (type == 3) /* read watchpoint */
1090 else if (type == 4) /* access watchpoint */
1091 wp_type = WPT_ACCESS;
1093 if (*separator != ',')
1095 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1096 return ERROR_SERVER_REMOTE_CLOSED;
1099 address = strtoul(separator+1, &separator, 16);
1101 if (*separator != ',')
1103 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1104 return ERROR_SERVER_REMOTE_CLOSED;
1107 size = strtoul(separator+1, &separator, 16);
1113 if (packet[0] == 'Z')
1115 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1117 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1122 gdb_put_packet(connection, "OK", 2);
1127 breakpoint_remove(target, address);
1128 gdb_put_packet(connection, "OK", 2);
1135 if (packet[0] == 'Z')
1137 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1139 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1144 gdb_put_packet(connection, "OK", 2);
1149 watchpoint_remove(target, address);
1150 gdb_put_packet(connection, "OK", 2);
1161 void gdb_query_packet(connection_t *connection, char *packet, int packet_size)
1163 command_context_t *cmd_ctx = connection->cmd_ctx;
1165 if (strstr(packet, "qRcmd,"))
1167 if (packet_size > 6)
1171 cmd = malloc((packet_size - 6)/2 + 1);
1172 for (i=0; i < (packet_size - 6)/2; i++)
1175 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1178 cmd[(packet_size - 6)/2] = 0x0;
1179 command_run_line(cmd_ctx, cmd);
1182 gdb_put_packet(connection, "OK", 2);
1186 gdb_put_packet(connection, "", 0);
1189 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1191 gdb_connection_t *gdb_connection = connection->priv;
1192 gdb_service_t *gdb_service = connection->service->priv;
1195 if (strstr(packet, "vFlashErase:"))
1198 unsigned long length;
1199 char *parse = packet + 12;
1202 ERROR("incomplete vFlashErase packet received, dropping connection");
1203 return ERROR_SERVER_REMOTE_CLOSED;
1206 addr = strtoul(parse, &parse, 16);
1208 if (*(parse++) != ',' || *parse == '\0')
1210 ERROR("incomplete vFlashErase packet received, dropping connection");
1211 return ERROR_SERVER_REMOTE_CLOSED;
1214 length = strtoul(parse, &parse, 16);
1218 ERROR("incomplete vFlashErase packet received, dropping connection");
1219 return ERROR_SERVER_REMOTE_CLOSED;
1223 if ((result = flash_erase(gdb_service->target, addr, length)) != ERROR_OK)
1225 /* GDB doesn't evaluate the actual error number returned,
1226 * treat a failed erase as an I/O error
1228 gdb_send_error(connection, EIO);
1229 ERROR("flash_erase returned %i", result);
1232 gdb_put_packet(connection, "OK", 2);
1237 if (strstr(packet, "vFlashWrite:"))
1240 unsigned long length;
1241 char *parse = packet + 12;
1245 ERROR("incomplete vFlashErase packet received, dropping connection");
1246 return ERROR_SERVER_REMOTE_CLOSED;
1248 addr = strtoul(parse, &parse, 16);
1249 if (*(parse++) != ':')
1251 ERROR("incomplete vFlashErase packet received, dropping connection");
1252 return ERROR_SERVER_REMOTE_CLOSED;
1254 length = packet_size - (parse - packet);
1256 /* create a new image if there isn't already one */
1257 if (gdb_connection->vflash_image == NULL)
1259 gdb_connection->vflash_image = malloc(sizeof(image_t));
1260 image_open(gdb_connection->vflash_image, "", "build");
1263 /* create new section with content from packet buffer */
1264 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1266 gdb_put_packet(connection, "OK", 2);
1271 if (!strcmp(packet, "vFlashDone"))
1276 /* process the flashing buffer */
1277 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1279 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1280 gdb_put_packet(connection, "E.memtype", 9);
1282 gdb_send_error(connection, EIO);
1286 ERROR("flash writing failed: %s", error_str);
1292 DEBUG("wrote %u bytes from vFlash image to flash", written);
1293 gdb_put_packet(connection, "OK", 2);
1296 image_close(gdb_connection->vflash_image);
1297 free(gdb_connection->vflash_image);
1298 gdb_connection->vflash_image = NULL;
1303 gdb_put_packet(connection, "", 0);
1307 int gdb_input(connection_t *connection)
1309 gdb_service_t *gdb_service = connection->service->priv;
1310 target_t *target = gdb_service->target;
1311 char packet[GDB_BUFFER_SIZE];
1314 gdb_connection_t *gdb_con = connection->priv;
1316 /* drain input buffer */
1319 packet_size = GDB_BUFFER_SIZE-1;
1320 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1324 case ERROR_GDB_BUFFER_TOO_SMALL:
1325 ERROR("BUG: buffer supplied for gdb packet was too small");
1327 case ERROR_SERVER_REMOTE_CLOSED:
1328 return ERROR_SERVER_REMOTE_CLOSED;
1330 ERROR("BUG: unexpected error");
1335 /* terminate with zero */
1336 packet[packet_size] = 0;
1338 DEBUG("recevied packet: '%s'", packet);
1340 if (packet_size > 0)
1346 /* Hct... -- set thread
1347 * we don't have threads, send empty reply */
1348 gdb_put_packet(connection, NULL, 0);
1351 gdb_query_packet(connection, packet, packet_size);
1354 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1357 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1360 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1363 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1366 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1369 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1373 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1376 gdb_last_signal_packet(connection, target, packet, packet_size);
1380 gdb_step_continue_packet(connection, target, packet, packet_size);
1383 retval = gdb_v_packet(connection, target, packet, packet_size);
1386 target->type->resume(target, 1, 0, 1, 0);
1387 gdb_put_packet(connection, "OK", 2);
1390 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1394 gdb_put_packet(connection, "OK", 2);
1395 return ERROR_SERVER_REMOTE_CLOSED;
1397 /* ignore unkown packets */
1398 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1399 gdb_put_packet(connection, NULL, 0);
1403 /* if a packet handler returned an error, exit input loop */
1404 if (retval != ERROR_OK)
1408 if (gdb_con->ctrl_c)
1410 if (target->state == TARGET_RUNNING)
1412 target->type->halt(target);
1413 gdb_con->ctrl_c = 0;
1417 } while (gdb_con->buf_cnt > 0);
1424 gdb_service_t *gdb_service;
1425 target_t *target = targets;
1430 WARNING("no gdb ports allocated as no target has been specified");
1436 WARNING("no gdb port specified, using default port 3333");
1442 char service_name[8];
1444 snprintf(service_name, 8, "gdb-%2.2i", i);
1446 gdb_service = malloc(sizeof(gdb_service_t));
1447 gdb_service->target = target;
1449 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1451 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1454 target = target->next;
1460 /* daemon configuration command gdb_port */
1461 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1466 /* only if the port wasn't overwritten by cmdline */
1468 gdb_port = strtoul(args[0], NULL, 0);
1473 int gdb_register_commands(command_context_t *command_context)
1475 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1476 COMMAND_CONFIG, "");